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

Go to the SVN repository for this file.

1 #include <ncbi_pch.hpp>
2 
3 #include <corelib/ncbiapp.hpp>
4 #include <corelib/ncbienv.hpp>
5 #include <corelib/ncbiargs.hpp>
6 
7 #include <algorithm>
8 
9 #include <corelib/ncbistre.hpp>
10 #include <serial/objostr.hpp>
11 #include <serial/serial.hpp>
15 
17 //#include <objects/seqalign/seqalign__.hpp>
18 
20 USING_SCOPE(prosplign);
21 //USING_SCOPE(objects);
22 
23 /////////////////////////////////////////////////////////////////////////////
24 // CCompartApplication::
25 
26 
28 {
29 private:
30  virtual void Init(void);
31  virtual int Run(void);
32 };
33 
34 
35 /////////////////////////////////////////////////////////////////////////////
36 
38 {
39  // Create command-line argument descriptions class
40  unique_ptr<CArgDescriptions> arg_desc(new CArgDescriptions);
41 
42  // Specify USAGE context
43  arg_desc->SetUsageContext(GetArguments().GetProgramBasename(),
44  "makes compartments from protein BLAST m8");
45 
47 
48  arg_desc->AddDefaultKey("ifmt", "InputFormat",
49  "Format for input",
51  "tabular");
52  arg_desc->SetConstraint("ifmt",
53  &(*new CArgAllow_Strings,
54  "tabular", "seq-align", "seq-align-set", "seq-annot"));
55 
56  arg_desc->AddFlag("hits",
57  "print hits");
58  // Setup arg.descriptions for this application
59  SetupArgDescriptions(arg_desc.release());
60 }
61 
62 
63 
64 void PrintCompartments(int& last_id, const string& query, const string& subj, const TCompartmentStructs& compartments, ostream& ostr)
65 {
66  ITERATE(TCompartmentStructs, i, compartments) {
67  ostr << ++last_id << '\t' << subj << '\t' << query << '\t';
68  ostr << i->from+1 << '\t' << i->to+1 << '\t' << (i->strand?'+':'-') << '\t';
69  ostr << i->covered_aa << '\t' << i->score << '\n';
70  }
71 }
72 
73 void DoCompartments(const CSplign::THitRefs& one_query_subj_pair_hitrefs,
74  const CCompartOptions& compart_options,
75  bool hits, int& last_id, const string& query, const string& subj)
76 {
77  unique_ptr<CCompartmentAccessor<THit> > comps_ptr =
78  CreateCompartmentAccessor(one_query_subj_pair_hitrefs, compart_options);
79  if (comps_ptr.get() == NULL)
80  return;
81 
82  if (!hits) {
83  TCompartments compart_list = FormatAsAsn(comps_ptr.get(), compart_options);
84  TCompartmentStructs compartments = MakeCompartments(compart_list, compart_options);
85  PrintCompartments(last_id, query, subj, compartments, cout);
86  } else {
87  CCompartmentAccessor<THit>& comps = *comps_ptr;
88 
89  THitRefs comphits;
90  if(comps.GetFirst(comphits)) {
91  do {
92  ITERATE(THitRefs, h, comphits) {
93  cout << **h << endl;
94  }
95  } while (comps.GetNext(comphits));
96  }
97  }
98 }
99 
100 string GetSeqIdString(const CSeq_id& id)
101 {
102  return id.IsGi() ? id.AsFastaString() : id.GetSeqIdString(true);
103 }
104 
105 int PdbBadRank(const CRef<CSeq_id>& id)
106 {
107  return (id && id->IsPdb()) ? kMax_Int-1 : CSeq_id::FastaAARank(id);
108 }
109 
111 {
112  // Get arguments
113  const CArgs& args = GetArgs();
114 
115  CCompartOptions compart_options(args);
116  string ifmt = args["ifmt"].AsString();
117  bool hits = args["hits"];
118 
119  int last_id = 0;
120 
121  string buf;
122  string old_q, old_s;
123  CSplign::THitRefs one_query_subj_pair_hitrefs;
124 
125  while(getline(cin, buf)) {
126  CSplign::THitRef hit (new CSplign::THit (buf.c_str(), PdbBadRank));
127  if (hit->GetQueryStrand() == false) {
128  NCBI_THROW(CException, eUnknown, "Reverse strand on protein sequence: "+buf);
129  }
130  string new_q = GetSeqIdString(*hit->GetQueryId());
131  string new_s = GetSeqIdString(*hit->GetSubjId());
132  if (new_q != old_q || new_s != old_s) {
133  DoCompartments(one_query_subj_pair_hitrefs, compart_options, hits, last_id, old_q, old_s);
134 
135  one_query_subj_pair_hitrefs.clear();
136  old_q = new_q;
137  old_s = new_s;
138  }
139  one_query_subj_pair_hitrefs.push_back(hit);
140  }
141 
142  DoCompartments(one_query_subj_pair_hitrefs, compart_options, hits, last_id, old_q, old_s);
143 
144  return 0;
145 }
146 
147 /////////////////////////////////////////////////////////////////////////////
148 // MAIN
149 
150 
151 int main(int argc, const char* argv[])
152 {
153  SetDiagFilter(eDiagFilter_All, "!AddRule()");
154  return CCompartApplication().AppMain(argc, argv);
155 }
CArgAllow_Strings –.
Definition: ncbiargs.hpp:1641
CArgDescriptions –.
Definition: ncbiargs.hpp:541
CArgs –.
Definition: ncbiargs.hpp:379
virtual void Init(void)
Initialize the application.
Definition: compartp.cpp:37
virtual int Run(void)
Run the application.
Definition: compartp.cpp:110
static void SetupArgDescriptions(CArgDescriptions *argdescr)
bool GetFirst(THitRefs &compartment)
Initialize iteration over the results.
bool GetNext(THitRefs &compartment)
Proceed with iteration over the results.
vector< THitRef > THitRefs
Definition: splign.hpp:295
TCompartmentStructs MakeCompartments(const TCompartments &asn_representation, CCompartOptions compart_options)
CSplign::THitRefs THitRefs
vector< SCompartment > TCompartmentStructs
list< CRef< CSeq_annot > > TCompartments
TCompartments FormatAsAsn(CCompartmentAccessor< THit > *comparts_ptr, CCompartOptions compart_options, const vector< pair< THit::TCoord, THit::TCoord > > *gaps=NULL)
unique_ptr< CCompartmentAccessor< THit > > CreateCompartmentAccessor(const THitRefs &orig_hitrefs, CCompartOptions compart_options, const vector< pair< THit::TCoord, THit::TCoord > > *gaps=NULL)
Selects compartments.
USING_SCOPE(prosplign)
int PdbBadRank(const CRef< CSeq_id > &id)
Definition: compartp.cpp:105
void PrintCompartments(int &last_id, const string &query, const string &subj, const TCompartmentStructs &compartments, ostream &ostr)
Definition: compartp.cpp:64
string GetSeqIdString(const CSeq_id &id)
Definition: compartp.cpp:100
int main(int argc, const char *argv[])
Definition: compartp.cpp:151
USING_NCBI_SCOPE
Definition: compartp.cpp:19
void DoCompartments(const CSplign::THitRefs &one_query_subj_pair_hitrefs, const CCompartOptions &compart_options, bool hits, int &last_id, const string &query, const string &subj)
Definition: compartp.cpp:73
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:832
virtual void SetupArgDescriptions(CArgDescriptions *arg_desc)
Setup the command line argument descriptions.
Definition: ncbiapp.cpp:1208
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
const CNcbiArguments & GetArguments(void) const
Get the application's cached unprocessed command-line arguments.
@ eString
An arbitrary string.
Definition: ncbiargs.hpp:589
#define NULL
Definition: ncbistd.hpp:225
void SetDiagFilter(EDiagFilter what, const char *filter_str)
Set diagnostic filter.
Definition: ncbidiag.cpp:7673
@ eDiagFilter_All
for all non-FATAL
Definition: ncbidiag.hpp:2531
#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
static int FastaAARank(const CRef< CSeq_id > &id)
Definition: Seq_id.hpp:778
#define kMax_Int
Definition: ncbi_limits.h:184
bool IsPdb(void) const
Check if variant Pdb is selected.
Definition: Seq_id_.hpp:922
char * buf
int i
Defines the CNcbiApplication and CAppException classes for creating NCBI applications.
Defines command line argument related classes.
Defines unified interface to application:
NCBI C++ stream class wrappers for triggering between "new" and "old" C++ stream libraries.
static string query
Modified on Fri Sep 20 14:57:08 2024 by modify_doxy.py rev. 669887