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

Go to the repository for this file.

1 /* $Id: Std_seg.cpp 74562 2016-09-13 13:46:29Z ivanov $
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: .......
27  *
28  * File Description:
29  * .......
30  *
31  * Remark:
32  * This code was originally generated by application DATATOOL
33  * using specifications from the data definition file
34  * 'seqalign.asn'.
35  */
36 
37 // standard includes
38 
39 // generated includes
40 #include <ncbi_pch.hpp>
43 
45 
46 // generated classes
47 
49 
50 BEGIN_objects_SCOPE // namespace ncbi::objects::
51 
52 // destructor
54 {
55 }
56 
57 
59 {
60  const size_t& dim = GetDim();
61  if (dim != GetLoc().size()) {
62  NCBI_THROW(CSeqalignException, eInvalidAlignment,
63  "CStd_seg::CheckNumRows():"
64  " loc.size is inconsistent with dim");
65  }
66  if (IsSetIds() && dim != GetIds().size()) {
67  NCBI_THROW(CSeqalignException, eInvalidAlignment,
68  "CStd_seg::CheckNumRows():"
69  " ids.size is inconsistent with dim");
70  }
71  _ASSERT(dim <= kMax_Int);
72  return (TDim)dim;
73 }
74 
75 
77 {
78  TDim row_i = 0;
80  if (row_i++ == row) {
81  if ((*i)->IsInt()) {
82  return (*i)->GetInt().GetFrom();
83  } else {
84  return -1;
85  }
86  }
87  }
88  if (row < 0 || row >= GetDim()) {
89  NCBI_THROW(CSeqalignException, eInvalidRowNumber,
90  "CStd_seg::GetSeqStart():"
91  " Invalid row number");
92  }
93  NCBI_THROW(CSeqalignException, eInvalidAlignment,
94  "CStd_seg::GetSeqStart():"
95  " loc.size is inconsistent with dim");
96 }
97 
98 
100 {
101  TDim dim = 0;
103  if (dim++ == row) {
104  if ((*i)->IsInt()) {
105  return (*i)->GetInt().GetTo();
106  } else {
107  return -1;
108  }
109  }
110  }
111  if (row < 0 || row >= GetDim()) {
112  NCBI_THROW(CSeqalignException, eInvalidRowNumber,
113  "CStd_seg::GetSeqStop():"
114  " Invalid row number");
115  }
116  NCBI_THROW(CSeqalignException, eInvalidAlignment,
117  "CStd_seg::GetSeqStop():"
118  " loc.size is inconsistent with dim");
119 }
120 
121 
122 void CStd_seg::Validate(bool /* full_test */) const
123 {
124  CheckNumRows();
125 }
126 
127 
128 void CStd_seg::SwapRows(int row0, int row1)
129 {
130  _ASSERT(row0 < (int)GetLoc().size());
131  _ASSERT(row1 < (int)GetLoc().size());
132  if (row0 < (int)GetLoc().size() && row1 < (int)GetLoc().size()) {
133  swap(SetLoc()[row0], SetLoc()[row1]);
134  if (IsSetIds()) {
135  swap(SetIds()[row0], SetIds()[row1]);
136  }
137  } else {
138  NCBI_THROW(CSeqalignException, eInvalidRowNumber,
139  "CStd_seg::SwapRows():"
140  " Invalid row number");
141  }
142 }
143 
144 
146 {
147  TDim dim = 0;
149  if (dim++ == row) {
150  if ((*i)->IsInt()) {
151  const CSeq_interval& interval = (*i)->GetInt();
152  return CRange<TSignedSeqPos>
153  (interval.GetFrom(), interval.GetTo());
154  } else {
155  return CRange<TSignedSeqPos>(-1, -1);
156  }
157  }
158  }
159  if (row < 0 || row >= GetDim()) {
160  NCBI_THROW(CSeqalignException, eInvalidRowNumber,
161  "CStd_seg::GetSeqRange():"
162  " Invalid row number");
163  }
164  NCBI_THROW(CSeqalignException, eInvalidAlignment,
165  "CStd_seg::GetSeqRange():"
166  " loc.size is inconsistent with dim");
167 }
168 
169 
172 
173 {
174  if (offset == 0) return;
175 
176  CSeq_loc& src_loc = *SetLoc()[row];
177 
178  switch (src_loc.Which()) {
179  case CSeq_loc::e_Int:
180  if (offset < 0) {
181  _ASSERT((TSignedSeqPos)src_loc.GetInt().GetFrom() + offset >= 0);
182  if ((TSignedSeqPos)src_loc.GetInt().GetFrom() < -offset) {
183  NCBI_THROW(CSeqalignException, eOutOfRange,
184  "Negative offset greater than seq position");
185  }
186  }
187  src_loc.SetInt().SetFrom() += offset;
188  src_loc.SetInt().SetTo() += offset;
189  break;
190  case CSeq_loc::e_Pnt:
191  if (offset < 0) {
192  _ASSERT((TSignedSeqPos)src_loc.GetPnt().GetPoint() + offset >= 0);
193  if ((TSignedSeqPos)src_loc.GetPnt().GetPoint() < -offset) {
194  NCBI_THROW(CSeqalignException, eOutOfRange,
195  "Negative offset greater than seq position");
196  }
197  }
198  src_loc.SetPnt().SetPoint() += offset;
199  break;
200  case CSeq_loc::e_Empty:
201  break;
202  default:
203  NCBI_THROW(CSeqalignException, eUnsupported,
204  "CStd_seg::OffsetRow only supports pnt and int source seq-locs");
205  }
206 }
207 
208 
209 /// @deprecated
211  const CSeq_loc& dst_loc,
212  bool ignore_strand)
213 {
214  // Limit to certain types of locs:
215  switch (dst_loc.Which()) {
216  case CSeq_loc::e_Whole:
217  return;
218  case CSeq_loc::e_Int:
219  break;
220  default:
221  NCBI_THROW(CSeqalignException, eUnsupported,
222  "CStd_seg::RemapToLoc only supports int target seq-locs");
223  }
224 
225  if (row < 0 || row >= GetDim()) {
226  NCBI_THROW(CSeqalignException, eInvalidRowNumber,
227  "CStd_seg::RemapToLoc():"
228  " Invalid row number");
229  }
230 
231  const CSeq_interval& dst_int = dst_loc.GetInt();
232  TSeqPos dst_len = dst_int.GetTo() - dst_int.GetFrom() + 1;
233 
234  CSeq_loc& src_loc = *SetLoc()[row];
235  TSeqPos src_stop = src_loc.GetStop(eExtreme_Positional);
236 
237 #if _DEBUG
238  // Check ids equality
239  const CSeq_id* single_src_id = 0;
240  src_loc.CheckId(single_src_id);
241  _ASSERT(single_src_id);
242 
243  const CSeq_id* single_dst_id = 0;
244  dst_loc.CheckId(single_dst_id);
245  _ASSERT(single_dst_id);
246 
247  if ( !single_src_id->Equals(*single_dst_id) ||
248  (IsSetIds() && !single_dst_id->Equals(*GetIds()[row])) ) {
249  NCBI_THROW(CSeqalignException, eInvalidInputData,
250  "CStd_seg::RemapToLoc target seq-loc id does not equal row's id.");
251  }
252 #endif
253 
254  // Check the range
255  if (dst_len <= src_stop && src_stop != kInvalidSeqPos) {
256  string errstr("CStd_seg::RemapToLoc():"
257  " Target Seq-loc is not long enough to"
258  " cover the Std-seg's seq-loc!"
259  " Maximum row seq pos is ");
260  errstr += NStr::IntToString(src_stop);
261  errstr += ". The total seq-loc len is only ";
262  errstr += NStr::IntToString(dst_len);
263  errstr += ", it should be at least ";
264  errstr += NStr::IntToString(src_stop + 1);
265  errstr += " (= max seq pos + 1).";
266  NCBI_THROW(CSeqalignException, eOutOfRange, errstr);
267  }
268 
269  // Actual remapping
270  switch (src_loc.Which()) {
271  case CSeq_loc::e_Int:
272  src_loc.SetInt().SetFrom() += dst_int.GetFrom();
273  src_loc.SetInt().SetTo() += dst_int.GetFrom();
274  if ( !ignore_strand ) {
275  src_loc.SetInt().SetStrand(dst_loc.GetInt().GetStrand());
276  }
277  break;
278  case CSeq_loc::e_Pnt:
279  src_loc.SetPnt().SetPoint() += dst_int.GetFrom();
280  if ( !ignore_strand ) {
281  src_loc.SetPnt().SetStrand(dst_loc.GetInt().GetStrand());
282  }
283  break;
284  case CSeq_loc::e_Empty:
285  break;
286  default:
287  NCBI_THROW(CSeqalignException, eUnsupported,
288  "CStd_seg::RemapToLoc only supports pnt and int source seq-locs");
289  }
290 }
291 
292 
294 {
295  if (GetDim() <= row) {
296  NCBI_THROW(CSeqalignException, eInvalidRowNumber,
297  "Invalid row number in CreateRowSeq_interval(): " +
299  }
300  CRef<CSeq_loc> ret(new CSeq_loc);
301  ret->Assign(*GetLoc()[row]);
302  return ret;
303 }
304 
305 
306 END_objects_SCOPE // namespace ncbi::objects::
307 
309 
310 /* Original file checksum: lines: 64, chars: 1879, CRC32: d01e569e */
@ eExtreme_Positional
numerical value
Definition: Na_strand.hpp:63
void Validate(bool full_test=false) const
Definition: Std_seg.cpp:122
void SwapRows(TDim row1, TDim row2)
Swap two rows (changing *order*, not content)
Definition: Std_seg.cpp:128
TSignedSeqPos GetSeqStop(TDim row) const
Definition: Std_seg.cpp:99
TDim CheckNumRows(void) const
Definition: Std_seg.cpp:58
~CStd_seg(void)
Definition: Std_seg.cpp:53
TSignedSeqPos GetSeqStart(TDim row) const
Definition: Std_seg.cpp:76
void RemapToLoc(TDim row, const CSeq_loc &dst_loc, bool ignore_strand=false)
Definition: Std_seg.cpp:210
CRef< CSeq_loc > CreateRowSeq_loc(TDim row) const
Definition: Std_seg.cpp:293
CRange< TSignedSeqPos > GetSeqRange(TDim row) const
Definition: Std_seg.cpp:145
void OffsetRow(TDim row, TSignedSeqPos offset)
Offset row's coords.
Definition: Std_seg.cpp:170
int offset
Definition: replacements.h:160
void swap(ncbi::pair_base_member< T1, T2 > &pair1, ncbi::pair_base_member< T1, T2 > &pair2) noexcept
Definition: ncbimisc.hpp:1616
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
int TSignedSeqPos
Type for signed sequence position.
Definition: ncbimisc.hpp:986
const TSeqPos kInvalidSeqPos
Define special value for invalid sequence position.
Definition: ncbimisc.hpp:977
#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
virtual bool Equals(const CSerialObject &object, ESerialRecursionMode how=eRecursive) const
Check if both objects contain the same values.
virtual void Assign(const CSerialObject &source, ESerialRecursionMode how=eRecursive)
Override Assign() to incorporate cache invalidation.
Definition: Seq_loc.cpp:337
#define kMax_Int
Definition: ncbi_limits.h:184
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:104
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:101
static string IntToString(int value, TNumToStringFlags flags=0, int base=10)
Convert int to string.
Definition: ncbistr.hpp:5057
TIds & SetIds(void)
Assign a value to Ids data member.
Definition: Std_seg_.hpp:338
vector< CRef< CSeq_loc > > TLoc
Definition: Std_seg_.hpp:93
const TLoc & GetLoc(void) const
Get the Loc member data.
Definition: Std_seg_.hpp:357
const TIds & GetIds(void) const
Get the Ids member data.
Definition: Std_seg_.hpp:332
TLoc & SetLoc(void)
Assign a value to Loc data member.
Definition: Std_seg_.hpp:363
bool IsSetIds(void) const
Check if a value has been assigned to Ids data member.
Definition: Std_seg_.hpp:320
TDim GetDim(void) const
Get the Dim member data.
Definition: Std_seg_.hpp:295
TFrom GetFrom(void) const
Get the From member data.
TTo GetTo(void) const
Get the To member data.
@ e_Empty
to NULL one Seq-id in a collection
Definition: Seq_loc_.hpp:99
@ e_Int
from to
Definition: Seq_loc_.hpp:101
@ e_Whole
whole sequence
Definition: Seq_loc_.hpp:100
int i
const struct ncbi::grid::netcache::search::fields::SIZE size
#define row(bind, expected)
Definition: string_bind.c:73
#define _ASSERT
Modified on Sat Nov 07 14:12:37 2026 by modify_doxy.py rev. 717554