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

Go to the SVN repository for this file.

1 /* $Id: demo_genomic_compart.cpp 92522 2021-01-26 20:12:41Z grichenk $
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: Denis Vakatov, Vladimir Ivanov
27  *
28  * File Description:
29  * Sample for the command-line arguments' processing ("ncbiargs.[ch]pp"):
30  *
31  */
32 
33 #include <ncbi_pch.hpp>
34 #include <corelib/ncbiapp.hpp>
35 #include <corelib/ncbienv.hpp>
36 #include <corelib/ncbiargs.hpp>
37 
39 
41 
42 #include <serial/serial.hpp>
43 #include <serial/objistr.hpp>
44 
46 #include <objmgr/scope.hpp>
47 #include <objmgr/bioseq_handle.hpp>
49 
52 
53 
54 /////////////////////////////////////////////////////////////////////////////
55 // CTestCompartApplication::
56 
57 
59 {
60 private:
61  virtual void Init(void);
62  virtual int Run(void);
63  virtual void Exit(void);
64 };
65 
66 
67 /////////////////////////////////////////////////////////////////////////////
68 // Init test for all different types of arguments
69 
70 
72 {
73  // Create command-line argument descriptions class
74  unique_ptr<CArgDescriptions> arg_desc(new CArgDescriptions);
75 
76  // Specify USAGE context
77  arg_desc->SetUsageContext(GetArguments().GetProgramBasename(),
78  "CArgDescriptions demo program");
79 
80  arg_desc->AddDefaultKey("i", "InputFile",
81  "File of ASN text Seq-aligns",
83  "-");
84 
85  arg_desc->AddDefaultKey("o", "OutputFile",
86  "File of results",
88  "-");
89 
90  arg_desc->AddFlag("allow-intersect",
91  "Permit compartments to contain intersecting alignments "
92  "provided the alignments are consistent");
93 
94  arg_desc->AddFlag("allow-intersect-query",
95  "Permit compartments to contain intersecting alignments "
96  "for the query only");
97 
98  arg_desc->AddFlag("allow-intersect-subject",
99  "Permit compartments to contain intersecting alignments "
100  "for the subject only");
101 
102  arg_desc->AddFlag("allow-intersect-both",
103  "Permit compartments to contain intersecting alignments "
104  "only if both query and subject intersect");
105 
106  // Setup arg.descriptions for this application
107  SetupArgDescriptions(arg_desc.release());
108 }
109 
110 
112 {
113  // Get arguments
114  const CArgs& args = GetArgs();
115 
118  CRef<CScope> scope(new CScope(*om));
119  scope->AddDefaults();
120 
121  list< CRef<CSeq_align> > aligns;
122  {{
123  CNcbiIstream& istr = args["i"].AsInputFile();
124  unique_ptr<CObjectIStream> is
126  while ( !is->EndOfData() ) {
127  CRef<CSeq_align> align(new CSeq_align);
128  *is >> *align;
129  aligns.push_back(align);
130  }
131  }}
132 
133  CNcbiOstream& ostr = args["o"].AsOutputFile();
134 
136  if (args["allow-intersect"]) {
138  }
139  if (args["allow-intersect-query"]) {
141  }
142  if (args["allow-intersect-subject"]) {
144  }
145  if (args["allow-intersect-both"]) {
147  }
148 
149  list< CRef<CSeq_align_set> > compartments;
150  FindCompartments(aligns, compartments, opts);
151 
152  LOG_POST(Error << "input alignments: " << aligns.size());
153  LOG_POST(Error << "output compartments: " << compartments.size());
154 
155  size_t count = 0;
156  NON_CONST_ITERATE (list< CRef<CSeq_align_set> >, i, compartments) {
157  ++count;
158  CSeq_align align;
159  align.SetSegs().SetDisc(**i);
160 
161  TSeqRange r1 = align.GetSeqRange(0);
162  TSeqRange r2 = align.GetSeqRange(1);
165 
166  LOG_POST(Error << "compartment " << count << ":");
167  LOG_POST(Error << " query = " << id1 << " (" << r1.GetFrom() + 1 << ".." << r1.GetTo() + 1 << ")");
168  LOG_POST(Error << " subject = " << id2 << " (" << r2.GetFrom() + 1 << ".." << r2.GetTo() + 1 << ")");
169  LOG_POST(Error << " alignments = " << (*i)->Get().size());
170 
171  string title("Compartment ");
172  title += NStr::NumericToString(count);
173  CSeq_annot annot;
174  annot.SetNameDesc(title);
175  annot.SetTitleDesc(title);
176  annot.SetData().SetAlign() = (*i)->Get();
177  ostr << MSerial_AsnText << annot;
178  }
179 
180  return 0;
181 }
182 
183 
184 /////////////////////////////////////////////////////////////////////////////
185 // Cleanup
186 
187 
189 {
190  SetDiagStream(0);
191 }
192 
193 
194 /////////////////////////////////////////////////////////////////////////////
195 // MAIN
196 
197 
198 int main(int argc, const char* argv[])
199 {
200  // Execute main application function
201  return CTestCompartApplication().AppMain(argc, argv);
202 }
CArgDescriptions –.
Definition: ncbiargs.hpp:541
CArgs –.
Definition: ncbiargs.hpp:379
static TRegisterLoaderInfo RegisterInObjectManager(CObjectManager &om, CReader *reader=0, CObjectManager::EIsDefault is_default=CObjectManager::eDefault, CObjectManager::TPriority priority=CObjectManager::kPriority_NotSet)
Definition: gbloader.cpp:366
CScope –.
Definition: scope.hpp:92
CRange< TSeqPos > GetSeqRange(TDim row) const
GetSeqRange NB: On a Spliced-seg, in case the product-type is protein, these only return the amin par...
Definition: Seq_align.cpp:153
const CSeq_id & GetSeq_id(TDim row) const
Get seq-id (the first one if segments have different ids).
Definition: Seq_align.cpp:317
void SetNameDesc(const string &name)
Definition: Seq_annot.cpp:66
void SetTitleDesc(const string &title)
Definition: Seq_annot.cpp:96
virtual int Run(void)
Run the application.
virtual void Init(void)
Initialize the application.
virtual void Exit(void)
Cleanup on application exit.
USING_SCOPE(objects)
int main(int argc, const char *argv[])
void FindCompartments(const list< CRef< CSeq_align > > &aligns, list< CRef< CSeq_align_set > > &align_sets, TCompartOptions options=fCompart_Defaults, float diff_len_filter=3.0f)
int TCompartOptions
@ fCompart_Defaults
@ fCompart_AllowIntersectionsSubject
@ fCompart_AllowIntersectionsBoth
@ fCompart_AllowIntersections
@ fCompart_AllowIntersectionsQuery
virtual const CArgs & GetArgs(void) const
Get parsed command line arguments.
Definition: ncbiapp.cpp:305
int AppMain(int argc, const char *const *argv, const char *const *envp=0, EAppDiagStream diag=eDS_Default, const char *conf=NcbiEmptyCStr, const string &name=NcbiEmptyString)
Main function (entry point) for the NCBI application.
Definition: ncbiapp.cpp:819
virtual void SetupArgDescriptions(CArgDescriptions *arg_desc)
Setup the command line argument descriptions.
Definition: ncbiapp.cpp:1195
#define NON_CONST_ITERATE(Type, Var, Cont)
Non constant version of ITERATE macro.
Definition: ncbimisc.hpp:822
const CNcbiArguments & GetArguments(void) const
Get the application's cached unprocessed command-line arguments.
@ eInputFile
Name of file (must exist and be readable)
Definition: ncbiargs.hpp:595
@ eOutputFile
Name of file (must be writable)
Definition: ncbiargs.hpp:596
#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
void SetDiagStream(CNcbiOstream *os, bool quick_flush=true, FDiagCleanup cleanup=0, void *cleanup_data=0, const string &stream_name="")
Set diagnostic stream.
Definition: ncbidiag.cpp:8083
void Error(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1197
#define MSerial_AsnText
I/O stream manipulators –.
Definition: serialbase.hpp:696
@ eSerial_AsnText
ASN.1 text.
Definition: serialdef.hpp:73
static CSeq_id_Handle GetHandle(const CSeq_id &id)
Normal way of getting a handle, works for any seq-id.
static CObjectIStream * Open(ESerialDataFormat format, CNcbiIstream &inStream, bool deleteInStream)
Create serial object reader and attach it to an input stream.
Definition: objistr.cpp:195
static CRef< CObjectManager > GetInstance(void)
Return the existing object manager or create one.
void AddDefaults(TPriority pri=kPriority_Default)
Add default data loaders from object manager.
Definition: scope.cpp:504
IO_PREFIX::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:149
IO_PREFIX::istream CNcbiIstream
Portable alias for istream.
Definition: ncbistre.hpp:146
static enable_if< is_arithmetic< TNumeric >::value||is_convertible< TNumeric, Int8 >::value, string >::type NumericToString(TNumeric value, TNumToStringFlags flags=0, int base=10)
Convert numeric value to string.
Definition: ncbistr.hpp:673
TTo GetTo(void) const
Get the To member data.
Definition: Range_.hpp:269
TFrom GetFrom(void) const
Get the From member data.
Definition: Range_.hpp:222
void SetSegs(TSegs &value)
Assign a value to Segs data member.
Definition: Seq_align_.cpp:310
void SetData(TData &value)
Assign a value to Data data member.
Definition: Seq_annot_.cpp:244
int i
Defines the CNcbiApplication and CAppException classes for creating NCBI applications.
Defines command line argument related classes.
Defines unified interface to application:
The Object manager core.
CRef< objects::CObjectManager > om
Modified on Thu Apr 25 08:16:43 2024 by modify_doxy.py rev. 669887