-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathArgparser.cpp
364 lines (333 loc) · 8.74 KB
/
Argparser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
* Argparser.cpp
*
* Created on: Apr 19, 2016
* Author: tyler
*/
#include "Argparser.h"
#include "generalUtils.h"
#include <iomanip>
#include <cstring>
Argparser::Argparser ()
: _task(0),
_minQ(20),
_Qoffset(33),
_minind(1),
_mincov(1),
_runinfo(1),
_printml(0),
_numericGrad(1),
_allow_overwrite(0),
_verbose(-1),
_is(std::cin),
_os(std::cout.rdbuf()),
_fail(0)
{}
int Argparser::parseInput (const int c, char** v, const char* version)
{
// reads-in and checks input parameters
int argPos = 1;
int increment = 0;
int rv = 0;
if (c < 3)
{
if (c < 2)
{
maininfo(version);
return 1;
}
else
return (help(v[argPos],version));
}
_task = v[argPos];
if ((rv = commandCheck(_task, v[2])) != 0)
return rv;
++argPos;
while (argPos < c)
{
if (strcmp(v[argPos], "-infile") == 0)
{
_infile = (fexists(v[argPos + 1]) || strcmp(v[argPos + 1], "-") == 0) ? v[argPos + 1] : "";
if (_infile.empty())
{
fprintf(stderr, "Couln't open input file %s\n",v[argPos+1]);
return -1;
}
}
else if (strcmp(v[argPos], "-outfile") == 0)
_outfile = v[argPos + 1];
else if (strcmp(v[argPos], "-offsetQ") == 0)
{
_Qoffset = atof(v[argPos + 1]);
if (_Qoffset < 0.0)
{
fprintf(stderr, "-offsetQ must be >= 0.0\n");
return -1;
}
}
else if (strcmp(v[argPos],"-minQ") == 0)
{
_minQ = atof(v[argPos + 1]);
if (_minQ < 0.0)
{
fprintf(stderr,"-minQ must be >= 0.0\n");
return -1;
}
}
else if (strcmp(v[argPos], "-minind") == 0)
{
_minind = atoi(v[argPos + 1]);
if (_minind < 0)
{
fprintf(stderr, "-minind must be a positive integer\n");
return -1;
}
}
else if (strcmp(v[argPos], "-mincov") == 0)
{
_mincov = atoi(v[argPos + 1]);
if (_mincov < 0)
{
fprintf(stderr, "-mincov must be a positive integer\n");
return -1;
}
}
else if (strcmp(v[argPos], "-runinfo") == 0)
{
_runinfo = atoi(v[argPos+1]);
if (_runinfo == 0 || _runinfo == 1 || _runinfo == 2) {} else {
fprintf(stderr, "Must use a valid -runinfo argument of 0, 1, or 2\n");
return -1;
}
}
else if (strcmp(v[argPos], "-printML") == 0)
{
_printml = atoi(v[argPos + 1]);
if (_printml == 1 || _printml == 0)
{}
else
{
fprintf(stderr,"-printML must be enabled with 1 or disabled with 0\n");
return -1;
}
}
else if (strcmp(v[argPos], "-numericGrad") == 0)
{
_numericGrad = atoi(v[argPos + 1]);
if (_numericGrad == 0 || _numericGrad == 1)
{}
else
{
fprintf(stderr,"-numericGrad 1 uses numerical gradient, -numericGrad 0 uses analytic gradient");
return -1;
}
}
else if (strcmp(v[argPos], "-verbose") == 0) {
_verbose = atoi(v[argPos+1]);
}
else if (strcmp(v[argPos], "-allow_overwrite") == 0) {
_allow_overwrite = atoi(v[argPos + 1]);
if (_allow_overwrite == 0 || _allow_overwrite == 1) {
} else {
fprintf(stderr, "-allow_overwrite must be enabled with 1 or disabled with 0\n");
return -1;
}
}
else
{
fprintf(stderr, "Unknown argument: %s\n", v[argPos]);
return -1;
}
argPos += 2 + increment;
increment = 0;
}
return 0;
}
int Argparser::help (const char* arg, const char* version)
{
if (strcmp(arg,"help") == 0 || strcmp(arg, "-help") == 0)
maininfo(version);
else if (strcmp(arg,"calcLR") == 0)
calcLRinfo();
else if (strcmp(arg,"findRegion") == 0)
findRegionInfo();
else
{
fprintf(stderr,"Unknown argument: %s\n",arg);
return -1;
}
return 1;
}
int Argparser::commandCheck(const char* command, const char* arg1)
{
if (strcmp(command,"calcLR") == 0)
{
if (strcmp(arg1,"-help") == 0)
{
calcLRinfo();
return 1;
}
else
return 0;
}
else if (strcmp(command,"findRegion") == 0)
if (strcmp(arg1,"-help") == 0)
{
findRegionInfo();
return 1;
}
else
return 0;
else
fprintf(stderr,"Invalid command: %s\n",command);
return -1;
}
int Argparser::setStreams (const std::string infile, const std::string outfile)
{
// open input stream
if (!infile.empty())
{
if (infile != "-")
{
if (getFILE(_fin, infile.c_str(), "in")) // open the pileup file
{
_is.rdbuf(_fin.rdbuf()); // change input buffer from STDIN to fstream's buffer
std::cerr << "Reading input from " << infile << "\n";
}
else
{
std::cerr << "Problem opening input file " << infile << "\n";
_fail = 1;
return (_fail);
}
}
else
std::cerr << "Reading from standard input\n";
}
else
{
std::cerr << "Problem parsing input stream\n";
_fail=1;
return(_fail);
}
// open output steam
if (!outfile.empty())
{
if (getFILE(_fout, outfile.c_str(), "out", _allow_overwrite))
{
_os.rdbuf(_fout.rdbuf()); // switch output stream's buffer to output file's buffer
std::cerr << "Dumping results to " << outfile << "\n";
}
else
{
std::cerr << "Problem opening output file " << outfile << "\n";
_fail = 1;
return(_fail);
}
}
else
std::cerr << "Dumping results to standard output\n";
return 0;
}
void Argparser::closeStreams ()
{
if (_fin.is_open())
_fin.close();
if (_fout.is_open())
{
_fout.flush();
_fout.close();
}
}
double Argparser::minQ () const
{
return _minQ;
}
double Argparser::offsetQ () const
{
return _Qoffset;
}
unsigned int Argparser::minind () const
{
return _minind;
}
unsigned int Argparser::mincov () const
{
return _mincov;
}
int Argparser::runinfo () const {return _runinfo;}
std::string Argparser::infile_name () const
{
return _infile;
}
std::string Argparser::outfile_name () const
{
return _outfile;
}
std::istream& Argparser::input ()
{
return _is;
}
std::ostream& Argparser::output ()
{
return _os;
}
char* Argparser::task () const
{
return _task;
}
int Argparser::printML() const
{
return _printml;
}
int Argparser::isNumericGrad () const
{
return _numericGrad;
}
int Argparser::verblevel () const
{
return _verbose;
}
int Argparser::fail () const
{
return _fail;
}
void Argparser::maininfo (const char* v)
{
int w = 12;
int w2 = 18;
int w3 = 8;
std::cerr << "\nngsParalog\nversion " << v << "\n\nUsage: ngsParalog [command] [arguments]\n"
<< "\nCommands:\n"
<< "\n" << std::setw(w) << std::left << "calcLR" << "Calculate per site likelihood ratio of duplication"
<< "\n" << std::setw(w) << std::left << "findRegion" << "Find the coordinates of duplicated genomic regions"
<< "\n\nSome general possibly useful arguments:"
<< "\n" << std::setw(w2) << std::left << "-allow_overwrite" << std::setw(w3) << "<0|1>" << "Enable overwriting previous output with 1 [" << _allow_overwrite << "]"
<< "\n\n";
}
void Argparser::calcLRinfo ()
{
int w = 12;
std::cerr << "\nUsage: ngsParalog calcLR [arguments]\n"
<< "\nArguments:\n"
<< "\n" << std::setw(w) << std::left << "-infile" << std::setw(w) << "<string>" << "Pileup format file of reads and quality scores; specifying '-' will read from STDIN [" << _infile << "]"
<< "\n" << std::setw(w) << std::left << "-outfile" << std::setw(w) << "<string>" << "Name of output file, if not provided results printed to STDOUT [" << _outfile << "]"
<< "\n" << std::setw(w) << std::left << "-offsetQ" << std::setw(w) << "<float>" << "Quality score offset [" << _Qoffset << "]"
<< "\n" << std::setw(w) << std::left << "-minQ" << std::setw(w) << "<float>" << "Minimum base quality score [" << _minQ << "]"
<< "\n" << std::setw(w) << std::left << "-minind" << std::setw(w) << "<int>" << "Minimum number of covered individuals to retain site [" << _minind << "]"
<< "\n" << std::setw(w) << std::left << "-mincov" << std::setw(w) << "<int>" << "Minimum number of reads for an individual to be considered 'covered' [" << _mincov << "]"
<< "\n" << std::setw(w) << std::left << "-runinfo" << std::setw(w) << "<int>" << "Controls verbosity. 0: suppress all messages, 1: warnings only, 2: print all run information. [" << _runinfo << "]"
<< "\n\nOutput by field:"
<< "\n(1) sequence ID"
<< "\n(2) position in sequence (1-base indexed)"
<< "\n(3) -log null likelihood"
<< "\n(4) -log alternative likelihood"
<< "\n(5) likelihood ratio of mismapped reads"
<< "\n\n";
}
void Argparser::findRegionInfo ()
{
//int w = 12;
std::cerr << "\nUsage: ngsParalog findRegion [arguments]\n"
<< "\nIn development\n\n";
}