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

Go to the SVN repository for this file.

1 /* $Id: blastdb_seqid.hpp 71673 2016-03-22 17:15:46Z rackerst $
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: Christiam Camacho
27  *
28  */
29 
30 /** @file blastdb_seqid.hpp
31  * Definition of an identifier for a sequence in a BLAST database
32  */
33 
34 #ifndef OBJTOOLS_BLASTDB_FORMAT___BLASTDB_SEQID__HPP
35 #define OBJTOOLS_BLASTDB_FORMAT___BLASTDB_SEQID__HPP
36 
38 
40 
41 /// Encapsulates identifier to retrieve data from a BLAST database
43 {
44 public:
45  /// Default value for an invalid entry
46  static const int kInvalid = -1;
47 
48  /// Default constructor, creates an invalid object
49  CBlastDBSeqId() : m_OID(kInvalid), m_EntryChoice(eNone) {}
50 
51  /// Constructor which takes a string as input, can be a GI, accession,
52  /// NCBI Seq-id
53  /// @param entry string to parse [in]
54  CBlastDBSeqId(const string& entry) : m_OID(kInvalid), m_EntryChoice(eNone)
55  {
56  try {
57  m_EntrySpecified.m_GI = NStr::StringToLong(entry);
58  m_EntryChoice = eGi;
59  return;
60  } catch (...) {}
61  m_EntrySpecified.m_SequenceId = new string(entry);
62  m_EntryChoice = eSeqId;
63  }
64 
65  /// Constructor which takes a PIG as input
66  /// @param pig PIG [in]
67  CBlastDBSeqId(int pig) : m_OID(kInvalid) {
68  m_EntrySpecified.m_PIG = pig;
69  m_EntryChoice = ePig;
70  }
71 
72  /// Copy constructor
73  /// @param rhs object to copy [in]
75  do_copy(rhs);
76  }
77 
78  /// Assignment operator
79  /// @param rhs object to copy [in]
81  do_copy(rhs);
82  return *this;
83  }
84 
85  /// Destructor
87  if (IsStringId()) {
88  delete m_EntrySpecified.m_SequenceId;
89  }
90  }
91 
92  /// Convert this object to a string
93  string AsString() const {
94  string retval;
95  switch (m_EntryChoice) {
96  case ePig: retval = "PIG " + NStr::IntToString(GetPig()); break;
97  case eGi: retval = "GI " + NStr::NumericToString(GetGi()); break;
98  case eSeqId: retval = "'" + GetStringId() + "'"; break;
99  case eNone:
100  if (GetOID() != CBlastDBSeqId::kInvalid) {
101  retval = "OID " + NStr::IntToString(GetOID());
102  }
103  break;
104  default:
105  abort();
106  }
107  return retval;
108  }
109 
110  /// Stream insertion operator
111  /// @param out stream to write to [in|out]
112  /// @param id object to write [in]
113  friend ostream& operator<<(ostream& out, const CBlastDBSeqId& id) {
114  out << id.AsString();
115  return out;
116  }
117 
118  /// Does this object contain a GI?
119  bool IsGi() const { return m_EntryChoice == eGi; }
120  /// Does this object contain a PIG?
121  bool IsPig() const { return m_EntryChoice == ePig; }
122  /// Does this object contain a string identifier?
123  bool IsStringId() const { return m_EntryChoice == eSeqId; }
124  /// Does this object contain an OID?
125  bool IsOID() const { return m_EntryChoice == eNone || m_OID != kInvalid; }
126 
127  /// Retrieve this object's GI
128  TIntId GetGi() const { return m_EntrySpecified.m_GI; }
129  /// Retrieve this object's PIG
130  int GetPig() const { return m_EntrySpecified.m_PIG; }
131  /// Retrieve this object's string identifier
132  const string& GetStringId() const { return *m_EntrySpecified.m_SequenceId; }
133 
134  /// Set this object's OID
135  /// @param oid OID to set [in]
136  void SetOID(CSeqDB::TOID oid) { m_OID = oid; }
137  /// Retrieve this object's OID
138  CSeqDB::TOID GetOID() const { return m_OID; }
139 
140 private:
141  /// This object's OID
143 
144  /// Enumeration to distinguish the types of entries stored by this object
146  eNone, ///< Invalid
147  ePig, ///< PIG
148  eGi, ///< GI
149  eSeqId ///< Sequence identifier as string
150  };
151  /// Choice of entry set, only valid if
153 
154  /// Union to hold the memory of the data stored
155  union {
156  int m_PIG; ///< Store a PIG
157  TIntId m_GI; ///< Store a GI
158  string* m_SequenceId; ///< Store a sequence identifier as a string
159  } m_EntrySpecified;
160 
161  /// Copies an object of this type
162  /// @param rhs object to copy [in]
163  void do_copy(const CBlastDBSeqId& rhs) {
164  if (this != &rhs) {
165  m_OID = rhs.m_OID;
166  m_EntryChoice = rhs.m_EntryChoice;
167  if (IsStringId()) {
169  m_EntrySpecified.m_SequenceId =
171  } else {
172  m_EntrySpecified = rhs.m_EntrySpecified;
173  }
174  }
175  }
176 };
177 
178 
180 
181 #endif /* OBJTOOLS_BLASTDB_FORMAT___BLASTDB_SEQID__HPP */
182 
Encapsulates identifier to retrieve data from a BLAST database.
CBlastDBSeqId()
Default constructor, creates an invalid object.
EEntryChoices m_EntryChoice
Choice of entry set, only valid if.
bool IsPig() const
Does this object contain a PIG?
CBlastDBSeqId(const CBlastDBSeqId &rhs)
Copy constructor.
CBlastDBSeqId(const string &entry)
Constructor which takes a string as input, can be a GI, accession, NCBI Seq-id.
CBlastDBSeqId & operator=(const CBlastDBSeqId &rhs)
Assignment operator.
int m_PIG
Store a PIG.
friend ostream & operator<<(ostream &out, const CBlastDBSeqId &id)
Stream insertion operator.
CSeqDB::TOID GetOID() const
Retrieve this object's OID.
static const int kInvalid
Default value for an invalid entry.
const string & GetStringId() const
Retrieve this object's string identifier.
string * m_SequenceId
Store a sequence identifier as a string.
CSeqDB::TOID m_OID
This object's OID.
TIntId m_GI
Store a GI.
bool IsStringId() const
Does this object contain a string identifier?
union CBlastDBSeqId::@770 m_EntrySpecified
Union to hold the memory of the data stored.
bool IsOID() const
Does this object contain an OID?
bool IsGi() const
Does this object contain a GI?
int GetPig() const
Retrieve this object's PIG.
EEntryChoices
Enumeration to distinguish the types of entries stored by this object.
void do_copy(const CBlastDBSeqId &rhs)
Copies an object of this type.
TIntId GetGi() const
Retrieve this object's GI.
void SetOID(CSeqDB::TOID oid)
Set this object's OID.
string AsString() const
Convert this object to a string.
~CBlastDBSeqId()
Destructor.
CBlastDBSeqId(int pig)
Constructor which takes a PIG as input.
CObject –.
Definition: ncbiobj.hpp:180
int TOID
Sequence type accepted and returned for OID indices.
Definition: seqdb.hpp:216
std::ofstream out("events_result.xml")
main entry point for tests
Int8 TIntId
Definition: ncbimisc.hpp:999
string
Definition: cgiapp.hpp:687
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
static string IntToString(int value, TNumToStringFlags flags=0, int base=10)
Convert int to string.
Definition: ncbistr.hpp:5084
static long StringToLong(const CTempString str, TStringToNumFlags flags=0, int base=10)
Convert string to long.
Definition: ncbistr.cpp:653
static enable_if< is_arithmetic< TNumeric >::value||is_convertible< TNumeric, Int8 >::value, string >::type NumericToString(TNumeric value, TNumToStringFlags flags=0, int base=10)
Convert numeric value to string.
Definition: ncbistr.hpp:673
#define NCBI_BLASTDB_FORMAT_EXPORT
Definition: ncbi_export.h:1089
void abort()
Defines BLAST database access classes.
bool IsStringId(const CSeq_id &id)
Determine if id is srting id.
#define _ASSERT
@ eGi
GI Index.
@ ePig
Protein Identifier Group.
Modified on Sun Apr 14 05:27:19 2024 by modify_doxy.py rev. 669887