NCBI C++ ToolKit
density_map.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef GUI_UTILS___DENSITY_GRAPH__HPP
2 #define GUI_UTILS___DENSITY_GRAPH__HPP
3 
4 /* $Id: density_map.hpp 47485 2023-05-02 14:46:59Z ucko $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Mike DiCuccio
30  *
31  * File Description:
32  *
33  */
34 
35 #include <corelib/ncbistd.hpp>
36 #include <objmgr/bioseq_handle.hpp>
37 #include <objmgr/feat_ci.hpp>
38 #include <objmgr/align_ci.hpp>
39 #include <objmgr/annot_ci.hpp>
43 
44 #include <util/range.hpp>
45 #include <util/range_coll.hpp>
46 #include <deque>
47 
48 /** @addtogroup GUI_UTILS
49 *
50 * @{
51 */
52 
53 
55 
56 /// TEMPLATE binary functor base struct
57 template<class Type>
59 {
60  virtual ~binary_functor() {}
61 
62  virtual Type operator() (const Type& left, const Type& right) const = 0;
63  virtual binary_functor<Type>* clone() const = 0;
64 };
65 
66 /// TEMPLATE STRUCT max
67 template<class Type>
68 struct plus_func
69  : public binary_functor<Type>
70 { // functor for addition
71  plus_func() {}
72 
73  virtual Type operator()(const Type& left, const Type& right) const
74  { // apply operator+ to operands
75  return left + right;
76  }
77 
78  virtual binary_functor<Type>* clone() const
79  {
80  return new plus_func<Type>;
81  }
82 };
83 
84 /// TEMPLATE STRUCT max
85 template<class Type>
86 struct max_func
87  : public binary_functor<Type>
88 { // functor for max
89  virtual Type operator()(const Type& left, const Type& right) const
90  { // apply operator+ to operands
91  return left > right ? left : right;
92  }
93 
94  virtual binary_functor<Type>* clone() const
95  {
96  return new max_func<Type>;
97  }
98 };
99 
100 /// TEMPLATE STRUCT min
101 template<class Type>
102 struct min_func
103  : public binary_functor<Type>
104 { // functor for min
105  Type operator()(const Type& left, const Type& right) const
106  { // apply operator+ to operands
107  return left < right ? left : right;
108  }
109 
110  virtual binary_functor<Type>* clone() const
111  {
112  return new min_func<Type>;
113  }
114 };
115 
116 
117 
118 /// Run iterator. iterate through runs of equal values in the bins.
119 
120 template <typename CntType>
122 public:
123  typedef vector<CntType> container_type;
124  typedef typename container_type::size_type position_type;
125 
127  const container_type& bins,
128  TSeqPos start, double window)
129  : m_Bins(bins),
130  m_Pos(here),
132  m_Start(start),
133  m_Window(window)
134  {
135  }
136 
137  CntType Advance();
138 
139  position_type GetPosition() const { return m_Pos; }
141  TSeqPos GetSeqPosition() const;
143  TSeqPos GetSeqRunLength() const;
144 
145  CntType GetValue() const { return Valid() ? m_Bins[m_Pos]: 0 ; }
146 
147  bool operator ==(const CDenMapRunIterator<CntType>& rhs) const { return m_Pos == rhs.m_Pos; }
148 
149  bool Valid() const { return m_Pos >= 0 && m_Pos < m_Bins.size() && m_RunLength != 0; }
151 
152 private:
153 
154 
156 
160 
162  const double m_Window;
163 
164 
165 };
166 
167 template <typename CntType>
169 {
170  return TSeqPos(GetPosition() * m_Window + 0.5) + m_Start;
171 }
172 
173 template <typename CntType>
175 {
176  return TSeqPos((GetPosition() + GetRunLength()) * m_Window + 0.5) + m_Start;
177 }
178 
179 
180 template <typename CntType>
182 {
183  return TSeqPos(GetRunLength() * m_Window + 0.5);
184 }
185 
186 
187 template <typename CntType>
190 {
191  if (m_Pos >= 0 && m_Pos < m_Bins.size()) {
192  CntType this_value = m_Bins[m_Pos];
194  for (i = m_Pos + 1;
195  i < m_Bins.size() && m_Bins[i] == this_value;
196  ++i) {}
197 
198  return i - m_Pos;
199  }
200  return 0;
201 }
202 
203 template <typename CntType>
204 CntType
206  m_Pos += m_RunLength;
207  m_RunLength = x_CalcRunLength();
208  return GetValue();
209 }
210 
211 
212 ///
213 /// class CDensityMap generates a low-resolution view of a set of features.
214 /// The features to be processed are defined by the SAnnotSelector object.
215 /// The return value is a vector of counts, one for each of the bins
216 /// defined by the window width and spanning the range from start to stop.
217 /// If start and stop are both zero, then the entire sequence is evaluated.
218 ///
219 
220 
221 template <typename CntType>
223 {
224 public:
225  typedef vector<CntType> container_type;
226  typedef typename container_type::const_iterator const_iterator;
227  typedef typename container_type::iterator iterator;
230  typedef vector<size_t> TDataPoints;
231 
232  CDensityMap(TSeqPos start = 0, TSeqPos stop = 0, double window = 1,
233  accum_functor* func = NULL, CntType def = (CntType)0);
234  CDensityMap(const objects::CBioseq_Handle& handle, double window = 1,
235  accum_functor* func = NULL, CntType def = (CntType)0);
236 
239  virtual ~CDensityMap() {}
240 
241  virtual void AddRange(TSeqRange range, CntType score = 1, bool expand = false);
242 
244  CntType score = 1, bool expand = false);
245 
246  CntType AddLocation(const objects::CSeq_loc& loc);
247 
248  CntType AddFeature(const objects::CSeq_feat& feature);
249 
250  /// All features on this bioseq selected by sel in the range of this.
251  CntType AddFeatures(const objects::CBioseq_Handle& handle,
252  objects::SAnnotSelector sel);
253 
254  /// All alignments on this bioseq selected by sel in the range of this.
255  CntType AddAlignments(const objects::CBioseq_Handle& handle,
256  objects::SAnnotSelector sel);
257 
258  /// All alignments in a given annotation on this bioseq within the range of this.
259  CntType AddAlignments(const objects::CBioseq_Handle& handle,
260  const objects::CSeq_annot& seq_annot);
261 
262  void Clear()
263  {
264  m_Max = m_DefVal;
265  m_Min = m_DefVal;
266  fill(m_Bins.begin(), m_Bins.end(), m_DefVal);
267  }
268  TSeqPos GetStart() const { return m_Range.GetFrom(); }
269  TSeqPos GetStop() const { return m_Range.GetTo(); }
270  TSeqRange GetRange() const { return m_Range; }
271  double GetWindow() const { return m_Window; }
272  size_t GetBins() const { return m_Bins.size(); }
273  CntType GetMax() const { return m_Max; }
274  CntType GetMin() const { return m_Min; }
275  CntType GetDefVal() const { return m_DefVal; }
276  void SetMax(const CntType& max) { m_Max = max; }
277  void SetMin(const CntType& min) { m_Min = min; }
278 
279  const accum_functor* GetAccum() const { return m_AccumFunc.get(); }
280 
281  /// extend our density map to cover the sequence position stop.
282  /// can only be used to extend to the right, that is only Stop is affected, not Start.
283  void ExtendTo(TSeqPos stop);
284 
285  const_iterator begin() const { return m_Bins.begin(); }
286  const_iterator end() const { return m_Bins.end(); }
287  iterator begin() { return m_Bins.begin(); }
288  iterator end() { return m_Bins.end(); }
289 
291  runlen_iterator RunLenIterator(typename container_type::size_type n) const
292  { return runlen_iterator(n, m_Bins, m_Range.GetFrom(), m_Window); }
293 
294  CntType operator[](typename container_type::size_type n) const { return m_Bins[n]; }
295  CntType& operator[](typename container_type::size_type n) { return m_Bins[n]; }
296 
297 
298 
299  /// OLD static method. Use AddFeatures method instead.
300  /// retrieve a density map. The return value is the maximum value
301  /// in the density graph.
302  static TSeqPos GetDensityMap(const objects::CBioseq_Handle& handle,
303  TSeqPos start, TSeqPos stop,
304  TSeqPos window,
305  objects::SAnnotSelector sel,
306  vector<TSeqPos>& density);
307 
308 
309 protected:
310  /// given the range and window size, how many bins should there be?
311  size_t x_CalcNbins()
312  {
313  return TSeqPos((m_Range.GetToOpen() - m_Range.GetFrom()) / m_Window);
314  }
315 
316  /// convert from sequence coords to a bin number.
317  size_t x_BinN(TSeqPos p)
318  {
319  return size_t((p - m_Range.GetFrom())/m_Window);
320  }
321 
322  /// closed range on a sequence this covers.
324 
325  /// coordinates per bin.
326  double m_Window;
327 
328  /// Default value.
329  CntType m_DefVal;
330 
331  /// maximum Count accumulated in the bins so far.
332  CntType m_Max;
333 
334  /// Smallest count in a bin.
335  CntType m_Min;
336 
337  /// Where we actually keep the accumulated counts/scores/whatever.
339 
340  unique_ptr<accum_functor> m_AccumFunc;
341 
342 };
343 
344 
345 
346 ///
347 ///
348 /////////////////////////////////////////////////////////////////////////////
349 ///
350 ///
351 
352 template <typename CntType>
354  TSeqPos stop,
355  double window /* = 1 */,
356  accum_functor* func /*= NULL */,
357  CntType def /*= (CntType)0*/)
358  : m_Range(start, stop)
359  , m_Window(window)
360  , m_DefVal(def)
361  , m_Max(def)
362  , m_Min(def)
363  , m_Bins(x_CalcNbins(), def)
364  , m_AccumFunc(func ? func : new plus_func<CntType>)
365 {
366 }
367 
368 
369 template <typename CntType>
370 CDensityMap<CntType>::CDensityMap(const objects::CBioseq_Handle& handle,
371  double window /* = 1 */,
372  accum_functor* func /*= new plus_func<CntType> */,
373  CntType def /*= (CntType)0*/)
374  : m_Range(0, handle.GetSeqVector().size())
375  , m_Window(window)
376  , m_DefVal(def)
377  , m_Max(def)
378  , m_Min(def)
379  , m_Bins(x_CalcNbins(), def)
380  , m_AccumFunc(func ? func : new plus_func<CntType>)
381 {
382 }
383 
384 
385 template <typename CntType>
387  : m_Range(map.m_Range)
388  , m_Window(map.m_Window)
389  , m_DefVal(map.m_DefVal)
390  , m_Max(map.m_Max)
391  , m_Min(map.m_Min)
392  , m_Bins(map.m_Bins)
393  , m_AccumFunc(map.m_AccumFunc->clone())
394 {
395 }
396 
397 
398 template <typename CntType>
400 {
401  // check self assignment
402  if (this == &map)
403  return *this;
404 
405  m_Range = map.m_Range;
406  m_Window = map.m_Window;
407  m_DefVal = map.m_DefVal;
408  m_Max = map.m_Max;
409  m_Min = map.m_Min;
410  m_Bins = map.m_Bins;
411  m_AccumFunc.reset(map.m_AccumFunc->clone());
412  return *this;
413 }
414 
415 
416 template <typename CntType>
418 {
419  if (stop > GetStop()) {
420  m_Range.SetTo(stop);
421  m_Bins.resize(x_CalcNbins(), m_DefVal);
422  }
423 }
424 
425 
426 template <typename CntType>
427 void CDensityMap<CntType>::AddRange(TSeqRange range, CntType score, bool expand)
428 {
429  //_ASSERT(range.GetFrom() <= range.GetTo());
430  if (range.GetFrom() > range.GetTo()) {
431  range = TSeqRange(range.GetTo(), range.GetFrom());
432  }
433 
434  if ( expand ) {
435  ExtendTo( range.GetTo());
436  }
437 
438  TSeqRange usable_range(m_Range.IntersectionWith(range));
439  if (usable_range.Empty()) {
440  return;
441  }
442 
443  m_Max = max(m_Max, score);
444  m_Min = min(m_Min, score);
445 
446  size_t begin_bin = x_BinN(usable_range.GetFrom());
447  size_t end_bin = begin_bin;
448  if (m_Window > 1.0) {
449  end_bin = x_BinN(usable_range.GetTo()) + 1;
450  } else {
451  end_bin = x_BinN(usable_range.GetToOpen());
452  }
453  end_bin = min(end_bin, m_Bins.size());
454 
455  for (size_t i = begin_bin; i < end_bin ; ++i) {
456  CntType& new_val = m_Bins[i];
457  new_val = (*m_AccumFunc)(new_val, score);
458  m_Max = max(m_Max, new_val);
459  m_Min = min(m_Min, new_val);
460  }
461 
462 }
463 
464 template <typename CntType>
466  CntType score, bool expand)
467 {
468  if ( expand ) {
469  ExtendTo( ranges.GetTo());
470  }
471 
472  ranges.IntersectWith(m_Range);
473  if (ranges.Empty()) {
474  return;
475  }
476  m_Max = max(m_Max, score);
477  m_Min = min(m_Min, score);
478 
479  size_t previous_end_bin = 0;
480  ITERATE (CRangeCollection<TSeqPos>, range_it, ranges) {
481  size_t begin_bin = x_BinN(range_it->GetFrom());
482  size_t end_bin = begin_bin;
483  if (m_Window > 1.0) {
484  end_bin = x_BinN(range_it->GetTo()) + 1;
485  } else {
486  end_bin = x_BinN(range_it->GetToOpen());
487  }
488 
489  begin_bin = max(begin_bin, previous_end_bin);
490  end_bin = min(end_bin, m_Bins.size());
491  previous_end_bin = end_bin;
492  for (size_t i = begin_bin; i < end_bin ; ++i) {
493  CntType& new_val = m_Bins[i];
494  new_val = (*m_AccumFunc)(new_val, score);
495  m_Max = max(m_Max, new_val);
496  m_Min = min(m_Min, new_val);
497  }
498  }
499 }
500 
501 
502 template <typename CntType>
503 CntType CDensityMap<CntType>::AddLocation(const objects::CSeq_loc& loc)
504 {
506  for( objects::CSeq_loc_CI it(loc); it; ++it) {
507  ranges += it.GetRange();
508  }
509  AddRanges(ranges, 1, false);
510  return GetMax();
511 }
512 
513 
514 template <typename CntType>
515 CntType CDensityMap<CntType>::AddFeature(const objects::CSeq_feat& feature)
516 {
517  return AddLocation(feature.GetLocation());
518 }
519 
520 
521 template <typename CntType>
523  const objects::CBioseq_Handle& handle,
524  objects::SAnnotSelector sel)
525 {
526  TSeqPos start(GetStart());
527  TSeqPos stop(GetStop());
528 
529  /// eliminate some expensive functions
530  sel.SetSortOrder(objects::SAnnotSelector::eSortOrder_None);
531 
532  // grab a feature iterator for our range
533  CConstRef<objects::CSeq_loc> loc(handle.GetRangeSeq_loc(start, stop));
534  objects::CFeat_CI feat_iter(handle.GetScope(), *loc, sel);
535  if (feat_iter.GetSize() == 0) {
536  return GetMax();
537  }
538 
539  for (; feat_iter; ++feat_iter) {
540  objects::CMappedFeat feat = *feat_iter;
541  //TSeqRange range = feat.GetLocation().GetTotalRange();
542  //AddRange(range, 1, false);
543  AddLocation(feat.GetLocation());
544  }
545  return GetMax();
546 }
547 
548 
549 template <typename CntType>
551  const objects::CBioseq_Handle& handle,
552  objects::SAnnotSelector sel)
553 {
555 
556 
557  // grab a feature iterator for our range
558  objects::CAlign_CI align_iter(handle, range, sel);
559 
560  for (size_t ai = 0; align_iter; ++align_iter, ++ai) {
561  const objects::CSeq_align& align = *align_iter;
562 
563  if (! align.CanGetDim()) {
564  _TRACE("Dimension not set. " << ai);
565  continue;
566  }
567  int dim = align.GetDim();
568  if (dim != 2) {
569  _TRACE("Dimension not 2. " << ai);
570  continue;
571  }
572 
573  /*
574  CAlnMix mix(handle.GetScope());
575  mix.Add(align);
576  mix.Merge(CAlnMix::fGapJoin);
577  CRef<CAlnMap> aln_map
578  (new CAlnMap(mix.GetDenseg()));
579  */
580 
581  CRef< objects::CAlnMap> aln_map;
582  if (align.GetSegs().IsStd()) {
583  _TRACE(ai << ": Std seg");
585  = align.CreateDensegFromStdseg();
586  aln_map = new objects::CAlnMap( ds_align->GetSegs().GetDenseg());
587  continue;
588  } else if (align.GetSegs().IsDenseg()) {
589  aln_map = new objects::CAlnMap(align.GetSegs().GetDenseg());
590  _TRACE(ai << ": Dense seg");
591  }
592 
593 
594  {{
595  string all_ids = ": ";
596  for (objects::CAlnMap::TDim di = 0; di < aln_map->GetNumRows();
597  ++di) {
598  all_ids += aln_map->GetSeqId(di).AsFastaString() + ", ";
599  }
600  _TRACE(all_ids);;
601  }}
602 
603  TSeqRange range_final(aln_map->GetSeqStart(0), aln_map->GetSeqStop(0));
604  AddRange(range_final, 1, false);
605  }
606 
607  return GetMax();
608 
609 }
610 
611 
612 
613 template <typename CntType>
615  const objects::CBioseq_Handle& handle,
616  const objects::CSeq_annot& seq_annot)
617 {
619 
620  objects::SAnnotSelector sel;
621  objects::CSeq_annot_Handle sah
622  = handle.GetScope().GetSeq_annotHandle(seq_annot);
623  sel.SetLimitSeqAnnot(sah)
624  .SetSortOrder(objects::SAnnotSelector::eSortOrder_None);
625 
626  // grab an alignment iterator for our range
627  objects::CAlign_CI align_iter(handle, range, sel);
628 
629  objects::CAlnMix mix(handle.GetScope());
630  for (int ai = 0; align_iter; ++align_iter, ++ai) {
631  const objects::CSeq_align& align = *align_iter;
632 
633  // check number of dimensions
634  if (! align.CanGetDim() || align.GetDim() != 2) {
635  // _TRACE("Dimension not 2. " << ai);
636  continue;
637  }
638 
639  // conver to a AlnMap.
640  CRef<objects::CAlnMap> aln_map;
641  if (align.GetSegs().IsStd()) {
642  // _TRACE(ai << ": Std seg" );
643  continue; // IGNORE std segs for now.
644  try {
645  CRef<objects::CSeq_align> ds_align =
646  align.CreateDensegFromStdseg();
647  aln_map =
648  new objects::CAlnMap(ds_align->GetSegs().GetDenseg());
649  {{
650  string all_ids = ": ";
651  for (objects::CAlnMap::TDim di = 0;
652  di < aln_map->GetNumRows(); ++di) {
653  all_ids += aln_map->GetSeqId(di).AsFastaString() + ", ";
654  }
655  _TRACE(all_ids);
656  }}
657  } catch (const objects::CSeqalignException& /*e*/) {
658  // _TRACE(" : FAILED! " << e.what());
659  continue;
660  }
661  } else if (align.GetSegs().IsDenseg()) {
662  mix.Add(align);//, CAlnMix::fDontUseObjMgr);
663  mix.Merge();
664  aln_map = new objects::CAlnMap(mix.GetDenseg());
665  //aln_map = new CAlnMap(align.GetSegs().GetDenseg());
666  if (aln_map->GetNumSegs() < 2)
667  continue;
668  _TRACE(ai << ": Dense seg");
669  }
670 
671 
672  // find out what row our bioseq is on.
673  objects::CAlnMap::TNumrow row = 0;
674  for (row = 0; row != aln_map->GetNumRows(); ++row) {
675  if ( handle.IsSynonym(aln_map->GetSeqId(row)) ) {
676  aln_map->SetAnchor(row);
677  break;
678  }
679  }
680  if (row == aln_map->GetNumRows()) {
681  continue;
682  }
683  // works since dimension always 2.
684  //objects::CAlnMap::TNumrow other_row = 1 - row;
685 
686  /**
687  // total range
688  _TRACE(" (" << aln_map->GetSeqStart(row) << "," << aln_map->GetSeqStop(row) << ")");
689  // IDs
690  {{
691  _TRACE(" Segs: " << aln_map->GetNumSegs());
692  string all_ids = ": ";
693  for (CAlnMap::TDim di = 0; di < aln_map->GetNumRows(); ++di) {
694  all_ids += aln_map->GetSeqId(di).AsFastaString() + ", ";
695  }
696  _TRACE(all_ids);
697  }}
698  // Segments.
699  _TRACE(" ");
700  for (CAlnMap::TNumseg s = 0; s < aln_map->GetNumSegs(); ++s) {
701  // if (aln_map->GetSegType(row, s) && CAlnMap:: )
702  _TRACE(" (" << aln_map->GetStart(row, s) << "," << aln_map->GetStop(row, s) << ")");
703  }
704  // Chunks
705  _TRACE(" Chunks: " );
706  CRef<CAlnMap::CAlnChunkVec> chunks =
707  aln_map->GetSeqChunks(row, CAlnMap::TSignedRange(start, stop), CAlnMap::fAlnSegsOnly);
708  for (CAlnMap::TNumchunk c = 0; c < chunks->size(); ++c) {
709  CConstRef<CAlnMap::CAlnChunk> chunk = (*chunks)[c];
710  _TRACE("(" << chunk->GetRange().GetFrom() << "," << chunk->GetRange().GetTo() << ") ");
711  }
712  **/
713 
714  /*
715  TSeqRange range(aln_map->GetSeqRange(row));
716  TSeqRange other_range(aln_map->GetSeqRange(other_row));
717  if (GetRange().IntersectingWith(range)) {
718  AddRange(range, 1, false);
719  }
720  */
721  }
722  /*
723  mix.Merge();
724  CRef< CAlnMap> aln_map;
725  aln_map = new CAlnMap(mix.GetDenseg());
726  */
727  return GetMax();
728 }
729 
730 
731 
732 template <typename CntType>
733 TSeqPos CDensityMap<CntType>::GetDensityMap(const objects::CBioseq_Handle& handle,
734  TSeqPos start, TSeqPos stop,
735  TSeqPos window,
736  objects::SAnnotSelector sel,
737  vector<TSeqPos>& density)
738 {
739  // set up our bins to handle pooled feature counts
740  if (stop == start && stop == 0) {
741  objects::CSeqVector vec = handle.GetSeqVector();
742  stop = vec.size();
743  }
744 
745  // grab a feature iterator for our range
746  objects::CFeat_CI feat_iter(handle.GetScope(),
747  *handle.GetRangeSeq_loc(start, stop),
748  sel);
749  if (feat_iter.GetSize() == 0) {
750  return 0;
751  }
752 
753  size_t bins = (stop - start) / window;
754  if (bins * window < stop - start) {
755  ++bins;
756  }
757 
758  density.resize(bins, 0);
759 
760  TSeqPos max_val = 0;
761 
762  // we keep a temporary list of mapped features - we use these
763  // the screen what's in our visible range
764  deque<objects::CMappedFeat> feats;
765  TSeqPos bin_count = 0;
766  NON_CONST_ITERATE(vector<TSeqPos>, bin_iter, density) {
767  TSeqPos bin_start = start + bin_count * window;
768  TSeqPos bin_stop = bin_start + window;
769 
770  // accumulate features for this bin
771  if (feat_iter) {
772  objects::CMappedFeat feat = *feat_iter;
773  TSeqRange range = feat.GetLocation().GetTotalRange();
774 
775  while (range.GetFrom() < bin_stop) {
776  feats.push_back(feat);
777 
778  ++feat_iter;
779  if ( !feat_iter ) {
780  break;
781  }
782  feat = *feat_iter;
783  range = feat.GetLocation().GetTotalRange();
784  }
785  }
786 
787  // remove features outside this bin
788  while (feats.size()) {
789  objects::CMappedFeat& feat = feats.front();
790  TSeqRange range = feat.GetLocation().GetTotalRange();
791 
792  if (range.GetTo() < bin_start) {
793  feats.pop_front();
794  } else {
795  break;
796  }
797  }
798 
799  // store our count and go to the next bin
800  *bin_iter = feats.size();
801  max_val = max(*bin_iter, max_val);
802  ++bin_count;
803  }
804 
805  return max_val;
806 }
807 
808 
810 
811 /* @} */
812 
813 #endif // GUI_UTILS___DENSITY_GRAPH__HPP
CAnchoredAln::TDim TDim
Run iterator. iterate through runs of equal values in the bins.
class CDensityMap generates a low-resolution view of a set of features.
position_type GetTo() const
Definition: range_coll.hpp:132
TThisType & IntersectWith(const TRange &r)
Definition: range_coll.hpp:173
bool Empty() const
Definition: range_coll.hpp:138
CRef –.
Definition: ncbiobj.hpp:618
Definition: map.hpp:338
Include a standard set of the NCBI C++ Toolkit most basic headers.
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
#define NON_CONST_ITERATE(Type, Var, Cont)
Non constant version of ITERATE macro.
Definition: ncbimisc.hpp:822
#define NULL
Definition: ncbistd.hpp:225
#define _TRACE(message)
Definition: ncbidbg.hpp:122
virtual ~binary_functor()
Definition: density_map.hpp:60
void ExtendTo(TSeqPos stop)
extend our density map to cover the sequence position stop.
virtual Type operator()(const Type &left, const Type &right) const
Definition: density_map.hpp:73
void SetMin(const CntType &min)
CntType AddAlignments(const objects::CBioseq_Handle &handle, const objects::CSeq_annot &seq_annot)
All alignments in a given annotation on this bioseq within the range of this.
const accum_functor * GetAccum() const
position_type m_Pos
CntType m_Min
Smallest count in a bin.
CntType m_DefVal
Default value.
vector< size_t > TDataPoints
CntType m_Max
maximum Count accumulated in the bins so far.
const_iterator end() const
TSeqPos GetSeqPosition() const
Type operator()(const Type &left, const Type &right) const
position_type m_RunLength
bool Valid() const
const double m_Window
virtual binary_functor< Type > * clone() const
Definition: density_map.hpp:94
container_type::const_iterator const_iterator
iterator begin()
CntType operator[](typename container_type::size_type n) const
size_t GetBins() const
TSeqRange m_Range
closed range on a sequence this covers.
const_iterator begin() const
void SetMax(const CntType &max)
container_type::iterator iterator
runlen_iterator RunLenBegin() const
size_t x_BinN(TSeqPos p)
convert from sequence coords to a bin number.
static TSeqPos GetDensityMap(const objects::CBioseq_Handle &handle, TSeqPos start, TSeqPos stop, TSeqPos window, objects::SAnnotSelector sel, vector< TSeqPos > &density)
OLD static method.
bool operator==(const CDenMapRunIterator< CntType > &rhs) const
TSeqPos GetSeqRunEndPosition() const
CntType AddAlignments(const objects::CBioseq_Handle &handle, objects::SAnnotSelector sel)
All alignments on this bioseq selected by sel in the range of this.
CntType GetMin() const
virtual void AddRanges(CRangeCollection< TSeqPos > ranges, CntType score=1, bool expand=false)
position_type GetRunLength() const
CDenMapRunIterator(position_type here, const container_type &bins, TSeqPos start, double window)
double m_Window
coordinates per bin.
const container_type & m_Bins
size_t x_CalcNbins()
given the range and window size, how many bins should there be?
CDensityMap< CntType > & operator=(const CDensityMap< CntType > &map)
TSeqPos GetStop() const
const TSeqPos m_Start
CDenMapRunIterator< CntType > runlen_iterator
CDensityMap(TSeqPos start=0, TSeqPos stop=0, double window=1, accum_functor *func=NULL, CntType def=(CntType) 0)
vector< CntType > container_type
virtual binary_functor< Type > * clone() const =0
virtual void AddRange(TSeqRange range, CntType score=1, bool expand=false)
runlen_iterator RunLenIterator(typename container_type::size_type n) const
CntType GetDefVal() const
CDensityMap(const CDensityMap< CntType > &map)
CntType & operator[](typename container_type::size_type n)
CntType AddLocation(const objects::CSeq_loc &loc)
virtual Type operator()(const Type &left, const Type &right) const
Definition: density_map.hpp:89
CntType AddFeature(const objects::CSeq_feat &feature)
vector< CntType > container_type
position_type x_CalcRunLength() const
CDensityMap(const objects::CBioseq_Handle &handle, double window=1, accum_functor *func=NULL, CntType def=(CntType) 0)
CntType AddFeatures(const objects::CBioseq_Handle &handle, objects::SAnnotSelector sel)
All features on this bioseq selected by sel in the range of this.
container_type m_Bins
Where we actually keep the accumulated counts/scores/whatever.
virtual ~CDensityMap()
TSeqPos GetSeqRunLength() const
CntType GetValue() const
double GetWindow() const
TSeqRange GetRange() const
virtual binary_functor< Type > * clone() const
CntType GetMax() const
DECLARE_OPERATOR_BOOL(Valid())
unique_ptr< accum_functor > m_AccumFunc
position_type GetPosition() const
binary_functor< CntType > accum_functor
virtual Type operator()(const Type &left, const Type &right) const =0
TSeqPos GetStart() const
virtual binary_functor< Type > * clone() const
Definition: density_map.hpp:78
iterator end()
container_type::size_type position_type
TSeqPos GetStop(const CSeq_loc &loc, CScope *scope, ESeqLocExtremes ext=eExtreme_Positional)
If only one CBioseq is represented by CSeq_loc, returns the position at the stop of the location.
TSeqPos GetStart(const CSeq_loc &loc, CScope *scope, ESeqLocExtremes ext=eExtreme_Positional)
If only one CBioseq is represented by CSeq_loc, returns the position at the start of the location.
position_type GetToOpen(void) const
Definition: range.hpp:138
bool Empty(void) const
Definition: range.hpp:148
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
yy_size_t n
range(_Ty, _Ty) -> range< _Ty >
const struct ncbi::grid::netcache::search::fields::SIZE size
T max(T x_, T y_)
T min(T x_, T y_)
#define row(bind, expected)
Definition: string_bind.c:73
TEMPLATE binary functor base struct.
Definition: density_map.hpp:59
TEMPLATE STRUCT max.
Definition: density_map.hpp:88
TEMPLATE STRUCT min.
TEMPLATE STRUCT max.
Definition: density_map.hpp:70
#define Type
Modified on Sat May 25 14:21:56 2024 by modify_doxy.py rev. 669887