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

Go to the SVN repository for this file.

1 #ifndef OBJTOOLS_ALNMGR__ALN_RNG_COLL_OPER__HPP
2 #define OBJTOOLS_ALNMGR__ALN_RNG_COLL_OPER__HPP
3 /* $Id: aln_rng_coll_oper.hpp 88953 2020-02-05 20:27:33Z vasilche $
4 * ===========================================================================
5 *
6 * PUBLIC DOMAIN NOTICE
7 * National Center for Biotechnology Information
8 *
9 * This software/database is a "United States Government Work" under the
10 * terms of the United States Copyright Act. It was written as part of
11 * the author's official duties as a United States Government employee and
12 * thus cannot be copyrighted. This software/database is freely available
13 * to the public for use. The National Library of Medicine and the U.S.
14 * Government have not placed any restriction on its use or reproduction.
15 *
16 * Although all reasonable efforts have been taken to ensure the accuracy
17 * and reliability of the software and data, the NLM and the U.S.
18 * Government do not and cannot warrant the performance or results that
19 * may be obtained by using this software or data. The NLM and the U.S.
20 * Government disclaim all warranties, express or implied, including
21 * warranties of performance, merchantability or fitness for any particular
22 * purpose.
23 *
24 * Please cite the author in any work or product based on this material.
25 *
26 * ===========================================================================
27 *
28 * Author: Kamen Todorov, NCBI
29 *
30 * File Description:
31 * CAlignRangeCollection operations
32 *
33 */
34 
35 
36 #include <corelib/ncbistd.hpp>
37 
39 
40 
42 
43 
44 /// Subtract one range collection from another. Both first and second
45 /// sequences are checked, so that the result does not intersect with
46 /// any row of the minuend.
47 template<class TAlnRng>
49  const CAlignRangeCollection<TAlnRng>& minuend,
50  const CAlignRangeCollection<TAlnRng>& subtrahend,
52 {
53  typedef CAlignRangeCollection<TAlnRng> TAlnRngColl;
54  _ASSERT( !subtrahend.empty() );
55 
56  // Calc differece on the first row
57  TAlnRngColl difference_on_first(minuend.GetPolicyFlags());
58  {
59  typename TAlnRngColl::const_iterator subtrahend_it = subtrahend.begin();
60  ITERATE (typename TAlnRngColl, minuend_it, minuend) {
61  SubtractOnFirst(*minuend_it,
62  subtrahend,
63  difference_on_first,
64  subtrahend_it);
65  }
66  }
67 
68  // Second row
69  typedef CAlignRangeCollExtender<TAlnRngColl> TAlnRngCollExt;
70  TAlnRngCollExt subtrahend_ext(subtrahend);
71  subtrahend_ext.UpdateIndex();
72 
73  typename TAlnRngCollExt::const_iterator subtrahend_ext_it = subtrahend_ext.begin();
74  TAlnRngCollExt diff_on_first_ext(difference_on_first);
75  diff_on_first_ext.UpdateIndex();
76  ITERATE (typename TAlnRngCollExt, minuend_it, diff_on_first_ext) {
77  SubtractOnSecond(*(minuend_it->second),
78  subtrahend_ext,
79  difference,
80  subtrahend_ext_it);
81  }
82 }
83 
84 
85 template<class Range>
86 struct PRangeLess
87 {
88  typedef typename Range::position_type position_type;
89  bool operator()(const Range& r, position_type pos)
90  {
91  return r.GetFirstToOpen() <= pos;
92  }
93  bool operator()(position_type pos, const Range& r)
94  {
95  return pos < r.GetFirstToOpen();
96  }
97  bool operator()(const Range& r1, const Range& r2)
98  {
99  return r1.GetFirstToOpen() <= r2.GetFirstToOpen();
100  }
101  bool operator()(const Range* r, position_type pos)
102  {
103  return r->GetFirstToOpen() <= pos;
104  }
105  bool operator()(position_type pos, const Range* r)
106  {
107  return pos < r->GetFirstToOpen();
108  }
109  bool operator()(const Range* r1, const Range* r2)
110  {
111  return r1->GetFirstToOpen() <= r2->GetFirstToOpen();
112  }
113 };
114 
115 
116 template<class Iter, class T, class Compare>
117 inline
118 Iter scan_to_lower_bound(Iter first, Iter last, const T& value, Compare comp)
119 {
120  //auto ret = std::lower_bound(first, last, value, comp);
121  while ( first != last && comp(*first, value) ) {
122  ++first;
123  }
124  //_ASSERT(ret == first);
125  return first;
126 }
127 
128 
129 template<class TAlnRng>
131  const TAlnRng& minuend,
132  const CAlignRangeCollection<TAlnRng>& subtrahend,
133  CAlignRangeCollection<TAlnRng>& difference,
135 {
137 
138  r_it = scan_to_lower_bound(r_it,
139  subtrahend.end(),
140  minuend.GetFirstFrom(),
141  p); /* NCBI_FAKE_WARNING: WorkShop */
142 
143  if (r_it == subtrahend.end()) {
144  difference.insert(minuend);
145  return;
146  }
147 
148  int trim; // whether and how much to trim when truncating
149 
150  trim = (r_it->GetFirstFrom() <= minuend.GetFirstFrom());
151 
152  TAlnRng r = minuend;
153  TAlnRng tmp_r;
154 
155  while (1) {
156  if (trim) {
157  // x--------)
158  // ...---...
159  trim = r_it->GetFirstToOpen() - r.GetFirstFrom();
160  TrimFirstFrom(r, trim);
161  if ((int) r.GetLength() <= 0) {
162  return;
163  }
164  ++r_it;
165  if (r_it == subtrahend.end()) {
166  difference.insert(r);
167  return;
168  }
169  }
170 
171  // x------)
172  // x--...
173  trim = r.GetFirstToOpen() - r_it->GetFirstFrom();
174 
175  if (trim <= 0) {
176  // x----)
177  // x--)
178  difference.insert(r);
179  return;
180  }
181  else {
182  // x----)
183  // x----...
184  tmp_r = r;
185  TrimFirstTo(tmp_r, trim);
186  difference.insert(tmp_r);
187  }
188  }
189 }
190 
191 
192 template <class TAlnRng>
193 struct PItLess
194 {
195  typedef typename TAlnRng::position_type position_type;
197  bool operator() (const value_type& p,
198  position_type pos)
199  {
200  return p.second->GetSecondTo() < pos;
201  }
203  const value_type& p)
204  {
205  return pos < p.second->GetSecondTo();
206  }
207  bool operator() (const value_type& p1,
208  const value_type& p2)
209  {
210  return p1.second->GetSecondTo() < p2.second->GetSecondTo();
211  }
212 };
213 
214 
215 template<class TAlnRng>
217  const TAlnRng& minuend,
219  CAlignRangeCollection<TAlnRng>& difference,
220  typename CAlignRangeCollExtender<CAlignRangeCollection<TAlnRng> >::const_iterator& r_it)
221 {
222  if (minuend.GetSecondFrom() < 0) {
223  difference.insert(minuend);
224  return;
225  }
226 
228 
229  r_it = scan_to_lower_bound(r_it,
230  subtrahend_ext.end(),
231  minuend.GetSecondFrom(),
232  p); /* NCBI_FAKE_WARNING: WorkShop */
233 
234  if (r_it == subtrahend_ext.end()) {
235  difference.insert(minuend);
236  return;
237  }
238 
239  int trim; // whether and how much to trim when truncating
240 
241  trim = (r_it->second->GetSecondFrom() <= minuend.GetSecondFrom());
242 
243  TAlnRng r = minuend;
244  TAlnRng tmp_r;
245 
246  while (1) {
247  if (trim) {
248  // x--------)
249  // ...---...
250  trim = r_it->second->GetSecondToOpen() - r.GetSecondFrom();
251  TrimSecondFrom(r, trim);
252  if ((int) r.GetLength() <= 0) {
253  return;
254  }
255  ++r_it;
256  if (r_it == subtrahend_ext.end()) {
257  difference.insert(r);
258  return;
259  }
260  }
261 
262  // x------)
263  // x--...
264  trim = r.GetSecondToOpen() - r_it->second->GetSecondFrom();
265 
266  if (trim <= 0) {
267  // x----)
268  // x--)
269  difference.insert(r);
270  return;
271  }
272  else {
273  // x----)
274  // x----...
275  tmp_r = r;
276  TrimSecondTo(tmp_r, trim);
277  difference.insert(tmp_r);
278  }
279  }
280 }
281 
282 
283 template <class TAlnRng>
284 void TrimFirstFrom(TAlnRng& rng, int trim)
285 {
286  rng.SetLength(rng.GetLength() - trim);
287  rng.SetFirstFrom(rng.GetFirstFrom() + trim);
288  if (rng.IsDirect()) {
289  rng.SetSecondFrom(rng.GetSecondFrom() + trim);
290  }
291 }
292 
293 
294 template <class TAlnRng>
295 void TrimFirstTo(TAlnRng& rng, int trim)
296 {
297  if (rng.IsReversed()) {
298  rng.SetSecondFrom(rng.GetSecondFrom() + trim);
299  }
300  rng.SetLength(rng.GetLength() - trim);
301 }
302 
303 template <class TAlnRng>
304 void TrimSecondFrom(TAlnRng& rng, int trim)
305 {
306  rng.SetLength(rng.GetLength() - trim);
307  rng.SetSecondFrom(rng.GetSecondFrom() + trim);
308  if (rng.IsDirect()) {
309  rng.SetFirstFrom(rng.GetFirstFrom() + trim);
310  }
311 }
312 
313 template <class TAlnRng>
314 void TrimSecondTo(TAlnRng& rng, int trim)
315 {
316  if (rng.IsReversed()) {
317  rng.SetFirstFrom(rng.GetFirstFrom() + trim);
318  }
319  rng.SetLength(rng.GetLength() - trim);
320 }
321 
322 
324 
325 #endif // OBJTOOLS_ALNMGR__ALN_RNG_COLL_OPER__HPP
void TrimSecondTo(TAlnRng &rng, int trim)
void SubtractAlnRngCollections(const CAlignRangeCollection< TAlnRng > &minuend, const CAlignRangeCollection< TAlnRng > &subtrahend, CAlignRangeCollection< TAlnRng > &difference)
Subtract one range collection from another.
void TrimFirstFrom(TAlnRng &rng, int trim)
void TrimFirstTo(TAlnRng &rng, int trim)
void SubtractOnFirst(const TAlnRng &minuend, const CAlignRangeCollection< TAlnRng > &subtrahend, CAlignRangeCollection< TAlnRng > &difference, typename CAlignRangeCollection< TAlnRng >::const_iterator &r_it)
Iter scan_to_lower_bound(Iter first, Iter last, const T &value, Compare comp)
void SubtractOnSecond(const TAlnRng &minuend, const CAlignRangeCollExtender< CAlignRangeCollection< TAlnRng > > &subtrahend_ext, CAlignRangeCollection< TAlnRng > &difference, typename CAlignRangeCollExtender< CAlignRangeCollection< TAlnRng > >::const_iterator &r_it)
void TrimSecondFrom(TAlnRng &rng, int trim)
class CAlignRangeCollection<TAlignRange> represent a sorted collection of TAlignRange.
TAlignRangeVector::const_iterator const_iterator
position_type GetFirstToOpen() const
position_type GetFirstFrom() const
const_iterator insert(const TAlignRange &r)
const_iterator end() const
const_iterator begin() const
Include a standard set of the NCBI C++ Toolkit most basic headers.
#define T(s)
Definition: common.h:230
static DLIST_TYPE *DLIST_NAME() first(DLIST_LIST_TYPE *list)
Definition: dlist.tmpl.h:46
static DLIST_TYPE *DLIST_NAME() last(DLIST_LIST_TYPE *list)
Definition: dlist.tmpl.h:51
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
sequence::ECompare Compare(const CSeq_loc &loc1, const CSeq_loc &loc2, CScope *scope)
Returns the sequence::ECompare containment relationship between CSeq_locs.
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
double value_type
The numeric datatype used by the parser.
Definition: muParserDef.h:228
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
double r(size_t dimension_, const Int4 *score_, const double *prob_, double theta_)
TAlnRng::position_type position_type
CAlignRangeCollExtender< CAlignRangeCollection< TAlnRng > >::TFrom2Range::value_type value_type
bool operator()(const value_type &p, position_type pos)
bool operator()(const Range &r1, const Range &r2)
bool operator()(const Range &r, position_type pos)
bool operator()(const Range *r1, const Range *r2)
Range::position_type position_type
bool operator()(const Range *r, position_type pos)
bool operator()(position_type pos, const Range *r)
bool operator()(position_type pos, const Range &r)
#define _ASSERT
Modified on Wed Apr 17 13:10:18 2024 by modify_doxy.py rev. 669887