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

Go to the SVN repository for this file.

1 /* $Id: cav_main.cpp 67329 2015-05-08 11:30:55Z thiessen $
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: Paul Thiessen
27 *
28 * File Description:
29 * Main application class for CDDAlignmentViewer
30 *
31 * ===========================================================================
32 */
33 
34 #include <ncbi_pch.hpp>
35 #include <corelib/ncbienv.hpp>
36 #include <corelib/ncbiargs.hpp>
37 #include <corelib/ncbi_limits.h>
38 
41 
42 
44 
45 void CAVApp::Init(void)
46 {
47  // create command-line argument descriptions
48  CArgDescriptions *argDescr = new CArgDescriptions();
49 
50  // usage
51  argDescr->SetUsageContext(GetArguments().GetProgramName(), "CDD Alignment Viewer");
52 
53  // output type (required)
54  argDescr->AddKey("type", "type", "one of: 'text' (unformatted), 'HTML', or 'FASTA'", argDescr->eString);
55  argDescr->SetConstraint("type", (new CArgAllow_Strings())->Allow("text")->Allow("HTML")->Allow("FASTA"));
56 
57  // lowercase flag for FASTA
58  argDescr->AddFlag("fasta_lc", "whether to show unaligned residues in lowercase in FASTA output");
59 
60  // paragraph width (optional, default 60)
61  argDescr->AddDefaultKey("width", "integer", "paragraph width", argDescr->eInteger, "60");
62  argDescr->SetConstraint("width", new CArgAllow_Integers(1, kMax_Int));
63 
64  // conservation threshhold (optional, default 2.0)
65  argDescr->AddDefaultKey("cons", "bits", "conservation threshhold (bit score)", argDescr->eDouble, "2.0");
66  argDescr->AddFlag("identity", "show identity, ignoring bit score");
67 
68  // whether to output left/right tails
69  argDescr->AddFlag("lefttails", "whether to show left tails");
70  argDescr->AddFlag("righttails", "whether to show right tails");
71 
72  // whether to do condensed display
73  argDescr->AddFlag("condensed", "condensed incompletely aligned columns (text/HTML only)");
74 
75  // don't use colored backgrounds
76  argDescr->AddFlag("no_color_bg", "don't use colored backgrounds for alignment paragraphs (HTML only)");
77 
78  // ignore bad pairwise alignments
79  argDescr->AddFlag("ignore_bad_aln", "ignore invalid pairwise alignments in input data");
80 
81  // input file name (required)
82  argDescr->AddPositional("in", "name of input file", argDescr->eString);
83 
84  SetupArgDescriptions(argDescr);
85 }
86 
87 int CAVApp::Run(void)
88 {
89  // process arguments
90  const CArgs& args = GetArgs();
91 
92  // open input file
93  CNcbiIfstream *asnIfstream = new CNcbiIfstream(args["in"].AsString().c_str(), IOS_BASE::in | IOS_BASE::binary);
94  if (!asnIfstream) {
95  ERR_POST(Error << "Can't open input file " << args["in"].AsString());
96  return -1;
97  }
98 
99  // load input file into memory
100  string asnString;
101  static const int bufSize = 65536;
102  char buf[bufSize];
103  asnString.resize(bufSize); // start small...
104  int nBytes = 0, n, i;
105  while (!(asnIfstream->eof() || asnIfstream->fail() || asnIfstream->bad())) {
106  asnIfstream->read(buf, bufSize);
107  n = asnIfstream->gcount();
108  if (nBytes+n > (int)asnString.size()) // ... then allocate new memory in 256k chunks
109  asnString.resize(asnString.size() + bufSize*4);
110  for (i=0; i<n; i++) asnString[nBytes + i] = buf[i];
111  nBytes += n;
112  }
113  delete asnIfstream;
114 
115  // process options
116  unsigned int options = CAV_DEBUG;
117 
118  if (args["type"].AsString() == "text")
119  options |= CAV_TEXT;
120  else if (args["type"].AsString() == "HTML")
121  options |= CAV_HTML | CAV_HTML_HEADER;
122  else if (args["type"].AsString() == "FASTA")
123  options |= CAV_FASTA;
124 
125  if (args["lefttails"].HasValue()) options |= CAV_LEFTTAILS;
126  if (args["righttails"].HasValue()) options |= CAV_RIGHTTAILS;
127  if (args["condensed"].HasValue()) options |= CAV_CONDENSED;
128  if (args["identity"].HasValue()) options |= CAV_SHOW_IDENTITY;
129  if (args["fasta_lc"].HasValue()) options |= CAV_FASTA_LOWERCASE;
130  if (args["no_color_bg"].HasValue()) options |= CAV_NO_PARAG_COLOR;
131  if (args["ignore_bad_aln"].HasValue()) options |= CAV_IGNORE_BAD_ALN;
132 
133  // for testing alignment features
134 #if 0
135  AlignmentFeature feats[2];
136  int nFeats = 2;
137 
138  feats[0].nLocations = 90/5;
139  feats[0].locations = new int[90/5];
140  for (i=5; i<=90; i+=5) feats[0].locations[i/5 - 1] = i - 1;
141  feats[0].shortName = "Feature 1";
142  feats[0].description = "Long description of Feature 1";
143  feats[0].featChar = '@';
144 
145  feats[1].nLocations = 90/3;
146  feats[1].locations = new int[90/3];
147  for (i=3; i<=90; i+=3) feats[1].locations[i/3 - 1] = i - 1;
148  feats[1].shortName = "Feature 2";
149  feats[1].description = "Long description of Feature 2";
150  feats[1].featChar = '%';
151 
152  options |= CAV_ANNOT_BOTTOM;
153 
154 #else
155  AlignmentFeature *feats = NULL;
156  int nFeats = 0;
157 #endif
158 
159  // actually do the function
160  return
162  static_cast<const void*>(asnString.data()),
163  asnString.size(),
164  options,
165  args["width"].AsInteger(),
166  args["cons"].AsDouble(),
167  "Alignment - created from command line",
168  nFeats, feats
169  );
170 }
171 
173 
174 
176 
177 int main(int argc, const char* argv[])
178 {
179  SetDiagStream(&NcbiCerr); // send all diagnostic messages to cerr
180  SetDiagPostLevel(eDiag_Info); // show all messages
181 
182  CAVApp app;
183  return app.AppMain(argc, argv, NULL, eDS_Default, NULL); // don't use config file
184 }
#define NcbiCerr
Definition: ncbistre.hpp:399
char * buf
void AddDefaultKey(const string &name, const string &synopsis, const string &comment, EType type, const string &default_value, TFlags flags=0, const string &env_var=kEmptyStr, const char *display_value=nullptr)
Add description for optional key with default value.
Definition: ncbiargs.cpp:2256
int CAV_DisplayMultiple(const void *asnDataBlock, int asnSize, unsigned int options, unsigned int paragraphWidth, double conservationThreshhold, const char *title, int nFeatures, const AlignmentFeature *features)
#define CAV_NO_PARAG_COLOR
Definition: cddalignview.h:60
Defines command line argument related classes.
#define CAV_LEFTTAILS
Definition: cddalignview.h:51
virtual void SetupArgDescriptions(CArgDescriptions *arg_desc)
Setup the command line argument descriptions.
Definition: ncbiapp.cpp:941
CArgAllow_Integers –.
Definition: ncbiargs.hpp:1669
USING_NCBI_SCOPE
Definition: cav_main.cpp:175
#define CAV_TEXT
Definition: cddalignview.h:49
EDiagSev SetDiagPostLevel(EDiagSev post_sev=eDiag_Error)
Set the threshold severity for posting the messages.
Definition: ncbidiag.cpp:5604
Defines unified interface to application:
#define NULL
Definition: ncbistd.hpp:225
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:626
int i
#define CAV_ANNOT_BOTTOM
Definition: cddalignview.h:59
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:101
Convertible into an integer number (int or Int8)
Definition: ncbiargs.hpp:566
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:7514
Informational message.
Definition: ncbidiag.hpp:645
void AddPositional(const string &name, const string &comment, EType type, TFlags flags=0)
Add description for mandatory positional argument.
Definition: ncbiargs.cpp:2284
#define ERR_POST(message)
Error posting with file, line number information but without error codes.
Definition: ncbidiag.hpp:185
void SetConstraint(const string &name, const CArgAllow *constraint, EConstraintNegate negate=eConstraint)
Set additional user defined constraint on argument value.
Definition: ncbiargs.cpp:2404
const char * shortName
Definition: cddalignview.h:69
#define CAV_SHOW_IDENTITY
Definition: cddalignview.h:56
void AddFlag(const string &name, const string &comment, CBoolEnum< EFlagValue > set_value=eFlagHasValueIfSet)
Add description for flag argument.
Definition: ncbiargs.cpp:2273
void AddKey(const string &name, const string &synopsis, const string &comment, EType type, TFlags flags=0)
Add description for mandatory key.
Definition: ncbiargs.cpp:2226
#define CAV_CONDENSED
Definition: cddalignview.h:53
Try standard log file (app.name + ".log") in /log/, use stderr on failure.
Definition: ncbidiag.hpp:1757
#define CAV_IGNORE_BAD_ALN
Definition: cddalignview.h:62
virtual const CArgs & GetArgs(void) const
Get parsed command line arguments.
Definition: ncbiapp.cpp:198
void Init(void)
Initialize the application.
Definition: cav_main.cpp:45
CArgDescriptions –.
Definition: ncbiargs.hpp:514
const char * description
Definition: cddalignview.h:70
An arbitrary string.
Definition: ncbiargs.hpp:563
void Error(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1041
const CNcbiArguments & GetArguments(void) const
Get the application's cached unprocessed command-line arguments.
Definition: ncbiapp.hpp:681
CArgs –.
Definition: ncbiargs.hpp:356
int Run(void)
Run the application.
Definition: cav_main.cpp:87
#define CAV_HTML
Definition: cddalignview.h:50
#define CAV_HTML_HEADER
Definition: cddalignview.h:55
#define CAV_RIGHTTAILS
Definition: cddalignview.h:52
yy_size_t n
void SetUsageContext(const string &usage_name, const string &usage_description, bool usage_sort_args=false, SIZE_TYPE usage_width=78)
Set extra info to be used by PrintUsage().
Definition: ncbiargs.cpp:3110
C interface header for cddalignview as function call.
#define GetProgramName
Avoid name clash with the NCBI C Toolkit.
Definition: ncbienv.hpp:49
IO_PREFIX::ifstream CNcbiIfstream
Portable alias for ifstream.
Definition: ncbistre.hpp:245
std::istream & in(std::istream &in_, double &x_)
int main(int argc, const char *argv[])
Definition: cav_main.cpp:177
Convertible into a floating point number (double)
Definition: ncbiargs.hpp:567
#define kMax_Int
Definition: ncbi_limits.h:184
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:98
#define CAV_FASTA
Definition: cddalignview.h:57
CArgAllow_Strings –.
Definition: ncbiargs.hpp:1566
#define CAV_FASTA_LOWERCASE
Definition: cddalignview.h:58
#define CAV_DEBUG
Definition: cddalignview.h:54
Modified on Wed Nov 22 17:08:43 2017 by modify_doxy.py rev. 546573