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

Go to the SVN repository for this file.

1 /* $Id: cuSequenceTable.cpp 74244 2016-08-22 15:01:19Z lanczyck $
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: Charlie
27  *
28  * File Description:
29  *
30  * Bioseq table indexed by seq-id
31  *
32  * ===========================================================================
33  */
34 
35 #include <ncbi_pch.hpp>
38 
40 BEGIN_SCOPE(cd_utils)
41 
43 {
44 }
45 
46 void SequenceTable::addSequences(vector< CRef< CBioseq > >& bioseqVec, bool grouped)
47 {
48  if (!grouped)
49  {
50  for (unsigned int i = 0; i < bioseqVec.size(); i++)
51  {
52  addSequence(bioseqVec[i]);
53  }
54  }
55  else
56  {
57  for (unsigned int i = 0; i < bioseqVec.size(); i++)
58  {
59  list< CRef< CSeq_id > >& seqIds = bioseqVec[i]->SetId();
60  for (list< CRef< CSeq_id > >::iterator it= seqIds.begin(); it != seqIds.end(); it++)
61  {
62  for (unsigned int j = 0; j < bioseqVec.size(); j++)
63  m_table.insert(SeqidToBioseqMap::value_type(*it, bioseqVec[j]));
64  }
65  }
66  }
67 }
68 
70 {
71  list< CRef< CSeq_id > >& seqIds = bioseq->SetId();
72  for (list< CRef< CSeq_id > >::iterator it= seqIds.begin(); it != seqIds.end(); it++)
73  {
75  }
76 }
77 
79 {
80  if (seqEntry.IsSet())
81  {
82  list< CRef< CSeq_entry > >& seqSet = seqEntry.SetSet().SetSeq_set();
83  list< CRef< CSeq_entry > >::iterator it = seqSet.begin();
84  for (; it != seqSet.end(); it++)
85  addSequences(*(*it));
86  }
87  else
88  {
89  CRef< CBioseq > bioseq(&(seqEntry.SetSeq()));
90  addSequence(bioseq);
91  }
92 }
93 
95 {
96  m_table.insert(seqTable.m_table.begin(), seqTable.m_table.end());
97 }
98 
100 {
101  bioseqVec.clear();
102  pair<SeqidToBioseqMap::const_iterator, SeqidToBioseqMap::const_iterator> range = m_table.equal_range(seqId);
103  for (SeqidToBioseqMap::const_iterator it = range.first; it!= range.second; it++)
104  bioseqVec.push_back(it->second);
105  return bioseqVec.size();
106 }
107 
109 {
110  vector< CRef< CBioseq > > bioseqVec;
111  if (findSequencesInTheGroup(seqId, bioseqVec) == 0)
112  return false;
113  for (unsigned int i = 0; i < bioseqVec.size(); i++)
114  {
115  const CBioseq::TId& ids = bioseqVec[i]->GetId();
116  CBioseq::TId::const_iterator it = ids.begin(), itend = ids.end();
117  for (; it != itend; ++it)
118  {
119  if ((*it)->Match(*seqId))
120  {
121  bioseq = bioseqVec[i];
122  return true;
123  }
124  }
125  }
126  return false;
127 }
128 
130 {
131  CRef< CBioseq > bioseq;
132  if (findSequence(seqId, bioseq))
133  {
134  seqEntry = new CSeq_entry;
135  seqEntry->SetSeq(*bioseq);
136  return true;
137  }
138  else
139  return false;
140 }
141 
142 void SequenceTable::dump(string filename)
143 {
144  CNcbiOfstream outStream(filename.c_str(), IOS_BASE::out | IOS_BASE::binary);
145  string err;
146  if (!outStream) {
147  err = "Cannot open file for writing";
148  }
150  for (; sit != m_table.end(); sit++)
151  {
152  if (!WriteASNToStream(outStream, *(sit->first), false,&err))
153  LOG_POST("Failed to write to "<<filename<<" because of "<<err);
154  if (!WriteASNToStream(outStream, *(sit->second), false,&err))
155  LOG_POST("Failed to write to "<<filename<< "because of "<<err);
156  }
157 }
158 
159 END_SCOPE(cd_utils)
Definition: Seq_entry.hpp:56
SeqidToBioseqMap m_table
void addSequence(CRef< CBioseq > bioseq)
bool findSequence(CRef< CSeq_id > seqId, CRef< CBioseq > &bioseq) const
void dump(string filename)
unsigned findSequencesInTheGroup(CRef< CSeq_id > seqId, vector< CRef< CBioseq > > &bioseqVec) const
void addSequences(vector< CRef< CBioseq > > &bioseqVec, bool grouped=false)
const_iterator_pair equal_range(const key_type &key) const
Definition: map.hpp:296
const_iterator end() const
Definition: map.hpp:292
iterator insert(const value_type &val)
Definition: map.hpp:305
const_iterator begin() const
Definition: map.hpp:291
static bool WriteASNToStream(ncbi::CNcbiOstream &os, const ASNClass &ASNobject, bool isBinary, std::string *err, ncbi::EFixNonPrint fixNonPrint=ncbi::eFNP_Default)
std::ofstream out("events_result.xml")
main entry point for tests
#define LOG_POST(message)
This macro is deprecated and it's strongly recomended to move in all projects (except tests) to macro...
Definition: ncbidiag.hpp:226
#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_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define BEGIN_SCOPE(ns)
Define a new scope.
Definition: ncbistl.hpp:72
IO_PREFIX::ofstream CNcbiOfstream
Portable alias for ofstream.
Definition: ncbistre.hpp:500
TSet & SetSet(void)
Select the variant.
Definition: Seq_entry_.cpp:130
bool IsSet(void) const
Check if variant Set is selected.
Definition: Seq_entry_.hpp:263
TSeq & SetSeq(void)
Select the variant.
Definition: Seq_entry_.cpp:108
TSeq_set & SetSeq_set(void)
Assign a value to Seq_set data member.
TId & SetId(void)
Assign a value to Id data member.
Definition: Bioseq_.hpp:296
list< CRef< CSeq_id > > TId
Definition: Bioseq_.hpp:94
int i
range(_Ty, _Ty) -> range< _Ty >
Modified on Sat Dec 02 09:23:21 2023 by modify_doxy.py rev. 669887