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

Go to the SVN repository for this file.

1 #ifndef OBJECTS_OBJMGR___SEQ_ID_HANDLE__HPP
2 #define OBJECTS_OBJMGR___SEQ_ID_HANDLE__HPP
3 
4 /* $Id: seq_id_handle.hpp 99793 2023-05-10 18:04:05Z vasilche $
5 * ===========================================================================
6 *
7 * PUBLIC DOMAIN NOTICE
8 * National Center for Biotechnology Information
9 *
10 * This software/database is a "United States Government Work" under the
11 * terms of the United States Copyright Act. It was written as part of
12 * the author's official duties as a United States Government employee and
13 * thus cannot be copyrighted. This software/database is freely available
14 * to the public for use. The National Library of Medicine and the U.S.
15 * Government have not placed any restriction on its use or reproduction.
16 *
17 * Although all reasonable efforts have been taken to ensure the accuracy
18 * and reliability of the software and data, the NLM and the U.S.
19 * Government do not and cannot warrant the performance or results that
20 * may be obtained by using this software or data. The NLM and the U.S.
21 * Government disclaim all warranties, express or implied, including
22 * warranties of performance, merchantability or fitness for any particular
23 * purpose.
24 *
25 * Please cite the author in any work or product based on this material.
26 *
27 * ===========================================================================
28 *
29 * Author: Aleksey Grichenko, Eugene Vasilchenko
30 *
31 * File Description:
32 * Seq-id handle for Object Manager
33 *
34 */
35 
36 
38 
39 #include <set>
40 #include <vector>
41 
44 
45 /** @addtogroup OBJECTS_Seqid
46  *
47  * @{
48  */
49 
50 
51 /////////////////////////////////////////////////////////////////////
52 ///
53 /// CSeq_id_Handle::
54 ///
55 /// Handle to be used instead of CSeq_id to optimize indexing and sorting.
56 /// Comparing seq-id handles is not guaranteed to produce the same results
57 /// as comparing seq-ids, to be stable or to remain the same between application
58 /// runs. For stable sorting use CSeq_id_Handle::PLessOrdered functor.
59 ///
60 
61 // forward declaration
62 class CSeq_id;
63 class CSeq_id_Handle;
64 class CSeq_id_Mapper;
65 class CSeq_id_Which_Tree;
66 
67 
68 class CSeq_id_Info : public CObject
69 {
70 public:
71  typedef TIntId TPacked;
72  typedef Uint8 TVariant;
73 
75  CSeq_id_Mapper* mapper);
77  CSeq_id_Mapper* mapper);
79 
81  {
82  return m_Seq_id;
83  }
84  NCBI_SEQ_EXPORT virtual CConstRef<CSeq_id> GetPackedSeqId(TPacked packed, TVariant variant) const;
85 
86  // locking
87  void AddLock(void) const
88  {
89  ++m_LockCounter;
90  }
91  void RemoveLock(void) const
92  {
93  if ( --m_LockCounter <= 0 ) {
95  }
96  }
97  bool IsLocked(void) const
98  {
99  return m_LockCounter != 0;
100  }
101 
103  {
104  return m_Seq_id_Type.load(memory_order_relaxed);
105  }
107  {
108  return *m_Mapper;
109  }
111 
112  virtual int CompareOrdered(const CSeq_id_Info& other, const CSeq_id_Handle& h_this, const CSeq_id_Handle& h_other) const;
113 
114 protected:
115  friend class CSeq_id_Which_Tree;
116 
117  NCBI_SEQ_EXPORT void x_RemoveLastLock(void) const;
118 
119  mutable atomic<Uint8> m_LockCounter{0};
120  atomic<CSeq_id::E_Choice> m_Seq_id_Type;
123 
124 private:
125  // to prevent copying
128 };
129 
130 
132 {
133 public:
134  void Lock(const CSeq_id_Info* info) const
135  {
137  info->AddLock();
138  }
139  void Relock(const CSeq_id_Info* info) const
140  {
141  Lock(info);
142  }
143  void Unlock(const CSeq_id_Info* info) const
144  {
145  info->RemoveLock();
147  }
148 };
149 
150 
154 };
155 
156 
158 {
159 public:
162 
163  // 'ctors
165  : m_Info(null), m_Packed(0), m_Variant(0)
166  {
167  }
168  explicit CSeq_id_Handle(const CSeq_id_Info* info, TPacked packed = 0, TVariant variant = 0)
169  : m_Info(info), m_Packed(packed), m_Variant(variant)
170  {
171  _ASSERT(info || (!packed && !variant));
172  }
174  : m_Info(null), m_Packed(0), m_Variant(0)
175  {
176  }
177 
178  /// Normal way of getting a handle, works for any seq-id.
180 
181  /// Construct CSeq_id from string representation and return handle for it.
182  static NCBI_SEQ_EXPORT CSeq_id_Handle GetHandle(const string& str_id);
183 
184  /// Faster way to create a handle for a gi.
186 
187  /// Faster way to create a handle for a gi.
189  {
190  return GetHandle(gi);
191  }
192 
193  bool operator== (const CSeq_id_Handle& handle) const
194  {
195  return m_Packed == handle.m_Packed && m_Info == handle.m_Info;
196  }
197  bool operator!= (const CSeq_id_Handle& handle) const
198  {
199  return m_Packed != handle.m_Packed || m_Info != handle.m_Info;
200  }
201  bool operator< (const CSeq_id_Handle& handle) const
202  {
203  // Packed (m_Packed != 0) first:
204  // zeroes are converted to a highest unsigned value by decrement.
206  TUintId p2 = INT_ID_TO(TUintId, handle.m_Packed-1);
207  return p1 < p2 || (p1 == p2 && m_Info < handle.m_Info);
208  }
209  bool NCBI_SEQ_EXPORT operator== (const CSeq_id& id) const;
210 
211  /// Compare ids in a defined order (see CSeq_id::CompareOrdered())
212  int NCBI_SEQ_EXPORT CompareOrdered(const CSeq_id_Handle& id) const;
213  /// Predicate for sorting CSeq_id_Handles in a defined order.
215  {
216  bool operator()(const CSeq_id_Handle& id1,
217  const CSeq_id_Handle& id2) const
218  {
219  return id1.CompareOrdered(id2) < 0;
220  }
221  };
222 
223  /// Check if the handle is a valid or an empty one
225 
226  /// Reset the handle (remove seq-id reference)
227  void Reset(void)
228  {
229  m_Info.Reset();
230  m_Packed = 0;
231  m_Variant = 0;
232  }
233 
234  //
235  bool NCBI_SEQ_EXPORT HaveMatchingHandles(void) const;
236  bool NCBI_SEQ_EXPORT HaveReverseMatch(void) const;
237  bool NCBI_SEQ_EXPORT HaveMatchingHandles(EAllowWeakMatch allow_weak_match) const;
238  bool NCBI_SEQ_EXPORT HaveReverseMatch(EAllowWeakMatch allow_weak_match) const;
239 
240  //
242  void NCBI_SEQ_EXPORT GetMatchingHandles(TMatches& matches) const;
245  EAllowWeakMatch allow_weak_match) const;
247  EAllowWeakMatch allow_weak_match) const;
248 
249  /// True if *this matches to h.
250  /// This mean that *this is either the same as h,
251  /// or more generic version of h.
252  bool NCBI_SEQ_EXPORT MatchesTo(const CSeq_id_Handle& h) const;
253 
254  /// True if "this" is a better bioseq than "h".
255  bool NCBI_SEQ_EXPORT IsBetter(const CSeq_id_Handle& h) const;
256 
257  string NCBI_SEQ_EXPORT AsString(void) const;
258 
260  {
261  return m_Info->GetType();
262  }
263  bool IsPacked(void) const
264  {
265  return m_Packed != 0;
266  }
267  TPacked GetPacked(void) const
268  {
269  return m_Packed;
270  }
271  bool IsSetVariant(void) const
272  {
273  return m_Variant != 0;
274  }
275  TVariant GetVariant(void) const
276  {
277  return m_Variant;
278  }
279  bool IsGi(void) const
280  {
281  return m_Packed && m_Info->GetType() == CSeq_id::e_Gi;
282  }
283  TGi GetGi(void) const
284  {
285  return IsGi()? TGi(m_Packed) : ZERO_GI;
286  }
287  bool IsAccVer(void) const
288  {
289  if (IsGi()) return false;
290  auto seq_id = GetSeqId();
291  if (!seq_id) return false;
292  auto text_id = seq_id->GetTextseq_Id();
293  return text_id &&
294  text_id->IsSetAccession() &&
295  text_id->IsSetVersion();
296  }
297  unsigned NCBI_SEQ_EXPORT GetHash(void) const;
298 
300  {
301  return GetSeqId()->IdentifyAccession();
302  }
303 
305  {
306  CConstRef<CSeq_id> ret;
307  if ( m_Packed || m_Variant ) {
309  }
310  else {
311  ret = m_Info->GetSeqId();
312  }
313  return ret;
314  }
316  {
317  if ( !m_Info ) {
318  return null;
319  }
320  return GetSeqId();
321  }
322 
324  {
325  return m_Info->GetMapper();
326  }
327 
328  void Swap(CSeq_id_Handle& idh)
329  {
330  m_Info.Swap(idh.m_Info);
331  swap(m_Packed, idh.m_Packed);
332  swap(m_Variant, idh.m_Variant);
333  }
334 
336 
337 public:
338  const CSeq_id_Info* x_GetInfo(void) const {
339  return m_Info;
340  }
341 
342 private:
343  friend class CSeq_id_Mapper;
344  friend class CSeq_id_Which_Tree;
345 
346  // Seq-id info
350 };
351 
352 /// Get CConstRef<CSeq_id> from a seq-id handle (for container
353 /// searching template functions)
354 template<>
355 inline
357 {
358  return idh.GetSeqId();
359 }
360 
361 
362 /////////////////////////////////////////////////////////////////////
363 //
364 // Inline methods
365 //
366 /////////////////////////////////////////////////////////////////////
367 
368 /* @} */
369 
370 
371 /// Return best label for a sequence from single Seq-id, or set of Seq-ids.
372 /// Return empty string if the label cannot be determined.
373 /// GetDirectLabel() will return non-empty string only if the Seq-id is
374 /// very likely enough to get good label without loading full set of
375 /// sequence Seq-ids.
376 NCBI_SEQ_EXPORT string GetDirectLabel(const CSeq_id& id);
378 NCBI_SEQ_EXPORT string GetLabel(const CSeq_id& id);
379 NCBI_SEQ_EXPORT string GetLabel(const CSeq_id_Handle& id);
380 NCBI_SEQ_EXPORT string GetLabel(const vector<CSeq_id_Handle>& ids);
381 NCBI_SEQ_EXPORT string GetLabel(const vector<CRef<CSeq_id> >& ids);
382 
383 
386 
387 
390 
392 inline
393 void swap(NCBI_NS_NCBI::objects::CSeq_id_Handle& idh1,
394  NCBI_NS_NCBI::objects::CSeq_id_Handle& idh2)
395 {
396  idh1.Swap(idh2);
397 }
398 
400 
401 #endif /* OBJECTS_OBJMGR___SEQ_ID_HANDLE__HPP */
CObject –.
Definition: ncbiobj.hpp:180
Definition: set.hpp:45
std::ofstream out("events_result.xml")
main entry point for tests
Uint8 TUintId
Definition: ncbimisc.hpp:1000
Int8 TIntId
Definition: ncbimisc.hpp:999
CStrictId< SStrictId_Gi, SStrictId_Gi::TId > TGi
Definition: ncbimisc.hpp:1025
ENull
CNullable –.
Definition: ncbimisc.hpp:645
#define ZERO_GI
Definition: ncbimisc.hpp:1088
#define INT_ID_TO(T, id)
Convert gi-compatible int to/from other types.
Definition: ncbimisc.hpp:1120
@ null
Definition: ncbimisc.hpp:646
string GetDirectLabel(const CSeq_id &id)
Return best label for a sequence from single Seq-id, or set of Seq-ids.
bool operator()(const CSeq_id_Handle &id1, const CSeq_id_Handle &id2) const
void RemoveLock(void) const
CSeq_id_Handle(const CSeq_id_Info *info, TPacked packed=0, TVariant variant=0)
virtual int CompareOrdered(const CSeq_id_Info &other, const CSeq_id_Handle &h_this, const CSeq_id_Handle &h_other) const
atomic< Uint8 > m_LockCounter
static EAccessionInfo IdentifyAccession(const CTempString &accession, TParseFlags flags=fParse_AnyRaw)
Deduces information from a bare accession a la WHICH_db_accession; may report false negatives on prop...
Definition: Seq_id.cpp:1634
void Lock(const CSeq_id_Info *info) const
bool operator!=(const CSeq_id_Handle &handle) const
bool IsPacked(void) const
CSeq_id_Info::TVariant TVariant
CSeq_id_Which_Tree & GetTree(void) const
CConstRef< CSeq_id > GetSeqId(void) const
bool HaveReverseMatch(void) const
BEGIN_STD_SCOPE void swap(NCBI_NS_NCBI::objects::CSeq_id_Handle &idh1, NCBI_NS_NCBI::objects::CSeq_id_Handle &idh2)
CRef< CSeq_id_Mapper > m_Mapper
void Reset(void)
Reset the handle (remove seq-id reference)
void Unlock(const CSeq_id_Info *info) const
EAccessionInfo
For IdentifyAccession (below)
Definition: Seq_id.hpp:220
CSeq_id_Mapper & GetMapper(void) const
CConstRef< CSeq_id > GetSeqIdOrNull(void) const
static CSeq_id_Handle GetGiHandle(TGi gi)
Faster way to create a handle for a gi.
CConstRef< CSeq_id > m_Seq_id
virtual CConstRef< CSeq_id > GetPackedSeqId(TPacked packed, TVariant variant) const
bool IsGi(void) const
CSeq_id_Info(const CSeq_id_Info &)
CSeq_id_Info::TPacked TPacked
void AddLock(void) const
bool IsAllowedSNPScaleLimit(CSeq_id::ESNPScaleLimit scale_limit) const
bool MatchesTo(const CSeq_id_Handle &h) const
True if *this matches to h.
CSeq_id::EAccessionInfo IdentifyAccession(void) const
bool operator<(const CSeq_id_Handle &handle) const
CConstRef< CSeq_id_Info, CSeq_id_InfoLocker > m_Info
CConstRef< CSeq_id > Get_ConstRef_Seq_id(const CSeq_id_Handle &idh)
Get CConstRef<CSeq_id> from a seq-id handle (for container searching template functions)
TVariant GetVariant(void) const
bool IsAccVer(void) const
const CSeq_id_Info & operator=(const CSeq_id_Info &)
EAllowWeakMatch
void GetReverseMatchingHandles(TMatches &matches) const
bool operator==(const CSeq_id_Handle &handle) const
int CompareOrdered(const CSeq_id_Handle &id) const
Compare ids in a defined order (see CSeq_id::CompareOrdered())
static CSeq_id_Handle GetHandle(const CSeq_id &id)
Normal way of getting a handle, works for any seq-id.
string AsString(void) const
DECLARE_OPERATOR_BOOL_REF(m_Info)
Check if the handle is a valid or an empty one.
CNcbiOstream & operator<<(CNcbiOstream &out, const CSeq_id_Handle &idh)
CSeq_id_Info(CSeq_id::E_Choice type, CSeq_id_Mapper *mapper)
atomic< CSeq_id::E_Choice > m_Seq_id_Type
const CSeq_id_Info * x_GetInfo(void) const
unsigned GetHash(void) const
void GetMatchingHandles(TMatches &matches) const
void Swap(CSeq_id_Handle &idh)
void x_RemoveLastLock(void) const
CSeq_id::E_Choice GetType(void) const
bool IsSetVariant(void) const
bool IsBetter(const CSeq_id_Handle &h) const
True if "this" is a better bioseq than "h".
CSeq_id::E_Choice Which(void) const
void Relock(const CSeq_id_Info *info) const
set< CSeq_id_Handle > TMatches
string GetLabel(const CSeq_id &id)
CConstRef< CSeq_id > GetSeqId(void) const
CSeq_id_Mapper & GetMapper(void) const
bool IsLocked(void) const
TGi GetGi(void) const
TPacked GetPacked(void) const
ESNPScaleLimit
SNP annotation scale limits.
Definition: Seq_id.hpp:847
bool HaveMatchingHandles(void) const
@ eAllowWeakMatch
@ eNoWeakMatch
void Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:1439
void Swap(TThisType &ref)
Swaps the pointer with another reference.
Definition: ncbiobj.hpp:1420
void Unlock(const CObject *object) const
Definition: ncbiobj.cpp:1158
void Lock(const CObject *object) const
Definition: ncbiobj.cpp:1143
uint64_t Uint8
8-byte (64-bit) unsigned integer
Definition: ncbitype.h:105
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define END_SCOPE(ns)
End the previously defined scope.
Definition: ncbistl.hpp:75
#define BEGIN_STD_SCOPE
Place it for adding new funtionality to STD scope.
Definition: ncbistl.hpp:92
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define END_STD_SCOPE
End previously defined STD scope.
Definition: ncbistl.hpp:95
#define BEGIN_SCOPE(ns)
Define a new scope.
Definition: ncbistl.hpp:72
IO_PREFIX::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:149
#define NCBI_SEQ_EXPORT
Definition: ncbi_export.h:825
E_Choice
Choice variants.
Definition: Seq_id_.hpp:93
@ e_Gi
GenInfo Integrated Database.
Definition: Seq_id_.hpp:106
static MDB_envinfo info
Definition: mdb_load.c:37
Predicate for sorting CSeq_id_Handles in a defined order.
Definition: type.c:6
#define _ASSERT
Modified on Tue Apr 23 07:40:02 2024 by modify_doxy.py rev. 669887