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

Go to the SVN repository for this file.

1 #ifndef OBJTOOLS_ALNMGR___ALN_TESTS__HPP
2 #define OBJTOOLS_ALNMGR___ALN_TESTS__HPP
3 /* $Id: aln_tests.hpp 53721 2012-04-12 15:23:21Z grichenk $
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 * Tests on Seq-align containers
32 *
33 * ===========================================================================
34 */
35 
36 
37 #include <corelib/ncbistd.hpp>
38 #include <corelib/ncbiobj.hpp>
39 
42 
43 
46 
47 
48 /// Container mapping seq-aligns to vectors of participating seq-ids.
49 /// TAlnSeqIdExtract is a functor used to extract seq-ids from seq-aligns.
50 /// @sa CAlnSeqIdsExtract
51 /// @sa TAlnIdMap
52 /// @sa TScopeAlnIdMap
53 template <class _TAlnVec,
54  class TAlnSeqIdExtract>
55 class CAlnIdMap : public CObject
56 {
57 public:
58  /// Container (vector) of seq-aligns.
59  typedef _TAlnVec TAlnVec;
60  /// Container (vector) of seq-ids.
61  /// @sa TAlnSeqIdIRef
62  typedef typename TAlnSeqIdExtract::TIdVec TIdVec;
63  typedef TIdVec value_type;
64  typedef size_t size_type;
65 
66 
67  /// Constructor.
68  /// @param extract
69  /// Functor for extracting AlnSeqId from seq-aligns.
70  /// @param expected_number_of_alns
71  /// Hint for optimization - the expected number of alignments.
72  CAlnIdMap(const TAlnSeqIdExtract& extract,
73  size_t expected_number_of_alns = 0)
74  : m_Extract(extract)
75  {
76  m_AlnIdVec.reserve(expected_number_of_alns);
77  m_AlnVec.reserve(expected_number_of_alns);
78  }
79 
80  /// Adding an alignment.
81  /// NB #1: An exception might be thrown here if the alignment's
82  /// seq-ids are inconsistent.
83  /// NB #2: Only seq-ids are validated in release mode. The
84  /// alignment is assumed to be otherwise valid. For
85  /// efficiency (to avoid multiple validation), it is up to
86  /// the user to assure the validity of the alignments.
87  void push_back(const CSeq_align& aln)
88  {
89 #ifdef _DEBUG
90  aln.Validate(true);
91 #endif
93  if (it != m_AlnMap.end()) {
95  eInvalidRequest,
96  "Seq-align was previously pushed_back.");
97  }
98  else {
99  try {
100  size_t aln_idx = m_AlnIdVec.size();
101  m_AlnMap.insert(make_pair(&aln, aln_idx));
102  m_AlnIdVec.resize(aln_idx + 1);
103  m_Extract(aln, m_AlnIdVec[aln_idx]);
104  _ASSERT( !m_AlnIdVec[aln_idx].empty() );
105  }
106  catch (const CAlnException& e) {
107  m_AlnMap.erase(&aln);
108  m_AlnIdVec.pop_back();
110  }
111  m_AlnVec.push_back(CConstRef<CSeq_align>(&aln));
112  }
113  }
114 
115  /// Accessing the vector of alignments
116  const TAlnVec& GetAlnVec(void) const
117  {
118  return m_AlnVec;
119  }
120 
121  /// Accessing the seq-ids of a particular seq-align
122  const TIdVec& operator[](size_t aln_idx) const
123  {
124  _ASSERT(aln_idx < m_AlnIdVec.size());
125  return m_AlnIdVec[aln_idx];
126  }
127 
128  /// Accessing the seq-ids of a particular seq-align
129  const TIdVec& operator[](const CSeq_align& aln) const
130  {
132  if (it == m_AlnMap.end()) {
133  NCBI_THROW(CAlnException, eInvalidRequest,
134  "alignment not present in map");
135  }
136  else {
137  return m_AlnIdVec[it->second];
138  }
139  }
140 
141  /// Size (number of alignments)
142  size_type size(void) const
143  {
144  return m_AlnIdVec.size();
145  }
146 
147 private:
148  const TAlnSeqIdExtract& m_Extract;
149 
152 
153  typedef vector<TIdVec> TAlnIdVec;
155 
157 };
158 
159 
160 /// Default implementations of CAlnIdMap.
163 
164 
166 
167 #endif // OBJTOOLS_ALNMGR___ALN_TESTS__HPP
USING_SCOPE(objects)
CAlnIdMap< vector< const CSeq_align * >, TIdExtract > TAlnIdMap
Default implementations of CAlnIdMap.
Definition: aln_tests.hpp:161
CAlnIdMap< vector< const CSeq_align * >, TScopeIdExtract > TScopeAlnIdMap
Definition: aln_tests.hpp:162
Container mapping seq-aligns to vectors of participating seq-ids.
Definition: aln_tests.hpp:56
TIdVec value_type
Definition: aln_tests.hpp:63
TAlnSeqIdExtract::TIdVec TIdVec
Container (vector) of seq-ids.
Definition: aln_tests.hpp:62
TAlnIdVec m_AlnIdVec
Definition: aln_tests.hpp:154
TAlnVec m_AlnVec
Definition: aln_tests.hpp:156
const TIdVec & operator[](const CSeq_align &aln) const
Accessing the seq-ids of a particular seq-align.
Definition: aln_tests.hpp:129
CAlnIdMap(const TAlnSeqIdExtract &extract, size_t expected_number_of_alns=0)
Constructor.
Definition: aln_tests.hpp:72
const TAlnSeqIdExtract & m_Extract
Definition: aln_tests.hpp:148
size_type size(void) const
Size (number of alignments)
Definition: aln_tests.hpp:142
const TIdVec & operator[](size_t aln_idx) const
Accessing the seq-ids of a particular seq-align.
Definition: aln_tests.hpp:122
void push_back(const CSeq_align &aln)
Adding an alignment.
Definition: aln_tests.hpp:87
map< const CSeq_align *, size_t > TAlnMap
Definition: aln_tests.hpp:150
vector< TIdVec > TAlnIdVec
Definition: aln_tests.hpp:153
_TAlnVec TAlnVec
Container (vector) of seq-aligns.
Definition: aln_tests.hpp:59
size_t size_type
Definition: aln_tests.hpp:64
const TAlnVec & GetAlnVec(void) const
Accessing the vector of alignments.
Definition: aln_tests.hpp:116
TAlnMap m_AlnMap
Definition: aln_tests.hpp:151
IAlnSeqId extracting functor.
CObject –.
Definition: ncbiobj.hpp:180
void Validate(bool full_test=false) const
Definition: Seq_align.cpp:649
void erase(iterator pos)
Definition: map.hpp:167
const_iterator end() const
Definition: map.hpp:152
iterator_bool insert(const value_type &val)
Definition: map.hpp:165
const_iterator find(const key_type &key) const
Definition: map.hpp:153
Include a standard set of the NCBI C++ Toolkit most basic headers.
#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
#define NCBI_EXCEPTION_THROW(exception_var)
Throw an existing exception object.
Definition: ncbiexpt.hpp:688
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
constexpr bool empty(list< Ts... >) noexcept
Portable reference counted smart and weak pointers using CWeakRef, CRef, CObject and CObjectEx.
#define _ASSERT
Modified on Sat Apr 27 11:21:09 2024 by modify_doxy.py rev. 669887