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

Go to the SVN repository for this file.

1 /* $Id: Textseq_id.cpp 83902 2018-09-28 12:31:02Z satskyse $
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: Jim Ostell
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 // standard includes
38 
39 // generated includes
40 #include <ncbi_pch.hpp>
43 
44 
45 // generated classes
46 
48 BEGIN_objects_SCOPE // namespace ncbi::objects::
49 
50 
51 // destructor
53 {
54  return;
55 }
56 
57 
59 (const CTempString& acc_in,
60  const CTempString& name_in,
61  int version,
62  const CTempString& release_in,
63  bool allow_dot_version)
64 {
65  // Perform general sanity checks up front.
66  if (version < 0) {
67  NCBI_THROW(CSeqIdException, eFormat,
68  "Unexpected negative version " + NStr::IntToString(version)
69  + " for accession " + string(acc_in));
70  }
71 
72  CTempString acc =
74  CTempString name =
76  CTempString release =
78 
79  if (acc.empty() && name.empty()) {
80  }
81 
82  if (acc.empty()) {
84  } else {
85  SIZE_TYPE idx = NPOS;
86 
87  if (allow_dot_version) {
88  idx = acc.rfind('.');
89  }
90 
91  if (idx == NPOS) {
92  // no version within acc
93  SetAccession(acc);
94 
95  // any standalone version is ok
96  if (version > 0) {
98  } else {
99  ResetVersion();
100  }
101  } else {
102  // accession.version
103  CTempString accession = acc.substr(0, idx);
104  CTempString acc_ver = acc.substr(idx + 1);
105  int ver = NStr::StringToNonNegativeInt(acc_ver);
106 
107  if (ver <= 0) {
108  NCBI_THROW(CSeqIdException, eFormat,
109  "Version embedded in accession " + string(acc)
110  + " is not a positive integer");
111  } else if (version > 0 && ver != version) {
112  NCBI_THROW(CSeqIdException, eFormat,
113  "Incompatible version " + NStr::IntToString(version)
114  + " supplied for accession " + string(acc));
115  }
116 
117  SetAccession(accession);
118  SetVersion(ver);
119  }
120  }
121 
122  if (name.empty()) {
123  ResetName();
124  } else {
125  SetName(name);
126  }
127 
128  if (acc.empty() && name.empty()) {
129  NCBI_THROW(CSeqIdException, eFormat,
130  "Accession and name missing for Textseq-id (but got"
131  " version " + NStr::IntToString(version) + ", release "
132  + string(release) + ')');
133  } else if (release.empty()) {
134  ResetRelease();
135  } else {
136  SetRelease(release);
137  }
138  return *this;
139 }
140 
141 
142 // comparison function
143 bool CTextseq_id::Match(const CTextseq_id& tsip2) const
144 {
145  // Check Accessions first
146  if (IsSetAccession() && tsip2.IsSetAccession()) {
147  if ( PNocase().Equals(GetAccession(), tsip2.GetAccession()) ) {
148  if (IsSetVersion() && tsip2.IsSetVersion()) {
149  return GetVersion() == tsip2.GetVersion();
150  } else {
151  return true;
152  }
153  } else {
154  return false;
155  }
156  }
157 
158  // then try name
159  if (IsSetName() && tsip2.IsSetName()) {
160  if ( PNocase().Equals(GetName(), tsip2.GetName()) ) {
161  if (IsSetVersion() && tsip2.IsSetVersion()) {
162  return GetVersion() == tsip2.GetVersion();
163  }
164  else {
165  return true;
166  }
167  } else {
168  return false;
169  }
170  }
171 
172  //nothing to compare
173  return false;
174 }
175 
176 
177 // comparison function
178 int CTextseq_id::Compare(const CTextseq_id& tsip2) const
179 {
180  // Check Accessions first
181  // no-accession before accession
182  if ( int diff = IsSetAccession() - tsip2.IsSetAccession() ) {
183  return diff;
184  }
185  if ( IsSetAccession() ) {
186  _ASSERT(tsip2.IsSetAccession());
187  // sort by accession
188  if ( int diff = PNocase().Compare(GetAccession(),
189  tsip2.GetAccession()) ) {
190  return diff;
191  }
192  }
193 
194  // Check version
195  // no-version before version
196  if ( int diff = IsSetVersion() - tsip2.IsSetVersion() ) {
197  return diff;
198  }
199  if ( IsSetVersion() ) {
200  _ASSERT(tsip2.IsSetVersion());
201  // smaller version first
202  if ( int diff = GetVersion() - tsip2.GetVersion() ) {
203  return diff;
204  }
205  }
206  if ( IsSetAccession() && IsSetVersion() ) {
207  // acc.ver are the same -> equal Seq-ids
208  return 0;
209  }
210 
211  // Check name
212  // no-name before name
213  if ( int diff = IsSetName() - tsip2.IsSetName() ) {
214  return diff;
215  }
216  if ( IsSetName() ) {
217  _ASSERT(tsip2.IsSetName());
218  if ( int diff = PNocase().Compare(GetName(), tsip2.GetName()) ) {
219  return diff;
220  }
221  }
222 
223  // All checks failed to distinguish Seq-ids.
224  return 0;
225 }
226 
227 
228 // format the contents FASTA string style
229 ostream& CTextseq_id::AsFastaString(ostream& s, bool allow_version) const
230 {
231  if (IsSetAccession()) {
232  s << GetAccession(); // no Upcase per Ostell - Karl 7/2001
233  if (allow_version && IsSetVersion()) {
234  int version = GetVersion();
235  if (version) {
236  s.put('.');
237  s << version;
238  }
239  }
240  }
241 
242  s.put('|');
243  if ( IsSetName() ) {
244  s << GetName(); // no Upcase per Ostell - Karl 7/2001
245  }
246  return s;
247 }
248 
249 
250 END_objects_SCOPE // namespace ncbi::objects::
CSeqIdException –.
Definition: Seq_id.hpp:1001
CTempString implements a light-weight string on top of a storage buffer whose lifetime management is ...
Definition: tempstr.hpp:65
ostream & AsFastaString(ostream &s, bool allow_version=true) const
Format the contents FASTA string style.
Definition: Textseq_id.cpp:229
CTextseq_id & Set(const CTempString &acc_in, const CTempString &name_in=kEmptyStr, int version=0, const CTempString &release_in=kEmptyStr, bool allow_dot_version=true)
Set all fields with a single call.
Definition: Textseq_id.cpp:59
int Compare(const CTextseq_id &tsip2) const
Definition: Textseq_id.cpp:178
bool Match(const CTextseq_id &tsip2) const
Comparison functions.
Definition: Textseq_id.cpp:143
virtual ~CTextseq_id(void)
Definition: Textseq_id.cpp:52
#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.
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
NCBI_NS_STD::string::size_type SIZE_TYPE
Definition: ncbistr.hpp:132
static CTempString TruncateSpaces_Unsafe(const CTempString str, ETrunc where=eTrunc_Both)
Truncate whitespace in a string.
Definition: ncbistr.cpp:3182
static int StringToNonNegativeInt(const CTempString str, TStringToNumFlags flags=0)
Convert string to non-negative integer value.
Definition: ncbistr.cpp:457
size_type rfind(const CTempString match, size_type pos=npos) const
Find the first instance of the entire matching string within the current string in a backward directi...
Definition: tempstr.hpp:705
#define NPOS
Definition: ncbistr.hpp:133
static string IntToString(int value, TNumToStringFlags flags=0, int base=10)
Convert int to string.
Definition: ncbistr.hpp:5078
PNocase_Generic< string > PNocase
Definition: ncbistr.hpp:4902
bool empty(void) const
Return true if the represented string is empty (i.e., the length is zero)
Definition: tempstr.hpp:334
CTempString substr(size_type pos) const
Obtain a substring from this string, beginning at a given offset.
Definition: tempstr.hpp:776
@ eTrunc_Both
Truncate whitespace at both begin and end of string.
Definition: ncbistr.hpp:2242
TName & SetName(void)
Assign a value to Name data member.
bool IsSetAccession(void) const
Check if a value has been assigned to Accession data member.
const TName & GetName(void) const
Get the Name member data.
void ResetVersion(void)
Reset Version data member.
void ResetAccession(void)
Reset Accession data member.
Definition: Textseq_id_.cpp:56
void ResetRelease(void)
Reset Release data member.
Definition: Textseq_id_.cpp:62
TVersion & SetVersion(void)
Assign a value to Version data member.
TVersion GetVersion(void) const
Get the Version member data.
TAccession & SetAccession(void)
Assign a value to Accession data member.
TRelease & SetRelease(void)
Assign a value to Release data member.
void ResetName(void)
Reset Name data member.
Definition: Textseq_id_.cpp:50
bool IsSetVersion(void) const
Check if a value has been assigned to Version data member.
bool IsSetName(void) const
Check if a value has been assigned to Name data member.
const TAccession & GetAccession(void) const
Get the Accession member data.
const string version
version string
Definition: variables.hpp:66
#define _ASSERT
Modified on Fri Sep 20 14:57:50 2024 by modify_doxy.py rev. 669887