NCBI C++ ToolKit
seqfetch_queue.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: seqfetch_queue.cpp 33210 2015-06-17 20:37:46Z katargir $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Mike DiCuccio
27  *
28  * File Description:
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
33 #include <corelib/ncbimtx.hpp>
34 
36 
37 #include <objmgr/scope.hpp>
38 #include <objmgr/bioseq_handle.hpp>
39 #include <objmgr/seq_map_ci.hpp>
40 #include <objmgr/feat_ci.hpp>
42 #include <objmgr/util/sequence.hpp>
43 
44 
47 
48 
49 /////////////////////////////////////////////////////////////////////////////
50 ///
51 /// Siple fatal request used for terminating processing
52 ///
54 {
55 public:
56  void Process()
57  {
58  CThread::Exit(0);
59  }
60 };
61 
62 
63 /////////////////////////////////////////////////////////////////////////////
64 ///
65 /// Wrapper class for a resolution request
66 ///
67 
69 {
70 public:
72  const TSeqRange& range,
74  CSeqFetchQueue& parent_queue,
75  int priority);
76 
77  void Process();
78 
79 private:
86 };
87 
88 
89 
91  const TSeqRange& range,
93  CSeqFetchQueue& parent_queue,
94  int priority)
95  : m_Scope(&scope)
96  , m_Id(idh)
97  , m_Range(range)
98  , m_Flags(flags)
99  , m_Queue(parent_queue)
100  , m_Priority(priority)
101 {
102 }
103 
104 
106 {
107  string id_str = m_Id.GetSeqId()->GetSeqIdString();
108 
109  /// we always get the bioseq handle
111 
112  /// also, always resolve all IDs
113  m_Scope->GetIds(m_Id);
114 
115 
118  ///
119  /// retrieve all = retrieve complete bioseq
120  /// we also ignore the range here
121  ///
122  bsh.GetCompleteBioseq();
123 
125  ///
126  /// force retrieving of all features
127  /// we exclude external annotations here
128  /// we only retrieve features in the selected range
129  ///
130  SAnnotSelector sel;
131  sel.SetResolveAll()
133  .SetAdaptiveDepth();
134  if (m_Range.IsWhole()) {
135  CFeat_CI feat_iter(bsh, sel);
136  } else {
137  CFeat_CI feat_iter(bsh, m_Range, sel);
138  }
139 
141  ///
142  /// retrieve a "core" set of features
143  /// this includes genes, mRNAs, and CDS features
144  /// we only retrieve features in the selected range
145  ///
146  SAnnotSelector sel;
147  sel.SetResolveAll()
153  if (m_Range.IsWhole()) {
154  CFeat_CI feat_iter(bsh, sel);
155  } else {
156  CFeat_CI feat_iter(bsh, m_Range, sel);
157  }
158  }
159 
160  /// we plan to enqueue more items; enqueue them at a lower priority to
161  /// make sure we finish the previous level first
162  int next_priority = max(0, m_Priority - 1);
163 
164  ///
165  /// retrieve producs, but only those on this sequence
166  ///
168  SAnnotSelector sel;
169  sel
170  .SetResolveNone()
174 
175  if (m_Range.IsWhole()) {
176  CFeat_CI feat_iter(bsh, sel);
177  for ( ; feat_iter; ++feat_iter) {
178  if ( !feat_iter->IsSetProduct() ) {
179  continue;
180  }
181  CSeq_id_Handle idh =
183  m_Queue.Add(idh, m_Range, m_Flags, next_priority);
184  }
185  } else {
187  CFeat_CI feat_iter(bsh, m_Range, sel);
188  for ( ; feat_iter; ++feat_iter) {
189  if ( !feat_iter->IsSetProduct() ) {
190  continue;
191  }
192  CSeq_id_Handle idh =
194 
195  /// we ignore the range on descent
196  m_Queue.Add(idh, whole, m_Flags, next_priority);
197  }
198  }
199  }
200 
201  ///
202  /// descend recursively through the seq-map at this level to retrieve
203  /// the main sequence here
204  ///
206  SSeqMapSelector sel;
208  .SetResolveCount(0);
209 
210  if (m_Range.IsWhole()) {
211  CSeqMap_CI seqmap_iter(bsh, sel);
212  for ( ; seqmap_iter; ++seqmap_iter) {
213  CSeq_id_Handle idh = seqmap_iter.GetRefSeqid();
214  if (idh) {
215  m_Queue.Add(idh, m_Range, m_Flags, next_priority);
216  }
217  }
218  } else {
220  CSeqMap_CI seqmap_iter(bsh, sel, m_Range.GetFrom());
221  for ( ; seqmap_iter; ++seqmap_iter) {
222  CSeq_id_Handle idh = seqmap_iter.GetRefSeqid();
223  if (idh) {
224  /// we ignore the range on descent
225  m_Queue.Add(idh, whole, m_Flags, next_priority);
226  }
227 
228  if (seqmap_iter.GetEndPosition() > m_Range.GetTo()) {
229  break;
230  }
231  }
232  }
233  }
234  /**
235  LOG_POST(Error << "retrieved: " << m_Id.GetSeqId()->GetSeqIdString()
236  << ": " << m_Flags << " / " << m_Priority);
237  **/
238 }
239 
240 
241 
242 /////////////////////////////////////////////////////////////////////////////
243 ///
244 /// CSeqFetchQueue implementation details
245 ///
246 
247 
248 CSeqFetchQueue::CSeqFetchQueue(objects::CScope& scope,
249  int threads, int max_queue_size)
250  : CStdPoolOfThreads(threads, max_queue_size)
251  , m_Scope(&scope)
252  , m_IsShuttingDown(false)
253 {
254 }
255 
256 
258 {
259  static CRef<CSeqFetchQueue> s_Queue;
260  if ( !s_Queue ) {
261  DEFINE_STATIC_MUTEX(s_Mutex);
262  CMutexGuard LOCK(s_Mutex);
263  if ( !s_Queue ) {
265  CRef<CScope> scope(new CScope(*om));
266  scope->AddDefaults();
267  s_Queue.Reset(new CSeqFetchQueue(*scope, 2));
268  }
269  }
270 
271  return *s_Queue;
272 }
273 
274 
276 {
277  return m_Queue.IsFull();
278 }
279 
280 
282 {
283  return m_Queue.IsEmpty();
284 }
285 
286 
288 {
289  m_IsShuttingDown = true;
290  while ( !IsEmpty() ) {
291  m_Queue.GetHandle();
292  }
293  Finish();
294 }
295 
296 
298 {
299  m_IsShuttingDown = true;
301  AcceptRequest(req);
302  while ( !IsEmpty() ) {
303  }
304  KillAllThreads(true);
305 }
306 
307 
309  const TSeqRange& range,
310  TRetrievalFlags flags,
311  int priority)
312 {
313  if (m_IsShuttingDown) {
314  return;
315  }
316 
318  (new CResolveRequest(*m_Scope, idh, range, flags, *this, priority));
319  AcceptRequest(req, priority);
320 }
321 
322 
323 /// enqueue an ID for retrieval
324 void CSeqFetchQueue::Add(const CSeq_id& id,
325  TRetrievalFlags flags)
326 {
327  if (m_IsShuttingDown) {
328  return;
329  }
330 
333  Add(idh, range, flags, 128);
334 }
335 
336 
337 void CSeqFetchQueue::Add(const CSeq_id& id,
338  const TSeqRange& range,
339  TRetrievalFlags flags)
340 {
341  if (m_IsShuttingDown) {
342  return;
343  }
344 
346  Add(idh, range, flags, 128);
347 }
348 
349 
350 /// enqueue an ID for retrieval
351 void CSeqFetchQueue::Add(objects::CSeq_id_Handle id,
353 {
354  if (m_IsShuttingDown) {
355  return;
356  }
357 
359  Add(id, range, flags, 128);
360 }
361 
362 
363 
364 /// Enqueue a set of IDs for retrieval
365 void CSeqFetchQueue::Add(const vector<CSeq_id_Handle>& ids,
366  TRetrievalFlags flags)
367 {
368  if (m_IsShuttingDown) {
369  return;
370  }
371 
373  ITERATE (vector<CSeq_id_Handle>, iter, ids) {
374  Add(*iter, range, flags, 128);
375  }
376 }
377 
378 
379 /// Enqueue a set of gis for retrieval
380 void CSeqFetchQueue::Add(const vector<TGi>& ids,
382 {
383  if (m_IsShuttingDown) {
384  return;
385  }
386 
388  ITERATE (vector<TGi>, iter, ids) {
390  Add(idh, range, flags, 128);
391  }
392 }
393 
394 
static CRef< CScope > m_Scope
#define false
Definition: bool.h:36
CBioseq_Handle –.
CFeat_CI –.
Definition: feat_ci.hpp:64
Siple fatal request used for terminating processing.
void Process()
Do the actual job Called by whichever thread handles this request.
Wrapper class for a resolution request.
CSeqFetchQueue & m_Queue
void Process()
Do the actual job Called by whichever thread handles this request.
CSeq_id_Handle m_Id
CSeqFetchQueue::TRetrievalFlags m_Flags
CResolveRequest(CScope &scope, CSeq_id_Handle idh, const TSeqRange &range, CSeqFetchQueue::TRetrievalFlags flags, CSeqFetchQueue &parent_queue, int priority)
CRef< CScope > m_Scope
CScope –.
Definition: scope.hpp:92
friend class CResolveRequest
void Finish()
Finish processing of all items in the queue.
void Clear()
Clear all items from the queue.
void Add(const objects::CSeq_id &id, TRetrievalFlags flags=fDefaults)
Enqueue a single ID for retrieval.
bool IsEmpty() const
bool m_IsShuttingDown
flag: set to true when the class is shutting down this blocks all further acceptances
CSeqFetchQueue(objects::CScope &scope, int threads=4, int max_queue_size=100000)
CSeqFetchQueue implementation details.
CRef< objects::CScope > m_Scope
@ fRetrieveProducts
should we enqueue product retrievals as well? this implies retrieval of mRNA and CDS features
@ fRetrieveRecursive
should we enqueue all items recursively? levels below the requested level are retrieved with a lower ...
@ fRetrieveFeatures
retrieve all features not in external annotations
@ fRetrieveCoreFeatures
retrieve the "core" features this includes genes, mRNAs, and CDSs only
bool IsFull() const
Ask if the queue is full or empty.
static CSeqFetchQueue & GetInstance()
static interface to access singleton
Iterator over CSeqMap.
Definition: seq_map_ci.hpp:252
static uch flags
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
string GetSeqIdString(bool with_version=false) const
Return seqid string with optional version for text seqid type.
Definition: Seq_id.cpp:2144
CConstRef< CSeq_id > GetSeqId(void) const
static CSeq_id_Handle GetGiHandle(TGi gi)
Faster way to create a handle for a gi.
static CSeq_id_Handle GetHandle(const CSeq_id &id)
Normal way of getting a handle, works for any seq-id.
CSeq_id_Handle GetIdHandle(const CSeq_loc &loc, CScope *scope)
TIds GetIds(const CSeq_id &id, TGetFlags flags=0)
Get "native" bioseq ids without filtering and matching.
Definition: scope.cpp:401
static CRef< CObjectManager > GetInstance(void)
Return the existing object manager or create one.
CBioseq_Handle GetBioseqHandle(const CSeq_id &id)
Get bioseq handle by seq-id.
Definition: scope.cpp:95
void AddDefaults(TPriority pri=kPriority_Default)
Add default data loaders from object manager.
Definition: scope.cpp:504
CConstRef< CBioseq > GetCompleteBioseq(void) const
Get the complete bioseq.
bool IsSetProduct(void) const
TSeqPos GetEndPosition(void) const
return end position of current segment in sequence (exclusive)
Definition: seq_map_ci.hpp:679
SSeqMapSelector & SetResolveCount(size_t res_cnt)
Set max depth of resolving seq-map.
Definition: seq_map_ci.hpp:151
SAnnotSelector & IncludeFeatSubtype(TFeatSubtype subtype)
Include feature subtype in the search.
SAnnotSelector & SetResolveAll(void)
SetResolveAll() is equivalent to SetResolveMethod(eResolve_All).
SSeqMapSelector & SetFlags(TFlags flags)
Select segment type(s)
Definition: seq_map_ci.hpp:179
SAnnotSelector & SetAdaptiveDepth(bool value=true)
SetAdaptiveDepth() requests to restrict subsegment resolution depending on annotations found on lower...
SAnnotSelector & SetExcludeExternal(bool exclude=true)
External annotations for the Object Manger are annotations located in top level Seq-entry different f...
const CSeq_loc & GetProduct(void) const
CSeq_id_Handle GetRefSeqid(void) const
The following function makes sense only when the segment is a reference to another seq.
Definition: seq_map_ci.cpp:312
SAnnotSelector & SetResolveNone(void)
SetResolveNone() is equivalent to SetResolveMethod(eResolve_None).
@ fFindRef
Definition: seq_map.hpp:137
void Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:773
bool IsWhole(void) const
Definition: range.hpp:284
static TThisType GetWhole(void)
Definition: range.hpp:272
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
virtual void KillAllThreads(TKillFlags flags)
Causes all threads in the pool to exit cleanly after finishing all pending requests,...
TItemHandle AcceptRequest(const CRef< CStdRequest > &request, TUserPriority priority=0, unsigned int timeout_sec=0, unsigned int timeout_nsec=0)
Put a request in the queue with a given priority.
bool IsEmpty(void) const
Check if the queue is empty.
bool IsFull(void) const
Check if the queue is full.
TItemHandle GetHandle(unsigned int timeout_sec=kMax_UInt, unsigned int timeout_nsec=0)
Get the first available request from the queue, and return a handle to it.
static void Exit(void *exit_data)
Cancel current thread.
Definition: ncbithr.cpp:906
#define DEFINE_STATIC_MUTEX(id)
Define static mutex and initialize it.
Definition: ncbimtx.hpp:512
TTo GetTo(void) const
Get the To member data.
Definition: Range_.hpp:269
TFrom GetFrom(void) const
Get the From member data.
Definition: Range_.hpp:222
where boath are integers</td > n< td ></td > n</tr > n< tr > n< td > tse</td > n< td > optional</td > n< td > String</td > n< td class=\"description\"> TSE option controls what blob is whole
range(_Ty, _Ty) -> range< _Ty >
Multi-threading – mutexes; rw-locks; semaphore.
T max(T x_, T y_)
The Object manager core.
USING_SCOPE(objects)
CRef< objects::CObjectManager > om
SAnnotSelector –.
Selector used in CSeqMap methods returning iterators.
Definition: seq_map_ci.hpp:113
Modified on Thu Nov 30 04:52:44 2023 by modify_doxy.py rev. 669887