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

Go to the SVN repository for this file.

1 /* $Id: wiggle_export_job.cpp 47038 2022-05-25 19:38:11Z evgeniev $
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: Vladislav Evgeniev
27  *
28  */
29 
30 #include <ncbi_pch.hpp>
31 
32 #include <corelib/ncbifile.hpp>
33 
35 
39 
43 
46 
48 : CAppJob("Wiggle Export"), m_Params(params)
49 {
50 }
51 
52 
54 {
55 
56  CRef<CSeq_annot> annot(0);
57  string bam_data;
58  string bam_index;
59  string assembly;
60  if (!CBamUtils::GetCoverageGraphInfo(seq_annot, bam_data, bam_index, assembly))
61  return annot;
62  string path, file, ext;
63  CFile::SplitPath(bam_data, &path, &file, &ext);
64  if (bam_index.empty()) {
65  bam_index = path + file + ".bai";
66  if (!CFile(bam_index).Exists()) {
67  bam_index = path + file + ".csi";
68  }
69  }
70 
72  for (auto&& graph : seq_annot.GetData().GetGraph()) {
73  if (graph->CanGetComp() && graph->GetComp() == m_Params.GetBinSize())
74  continue;
75  auto id = graph->GetLoc().GetId();
76  _ASSERT(id);
77  if (!id)
78  continue;
79  ids.push_back(ConstRef(id));
80  }
81 
82  if (ids.empty())
83  return annot;
84  x_SetStatusText("Creating coverage graphs");
85  annot = CBamUtils::CreateCoverageGraphs(bam_data, bam_index, file, assembly, m_Params.GetBinSize(), &ids);
86  return annot;
87 }
88 
90 {
91  string err_msg;
92 
93  wxString fileName = m_Params.GetFileName();
94 
95  try {
96  ios::openmode mode = ios::out;
98 
99  CNcbiOfstream os(fileName.fn_str(), mode);
100  CWiggleWriter writer(os);
101  writer.SetCanceler(x_GetICanceled());
102  /*
103  Export by range is not supported yet
104  if (!m_Params.GetFrom().empty() && !m_Params.GetTo().empty()) {
105  auto from = NStr::StringToInt(ToStdString(m_Params.GetFrom()), NStr::fConvErr_NoThrow);
106  auto to = NStr::StringToInt(ToStdString(m_Params.GetTo()), NStr::fConvErr_NoThrow);
107  if (from > 0 && to > 0 && from < to)
108  writer.SetRange().Set(from, to);
109  }
110  */
111 
113  const CSeq_annot *seq_annot = dynamic_cast<const CSeq_annot*>(it->object.GetPointer());
114  if (!seq_annot)
115  continue;
116  CRef<CSeq_annot> new_annot = x_RecalculateGraph(*seq_annot, const_cast<CScope&>(*it->scope.GetPointer()));
117  x_SetStatusText("Exporting data");
118  writer.WriteAnnot(new_annot ? *new_annot : *seq_annot);
119  }
120  writer.WriteFooter();
121  }
122  catch (CException& e) {
123  err_msg = "Failed to save file:\n";
124  err_msg += e.GetMsg();
125  }
126 
127  if (err_msg.empty()) {
128  LOG_POST(Info << "CWiggleExportJob::Run() Finished " << m_Descr);
129  return eCompleted;
130  } else {
131  m_Error.Reset(new CAppJobError(err_msg));
132  return eFailed;
133  }
134 }
135 
User-defined methods of the data storage class.
User-defined methods of the data storage class.
CAppJobError Default implementation for IAppJobError - encapsulates a text error message.
CAppJob - default implementation of IAppJob that could be used as a base class.
static CRef< objects::CSeq_annot > CreateCoverageGraphs(const string &bam_file, const string &index_file, const string &annot_name, const string &assembly, int bin_size=1000, TSeqIdsVector *ids=0, bool is_esitmated=false, ICanceled *canceled=0)
Definition: bam_utils.cpp:191
static bool GetCoverageGraphInfo(const objects::CSeq_annot &seq_annot, string &bam_data, string &bam_index, string &assembly)
Definition: bam_utils.cpp:261
vector< CConstRef< objects::CSeq_id > > TSeqIdsVector
Definition: bam_utils.hpp:44
CFile –.
Definition: ncbifile.hpp:1605
void SetCanceler(ICanceled *pCanceller)
Definition: writer.hpp:59
CScope –.
Definition: scope.hpp:92
CWiggleExportParams m_Params
CRef< objects::CSeq_annot > x_RecalculateGraph(const objects::CSeq_annot &seq_annot, objects::CScope &scope)
virtual EJobState Run()
Function that does all the useful work, called by the Engine.
CWiggleExportJob(const CWiggleExportParams &params)
wxString GetFileName() const
const TConstScopedObjects & GetObjects()
bool WriteAnnot(const CSeq_annot &, const string &="", const string &="") override
Write a raw Seq-annot to the internal output stream.
bool WriteFooter() override
Write a file trailer.
std::ofstream out("events_result.xml")
main entry point for tests
#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
static void SplitPath(const string &path, string *dir=0, string *base=0, string *ext=0)
Split a path string into its basic components.
Definition: ncbifile.cpp:358
ICanceled * x_GetICanceled()
CRef< CAppJobError > m_Error
string m_Descr
mutex to sync our internals
EJobState
Job states (describe FSM)
Definition: app_job.hpp:86
virtual void x_SetStatusText(const string &text)
vector< SConstScopedObject > TConstScopedObjects
Definition: objects.hpp:65
@ eCompleted
Definition: app_job.hpp:89
@ eFailed
Definition: app_job.hpp:90
CConstRef< C > ConstRef(const C *object)
Template function for conversion of const object pointer to CConstRef.
Definition: ncbiobj.hpp:2024
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
const TGraph & GetGraph(void) const
Get the variant data.
Definition: Seq_annot_.hpp:661
const TData & GetData(void) const
Get the Data member data.
Definition: Seq_annot_.hpp:873
FILE * file
range(_Ty, _Ty) -> range< _Ty >
mdb_mode_t mode
Definition: lmdb++.h:38
Defines classes: CDirEntry, CFile, CDir, CSymLink, CMemoryFile, CFileUtil, CFileLock,...
#define _ASSERT
USING_SCOPE(objects)
Modified on Sun Jul 21 04:19:40 2024 by modify_doxy.py rev. 669887