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

Go to the SVN repository for this file.

1 /* $Id: writer.hpp 99481 2023-04-04 13:16:28Z stakhovv $
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  * Authors: Frank Ludwig
27  *
28  * File Description: Stubs for various writer modules
29  *
30  */
31 
32 #ifndef OBJTOOLS_WRITERS___WRITER__HPP
33 #define OBJTOOLS_WRITERS___WRITER__HPP
34 
35 #include <corelib/ncbistd.hpp>
36 #include <util/icanceled.hpp>
37 #include <objmgr/bioseq_handle.hpp>
40 #include <objmgr/feat_ci.hpp>
41 #include <objmgr/align_ci.hpp>
42 
47 
49 BEGIN_objects_SCOPE
50 
51 class CMappedFeat;
52 
54 {
55 public:
56  CInterruptable(): mpCancelled(nullptr) {};
57  virtual ~CInterruptable() {};
58 
60  ICanceled* pCanceller) { mpCancelled = pCanceller; };
61 
62  bool IsCanceled() const {
63  if (! mpCancelled) {
64  return false;
65  }
66  return mpCancelled->IsCanceled();
67  }
68 
69 protected:
71 };
72 
73 // ============================================================================
74 /// Defines and provides stubs for a general interface to a variety of file
75 /// formatters. These writers take Genbank object in raw or handle form, and
76 /// render them to an output stream in their respective formats.
77 ///
79  public CObject, public CInterruptable
80 // ============================================================================
81 {
82 public:
83  /// Customization flags that are relevant to all CWriterBase derived writers.
84  ///
85  typedef enum {
86  fNormal = 0,
87  fDebugOutput = (1<<0),
88  fWriterBaseLast = fDebugOutput,
89  } TFlags;
90 
91 protected:
93  CNcbiOstream& ostr,
94  unsigned int uFlags=0 ) :
95  m_Os( ostr ),
96  m_uFlags( uFlags ),
97  m_Range(CRange<TSeqPos>::GetWhole()),
98  mpMessageListener(nullptr)
99  {
100  };
101 
102 public:
103  virtual ~CWriterBase()
104  {};
105 
106  /// Write a raw Seq-annot to the internal output stream.
107  /// This implementation will just generate an error and then exit. It should
108  /// be re-implemented in format specific subclasses.
109  /// @param annot
110  /// the Seq-annot object to be written.
111  /// @param name
112  /// parameter describing the object. Handling will be format specific
113  /// @param descr
114  /// parameter describing the object. Handling will be format specific
115  ///
116  virtual bool WriteAnnot(
117  const CSeq_annot& /*annot*/,
118  const string& /*name*/ = "",
119  const string& /*descr*/ = "" )
120  {
121  cerr << "Object type not supported!" << endl;
122  return false;
123  };
124 
125  /// Write a raw Seq-align to the internal output stream.
126  /// This implementation will just generate an error and then exit. It should
127  /// be re-implemented in format specific subclasses.
128  /// @param align
129  /// the Seq-align object to be written.
130  /// @param name
131  /// parameter describing the object. Handling will be format specific.
132  /// @param descr
133  /// parameter describing the object. Handling will be format specific.
134  ///
135  virtual bool WriteAlign(
136  const CSeq_align&,
137  const string& /*name*/ = "",
138  const string& /*descr*/ = "" )
139  {
140  cerr << "Object type not supported!" << endl;
141  return false;
142  };
143 
144  /// Write a Seq-entry handle to the internal output stream.
145  /// This implementation will just generate an error and then exit. It should
146  /// be re-implemented in format specific subclasses.
147  /// @param seh
148  /// the Seq-entry handle to be written.
149  /// @param name
150  /// parameter describing the object. Handling will be format specific.
151  /// @param descr
152  /// parameter describing the object. Handling will be format specific.
153  ///
154  virtual bool WriteSeqEntryHandle(
155  CSeq_entry_Handle /*seh*/,
156  const string& = "",
157  const string& = "" )
158  {
159  cerr << "Object type not supported!" << endl;
160  return false;
161  };
162 
163  /// Write a Bioseq handle to the internal output stream.
164  /// This implementation will just generate an error and then exit. It should
165  /// be re-implemented in format specific subclasses.
166  /// @param bsh
167  /// the Bioseq handle to be written.
168  /// @param name
169  /// parameter describing the object. Handling will be format specific.
170  /// @param descr
171  /// parameter describing the object. Handling will be format specific.
172  ///
173  virtual bool WriteBioseqHandle(
174  CBioseq_Handle /*bsh*/,
175  const string& = "",
176  const string& = "" )
177  {
178  cerr << "Object type not supported!" << endl;
179  return false;
180  };
181 
182  /// Write a Seq-annot handle to the internal output stream.
183  /// This implementation will just generate an error and then exit. It should
184  /// be re-implemented in format specific subclasses.
185  /// @param sah
186  /// the Seq-annot handle to be written.
187  /// @param name
188  /// parameter describing the object. Handling will be format specific.
189  /// @param descr
190  /// parameter describing the object. Handling will be format specific.
191  ///
192  virtual bool WriteSeqAnnotHandle(
193  CSeq_annot_Handle /*sah*/,
194  const string& = "",
195  const string& = "" )
196  {
197  cerr << "Object type not supported!" << endl;
198  return false;
199  };
200 
201  /// Write a file header.
202  /// Header syntax and rules depend on the file format. This do-nothing
203  /// implementation should therefore be re-implemented in format specific
204  /// subclasses.
205  ///
206  virtual bool WriteHeader() { return true; };
207 
208  /// Write a file header, using annotation information.
209  /// Header syntax and rules depend on the file format. This do-nothing
210  /// implementation should therefore be re-implemented in format specific
211  /// subclasses.
212  ///
213  virtual bool WriteHeader(
214  const CSeq_annot&) { return WriteHeader(); };
215 
216  /// Write a file trailer.
217  /// Trailer syntax and rules depend on the file format. This do-nothing
218  /// implementation should therefore be re-implemented in format specific
219  /// subclasses.
220  ///
221  virtual bool WriteFooter() { return true; };
222 
223  // Attach an optional message listener.
224  // This message listener will be used to deal with error and warnings
225  // encountered during input processing.
226  /// @param pMessageListener
227  /// ptr to message listener to attach. nullptr to detach the current
228  /// handler without attaching a new one.
230  CWriterListener* pMessageListener) {mpMessageListener = pMessageListener;};
231 
232  // Report errors and warnings in form as a CWriterMessage
233  // If a message listener is attached then the attached listener will be used
234  // to deal with the message. Otherwise, aa equivalent CObjWriterException will
235  // be thrown.
236  /// @param message
237  /// message to process.
238  virtual void PutMessage(const CWriterMessage& message) {
239  if (mpMessageListener) {
240  mpMessageListener->PutMessage(message);
241  return;
242  }
243  NCBI_THROW(CObjWriterException, eBadInput, message.GetText());
244  };
245 
247  if ( !m_Selector.get() ) {
248  m_Selector.reset(new SAnnotSelector());
250  }
251  return *m_Selector;
252  }
253 
255  return SetAnnotSelector();
256  }
257 
258  virtual CRange<TSeqPos>& SetRange(void) {
259  return m_Range;
260  }
261 
262  virtual const CRange<TSeqPos>& GetRange(void) const {
263  return m_Range;
264  }
265 
266 protected:
268  unsigned int m_uFlags;
269  unique_ptr<SAnnotSelector> m_Selector;
272 };
273 
274 
276 {
277 protected:
278  virtual bool xWriteFeature(CFeat_CI /*feat*/) { return false; };
279 
280 public:
281  virtual ~CFeatWriter(void) = default;
282 
284  auto it = first;
285  while (it) {
286  if (!xWriteFeature(it)) {
287  return false;
288  }
289  ++it;
290  }
291 
292  return true;
293  }
294 };
295 
296 
298 {
299 protected:
300  virtual bool xWriteAlign(
301  const CSeq_align&,
302  const string& ="") = 0;
303 
304 public:
305  virtual ~CAlignWriter(void) = default;
306 
308  auto it = first;
309  while (it) {
310  if (!xWriteAlign(*it)) {
311  return false;
312  }
313  ++it;
314  }
315 
316  return true;
317  }
318 };
319 
320 
321 END_objects_SCOPE
323 
324 #endif // OBJTOOLS_WRITERS___WRITER__HPP
virtual bool xWriteAlign(const CSeq_align &, const string &="")=0
virtual ~CAlignWriter(void)=default
bool WriteAlignments(CAlign_CI first)
Definition: writer.hpp:307
CAlign_CI –.
Definition: align_ci.hpp:63
CBioseq_Handle –.
virtual bool xWriteFeature(CFeat_CI)
Definition: writer.hpp:278
virtual ~CFeatWriter(void)=default
bool WriteFeatures(CFeat_CI &first)
Definition: writer.hpp:283
CFeat_CI –.
Definition: feat_ci.hpp:64
bool IsCanceled() const
Definition: writer.hpp:62
ICanceled * mpCancelled
Definition: writer.hpp:70
virtual ~CInterruptable()
Definition: writer.hpp:57
void SetCanceler(ICanceled *pCanceller)
Definition: writer.hpp:59
CMappedFeat –.
Definition: mapped_feat.hpp:59
CObject –.
Definition: ncbiobj.hpp:180
virtual string GetText(void) const
Definition: message.cpp:90
CRange –.
Definition: Range.hpp:68
CSeq_annot_Handle –.
CSeq_entry_Handle –.
Defines and provides stubs for a general interface to a variety of file formatters.
Definition: writer.hpp:81
virtual bool WriteAnnot(const CSeq_annot &, const string &="", const string &="")
Write a raw Seq-annot to the internal output stream.
Definition: writer.hpp:116
CWriterListener * mpMessageListener
Definition: writer.hpp:271
unsigned int m_uFlags
Definition: writer.hpp:268
virtual ~CWriterBase()
Definition: writer.hpp:103
virtual const CRange< TSeqPos > & GetRange(void) const
Definition: writer.hpp:262
virtual bool WriteFooter()
Write a file trailer.
Definition: writer.hpp:221
CRange< TSeqPos > m_Range
Definition: writer.hpp:270
void SetMessageListener(CWriterListener *pMessageListener)
Definition: writer.hpp:229
virtual bool WriteAlign(const CSeq_align &, const string &="", const string &="")
Write a raw Seq-align to the internal output stream.
Definition: writer.hpp:135
SAnnotSelector & GetAnnotSelector(void)
Definition: writer.hpp:254
virtual bool WriteBioseqHandle(CBioseq_Handle, const string &="", const string &="")
Write a Bioseq handle to the internal output stream.
Definition: writer.hpp:173
virtual bool WriteHeader()
Write a file header.
Definition: writer.hpp:206
unique_ptr< SAnnotSelector > m_Selector
Definition: writer.hpp:269
virtual void PutMessage(const CWriterMessage &message)
Definition: writer.hpp:238
virtual SAnnotSelector & SetAnnotSelector(void)
Definition: writer.hpp:246
virtual bool WriteHeader(const CSeq_annot &)
Write a file header, using annotation information.
Definition: writer.hpp:213
CWriterBase(CNcbiOstream &ostr, unsigned int uFlags=0)
Definition: writer.hpp:92
virtual bool WriteSeqAnnotHandle(CSeq_annot_Handle, const string &="", const string &="")
Write a Seq-annot handle to the internal output stream.
Definition: writer.hpp:192
virtual bool WriteSeqEntryHandle(CSeq_entry_Handle, const string &="", const string &="")
Write a Seq-entry handle to the internal output stream.
Definition: writer.hpp:154
CNcbiOstream & m_Os
Definition: writer.hpp:267
virtual CRange< TSeqPos > & SetRange(void)
Definition: writer.hpp:258
Interface for testing cancellation request in a long lasting operation.
Definition: icanceled.hpp:51
Include a standard set of the NCBI C++ Toolkit most basic headers.
static DLIST_TYPE *DLIST_NAME() first(DLIST_LIST_TYPE *list)
Definition: dlist.tmpl.h:46
unsigned int TSeqPos
Type for sequence locations and lengths.
Definition: ncbimisc.hpp:875
#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
SAnnotSelector & SetSortOrder(ESortOrder sort_order)
Set sort order of annotations.
@ eSortOrder_Normal
default - increasing start, decreasing length
#define NCBI_DEPRECATED
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
IO_PREFIX::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:149
#define NCBI_XOBJWRITE_EXPORT
Definition: ncbi_export.h:1347
#define nullptr
Definition: ncbimisc.hpp:45
SAnnotSelector –.
Modified on Fri Sep 20 14:57:45 2024 by modify_doxy.py rev. 669887