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

Go to the SVN repository for this file.

1 /* $Id: gff_export_job.cpp 47479 2023-05-02 13:24:02Z ucko $
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: Roman Katargin
27 *
28 */
29 
30 #include <ncbi_pch.hpp>
31 
32 #include <corelib/ncbifile.hpp>
33 
35 
36 #include <objmgr/feat_ci.hpp>
37 
39 
42 
44 : CAppJob("GFF3 Export"), m_Params(params)
45 {
46 }
47 
49 {
50  string err_msg;
51 
52  try {
54 
55  if (objects.empty()) {
56  m_Error.Reset(new CAppJobError("No objects specified for export."));
57  return eFailed;
58  }
59 
61  scope->AddDefaults();
62 
63  CNcbiOfstream ostr(m_Params.GetFileName().fn_str());
64 
65  unsigned int flags = 0;
66  if (m_Params.GetExtraQuals())
68 
69  unsigned long depth;
70  m_Params.GetFeatureDepth().ToULong(&depth);
71 
72  CGff3Writer writer(*scope, ostr, flags);
73 
74  writer.WriteHeader();
76  const CSeq_annot* seqAnnot = dynamic_cast<const CSeq_annot*> (it->object.GetPointer());
77  if (seqAnnot) {
78  CRef<CSeq_annot> annot(new CSeq_annot());
79  annot->Assign(*seqAnnot);
80  writer.WriteAnnot(*annot);
81  continue;
82  }
83 
84  const CSeq_align* seqAlign = dynamic_cast<const CSeq_align*> (it->object.GetPointer());
85  if (seqAlign) {
86  writer.WriteAlign(*seqAlign);
87  continue;
88  }
89 
90  const CSeq_entry* seqEntry = dynamic_cast<const CSeq_entry*> (it->object.GetPointer());
91  if (seqEntry) {
92  CSeq_entry_Handle seh = scope->AddTopLevelSeqEntry(*seqEntry);
93  writer.WriteSeqEntryHandle(seh);
94  continue;
95  }
96 
97  const CBioseq* bioseq = dynamic_cast<const CBioseq*> (it->object.GetPointer());
98  if (bioseq) {
99  CBioseq_Handle bsh = scope->AddBioseq(*bioseq);
100  writer.WriteBioseqHandle(bsh);
101  continue;
102  }
103 
104  const CSeq_loc* seqLoc = dynamic_cast<const CSeq_loc*> (it->object.GetPointer());
105  if (seqLoc) {
106  CRef<CSeq_annot> annot(new CSeq_annot());
107 
109  if (depth > 0) {
110  sel.SetResolveDepth(static_cast<int>(depth));
111  if (m_Params.GetExactFlevel())
112  sel.SetExactDepth(true);
113  }
114 
115  CFeat_CI iter(*scope, *seqLoc, sel);
116  for (; iter; ++iter) {
117  CRef<CSeq_feat> feat(new CSeq_feat());
118  feat->Assign(iter->GetOriginalFeature());
119  annot->SetData().SetFtable().push_back(feat);
120  }
121 
122  writer.WriteAnnot(*annot);
123  continue;
124  }
125 
126  const CSeq_id* seqId = dynamic_cast<const CSeq_id*>(it->object.GetPointer());
127  if (seqId) {
128  CBioseq_Handle bsh = scope->GetBioseqHandle(*seqId);
129  writer.WriteBioseqHandle(bsh);
130  continue;
131  }
132  }
133  writer.WriteFooter();
134  }
135  catch (CException& e) {
136  err_msg = "Failed to save file:\n";
137  err_msg += e.GetMsg();
138  }
139 
140  if (err_msg.empty()) {
141  LOG_POST(Info << "CGtfExportJob::Run() Finished " << m_Descr);
142  return eCompleted;
143  } else {
144  m_Error.Reset(new CAppJobError(err_msg));
145  return eFailed;
146  }
147 }
148 
CAppJobError Default implementation for IAppJobError - encapsulates a text error message.
CAppJob - default implementation of IAppJob that could be used as a base class.
CBioseq_Handle –.
CFeat_CI –.
Definition: feat_ci.hpp:64
bool WriteFooter() override
Write a trailer marking the end of a parsing context.
Definition: gff_writer.cpp:346
bool WriteAnnot(const CSeq_annot &annot, const string &asmblyName="", const string &asmblyAccession="") override
Convenience function to render a "naked" Seq-annot.
Definition: gff_writer.cpp:91
bool WriteBioseqHandle(CBioseq_Handle bsh, const string &asmblyName="", const string &asmblyAccession="") override
Write Bioseq contained in given handle Essentially, will write all features that live on the given Bi...
Definition: gff_writer.cpp:192
bool WriteSeqEntryHandle(CSeq_entry_Handle seh, const string &asmblyName="", const string &asmblyAccession="") override
Write Seq-entry contained in a given handle.
Definition: gff_writer.cpp:118
bool WriteAlign(const CSeq_align &, const string &asmblyName="", const string &asmblyAccession="") override
Write a raw Seq-align to the internal output stream.
bool WriteHeader() override
Write a file header.
CGffExportParams m_Params
CGffExportJob(const CGffExportParams &params)
virtual EJobState Run()
Function that does all the useful work, called by the Engine.
bool GetExactFlevel() const
wxString GetFileName() const
wxString GetFeatureDepth() const
bool GetExtraQuals() const
const TConstScopedObjects & GetObjects() const
CScope –.
Definition: scope.hpp:92
CSeq_entry_Handle –.
Definition: Seq_entry.hpp:56
namespace ncbi::objects::
Definition: Seq_feat.hpp:58
static uch flags
static unsigned char depth[2 *(256+1+29)+1]
USING_SCOPE(ncbi::objects)
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
#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
const string & GetMsg(void) const
Get message string.
Definition: ncbiexpt.cpp:461
void Info(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1185
CRef< CAppJobError > m_Error
string m_Descr
mutex to sync our internals
EJobState
Job states (describe FSM)
Definition: app_job.hpp:86
vector< SConstScopedObject > TConstScopedObjects
Definition: objects.hpp:65
@ eCompleted
Definition: app_job.hpp:89
@ eFailed
Definition: app_job.hpp:90
virtual void Assign(const CSerialObject &source, ESerialRecursionMode how=eRecursive)
Set object to copy of another one.
CBioseq_Handle AddBioseq(CBioseq &bioseq, TPriority pri=kPriority_Default, EExist action=eExist_Throw)
Add bioseq, return bioseq handle.
Definition: scope.cpp:530
static CRef< CObjectManager > GetInstance(void)
Return the existing object manager or create one.
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:522
CBioseq_Handle GetBioseqHandle(const CSeq_id &id)
Get bioseq handle by seq-id.
Definition: scope.cpp:95
void AddDefaults(TPriority pri=kPriority_Default)
Add default data loaders from object manager.
Definition: scope.cpp:504
SAnnotSelector & SetExactDepth(bool value=true)
SetExactDepth() specifies that annotations will be searched on the segment level specified by SetReso...
const CSeq_feat & GetOriginalFeature(void) const
Get original feature with unmapped location/product.
SAnnotSelector & SetResolveDepth(int depth)
SetResolveDepth sets the limit of subsegment resolution in searching annotations.
void Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:773
#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::ofstream CNcbiOfstream
Portable alias for ofstream.
Definition: ncbistre.hpp:500
void SetData(TData &value)
Assign a value to Data data member.
Definition: Seq_annot_.cpp:244
Defines classes: CDirEntry, CFile, CDir, CSymLink, CMemoryFile, CFileUtil, CFileLock,...
SAnnotSelector –.
Modified on Wed Apr 17 13:08:55 2024 by modify_doxy.py rev. 669887