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

Go to the SVN repository for this file.

1 /* $Id: sparse_multi_ds.cpp 45564 2020-09-08 19:05:27Z shkeda $
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: Andrey Yazhuk
27  *
28  * File Description:
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
33 
39 
40 
43 
44 ///////////////////////////////////////////////////////////////////////////////
45 /// CSparseMultiDataSource
46 
49  &CSparseMultiDataSource::OnAppJobNotification)
51  &CSparseMultiDataSource::OnAppJobNotification)
53 
54 
56 {
57 }
58 
59 
61 {
63  try {
64  if(x_IsJobRunning()) {
65  x_DeleteJob();
66  }
67  }
68  catch (CException& e) {
69  LOG_POST(Error << "CSparseMultiDataSource::~CSparseMultiDataSource(): "
70  << "failed to delete job: " << e.GetMsg());
71  }
72 
73 }
74 
75 
77 {
78  return m_Alignment == NULL;
79 }
80 
81 
83 {
84  CAppJobNotification* notn = dynamic_cast<CAppJobNotification*>(evt);
85  _ASSERT(notn);
86 
87  if(notn) {
88  int job_id = notn->GetJobID();
89  if(m_JobID != job_id) {
90  ERR_POST("CSparseMultiDataSource::OnAppJobNotification() - unknown Job ID " << job_id);
91  } else {
92  switch(notn->GetState()) {
93  case IAppJob::eCompleted: {
94  CBuildSparseAlnResult* res = dynamic_cast<CBuildSparseAlnResult*>(notn->GetResult().GetPointer());
95  _ASSERT(res);
96  m_Alignment = res->m_SparseAln;
99  NotifyListener(up);
100  m_Job.Reset();
101  break;
102  }
103  case IAppJob::eFailed: {
105  CUpdate up(CDataChangeNotifier::eError, error->GetText());
106  NotifyListener(up);
107  }
108  case IAppJob::eCanceled: {
109  CUpdate up(CDataChangeNotifier::eError, "Canceled");
110  NotifyListener(up);
111  break;
112  }
113  case IAppJob::eRunning: {
114  CConstIRef<IAppJobProgress> progressSoFar = notn->GetProgress();
115  if (progressSoFar) {
117  progressSoFar->GetText(), progressSoFar->GetNormDone());
118  NotifyListener(up);
119  }
120  break;
121  }
122  default:
123  _ASSERT(false);
124  }
125  }
126  }
127 }
128 
129 
130 
132 {
133  m_Alignment->SetGapChar(gap_char);
134 }
135 
136 
138 {
139  m_Alignment.Reset(&align);
140  x_CreateHandles();
141 }
142 
143 
145  const CAlnUserOptions& options,
146  bool sync)
147 {
148  _ASSERT( ! m_Job);
150 
151  m_Job.Reset(new CBuildSparseAlnJob(aligns, options, *m_Scope));
152 
153  if(sync) {
154  // do everything synchronously
155  m_JobID = -1;
157 
158  if(m_Job->Run()) {
161  if (!res) {
162  NCBI_THROW(CCoreException, eNullPtr, "Cannot get merge results: AlnVec is empty");
163  }
164  _ASSERT(res);
165  m_Alignment = res->m_SparseAln;
166  x_CreateHandles();
167  }
168 
169  //m_HasScores = x_TransferResults(m_Job->GetResult().GetPointer());
170  m_Job.Reset();
171 
172  CUpdate up(type, "");
173  NotifyListener(up);
174  } else {
175  // use CAppJobDispatcher to execute background jobs
176  m_JobID = disp.StartJob(*m_Job, "ThreadPool", *this, 1, true);
177  }
178 }
179 
180 
182 {
183  return -1;//x_GetConsensusRow();
184 }
185 
186 
188 {
189  return false;
190 }
191 
192 
194 {
195  _ASSERT(false); // not supported
196 }
197 
198 
200 {
202  if( ! m_Alignment) {
203  return type;
204  }
205 
206  CRef<CScope> scope = m_Alignment->GetScope();
208  for (CSparseAln::TNumrow i = 0; i < rows; ++i) {
211  if (bsh) {
212  if (bsh.IsNucleotide()) {
213  this_type = IAlnExplorer::fDNA;
214  } else {
215  this_type = IAlnExplorer::fProtein;
216  }
217  }
218 
219  switch (this_type) {
221  return this_type;
222 
223  default:
224  if (type == IAlnExplorer::fInvalid) {
225  type = this_type;
226  } else if (type != this_type) {
227  return IAlnExplorer::fMixed;
228  }
229  }
230  }
231 
232  return type;
233 }
234 
235 
237 {
238  return m_Alignment->GetNumRows();
239 }
240 
241 
243 {
244  return m_Alignment->GetAlnRange().GetFrom();
245 }
246 
247 
249 {
250  return m_Alignment->GetAlnRange().GetTo();
251 }
252 
253 
255 {
256  return m_Alignment->GetSeqStart(row);
257 }
258 
259 
261 {
262  return m_Alignment->GetSeqStop(row);
263 }
264 
265 
267 {
268  return m_Alignment->IsSetAnchor();
269 }
270 
271 
273 {
274  return m_Alignment->GetAnchor();
275 }
276 
277 
279 {
280  return false;
281 }
282 
283 
285 {
286  _ASSERT(false); // not supported
287  return false;
288 }
289 
290 
292 {
294  return false;
295 }
296 
297 
299 {
300  return true;
301 }
302 
303 
305 {
306  return m_Alignment->GetSeqId(row);
307 }
308 
309 
311 {
312  return m_Alignment->IsPositiveStrand(row);
313 }
314 
315 
317 {
318  return m_Alignment->IsNegativeStrand(row);
319 }
320 
321 
324  bool try_reverse_dir) const
325 {
326  return m_Alignment->GetAlnPosFromSeqPos(row, aln_pos, dir, try_reverse_dir);
327 }
328 
329 
332  bool try_reverse_dir) const
333 {
334  return m_Alignment->GetSeqPosFromAlnPos(row, seq_pos, dir, try_reverse_dir);
335 }
336 
337 
341  IAlnSegmentIterator::EFlags flag) const
342 {
343  return m_Alignment->CreateSegmentIterator(row, range, flag);
344 }
345 
346 
348 {
349  return true;
350 }
351 
352 
354  string &buffer,
355  const IAlnExplorer::TRange &seq_range) const
356 {
357  m_Alignment->GetSeqString(row, buffer, seq_range);
358  return buffer;
359 }
360 
361 
363  string &buffer,
364  const IAlnExplorer::TSignedRange& aln_range) const
365 {
366  m_Alignment->GetAlnSeqString(row, buffer, aln_range);
367  return buffer;
368 }
369 
370 
372 {
373  return true;
374 }
375 
376 
378 {
379  return m_Alignment->GetBioseqHandle(row);
380 }
381 
382 
384 {
385  return m_Handles[row];
386 }
387 
389 {
390  return m_Alignment->GetBaseWidth(row);
391 }
392 
394 {
395  return GetRowHandle(row)->GetGenCode();
396 }
397 
398 
400  TNumrow row, const TRangeColl& seq_coll, TRangeColl& aln_coll
401 ) const {
402  //ERR_POST("CSparseMultiDataSource::GetAlnFromSeq() - not implemented yet");
403 
404  aln_coll.empty();
405 
406  TSeqPos seq_start = m_Alignment->GetSeqStart( row );
407  TSeqPos seq_stop = m_Alignment->GetSeqStop( row );
408  if( seq_start > seq_stop ){
409  swap( seq_start, seq_stop );
410  }
411 
412  ITERATE( TRangeColl, it, seq_coll ){
413  // clip collection by alignment
414  TSeqPos from = max( seq_start, it->GetFrom() );
415  TSeqPos to = min( seq_stop, it->GetTo() );
416  // translate
419  if( aln_from > aln_to ){
420  swap( aln_from, aln_to );
421  }
422 
423  aln_coll.CombineWith( TSeqRange( aln_from, aln_to ) );
424  }
425 
426 }
427 
428 
430  TRangeColl& seq_coll) const
431 {
432  seq_coll.empty();
433 
434  TSeqPos aln_start = m_Alignment->GetSeqAlnStart( row );
435  TSeqPos aln_stop = m_Alignment->GetSeqAlnStop( row );
436 
437  ITERATE( TRangeColl, it, aln_coll ){
438  // clip collection by alignment
439  TSeqPos aln_from = max( aln_start, it->GetFrom() );
440  TSeqPos aln_to = min( aln_stop, it->GetTo() );
441 
442  // translate
445 
446  if( from > to ){
447  swap( from, to );
448  }
449 
450  seq_coll.CombineWith( TSeqRange( from, to ) );
451  }
452 }
453 
455 {
457  delete *it;
458  }
459  m_Handles.clear();
460 }
461 
462 
464 {
465  TNumrow row_n = m_Alignment->GetNumRows();
466  m_Handles.resize(row_n);
467 
468  for( TNumrow r = 0; r < row_n; r++ ) {
470  }
471 }
472 
474 {
475  return m_Job.GetPointer() != NULL;
476 }
477 
478 
480 {
481  _ASSERT(m_Job && m_JobID != -1);
482 
484  disp.DeleteJob(m_JobID);
485 
486  m_JobID = -1;
487  m_Job.Reset();
488 
489  //TODO update listener ?
490 }
491 
492 
494 {
496  if(prg) {
497  string text = prg->GetText();
498 
499  if(x_IsJobRunning()) {
501  NotifyListener(up);
502  }
503  }
504 }
505 
static CRef< CScope > m_Scope
Options for different alignment manager operations.
CAppJobDispatcher.
CAppJobNotification Notification send by CAppJobEventTranslator.
CBioseq_Handle –.
CBuildSparseAlnJob.
virtual EJobState Run()
Function that does all the useful work, called by the Engine.
virtual CRef< CObject > GetResult()
Returns the Job Result.
CRef< CSparseAln > m_SparseAln
CCoreException –.
Definition: ncbiexpt.hpp:1476
CUpdate - notification send by CUIDataSource to the listener.
virtual void NotifyListener(CUpdate &update)
CEventHandler.
CEvent - generic event implementation TODO TODO - Attachments.
Definition: event.hpp:86
TThisType & CombineWith(const TRange &r)
Definition: range_coll.hpp:195
bool empty() const
Definition: range_coll.hpp:102
CScope –.
Definition: scope.hpp:92
Sparse alignment.
Definition: sparse_aln.hpp:51
bool IsSetAnchor(void) const
Check if anchor is set - always true for sparse alignments.
Definition: sparse_aln.hpp:92
TSignedSeqPos GetSeqAlnStart(TNumrow row) const
Definition: sparse_aln.cpp:271
TRng GetAlnRange(void) const
Get whole alignment range.
Definition: sparse_aln.cpp:238
TNumrow GetNumRows(void) const
Synonym of the above.
Definition: sparse_aln.hpp:80
int GetBaseWidth(TNumrow row) const
Get base width for the sequence (1 for nucleotides, 3 for proteins).
Definition: sparse_aln.hpp:225
TSignedSeqPos GetSeqAlnStop(TNumrow row) const
Definition: sparse_aln.cpp:278
void SetGapChar(TResidue gap_char)
Gap character modifier.
Definition: sparse_aln.cpp:244
TSeqPos GetSeqStop(TNumrow row) const
Definition: sparse_aln.cpp:299
virtual IAlnSegmentIterator * CreateSegmentIterator(TNumrow row, const TSignedRange &range, IAlnSegmentIterator::EFlags flags) const
Create segment iterator.
Definition: sparse_aln.cpp:710
string & GetAlnSeqString(TNumrow row, string &buffer, const TSignedRange &rq_aln_rng, bool force_translation=false) const
Fetch alignment sequence data.
Definition: sparse_aln.cpp:552
TSignedSeqPos GetAlnPosFromSeqPos(TNumrow row, TSeqPos seq_pos, ESearchDirection dir=eNone, bool try_reverse_dir=true) const
Map sequence position to alignment coordinates.
Definition: sparse_aln.cpp:373
TSignedSeqPos GetSeqPosFromAlnPos(TNumrow for_row, TSeqPos aln_pos, ESearchDirection dir=eNone, bool try_reverse_dir=true) const
Definition: sparse_aln.cpp:385
bool IsPositiveStrand(TNumrow row) const
Check direction of the row.
Definition: sparse_aln.cpp:313
const objects::CBioseq_Handle & GetBioseqHandle(TNumrow row) const
Get bioseq handle for the row.
Definition: sparse_aln.cpp:396
TNumrow GetAnchor(void) const
Get anchor row index.
Definition: sparse_aln.hpp:95
TSeqPos GetSeqStart(TNumrow row) const
Definition: sparse_aln.cpp:292
CRef< objects::CScope > GetScope(void) const
Scope accessor.
Definition: sparse_aln.cpp:250
string & GetSeqString(TNumrow row, string &buffer, TSeqPos seq_from, TSeqPos seq_to, bool force_translation=false) const
Fetch sequence data for the given row and range.
Definition: sparse_aln.cpp:496
bool IsNegativeStrand(TNumrow row) const
Definition: sparse_aln.cpp:321
const objects::CSeq_id & GetSeqId(TNumrow row) const
Get seq-id for the row.
Definition: sparse_aln.cpp:264
virtual void Init(CSparseAln &align)
CRef< CSparseAln > m_Alignment
CRef< CBuildSparseAlnJob > m_Job
virtual int GetGenCode(IAlnExplorer::TNumrow row) const
virtual bool IsSetAnchor(void) const
Anchoring methods.
virtual bool IsPositiveStrand(TNumrow row) const
virtual IAlnSegmentIterator * CreateSegmentIterator(TNumrow row, const IAlnExplorer::TSignedRange &range, IAlnSegmentIterator::EFlags flag) const
virtual void x_ClearHandles()
virtual bool IsNegativeStrand(TNumrow row) const
virtual const objects::CBioseq_Handle & GetBioseqHandle(TNumrow row) const
virtual TSeqPos GetAlnStart(void) const
virtual bool SetAnchor(TNumrow anchor)
virtual bool CanCreateConsensus()
virtual TNumrow GetNumRows(void) const
number of rows in alignment
virtual IAlnExplorer::EAlignType GetAlignType() const
virtual void SetGapChar(TResidue gap_char)
virtual bool UnsetAnchor(void)
vector< CSparseRowHandle * > THandleVector
virtual void GetSeqFromAln(TNumrow row, const TRangeColl &aln_coll, TRangeColl &seq_coll) const
virtual bool CanGetBioseqHandle(TNumrow row) const
virtual void x_OnJobProgress(CAppJobNotification &notn)
virtual TSignedSeqPos GetAlnPosFromSeqPos(TNumrow row, TSeqPos seq_pos, IAlnExplorer::ESearchDirection dir=IAlnExplorer::eNone, bool try_reverse_dir=true) const
CRef< objects::CScope > m_Scope
virtual TNumrow GetAnchor(void) const
virtual bool IsEmpty() const
virtual TSeqPos GetSeqStop(TNumrow row) const
virtual bool x_IsJobRunning()
virtual TSeqPos GetBaseWidth(TNumrow row) const
virtual bool CanChangeAnchor(void) const
virtual void x_CreateHandles()
void OnAppJobNotification(CEvent *evt)
virtual void GetAlnFromSeq(TNumrow row, const TRangeColl &seq_coll, TRangeColl &aln_coll) const
virtual TSignedSeqPos GetSeqPosFromAlnPos(TNumrow for_row, TSeqPos aln_pos, IAlnExplorer::ESearchDirection dir=IAlnExplorer::eNone, bool try_reverse_dir=true) const
virtual bool CanGetId(TNumrow row) const
CSparseAlignment::TNumrow TNumrow
virtual int GetConsensusRow() const
returns index of the Consensus row or -1 if it doesn't exist
virtual void CreateConsensus()
virtual TSeqPos GetSeqStart(TNumrow row) const
virtual string & GetSeqString(TNumrow row, string &buffer, const IAlnExplorer::TRange &seq_rng) const
virtual const objects::CSeq_id & GetSeqId(TNumrow row) const
virtual string & GetAlnSeqString(TNumrow row, string &buffer, const IAlnExplorer::TSignedRange &aln_rng) const
virtual bool CanGetSeqString(TNumrow row) const
virtual const IAlignRowHandle * GetRowHandle(TNumrow row) const
virtual TSeqPos GetAlnStop(void) const
CSparseRowHandle.
IAlignRowHandle provides an abstract way to access alignment row data.
Definition: alnmulti_ds.hpp:59
virtual int GetGenCode() const =0
ESearchDirection
Position search options.
@ eRight
Towards higher aln coord (always to the right)
@ eLeft
Towards lower aln coord (always to the left)
IAlnExplorer::TResidue TResidue
IAlnExplorer::TNumrow TNumrow
Alignment segment iterator interface.
EFlags
Iterator options.
static int type
Definition: getdata.c:31
unsigned int TSeqPos
Type for sequence locations and lengths.
Definition: ncbimisc.hpp:875
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
int TSignedSeqPos
Type for signed sequence position.
Definition: ncbimisc.hpp:887
#define NON_CONST_ITERATE(Type, Var, Cont)
Non constant version of ITERATE macro.
Definition: ncbimisc.hpp:822
void swap(NCBI_NS_NCBI::pair_base_member< T1, T2 > &pair1, NCBI_NS_NCBI::pair_base_member< T1, T2 > &pair2)
Definition: ncbimisc.hpp:1508
#define NULL
Definition: ncbistd.hpp:225
#define ERR_POST(message)
Error posting with file, line number information but without error codes.
Definition: ncbidiag.hpp:186
#define LOG_POST(message)
This macro is deprecated and it's strongly recomended to move in all projects (except tests) to macro...
Definition: ncbidiag.hpp:226
void Error(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1197
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
Definition: ncbiexpt.hpp:704
const string & GetMsg(void) const
Get message string.
Definition: ncbiexpt.cpp:461
CConstIRef< IAppJobError > GetError() const
returns non-null pointer only if job Failed
static CAppJobDispatcher & GetInstance()
CRef< CObject > GetResult() const
returns non-null pointer only if Completed or Running and has temporary results available
bool DeleteJob(TJobID job_id)
when a Job is deleted the listener is not notified
TJobID StartJob(IAppJob &job, const string &engine_name, IEngineParams *params=NULL)
Starts a Job on the specified engine in "passive mode" - no notifications or progress reports will be...
CConstIRef< IAppJobProgress > GetProgress() const
returns non-null pointer only if notification type is eProgress
#define END_EVENT_MAP()
Ends definition of Command Map.
#define BEGIN_EVENT_MAP(thisClass, baseClass)
Begins definition of Command Map for CEventHandler-derived class.
TJobState GetState() const
@ eCanceled
Definition: app_job.hpp:91
@ eCompleted
Definition: app_job.hpp:89
@ eRunning
Definition: app_job.hpp:88
@ eFailed
Definition: app_job.hpp:90
CBioseq_Handle GetBioseqHandle(const CSeq_id &id)
Get bioseq handle by seq-id.
Definition: scope.cpp:95
bool IsNucleotide(void) const
TObjectType * GetPointer(void) THROWS_NONE
Get pointer,.
Definition: ncbiobj.hpp:998
void Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:773
CRange< TSeqPos > TSeqRange
typedefs for sequence ranges
Definition: range.hpp:419
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
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
int i
static void text(MDB_val *v)
Definition: mdb_dump.c:62
range(_Ty, _Ty) -> range< _Ty >
T max(T x_, T y_)
T min(T x_, T y_)
double r(size_t dimension_, const Int4 *score_, const double *prob_, double theta_)
static pcre_uint8 * buffer
Definition: pcretest.c:1051
ON_EVENT(CAppJobNotification, CAppJobNotification::eStateChanged, &CSparseMultiDataSource::OnAppJobNotification) ON_EVENT(CAppJobNotification
CSparseMultiDataSource.
USING_SCOPE(ncbi::objects)
Definition: type.c:6
#define _ASSERT
Modified on Sat Dec 02 09:23:31 2023 by modify_doxy.py rev. 669887