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

Go to the repository for this file.

1 /* $Id: Seq_loc.cpp 106759 2026-06-09 16:52:47Z vasilche $
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  * Author: Author: Cliff Clausen, Eugene Vasilchenko
27  *
28  * File Description:
29  * .......
30  *
31  * Remark:
32  * This code was originally generated by application DATATOOL
33  * using specifications from the ASN data definition file
34  * 'seqloc.asn'.
35  *
36  * ===========================================================================
37  */
38 
39 #include <ncbi_pch.hpp>
40 #include <serial/enumvalues.hpp>
52 #include <util/range_coll.hpp>
55 #include <algorithm>
56 
57 
58 #define NCBI_USE_ERRCODE_X Objects_SeqLoc
59 
61 BEGIN_objects_SCOPE // namespace ncbi::objects::
62 
63 
64 // CSeqLocException
65 const char* CSeqLocException::GetErrCodeString(void) const
66 {
67  switch (GetErrCode()) {
68  case eNotSet: return "eNotSet";
69  case eMultipleId: return "eMultipleId";
70  case eUnsupported: return "eUnsupported";
71  case eBadLocation: return "eBadLocation";
72  case eBadIterator: return "eBadIterator";
73  case eIncomatible: return "eIncomatible";
74  case eOutOfRange: return "eOutOfRange";
75  case eOtherError: return "eOtherError";
76  default: return CException::GetErrCodeString();
77  }
78 }
79 
80 
81 // constructors
83 {
84  InvalidateCache();
85  switch ( index ) {
86  case e_Null:
87  {
88  SetNull();
89  break;
90  }
91  case e_Empty:
92  {
93  SetEmpty();
94  break;
95  }
96  case e_Whole:
97  {
98  SetWhole();
99  break;
100  }
101  case e_Int:
102  {
103  SetInt();
104  break;
105  }
106  case e_Packed_int:
107  {
108  SetPacked_int();
109  break;
110  }
111  case e_Pnt:
112  {
113  SetPnt();
114  break;
115  }
116  case e_Packed_pnt:
117  {
118  SetPacked_pnt();
119  break;
120  }
121  case e_Mix:
122  {
123  SetMix();
124  break;
125  }
126  case e_Equiv:
127  {
128  SetEquiv();
129  break;
130  }
131  case e_Bond:
132  {
133  SetBond();
134  break;
135  }
136  case e_Feat:
137  {
138  SetFeat();
139  break;
140  }
141  case e_not_set:
142  default:
143  break;
144  }
145 }
146 
147 
148 CSeq_loc::CSeq_loc(TId& id, TPoint point, TStrand strand)
149 {
150  InvalidateCache();
151  SetPnt(*new CSeq_point(id, point, strand));
152 }
153 
154 
155 CSeq_loc::CSeq_loc(TId& id, const TPoints& points, TStrand strand)
156 {
157  InvalidateCache();
158  if ( points.size() == 1 ) {
159  SetPnt(*new CSeq_point(id, points.front(), strand));
160  } else {
161  SetPacked_pnt(*new CPacked_seqpnt(id, points, strand));
162  }
163 }
164 
165 
166 CSeq_loc::CSeq_loc(TId& id, TPoint from, TPoint to, TStrand strand)
167 {
168  InvalidateCache();
169  SetInt(*new CSeq_interval(id, from, to, strand));
170 }
171 
172 
173 CSeq_loc::CSeq_loc(TId& id, TRanges ranges, TStrand strand)
174 {
175  InvalidateCache();
176  if ( ranges.size() == 1 ) {
177  SetInt(*new CSeq_interval(id,
178  ranges.front().GetFrom(), ranges.front().GetTo(), strand));
179  } else {
180  SetPacked_int(*new CPacked_seqint(id, ranges, strand));
181  }
182 }
183 
184 
185 // destructor
187 {
188 }
189 
190 
191 inline
192 void x_Assign(CInt_fuzz& dst, const CInt_fuzz& src)
193 {
194  switch ( src.Which() ) {
196  dst.Reset();
197  break;
198  case CInt_fuzz::e_P_m:
199  dst.SetP_m(src.GetP_m());
200  break;
201  case CInt_fuzz::e_Range:
202  dst.SetRange().SetMin(src.GetRange().GetMin());
203  dst.SetRange().SetMax(src.GetRange().GetMax());
204  break;
205  case CInt_fuzz::e_Pct:
206  dst.SetPct(src.GetPct());
207  break;
208  case CInt_fuzz::e_Lim:
209  dst.SetLim(src.GetLim());
210  break;
211  case CInt_fuzz::e_Alt:
212  dst.SetAlt() = src.GetAlt();
213  break;
214  default:
216  "Int-fuzz is not set");
217  }
218 }
219 
220 
221 inline
222 void x_Assign(CSeq_interval& dst, const CSeq_interval& src)
223 {
224  dst.SetFrom(src.GetFrom());
225  dst.SetTo(src.GetTo());
226  if ( src.IsSetStrand() ) {
227  dst.SetStrand(src.GetStrand());
228  }
229  else {
230  dst.ResetStrand();
231  }
232  dst.SetId().Assign(src.GetId());
233  if ( src.IsSetFuzz_from() ) {
234  x_Assign(dst.SetFuzz_from(), src.GetFuzz_from());
235  }
236  else {
237  dst.ResetFuzz_from();
238  }
239  if ( src.IsSetFuzz_to() ) {
240  x_Assign(dst.SetFuzz_to(), src.GetFuzz_to());
241  }
242  else {
243  dst.ResetFuzz_to();
244  }
245 }
246 
247 
248 inline
249 void x_Assign(CSeq_point& dst, const CSeq_point& src)
250 {
251  dst.SetPoint(src.GetPoint());
252  if ( src.IsSetStrand() ) {
253  dst.SetStrand(src.GetStrand());
254  }
255  else {
256  dst.ResetStrand();
257  }
258  dst.SetId().Assign(src.GetId());
259  if ( src.IsSetFuzz() ) {
260  x_Assign(dst.SetFuzz(), src.GetFuzz());
261  }
262  else {
263  dst.ResetFuzz();
264  }
265 }
266 
267 
268 inline
269 void x_Assign(CPacked_seqint& dst, const CPacked_seqint& src)
270 {
271  CPacked_seqint::Tdata& data = dst.Set();
272  data.clear();
273  ITERATE ( CPacked_seqint::Tdata, i, src.Get() ) {
274  data.push_back(CRef<CSeq_interval>(new CSeq_interval));
275  x_Assign(*data.back(), **i);
276  }
277 }
278 
279 
280 inline
281 void x_Assign(CPacked_seqpnt& dst, const CPacked_seqpnt& src)
282 {
283  if ( src.IsSetStrand() ) {
284  dst.SetStrand(src.GetStrand());
285  }
286  else {
287  dst.ResetStrand();
288  }
289  dst.SetId().Assign(src.GetId());
290  if ( src.IsSetFuzz() ) {
291  x_Assign(dst.SetFuzz(), src.GetFuzz());
292  }
293  else {
294  dst.ResetFuzz();
295  }
296  dst.SetPoints() = src.GetPoints();
297 }
298 
299 
300 inline
301 void x_Assign(CSeq_bond& dst, const CSeq_bond& src)
302 {
303  x_Assign(dst.SetA(), src.GetA());
304  if ( src.IsSetB() ) {
305  x_Assign(dst.SetB(), src.GetB());
306  }
307  else {
308  dst.ResetB();
309  }
310 }
311 
312 
313 inline
314 void x_Assign(CSeq_loc_mix& dst, const CSeq_loc_mix& src)
315 {
316  CSeq_loc_mix::Tdata& data = dst.Set();
317  data.clear();
318  ITERATE ( CSeq_loc_mix::Tdata, i, src.Get() ) {
319  data.push_back(CRef<CSeq_loc>(new CSeq_loc));
320  data.back()->Assign(**i);
321  }
322 }
323 
324 
325 inline
326 void x_Assign(CSeq_loc_equiv& dst, const CSeq_loc_equiv& src)
327 {
328  CSeq_loc_equiv::Tdata& data = dst.Set();
329  data.clear();
330  ITERATE ( CSeq_loc_equiv::Tdata, i, src.Get() ) {
331  data.push_back(CRef<CSeq_loc>(new CSeq_loc));
332  data.back()->Assign(**i);
333  }
334 }
335 
336 
338 {
339  InvalidateCache();
340  if ( GetTypeInfo() == obj.GetThisTypeInfo() ) {
341  const CSeq_loc& loc = static_cast<const CSeq_loc&>(obj);
342  switch ( loc.Which() ) {
343  case e_not_set:
344  Reset();
345  return;
346  case e_Null:
347  SetNull();
348  return;
349  case e_Empty:
350  SetEmpty().Assign(loc.GetEmpty());
351  return;
352  case e_Whole:
353  SetWhole().Assign(loc.GetWhole());
354  return;
355  case e_Int:
356  x_Assign(SetInt(), loc.GetInt());
357  return;
358  case e_Pnt:
359  x_Assign(SetPnt(), loc.GetPnt());
360  return;
361  case e_Packed_int:
362  x_Assign(SetPacked_int(), loc.GetPacked_int());
363  return;
364  case e_Packed_pnt:
365  x_Assign(SetPacked_pnt(), loc.GetPacked_pnt());
366  return;
367  case e_Mix:
368  x_Assign(SetMix(), loc.GetMix());
369  return;
370  case e_Equiv:
371  x_Assign(SetEquiv(), loc.GetEquiv());
372  return;
373  case e_Bond:
374  x_Assign(SetBond(), loc.GetBond());
375  return;
376  case e_Feat:
377  SetFeat().Assign(loc.GetFeat());
378  return;
379  }
380  }
381  CSerialObject::Assign(obj, how);
382 }
383 
384 
386 {
387  TSeqPos range_from = m_TotalRangeCacheFrom.load(memory_order_acquire);
388  if ( range_from == TSeqPos(kDirtyCache) ) {
389  const CSeq_id* id = 0;
390  TRange range = x_CalculateTotalRangeCheckId(id);
391  m_IdCache.store(id, memory_order_release);
392  m_TotalRangeCacheToOpen.store(range.GetToOpen(), memory_order_relaxed);
393  m_TotalRangeCacheFrom.store(range.GetFrom(), memory_order_release);
394  return range;
395  }
396  else {
397  TSeqPos range_to_open = m_TotalRangeCacheToOpen.load(memory_order_relaxed);
398  return COpenRange<TSeqPos>(range_from, range_to_open);
399  }
400 }
401 
402 
403 bool CSeq_loc::x_UpdateId(const CSeq_id*& total_id, const CSeq_id* id,
404  bool may_throw) const
405 {
406  if ( total_id == id ) {
407  return true;
408  }
409 
410  if ( !total_id ) {
411  total_id = id;
412  } else if ( (id && !total_id->Equals(*id)) ) {
413  if (may_throw) {
414  NCBI_THROW(CSeqLocException, eMultipleId,
415  "CSeq_loc::GetTotalRange() is not defined "
416  "for seq-loc with several different seq-ids");
417  } else {
418  return false;
419  }
420  }
421  return true;
422 }
423 
424 
425 
426 // returns enclosing location range
427 // the total range is meaningless if there are several seq-ids
428 // in the location
430 {
431  TRange total_range;
432  switch ( Which() ) {
433  case e_not_set:
434  case e_Null:
435  {
436  // Ignore locations without id
437  total_range = TRange::GetEmpty();
438  break;
439  }
440  case e_Empty:
441  {
442  x_UpdateId(id, &GetEmpty());
443  total_range = TRange::GetEmpty();
444  break;
445  }
446  case e_Whole:
447  {
448  x_UpdateId(id, &GetWhole());
449  total_range = TRange::GetWhole();
450  break;
451  }
452  case e_Int:
453  {
454  const CSeq_interval& loc = GetInt();
455  x_UpdateId(id, &loc.GetId());
456  total_range.Set(loc.GetFrom(), loc.GetTo());
457  break;
458  }
459  case e_Pnt:
460  {
461  const CSeq_point& pnt = GetPnt();
462  x_UpdateId(id, &pnt.GetId());
463  TSeqPos pos = pnt.GetPoint();
464  total_range.Set(pos, pos);
465  break;
466  }
467  case e_Packed_int:
468  {
469  // Check ID of each interval
470  const CPacked_seqint& ints = GetPacked_int();
471  total_range = TRange::GetEmpty();
472  ITERATE ( CPacked_seqint::Tdata, ii, ints.Get() ) {
473  const CSeq_interval& loc = **ii;
474  x_UpdateId(id, &loc.GetId());
475  total_range += TRange(loc.GetFrom(), loc.GetTo());
476  }
477  break;
478  }
479  case e_Packed_pnt:
480  {
481  const CPacked_seqpnt& pnts = GetPacked_pnt();
482  x_UpdateId(id, &pnts.GetId());
483  total_range = TRange::GetEmpty();
485  TSeqPos pos = *pi;
486  total_range += TRange(pos, pos);
487  }
488  break;
489  }
490  case e_Mix:
491  {
492  // Check ID of each sub-location.
493  const CSeq_loc_mix& mix = GetMix();
494  total_range = TRange::GetEmpty();
495  ITERATE( CSeq_loc_mix::Tdata, li, mix.Get() ) {
496  // instead of Get... to be able to check ID
497  total_range += (*li)->x_CalculateTotalRangeCheckId(id);
498  }
499  break;
500  }
501  case e_Equiv:
502  {
503  // Does it make any sense to GetTotalRange() from an equiv?
504  const CSeq_loc_equiv::Tdata& equiv = GetEquiv().Get();
505  total_range = TRange::GetEmpty();
506  ITERATE( CSeq_loc_equiv::Tdata, li, equiv ) {
507  total_range += (*li)->x_CalculateTotalRangeCheckId(id);
508  }
509  break;
510  }
511  case e_Bond:
512  {
513  // Does it make any sense to GetTotalRange() from a bond?
514  const CSeq_bond& bond = GetBond();
515  const CSeq_point& pointA = bond.GetA();
516  x_UpdateId(id, &pointA.GetId());
517  TSeqPos pos = pointA.GetPoint();
518  total_range = TRange(pos, pos);
519  if ( bond.IsSetB() ) {
520  const CSeq_point& pointB = bond.GetB();
521  x_UpdateId(id, &pointB.GetId());
522  pos = pointB.GetPoint();
523  total_range += TRange(pos, pos);
524  }
525  break;
526  }
527 
528  case e_Feat:
529  default:
530  NCBI_THROW_FMT(CSeqLocException, eUnsupported,
531  "CSeq_loc::CalculateTotalRange(): "
532  "unsupported location type: "<<
533  SelectionName(Which()));
534  }
535 
536  return total_range;
537 }
538 
539 
540 int CSeq_loc::x_CompareSingleId(const CSeq_loc& loc, const CSeq_id* id1,
541  const CSeq_id* id2,
542  TCompareFlags flags) const
543 {
544  if ( !id1 || !id2 ) {
545  NCBI_THROW(CSeqLocException, eMultipleId,
546  "CSeq_loc::Compare(): "
547  "cannot compare locations with several different seq-ids");
548  }
549  if ( int diff = INT_ID_TO(int, id1->CompareOrdered(*id2)) ) {
550  // ids are different - order by them
551  return diff;
552  }
553 
556  TSeqPos from2 = loc.GetStart(eExtreme_Positional);
557  TSeqPos to2 = loc.GetStop(eExtreme_Positional);
558 
559  // (from > to) means circular location.
560  // Any circular location is less than (before) non-circular one.
561  // If both are circular, compare them regular way.
562  bool circular1 = from1 > to1;
563  bool circular2 = from2 > to2;
564  if ( int diff = circular2 - circular1 ) {
565  return diff;
566  }
567 
568  // smallest left extreme first
569  if ( from1 != from2 ) {
570  return from1 < from2? -1: 1;
571  }
572  // longest feature first
573  if ( to1 != to2 ) {
574  return to1 > to2? -1: 1;
575  }
576  if (flags & fCompare_Strand) {
577  if (!IsSetStrand()) {
578  return loc.IsSetStrand() ? -1 : 0;
579  }
580  else {
581  if (!loc.IsSetStrand()) return 1;
582  }
583  auto s1 = GetStrand();
584  auto s2 = loc.GetStrand();
585  if (s1 != s2) return s1 < s2 ? -1 : 1;
586  }
587  return 0;
588 }
589 
590 int CSeq_loc::Compare(const CSeq_loc& loc_arg) const
591 {
592  return Compare(loc_arg, fCompare_Default);
593 }
594 
595 
596 int CSeq_loc::Compare(const CSeq_loc& loc_arg, TCompareFlags flags) const
597 {
598  // first try fast single-id comparison
599  const CSeq_id* id1 = GetId();
600  const CSeq_id* id2 = id1 == NULL ? NULL : loc_arg.GetId();
601  if (id1 != NULL && id2 != NULL) {
602  return x_CompareSingleId(loc_arg, id1, id2, flags);
603  }
604  // Slow comparison of ranges on each Seq-id separately.
605  CSeq_loc_CI iter1(*this, CSeq_loc_CI::eEmpty_Allow);
606  CSeq_loc_CI iter2(loc_arg, CSeq_loc_CI::eEmpty_Allow);
607 
608  for ( ; iter1 && iter2; ) {
609  CRef<CSeq_loc> loc1, loc2;
610  for ( int k = 0; k < 2; ++k ) {
611  CSeq_loc_CI& iter = k? iter2: iter1;
612  CRef<CSeq_loc>& loc = k? loc2: loc1;
613  // skip null locations (no Seq-id)
614  while ( iter && iter.GetSeq_id().Which() == CSeq_id::e_not_set ) {
615  ++iter;
616  }
617  if ( !iter ) {
618  // all the remaining segments were null -> end of location
619  loc = null;
620  continue;
621  }
622  const CSeq_id& id = iter.GetSeq_id();
623  loc = const_cast<CSeq_loc*>(&*iter.GetRangeAsSeq_loc());
624  // skip all sub-locations with the same Seq-id
625  while ( ++iter ) {
626  if ( !iter.GetSeq_id().Equals(id) ) {
627  if ( iter.GetSeq_id().Which() == CSeq_id::e_not_set ) {
628  // skip null sub-location
629  continue;
630  }
631  else {
632  // different non-null locations (has Seq-id)
633  break;
634  }
635  }
636  if ( !loc->IsMix() ) {
637  CRef<CSeq_loc> tmp = loc;
638  loc = new CSeq_loc;
639  loc->SetMix().AddSeqLoc(*tmp);
640  }
641  loc->SetMix().AddSeqLoc(const_cast<CSeq_loc&>(*iter.GetRangeAsSeq_loc()));
642  }
643  }
644  if ( loc1 && !loc2 ) {
645  // *this location is longer -> *this > loc
646  return 1;
647  }
648  if ( loc2 && !loc1 ) {
649  // second location is longer -> *this < loc
650  return -1;
651  }
652  if ( !loc1 && !loc2 ) {
653  // both locations ended
654  return 0;
655  }
656  id1 = loc1->GetId();
657  id2 = loc2->GetId();
658  if ( int diff = loc1->x_CompareSingleId(*loc2, id1, id2, flags) ) {
659  // next sub-locs are different
660  return diff;
661  }
662  }
663  if ( iter1 && !iter2 ) {
664  // *this location is longer -> *this > loc
665  return 1;
666  }
667  if ( iter2 && !iter1 ) {
668  // second locatio is longer -> *this < loc
669  return -1;
670  }
671  // same-length locations
672  return 0;
673 }
674 
675 
676 void CSeq_loc::PostRead(void) const
677 {
678  InvalidateCache();
679 }
680 
681 
683 
684 
685 // Transform a CSeq_loc into a CRange< TSeqPos >,
686 // that will be its total range.
687 static inline
688 CRange<TSeqPos> s_LocToRange(const CSeq_loc& loc )
689 {
690  try {
691  return loc.GetTotalRange();
692  }
693  catch ( CException& /*ignored*/ ) {
694  // assume empty
695  return CRange<TSeqPos>::GetEmpty();
696  }
697 }
698 
699 
700 // Transform a CSeq_interval into a CRange< TSeqPos >,
701 // that will be its total range.
702 static inline
704 {
705  return CRange<TSeqPos>(interval.GetFrom(), interval.GetTo());
706 };
707 
708 static inline
709 bool s_CheckIfMatchesBioseq( const CSeq_loc &loc,
710  const CSeq_loc::ISubLocFilter *filter )
711 {
712  return !filter || (*filter)( loc.GetId() );
713 }
714 
715 static inline
717  const CSeq_loc::ISubLocFilter *filter )
718 {
719  return !filter || (*filter)( &loc.GetId() );
720 }
721 
722 // Compare two ranges (reversed on minus strand)
723 static inline
725  const COpenRange<TSeqPos> &y_rng,
726  bool minus_strand)
727 {
728  if ( minus_strand ) {
729  // This is backwards for compatibliity with C
730  // Minus strand features should come in revered order
731 
732  // largest right extreme last
733  if ( x_rng.GetToOpen() != y_rng.GetToOpen() ) {
734  return x_rng.GetToOpen() < y_rng.GetToOpen()? -1: 1;
735  }
736  // longest last
737  if ( x_rng.GetFrom() != y_rng.GetFrom() ) {
738  return x_rng.GetFrom() > y_rng.GetFrom()? -1: 1;
739  }
740  }
741  else {
742  // smallest left extreme first
743  if ( x_rng.GetFrom() != y_rng.GetFrom() ) {
744  return x_rng.GetFrom() < y_rng.GetFrom()? -1: 1;
745  }
746  // longest first
747  if ( x_rng.GetToOpen() != y_rng.GetToOpen() ) {
748  return x_rng.GetToOpen() > y_rng.GetToOpen()? -1: 1;
749  }
750  }
751  return 0;
752 }
753 
754 
755 // Compare two containers with intervals (CSeq_loc or CSeq_interval)
756 template< class Container1, class Container2 >
757 int s_CompareIntervals(const Container1& container1,
758  const Container2& container2,
759  bool minus_strand,
760  const CSeq_loc::ISubLocFilter *filter )
761 {
762  typename Container1::const_iterator iter1 = container1.begin();
763  typename Container1::const_iterator iter1end = container1.end();
764  typename Container2::const_iterator iter2 = container2.begin();
765  typename Container2::const_iterator iter2end = container2.end();
766  for( ; iter1 != iter1end && iter2 != iter2end ; ++iter1, ++iter2 ) {
767 
768  // If specified, skip far location pieces
769  if( NULL != filter ) {
770  while( iter1 != iter1end && ! s_CheckIfMatchesBioseq(**iter1, filter) ) {
771  ++iter1;
772  }
773  while( iter2 != iter2end && ! s_CheckIfMatchesBioseq(**iter2, filter) ) {
774  ++iter2;
775  }
776  if( iter1 == iter1end || iter2 == iter2end ) {
777  break;
778  }
779  }
780 
781  if ( int diff = s_CompareRanges(s_LocToRange(**iter1),
782  s_LocToRange(**iter2),
783  minus_strand) ) {
784  return diff;
785  }
786  }
787 
788  // finally, shorter sequence first
789 
790  if( iter1 == iter1end ) {
791  if( iter2 == iter2end ) {
792  return 0;
793  } else {
794  return -1;
795  }
796  } else {
797  return 1;
798  }
799 }
800 
801 
803 
804 
805 int CSeq_loc::CompareSubLoc(const CSeq_loc& loc2, ENa_strand strand,
806  const CSeq_loc::ISubLocFilter *filter) const
807 {
808  bool minus_strand = IsReverse(strand);
809  if ( IsMix() ) {
810  if ( loc2.IsMix() ) {
811  return s_CompareIntervals(GetMix().Get(),
812  loc2.GetMix().Get(),
813  minus_strand,
814  filter );
815  }
816  else if ( loc2.IsPacked_int() ) {
817  return s_CompareIntervals(GetMix().Get(),
818  loc2.GetPacked_int().Get(),
819  minus_strand,
820  filter );
821  }
822  else {
823  // complex loc1 is last on plus strand and first on minus strand
824  return minus_strand? -1: 1;
825  }
826  }
827  else if ( IsPacked_int() ) {
828  if ( loc2.IsMix() ) {
829  return -s_CompareIntervals(loc2.GetMix().Get(),
830  GetPacked_int().Get(),
831  minus_strand,
832  filter );
833  }
834  else if ( loc2.IsPacked_int() ) {
835  return s_CompareIntervals(GetPacked_int().Get(),
836  loc2.GetPacked_int().Get(),
837  minus_strand,
838  filter );
839  }
840  else {
841  // complex loc1 is last on plus strand and first on minus strand
842  return minus_strand? -1: 1;
843  }
844  }
845  else {
846  if ( loc2.IsMix() || loc2.IsPacked_int() ) {
847  // complex loc2 is last on plus strand and first on minus strand
848  return minus_strand? 1: -1;
849  }
850  else {
851  // two simple locations
852  return 0;
853  }
854  }
855 }
856 
857 
859 {
860  switch ( Which() ) {
861  case e_Int:
862  return GetInt().IsSetStrand();
863  case e_Pnt:
864  return GetPnt().IsSetStrand();
865  case e_Packed_int:
866  return GetPacked_int().IsSetStrand(flag);
867  case e_Packed_pnt:
868  return GetPacked_pnt().IsSetStrand();
869  case e_Mix:
870  return GetMix().IsSetStrand(flag);
871  case e_Bond:
872  return GetBond().IsSetStrand(flag);
873 
874  case e_Equiv:
875  case e_Feat:
876  default:
877  return false;
878  }
879 }
880 
881 
883 {
884  switch ( Which() ) {
885  case e_not_set:
886  case e_Null:
887  case e_Empty:
888  return eNa_strand_unknown;
889  case e_Whole:
890  return eNa_strand_both;
891  case e_Int:
892  return GetInt().IsSetStrand() ? GetInt().GetStrand() : eNa_strand_unknown;
893  case e_Pnt:
894  return GetPnt().IsSetStrand() ? GetPnt().GetStrand() : eNa_strand_unknown;
895  case e_Packed_int:
896  return GetPacked_int().GetStrand();
897  case e_Packed_pnt:
898  return GetPacked_pnt().IsSetStrand() ?
899  GetPacked_pnt().GetStrand() : eNa_strand_unknown;
900  case e_Mix:
901  return GetMix().GetStrand();
902  case e_Bond:
903  return GetBond().GetStrand();
904 
905  case e_Equiv:
906  case e_Feat:
907  default:
908  NCBI_THROW_FMT(CSeqLocException, eUnsupported,
909  "CSeq_loc::GetStrand(): unsupported location type"<<
910  SelectionName(Which()));
911  }
912 }
913 
914 
916 {
917  switch ( Which() ) {
918  case e_not_set:
919  case e_Null:
920  case e_Empty:
921  {
922  return kInvalidSeqPos;
923  }
924  case e_Whole:
925  {
926  return TRange::GetWhole().GetFrom();
927  }
928  case e_Int:
929  {
930  return GetInt().GetStart(ext);
931  }
932  case e_Pnt:
933  {
934  return GetPnt().GetPoint();
935  }
936  case e_Packed_int:
937  {
938  return GetPacked_int().GetStart(ext);
939  }
940  case e_Packed_pnt:
941  {
942  return GetPacked_pnt().GetStart(ext);
943  }
944  case e_Mix:
945  {
946  return GetMix().GetStart(ext);
947  }
948  case e_Bond:
949  {
950  return GetBond().GetStart(ext);
951  }
952 
953  case e_Equiv:
954  case e_Feat:
955  default:
956  NCBI_THROW_FMT(CSeqLocException, eUnsupported,
957  "CSeq_loc::GetStart(): "
958  "unsupported location type: "<<SelectionName(Which()));
959  }
960 }
961 
962 
964 {
965  switch ( Which() ) {
966  case e_not_set:
967  case e_Null:
968  case e_Empty:
969  {
970  return kInvalidSeqPos;
971  }
972  case e_Whole:
973  {
974  return TRange::GetWhole().GetTo();
975  }
976  case e_Int:
977  {
978  return GetInt().GetStop(ext);
979  }
980  case e_Pnt:
981  {
982  return GetPnt().GetPoint();
983  }
984  case e_Packed_int:
985  {
986  return GetPacked_int().GetStop(ext);
987  }
988  case e_Packed_pnt:
989  {
990  return GetPacked_pnt().GetStop(ext);
991  }
992  case e_Mix:
993  {
994  return GetMix().GetStop(ext);
995  }
996  case e_Bond:
997  {
998  return GetBond().GetStop(ext);
999  }
1000 
1001  case e_Equiv:
1002  case e_Feat:
1003  default:
1004  NCBI_THROW_FMT(CSeqLocException, eUnsupported,
1005  "CSeq_loc::GetStop(): "
1006  "unsupported location type: "<<SelectionName(Which()));
1007  }
1008 }
1009 
1010 
1012 {
1013  if (seq_len == kInvalidSeqPos) {
1014  return GetTotalRange().GetLength();
1015  }
1018  bool minus = IsReverseStrand();
1019 
1020  if (start < stop) {
1021  return minus ? (seq_len - stop + start + 1) : (stop - start + 1);
1022  } else {
1023  return minus ? (start - stop + 1) : (seq_len - start + stop + 1);
1024  }
1025 }
1026 
1027 
1029 {
1030  return CSeq_loc_CI(*this);
1031 }
1032 
1033 
1035 {
1036  return CSeq_loc_CI();
1037 }
1038 
1039 
1040 // CSeq_loc_CI implementation
1041 
1043  : m_IsSetStrand(false),
1044  m_Strand(eNa_strand_unknown)
1045 {
1046 }
1047 
1048 
1050 {
1051 }
1052 
1053 
1055 {
1056 public:
1057  CSeq_loc_CI_Impl(void);
1058  CSeq_loc_CI_Impl(const CSeq_loc& loc,
1059  CSeq_loc_CI::EEmptyFlag empty_flag,
1061  virtual ~CSeq_loc_CI_Impl(void) {}
1062 
1064  typedef vector<SSeq_loc_CI_RangeInfo> TRanges;
1065 
1066  struct SEquivSet {
1068 
1069  // parts' end offsets within the equiv set, ordered by position
1070  // must contain at least one element - the only part
1071  // all parts must be not empty - contain at least one element
1072  typedef vector<size_t> TParts;
1073  typedef TParts::const_iterator TPart_CI;
1074  typedef TParts::iterator TPart_I;
1075 
1077 
1078  // return number of Seq-loc element in all equiv parts (always > 0)
1079  size_t GetElementsCount(void) const
1080  {
1081  _ASSERT(!m_Parts.empty());
1082  _ASSERT(m_Parts.back() > 0);
1083  return m_Parts.back();
1084  }
1085  // return index of the first element in the equiv set
1086  size_t GetStartIndex(void) const
1087  {
1088  return m_StartIndex;
1089  }
1090  // return index of element after the equiv set
1091  size_t GetEndIndex(void) const // exclusive
1092  {
1093  return GetStartIndex()+GetElementsCount();
1094  }
1095 
1096  // return true if the equiv set contains element with specified index
1097  bool Contains(size_t idx) const
1098  {
1099  return idx >= GetStartIndex() && idx < GetEndIndex();
1100  }
1101 
1102  // return number of equiv parts (always > 0)
1103  size_t GetPartsCount(void) const
1104  {
1105  _ASSERT(!m_Parts.empty());
1106  return m_Parts.size();
1107  }
1108 
1109  // return offset of the first element in equiv part within equiv set
1110  size_t GetPartStartOffset(TPart_CI part) const
1111  {
1112  _ASSERT(part < m_Parts.end());
1113  return part == m_Parts.begin()? 0: *--part;
1114  }
1115  // return offset of the end element in equiv part within equiv set
1116  size_t GetPartEndOffset(TPart_CI part) const
1117  {
1118  _ASSERT(part < m_Parts.end());
1119  return *part;
1120  }
1121 
1122  // return index of the first element in equiv part within equiv set
1123  size_t GetPartStartIndex(TPart_CI part) const
1124  {
1125  return GetStartIndex()+GetPartStartOffset(part);
1126  }
1127  // return index of the end element in equiv part within equiv set
1128  size_t GetPartEndIndex(TPart_CI part) const
1129  {
1130  return GetStartIndex()+GetPartEndOffset(part);
1131  }
1132 
1133  // return part containing element with specified index
1135  {
1136  _ASSERT(Contains(idx));
1137  return upper_bound(m_Parts.begin(), m_Parts.end(),
1138  idx - GetStartIndex());
1139  }
1140  // return part containing element with specified index
1142  {
1143  _ASSERT(Contains(idx));
1144  return upper_bound(m_Parts.begin(), m_Parts.end(),
1145  idx - GetStartIndex());
1146  }
1147  };
1148  struct PByLevel {
1149  bool operator()(const SEquivSet* e1, const SEquivSet* e2) const
1150  {
1151  size_t s1 = e1->GetElementsCount();
1152  size_t s2 = e2->GetElementsCount();
1153  if ( s1 != s2 ) {
1154  // smallest first
1155  return s1 < s2;
1156  }
1157  s1 = e1->GetPartsCount();
1158  s2 = e2->GetPartsCount();
1159  if ( s1 != s2 ) {
1160  // enclosing set must have single part with the inner set
1161  _ASSERT(s1 == 1 || s2 == 1);
1162  // more parts (inner) first
1163  return s1 > s2;
1164  }
1165  // it's still possible that both equiv sets contain
1166  // one part with the same set of elements
1167  // in this case the sets are equivalent and one set contains
1168  // another, no matter which one, but we still order the sets
1169  // by their creation order
1170  return e1 < e2;
1171  }
1172  };
1173  typedef vector<SEquivSet> TEquivSets;
1174 
1175  TRanges& GetRanges(void) { return m_Ranges; }
1176  const TRanges& GetRanges(void) const { return m_Ranges; }
1177 
1178  bool IsEnd(size_t idx) const { return idx >= m_Ranges.size(); }
1179 
1180  void DeleteRange(size_t idx);
1182 
1183  // Point/Interval support
1184 
1185  // If any part of the location part is changed the embedding location
1186  // becomes invalid.
1187  // This method resets embedding location if it's related to the part only.
1189  {
1190  return info.m_Loc && info.m_Loc->IsPnt();
1191  }
1193  {
1194  if ( !info.m_Loc ) {
1195  return false;
1196  }
1197  switch ( info.m_Loc->Which() ) {
1198  case CSeq_loc::e_Pnt:
1200  case CSeq_loc::e_Bond:
1201  return true;
1202  default:
1203  return false;
1204  }
1205  }
1206  // Update CSeq_point object if any
1207  void UpdatePoint(CSeq_point& pnt,
1208  const SSeq_loc_CI_RangeInfo& info) const;
1209  // Update CSeq_point object if any
1211  // Create CSeq_point object
1213  // Update embedded personal Seq-loc object, making sure it's not a point
1215 
1216  bool CanBePoint(const SSeq_loc_CI_RangeInfo& info) const;
1217 
1218  bool CanBeInterval(const SSeq_loc_CI_RangeInfo& info) const;
1219  bool CanBePoint(size_t idx) const
1220  {
1221  return CanBePoint(m_Ranges[idx]);
1222  }
1223  bool CanBeInterval(size_t idx) const
1224  {
1225  return CanBeInterval(m_Ranges[idx]);
1226  }
1227  bool CanBePacked_pnt(size_t idx_begin, size_t idx_end) const;
1228  bool CanBePacked_int(size_t idx_begin, size_t idx_end) const;
1229 
1230  CRef<CInt_fuzz> MakeFuzz(const CInt_fuzz& fuzz) const;
1233 
1236 
1237  CRef<CSeq_loc> MakeLocPacked_pnt(size_t idx_begin, size_t idx_end) const;
1238  CRef<CSeq_loc> MakeLocPacked_int(size_t idx_begin, size_t idx_end) const;
1239 
1240  // Bond support
1241  bool CanBeBond(size_t idx_begin, size_t idx_end) const;
1242  size_t GetBondBegin(size_t idx) const;
1243  size_t GetBondEnd(size_t idx) const;
1244  pair<size_t, size_t> GetBondRange(size_t idx) const
1245  {
1246  return make_pair(GetBondBegin(idx), GetBondEnd(idx));
1247  }
1249  {
1250  return info.m_Loc && info.m_Loc->IsBond();
1251  }
1252  bool IsInBond(size_t idx) const
1253  {
1254  return IsInBond(m_Ranges[idx]);
1255  }
1256  bool IsBondPartA(size_t idx) const
1257  {
1258  return IsInBond(idx) && idx == GetBondBegin(idx);
1259  }
1260  bool IsBondPartB(size_t idx) const
1261  {
1262  return IsInBond(idx) && idx == GetBondBegin(idx)+1;
1263  }
1264  void x_BreakBond(size_t idx)
1265  {
1266  SetPoint(m_Ranges[idx]);
1267  }
1268  void x_CreateBond(size_t idx)
1269  {
1270  CRef<CSeq_loc> loc(new CSeq_loc);
1271  loc->SetBond();
1272  m_Ranges[idx].m_Loc = loc;
1273  }
1274  void x_AddBondPartB(size_t idx)
1275  {
1276  // add part B be to the bond
1277  m_Ranges[idx+1].m_Loc = m_Ranges[idx].m_Loc;
1278  }
1279  void RemoveBond(size_t idx);
1280  void MakeBondA(size_t idx);
1281  void MakeBondAB(size_t idx);
1282  void MakeBondB(size_t idx);
1283  CRef<CSeq_loc> MakeLocBond(size_t idx_begin, size_t idx_end) const;
1284 
1285  // Equiv support
1286  bool HasEquivSets(void) const
1287  {
1288  return !m_EquivSets.empty();
1289  }
1290  bool IsInEquivSet(size_t idx) const
1291  {
1292  ITERATE ( TEquivSets, it, m_EquivSets ) {
1293  if ( it->Contains(idx) ) {
1294  return true;
1295  }
1296  }
1297  return false;
1298  }
1299  size_t GetEquivSetsCount(size_t idx) const
1300  {
1301  size_t count = 0;
1302  ITERATE ( TEquivSets, it, m_EquivSets ) {
1303  if ( it->Contains(idx) ) {
1304  ++count;
1305  }
1306  }
1307  return count;
1308  }
1309  const SEquivSet& GetEquivSet(size_t idx, size_t level) const
1310  {
1311  vector<const SEquivSet*> sets;
1312  ITERATE ( TEquivSets, it, m_EquivSets ) {
1313  if ( it->Contains(idx) ) {
1314  sets.push_back(&*it);
1315  }
1316  }
1317  if ( level >= sets.size() ) {
1318  NCBI_THROW_FMT(CSeqLocException, eOutOfRange,
1319  "CSeq_loc_CI: bad equiv set level: "<<level);
1320  }
1321  sort(sets.begin(), sets.end(), PByLevel());
1322  return *sets[level];
1323  }
1324  pair<size_t, size_t> GetEquivSetRange(size_t idx, size_t level) const
1325  {
1326  const SEquivSet& s = GetEquivSet(idx, level);
1327  return make_pair(s.GetStartIndex(), s.GetEndIndex());
1328  }
1329  pair<size_t, size_t> GetEquivPartRange(size_t idx, size_t level) const
1330  {
1331  const SEquivSet& s = GetEquivSet(idx, level);
1333  return make_pair(s.GetPartStartIndex(p), s.GetPartEndIndex(p));
1334  }
1335  // Return first equiv part bound point that fall into the range.
1336  // Beginning and ending points are not counted, so 0 is not possible.
1337  // Return 0 if there are no such breaks.
1338  size_t HasEquivBreak(size_t begin, size_t end) const;
1339 
1341  const SEquivSet* FindInnerEquivSet(size_t begin, size_t end,
1342  const TUsedEquivs& used_equivs) const;
1343 
1346  CRef<CSeq_loc> MakeLoc(CSeq_loc_I::EMakeType make_type) const;
1347  CRef<CSeq_loc> MakeLoc(size_t idx_begin,
1348  size_t idx_end,
1349  CSeq_loc_I::EMakeType make_type,
1350  TUsedEquivs& used_equivs) const;
1351 
1352  bool HasChanges(void) const
1353  {
1354  return m_HasChanges;
1355  }
1356 
1357  void SetHasChanges(void)
1358  {
1359  m_HasChanges = true;
1360  }
1361 
1363  {
1364  return m_EquivMode;
1365  }
1367  {
1368  m_EquivMode = mode;
1369  }
1370 
1371 private:
1372  // Never copy this object
1375 
1376  // Process the location, fill the list
1377  void x_ProcessLocation(const CSeq_loc& loc);
1378  void x_ProcessInterval(const CSeq_interval& seq_int,
1379  const CSeq_loc& loc);
1380  void x_ProcessPoint(const CSeq_point& seq_pnt,
1381  const CSeq_loc& loc);
1382 
1383  static void x_SetId(SSeq_loc_CI_RangeInfo& info, const CSeq_id& id);
1384 
1385  // Prevent seq-loc destruction
1387  // List of intervals
1389  // List of equiv parts
1391  // Empty locations processing option
1393  // true if anything was changed in the Seq-loc
1395  // equiv editing mode
1397 };
1398 
1399 
1401  : m_HasChanges(false),
1402  m_EquivMode(CSeq_loc_I::eEquiv_none)
1403 {
1404 }
1405 
1406 
1408  CSeq_loc_CI::EEmptyFlag empty_flag,
1410  : m_Location(&loc),
1411  m_EmptyFlag(empty_flag),
1412  m_HasChanges(false),
1413  m_EquivMode(CSeq_loc_I::eEquiv_none)
1414 {
1415  x_ProcessLocation(loc);
1416  if ( order == CSeq_loc_CI::eOrder_Positional && loc.IsReverseStrand() ) {
1417  reverse(m_Ranges.begin(), m_Ranges.end());
1418  }
1419 }
1420 
1421 
1423  const CSeq_id& id)
1424 {
1425  info.m_Id = &id;
1426  info.m_IdHandle = CSeq_id_Handle::GetHandle(id);
1427 }
1428 
1429 
1430 template<class Container>
1431 static void s_ReserveMore(Container& cont, size_t add_size)
1432 {
1433  if ( cont.size()+add_size > cont.capacity() ) {
1434  cont.reserve(max(cont.size()+add_size, cont.capacity()*2));
1435  }
1436 }
1437 
1438 
1439 void CSeq_loc_CI_Impl::x_ProcessLocation(const CSeq_loc& loc)
1440 {
1441  switch ( loc.Which() ) {
1442  case CSeq_loc::e_not_set:
1443  /*
1444  NCBI_THROW(CSeqLocException, eNotSet,
1445  "CSeq_loc_CI: location is not set");
1446  */
1447  case CSeq_loc::e_Null:
1448  case CSeq_loc::e_Empty:
1449  {
1452  if (loc.Which() == CSeq_loc::e_Empty) {
1453  x_SetId(info, loc.GetEmpty());
1454  }
1455  else {
1456  info.m_Id.Reset(new CSeq_id);
1457  }
1458  info.m_Range = TRange::GetEmpty();
1459  info.m_Loc = &loc;
1460  m_Ranges.push_back(info);
1461  }
1462  return;
1463  }
1464  case CSeq_loc::e_Whole:
1465  {
1467  x_SetId(info, loc.GetWhole());
1468  info.m_Range = TRange::GetWhole();
1469  info.m_Loc = &loc;
1470  m_Ranges.push_back(info);
1471  return;
1472  }
1473  case CSeq_loc::e_Int:
1474  {
1475  x_ProcessInterval(loc.GetInt(), loc);
1476  return;
1477  }
1478  case CSeq_loc::e_Pnt:
1479  {
1480  x_ProcessPoint(loc.GetPnt(), loc);
1481  return;
1482  }
1484  {
1485  const CPacked_seqint::Tdata& data = loc.GetPacked_int().Get();
1486  s_ReserveMore(m_Ranges, data.size());
1488  x_ProcessInterval(**ii, loc);
1489  }
1490  return;
1491  }
1493  {
1494  const CPacked_seqpnt& pack_pnt = loc.GetPacked_pnt();
1495  s_ReserveMore(m_Ranges, pack_pnt.GetPoints().size());
1497  x_SetId(info, pack_pnt.GetId());
1498  if ( pack_pnt.IsSetStrand() ) {
1499  info.SetStrand(pack_pnt.GetStrand());
1500  }
1501  if (pack_pnt.IsSetFuzz()) {
1502  info.m_Fuzz.first = info.m_Fuzz.second = &pack_pnt.GetFuzz();
1503  }
1504  info.m_Loc = &loc;
1505  ITERATE ( CPacked_seqpnt::TPoints, pi, pack_pnt.GetPoints() ) {
1506  info.m_Range.Set(*pi, *pi);
1507  m_Ranges.push_back(info);
1508  }
1509  return;
1510  }
1511  case CSeq_loc::e_Mix:
1512  {
1513  const CSeq_loc_mix::Tdata& data = loc.GetMix().Get();
1514  s_ReserveMore(m_Ranges, data.size());
1516  x_ProcessLocation(**li);
1517  }
1518  return;
1519  }
1520  case CSeq_loc::e_Equiv:
1521  {
1522  const CSeq_loc_equiv::Tdata& data = loc.GetEquiv().Get();
1523  s_ReserveMore(m_Ranges, data.size());
1524  SEquivSet eq_set;
1525  size_t start = m_Ranges.size();
1526  eq_set.m_StartIndex = start;
1528  size_t part_start = m_Ranges.size();
1529  x_ProcessLocation(**li);
1530  size_t part_end = m_Ranges.size();
1531  if ( part_start != part_end ) {
1532  // add only non-empty parts
1533  eq_set.m_Parts.push_back(part_end-start);
1534  }
1535  }
1536  if ( !eq_set.m_Parts.empty() ) {
1537  // add only non-empty equiv sets
1538  m_EquivSets.push_back(eq_set);
1539  }
1540  return;
1541  }
1542  case CSeq_loc::e_Bond:
1543  {
1544  const CSeq_bond& bond = loc.GetBond();
1545  x_ProcessPoint(bond.GetA(), loc);
1546  if ( bond.IsSetB() ) {
1547  x_ProcessPoint(bond.GetB(), loc);
1548  }
1549  return;
1550  }
1551  default:
1552  NCBI_THROW_FMT(CSeqLocException, eUnsupported,
1553  "CSeq_loc_CI: unsupported location type: "<<
1554  loc.SelectionName(loc.Which()));
1555  }
1556 }
1557 
1558 
1560  const CSeq_loc& loc)
1561 {
1562  _ASSERT(loc.Which() == CSeq_loc::e_Int ||
1563  loc.Which() == CSeq_loc::e_Packed_int);
1565  x_SetId(info, seq_int.GetId());
1566  info.m_Range.Set(seq_int.GetFrom(), seq_int.GetTo());
1567  if ( seq_int.IsSetStrand() ) {
1568  info.SetStrand(seq_int.GetStrand());
1569  }
1570  info.m_Loc = &loc;
1571  if (seq_int.IsSetFuzz_from()) {
1572  info.m_Fuzz.first = &seq_int.GetFuzz_from();
1573  }
1574  if (seq_int.IsSetFuzz_to()) {
1575  info.m_Fuzz.second = &seq_int.GetFuzz_to();
1576  }
1577  m_Ranges.push_back(info);
1578 }
1579 
1580 
1582  const CSeq_loc& loc)
1583 {
1584  _ASSERT(loc.Which() == CSeq_loc::e_Pnt ||
1585  loc.Which() == CSeq_loc::e_Bond);
1587  x_SetId(info, seq_pnt.GetId());
1588  info.m_Range.Set(seq_pnt.GetPoint(), seq_pnt.GetPoint());
1589  if ( seq_pnt.IsSetStrand() ) {
1590  info.SetStrand(seq_pnt.GetStrand());
1591  }
1592  info.m_Loc = &loc;
1593  if (seq_pnt.IsSetFuzz()) {
1594  info.m_Fuzz.first = info.m_Fuzz.second = &seq_pnt.GetFuzz();
1595  }
1596  m_Ranges.push_back(info);
1597 }
1598 
1599 
1601  size_t end) const
1602 {
1603  size_t ret = end;
1604  ITERATE ( TEquivSets, it, m_EquivSets ) {
1605  const SEquivSet& eq_set = *it;
1606  size_t eq_begin = eq_set.GetStartIndex();
1607  size_t eq_end = eq_set.GetEndIndex();
1608  if ( eq_end <= begin || eq_begin >= end ) {
1609  // no overlaps
1610  continue;
1611  }
1612  if ( eq_begin > begin && eq_begin < end ) {
1613  // range overlaps with the beginning of the equiv set
1614  ret = min(ret, eq_begin);
1615  continue;
1616  }
1617  // otherwise the equiv starts before the range and ends somewhere
1618  // after beginning of the range.
1619  // so the first part that contains range's begin point is the one,
1620  // end its end point is one of the breaking points we are looking for.
1621  SEquivSet::TPart_CI part = eq_set.FindPartByElementIndex(begin);
1622  ret = min(ret, eq_set.GetPartEndIndex(part));
1623  }
1624  // the first break point may be after the requested range
1625  return ret == end? 0: ret;
1626 }
1627 
1628 
1631  size_t end,
1632  const TUsedEquivs& used_equivs) const
1633 {
1634  const SEquivSet* ret = 0;
1635  ITERATE ( TEquivSets, it, m_EquivSets ) {
1636  const SEquivSet& eq_set = *it;
1637  size_t eq_begin = eq_set.GetStartIndex();
1638  size_t eq_end = eq_set.GetEndIndex();
1639  if ( eq_begin < begin || eq_end > end ) {
1640  continue;
1641  }
1642  if ( used_equivs.find(&eq_set) != used_equivs.end() ) {
1643  // already processed
1644  continue;
1645  }
1646  if ( !ret || PByLevel()(ret, &eq_set) ) {
1647  ret = &eq_set;
1648  }
1649  }
1650  return ret;
1651 }
1652 
1653 
1655 {
1656  SetHasChanges();
1657  m_Ranges.erase(m_Ranges.begin()+idx);
1658  // update equiv sets
1660  SEquivSet& eq_set = *it;
1661  size_t start_index = eq_set.GetStartIndex();
1662  if ( idx < start_index ) {
1663  // shift whole equiv set because an element before is deleted
1664  --eq_set.m_StartIndex;
1665  continue;
1666  }
1667  size_t offset = idx - start_index;
1668  size_t prev_offset = 0;
1669  ERASE_ITERATE ( SEquivSet::TParts, it2, eq_set.m_Parts ) {
1670  if ( offset < *it2 ) {
1671  --*it2;
1672  if ( *it2 == prev_offset ) {
1673  VECTOR_ERASE(it2, eq_set.m_Parts);
1674  }
1675  else {
1676  prev_offset = *it2;
1677  }
1678  }
1679  }
1680  if ( eq_set.GetElementsCount() == 0 ) {
1682  }
1683  }
1684 }
1685 
1686 
1687 static inline bool s_InsertExpands(size_t idx, size_t start, size_t end)
1688 {
1689  return (idx > start && idx <= end) || (idx == start && idx == 0);
1690 }
1691 
1692 
1695 {
1696  SetHasChanges();
1698  *m_Ranges.insert(m_Ranges.begin()+idx, SSeq_loc_CI_RangeInfo());
1699  CConstRef<CSeq_loc> loc;
1700  // connect with previous or next part (if any)
1701  if ( idx > 0 ) {
1702  loc = m_Ranges[idx-1].m_Loc;
1703  }
1704  else if ( idx < m_Ranges.size() ) {
1705  loc = m_Ranges[idx].m_Loc;
1706  }
1707  if ( loc ) {
1708  // if type is incompatible with part it's connected to, do not connect
1709  switch ( loc->Which() ) {
1710  case CSeq_loc::e_Null:
1711  case CSeq_loc::e_Empty:
1712  case CSeq_loc::e_Whole:
1713  case CSeq_loc::e_Pnt:
1714  case CSeq_loc::e_Int:
1715  loc = null;
1716  break;
1717  case CSeq_loc::e_Bond:
1718  if ( type != CSeq_loc::e_Pnt ) {
1719  loc = null;
1720  }
1721  break;
1723  if ( type != CSeq_loc::e_Pnt ) {
1724  loc = null;
1725  }
1726  break;
1728  if ( type != CSeq_loc::e_Pnt && type != CSeq_loc::e_Int ) {
1729  loc = null;
1730  }
1731  break;
1732  default:
1733  break;
1734  }
1735  }
1736  info.m_Loc = loc;
1737 
1738  SEquivSet* add_to_set_beg = 0;
1739  vector<SEquivSet*> add_to_set_mid;
1740  SEquivSet* add_to_set_end = 0;
1741  // update existing equiv sets
1743  SEquivSet& eq_set = *it;
1744  size_t eq_start = eq_set.GetStartIndex();
1745  size_t eq_end = eq_set.GetEndIndex();
1746  if ( idx == eq_start &&
1749  // new element is inserted just before the equiv and will be added
1750  // shift it for now
1751  ++eq_set.m_StartIndex;
1752  if ( !add_to_set_beg || PByLevel()(&eq_set, add_to_set_beg) ) {
1753  add_to_set_beg = &eq_set;
1754  }
1755  }
1756  else if ( idx == eq_end &&
1759  // new element is inserted just after the equiv and will be added
1760  if ( !add_to_set_end || PByLevel()(&eq_set, add_to_set_end) ) {
1761  add_to_set_end = &eq_set;
1762  }
1763  }
1764  else if ( idx > eq_start && idx < eq_end ) {
1765  // new element is completely within eq_set, so it will be added
1766  add_to_set_mid.push_back(&eq_set);
1767  }
1768  else if ( idx <= eq_start ) {
1769  // new element is completely before the equiv - shift right
1770  ++eq_set.m_StartIndex;
1771  }
1772  }
1773  sort(add_to_set_mid.begin(), add_to_set_mid.end(), PByLevel());
1774  if ( add_to_set_beg ) {
1775  // add at the beginning of the set
1776  NON_CONST_ITERATE ( SEquivSet::TParts, it, add_to_set_beg->m_Parts ) {
1777  ++*it;
1778  }
1780  // add as a new part
1781  add_to_set_beg->m_Parts.insert(add_to_set_beg->m_Parts.begin(), 1);
1783  }
1784  }
1785  else if ( add_to_set_end ) {
1786  // add at the end of the set
1788  // add as a new part
1789  add_to_set_end->m_Parts.push_back(add_to_set_end->m_Parts.back()+1);
1791  }
1792  else {
1793  ++add_to_set_end->m_Parts.back();
1794  }
1795  }
1797  !add_to_set_mid.empty() ) {
1798  SEquivSet& eq_set = **add_to_set_mid.begin();
1799  add_to_set_mid.erase(add_to_set_mid.begin());
1800  SEquivSet::TPart_I it = eq_set.FindPartByElementIndex(idx);
1801  it = eq_set.m_Parts.insert(it, idx-eq_set.GetStartIndex()+1)+1;
1802  for ( ; it != eq_set.m_Parts.end(); ++it ) {
1803  ++*it;
1804  }
1806  }
1807  ITERATE ( vector<SEquivSet*>, set_it, add_to_set_mid ) {
1808  SEquivSet& eq_set = **set_it;
1809  SEquivSet::TPart_I it = eq_set.FindPartByElementIndex(idx);
1810  for ( ; it != eq_set.m_Parts.end(); ++it ) {
1811  ++*it;
1812  }
1813  }
1814  // now all existing equivs were updated
1815 
1817  // create new equiv
1818  SEquivSet eq_set;
1819  eq_set.m_StartIndex = idx;
1820  eq_set.m_Parts.push_back(1);
1821  m_EquivSets.push_back(eq_set);
1823  }
1824  return info;
1825 }
1826 
1827 
1829 {
1830  if ( info.m_Range.GetLength() != 1 ) {
1831  return false;
1832  }
1833  if ( info.m_Fuzz.first != info.m_Fuzz.second ) {
1834  return false;
1835  }
1836  if ( !info.m_IdHandle ) {
1837  return false;
1838  }
1839  return true;
1840 }
1841 
1842 
1844 {
1845  if ( info.m_Range.Empty() || info.m_Range.IsWhole() ) {
1846  return false;
1847  }
1848  if ( !info.m_IdHandle ) {
1849  return false;
1850  }
1851  return true;
1852 }
1853 
1854 
1855 bool CSeq_loc_CI_Impl::CanBeBond(size_t idx_begin, size_t idx_end) const
1856 {
1857  size_t count = idx_end - idx_begin;
1858  if ( count != 1 && count != 2 ) {
1859  return false;
1860  }
1861  if ( !IsInBond(idx_begin) ) {
1862  return false;
1863  }
1864  if ( GetBondRange(idx_begin) != make_pair(idx_begin, idx_end) ) {
1865  return false;
1866  }
1867  for ( size_t idx = idx_begin; idx < idx_end; ++idx ) {
1868  _ASSERT(IsInBond(idx));
1869  const SSeq_loc_CI_RangeInfo& info = m_Ranges[idx];
1870  if ( !CanBePoint(info) ) {
1871  return false;
1872  }
1873  }
1874  if ( HasEquivBreak(idx_begin, idx_end) ) {
1875  return false;
1876  }
1877  return true;
1878 }
1879 
1880 
1881 size_t CSeq_loc_CI_Impl::GetBondBegin(size_t idx) const
1882 {
1883  const CSeq_loc* loc = m_Ranges[idx].m_Loc.GetPointerOrNull();
1884  _ASSERT(loc && loc->IsBond());
1885  while ( idx > 0 && m_Ranges[idx-1].m_Loc == loc ) {
1886  --idx;
1887  }
1888  return idx;
1889 }
1890 
1891 
1892 size_t CSeq_loc_CI_Impl::GetBondEnd(size_t idx) const
1893 {
1894  const CSeq_loc* loc = m_Ranges[idx].m_Loc.GetPointerOrNull();
1895  _ASSERT(loc && loc->IsBond());
1896  while ( idx < m_Ranges.size() && m_Ranges[idx].m_Loc == loc ) {
1897  ++idx;
1898  }
1899  return idx;
1900 }
1901 
1902 
1904 {
1905  if ( !IsInBond(idx) ) {
1906  NCBI_THROW_FMT(CSeqLocException, eBadIterator,
1907  "CSeq_loc_I::RemoveBond(): "
1908  "there is no bond at current position");
1909  }
1910  pair<size_t, size_t> range = GetBondRange(idx);
1911  SetHasChanges();
1912  // unbond all parts
1913  for ( idx = range.first; idx < range.second; ++idx ) {
1914  x_BreakBond(idx);
1915  }
1916 }
1917 
1918 
1920 {
1921  pair<size_t, size_t> range;
1922  if ( IsInBond(idx) ) {
1923  range = GetBondRange(idx);
1924  }
1925  size_t bond_len = range.second-range.first;
1926  if ( bond_len > 0 ) {
1927  // update existing bond
1928  if ( idx != range.first ) {
1929  NCBI_THROW_FMT(CSeqLocException, eBadIterator,
1930  "CSeq_loc_I::MakeBondA(): "
1931  "current position is B part of other bond");
1932  }
1933  if ( bond_len == 1 ) {
1934  // already bond with only part A
1935  return;
1936  }
1937  SetHasChanges();
1938  // unbond other parts
1939  for ( idx = range.first+1; idx < range.second; ++idx ) {
1940  x_BreakBond(idx);
1941  }
1942  }
1943  else {
1944  SetHasChanges();
1945  x_CreateBond(idx);
1946  }
1947 }
1948 
1949 
1951 {
1952  if ( idx+1 >= m_Ranges.size() ) {
1953  NCBI_THROW_FMT(CSeqLocException, eBadIterator,
1954  "CSeq_loc_I::MakeBondAB(): "
1955  "no more parts in the location");
1956  }
1957  pair<size_t, size_t> range;
1958  if ( IsInBond(idx) ) {
1959  range = GetBondRange(idx);
1960  }
1961  size_t bond_len = range.second-range.first;
1962  if ( bond_len > 0 ) {
1963  // update existing bond
1964  if ( idx != range.first ) {
1965  NCBI_THROW_FMT(CSeqLocException, eBadIterator,
1966  "CSeq_loc_I::MakeBondAB(): "
1967  "current position is B part of other bond");
1968  }
1969  if ( bond_len == 2 ) {
1970  // already a bond with both parts A and B
1971  return;
1972  }
1973  SetHasChanges();
1974  if ( bond_len > 2 ) {
1975  // unbond extra parts after B
1976  for ( idx = range.first+2; idx < range.second; ++idx ) {
1977  x_BreakBond(idx);
1978  }
1979  return;
1980  }
1981  x_AddBondPartB(idx);
1982  }
1983  else {
1984  SetHasChanges();
1985  x_CreateBond(idx);
1986  x_AddBondPartB(idx);
1987  }
1988 }
1989 
1990 
1992 {
1993  if ( idx == 0 ) {
1994  NCBI_THROW_FMT(CSeqLocException, eBadIterator,
1995  "CSeq_loc_I::MakeBondB(): "
1996  "no parts before current");
1997  }
1998  pair<size_t, size_t> range;
1999  if ( IsInBond(idx) ) {
2000  range = GetBondRange(idx);
2001  }
2002  else if ( IsInBond(idx-1) ) {
2003  range = GetBondRange(idx-1);
2004  }
2005  size_t bond_len = range.second-range.first;
2006  if ( bond_len > 0 ) {
2007  // update existing bond
2008  if ( range.first != idx + 1 ) {
2009  NCBI_THROW_FMT(CSeqLocException, eBadIterator,
2010  "CSeq_loc_I::MakeBondB(): "
2011  "current position is not a B part of other bond");
2012  }
2013  if ( bond_len == 2 ) {
2014  // already a bond with both parts A and B
2015  return;
2016  }
2017  SetHasChanges();
2018  if ( bond_len > 2 ) {
2019  // unbond extra parts after B
2020  for ( idx = range.first+2; idx < range.second; ++idx ) {
2021  x_BreakBond(idx);
2022  }
2023  return;
2024  }
2025  _ASSERT(bond_len == 1);
2026  x_AddBondPartB(idx);
2027  }
2028  else {
2029  SetHasChanges();
2030  x_CreateBond(idx);
2031  x_AddBondPartB(idx);
2032  }
2033 }
2034 
2035 
2037  size_t idx_end) const
2038 {
2039  CRef<CSeq_loc> loc(new CSeq_loc);
2040  loc->SetBond().SetA(*MakePoint(m_Ranges[idx_begin]));
2041  if ( idx_begin+1 < idx_end ) {
2042  loc->SetBond().SetB(*MakePoint(m_Ranges[idx_begin+1]));
2043  }
2044  return loc;
2045 }
2046 
2047 
2048 static
2050 {
2051  if ( !info.m_IdHandle ) {
2053  "CSeq_loc_I: part id is null");
2054  }
2055  return Ref(&const_cast<CSeq_id&>(*info.m_Id));
2056 }
2057 
2058 
2060 {
2061  return Ref(&const_cast<CInt_fuzz&>(fuzz));
2062 }
2063 
2064 
2066  const SSeq_loc_CI_RangeInfo& info) const
2067 {
2068  pnt.SetId(*MakeId(info));
2069  pnt.SetPoint(info.m_Range.GetFrom());
2070  if ( info.m_IsSetStrand ) {
2071  pnt.SetStrand(info.m_Strand);
2072  }
2073  else {
2074  pnt.ResetStrand();
2075  }
2076  if ( info.m_Fuzz.first ) {
2077  pnt.SetFuzz(*MakeFuzz(*info.m_Fuzz.first));
2078  }
2079  else {
2080  pnt.ResetFuzz();
2081  }
2082 }
2083 
2084 
2086 {
2087  info.m_Loc = MakeLocPoint(info);
2088 }
2089 
2090 
2093 {
2094  CRef<CSeq_point> seq_pnt(new CSeq_point);
2095  UpdatePoint(*seq_pnt, info);
2096  return seq_pnt;
2097 }
2098 
2099 
2102 {
2103  CRef<CSeq_loc> loc(new CSeq_loc);
2104  loc->SetPnt(*MakePoint(info));
2105  return loc;
2106 }
2107 
2108 
2110 {
2111  SetHasChanges();
2112  if ( WasPoint(info) ) {
2113  UpdatePoint(const_cast<CSeq_point&>(info.m_Loc->GetPnt()), info);
2114  }
2115 }
2116 
2117 
2119 {
2120  SetHasChanges();
2121  if ( info.m_Loc ) {
2122  switch ( info.m_Loc->Which() ) {
2123  case CSeq_loc::e_Whole:
2124  case CSeq_loc::e_Empty:
2125  case CSeq_loc::e_Null:
2126  case CSeq_loc::e_Int:
2127  case CSeq_loc::e_Pnt:
2128  info.m_Loc = null;
2129  break;
2130  default:
2131  break;
2132  }
2133  }
2134 }
2135 
2136 
2139 {
2140  CRef<CSeq_interval> seq_int(new CSeq_interval);
2141  seq_int->SetId(*MakeId(info));
2142  seq_int->SetFrom(info.m_Range.GetFrom());
2143  seq_int->SetTo(info.m_Range.GetTo());
2144  if ( info.m_IsSetStrand ) {
2145  seq_int->SetStrand(info.m_Strand);
2146  }
2147  if ( info.m_Fuzz.first ) {
2148  seq_int->SetFuzz_from(*MakeFuzz(*info.m_Fuzz.first));
2149  }
2150  if ( info.m_Fuzz.second ) {
2151  seq_int->SetFuzz_to(*MakeFuzz(*info.m_Fuzz.second));
2152  }
2153  return seq_int;
2154 }
2155 
2156 
2159 {
2160  CRef<CSeq_loc> loc(new CSeq_loc);
2161  loc->SetInt(*MakeInterval(info));
2162  return loc;
2163 }
2164 
2165 
2168 {
2169  CRef<CSeq_loc> loc(new CSeq_loc);
2170  if ( info.m_Range.IsWhole() ) {
2171  loc->SetWhole(*MakeId(info));
2172  }
2173  else {
2174  if ( !info.m_Range.Empty() ) {
2175  NCBI_THROW(CSeqLocException, eOtherError,
2176  "CSeq_loc_I::MakeSeq_loc(): "
2177  "cannot determine type of loc part");
2178  }
2179  if ( info.m_IdHandle ) {
2180  loc->SetEmpty(*MakeId(info));
2181  }
2182  else {
2183  loc->SetNull();
2184  }
2185  }
2186  return loc;
2187 }
2188 
2189 
2192 {
2193  if ( ShouldBePoint(info) && CanBePoint(info) ) {
2194  return MakeLocPoint(info);
2195  }
2196  else if ( info.m_Range.IsWhole() || info.m_Range.Empty() ) {
2197  return MakeLocOther(info);
2198  }
2199  else {
2200  return MakeLocInterval(info);
2201  }
2202 }
2203 
2204 
2206  size_t idx_end) const
2207 {
2208  _ASSERT(idx_end >= idx_begin);
2209  if ( idx_end == idx_begin ) {
2210  return false;
2211  }
2212  const SSeq_loc_CI_RangeInfo& info0 = m_Ranges[idx_begin];
2213  for ( size_t idx = idx_begin; idx < idx_end; ++idx ) {
2214  if ( IsInBond(idx) ) {
2215  return false;
2216  }
2217  const SSeq_loc_CI_RangeInfo& info = m_Ranges[idx];
2218  if ( !CanBePoint(m_Ranges[idx]) ) {
2219  return false;
2220  }
2221  if ( idx != idx_begin ) {
2222  if ( info.m_IdHandle != info0.m_IdHandle ) {
2223  return false;
2224  }
2225  if ( info.m_IsSetStrand != info0.m_IsSetStrand ) {
2226  return false;
2227  }
2228  if ( info0.m_IsSetStrand && info.m_Strand != info0.m_Strand ) {
2229  return false;
2230  }
2231  if ( info.m_Fuzz.first != info0.m_Fuzz.first ) {
2232  return false;
2233  }
2234  }
2235  }
2236  if ( HasEquivBreak(idx_begin, idx_end) ) {
2237  return false;
2238  }
2239  return true;
2240 }
2241 
2242 
2244  size_t idx_end) const
2245 {
2246  _ASSERT(idx_end >= idx_begin);
2247  for ( size_t idx = idx_begin; idx < idx_end; ++idx ) {
2248  if ( IsInBond(idx) ) {
2249  return false;
2250  }
2251  if ( !CanBeInterval(idx) ) {
2252  return false;
2253  }
2254  }
2255  if ( HasEquivBreak(idx_begin, idx_end) ) {
2256  return false;
2257  }
2258  return true;
2259 }
2260 
2261 
2263  size_t idx_end) const
2264 {
2265  const SSeq_loc_CI_RangeInfo& info0 = m_Ranges[idx_begin];
2266  CRef<CSeq_loc> loc(new CSeq_loc);
2267  CPacked_seqpnt& pnts = loc->SetPacked_pnt();
2268  pnts.SetId(*MakeId(info0));
2269  if ( info0.m_IsSetStrand ) {
2270  pnts.SetStrand(info0.m_Strand);
2271  }
2272  if ( info0.m_Fuzz.first ) {
2273  pnts.SetFuzz(*MakeFuzz(*info0.m_Fuzz.first));
2274  }
2275  pnts.SetPoints().reserve(idx_end-idx_begin);
2276  for ( size_t idx = idx_begin; idx < idx_end; ++idx ) {
2277  const SSeq_loc_CI_RangeInfo& info = m_Ranges[idx];
2278  pnts.SetPoints().push_back(info.m_Range.GetFrom());
2279  }
2280  return loc;
2281 }
2282 
2283 
2285  size_t idx_end) const
2286 {
2287  CRef<CSeq_loc> loc(new CSeq_loc);
2288  CPacked_seqint::Tdata& ints = loc->SetPacked_int().Set();
2289  for ( size_t idx = idx_begin; idx < idx_end; ++idx ) {
2290  ints.push_back(MakeInterval(m_Ranges[idx]));
2291  }
2292  return loc;
2293 }
2294 
2295 
2296 static
2298 {
2299  if ( !loc ) {
2300  loc = loc2;
2301  return;
2302  }
2303  if ( !loc->IsMix() ) {
2304  CRef<CSeq_loc> loc1 = loc;
2305  loc = new CSeq_loc;
2306  loc->SetMix().Set().push_back(loc1);
2307  }
2308  loc->SetMix().Set().push_back(loc2);
2309 }
2310 
2311 
2313  size_t idx_end,
2314  CSeq_loc_I::EMakeType make_type,
2315  TUsedEquivs& used_equivs) const
2316 {
2317  if ( const SEquivSet* eq_set = FindInnerEquivSet(idx_begin, idx_end, used_equivs) ) {
2318  used_equivs.insert(eq_set);
2319  size_t idx = eq_set->GetStartIndex();
2320  CRef<CSeq_loc> loc;
2321  if ( idx > idx_begin ) {
2322  loc = MakeLoc(idx_begin, idx, make_type, used_equivs);
2323  }
2324  CRef<CSeq_loc> loc2(new CSeq_loc);
2325  CSeq_loc_equiv& equiv = loc2->SetEquiv();
2326  ITERATE ( SEquivSet::TParts, it, eq_set->m_Parts ) {
2327  size_t part_end = eq_set->GetPartEndIndex(it);
2328  CRef<CSeq_loc> part_loc =
2329  MakeLoc(idx, part_end, make_type, used_equivs);
2330  equiv.Set().push_back(part_loc);
2331  idx = part_end;
2332  }
2333  AddLoc(loc, loc2);
2334  if ( idx < idx_end ) {
2335  loc2 = MakeLoc(idx, idx_end, make_type, used_equivs);
2336  if ( loc2->IsMix() ) {
2337  ITERATE ( CSeq_loc_mix::Tdata, it, loc2->GetMix().Get() ) {
2338  AddLoc(loc, *it);
2339  }
2340  }
2341  else {
2342  AddLoc(loc, loc2);
2343  }
2344  }
2345  return loc;
2346  }
2347 
2348  if ( HasEquivBreak(idx_begin, idx_end) ) {
2349  NCBI_THROW(CSeqLocException, eBadLocation,
2350  "CSeq_loc_I::MakeSeq_loc: equiv set overlaps with range");
2351  }
2352 
2353  if ( idx_begin == idx_end ) {
2354  CRef<CSeq_loc> loc(new CSeq_loc);
2355  loc->SetMix();
2356  return loc;
2357  }
2358 
2359  // check bonds
2360  const SSeq_loc_CI_RangeInfo& info0 = m_Ranges[idx_begin];
2361  if ( make_type == CSeq_loc_I::eMake_PreserveType && info0.m_Loc ) {
2362  // try to restore original CSeq_loc type
2363  if ( info0.m_Loc->IsPacked_pnt() &&
2364  CanBePacked_pnt(idx_begin, idx_end) ) {
2365  return MakeLocPacked_pnt(idx_begin, idx_end);
2366  }
2367  if ( info0.m_Loc->IsPacked_int() &&
2368  CanBePacked_int(idx_begin, idx_end) ) {
2369  return MakeLocPacked_int(idx_begin, idx_end);
2370  }
2371  }
2372  if ( make_type == CSeq_loc_I::eMake_CompactType &&
2373  idx_end - idx_begin > 1 ) {
2374  // try to make compact variants
2375  if ( CanBePacked_pnt(idx_begin, idx_end) ) {
2376  return MakeLocPacked_pnt(idx_begin, idx_end);
2377  }
2378  if ( CanBePacked_int(idx_begin, idx_end) ) {
2379  return MakeLocPacked_int(idx_begin, idx_end);
2380  }
2381  }
2382 
2383  CRef<CSeq_loc> loc;
2384  for ( size_t idx = idx_begin; idx < idx_end; ++idx ) {
2385  if ( IsInBond(idx) ) {
2386  pair<size_t, size_t> range = GetBondRange(idx);
2387  if ( range.first < idx || range.second > idx_end ||
2388  !CanBeBond(range.first, range.second) ) {
2389  NCBI_THROW(CSeqLocException, eIncomatible,
2390  "CSeq_loc_I::MakeSeq_loc: "
2391  "invalid bond");
2392  }
2393  AddLoc(loc, MakeLocBond(range.first, range.second));
2394  idx = range.second-1;
2395  continue;
2396  }
2397  const SSeq_loc_CI_RangeInfo& info = m_Ranges[idx];
2398  if ( make_type == CSeq_loc_I::eMake_PreserveType ) {
2399  if ( WasPoint(info) && CanBePoint(info) ) {
2400  AddLoc(loc, MakeLocPoint(info));
2401  continue;
2402  }
2403  if ( (!info.m_Loc || info.m_Loc->IsInt()) &&
2404  CanBeInterval(info) ) {
2405  AddLoc(loc, MakeLocInterval(info));
2406  continue;
2407  }
2408  }
2409  if ( CanBePoint(info) ) {
2410  AddLoc(loc, MakeLocPoint(info));
2411  continue;
2412  }
2413  if ( CanBeInterval(info) ) {
2414  AddLoc(loc, MakeLocInterval(info));
2415  continue;
2416  }
2417  AddLoc(loc, MakeLocOther(info));
2418  }
2419  if ( !loc ) {
2420  loc = new CSeq_loc;
2421  }
2422  return loc;
2423 }
2424 
2425 
2427 {
2428  TUsedEquivs used_equivs;
2429  return MakeLoc(0, m_Ranges.size(), make_type, used_equivs);
2430 }
2431 
2432 
2434  : m_Impl(new CSeq_loc_CI_Impl),
2435  m_Index(0)
2436 {
2437 }
2438 
2439 
2440 CSeq_loc_CI::CSeq_loc_CI(const CSeq_loc& loc,
2441  EEmptyFlag empty_flag,
2442  ESeqLocOrder order)
2443  : m_Impl(new CSeq_loc_CI_Impl(loc, empty_flag, order)),
2444  m_Index(0)
2445 {
2446 }
2447 
2448 
2449 CSeq_loc_CI::CSeq_loc_CI(const CSeq_loc_CI& iter, size_t pos)
2450  : m_Impl(iter.m_Impl),
2451  m_Index(0)
2452 {
2453  SetPos(pos);
2454 }
2455 
2456 
2458 {
2459 }
2460 
2461 
2462 CSeq_loc_CI::CSeq_loc_CI(const CSeq_loc_CI& iter)
2463  : m_Impl(iter.m_Impl),
2464  m_Index(iter.m_Index)
2465 {
2466 }
2467 
2468 
2469 CSeq_loc_CI& CSeq_loc_CI::operator= (const CSeq_loc_CI& iter)
2470 {
2471  m_Impl = iter.m_Impl;
2472  m_Index = iter.m_Index;
2473  return *this;
2474 }
2475 
2476 
2477 bool CSeq_loc_CI::operator== (const CSeq_loc_CI& iter) const
2478 {
2479  // Check if both are at end.
2480  bool is_end = m_Impl->IsEnd(m_Index);
2481  if ( iter.m_Impl->IsEnd(iter.m_Index) ) {
2482  return is_end;
2483  }
2484  else {
2485  return !is_end && m_Impl == iter.m_Impl && m_Index == iter.m_Index;
2486  }
2487 }
2488 
2489 
2490 bool CSeq_loc_CI::operator!= (const CSeq_loc_CI& iter) const
2491 {
2492  return !(*this == iter);
2493 }
2494 
2495 
2496 bool CSeq_loc_CI::IsInBond(void) const
2497 {
2498  x_CheckValid("IsInBond()");
2499  return m_Impl->IsInBond(m_Index);
2500 }
2501 
2502 
2503 bool CSeq_loc_CI::IsBondA(void) const
2504 {
2505  x_CheckValid("IsBondA()");
2506  return m_Impl->IsBondPartA(m_Index);
2507 }
2508 
2509 
2510 bool CSeq_loc_CI::IsBondB(void) const
2511 {
2512  x_CheckValid("IsBondB()");
2513  return m_Impl->IsBondPartB(m_Index);
2514 }
2515 
2516 
2517 pair<CSeq_loc_CI, CSeq_loc_CI>
2519 {
2520  x_CheckValid("GetBondRange()");
2521  pair<size_t, size_t> indexes = m_Impl->GetBondRange(m_Index);
2522  return make_pair(CSeq_loc_CI(*this, indexes.first),
2523  CSeq_loc_CI(*this, indexes.second));
2524 }
2525 
2526 
2528 {
2529  return m_Impl->HasEquivSets();
2530 }
2531 
2532 
2534 {
2535  x_CheckValid("IsInEquivSet()");
2536  return m_Impl->IsInEquivSet(m_Index);
2537 }
2538 
2539 
2541 {
2542  x_CheckValid("GetEquivSetsCount()");
2543  return m_Impl->GetEquivSetsCount(m_Index);
2544 }
2545 
2546 
2547 pair<CSeq_loc_CI, CSeq_loc_CI>
2549 {
2550  x_CheckValid("GetEquivSetRange()");
2551  pair<size_t, size_t> indexes = m_Impl->GetEquivSetRange(m_Index, level);
2552  return make_pair(CSeq_loc_CI(*this, indexes.first),
2553  CSeq_loc_CI(*this, indexes.second));
2554 }
2555 
2556 
2557 pair<CSeq_loc_CI, CSeq_loc_CI>
2559 {
2560  x_CheckValid("GetEquivPartRange()");
2561  pair<size_t, size_t> indexes = m_Impl->GetEquivPartRange(m_Index, level);
2562  return make_pair(CSeq_loc_CI(*this, indexes.first),
2563  CSeq_loc_CI(*this, indexes.second));
2564 }
2565 
2566 
2567 const CSeq_loc& CSeq_loc_CI::GetSeq_loc(void) const
2568 {
2569  return GetEmbeddingSeq_loc();
2570 }
2571 
2572 
2573 const CSeq_loc& CSeq_loc_CI::GetEmbeddingSeq_loc(void) const
2574 {
2575  x_CheckValid("GetEmbeddingSeq_loc()");
2576  CConstRef<CSeq_loc> loc = x_GetRangeInfo().m_Loc;
2577  if ( !loc ) {
2579  "CSeq_loc_CI::GetSeq_loc(): NULL seq-loc");
2580  }
2581  return *loc;
2582 }
2583 
2584 
2586 {
2587  x_CheckValid("GetRangeAsSeq_loc()");
2588  const SSeq_loc_CI_RangeInfo& info = x_GetRangeInfo();
2589  if ( info.m_Loc ) {
2590  switch ( info.m_Loc->Which() ) {
2591  // Single-range, empty or whole seq-loc can be used as-is.
2592  case CSeq_loc::e_not_set:
2593  case CSeq_loc::e_Null:
2594  case CSeq_loc::e_Empty:
2595  case CSeq_loc::e_Whole:
2596  case CSeq_loc::e_Int:
2597  case CSeq_loc::e_Pnt:
2598  return info.m_Loc;
2599  default:
2600  break;
2601  }
2602  }
2603  // Create a new seq-loc for the current range.
2604  CRef<CSeq_loc> loc = m_Impl->MakeRangeLoc(info);
2605  return ConstRef(loc.Release());
2606 }
2607 
2608 
2609 bool CSeq_loc_CI::x_IsValid(void) const
2610 {
2611  return m_Impl && m_Index < m_Impl->GetRanges().size();
2612 }
2613 
2614 
2615 void CSeq_loc_CI::x_ThrowNotValid(const char* where) const
2616 {
2617  NCBI_THROW_FMT(CSeqLocException, eBadIterator,
2618  x_GetIteratorType() << "::" << where << ": "
2619  "iterator is not valid");
2620 }
2621 
2622 
2623 const char* CSeq_loc_CI::x_GetIteratorType(void) const
2624 {
2625  return "CSeq_loc_CI";
2626 }
2627 
2628 
2630 {
2631  // The index validity must be checked by the caller.
2632  return m_Impl->GetRanges()[m_Index];
2633 }
2634 
2635 
2636 size_t CSeq_loc_CI::GetSize(void) const
2637 {
2638  return m_Impl->GetRanges().size();
2639 }
2640 
2641 
2642 void CSeq_loc_CI::SetPos(size_t pos)
2643 {
2644  if ( pos > GetSize() ) {
2645  NCBI_THROW_FMT(CSeqLocException, eOtherError,
2646  x_GetIteratorType() << "::SetPos(): "
2647  "position is too big: "<<pos<<" > "<<GetSize());
2648  }
2649  m_Index = pos;
2650 }
2651 
2652 
2653 /////////////////////////////////////////////////////////////////////////////
2654 // CSeq_loc_I
2655 
2657 {
2658 }
2659 
2660 
2662  : CSeq_loc_CI(loc, eEmpty_Allow, eOrder_Biological)
2663 {
2664 }
2665 
2666 
2667 CSeq_loc_I::CSeq_loc_I(const CSeq_loc_I& iter, size_t pos)
2668  : CSeq_loc_CI(iter)
2669 {
2670  SetPos(pos);
2671 }
2672 
2673 
2675 {
2676 }
2677 
2678 
2680 {
2681  return m_Impl && m_Index <= m_Impl->GetRanges().size();
2682 }
2683 
2684 
2685 const char* CSeq_loc_I::x_GetIteratorType(void) const
2686 {
2687  return "CSeq_loc_I";
2688 }
2689 
2690 
2692 {
2693  // The index validity must be checked by the caller.
2694  return m_Impl->GetRanges()[m_Index];
2695 }
2696 
2697 
2699  const CSeq_id_Handle& id)
2700 {
2701  info.m_Id = id.GetSeqId();
2702  info.m_IdHandle = id;
2703 }
2704 
2705 
2706 bool CSeq_loc_I::HasChanges(void) const
2707 {
2708  return m_Impl->HasChanges();
2709 }
2710 
2711 
2713 {
2714  return m_Impl->GetEquivMode();
2715 }
2716 
2717 
2719 {
2720  m_Impl->SetEquivMode(mode);
2721 }
2722 
2723 
2725 {
2726  x_CheckValid("Delete()");
2727  m_Impl->DeleteRange(m_Index);
2728 }
2729 
2730 
2732 {
2733  x_CheckValidForInsert("InsertNull()");
2734  m_Impl->InsertRange(m_Index, CSeq_loc::e_Null);
2735  return CSeq_loc_I(*this, m_Index++);
2736 }
2737 
2738 
2740 {
2741  x_CheckValidForInsert("InsertEmpty()");
2743  m_Impl->InsertRange(m_Index, CSeq_loc::e_Empty);
2744  x_SetSeq_id_Handle(info, id);
2745  return CSeq_loc_I(*this, m_Index++);
2746 }
2747 
2748 
2750 {
2751  x_CheckValidForInsert("InsertWhole()");
2753  m_Impl->InsertRange(m_Index, CSeq_loc::e_Whole);
2754  x_SetSeq_id_Handle(info, id);
2755  info.m_Range = TRange::GetWhole();
2756  return CSeq_loc_I(*this, m_Index++);
2757 }
2758 
2759 
2761  TSeqPos pos,
2762  ENa_strand strand)
2763 {
2764  x_CheckValidForInsert("InsertPoint()");
2766  m_Impl->InsertRange(m_Index, CSeq_loc::e_Pnt);
2767  x_SetSeq_id_Handle(info, id);
2768  info.m_Range.SetFrom(pos).SetLength(1);
2769  if ( strand != eNa_strand_unknown ) {
2770  info.SetStrand(strand);
2771  }
2772  m_Impl->SetPoint(info);
2773  return CSeq_loc_I(*this, m_Index++);
2774 }
2775 
2776 
2778  const TRange& range,
2779  ENa_strand strand)
2780 {
2781  x_CheckValidForInsert("InsertInterval()");
2783  m_Impl->InsertRange(m_Index, CSeq_loc::e_Int);
2784  x_SetSeq_id_Handle(info, id);
2785  info.m_Range = range;
2786  if ( strand != eNa_strand_unknown ) {
2787  info.SetStrand(strand);
2788  }
2789  if ( !range.IsWhole() && !range.Empty() ) {
2790  info.m_Loc = m_Impl->MakeLocInterval(info);
2791  }
2792  return CSeq_loc_I(*this, m_Index++);
2793 }
2794 
2795 
2797 {
2798  x_CheckValid("SetSeq_id_Handle()");
2800  if ( info.m_IdHandle != id ) {
2801  x_SetSeq_id_Handle(info, id);
2802  m_Impl->UpdatePoint(info);
2803  }
2804 }
2805 
2806 
2807 void CSeq_loc_I::SetRange(const TRange& range)
2808 {
2809  x_CheckValid("SetRange()");
2811  if ( info.m_Range != range ) {
2812  info.m_Range = range;
2813  m_Impl->UpdateLoc(info);
2814  }
2815 }
2816 
2817 
2819 {
2820  x_CheckValid("SetFrom()");
2822  if ( info.m_Range.GetFrom() != from ) {
2823  info.m_Range.SetFrom(from);
2824  m_Impl->UpdateLoc(info);
2825  }
2826 }
2827 
2828 
2830 {
2831  x_CheckValid("SetTo()");
2833  if ( info.m_Range.GetTo() != to ) {
2834  info.m_Range.SetTo(to);
2835  m_Impl->UpdateLoc(info);
2836  }
2837 }
2838 
2839 
2841 {
2842  x_CheckValid("SetPoint()");
2844  if ( !m_Impl->WasPoint(info) || info.m_Range != TRange(pos, pos) ) {
2845  info.m_Range = TRange(pos, pos);
2846  if ( m_Impl->WasPoint(info) ) {
2847  m_Impl->UpdatePoint(info);
2848  }
2849  else {
2850  m_Impl->SetPoint(info);
2851  }
2852  }
2853 }
2854 
2855 
2857 {
2858  x_CheckValid("ResetStrand()");
2860  if ( info.m_IsSetStrand ) {
2861  info.m_IsSetStrand = false;
2862  info.m_Strand = eNa_strand_unknown;
2863  m_Impl->UpdatePoint(info);
2864  }
2865 }
2866 
2867 
2869 {
2870  x_CheckValid("SetStrand()");
2872  if ( !info.m_IsSetStrand || info.m_Strand != strand ) {
2873  info.SetStrand(strand);
2874  m_Impl->UpdatePoint(info);
2875  }
2876 }
2877 
2878 
2880 {
2881  x_CheckValid("ResetFuzzFrom()");
2883  if ( info.m_Fuzz.first ) {
2884  info.m_Fuzz.first = null;
2885  m_Impl->UpdateLoc(info);
2886  }
2887 }
2888 
2889 
2891 {
2892  x_CheckValid("SetFuzzFrom()");
2894  if ( !info.m_Fuzz.first || !info.m_Fuzz.first->Equals(fuzz) ) {
2895  info.m_Fuzz.first = SerialClone(fuzz);
2896  m_Impl->UpdateLoc(info);
2897  }
2898 }
2899 
2900 
2902 {
2903  x_CheckValid("ResetFuzzTo()");
2905  if ( info.m_Fuzz.second ) {
2906  info.m_Fuzz.second = null;
2907  m_Impl->UpdateLoc(info);
2908  }
2909 }
2910 
2911 
2913 {
2914  x_CheckValid("SetFuzzTo()");
2916  if ( !info.m_Fuzz.second || !info.m_Fuzz.second->Equals(fuzz) ) {
2917  info.m_Fuzz.second = SerialClone(fuzz);
2918  m_Impl->UpdateLoc(info);
2919  }
2920 }
2921 
2922 
2924 {
2925  x_CheckValid("ResetFuzz()");
2927  if ( info.m_Fuzz.first || info.m_Fuzz.second ) {
2928  info.m_Fuzz.first = info.m_Fuzz.second = null;
2929  m_Impl->UpdatePoint(info);
2930  }
2931 }
2932 
2933 
2935 {
2936  x_CheckValid("SetFuzz()");
2938  if ( !info.m_Fuzz.first || !info.m_Fuzz.first->Equals(fuzz) ||
2939  info.m_Fuzz.first != info.m_Fuzz.second ) {
2940  info.m_Fuzz.first = info.m_Fuzz.second = SerialClone(fuzz);
2941  m_Impl->UpdatePoint(info);
2942  }
2943 }
2944 
2945 
2947 {
2948  return m_Impl->MakeLoc(make_type);
2949 }
2950 
2951 
2952 pair<CSeq_loc_I, CSeq_loc_I>
2953 CSeq_loc_I::GetEquivSetRange(size_t level) const
2954 {
2955  x_CheckValid("GetEquivSetRange()");
2956  pair<size_t, size_t> indexes = m_Impl->GetEquivSetRange(m_Index, level);
2957  return make_pair(CSeq_loc_I(*this, indexes.first),
2958  CSeq_loc_I(*this, indexes.second));
2959 }
2960 
2961 
2962 pair<CSeq_loc_I, CSeq_loc_I>
2964 {
2965  x_CheckValid("GetEquivPartRange()");
2966  pair<size_t, size_t> indexes = m_Impl->GetEquivPartRange(m_Index, level);
2967  return make_pair(CSeq_loc_I(*this, indexes.first),
2968  CSeq_loc_I(*this, indexes.second));
2969 }
2970 
2971 
2973 {
2974  x_CheckValid("RemoveBond()");
2975  m_Impl->RemoveBond(m_Index);
2976 }
2977 
2978 
2980 {
2981  x_CheckValid("MakeBondA()");
2982  m_Impl->MakeBondA(m_Index);
2983 }
2984 
2985 
2987 {
2988  x_CheckValid("MakeBondAB()");
2989  m_Impl->MakeBondAB(m_Index);
2990 }
2991 
2992 
2994 {
2995  x_CheckValid("MakeBondB()");
2996  m_Impl->MakeBondB(m_Index);
2997 }
2998 
2999 
3000 // CSeq_loc_I
3001 /////////////////////////////////////////////////////////////////////////////
3002 
3003 
3004 // Append a string representation of a CSeq_id to label
3005 static inline
3006 void s_GetLabel(const CSeq_id& id, string* label)
3007 {
3008  CNcbiOstrstream os;
3009  id.WriteAsFasta(os);
3011 }
3012 
3013 
3014 // Append to label info for a CSeq_point
3015 static inline
3017 (const CSeq_point& pnt,
3018  const CSeq_id* last_id,
3019  string* label)
3020 {
3021  // If CSeq_id does not match last_id, then append id to label
3022  if ( !last_id || !last_id->Match(pnt.GetId()) ) {
3023  s_GetLabel(pnt.GetId(), label);
3024  *label += ":";
3025  }
3026 
3027  // Add strand info to label
3028  if (pnt.IsSetStrand() && IsReverse(pnt.GetStrand())) {
3029  *label += "c";
3030  }
3031 
3032  if (pnt.IsSetFuzz()) {
3033  // Append Fuzz info to label
3034  pnt.GetFuzz().GetLabel(label, pnt.GetPoint());
3035  } else {
3036  // Append 1 based point to label
3037  *label += NStr::IntToString(pnt.GetPoint()+1);
3038  }
3039 
3040  // update last_id
3041  last_id = &pnt.GetId();
3042  return last_id;
3043 }
3044 
3045 
3046 static inline
3048 (const CPacked_seqpnt& pnts,
3049  const CSeq_id* last_id,
3050  string* label)
3051 {
3052  *label += "(";
3053  // If CSeq_id does not match last_id, then append id to label
3054  if ( !last_id || !last_id->Match(pnts.GetId()) ) {
3055  s_GetLabel(pnts.GetId(), label);
3056  *label += ":";
3057  }
3058  const char* prefix = pnts.IsSetStrand() && IsReverse(pnts.GetStrand())? "c": nullptr;
3059  const CInt_fuzz* fuzz = pnts.IsSetFuzz()? &pnts.GetFuzz(): nullptr;
3060  bool first = true;
3061  ITERATE (CPacked_seqpnt::TPoints, iter, pnts.GetPoints()) {
3062  if ( !first ) {
3063  *label += ", ";
3064  }
3065  first = false;
3066  if ( prefix ) {
3067  *label += prefix;
3068  }
3069  if ( fuzz ) {
3070  fuzz->GetLabel(label, *iter);
3071  }
3072  else {
3073  *label += NStr::IntToString(*iter+1);
3074  }
3075  }
3076  *label += ")";
3077  last_id = &pnts.GetId();
3078  return last_id;
3079 }
3080 
3081 
3082 // Append to label info for CSeq_interval
3083 static inline
3085 (const CSeq_interval& itval,
3086  const CSeq_id* last_id,
3087  string* label)
3088 {
3089  if (!last_id || !last_id->Match(itval.GetId())) {
3090  s_GetLabel(itval.GetId(), label);
3091  *label += ":";
3092  }
3093  last_id = &itval.GetId();
3094  if (itval.IsSetStrand() && IsReverse(itval.GetStrand())) {
3095  *label += "c";
3096  if (itval.IsSetFuzz_to()) {
3097  itval.GetFuzz_to().GetLabel(label, itval.GetTo(), false);
3098  } else {
3099  *label += NStr::IntToString(itval.GetTo()+1);
3100  }
3101  *label += "-";
3102  if (itval.IsSetFuzz_from()) {
3103  itval.GetFuzz_from().GetLabel(label, itval.GetFrom());
3104  } else {
3105  *label += NStr::IntToString(itval.GetFrom()+1);
3106  }
3107  } else {
3108  if (itval.IsSetFuzz_from()) {
3109  itval.GetFuzz_from().GetLabel
3110  (label, itval.GetFrom(), false);
3111  } else {
3112  *label += NStr::IntToString(itval.GetFrom()+1);
3113  }
3114  *label += "-";
3115  if (itval.IsSetFuzz_to()) {
3116  itval.GetFuzz_to().GetLabel(label, itval.GetTo());
3117  } else {
3118  *label += NStr::IntToString(itval.GetTo()+1);
3119  }
3120  }
3121  return last_id;
3122 }
3123 
3124 
3125 static inline
3127 (const CPacked_seqint::Tdata& ints,
3128  const CSeq_id* last_id,
3129  string* label)
3130 {
3131  *label += "(";
3132  bool first = true;
3133  ITERATE(CPacked_seqint::Tdata, it, ints) {
3134  if (!first) {
3135  *label += ", ";
3136  }
3137  first = false;
3138  last_id = s_GetLabel(**it, last_id, label);
3139  }
3140  *label += ")";
3141  return last_id;
3142 }
3143 
3144 // Forward declaration
3145 static const CSeq_id* s_GetLabel
3146 (const CSeq_loc& loc,
3147  const CSeq_id* last_id,
3148  string* label,
3149  bool first = false);
3150 
3151 
3152 // Appends to label info for each CSeq_loc in a list of CSeq_locs
3153 static inline
3155 (const list<CRef<CSeq_loc> >& loc_list,
3156  const CSeq_id* last_id,
3157  string* label)
3158 {
3159  bool first = true;
3160  ITERATE (list<CRef<CSeq_loc> >, it, loc_list) {
3161 
3162  // Append to label for each CSeq_loc in list
3163  last_id = s_GetLabel(**it, last_id, label, first);
3164  first = false;
3165  }
3166 
3167  return last_id;
3168 }
3169 
3170 
3171 // Builds a label based upon a CSeq_loc and all embedded CSeq_locs
3172 static const CSeq_id* s_GetLabel
3173 (const CSeq_loc& loc,
3174  const CSeq_id* last_id,
3175  string* label,
3176  bool first)
3177 {
3178  // Ensure label is not null
3179  if (!label) {
3180  return last_id;
3181  }
3182 
3183  // Put a comma separator if necessary
3184  if (!first) {
3185  *label += ", ";
3186  }
3187 
3188  // Loop through embedded CSeq_locs and create a label, depending on
3189  // type of CSeq_loc
3190  switch (loc.Which()) {
3191  case CSeq_loc::e_Null:
3192  *label += "~";
3193  break;
3194  case CSeq_loc::e_Empty:
3195  *label += "{";
3196  s_GetLabel(loc.GetEmpty(), label);
3197  last_id = &loc.GetEmpty();
3198  *label += "}";
3199  break;
3200  case CSeq_loc::e_Whole:
3201  s_GetLabel(loc.GetWhole(), label);
3202  last_id = &loc.GetWhole();
3203  break;
3204  case CSeq_loc::e_Int:
3205  last_id = s_GetLabel(loc.GetInt(), last_id, label);
3206  break;
3208  last_id = s_GetLabel(loc.GetPacked_int().Get(), last_id, label);
3209  break;
3210  case CSeq_loc::e_Pnt:
3211  last_id = s_GetLabel(loc.GetPnt(), last_id, label);
3212  break;
3214  last_id = s_GetLabel(loc.GetPacked_pnt(), last_id, label);
3215  break;
3216  case CSeq_loc::e_Mix:
3217  *label += "[";
3218  last_id = s_GetLabel(loc.GetMix().Get(), last_id, label);
3219  *label += "]";
3220  break;
3221  case CSeq_loc::e_Equiv:
3222  *label += "[";
3223  last_id = s_GetLabel(loc.GetEquiv().Get(), last_id, label);
3224  *label += "]";
3225  break;
3226  case CSeq_loc::e_Bond:
3227  last_id = s_GetLabel(loc.GetBond().GetA(), last_id, label);
3228  *label += "=";
3229  if (loc.GetBond().IsSetB()) {
3230  last_id = s_GetLabel(loc.GetBond().GetB(), last_id, label);
3231  } else {
3232  *label += "?";
3233  }
3234  break;
3235  case CSeq_loc::e_Feat:
3236  *label += "(feat)";
3237  break;
3238  default:
3239  *label += "(?\?)";
3240  break;
3241  }
3242  return last_id;
3243 }
3244 
3245 
3247 {
3248  switch (Which ()) {
3249  case e_Null :
3250  return true;
3251 
3252  case e_Int :
3253  return GetInt().IsPartialStart(ext);
3254 
3255  case e_Packed_int :
3256  return GetPacked_int().IsPartialStart(ext);
3257 
3258  case e_Pnt :
3259  return GetPnt().IsPartialStart(ext);
3260 
3261  case e_Packed_pnt :
3262  return GetPacked_pnt().IsPartialStart(ext);
3263 
3264  case e_Mix :
3265  return GetMix().IsPartialStart(ext);
3266 
3267  default :
3268  break;
3269  }
3270 
3271  return false;
3272 }
3273 
3274 
3276 {
3277  switch (Which ()) {
3278  case e_Null :
3279  return true;
3280 
3281  case e_Int :
3282  return GetInt().IsPartialStop(ext);
3283 
3284  case e_Packed_int :
3285  return GetPacked_int().IsPartialStop(ext);
3286 
3287  case e_Pnt :
3288  return GetPnt().IsPartialStop(ext);
3289 
3290  case e_Packed_pnt :
3291  return GetPacked_pnt().IsPartialStop(ext);
3292 
3293  case e_Mix :
3294  return GetMix().IsPartialStop(ext);
3295 
3296  default :
3297  break;
3298  }
3299 
3300  return false;
3301 }
3302 
3303 
3305 {
3306  if (val == IsPartialStart(ext)) {
3307  return;
3308  }
3309 
3310  switch (Which()) {
3311  case e_Int:
3312  SetInt().SetPartialStart(val, ext);
3313  break;
3314 
3315  case e_Packed_int :
3316  SetPacked_int().SetPartialStart(val, ext);
3317  break;
3318 
3319  case e_Pnt:
3320  SetPnt().SetPartialStart(val, ext);
3321  break;
3322 
3323  case e_Packed_pnt:
3324  SetPacked_pnt().SetPartialStart(val, ext);
3325  break;
3326 
3327  case e_Mix :
3328  SetMix().SetPartialStart(val, ext);
3329  break;
3330 
3331  default :
3332  break;
3333  }
3334 }
3335 
3336 
3338 {
3339  if (val == IsPartialStop(ext)) {
3340  return;
3341  }
3342 
3343  switch (Which()) {
3344  case e_Int:
3345  SetInt().SetPartialStop(val, ext);
3346  break;
3347 
3348  case e_Packed_int :
3349  SetPacked_int().SetPartialStop(val, ext);
3350  break;
3351 
3352  case e_Pnt:
3353  SetPnt().SetPartialStop(val, ext);
3354  break;
3355 
3356  case e_Packed_pnt:
3357  SetPacked_pnt().SetPartialStop(val, ext);
3358  break;
3359 
3360  case e_Mix:
3361  SetMix().SetPartialStop(val, ext);
3362  break;
3363 
3364  default :
3365  break;
3366  }
3367 }
3368 
3369 
3371 {
3372  switch (Which ()) {
3373  case e_Int :
3374  return GetInt().IsTruncatedStart(ext);
3375 
3376  case e_Packed_int :
3377  return GetPacked_int().IsTruncatedStart(ext);
3378 
3379  case e_Pnt :
3380  return GetPnt().IsTruncatedStart(ext);
3381 
3382  case e_Packed_pnt :
3383  return GetPacked_pnt().IsTruncatedStart(ext);
3384 
3385  case e_Mix :
3386  return GetMix().IsTruncatedStart(ext);
3387 
3388  default :
3389  break;
3390  }
3391 
3392  return false;
3393 }
3394 
3395 
3397 {
3398  switch (Which ()) {
3399  case e_Int :
3400  return GetInt().IsTruncatedStop(ext);
3401 
3402  case e_Packed_int :
3403  return GetPacked_int().IsTruncatedStop(ext);
3404 
3405  case e_Pnt :
3406  return GetPnt().IsTruncatedStop(ext);
3407 
3408  case e_Packed_pnt :
3409  return GetPacked_pnt().IsTruncatedStop(ext);
3410 
3411  case e_Mix :
3412  return GetMix().IsTruncatedStop(ext);
3413 
3414  default :
3415  break;
3416  }
3417 
3418  return false;
3419 }
3420 
3421 
3423 {
3424  if (val == IsTruncatedStart(ext)) {
3425  return;
3426  }
3427 
3428  switch (Which()) {
3429  case e_Int:
3430  SetInt().SetTruncatedStart(val, ext);
3431  break;
3432 
3433  case e_Packed_int :
3434  SetPacked_int().SetTruncatedStart(val, ext);
3435  break;
3436 
3437  case e_Pnt:
3438  SetPnt().SetTruncatedStart(val, ext);
3439  break;
3440 
3441  case e_Packed_pnt:
3442  SetPacked_pnt().SetTruncatedStart(val, ext);
3443  break;
3444 
3445  case e_Mix :
3446  SetMix().SetTruncatedStart(val, ext);
3447  break;
3448 
3449  default :
3450  break;
3451  }
3452 }
3453 
3454 
3456 {
3457  if (val == IsTruncatedStop(ext)) {
3458  return;
3459  }
3460 
3461  switch (Which()) {
3462  case e_Int:
3463  SetInt().SetTruncatedStop(val, ext);
3464  break;
3465 
3466  case e_Packed_int :
3467  SetPacked_int().SetTruncatedStop(val, ext);
3468  break;
3469 
3470  case e_Pnt:
3471  SetPnt().SetTruncatedStop(val, ext);
3472  break;
3473 
3474  case e_Packed_pnt:
3475  SetPacked_pnt().SetTruncatedStop(val, ext);
3476  break;
3477 
3478  case e_Mix:
3479  SetMix().SetTruncatedStop(val, ext);
3480  break;
3481 
3482  default :
3483  break;
3484  }
3485 }
3486 
3487 
3488 // Appends a label suitable for display (e.g., error messages)
3489 // label must point to an existing string object
3490 // Method just returns if label is null
3491 void CSeq_loc::GetLabel(string* label) const
3492 {
3493  s_GetLabel(*this, 0, label, true);
3494 }
3495 
3496 
3497 // assign the 'id' field of each sub-interval to the supplied id
3499 {
3500  InvalidateCache();
3501  switch (Which()) {
3502  case e_Null:
3503  break;
3504 
3505  case e_Int:
3506  SetInt().SetId(id);
3507  break;
3508 
3509  case e_Pnt:
3510  SetPnt().SetId(id);
3511  break;
3512 
3513  case e_Packed_int:
3514  NON_CONST_ITERATE (CPacked_seqint::Tdata, iter, SetPacked_int().Set()) {
3515  (*iter)->SetId(id);
3516  }
3517  break;
3518 
3519  case e_Packed_pnt:
3520  SetPacked_pnt().SetId(id);
3521  break;
3522 
3523  case e_Mix:
3524  NON_CONST_ITERATE (CSeq_loc_mix::Tdata, iter, SetMix().Set()) {
3525  (*iter)->SetId(id);
3526  }
3527  break;
3528 
3529  case e_Whole:
3530  SetWhole(id);
3531  break;
3532 
3533  case e_Empty:
3534  SetEmpty(id);
3535  break;
3536 
3537  case e_Equiv:
3538  NON_CONST_ITERATE (CSeq_loc_equiv::Tdata, iter, SetEquiv().Set()) {
3539  (*iter)->SetId(id);
3540  }
3541  break;
3542 
3543  case e_Bond:
3544  if (GetBond().IsSetA()) {
3545  SetBond().SetA().SetId(id);
3546  }
3547  if (GetBond().IsSetB()) {
3548  SetBond().SetB().SetId(id);
3549  }
3550  break;
3551 
3552  case e_Feat:
3553  ERR_POST_X(1, Error
3554  << "unhandled loc type in CSeq_loc::SetId(): e_Feat");
3555  break;
3556 
3557  default:
3558  ERR_POST_X(2, Error << "unhandled loc type in CSeq_loc::SetId(): "
3559  << Which());
3560  break;
3561  }
3562 }
3563 
3564 
3565 bool CSeq_loc::x_CheckId(const CSeq_id*& id, bool may_throw) const
3566 {
3567  switch ( Which() ) {
3568  case e_not_set:
3569  case e_Null:
3570  {
3571  // no Seq-id
3572  return true;
3573  }
3574  case e_Empty:
3575  {
3576  return x_UpdateId(id, &GetEmpty(), may_throw);
3577  }
3578  case e_Whole:
3579  {
3580  return x_UpdateId(id, &GetWhole(), may_throw);
3581  }
3582  case e_Int:
3583  {
3584  const CSeq_interval& loc = GetInt();
3585  return x_UpdateId(id, &loc.GetId(), may_throw);
3586  }
3587  case e_Pnt:
3588  {
3589  const CSeq_point& pnt = GetPnt();
3590  return x_UpdateId(id, &pnt.GetId(), may_throw);
3591  }
3592  case e_Packed_int:
3593  {
3594  // Check ID of each interval
3595  const CPacked_seqint& ints = GetPacked_int();
3596  ITERATE ( CPacked_seqint::Tdata, ii, ints.Get() ) {
3597  const CSeq_interval& loc = **ii;
3598  if ( !x_UpdateId(id, &loc.GetId(), may_throw) ) {
3599  return false;
3600  }
3601  }
3602  return true;
3603  }
3604  case e_Packed_pnt:
3605  {
3606  const CPacked_seqpnt& pnts = GetPacked_pnt();
3607  return x_UpdateId(id, &pnts.GetId(), may_throw);
3608  break;
3609  }
3610  case e_Mix:
3611  {
3612  // Check ID of each sub-location.
3613  const CSeq_loc_mix& mix = GetMix();
3614  ITERATE( CSeq_loc_mix::Tdata, li, mix.Get() ) {
3615  if ( !(*li)->CheckId(id, may_throw) ) {
3616  return false;
3617  }
3618  }
3619  return true;
3620  }
3621  case e_Bond:
3622  {
3623  const CSeq_bond& bond = GetBond();
3624  if ( bond.CanGetA() ) {
3625  if ( !x_UpdateId(id, &bond.GetA().GetId(), may_throw) ) {
3626  return false;
3627  }
3628  }
3629  if ( bond.CanGetB() ) {
3630  return x_UpdateId(id, &bond.GetB().GetId(), may_throw);
3631  }
3632  return true;
3633  }
3634  case e_Equiv:
3635  {
3636  // Doesn't make much sense to test equiv, but ...
3637  ITERATE(CSeq_loc_equiv::Tdata, li, GetEquiv().Get()) {
3638  if ( !(*li)->CheckId(id, may_throw) ) {
3639  return false;
3640  }
3641  }
3642  return true;
3643  }
3644  case e_Feat:
3645  default:
3646  if (may_throw) {
3647  NCBI_THROW_FMT(CSeqLocException, eUnsupported,
3648  "CSeq_loc::CheckId(): "
3649  "unsupported location type: "<<
3650  SelectionName(Which()));
3651  }
3652  return false;
3653  }
3654 }
3655 
3656 
3658 {
3659  switch ( Which() ) {
3660  case e_not_set:
3661  {
3662  SetMix();
3663  break;
3664  }
3665  case e_Mix:
3666  {
3667  break;
3668  }
3669  case e_Packed_int:
3670  {
3671  // unpack
3672  CRef<CSeq_loc> self(new CSeq_loc);
3673  self->Assign(*this, eShallow);
3674 
3675  CSeq_loc_mix& mix = SetMix();
3676  NON_CONST_ITERATE (CPacked_seqint::Tdata, it, self->SetPacked_int().Set()) {
3677  CRef<CSeq_loc> ival(new CSeq_loc);
3678  ival->SetInt(**it);
3679  mix.Set().push_back(ival);
3680  }
3681  break;
3682  }
3683  default:
3684  {
3685  CRef<CSeq_loc> self(new CSeq_loc);
3686  self->Assign(*this, eShallow);
3687  CSeq_loc_mix& mix = SetMix();
3688  mix.AddSeqLoc(*self);
3689  }
3690  }
3691 }
3692 
3693 
3695 {
3696  switch ( Which() ) {
3697  case e_not_set:
3698  case e_Null:
3699  {
3700  SetPacked_int();
3701  return;
3702  }
3703  case e_Packed_int:
3704  {
3705  return;
3706  }
3707  case e_Int:
3708  {
3709  CConstRef<CSeq_interval> self(&GetInt());
3710  SetPacked_int().AddInterval(*self);
3711  return;
3712  }
3713  case e_Pnt:
3714  {
3715  // Make an interval of length 1
3716  CRef<CSeq_interval> new_int(new CSeq_interval);
3717  new_int->SetId().Assign(GetPnt().GetId());
3718  new_int->SetFrom(GetPnt().GetPoint());
3719  new_int->SetTo(GetPnt().GetPoint());
3720  if (GetPnt().IsSetStrand()) {
3721  new_int->SetStrand(GetPnt().GetStrand());
3722  }
3723  if (GetPnt().IsSetFuzz()) {
3724  const CInt_fuzz& fuzz = GetPnt().GetFuzz();
3725  if (!fuzz.IsLim() || fuzz.GetLim() != CInt_fuzz::eLim_gt) {
3726  new_int->SetFuzz_from().Assign(fuzz);
3727  }
3728  if (!fuzz.IsLim() || fuzz.GetLim() != CInt_fuzz::eLim_lt) {
3729  new_int->SetFuzz_to().Assign(fuzz);
3730  }
3731  }
3732  SetPacked_int().AddInterval(*new_int);
3733  return;
3734  }
3735  case e_Mix:
3736  {
3737  // Recursively convert each sub-location to packed-int, then merge.
3738  // Work with copies of sub-locs so that if an exception is thrown
3739  // the original location remains unchanged.
3740  vector<CRef<CSeq_loc> > sub_locs;
3741  sub_locs.reserve(GetMix().Get().size());
3742  ITERATE (CSeq_loc_mix::Tdata, orig_sub_loc, GetMix().Get()) {
3743  CRef<CSeq_loc> new_sub_loc(new CSeq_loc);
3744  new_sub_loc->Assign(**orig_sub_loc);
3745  new_sub_loc->ChangeToPackedInt();
3746  sub_locs.push_back(new_sub_loc);
3747  }
3748  SetPacked_int(); // in case there are zero intervals
3749  ITERATE (vector<CRef<CSeq_loc> >, sub_loc, sub_locs) {
3750  copy((*sub_loc)->GetPacked_int().Get().begin(),
3751  (*sub_loc)->GetPacked_int().Get().end(),
3752  back_inserter(SetPacked_int().Set()));
3753  }
3754  return;
3755  }
3756  default:
3757  NCBI_THROW_FMT(CSeqLocException, eIncomatible,
3758  "CSeq_loc::ChangeToPackedInt(): "
3759  "Can not convert location to packed-int: "<<
3760  SelectionName(Which()));
3761  }
3762 }
3763 
3764 
3765 void CSeq_loc::x_ChangeToMix(const CSeq_loc& other)
3766 {
3767  ChangeToMix();
3768  SetMix().AddSeqLoc(other);
3769 }
3770 
3771 
3773 {
3774  _ASSERT(IsInt());
3775 
3776  ChangeToPackedInt();
3777  SetPacked_int().AddInterval(other);
3778 }
3779 
3780 
3781 void CSeq_loc::x_ChangeToPackedInt(const CSeq_loc& other)
3782 {
3783  _ASSERT(IsInt());
3784  _ASSERT(other.IsInt() || other.IsPacked_int());
3785 
3786  ChangeToPackedInt();
3787 
3788  if ( other.IsInt() ) {
3789  SetPacked_int().AddInterval(other.GetInt());
3790  } else { // other is packed int
3791  SetPacked_int().AddIntervals(other.GetPacked_int());
3792  }
3793 }
3794 
3795 
3796 void CSeq_loc::x_ChangeToPackedPnt(const CSeq_loc& other)
3797 {
3798  _ASSERT(IsPnt());
3799  _ASSERT(other.IsPnt() || other.IsPacked_pnt());
3800 
3801  CRef<CSeq_point> pnt(&SetPnt());
3802  CPacked_seqpnt& ppnt = SetPacked_pnt();
3803  if ( pnt->IsSetStrand() ) {
3804  ppnt.SetStrand(pnt->GetStrand());
3805  }
3806  if ( pnt->IsSetId() ) {
3807  ppnt.SetId(pnt->SetId());
3808  }
3809  if ( pnt->IsSetFuzz() ) {
3810  ppnt.SetFuzz(pnt->SetFuzz());
3811  }
3812  ppnt.AddPoint(pnt->GetPoint());
3813 
3814  if ( other.IsPnt() ) {
3815  ppnt.AddPoint(other.GetPnt().GetPoint());
3816  } else { // other is packed point
3817  ppnt.AddPoints(other.GetPacked_pnt().GetPoints());
3818  }
3819 }
3820 
3821 
3822 template<typename T1, typename T2>
3823 bool s_CanAdd(const T1& obj1, const T2& obj2)
3824 {
3825  // test strands
3826  {{
3827  ENa_strand s1 = obj1.CanGetStrand() ? obj1.GetStrand() : eNa_strand_unknown;
3828  ENa_strand s2 = obj2.CanGetStrand() ? obj2.GetStrand() : eNa_strand_unknown;
3829  if ( s1 != s2 ) {
3830  return false;
3831  }
3832  }}
3833 
3834  // test ids
3835  {{
3836  const CSeq_id* id1 = obj1.CanGetId() ? &obj1.GetId() : 0;
3837  const CSeq_id* id2 = obj2.CanGetId() ? &obj2.GetId() : 0;
3838  // both null - ok to add (this will probably never happen)
3839  if (id1 == 0 && id2 == 0) return true;
3840  // ids are different (one may be null)
3841  if (id1 == 0 || id2 == 0 || !id1->Match(*id2) ) {
3842  return false;
3843  }
3844  }}
3845 
3846  // test fuzz
3847  {{
3848  const CInt_fuzz* f1 = obj1.CanGetFuzz() ? &obj1.GetFuzz() : 0;
3849  const CInt_fuzz* f2 = obj2.CanGetFuzz() ? &obj2.GetFuzz() : 0;
3850  // both null - ok to add
3851  if (f1 == 0 && f2 == 0) return true;
3852  // fuzzes are different (one may be null)
3853  if (f1 == 0 || f2 == 0 || !f1->Equals(*f2)) {
3854  return false;
3855  }
3856  }}
3857 
3858  return true;
3859 }
3860 
3861 
3862 bool s_CanAdd(const CSeq_loc& loc1, const CSeq_loc& loc2)
3863 {
3864  switch ( loc1.Which() ) {
3865  case CSeq_loc::e_Pnt:
3866  {
3867  switch ( loc2.Which() ) {
3868  case CSeq_loc::e_Pnt:
3869  return s_CanAdd(loc1.GetPnt(), loc2.GetPnt());
3871  return s_CanAdd(loc1.GetPnt(), loc2.GetPacked_pnt());
3872  default:
3873  break;
3874  }
3875  break;
3876  }
3878  {
3879  switch ( loc2.Which() ) {
3880  case CSeq_loc::e_Pnt:
3881  return s_CanAdd(loc1.GetPacked_pnt(), loc2.GetPnt());
3883  return s_CanAdd(loc1.GetPacked_pnt(), loc2.GetPacked_pnt());
3884  default:
3885  break;
3886  }
3887  break;
3888  }
3889  default:
3890  {
3891  return false;
3892  }
3893  }
3894 
3895  return false;
3896 }
3897 
3898 
3899 void CSeq_loc::Add(const CSeq_loc& other)
3900 {
3901  InvalidateCache();
3902  switch ( Which() ) {
3903  case e_not_set:
3904  {
3905  Assign(other);
3906  break;
3907  }
3908  case e_Null:
3909  {
3910  // ??? skip if other is null?
3911  x_ChangeToMix(other);
3912  break;
3913  }
3914  case e_Empty:
3915  {
3916  // ??? skip if other is empty and ids match?
3917  x_ChangeToMix(other);
3918  break;
3919  }
3920 
3921  case e_Whole:
3922  {
3923  x_ChangeToMix(other);
3924  break;
3925  }
3926  case e_Int:
3927  {
3928  if ( other.IsInt() || other.IsPacked_int() ) {
3929  x_ChangeToPackedInt(other);
3930  } else {
3931  x_ChangeToMix(other);
3932  }
3933  }
3934  break;
3935  case e_Pnt:
3936  {
3937  if ( s_CanAdd(*this, other) ) {
3938  x_ChangeToPackedPnt(other);
3939  } else {
3940  x_ChangeToMix(other);
3941  }
3942  break;
3943  }
3944  case e_Packed_int:
3945  {
3946  if ( other.IsInt() ) {
3947  SetPacked_int().AddInterval(other.GetInt());
3948  } else if ( other.IsPacked_int() ) {
3949  SetPacked_int().AddIntervals(other.GetPacked_int());
3950  } else {
3951  x_ChangeToMix(other);
3952  }
3953  break;
3954  }
3955  case e_Packed_pnt:
3956  {
3957  if ( s_CanAdd(*this, other) ) {
3958  if ( other.IsPnt() ) {
3959  SetPacked_pnt().AddPoint(other.GetPnt().GetPoint());
3960  } else if ( other.IsPacked_pnt() ) {
3961  SetPacked_pnt().AddPoints(other.GetPacked_pnt().GetPoints());
3962  }
3963  } else {
3964  x_ChangeToMix(other);
3965  }
3966  break;
3967  }
3968  case e_Mix:
3969  {
3970  SetMix().AddSeqLoc(other);
3971  break;
3972  }
3973  case e_Equiv:
3974  {
3975  SetEquiv().Add(other);
3976  break;
3977  }
3978  case e_Bond:
3979  {
3980  x_ChangeToMix(other);
3981  break;
3982  }
3983  case e_Feat:
3984  default:
3985  NCBI_THROW_FMT(CSeqLocException, eIncomatible,
3986  "CSeq_loc::Add(): "
3987  "cannot add sub-location to location of type: "<<
3988  SelectionName(Which()));
3989  }
3990 }
3991 
3992 
3994 {
3995  switch ( Which() ) {
3996  case e_Int:
3997  SetInt().FlipStrand();
3998  break;
3999  case e_Pnt:
4000  SetPnt().FlipStrand();
4001  break;
4002  case e_Packed_int:
4003  SetPacked_int().FlipStrand();
4004  break;
4005  case e_Packed_pnt:
4006  SetPacked_pnt().FlipStrand();
4007  break;
4008  case e_Mix:
4009  SetMix().FlipStrand();
4010  break;
4011 
4012  default:
4013  break;
4014  }
4015 }
4016 
4017 
4018 // Types used in operations with seq-locs
4019 
4021 {
4022 public:
4025 
4028  {
4029  }
4030  CRangeWithFuzz(const CSeq_loc_CI& it)
4031  : TParent(it.GetRange()),
4032  m_Fuzz_from(it.GetFuzzFrom()),
4033  m_Fuzz_to(it.GetFuzzTo()),
4034  m_Strand(it.GetStrand())
4035  {
4036  }
4037 
4038  void ResetFuzzFrom(void) { m_Fuzz_from.Reset(); }
4039  void ResetFuzzTo(void) { m_Fuzz_to.Reset(); }
4040  bool IsSetFuzzFrom(void) const { return m_Fuzz_from; }
4041  bool IsSetFuzzTo(void) const { return m_Fuzz_to; }
4042  const CInt_fuzz& GetFuzzFrom(void) const { return *m_Fuzz_from; }
4043  const CInt_fuzz& GetFuzzTo(void) const { return *m_Fuzz_to; }
4044 
4045  // Add fuzzes assuming that both ranges had the same 'from'
4046  void AddFuzzFrom(const CRangeWithFuzz& rg)
4047  {
4049  }
4050 
4051  // Add fuzzes assuming that both ranges had the same 'to'
4052  void AddFuzzTo(const CRangeWithFuzz& rg)
4053  {
4055  }
4056 
4057  void AddFuzzFrom(const CInt_fuzz& fuzz, ENa_strand strand)
4058  {
4059  x_AddFuzz(m_Fuzz_from, ConstRef(&fuzz), strand);
4060  }
4061 
4062  void AddFuzzTo(const CInt_fuzz& fuzz, ENa_strand strand)
4063  {
4064  x_AddFuzz(m_Fuzz_to, ConstRef(&fuzz), strand);
4065  }
4066 
4067  void CopyFrom(const CRangeWithFuzz& rg)
4068  {
4069  SetFrom(rg.GetFrom());
4070  m_Fuzz_from = rg.m_Fuzz_from;
4071  }
4072 
4073  void CopyTo(const CRangeWithFuzz& rg)
4074  {
4075  SetTo(rg.GetTo());
4076  m_Fuzz_to = rg.m_Fuzz_to;
4077  }
4078 
4080  {
4081  TParent::position_type old_from = GetFrom();
4082  TParent::position_type old_to = GetTo();
4083  TParent::operator+=(rg);
4084  if (old_from != GetFrom()) {
4086  }
4087  else if (old_from == rg.GetFrom()) {
4088  // Reset fuzz if it's not the same for both ranges
4089  AddFuzzFrom(rg);
4090  }
4091  if (old_to != GetTo()) {
4093  }
4094  else if (old_to == rg.GetTo()) {
4095  AddFuzzTo(rg);
4096  }
4097  return *this;
4098  }
4099 
4101  {
4102  TParent::position_type old_from = GetFrom();
4103  TParent::position_type old_to = GetTo();
4104  TParent::operator+=(rg);
4105  // Reset fuzz if the corresponding extreme changes
4106  if (old_from != GetFrom()) {
4107  ResetFuzzFrom();
4108  }
4109  if (old_to != GetTo()) {
4110  ResetFuzzTo();
4111  }
4112  return *this;
4113  }
4114 
4115 private:
4117  const CInt_fuzz* copy_from)
4118  {
4119  TFuzz copy_from_cref;
4120  if (copy_from == fuzz) copy_from_cref.Reset(copy_from);
4121  // Since TFuzz is a const-ref, setting fuzz requires creating
4122  // a new object
4123  CRef<CInt_fuzz> new_fuzz(new CInt_fuzz);
4124  // The new value is optional
4125  if ( copy_from ) {
4126  new_fuzz->Assign(*copy_from);
4127  }
4128  fuzz.Reset(new_fuzz);
4129  return new_fuzz;
4130  }
4131 
4132  void x_AddFuzz(TFuzz& fuzz,
4133  const TFuzz& other,
4134  ENa_strand other_strand)
4135  {
4136  if ( !fuzz ) {
4137  // Use fuzz from the other range if available
4138  if ( other ) {
4139  x_SetFuzz(fuzz, other.GetPointerOrNull());
4140  }
4141  return;
4142  }
4143  if ( !other ) {
4144  // The other range has no fuzz, keep the current one
4145  return;
4146  }
4147  if (fuzz->Which() != other->Which()) {
4148  // Fuzzes have different types, reset to lim-unk.
4149  CRef<CInt_fuzz> new_fuzz = x_SetFuzz(fuzz, NULL);
4150  new_fuzz->SetLim(CInt_fuzz::eLim_unk);
4151  return;
4152  }
4153 
4154  const CInt_fuzz& fz = *fuzz;
4155  const CInt_fuzz& ofz = *other;
4156  // Both fuzzes are set and have the same type, try to merge them
4157  switch ( fz.Which() ) {
4158  case CInt_fuzz::e_Lim:
4159  {
4160  CInt_fuzz::ELim this_lim = fz.GetLim();
4161  CInt_fuzz::ELim other_lim = ofz.GetLim();
4162  bool this_rev = IsReverse(m_Strand);
4163  bool other_rev = IsReverse(other_strand);
4164  bool other_lt = other_lim == CInt_fuzz::eLim_lt ||
4165  (!other_rev && other_lim == CInt_fuzz::eLim_tl) ||
4166  (other_rev && other_lim == CInt_fuzz::eLim_tr);
4167  bool other_gt = other_lim == CInt_fuzz::eLim_gt ||
4168  (!other_rev && other_lim == CInt_fuzz::eLim_tr) ||
4169  (other_rev && other_lim == CInt_fuzz::eLim_tl);
4170  switch ( fz.GetLim() ) {
4171  case CInt_fuzz::eLim_lt:
4172  if ( other_lt ) {
4173  return; // the same
4174  }
4175  break;
4176  case CInt_fuzz::eLim_gt:
4177  if ( other_gt ) {
4178  return; // the same
4179  }
4180  break;
4181  case CInt_fuzz::eLim_tl:
4182  if ((!this_rev && other_lt) ||
4183  (this_rev && other_gt)) {
4184  return; // the same
4185  }
4186  break;
4187  case CInt_fuzz::eLim_tr:
4188  if ((!this_rev && other_gt) ||
4189  (this_rev && other_lt)) {
4190  return; // the same
4191  }
4192  break;
4193  default:
4194  if (other_lim == this_lim) {
4195  return;
4196  }
4197  break;
4198  }
4199  // Different limits - reset to lim-unk.
4200  CRef<CInt_fuzz> new_fuzz = x_SetFuzz(fuzz, NULL);
4201  new_fuzz->SetLim(CInt_fuzz::eLim_unk);
4202  break;
4203  }
4204  case CInt_fuzz::e_Alt:
4205  {
4206  // Use union
4207  CRef<CInt_fuzz> new_fuzz = x_SetFuzz(fuzz, fuzz);
4208  new_fuzz->SetAlt().insert(
4209  new_fuzz->SetAlt().end(),
4210  ofz.GetAlt().begin(),
4211  ofz.GetAlt().end());
4212  break;
4213  }
4214  case CInt_fuzz::e_Range:
4215  {
4216  // Use union
4218  CInt_fuzz::C_Range::TMin min2 = ofz.GetRange().GetMin();
4220  CInt_fuzz::C_Range::TMax max2 = ofz.GetRange().GetMax();
4221  if (min1 > min2 || max1 < max2) {
4222  CRef<CInt_fuzz> new_fuzz = x_SetFuzz(fuzz, NULL);
4223  new_fuzz->SetRange().SetMin(min1 < min2 ? min1 : min2);
4224  new_fuzz->SetRange().SetMax(max1 > max2 ? max1 : max2);
4225  }
4226  break;
4227  }
4228  case CInt_fuzz::e_P_m:
4229  {
4230  // Use max value
4231  CInt_fuzz::TP_m pm = ofz.GetP_m();
4232  if (fz.GetP_m() < pm) {
4233  CRef<CInt_fuzz> new_fuzz = x_SetFuzz(fuzz, NULL);
4234  new_fuzz->SetP_m(pm);
4235  }
4236  break;
4237  }
4238  case CInt_fuzz::e_Pct:
4239  {
4240  // Use max value
4241  CInt_fuzz::TPct pct = ofz.GetPct();
4242  if (fz.GetPct() < pct) {
4243  CRef<CInt_fuzz> new_fuzz = x_SetFuzz(fuzz, NULL);
4244  new_fuzz->SetPct(pct);
4245  }
4246  break;
4247  }
4248  default:
4249  // Failed to merge fuzzes
4250  fuzz.Reset();
4251  break;
4252  }
4253  }
4254 
4258 };
4259 
4260 
4262 {
4263 public:
4265 
4267  : m_Handle(idh)
4268  {
4269  if (id.IsLocal() && id.GetLocal().IsStr()) {
4270  m_Id.Reset(&id);
4271  }
4272  }
4273 
4274  CConstRef<CSeq_id> GetSeqId(void) const { return m_Id ? m_Id : m_Handle.GetSeqId(); }
4275 
4276  const CSeq_id_Handle& GetHandle(void) const { return m_Handle; }
4277 
4278  bool operator== (const CSeq_id_Handle_Wrapper& handle) const
4279  {
4280  return m_Handle == handle.m_Handle;
4281  }
4282 
4283  bool operator!= (const CSeq_id_Handle_Wrapper& handle) const
4284  {
4285  return m_Handle != handle.m_Handle;
4286  }
4287  bool operator< (const CSeq_id_Handle_Wrapper& handle) const
4288  {
4289  return m_Handle < handle.m_Handle;
4290  }
4291 
4293 
4294 private:
4297 };
4298 
4299 
4301 typedef vector<TRangeWithFuzz> TRanges;
4305 
4306 
4308 {
4309 public:
4310  CRange_Less(void) {}
4311 
4312  bool operator() (const TRangeWithFuzz& rg1, const TRangeWithFuzz& rg2) const
4313  {
4314  if ( rg1.IsWhole() ) {
4315  return !rg2.IsWhole();
4316  }
4317  if ( rg1.Empty() ) {
4318  return !rg2.Empty() && !rg2.IsWhole();
4319  }
4320  return !rg2.IsWhole() && !rg2.Empty() && rg1 < rg2;
4321  }
4322 };
4323 
4324 
4326 {
4327 public:
4329 
4330  bool operator() (const TRangeWithFuzz& rg1, const TRangeWithFuzz& rg2) const
4331  {
4332  if ( rg1.IsWhole() ) {
4333  return !rg2.IsWhole();
4334  }
4335  if ( rg1.Empty() ) {
4336  return !rg2.Empty() && !rg2.IsWhole();
4337  }
4338  if ( rg2.IsWhole() || rg2.Empty() ) {
4339  return false;
4340  }
4341  if (rg1.GetTo() != rg2.GetTo()) {
4342  return rg1.GetTo() > rg2.GetTo();
4343  }
4344  return rg1.GetFrom() < rg2.GetFrom();
4345  }
4346 };
4347 
4348 
4349 static inline
4351 {
4352  return (flags & CSeq_loc::fStrand_Ignore) != 0 ||
4353  IsReverse(str1) == IsReverse(str2);
4354 }
4355 
4356 
4357 static
4359  const TRangeWithFuzz& rg2, ENa_strand str2,
4361 {
4362  if ( !x_MatchStrand(str1, str2, flags) ) {
4363  return false;
4364  }
4365  // Check contained
4366  if ( (flags & CSeq_loc::fMerge_Contained) != 0 ) {
4367  if (rg1.GetFrom() <= rg2.GetFrom() && rg1.GetTo() >= rg2.GetTo()) {
4368  // rg2 already contained in rg1
4369  if (rg1.GetFrom() == rg2.GetFrom()) {
4370  rg1.AddFuzzFrom(rg2);
4371  }
4372  if (rg1.GetTo() == rg2.GetTo()) {
4373  rg1.AddFuzzTo(rg2);
4374  }
4375  return true;
4376  }
4377  if (rg1.GetFrom() >= rg2.GetFrom() && rg1.GetTo() <= rg2.GetTo()) {
4378  // rg1 contained in rg2
4379  bool same_from = rg1.GetFrom() == rg2.GetFrom();
4380  bool same_to = rg1.GetTo() == rg2.GetTo();
4381  rg1 = rg2;
4382  if (same_from) {
4383  rg1.AddFuzzFrom(rg2);
4384  }
4385  if (same_to) {
4386  rg1.AddFuzzTo(rg2);
4387  }
4388  return true;
4389  }
4390  }
4391  // Check overlapping
4392  if ( (flags & CSeq_loc::fMerge_OverlappingOnly) != 0 &&
4393  rg1.IntersectingWith(rg2) ) {
4394  rg1 += rg2;
4395  return true;
4396  }
4397  // Check abutting
4398  if ((flags & CSeq_loc::fMerge_AbuttingOnly) != 0) {
4399  if ( !IsReverse(str1) ) {
4400  if ( rg1.GetToOpen() == rg2.GetFrom() ) {
4401  rg1.CopyTo(rg2);
4402  return true;
4403  }
4404  }
4405  else {
4406  if (rg1.GetFrom() == rg2.GetToOpen()) {
4407  rg1.CopyFrom(rg2);
4408  return true;
4409  }
4410  }
4411  }
4412  return false;
4413 }
4414 
4415 
4416 static
4417 void x_PushRange(CSeq_loc& dst,
4418  const CSeq_id_Handle_Wrapper& idh,
4419  const TRangeWithFuzz& rg,
4420  ENa_strand strand)
4421 {
4422  if (dst.Which() != CSeq_loc::e_not_set) {
4423  if ( !dst.IsMix() ) {
4424  dst.ChangeToMix();
4425  }
4426  }
4427  if ( !idh ) {
4428  // NULL
4429  if (dst.IsMix()) {
4430  dst.SetMix().Set().push_back(Ref(new CSeq_loc(CSeq_loc::e_Null)));
4431  }
4432  else {
4433  dst.SetNull();
4434  }
4435  return;
4436  }
4437  CRef<CSeq_id> id(new CSeq_id());
4438  id->Assign(*idh.GetSeqId());
4439  if ( rg.IsWhole() ) {
4440  if (dst.IsMix()) {
4441  CRef<CSeq_loc> whole(new CSeq_loc);
4442  whole->SetWhole(*id);
4443  dst.SetMix().Set().push_back(whole);
4444  }
4445  else {
4446  dst.SetWhole(*id);
4447  }
4448  }
4449  else if ( rg.Empty() ) {
4450  if (dst.IsMix()) {
4451  CRef<CSeq_loc> empty(new CSeq_loc);
4452  empty->SetEmpty(*id);
4453  dst.SetMix().Set().push_back(empty);
4454  }
4455  else {
4456  dst.SetEmpty(*id);
4457  }
4458  }
4459  else if ( rg.GetLength() == 1 &&
4460  rg.IsSetFuzzFrom() == rg.IsSetFuzzTo() &&
4461  ( !rg.IsSetFuzzFrom() ||
4462  rg.GetFuzzFrom().Equals(rg.GetFuzzTo()) ) )
4463  {
4464  // Preserve points
4465  CRef<CSeq_point> pnt(new CSeq_point);
4466  pnt->SetId(*id);
4467  pnt->SetPoint(rg.GetFrom());
4468  if (strand != eNa_strand_unknown) {
4469  pnt->SetStrand(strand);
4470  }
4471  if ( rg.IsSetFuzzFrom() ) {
4472  pnt->SetFuzz().Assign(rg.GetFuzzFrom());
4473  } else if( rg.IsSetFuzzTo() ) {
4474  pnt->SetFuzz().Assign(rg.GetFuzzTo());
4475  }
4476  if (dst.IsMix()) {
4477  CRef<CSeq_loc> pnt_loc(new CSeq_loc);
4478  pnt_loc->SetPnt(*pnt);
4479  dst.SetMix().Set().push_back(pnt_loc);
4480  }
4481  else {
4482  dst.SetPnt(*pnt);
4483  }
4484  }
4485  else {
4486  if (dst.IsMix()) {
4487  CRef<CSeq_loc> int_loc(new CSeq_loc);
4488  CSeq_interval& ival = int_loc->SetInt();
4489  ival.SetFrom(rg.GetFrom());
4490  ival.SetTo(rg.GetTo());
4491  ival.SetId().Assign(*id);
4492  if (strand != eNa_strand_unknown) {
4493  ival.SetStrand(strand);
4494  }
4495  if ( rg.IsSetFuzzFrom() ) {
4496  ival.SetFuzz_from().Assign(rg.GetFuzzFrom());
4497  }
4498  if ( rg.IsSetFuzzTo() ) {
4499  ival.SetFuzz_to().Assign(rg.GetFuzzTo());
4500  }
4501  dst.SetMix().Set().push_back(int_loc);
4502  }
4503  else {
4504  CRef<CSeq_interval> interval(new CSeq_interval(*id,
4505  rg.GetFrom(),
4506  rg.GetTo(),
4507  strand));
4508  if ( rg.IsSetFuzzFrom() ) {
4509  interval->SetFuzz_from().Assign(rg.GetFuzzFrom());
4510  }
4511  if ( rg.IsSetFuzzTo() ) {
4512  interval->SetFuzz_to().Assign(rg.GetFuzzTo());
4513  }
4514  dst.SetInt(*interval);
4515  }
4516  }
4517 }
4518 
4519 
4520 static
4521 void x_SingleRange(CSeq_loc& dst,
4522  const CSeq_loc& src,
4523  ISynonymMapper& syn_mapper)
4524 {
4525  // Create a single range
4527  CSeq_id_Handle_Wrapper first_id;
4528  ENa_strand first_strand = eNa_strand_unknown;
4529  for (CSeq_loc_CI it(src, CSeq_loc_CI::eEmpty_Allow); it; ++it) {
4530  CSeq_id_Handle_Wrapper next_id(syn_mapper.GetBestSynonym(it.GetSeq_id()), it.GetSeq_id());
4531  if ( !next_id ) {
4532  // Ignore NULLs
4533  continue;
4534  }
4535  if ( first_id ) {
4536  // Seq-id may be missing for NULL seq-loc
4537  if (next_id && first_id != next_id) {
4538  NCBI_THROW(CSeqLocException, eMultipleId,
4539  "Can not merge multi-id seq-loc");
4540  }
4541  }
4542  else {
4543  first_id = next_id;
4544  first_strand = it.GetStrand();
4545  }
4546  total_rg += TRangeWithFuzz(it);
4547  }
4548  if ( first_id ) {
4549  CRef<CSeq_id> id(new CSeq_id);
4550  id->Assign(*first_id.GetSeqId());
4551  CRef<CSeq_interval> interval(new CSeq_interval(*id,
4552  total_rg.GetFrom(),
4553  total_rg.GetTo(),
4554  first_strand));
4555  if ( total_rg.IsSetFuzzFrom() ) {
4556  interval->SetFuzz_from().Assign(total_rg.GetFuzzFrom());
4557  }
4558  if ( total_rg.IsSetFuzzTo() ) {
4559  interval->SetFuzz_to().Assign(total_rg.GetFuzzTo());
4560  }
4561  dst.SetInt(*interval);
4562  }
4563  else {
4564  // Null seq-loc
4565  dst.SetNull();
4566  }
4567 }
4568 
4569 
4570 static
4571 void x_RangesToSeq_loc(CSeq_loc& dst,
4572  TIdToRangeMap& id_map,
4573  ENa_strand default_strand,
4575 {
4576  // Iterate ids for each strand
4577  NON_CONST_ITERATE(TIdToRangeMap, id_it, id_map) {
4578  if ( !id_it->first ) {
4579  // All NULLs merged
4580  x_PushRange(dst,
4581  id_it->first,
4584  continue;
4585  }
4586  CRef<CSeq_id> id(new CSeq_id);
4587  id->Assign(*id_it->first.GetSeqId());
4588  TRanges& ranges = id_it->second;
4589  if ( (flags & CSeq_loc::fSort) != 0 ) {
4590  if ( !IsReverse(default_strand) ) {
4591  sort(ranges.begin(), ranges.end(), CRange_Less());
4592  }
4593  else {
4594  sort(ranges.begin(), ranges.end(), CRange_ReverseLess());
4595  }
4596  }
4597  // Merge ranges according to the flags, add to destination
4599  bool have_range = false;
4600  ITERATE(TRanges, rg, ranges) {
4601  if (x_MergeRanges(last_rg, default_strand,
4602  *rg, default_strand,
4603  flags)) {
4604  have_range = true;
4605  continue;
4606  }
4607  // No merging - push current range, reset last values
4608  if (have_range) {
4609  x_PushRange(dst, id_it->first, last_rg, default_strand);
4610  }
4611  last_rg = *rg;
4612  have_range = true;
4613  }
4614  if (have_range) {
4615  x_PushRange(dst, id_it->first, last_rg, default_strand);
4616  }
4617  }
4618 }
4619 
4620 
4621 static
4622 void x_MergeNoSort(CSeq_loc& dst,
4623  const CSeq_loc& src,
4625  ISynonymMapper& syn_mapper)
4626 {
4627  _ASSERT((flags & CSeq_loc::fSort) == 0);
4628  CSeq_id_Handle_Wrapper last_id;
4630  ENa_strand last_strand = eNa_strand_unknown;
4631  bool have_range = false;
4632  for (CSeq_loc_CI it(src, CSeq_loc_CI::eEmpty_Allow); it; ++it) {
4633  CSeq_id_Handle_Wrapper idh(syn_mapper.GetBestSynonym(it.GetSeq_id()), it.GetSeq_id());
4634  // ID and strand must match
4635  TRangeWithFuzz it_rg(it);
4636  if ( have_range && last_id == idh ) {
4637  if (x_MergeRanges(last_rg,
4638  last_strand,
4639  it_rg,
4640  it.GetStrand(),
4641  flags)) {
4642  have_range = true;
4643  continue;
4644  }
4645  }
4646  // No merging - push current range, reset last values
4647  if ( have_range ) {
4648  x_PushRange(dst, last_id, last_rg, last_strand);
4649  }
4650  last_id = idh;
4651  last_rg = it_rg;
4652  last_strand = it.GetStrand();
4653  have_range = true;
4654  }
4655  if ( have_range ) {
4656  x_PushRange(dst, last_id, last_rg, last_strand);
4657  }
4658  if (dst.Which() == CSeq_loc::e_not_set) {
4659  dst.SetNull();
4660  }
4661 }
4662 
4663 
4664 static
4665 void x_MergeAndSort(CSeq_loc& dst,
4666  const CSeq_loc& src,
4668  ISynonymMapper& syn_mapper)
4669 {
4670  bool use_strand = (flags & CSeq_loc::fStrand_Ignore) == 0;
4671  // Id -> range map for both strands
4672  unique_ptr<TIdToRangeMap> pid_map_minus(use_strand ?
4673  new TIdToRangeMap : 0);
4674  TIdToRangeMap id_map_plus;
4675  TIdToRangeMap& id_map_minus = use_strand ?
4676  *pid_map_minus.get() : id_map_plus;
4677 
4678  // Prepare default strands
4679  ENa_strand default_plus = eNa_strand_unknown;
4680  ENa_strand default_minus = eNa_strand_unknown;
4681 
4682  // Split location by by id/strand/range
4683  for (CSeq_loc_CI it(src, CSeq_loc_CI::eEmpty_Allow); it; ++it) {
4684  CSeq_id_Handle_Wrapper idh(syn_mapper.GetBestSynonym(it.GetSeq_id()), it.GetSeq_id());
4685  ENa_strand strand = it.GetStrand();
4686  if ( IsReverse(strand) ) {
4687  id_map_minus[idh].push_back(TRangeWithFuzz(it));
4688  if (use_strand) {
4689  if (default_minus == eNa_strand_unknown) {
4690  default_minus = strand;
4691  }
4692  else if (default_minus != strand) {
4693  default_minus = eNa_strand_minus;
4694  }
4695  }
4696  }
4697  else {
4698  id_map_plus[idh].push_back(TRangeWithFuzz(it));
4699  if (use_strand) {
4700  if (default_plus == eNa_strand_unknown) {
4701  default_plus = strand;
4702  }
4703  else if (default_plus != strand) {
4704  default_plus = eNa_strand_plus;
4705  }
4706  }
4707  }
4708  }
4709 
4710  x_RangesToSeq_loc(dst, id_map_plus, default_plus, flags);
4711  if ( use_strand ) {
4712  x_RangesToSeq_loc(dst, id_map_minus, default_minus, flags);
4713  }
4714  if (dst.Which() == CSeq_loc::e_not_set) {
4715  dst.SetNull();
4716  }
4717 }
4718 
4719 
4721 
4722 static
4723 void x_SingleRange(CSeq_loc& dst,
4724  const CSeq_loc& minuend,
4725  const TFuzzMap& fuzz_from_plus,
4726  const TFuzzMap& fuzz_from_minus,
4727  const TFuzzMap& fuzz_to_plus,
4728  const TFuzzMap& fuzz_to_minus,
4729  TIdToRangeColl& rg_coll_plus,
4730  TIdToRangeColl& rg_coll_minus,
4731  ISynonymMapper& syn_mapper,
4732  ILengthGetter& len_getter)
4733 {
4735  CSeq_id_Handle_Wrapper first_id;
4736  ENa_strand first_strand = eNa_strand_unknown;
4737  for (CSeq_loc_CI it(minuend, CSeq_loc_CI::eEmpty_Allow); it; ++it) {
4738  CSeq_id_Handle_Wrapper next_id(syn_mapper.GetBestSynonym(it.GetSeq_id()), it.GetSeq_id());
4739  if ( !next_id ) {
4740  // Ignore NULLs
4741  continue;
4742  }
4743  if ( first_id ) {
4744  // Seq-id may be missing for NULL seq-loc
4745  if (next_id && first_id != next_id) {
4746  NCBI_THROW(CSeqLocException, eMultipleId,
4747  "Can not merge multi-id seq-loc");
4748  }
4749  }
4750  else {
4751  first_id = next_id;
4752  first_strand = it.GetStrand();
4753  }
4754  TRangeWithFuzz it_range = TRangeWithFuzz(it);
4755  if (it_range.GetFrom() >= total_rg.GetFrom() &&
4756  it_range.GetTo() <= total_rg.GetTo()) {
4757  // Nothing new can be added from this interval
4758  continue;
4759  }
4760  if ( it_range.IsWhole() ) {
4761  it_range.SetOpen(0, len_getter.GetLength(it.GetSeq_id()));
4762  it_range.ResetFuzzFrom();
4763  it_range.ResetFuzzTo();
4764  }
4765  TRangeColl it_rg_coll(it_range);
4766  TIdToRangeColl& rg_coll = IsReverse(it.GetStrand()) ?
4767  rg_coll_minus : rg_coll_plus;
4768  TIdToRangeColl::const_iterator id_it = rg_coll.find(next_id);
4769  if (id_it != rg_coll.end()) {
4770  it_rg_coll -= id_it->second;
4771  }
4772  TRangeWithFuzz curr_rg(it_rg_coll.GetLimits());
4773  if (curr_rg.GetFrom() == it_range.GetFrom()) {
4774  curr_rg.AddFuzzFrom(it_range);
4775  }
4776  if (curr_rg.GetTo() == it_range.GetTo()) {
4777  curr_rg.AddFuzzTo(it_range);
4778  }
4779 
4780  // If range start/stop comes from subtrahend, copy fuzz.
4781  const TFuzzMap& fm_from = IsReverse(it.GetStrand()) ? fuzz_from_minus : fuzz_from_plus;
4782  TFuzzMap::const_iterator subtr_fuzz = fm_from.find(curr_rg.GetToOpen());
4783  if (subtr_fuzz != fm_from.end()) {
4784  curr_rg.AddFuzzTo(*subtr_fuzz->second, it.GetStrand());
4785  }
4786  const TFuzzMap& fm_to = IsReverse(it.GetStrand()) ? fuzz_to_minus : fuzz_to_plus;
4787  subtr_fuzz = fm_to.find(curr_rg.GetFrom());
4788  if (subtr_fuzz != fm_to.end()) {
4789  curr_rg.AddFuzzFrom(*subtr_fuzz->second, it.GetStrand());
4790  }
4791 
4792  total_rg += curr_rg;
4793  }
4794 
4795  if ( first_id ) {
4796  CRef<CSeq_id> id(new CSeq_id);
4797  id->Assign(*first_id.GetSeqId());
4798  CRef<CSeq_interval> interval(new CSeq_interval(*id,
4799  total_rg.GetFrom(),
4800  total_rg.GetTo(),
4801  first_strand));
4802  if ( total_rg.IsSetFuzzFrom() ) {
4803  CRef<CInt_fuzz> fuzz(new CInt_fuzz);
4804  fuzz->Assign(total_rg.GetFuzzFrom());
4805  interval->SetFuzz_from(*fuzz);
4806  }
4807  if ( total_rg.IsSetFuzzTo() ) {
4808  CRef<CInt_fuzz> fuzz(new CInt_fuzz);
4809  fuzz->Assign(total_rg.GetFuzzTo());
4810  interval->SetFuzz_to(*fuzz);
4811  }
4812  dst.SetInt(*interval);
4813  }
4814  else {
4815  // Null seq-loc
4816  dst.SetNull();
4817  }
4818 }
4819 
4820 
4821 static
4822 void x_SubNoSort(CSeq_loc& dst,
4823  const CSeq_loc& minuend,
4824  const TFuzzMap& fuzz_from_plus,
4825  const TFuzzMap& fuzz_from_minus,
4826  const TFuzzMap& fuzz_to_plus,
4827  const TFuzzMap& fuzz_to_minus,
4828  TIdToRangeColl& rg_coll_plus,
4829  TIdToRangeColl& rg_coll_minus,
4830  ISynonymMapper& syn_mapper,
4831  ILengthGetter& len_getter,
4833 {
4834  _ASSERT((flags & CSeq_loc::fSort) == 0);
4835  CSeq_id_Handle_Wrapper last_id;
4837  ENa_strand last_strand = eNa_strand_unknown;
4838  bool have_range = false;
4839  for (CSeq_loc_CI it(minuend, CSeq_loc_CI::eEmpty_Allow); it; ++it) {
4840  CSeq_id_Handle_Wrapper idh(syn_mapper.GetBestSynonym(it.GetSeq_id()), it.GetSeq_id());
4841  bool rev = IsReverse(it.GetStrand());
4842  TRangeWithFuzz it_range = TRangeWithFuzz(it);
4843  if ( it_range.IsWhole() ) {
4844  it_range.SetOpen(0, len_getter.GetLength(it.GetSeq_id()));
4845  }
4846  TRangeColl it_rg_coll(it_range);
4847  TIdToRangeColl& rg_coll = IsReverse(it.GetStrand()) ?
4848  rg_coll_minus : rg_coll_plus;
4849  TIdToRangeColl::const_iterator id_it = rg_coll.find(idh);
4850  list<TRangeWithFuzz> result_ranges;
4851  bool modified = false;
4852  if (id_it != rg_coll.end()) {
4853  // Check if there's anything to subtract
4854  ITERATE(TRangeColl, check_it, id_it->second) {
4855  if ( it_rg_coll.IntersectingWith(*check_it) ) {
4856  it_rg_coll -= id_it->second;
4857  modified = true;
4858  ITERATE(TRangeColl, result_it, it_rg_coll) {
4859  if ( rev ) {
4860  result_ranges.push_front(*result_it);
4861  }
4862  else {
4863  result_ranges.push_back(*result_it);
4864  }
4865  }
4866  break;
4867  }
4868  }
4869  }
4870  if ( modified ) {
4871  ITERATE(list<TRangeWithFuzz>, rg_it, result_ranges) {
4872  TRangeWithFuzz curr_rg(*rg_it);
4873  if (curr_rg.GetFrom() == it_range.GetFrom()) {
4874  curr_rg.AddFuzzFrom(it_range);
4875  }
4876  if (curr_rg.GetTo() == it_range.GetTo()) {
4877  curr_rg.AddFuzzTo(it_range);
4878  }
4879 
4880  // If range start/stop comes from subtrahend, copy fuzz.
4881  const TFuzzMap& fm_from = IsReverse(it.GetStrand()) ? fuzz_from_minus : fuzz_from_plus;
4882  TFuzzMap::const_iterator subtr_fuzz = fm_from.find(curr_rg.GetToOpen());
4883  if (subtr_fuzz != fm_from.end()) {
4884  curr_rg.AddFuzzTo(*subtr_fuzz->second, it.GetStrand());
4885  }
4886  const TFuzzMap& fm_to = IsReverse(it.GetStrand()) ? fuzz_to_minus : fuzz_to_plus;
4887  subtr_fuzz = fm_to.find(curr_rg.GetFrom());
4888  if (subtr_fuzz != fm_to.end()) {
4889  curr_rg.AddFuzzFrom(*subtr_fuzz->second, it.GetStrand());
4890  }
4891 
4892  if ( have_range && last_id == idh ) {
4893  if (x_MergeRanges(last_rg,
4894  last_strand,
4895  curr_rg,
4896  it.GetStrand(),
4897  flags)) {
4898  have_range = true;
4899  continue;
4900  }
4901  }
4902  // No merging - push current range, reset last values
4903  if ( have_range ) {
4904  x_PushRange(dst, last_id, last_rg, last_strand);
4905  }
4906  last_id = idh;
4907  last_rg = curr_rg;
4908  last_strand = it.GetStrand();
4909  have_range = true;
4910  }
4911  }
4912  else {
4913  if ( have_range ) {
4914  bool merged = false;
4915  if (last_id == idh) {
4916  merged = x_MergeRanges(last_rg,
4917  last_strand,
4918  it_range,
4919  it.GetStrand(),
4920  flags);
4921  }
4922  if ( !merged ) {
4923  x_PushRange(dst, last_id, last_rg, last_strand);
4924  }
4925  }
4926  last_id = idh;
4927  last_rg = it_range;
4928  last_strand = it.GetStrand();
4929  have_range = true;
4930  }
4931  }
4932  if ( have_range ) {
4933  x_PushRange(dst, last_id, last_rg, last_strand);
4934  }
4935  if (dst.Which() == CSeq_loc::e_not_set) {
4936  dst.SetNull();
4937  }
4938 }
4939 
4940 
4941 static
4942 void x_SubAndSort(CSeq_loc& dst,
4943  const CSeq_loc& minuend,
4944  const TFuzzMap& fuzz_from_plus,
4945  const TFuzzMap& fuzz_from_minus,
4946  const TFuzzMap& fuzz_to_plus,
4947  const TFuzzMap& fuzz_to_minus,
4948  TIdToRangeColl& rg_coll_plus,
4949  TIdToRangeColl& rg_coll_minus,
4950  ISynonymMapper& syn_mapper,
4951  ILengthGetter& len_getter,
4953 {
4954  bool use_strand = (flags & CSeq_loc::fStrand_Ignore) == 0;
4955 
4956  // Id -> range map for both strands
4957  unique_ptr<TIdToRangeMap> p_id_map_minus(use_strand ?
4958  new TIdToRangeMap : 0);
4959  TIdToRangeMap id_map_plus;
4960  TIdToRangeMap& id_map_minus = use_strand ?
4961  *p_id_map_minus.get() : id_map_plus;
4962 
4963  // Prepare default strands
4964  ENa_strand default_plus = use_strand ?
4966  ENa_strand default_minus = use_strand ?
4968 
4969  for (CSeq_loc_CI it(minuend, CSeq_loc_CI::eEmpty_Allow); it; ++it) {
4970  CSeq_id_Handle_Wrapper idh(syn_mapper.GetBestSynonym(it.GetSeq_id()), it.GetSeq_id());
4971  TRangeWithFuzz it_range = TRangeWithFuzz(it);
4972  if ( it_range.IsWhole() ) {
4973  it_range.SetOpen(0, len_getter.GetLength(it.GetSeq_id()));
4974  it_range.ResetFuzzFrom();
4975  it_range.ResetFuzzTo();
4976  }
4977  TRangeColl it_rg_coll(it_range);
4978  TRanges& rg_map = IsReverse(it.GetStrand()) ?
4979  id_map_minus[idh] : id_map_plus[idh];
4980  TIdToRangeColl& rg_coll = IsReverse(it.GetStrand()) ?
4981  rg_coll_minus : rg_coll_plus;
4982  TIdToRangeColl::const_iterator id_it = rg_coll.find(idh);
4983  bool modified = false;
4984  if (id_it != rg_coll.end()) {
4985  // Check if there's anything to subtract
4986  ITERATE(TRangeColl, check_it, id_it->second) {
4987  if ( it_rg_coll.IntersectingWith(*check_it) ) {
4988  it_rg_coll -= id_it->second;
4989  modified = true;
4990  break;
4991  }
4992  }
4993  }
4994  if ( modified ) {
4995  ITERATE(TRangeColl, rg_it, it_rg_coll) {
4996  TRangeWithFuzz curr_rg(*rg_it);
4997  // If range start/stop comes from the minuend, preserve strand.
4998  if (curr_rg.GetFrom() == it_range.GetFrom()) {
4999  curr_rg.AddFuzzFrom(it_range);
5000  }
5001  if (curr_rg.GetTo() == it_range.GetTo()) {
5002  curr_rg.AddFuzzTo(it_range);
5003  }
5004 
5005  // If range start/stop comes from subtrahend, copy fuzz.
5006  const TFuzzMap& fm_from = IsReverse(it.GetStrand()) ? fuzz_from_minus : fuzz_from_plus;
5007  TFuzzMap::const_iterator subtr_fuzz = fm_from.find(curr_rg.GetToOpen());
5008  if (subtr_fuzz != fm_from.end()) {
5009  curr_rg.AddFuzzTo(*subtr_fuzz->second, it.GetStrand());
5010  }
5011  const TFuzzMap& fm_to = IsReverse(it.GetStrand()) ? fuzz_to_minus : fuzz_to_plus;
5012  subtr_fuzz = fm_to.find(curr_rg.GetFrom());
5013  if (subtr_fuzz != fm_to.end()) {
5014  curr_rg.AddFuzzFrom(*subtr_fuzz->second, it.GetStrand());
5015  }
5016 
5017  rg_map.push_back(curr_rg);
5018  }
5019  }
5020  else {
5021  rg_map.push_back(it_range);
5022  }
5023  }
5024 
5025  x_RangesToSeq_loc(dst, id_map_plus, default_plus, flags);
5026  if ( use_strand ) {
5027  x_RangesToSeq_loc(dst, id_map_minus, default_minus, flags);
5028  }
5029  if (dst.Which() == CSeq_loc::e_not_set) {
5030  dst.SetNull();
5031  }
5032 }
5033 
5034 
5036 {
5037 public:
5039  virtual ~CDummySynonymMapper(void) {}
5040 
5042  {
5043  return CSeq_id_Handle::GetHandle(id);
5044  }
5045 };
5046 
5047 
5049 {
5050 public:
5052  virtual ~CDummyLengthGetter(void) {}
5053 
5054  virtual TSeqPos GetLength(const CSeq_id&)
5055  {
5057  }
5058 };
5059 
5060 
5062  ISynonymMapper* syn_mapper) const
5063 {
5064  unique_ptr<CDummySynonymMapper> p_mapper;
5065  if ( !syn_mapper ) {
5066  p_mapper.reset(new CDummySynonymMapper);
5067  syn_mapper = p_mapper.get();
5068  }
5069 
5070  CRef<CSeq_loc> ret(new CSeq_loc);
5071  if ( (flags & CSeq_loc::fMerge_SingleRange) != 0 ) {
5072  x_SingleRange(*ret, *this, *syn_mapper);
5073  }
5074  else if ( (flags & CSeq_loc::fSort) == 0 ) {
5075  x_MergeNoSort(*ret, *this, flags, *syn_mapper);
5076  }
5077  else {
5078  x_MergeAndSort(*ret, *this, flags, *syn_mapper);
5079  }
5080  return ret;
5081 }
5082 
5083 
5084 CRef<CSeq_loc> CSeq_loc::Add(const CSeq_loc& other,
5085  TOpFlags flags,
5086  ISynonymMapper* syn_mapper) const
5087 {
5088  unique_ptr<CDummySynonymMapper> p_mapper;
5089  if ( !syn_mapper ) {
5090  p_mapper.reset(new CDummySynonymMapper);
5091  syn_mapper = p_mapper.get();
5092  }
5093 
5094  CRef<CSeq_loc> ret(new CSeq_loc);
5095  CSeq_loc tmp;
5096  tmp.SetMix().AddSeqLoc(const_cast<CSeq_loc&>(*this));
5097  tmp.SetMix().AddSeqLoc(const_cast<CSeq_loc&>(other));
5098  if ( (flags & CSeq_loc::fMerge_SingleRange) != 0 ) {
5099  x_SingleRange(*ret, tmp, *syn_mapper);
5100  }
5101  else if ( (flags & CSeq_loc::fSort) == 0 ) {
5102  x_MergeNoSort(*ret, tmp, flags, *syn_mapper);
5103  }
5104  else {
5105  x_MergeAndSort(*ret, tmp, flags, *syn_mapper);
5106  }
5107  return ret;
5108 }
5109 
5110 
5111 CRef<CSeq_loc> CSeq_loc::Subtract(const CSeq_loc& other,
5112  TOpFlags flags,
5113  ISynonymMapper* syn_mapper,
5114  ILengthGetter* len_getter) const
5115 {
5116  unique_ptr<CDummySynonymMapper> p_mapper;
5117  if ( !syn_mapper ) {
5118  p_mapper.reset(new CDummySynonymMapper);
5119  syn_mapper = p_mapper.get();
5120  }
5121  unique_ptr<CDummyLengthGetter> p_getter;
5122  if ( !len_getter ) {
5123  p_getter.reset(new CDummyLengthGetter);
5124  len_getter = p_getter.get();
5125  }
5126 
5127  CRef<CSeq_loc> ret(new CSeq_loc);
5128 
5129  bool use_strand = (flags & CSeq_loc::fStrand_Ignore) == 0;
5130 
5131  // Range collection for each strand
5132  unique_ptr<TIdToRangeColl> p_rg_coll_minus(use_strand ?
5133  new TIdToRangeColl : 0);
5134  TIdToRangeColl rg_coll_plus;
5135  TIdToRangeColl& rg_coll_minus = use_strand ?
5136  *p_rg_coll_minus.get() : rg_coll_plus;
5137 
5138  // Collect fuzzes so that they can be copied to the new ranges after subtracting.
5139  // Note: if there are multiple ranges with the same boundaries only the last
5140  // fuzz found will be used.
5141  TFuzzMap fuzz_from_plus, fuzz_from_minus, fuzz_to_plus, fuzz_to_minus;
5142 
5143  // Create range collection(s) for loc2
5144  for (CSeq_loc_CI it(other); it; ++it) {
5145  if ( it.IsEmpty() ) {
5146  continue;
5147  }
5148  CSeq_id_Handle_Wrapper idh(syn_mapper->GetBestSynonym(it.GetSeq_id()), it.GetSeq_id());
5149  TRangeColl& rmap = IsReverse(it.GetStrand()) ?
5150  rg_coll_minus[idh] : rg_coll_plus[idh];
5151  rmap += TRangeWithFuzz(it);
5152 
5153  const CInt_fuzz* fuzz = it.GetFuzzFrom();
5154  if ( fuzz ) {
5155  if (it.IsSetStrand() && IsReverse(it.GetStrand())) {
5156  fuzz_from_minus[it.GetRange().GetFrom()].Reset(fuzz);
5157  }
5158  else {
5159  fuzz_from_plus[it.GetRange().GetFrom()].Reset(fuzz);
5160  }
5161  }
5162  fuzz = it.GetFuzzTo();
5163  if ( fuzz ) {
5164  if (it.IsSetStrand() && IsReverse(it.GetStrand())) {
5165  fuzz_to_minus[it.GetRange().GetToOpen()].Reset(fuzz);
5166  }
5167  else {
5168  fuzz_to_plus[it.GetRange().GetToOpen()].Reset(fuzz);
5169  }
5170  }
5171  }
5172 
5173  if ( (flags & CSeq_loc::fMerge_SingleRange) != 0 ) {
5174  x_SingleRange(*ret,
5175  *this,
5176  fuzz_from_plus, fuzz_from_minus, fuzz_to_plus, fuzz_to_minus,
5177  rg_coll_plus,
5178  rg_coll_minus,
5179  *syn_mapper,
5180  *len_getter);
5181  }
5182  else if ( (flags & CSeq_loc::fSort) == 0 ) {
5183  x_SubNoSort(*ret,
5184  *this,
5185  fuzz_from_plus, fuzz_from_minus, fuzz_to_plus, fuzz_to_minus,
5186  rg_coll_plus,
5187  rg_coll_minus,
5188  *syn_mapper,
5189  *len_getter,
5190  flags);
5191  }
5192  else {
5193  x_SubAndSort(*ret,
5194  *this,
5195  fuzz_from_plus, fuzz_from_minus, fuzz_to_plus, fuzz_to_minus,
5196  rg_coll_plus,
5197  rg_coll_minus,
5198  *syn_mapper,
5199  *len_getter,
5200  flags);
5201  }
5202 
5203  return ret;
5204 }
5205 
5206 
5207 CRef<CSeq_loc> CSeq_loc::Intersect(const CSeq_loc& other,
5208  TOpFlags flags,
5209  ISynonymMapper* syn_mapper) const
5210 {
5211  unique_ptr<CDummyLengthGetter> len_getter(new CDummyLengthGetter);
5212  CRef<CSeq_loc> tmp = Subtract(other,
5213  // This flag should be used only in the second subtraction
5214  flags & ~fMerge_SingleRange,
5215  syn_mapper, len_getter.get());
5216  return Subtract(*tmp, flags, syn_mapper, len_getter.get());
5217 }
5218 
5219 
5221 {
5222  switch ( Which() ) {
5223  case e_Int:
5224  SetInt().SetStrand(strand);
5225  break;
5226  case e_Pnt:
5227  SetPnt().SetStrand(strand);
5228  break;
5229  case e_Packed_int:
5230  SetPacked_int().SetStrand(strand);
5231  break;
5232  case e_Packed_pnt:
5233  SetPacked_pnt().SetStrand(strand);
5234  break;
5235  case e_Mix:
5236  SetMix().SetStrand(strand);
5237  break;
5238 
5239  default:
5240  break;
5241  }
5242 }
5243 
5244 
5246 {
5247  switch ( Which() ) {
5248  case e_Int:
5249  SetInt().ResetStrand();
5250  break;
5251  case e_Pnt:
5252  SetPnt().ResetStrand();
5253  break;
5254  case e_Packed_int:
5255  SetPacked_int().ResetStrand();
5256  break;
5257  case e_Packed_pnt:
5258  SetPacked_pnt().ResetStrand();
5259  break;
5260  case e_Mix:
5261  SetMix().ResetStrand();
5262  break;
5263 
5264  default:
5265  break;
5266  }
5267 }
5268 
5269 
5270 END_objects_SCOPE // namespace ncbi::objects::
5272 
5273 #undef NCBI_USE_ERRCODE_X
User-defined methods of the data storage class.
bool IsReverse(ENa_strand s)
Definition: Na_strand.hpp:75
ESeqLocExtremes
Used to determine the meaning of a location's Start/Stop positions.
Definition: Na_strand.hpp:61
@ eExtreme_Positional
numerical value
Definition: Na_strand.hpp:63
@ eExtreme_Biological
5' and 3'
Definition: Na_strand.hpp:62
EIsSetStrand
Flag used by IsSetStrand() to switch between any/all modes.
Definition: Na_strand.hpp:51
User-defined methods of the data storage class.
void x_Assign(CInt_fuzz &dst, const CInt_fuzz &src)
Definition: Seq_loc.cpp:192
int s_CompareIntervals(const Container1 &container1, const Container2 &container2, bool minus_strand, const CSeq_loc::ISubLocFilter *filter)
Definition: Seq_loc.cpp:757
static void x_RangesToSeq_loc(CSeq_loc &dst, TIdToRangeMap &id_map, ENa_strand default_strand, CSeq_loc::TOpFlags flags)
Definition: Seq_loc.cpp:4571
static bool x_MatchStrand(ENa_strand str1, ENa_strand str2, CSeq_loc::TOpFlags flags)
Definition: Seq_loc.cpp:4350
static void s_GetLabel(const CSeq_id &id, string *label)
Definition: Seq_loc.cpp:3006
map< CSeq_id_Handle_Wrapper, TRanges > TIdToRangeMap
Definition: Seq_loc.cpp:4302
static void x_SubAndSort(CSeq_loc &dst, const CSeq_loc &minuend, const TFuzzMap &fuzz_from_plus, const TFuzzMap &fuzz_from_minus, const TFuzzMap &fuzz_to_plus, const TFuzzMap &fuzz_to_minus, TIdToRangeColl &rg_coll_plus, TIdToRangeColl &rg_coll_minus, ISynonymMapper &syn_mapper, ILengthGetter &len_getter, CSeq_loc::TOpFlags flags)
Definition: Seq_loc.cpp:4942
vector< TRangeWithFuzz > TRanges
Definition: Seq_loc.cpp:4301
static bool s_InsertExpands(size_t idx, size_t start, size_t end)
Definition: Seq_loc.cpp:1687
BEGIN_LOCAL_NAMESPACE
Definition: Seq_loc.cpp:682
map< TSeqPos, CConstRef< CInt_fuzz > > TFuzzMap
Definition: Seq_loc.cpp:4720
static void x_MergeNoSort(CSeq_loc &dst, const CSeq_loc &src, CSeq_loc::TOpFlags flags, ISynonymMapper &syn_mapper)
Definition: Seq_loc.cpp:4622
CRangeWithFuzz TRangeWithFuzz
Definition: Seq_loc.cpp:4300
static int s_CompareRanges(const COpenRange< TSeqPos > &x_rng, const COpenRange< TSeqPos > &y_rng, bool minus_strand)
Definition: Seq_loc.cpp:724
static void x_SubNoSort(CSeq_loc &dst, const CSeq_loc &minuend, const TFuzzMap &fuzz_from_plus, const TFuzzMap &fuzz_from_minus, const TFuzzMap &fuzz_to_plus, const TFuzzMap &fuzz_to_minus, TIdToRangeColl &rg_coll_plus, TIdToRangeColl &rg_coll_minus, ISynonymMapper &syn_mapper, ILengthGetter &len_getter, CSeq_loc::TOpFlags flags)
Definition: Seq_loc.cpp:4822
static CRef< CSeq_id > MakeId(const SSeq_loc_CI_RangeInfo &info)
Definition: Seq_loc.cpp:2049
static void s_ReserveMore(Container &cont, size_t add_size)
Definition: Seq_loc.cpp:1431
END_LOCAL_NAMESPACE
Definition: Seq_loc.cpp:802
static void x_MergeAndSort(CSeq_loc &dst, const CSeq_loc &src, CSeq_loc::TOpFlags flags, ISynonymMapper &syn_mapper)
Definition: Seq_loc.cpp:4665
static bool s_CheckIfMatchesBioseq(const CSeq_loc &loc, const CSeq_loc::ISubLocFilter *filter)
Definition: Seq_loc.cpp:709
static void AddLoc(CRef< CSeq_loc > &loc, CRef< CSeq_loc > loc2)
Definition: Seq_loc.cpp:2297
map< CSeq_id_Handle_Wrapper, TRangeColl > TIdToRangeColl
Definition: Seq_loc.cpp:4304
CRangeCollection< TSeqPos > TRangeColl
Definition: Seq_loc.cpp:4303
bool s_CanAdd(const T1 &obj1, const T2 &obj2)
Definition: Seq_loc.cpp:3823
static bool x_MergeRanges(TRangeWithFuzz &rg1, ENa_strand str1, const TRangeWithFuzz &rg2, ENa_strand str2, CSeq_loc::TOpFlags flags)
Definition: Seq_loc.cpp:4358
static CRange< TSeqPos > s_LocToRange(const CSeq_loc &loc)
Definition: Seq_loc.cpp:688
static void x_SingleRange(CSeq_loc &dst, const CSeq_loc &src, ISynonymMapper &syn_mapper)
Definition: Seq_loc.cpp:4521
static void x_PushRange(CSeq_loc &dst, const CSeq_id_Handle_Wrapper &idh, const TRangeWithFuzz &rg, ENa_strand strand)
Definition: Seq_loc.cpp:4417
CLocalRange< TOffset > TRange
define for the fundamental building block of sequence ranges
Definition: base.hpp:115
virtual ~CDummyLengthGetter(void)
Definition: Seq_loc.cpp:5052
CDummyLengthGetter(void)
Definition: Seq_loc.cpp:5051
virtual TSeqPos GetLength(const CSeq_id &)
Definition: Seq_loc.cpp:5054
virtual CSeq_id_Handle GetBestSynonym(const CSeq_id &id)
Definition: Seq_loc.cpp:5041
virtual ~CDummySynonymMapper(void)
Definition: Seq_loc.cpp:5039
void GetLabel(string *label, TSeqPos pos, bool right=true) const
Definition: Int_fuzz.cpp:61
CNcbiOstrstreamToString class helps convert CNcbiOstrstream to a string Sample usage:
Definition: ncbistre.hpp:764
CObject –.
Definition: ncbiobj.hpp:180
void AddPoint(TSeqPos point)
void AddPoints(const TPoints &points)
TRange GetLimits() const
Definition: range_coll.hpp:165
bool IntersectingWith(const TRange &r) const
Definition: range_coll.hpp:187
ENa_strand m_Strand
Definition: Seq_loc.cpp:4257
void AddFuzzFrom(const CRangeWithFuzz &rg)
Definition: Seq_loc.cpp:4046
void AddFuzzFrom(const CInt_fuzz &fuzz, ENa_strand strand)
Definition: Seq_loc.cpp:4057
CConstRef< CInt_fuzz > TFuzz
Definition: Seq_loc.cpp:4024
bool IsSetFuzzFrom(void) const
Definition: Seq_loc.cpp:4040
CRef< CInt_fuzz > x_SetFuzz(TFuzz &fuzz, const CInt_fuzz *copy_from)
Definition: Seq_loc.cpp:4116
CSeq_loc::TRange TParent
Definition: Seq_loc.cpp:4023
const CInt_fuzz & GetFuzzTo(void) const
Definition: Seq_loc.cpp:4043
void x_AddFuzz(TFuzz &fuzz, const TFuzz &other, ENa_strand other_strand)
Definition: Seq_loc.cpp:4132
const CInt_fuzz & GetFuzzFrom(void) const
Definition: Seq_loc.cpp:4042
bool IsSetFuzzTo(void) const
Definition: Seq_loc.cpp:4041
CRangeWithFuzz(const TParent &rg)
Definition: Seq_loc.cpp:4026
void ResetFuzzTo(void)
Definition: Seq_loc.cpp:4039
void AddFuzzTo(const CRangeWithFuzz &rg)
Definition: Seq_loc.cpp:4052
void CopyTo(const CRangeWithFuzz &rg)
Definition: Seq_loc.cpp:4073
TFuzz m_Fuzz_from
Definition: Seq_loc.cpp:4255
CRangeWithFuzz(const CSeq_loc_CI &it)
Definition: Seq_loc.cpp:4030
void ResetFuzzFrom(void)
Definition: Seq_loc.cpp:4038
void AddFuzzTo(const CInt_fuzz &fuzz, ENa_strand strand)
Definition: Seq_loc.cpp:4062
void CopyFrom(const CRangeWithFuzz &rg)
Definition: Seq_loc.cpp:4067
CRange_Less(void)
Definition: Seq_loc.cpp:4310
bool operator()(const TRangeWithFuzz &rg1, const TRangeWithFuzz &rg2) const
Definition: Seq_loc.cpp:4312
bool operator()(const TRangeWithFuzz &rg1, const TRangeWithFuzz &rg2) const
Definition: Seq_loc.cpp:4330
CRange_ReverseLess(void)
Definition: Seq_loc.cpp:4328
Seq-loc exceptions.
Definition: Seq_loc.hpp:74
CSeq_id_Handle_Wrapper(const CSeq_id_Handle &idh, const CSeq_id &id)
Definition: Seq_loc.cpp:4266
const CSeq_id_Handle & GetHandle(void) const
Definition: Seq_loc.cpp:4276
CSeq_id_Handle m_Handle
Definition: Seq_loc.cpp:4295
CConstRef< CSeq_id > GetSeqId(void) const
Definition: Seq_loc.cpp:4274
CConstRef< CSeq_id > m_Id
Definition: Seq_loc.cpp:4296
DECLARE_OPERATOR_BOOL(m_Handle)
Used as a helper for determining which pieces of a CSeq_loc to compare.
Definition: Seq_loc.hpp:261
bool IsInBond(size_t idx) const
Definition: Seq_loc.cpp:1252
TRanges m_Ranges
Definition: Seq_loc.cpp:1388
CRef< CSeq_loc > MakeLocPacked_int(size_t idx_begin, size_t idx_end) const
Definition: Seq_loc.cpp:2284
CSeq_loc_I::EEquivMode m_EquivMode
Definition: Seq_loc.cpp:1396
CRef< CSeq_loc > MakeLoc(CSeq_loc_I::EMakeType make_type) const
Definition: Seq_loc.cpp:2426
void MakeBondB(size_t idx)
Definition: Seq_loc.cpp:1991
void SetHasChanges(void)
Definition: Seq_loc.cpp:1357
pair< size_t, size_t > GetEquivPartRange(size_t idx, size_t level) const
Definition: Seq_loc.cpp:1329
bool IsEnd(size_t idx) const
Definition: Seq_loc.cpp:1178
bool CanBePoint(size_t idx) const
Definition: Seq_loc.cpp:1219
pair< size_t, size_t > GetBondRange(size_t idx) const
Definition: Seq_loc.cpp:1244
void x_AddBondPartB(size_t idx)
Definition: Seq_loc.cpp:1274
void MakeBondAB(size_t idx)
Definition: Seq_loc.cpp:1950
size_t HasEquivBreak(size_t begin, size_t end) const
Definition: Seq_loc.cpp:1600
CSeq_loc_CI_Impl(void)
Definition: Seq_loc.cpp:1400
void RemoveBond(size_t idx)
Definition: Seq_loc.cpp:1903
CRef< CSeq_loc > MakeRangeLoc(const SSeq_loc_CI_RangeInfo &info) const
Definition: Seq_loc.cpp:2191
vector< SSeq_loc_CI_RangeInfo > TRanges
Definition: Seq_loc.cpp:1064
CRef< CSeq_loc > MakeLocOther(const SSeq_loc_CI_RangeInfo &info) const
Definition: Seq_loc.cpp:2167
void x_ProcessLocation(const CSeq_loc &loc)
Definition: Seq_loc.cpp:1439
CConstRef< CSeq_loc > m_Location
Definition: Seq_loc.cpp:1386
CRef< CSeq_loc > MakeLocBond(size_t idx_begin, size_t idx_end) const
Definition: Seq_loc.cpp:2036
vector< SEquivSet > TEquivSets
Definition: Seq_loc.cpp:1173
void x_CreateBond(size_t idx)
Definition: Seq_loc.cpp:1268
bool CanBePoint(const SSeq_loc_CI_RangeInfo &info) const
Definition: Seq_loc.cpp:1828
static void x_SetId(SSeq_loc_CI_RangeInfo &info, const CSeq_id &id)
Definition: Seq_loc.cpp:1422
CRef< CSeq_loc > MakeLocPoint(const SSeq_loc_CI_RangeInfo &info) const
Definition: Seq_loc.cpp:2101
void UpdatePoint(CSeq_point &pnt, const SSeq_loc_CI_RangeInfo &info) const
Definition: Seq_loc.cpp:2065
CSeq_loc_I::EEquivMode GetEquivMode(void) const
Definition: Seq_loc.cpp:1362
bool CanBeBond(size_t idx_begin, size_t idx_end) const
Definition: Seq_loc.cpp:1855
void SetEquivMode(CSeq_loc_I::EEquivMode mode)
Definition: Seq_loc.cpp:1366
bool WasPoint(const SSeq_loc_CI_RangeInfo &info) const
Definition: Seq_loc.cpp:1188
TRanges & GetRanges(void)
Definition: Seq_loc.cpp:1175
void UpdateLoc(SSeq_loc_CI_RangeInfo &info)
Definition: Seq_loc.cpp:2118
const TRanges & GetRanges(void) const
Definition: Seq_loc.cpp:1176
size_t GetBondBegin(size_t idx) const
Definition: Seq_loc.cpp:1881
virtual ~CSeq_loc_CI_Impl(void)
Definition: Seq_loc.cpp:1061
void SetPoint(SSeq_loc_CI_RangeInfo &info)
Definition: Seq_loc.cpp:2085
void x_ProcessInterval(const CSeq_interval &seq_int, const CSeq_loc &loc)
Definition: Seq_loc.cpp:1559
TEquivSets m_EquivSets
Definition: Seq_loc.cpp:1390
void x_ProcessPoint(const CSeq_point &seq_pnt, const CSeq_loc &loc)
Definition: Seq_loc.cpp:1581
void x_BreakBond(size_t idx)
Definition: Seq_loc.cpp:1264
CSeq_loc_CI_Impl & operator=(const CSeq_loc_CI_Impl &)
set< const SEquivSet * > TUsedEquivs
Definition: Seq_loc.cpp:1340
CRef< CSeq_interval > MakeInterval(const SSeq_loc_CI_RangeInfo &info) const
Definition: Seq_loc.cpp:2138
void DeleteRange(size_t idx)
Definition: Seq_loc.cpp:1654
bool CanBePacked_int(size_t idx_begin, size_t idx_end) const
Definition: Seq_loc.cpp:2243
bool CanBeInterval(const SSeq_loc_CI_RangeInfo &info) const
Definition: Seq_loc.cpp:1843
pair< size_t, size_t > GetEquivSetRange(size_t idx, size_t level) const
Definition: Seq_loc.cpp:1324
bool IsBondPartB(size_t idx) const
Definition: Seq_loc.cpp:1260
bool IsInEquivSet(size_t idx) const
Definition: Seq_loc.cpp:1290
const SEquivSet * FindInnerEquivSet(size_t begin, size_t end, const TUsedEquivs &used_equivs) const
Definition: Seq_loc.cpp:1630
CSeq_loc_CI::EEmptyFlag m_EmptyFlag
Definition: Seq_loc.cpp:1392
CSeq_loc_CI_Impl(const CSeq_loc_CI_Impl &)
void MakeBondA(size_t idx)
Definition: Seq_loc.cpp:1919
bool CanBePacked_pnt(size_t idx_begin, size_t idx_end) const
Definition: Seq_loc.cpp:2205
bool HasEquivSets(void) const
Definition: Seq_loc.cpp:1286
SSeq_loc_CI_RangeInfo::TRange TRange
Definition: Seq_loc.cpp:1063
const SEquivSet & GetEquivSet(size_t idx, size_t level) const
Definition: Seq_loc.cpp:1309
bool ShouldBePoint(const SSeq_loc_CI_RangeInfo &info) const
Definition: Seq_loc.cpp:1192
bool IsBondPartA(size_t idx) const
Definition: Seq_loc.cpp:1256
CRef< CSeq_point > MakePoint(const SSeq_loc_CI_RangeInfo &info) const
Definition: Seq_loc.cpp:2092
bool IsInBond(const SSeq_loc_CI_RangeInfo &info) const
Definition: Seq_loc.cpp:1248
CRef< CInt_fuzz > MakeFuzz(const CInt_fuzz &fuzz) const
Definition: Seq_loc.cpp:2059
CRef< CSeq_loc > MakeLocPacked_pnt(size_t idx_begin, size_t idx_end) const
Definition: Seq_loc.cpp:2262
SSeq_loc_CI_RangeInfo & InsertRange(size_t idx, CSeq_loc::E_Choice type)
Definition: Seq_loc.cpp:1693
size_t GetBondEnd(size_t idx) const
Definition: Seq_loc.cpp:1892
bool CanBeInterval(size_t idx) const
Definition: Seq_loc.cpp:1223
CRef< CSeq_loc > MakeLocInterval(const SSeq_loc_CI_RangeInfo &info) const
Definition: Seq_loc.cpp:2158
size_t GetEquivSetsCount(size_t idx) const
Definition: Seq_loc.cpp:1299
bool HasChanges(void) const
Definition: Seq_loc.cpp:1352
Seq-loc iterator class – iterates all intervals from a seq-loc in the correct order.
Definition: Seq_loc.hpp:593
void AddSeqLoc(const CSeq_loc &other)
Base class for all serializable objects.
Definition: serialbase.hpp:150
Interface for getting bioseq length.
Definition: Seq_loc.hpp:420
Interface for mapping IDs to the best synonym.
Definition: Seq_loc.hpp:408
container_type::const_iterator const_iterator
Definition: map.hpp:53
const_iterator end() const
Definition: map.hpp:152
const_iterator find(const key_type &key) const
Definition: map.hpp:153
Definition: map.hpp:338
Definition: set.hpp:45
iterator_bool insert(const value_type &val)
Definition: set.hpp:149
const_iterator find(const key_type &key) const
Definition: set.hpp:137
const_iterator end() const
Definition: set.hpp:136
static uch flags
bool operator<(const CEquivRange &A, const CEquivRange &B)
bool operator==(const CEquivRange &A, const CEquivRange &B)
#define false
Definition: bool.h:36
static DLIST_TYPE *DLIST_NAME() first(DLIST_LIST_TYPE *list)
Definition: dlist.tmpl.h:46
static char tmp[3200]
Definition: utf8.c:42
int offset
Definition: replacements.h:160
char data[12]
Definition: iconv.c:80
unsigned int TSeqPos
Type for sequence locations and lengths.
Definition: ncbimisc.hpp:974
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:914
#define ERASE_ITERATE(Type, Var, Cont)
Non-constant version with ability to erase current element, if container permits.
Definition: ncbimisc.hpp:942
#define VECTOR_ERASE(Var, Cont)
Use this macro inside body of ERASE_ITERATE cycle to erase from vector-like container.
Definition: ncbimisc.hpp:951
#define NON_CONST_ITERATE(Type, Var, Cont)
Non constant version of ITERATE macro.
Definition: ncbimisc.hpp:921
const TSeqPos kInvalidSeqPos
Define special value for invalid sequence position.
Definition: ncbimisc.hpp:977
#define INT_ID_TO(T, id)
Convert gi-compatible int to/from other types.
Definition: ncbimisc.hpp:1219
#define NULL
Definition: ncbistd.hpp:225
#define ERR_POST_X(err_subcode, message)
Error posting with default error code and given error subcode.
Definition: ncbidiag.hpp:548
void Error(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1197
TErrCode GetErrCode(void) const
Get error code.
Definition: ncbiexpt.cpp:453
#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
#define NCBI_THROW_FMT(exception_class, err_code, message)
The same as NCBI_THROW but with message processed as output to ostream.
Definition: ncbiexpt.hpp:719
virtual const char * GetErrCodeString(void) const
Get error code interpreted as text.
Definition: ncbiexpt.cpp:444
const float pi
Definition: math.hpp:54
ESerialRecursionMode
How to assign and compare child sub-objects of serial objects.
Definition: serialdef.hpp:191
virtual void Assign(const CSerialObject &source, ESerialRecursionMode how=eRecursive)
Set object to copy of another one.
virtual const CTypeInfo * GetThisTypeInfo(void) const =0
C * SerialClone(const C &src)
Create on heap a clone of the source object.
Definition: serialbase.hpp:515
virtual bool Equals(const CSerialObject &object, ESerialRecursionMode how=eRecursive) const
Check if both objects contain the same values.
@ eShallow
Assign/Compare pointers only.
Definition: serialdef.hpp:193
CConstRef< CSeq_id > GetSeqId(void) const
int CompareOrdered(const CSeq_id &sid2) const
Definition: Seq_id.cpp:622
bool Match(const CSeq_id &sid2) const
Match() - TRUE if SeqIds are equivalent.
Definition: Seq_id.hpp:1136
static CSeq_id_Handle GetHandle(const CSeq_id &id)
Normal way of getting a handle, works for any seq-id.
void SetRange(const TRange &range)
Set the range.
Definition: Seq_loc.cpp:2807
bool IsBondA(void) const
Return true if current position is A part of a bond.
Definition: Seq_loc.cpp:2503
CRef< CSeq_loc > MakeSeq_loc(EMakeType make_type=eMake_CompactType) const
return constructed CSeq_loc with all changes
Definition: Seq_loc.cpp:2946
void SetTruncatedStart(bool val, ESeqLocExtremes ext)
set / remove e_Lim fuzz on start or stop (tl/tr - indicating removed parts of the seq-loc)
Definition: Seq_loc.cpp:3422
void SetPacked_int(TPacked_int &v)
Definition: Seq_loc.hpp:984
size_t GetEquivSetsCount(void) const
Return number of recursuve equiv parts current position in.
Definition: Seq_loc.cpp:2540
virtual const char * x_GetIteratorType(void) const
Definition: Seq_loc.cpp:2685
void SetFrom(TSeqPos from)
Set the range from position.
Definition: Seq_loc.cpp:2818
void ResetFuzz(void)
Reset fuzz of a point.
Definition: Seq_loc.cpp:2923
void SetMix(TMix &v)
Definition: Seq_loc.hpp:987
void SetWhole(TWhole &v)
Definition: Seq_loc.hpp:982
bool x_IsValid(void) const
Definition: Seq_loc.cpp:2609
CSeq_loc_CI(void)
constructors
Definition: Seq_loc.cpp:2433
void SetTo(TSeqPos to)
Set the range to position.
Definition: Seq_loc.cpp:2829
CSeq_loc_I InsertPoint(const CSeq_id_Handle &id, TSeqPos pos, ENa_strand strand=eNa_strand_unknown)
Insert new element before the current one (.
Definition: Seq_loc.cpp:2760
void SetPacked_pnt(TPacked_pnt &v)
Definition: Seq_loc.hpp:986
bool IsPartialStart(ESeqLocExtremes ext) const
check start or stop of location for e_Lim fuzz
Definition: Seq_loc.cpp:3246
void Delete(void)
Delete current element, and make iterator to point to the next element.
Definition: Seq_loc.cpp:2724
virtual ~CSeq_loc(void)
Definition: Seq_loc.cpp:186
void x_ChangeToPackedPnt(const CSeq_loc &other)
Definition: Seq_loc.cpp:3796
ENa_strand GetStrand(void) const
Get the location's strand.
Definition: Seq_loc.cpp:882
void x_CheckValidForInsert(const char *where) const
Definition: Seq_loc.hpp:1112
void ChangeToPackedInt(void)
Works only if location is currently an interval, point, packed-int (handled trivially),...
Definition: Seq_loc.cpp:3694
bool x_UpdateId(const CSeq_id *&total_id, const CSeq_id *id, bool may_throw=true) const
Definition: Seq_loc.cpp:403
CSeq_loc_CI & operator=(const CSeq_loc_CI &iter)
Definition: Seq_loc.cpp:2469
EEmptyFlag
Options for empty locations processing.
Definition: Seq_loc.hpp:456
void FlipStrand(void)
Flip the strand (e.g. plus to minus)
Definition: Seq_loc.cpp:3993
void MakeBondA(void)
Make bond at current position (only A) The current part must be a point If current posision is alread...
Definition: Seq_loc.cpp:2979
virtual void Assign(const CSerialObject &source, ESerialRecursionMode how=eRecursive)
Override Assign() to incorporate cache invalidation.
Definition: Seq_loc.cpp:337
void SetId(CSeq_id &id)
set the 'id' field in all parts of this location
Definition: Seq_loc.cpp:3498
bool IsInEquivSet(void) const
Return true if current position is in some equiv part.
Definition: Seq_loc.cpp:2533
CSeq_loc_I(void)
constructors
Definition: Seq_loc.cpp:2656
void ResetStrand(void)
Reset the range strand.
Definition: Seq_loc.cpp:2856
void ResetFuzzTo(void)
Reset fuzz to.
Definition: Seq_loc.cpp:2901
EMakeType
Options for creation modified locations Bond and equiv types are preserved if possible.
Definition: Seq_loc.hpp:597
void ResetFuzzFrom(void)
Reset fuzz from.
Definition: Seq_loc.cpp:2879
CSeq_loc_CI const_iterator
Make CSeq_loc look like an STL container.
Definition: Seq_loc.hpp:367
virtual CSeq_id_Handle GetBestSynonym(const CSeq_id &id)=0
TSeqPos GetStart(ESeqLocExtremes ext) const
Return start and stop positions of the seq-loc.
Definition: Seq_loc.cpp:915
pair< CSeq_loc_CI, CSeq_loc_CI > GetBondRange(void) const
Return iterators that cover bond of current position result.first is the first segment in the equiv s...
Definition: Seq_loc.cpp:2518
CConstRef< CSeq_loc > GetRangeAsSeq_loc(void) const
Get seq-loc for the current iterator position.
Definition: Seq_loc.cpp:2585
void MakeBondB(void)
Make bond at previous position with the current position (A and B) The current and previous parts mus...
Definition: Seq_loc.cpp:2993
pair< CConstRef< CInt_fuzz >, CConstRef< CInt_fuzz > > m_Fuzz
Definition: Seq_loc.hpp:444
CRef< CSeq_loc > Merge(TOpFlags flags, ISynonymMapper *syn_mapper) const
All functions create and return a new seq-loc object.
Definition: Seq_loc.cpp:5061
void SetStrand(ENa_strand strand)
Set the range strand.
Definition: Seq_loc.cpp:2868
void SetPnt(TPnt &v)
Definition: Seq_loc.hpp:985
const CSeq_loc & GetSeq_loc(void) const
Definition: Seq_loc.cpp:2567
void MakeBondAB(void)
Make bond at current position with the next position (A and B) The current and next parts must be poi...
Definition: Seq_loc.cpp:2986
bool HasEquivSets(void) const
Return true if location has equiv parts.
Definition: Seq_loc.cpp:2527
TRange x_CalculateTotalRangeCheckId(const CSeq_id *&id) const
Definition: Seq_loc.cpp:429
const_iterator end(void) const
Definition: Seq_loc.cpp:1034
const_iterator begin(void) const
Definition: Seq_loc.cpp:1028
void x_ChangeToMix(const CSeq_loc &other)
Definition: Seq_loc.cpp:3765
void SetEmpty(TEmpty &v)
Definition: Seq_loc.hpp:981
bool HasChanges(void) const
return true of any part was changed since initialization
Definition: Seq_loc.cpp:2706
void SetInt(TInt &v)
Definition: Seq_loc.hpp:983
int Compare(const CSeq_loc &loc) const
bool IsTruncatedStop(ESeqLocExtremes ext) const
Definition: Seq_loc.cpp:3396
void x_ChangeToPackedInt(const CSeq_interval &other)
Definition: Seq_loc.cpp:3772
int CompareSubLoc(const CSeq_loc &loc, ENa_strand strand, const ISubLocFilter *filter=NULL) const
Compare first-level sub-locations sequentially to order them by biological "complexity".
Definition: Seq_loc.cpp:805
bool x_IsValidForInsert(void) const
Definition: Seq_loc.cpp:2679
void SetSeq_id_Handle(const CSeq_id_Handle &id)
Set seq_id of the current location.
Definition: Seq_loc.cpp:2796
void SetFuzz(CInt_fuzz &fuzz)
Change fuzz of a point.
Definition: Seq_loc.cpp:2934
void x_SetSeq_id_Handle(SSeq_loc_CI_RangeInfo &info, const CSeq_id_Handle &id)
Definition: Seq_loc.cpp:2698
void SetFuzzTo(CInt_fuzz &fuzz)
Change fuzz to values.
Definition: Seq_loc.cpp:2912
int TCompareFlags
Definition: Seq_loc.hpp:248
bool IsInBond(void) const
Location of type equiv define set of equivalent locations.
Definition: Seq_loc.cpp:2496
void Add(const CSeq_loc &other)
Simple adding of seq-locs.
Definition: Seq_loc.cpp:3899
void SetPoint(TSeqPos pos)
Set the range from and to positions.
Definition: Seq_loc.cpp:2840
CRef< CSeq_loc > Subtract(const CSeq_loc &other, TOpFlags flags, ISynonymMapper *syn_mapper, ILengthGetter *len_getter) const
Subtract seq-loc from this, merge/sort resulting ranges depending on flags.
Definition: Seq_loc.cpp:5111
bool operator==(const CSeq_loc_CI &iter) const
Definition: Seq_loc.cpp:2477
bool IsTruncatedStart(ESeqLocExtremes ext) const
check if parts of the seq-loc are missing
Definition: Seq_loc.cpp:3370
bool IsSetStrand(EIsSetStrand flag=eIsSetStrand_Any) const
Check if strand is set for any/all part(s) of the seq-loc depending on the flag.
Definition: Seq_loc.cpp:858
ENa_strand m_Strand
Definition: Seq_loc.hpp:442
TSeqPos GetCircularLength(TSeqPos seq_len) const
Special case for circular sequences.
Definition: Seq_loc.cpp:1011
const CSeq_loc & GetEmbeddingSeq_loc(void) const
Get the nearest seq-loc containing the current range.
Definition: Seq_loc.cpp:2573
CSeq_loc_I InsertInterval(const CSeq_id_Handle &id, const TRange &range, ENa_strand strand=eNa_strand_unknown)
Insert new element before the current one (.
Definition: Seq_loc.cpp:2777
pair< CSeq_loc_CI, CSeq_loc_CI > GetEquivSetRange(size_t level=0) const
Return iterators that cover equiv set of current position result.first is the first segment in the eq...
Definition: Seq_loc.cpp:2548
void SetPos(size_t pos)
Set iterator's position.
Definition: Seq_loc.cpp:2642
pair< CSeq_loc_I, CSeq_loc_I > GetEquivPartRange(size_t level=0) const
Return iterators that cover equiv part of current position result.first is the first segment in the e...
Definition: Seq_loc.cpp:2963
const CSeq_id * GetId(void) const
Get the id of the location return NULL if has multiple ids or no id at all.
Definition: Seq_loc.hpp:941
void PostRead(void) const
Invalidate id/range cache after deserialization.
Definition: Seq_loc.cpp:676
size_t GetSize(void) const
Get number of ranges.
Definition: Seq_loc.cpp:2636
CSeq_id_Handle m_IdHandle
Definition: Seq_loc.hpp:438
bool IsBondB(void) const
Return true if current position is B part of a bond.
Definition: Seq_loc.cpp:2510
void ResetStrand(void)
Reset the strand on this location.
Definition: Seq_loc.cpp:5245
void SetPartialStart(bool val, ESeqLocExtremes ext)
set / remove e_Lim fuzz on start or stop (lt/gt - indicating partial interval)
Definition: Seq_loc.cpp:3304
virtual TSeqPos GetLength(const CSeq_id &id)=0
void x_ThrowNotValid(const char *where) const
Definition: Seq_loc.cpp:2615
virtual const char * x_GetIteratorType(void) const
Definition: Seq_loc.cpp:2623
void SetEquiv(TEquiv &v)
Definition: Seq_loc.hpp:988
pair< CSeq_loc_CI, CSeq_loc_CI > GetEquivPartRange(size_t level=0) const
Return iterators that cover equiv part of current position result.first is the first segment in the e...
Definition: Seq_loc.cpp:2558
CSeq_loc_I InsertWhole(const CSeq_id_Handle &id)
Insert new element before the current one (.
Definition: Seq_loc.cpp:2749
const SSeq_loc_CI_RangeInfo & x_GetRangeInfo(void) const
Definition: Seq_loc.cpp:2629
void SetFuzzFrom(CInt_fuzz &fuzz)
Change fuzz from.
Definition: Seq_loc.cpp:2890
void RemoveBond(void)
Remove bond at current position - it may be either A or B part.
Definition: Seq_loc.cpp:2972
CRef< CSeq_loc > Intersect(const CSeq_loc &other, TOpFlags flags, ISynonymMapper *syn_mapper) const
Find the intersection with the seq-loc, merge/sort resulting ranges depending on flags.
Definition: Seq_loc.cpp:5207
bool operator!=(const CSeq_loc_CI &iter) const
Definition: Seq_loc.cpp:2490
EEquivMode
This enum defines a way equiv sets are expanded or created when one of Insert*() methods is called.
Definition: Seq_loc.hpp:760
CSeq_loc_I InsertEmpty(const CSeq_id_Handle &id)
Insert new element before the current one (.
Definition: Seq_loc.cpp:2739
pair< CSeq_loc_I, CSeq_loc_I > GetEquivSetRange(size_t level=0) const
Return iterators that cover equiv set of current position result.first is the first segment in the eq...
Definition: Seq_loc.cpp:2953
void SetBond(TBond &v)
Definition: Seq_loc.hpp:989
int TOpFlags
Definition: Seq_loc.hpp:336
TRange x_UpdateTotalRange(void) const
Definition: Seq_loc.cpp:385
void GetLabel(string *label) const
Appends a label suitable for display (e.g., error messages) label must point to an existing string ob...
Definition: Seq_loc.cpp:3491
void SetStrand(ENa_strand strand)
Set the strand for all of the location's ranges.
Definition: Seq_loc.cpp:5220
void SetEquivMode(EEquivMode mode)
Change equiv modification mode.
Definition: Seq_loc.cpp:2718
void SetPartialStop(bool val, ESeqLocExtremes ext)
Definition: Seq_loc.cpp:3337
CSeq_loc(void)
constructors
Definition: Seq_loc.hpp:906
void ChangeToMix(void)
Definition: Seq_loc.cpp:3657
int x_CompareSingleId(const CSeq_loc &loc, const CSeq_id *id1, const CSeq_id *id2, TCompareFlags flags) const
Compare single-id locations, or throw an exception if any location is multi-id.
Definition: Seq_loc.cpp:540
SSeq_loc_CI_RangeInfo & x_GetRangeInfo(void)
Definition: Seq_loc.cpp:2691
virtual ~CSeq_loc_I(void)
destructor
Definition: Seq_loc.cpp:2674
EEquivMode GetEquivMode(void) const
Get equiv modification mode.
Definition: Seq_loc.cpp:2712
void SetNull(void)
Override all setters to incorporate cache invalidation.
Definition: Seq_loc.hpp:960
bool IsPartialStop(ESeqLocExtremes ext) const
Definition: Seq_loc.cpp:3275
void SetTruncatedStop(bool val, ESeqLocExtremes ext)
Definition: Seq_loc.cpp:3455
virtual ~CSeq_loc_CI(void)
destructor
Definition: Seq_loc.cpp:2457
CSeq_loc_I InsertNull(void)
Set of Insert*() methods.
Definition: Seq_loc.cpp:2731
CConstRef< CSeq_loc > m_Loc
Definition: Seq_loc.hpp:443
bool x_CheckId(const CSeq_id *&id, bool may_throw=true) const
Definition: Seq_loc.cpp:3565
TSeqPos GetStop(ESeqLocExtremes ext) const
Definition: Seq_loc.cpp:963
virtual const char * GetErrCodeString(void) const override
Get error code interpreted as text.
Definition: Seq_loc.cpp:65
@ eOrder_Positional
Definition: Seq_loc.hpp:461
@ eEmpty_Allow
ignore empty locations
Definition: Seq_loc.hpp:458
@ eMake_CompactType
Definition: Seq_loc.hpp:598
@ eMake_PreserveType
use most compact Seq-loc type (default)
Definition: Seq_loc.hpp:599
@ fStrand_Ignore
Definition: Seq_loc.hpp:325
@ fMerge_SingleRange
Definition: Seq_loc.hpp:332
@ fMerge_OverlappingOnly
Definition: Seq_loc.hpp:329
@ fMerge_AbuttingOnly
Definition: Seq_loc.hpp:327
@ fMerge_Contained
Definition: Seq_loc.hpp:326
@ eEquiv_prepend
If the insertion point is just before any equiv part, including the first one in an equiv,...
Definition: Seq_loc.hpp:804
@ eEquiv_new_part
New equiv part will be started with the inserted element.
Definition: Seq_loc.hpp:788
@ eEquiv_new_equiv
A new equiv set will be created, even if the insertion point is already inside of an existing equiv,...
Definition: Seq_loc.hpp:771
@ eEquiv_append
If the insertion point is just after any equiv part, including the last one in an equiv,...
Definition: Seq_loc.hpp:796
@ eMultipleId
Seq-loc on multiple ids when one id is required.
Definition: Seq_loc.hpp:78
@ eNotSet
Seq-loc is not set.
Definition: Seq_loc.hpp:77
@ eIncomatible
Seq-loc type is incompatible with operation.
Definition: Seq_loc.hpp:82
@ eBadLocation
Seq-loc is incorrectly formed.
Definition: Seq_loc.hpp:80
@ eOutOfRange
parameter is out of valid range
Definition: Seq_loc.hpp:83
@ eBadIterator
Seq-loc iterator is in bad state.
Definition: Seq_loc.hpp:81
@ eUnsupported
Seq-loc has data that is not supported yet.
Definition: Seq_loc.hpp:79
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.
const CSeq_id & GetId(const CSeq_loc &loc, CScope *scope)
If all CSeq_ids embedded in CSeq_loc refer to the same CBioseq, returns the first CSeq_id found,...
ENa_strand GetStrand(const CSeq_loc &loc, CScope *scope=0)
Returns eNa_strand_unknown if multiple Bioseqs in loc Returns eNa_strand_other if multiple strands in...
int TCompareFlags
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.
sequence::ECompare Compare(const CSeq_loc &loc1, const CSeq_loc &loc2, CScope *scope)
Returns the sequence::ECompare containment relationship between CSeq_locs.
CConstRef< C > ConstRef(const C *object)
Template function for conversion of const object pointer to CConstRef.
Definition: ncbiobj.hpp:1875
TObjectType * GetPointerOrNull(void) const noexcept
Get pointer value.
Definition: ncbiobj.hpp:1664
CRef< C > Ref(C *object)
Helper functions to get CRef<> and CConstRef<> objects.
Definition: ncbiobj.hpp:1866
void Reset(void) noexcept
Reset reference object.
Definition: ncbiobj.hpp:1431
TObjectType * Release(void)
Release a reference to the object and return a pointer to the object.
Definition: ncbiobj.hpp:842
position_type GetLength(void) const
Definition: range.hpp:158
position_type GetTo(void) const
Definition: range.hpp:142
bool IntersectingWith(const TThisType &r) const
Definition: range.hpp:331
position_type GetToOpen(void) const
Definition: range.hpp:138
position_type GetFrom(void) const
Definition: range.hpp:134
static TThisType GetEmpty(void)
Definition: range.hpp:306
TThisType & operator+=(const TThisType &r)
Definition: range.hpp:371
TThisType & Set(position_type from, position_type to)
Definition: range.hpp:188
bool Empty(void) const
Definition: range.hpp:148
TThisType & SetOpen(position_type from, position_type toOpen)
Definition: range.hpp:184
bool IsWhole(void) const
Definition: range.hpp:284
static position_type GetWholeToOpen(void)
Definition: range.hpp:260
static TThisType GetWhole(void)
Definition: range.hpp:272
TParent::position_type position_type
Definition: range.hpp:391
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:104
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:101
bool operator!=(const CNCBI_IPAddr &lhs, unsigned int rhs)
Compare an address with an IPv4 address in network byte order.
static string IntToString(int value, TNumToStringFlags flags=0, int base=10)
Convert int to string.
Definition: ncbistr.hpp:5057
static const char label[]
void SetFrom(TFrom value)
Assign a value to From data member.
Definition: Range_.hpp:231
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
void SetTo(TTo value)
Assign a value to To data member.
Definition: Range_.hpp:278
void SetMin(TMin value)
Assign a value to Min data member.
Definition: Int_fuzz_.hpp:528
bool IsLim(void) const
Check if variant Lim is selected.
Definition: Int_fuzz_.hpp:636
TRange & SetRange(void)
Select the variant.
Definition: Int_fuzz_.cpp:165
TAlt & SetAlt(void)
Select the variant.
Definition: Int_fuzz_.hpp:676
TPct GetPct(void) const
Get the variant data.
Definition: Int_fuzz_.hpp:615
virtual void Reset(void)
Reset the whole object.
Definition: Int_fuzz_.cpp:95
TLim GetLim(void) const
Get the variant data.
Definition: Int_fuzz_.hpp:642
TP_m & SetP_m(void)
Select the variant.
Definition: Int_fuzz_.hpp:589
ELim
some limit value
Definition: Int_fuzz_.hpp:209
TMin GetMin(void) const
Get the Min member data.
Definition: Int_fuzz_.hpp:519
void SetMax(TMax value)
Assign a value to Max data member.
Definition: Int_fuzz_.hpp:481
E_Choice Which(void) const
Which variant is currently selected.
Definition: Int_fuzz_.hpp:547
TLim & SetLim(void)
Select the variant.
Definition: Int_fuzz_.hpp:649
const TAlt & GetAlt(void) const
Get the variant data.
Definition: Int_fuzz_.hpp:669
TPct & SetPct(void)
Select the variant.
Definition: Int_fuzz_.hpp:622
TMax GetMax(void) const
Get the Max member data.
Definition: Int_fuzz_.hpp:472
TP_m GetP_m(void) const
Get the variant data.
Definition: Int_fuzz_.hpp:582
const TRange & GetRange(void) const
Get the variant data.
Definition: Int_fuzz_.cpp:159
@ eLim_gt
greater than
Definition: Int_fuzz_.hpp:211
@ eLim_unk
unknown
Definition: Int_fuzz_.hpp:210
@ eLim_lt
less than
Definition: Int_fuzz_.hpp:212
@ eLim_tl
space to left of position
Definition: Int_fuzz_.hpp:214
@ eLim_tr
space to right of position
Definition: Int_fuzz_.hpp:213
@ e_not_set
No variant selected.
Definition: Int_fuzz_.hpp:225
@ e_Pct
% plus or minus (x10) 0-1000
Definition: Int_fuzz_.hpp:228
@ e_Alt
set of alternatives for the integer
Definition: Int_fuzz_.hpp:230
@ e_P_m
plus or minus fixed amount
Definition: Int_fuzz_.hpp:226
void SetTo(TTo value)
Assign a value to To data member.
const TFuzz_from & GetFuzz_from(void) const
Get the Fuzz_from member data.
void SetPoint(TPoint value)
Assign a value to Point data member.
Definition: Seq_point_.hpp:312
void SetId(TId &value)
Assign a value to Id data member.
Definition: Seq_point_.cpp:61
void SetA(TA &value)
Assign a value to A data member.
Definition: Seq_bond_.cpp:60
bool IsSetId(void) const
WARNING: this used to be optional Check if a value has been assigned to Id data member.
Definition: Seq_point_.hpp:378
void ResetFuzz_to(void)
Reset Fuzz_to data member.
bool IsMix(void) const
Check if variant Mix is selected.
Definition: Seq_loc_.hpp:552
const TB & GetB(void) const
Get the B member data.
Definition: Seq_bond_.hpp:243
list< CRef< CSeq_interval > > Tdata
ENa_strand
strand of nucleic acid
Definition: Na_strand_.hpp:64
const Tdata & Get(void) const
Get the member data.
TStrand GetStrand(void) const
Get the Strand member data.
const TId & GetId(void) const
Get the Id member data.
void ResetStrand(void)
Reset Strand data member.
bool IsPacked_pnt(void) const
Check if variant Packed_pnt is selected.
Definition: Seq_loc_.hpp:546
Tdata & Set(void)
Assign a value to data member.
TPoint GetPoint(void) const
Get the Point member data.
Definition: Seq_point_.hpp:303
const TFuzz_to & GetFuzz_to(void) const
Get the Fuzz_to member data.
void SetId(TId &value)
Assign a value to Id data member.
void SetStrand(TStrand value)
Assign a value to Strand data member.
Definition: Seq_point_.hpp:359
bool IsSetStrand(void) const
Check if a value has been assigned to Strand data member.
TFrom GetFrom(void) const
Get the From member data.
void SetFuzz(TFuzz &value)
Assign a value to Fuzz data member.
Definition: Seq_point_.cpp:71
bool IsSetFuzz(void) const
Check if a value has been assigned to Fuzz data member.
Definition: Seq_point_.hpp:408
list< CRef< CSeq_loc > > Tdata
const TFuzz & GetFuzz(void) const
Get the Fuzz member data.
Definition: Seq_point_.hpp:420
void ResetFuzz(void)
Reset Fuzz data member.
Definition: Seq_point_.cpp:66
bool CanGetA(void) const
Check if it is safe to call GetA method.
Definition: Seq_bond_.hpp:207
E_Choice Which(void) const
Which variant is currently selected.
Definition: Seq_loc_.hpp:475
const TId & GetId(void) const
Get the Id member data.
const TId & GetId(void) const
Get the Id member data.
Definition: Seq_point_.hpp:390
void SetFrom(TFrom value)
Assign a value to From data member.
TStrand GetStrand(void) const
Get the Strand member data.
Definition: Seq_point_.hpp:350
void SetStrand(TStrand value)
Assign a value to Strand data member.
bool IsSetFuzz(void) const
Check if a value has been assigned to Fuzz data member.
const Tdata & Get(void) const
Get the member data.
void ResetFuzz(void)
Reset Fuzz data member.
void SetFuzz(TFuzz &value)
Assign a value to Fuzz data member.
void ResetStrand(void)
Reset Strand data member.
Definition: Seq_point_.hpp:343
void SetId(TId &value)
Assign a value to Id data member.
const Tdata & Get(void) const
Get the member data.
void ResetFuzz_from(void)
Reset Fuzz_from data member.
TPoints & SetPoints(void)
Assign a value to Points data member.
list< CRef< CSeq_loc > > Tdata
vector< TSeqPos > TPoints
void SetFuzz_to(TFuzz_to &value)
Assign a value to Fuzz_to data member.
void SetFuzz_from(TFuzz_from &value)
Assign a value to Fuzz_from data member.
bool IsPacked_int(void) const
Check if variant Packed_int is selected.
Definition: Seq_loc_.hpp:534
const TA & GetA(void) const
Get the A member data.
Definition: Seq_bond_.hpp:213
bool CanGetB(void) const
Check if it is safe to call GetB method.
Definition: Seq_bond_.hpp:237
bool IsSetStrand(void) const
Check if a value has been assigned to Strand data member.
Tdata & Set(void)
Assign a value to data member.
bool IsSetFuzz_to(void) const
Check if a value has been assigned to Fuzz_to data member.
Tdata & Set(void)
Assign a value to data member.
bool IsSetStrand(void) const
Check if a value has been assigned to Strand data member.
Definition: Seq_point_.hpp:331
const TPoints & GetPoints(void) const
Get the Points member data.
TStrand GetStrand(void) const
Get the Strand member data.
TTo GetTo(void) const
Get the To member data.
void SetB(TB &value)
Assign a value to B data member.
Definition: Seq_bond_.cpp:70
bool IsSetFuzz_from(void) const
Check if a value has been assigned to Fuzz_from data member.
void SetStrand(TStrand value)
Assign a value to Strand data member.
void ResetStrand(void)
Reset Strand data member.
E_Choice
Choice variants.
Definition: Seq_loc_.hpp:96
const TFuzz & GetFuzz(void) const
Get the Fuzz member data.
const TMix & GetMix(void) const
Get the variant data.
Definition: Seq_loc_.cpp:282
void ResetB(void)
Reset B data member.
Definition: Seq_bond_.cpp:65
bool IsSetB(void) const
other end may not be available Check if a value has been assigned to B data member.
Definition: Seq_bond_.hpp:231
@ eNa_strand_plus
Definition: Na_strand_.hpp:66
@ eNa_strand_minus
Definition: Na_strand_.hpp:67
@ eNa_strand_unknown
Definition: Na_strand_.hpp:65
@ eNa_strand_both
in forward orientation
Definition: Na_strand_.hpp:68
@ e_not_set
No variant selected.
Definition: Seq_id_.hpp:94
@ e_not_set
No variant selected.
Definition: Seq_loc_.hpp:97
@ e_Null
not placed
Definition: Seq_loc_.hpp:98
@ e_Equiv
equivalent sets of locations
Definition: Seq_loc_.hpp:106
@ e_Empty
to NULL one Seq-id in a collection
Definition: Seq_loc_.hpp:99
@ e_Feat
indirect, through a Seq-feat
Definition: Seq_loc_.hpp:108
@ e_Int
from to
Definition: Seq_loc_.hpp:101
@ e_Whole
whole sequence
Definition: Seq_loc_.hpp:100
Definition of all error codes used in objects libraries.
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
@ e_not_set
int i
CNcbiMatrix< T > & operator+=(CNcbiMatrix< T > &, const CNcbiMatrix< U > &)
global addition: matrix += matrix
Definition: matrix.hpp:570
static MDB_envinfo info
Definition: mdb_load.c:37
const TYPE & Get(const CNamedParameterList *param)
constexpr auto sort(_Init &&init)
constexpr bool empty(list< Ts... >) noexcept
mdb_mode_t mode
Definition: lmdb++.h:62
const struct ncbi::grid::netcache::search::fields::SIZE size
ncbi::objects::ENa_strand TStrand
T max(T x_, T y_)
T minus(T x_)
T min(T x_, T y_)
void copy(Njn::Matrix< S > *matrix_, const Njn::Matrix< T > &matrix0_)
Definition: njn_matrix.hpp:613
#define count
@ eNotSet
Definition: splign_app.cpp:550
bool operator()(const SEquivSet *e1, const SEquivSet *e2) const
Definition: Seq_loc.cpp:1149
size_t GetPartStartIndex(TPart_CI part) const
Definition: Seq_loc.cpp:1123
size_t GetPartEndOffset(TPart_CI part) const
Definition: Seq_loc.cpp:1116
size_t GetStartIndex(void) const
Definition: Seq_loc.cpp:1086
size_t GetElementsCount(void) const
Definition: Seq_loc.cpp:1079
vector< size_t > TParts
Definition: Seq_loc.cpp:1072
size_t GetPartsCount(void) const
Definition: Seq_loc.cpp:1103
TPart_CI FindPartByElementIndex(size_t idx) const
Definition: Seq_loc.cpp:1141
size_t GetPartStartOffset(TPart_CI part) const
Definition: Seq_loc.cpp:1110
bool Contains(size_t idx) const
Definition: Seq_loc.cpp:1097
size_t GetPartEndIndex(TPart_CI part) const
Definition: Seq_loc.cpp:1128
size_t GetEndIndex(void) const
Definition: Seq_loc.cpp:1091
TPart_I FindPartByElementIndex(size_t idx)
Definition: Seq_loc.cpp:1134
TParts::const_iterator TPart_CI
Definition: Seq_loc.cpp:1073
TParts::iterator TPart_I
Definition: Seq_loc.cpp:1074
Definition: type.c:6
#define _ASSERT
Modified on Sun Nov 08 14:03:15 2026 by modify_doxy.py rev. 717554