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

Go to the SVN repository for this file.

1 #ifndef OBJTOOLS_ALNMGR___ALN_ASN_READER__HPP
2 #define OBJTOOLS_ALNMGR___ALN_ASN_READER__HPP
3 /* $Id: aln_asn_reader.hpp 55179 2012-07-25 15:20:22Z 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 * Authors: Kamen Todorov, NCBI
29 *
30 * File Description:
31 * Reading Seq-aligns from an ASN.1 input stream.
32 *
33 * The stream can be in any CObjectIStream format: text or binary
34 * ASN.1, or XML. If binary, since the type of object cannot be
35 * automatically recognized, the top_level_asn_object needs to be
36 * provided.
37 *
38 * ===========================================================================
39 */
40 
41 
42 #include <corelib/ncbistd.hpp>
43 #include <corelib/ncbiobj.hpp>
44 #include <serial/iterator.hpp>
45 
50 
51 #include <objmgr/scope.hpp>
52 
53 
56 
57 
58 /// Helper class for reading seq-align objects from a CObjectIStream.
60 {
61 public:
62  /// Create alignment reader. If the scope is not null, the loaded seq-entry
63  /// objects are added to the scope.
65  : m_Scope(scope),
67  {
68  }
69 
70  /// Switch verbose report about loaded objects on/off.
71  void SetVerbose(bool verbose = true)
72  {
74  }
75 
76  /// Read all seq-align objects from the stream.
77  /// @param obj_in_stream
78  /// The object stream to read from.
79  /// @param callback
80  /// Callback receiving the loaded seq-aligns. The callback must accept
81  /// const CSeq_align*.
82  /// @param top_level_asn_object
83  /// Name of the top level object type. Required for binary ASN.1 files
84  /// to force the type.
85  template <class TCallback>
86  void Read(CObjectIStream* obj_in_stream,
87  TCallback callback,
88  const string& top_level_asn_object = kEmptyStr)
89  {
90  while ( !obj_in_stream->EndOfData() ) {
91  // determine the ASN.1 object type
92  string obj = obj_in_stream->ReadFileHeader();
93  if ( obj.empty() ) {
94  // auto-detection is not possible in ASN.1 binary mode
95  // hopefully top_level_asn_object was specified by the user
96  if ( top_level_asn_object.empty() ) {
98  "ReadFileHeader() returned empty. "
99  "Binary ASN.1 file? "
100  "Please supply the top_level_asn_object.");
101  }
102  else {
103  obj = top_level_asn_object;
104  }
105  }
106  else {
107  if (!top_level_asn_object.empty() && obj != top_level_asn_object) {
108  // object differs from the specified, skip it
109  continue;
110  }
111  }
112 
115 
116  if (obj == "Seq-entry") {
118  obj_in_stream->Read(Begin(*se), CObjectIStream::eNoFileHeader);
119  if ( m_Scope ) {
121  }
122  for (i = Begin(*se); i; ++i) {
123  if ( CType<CSeq_align>::Match(i) ) {
124  callback(CType<CSeq_align>::Get(i));
125  }
126  }
127  }
128  else if (obj == "Seq-submit") {
130  obj_in_stream->Read(Begin(*ss), CObjectIStream::eNoFileHeader);
132  int tse_cnt = 0;
133  for (i = Begin(*ss); i; ++i) {
134  if ( CType<CSeq_align>::Match(i) ) {
135  callback(CType<CSeq_align>::Get(i));
136  }
137  else if ( CType<CSeq_entry>::Match(i) ) {
138  if ( !(tse_cnt++) ) {
139  //m_Scope.AddTopLevelSeqEntry
140  (*(CType<CSeq_entry>::Get(i)));
141  }
142  }
143  }
144  }
145  else if (obj == "Seq-align") {
147  obj_in_stream->Read(Begin(*sa), CObjectIStream::eNoFileHeader);
148  for (i = Begin(*sa); i; ++i) {
149  if ( CType<CSeq_align>::Match(i) ) {
150  callback(CType<CSeq_align>::Get(i));
151  }
152  }
153  }
154  else if (obj == "Seq-align-set") {
156  obj_in_stream->Read(Begin(*sas), CObjectIStream::eNoFileHeader);
157  for (i = Begin(*sas); i; ++i) {
158  if ( CType<CSeq_align>::Match(i) ) {
159  callback(CType<CSeq_align>::Get(i));
160  }
161  }
162  }
163  else if (obj == "Seq-annot") {
164  CRef<CSeq_annot> san(new CSeq_annot);
165  obj_in_stream->Read(Begin(*san), CObjectIStream::eNoFileHeader);
166  for (i = Begin(*san); i; ++i) {
167  if ( CType<CSeq_align>::Match(i) ) {
168  callback(CType<CSeq_align>::Get(i));
169  }
170  }
171  }
172  else if (obj == "Dense-seg") {
174  obj_in_stream->Read(Begin(*ds), CObjectIStream::eNoFileHeader);
177  sa->SetSegs().SetDenseg(*ds);
178  sa->SetDim(ds->GetDim());
179  callback(sa);
180  }
181  else {
182  if ( obj.empty() ) {
184  "ReadFileHeader() returned empty. "
185  "Binary ASN.1 file? "
186  "Please supply the top_level_asn_object.");
187  }
188  else {
189  cerr << "Don't know how to extract alignments from: " << obj << endl;
190  cerr << "Do you know? Please contact us at aln-mgr@ncbi.nlm.nih.gov." << endl;
191  }
192  return;
193  }
194 
195  if (m_Verbose) {
196  cerr << "Finished reading " << obj << "." << endl;
197  }
198 
199  }
200  }
201 
202 private:
204  bool m_Verbose;
205 };
206 
207 
209 
210 #endif // OBJTOOLS_ALNMGR___ALN_ASN_READER__HPP
CBeginInfo Begin(C &obj)
Get starting point of object hierarchy.
Definition: iterator.hpp:958
USING_SCOPE(objects)
CObjectIStream –.
Definition: objistr.hpp:92
Definition: dbpivot.c:60
void SetDim(TDim value)
Assign a value to Dim data member.
Definition: Seq_align_.hpp:852
CAlnAsnReader(CScope *scope=NULL)
Create alignment reader.
virtual bool EndOfData(void)
Check if there is still some meaningful data that can be read; in text streams this function will ski...
Definition: objistr.cpp:564
Template base class for CTypesIterator and CTypesConstIterator Do not use it directly.
Definition: objecttype.hpp:48
Helper class for reading seq-align objects from a CObjectIStream.
#define NULL
Definition: ncbistd.hpp:225
#define kEmptyStr
Definition: ncbistr.hpp:120
int i
CSeq_entry_Handle AddTopLevelSeqEntry(CSeq_entry &top_entry, TPriority pri=kPriority_Default, EExist action=eExist_Default)
Add seq_entry, default priority is higher than for defaults or loaders Add object to the score with p...
Definition: scope.cpp:479
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:101
void SetType(TType value)
Assign a value to Type data member.
Definition: Seq_align_.hpp:805
virtual string ReadFileHeader(void)
Read file header.
Definition: objistr.cpp:1093
static void AddTo(CTypesIterator &it)
Definition: objecttype.hpp:94
int verbose
Definition: fastme.cpp:45
static C * Get(const CTypesIterator &it)
Definition: objecttype.hpp:116
map< string, string > ss
TDim GetDim(void) const
Get the Dim member data.
Definition: Dense_seg_.hpp:408
Portable reference counted smart and weak pointers using CWeakRef, CRef, CObject and CObjectEx...
CException –.
Definition: ncbiexpt.hpp:709
CScope –.
Definition: scope.hpp:90
void Read(CObjectIStream *obj_in_stream, TCallback callback, const string &top_level_asn_object=kEmptyStr)
Read all seq-align objects from the stream.
Definition: Seq_entry.hpp:55
void SetSegs(TSegs &value)
Assign a value to Segs data member.
Definition: Seq_align_.cpp:309
#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:546
void SetVerbose(bool verbose=true)
Switch verbose report about loaded objects on/off.
void Read(const CObjectInfo &object)
Read object of know type.
Definition: objistr.cpp:923
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:98
User-defined methods of the data storage class.
Modified on Mon Jan 23 17:05:38 2017 by modify_doxy.py rev. 506947