-
Notifications
You must be signed in to change notification settings - Fork 38
/
program.cpp
283 lines (257 loc) · 8.24 KB
/
program.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
/* The MIT License
Copyright (c) 2013 Adrian Tan <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "program.h"
void VTOutput::failure(TCLAP::CmdLineInterface& c, TCLAP::ArgException& e)
{
std::clog << "\n";
std::clog << " " << e.what() << "\n\n";
usage(c);
exit(1);
}
void VTOutput::usage(TCLAP::CmdLineInterface& c)
{
std::string s = "";
std::list<TCLAP::Arg*> args = c.getArgList();
//prints unlabeled arument list first
for (TCLAP::ArgListIterator it = args.begin(); it != args.end(); it++)
{
if (typeid(**it)==typeid(TCLAP::UnlabeledValueArg<std::string>))
{
TCLAP::UnlabeledValueArg<std::string> *i = (TCLAP::UnlabeledValueArg<std::string> *) (*it);
s = i->getName();
}
else if (typeid(**it)==typeid(TCLAP::UnlabeledMultiArg<std::string>))
{
TCLAP::UnlabeledMultiArg<std::string> *i = (TCLAP::UnlabeledMultiArg<std::string> *) (*it);
s = i->getName();
}
}
std::clog << c.getProgramName() << " v" << c.getVersion() << "\n\n";
std::clog << "description : " << c.getMessage() << "\n\n";
std::clog << "usage : vt " << c.getProgramName() << " [options] " << s << "\n\n";
//prints rest of arguments
for (TCLAP::ArgListIterator it = args.begin(); it != args.end(); it++)
{
if (it==args.begin())
{
std::clog << "options : ";
}
else
{
std::clog << " ";
}
if (typeid(**it)==typeid(TCLAP::ValueArg<std::string>) ||
typeid(**it)==typeid(TCLAP::ValueArg<uint32_t>) ||
typeid(**it)==typeid(TCLAP::ValueArg<int32_t>) ||
typeid(**it)==typeid(TCLAP::ValueArg<double>) ||
typeid(**it)==typeid(TCLAP::ValueArg<float>))
{
TCLAP::ValueArg<std::string> *i = (TCLAP::ValueArg<std::string> *) (*it);
std::clog << "-" << i->getFlag()
<< " " << i->getDescription() << "\n";
}
else if (typeid(**it)==typeid(TCLAP::SwitchArg))
{
TCLAP::SwitchArg *i = (TCLAP::SwitchArg *) (*it);
std::clog << "-" << i->getFlag()
<< " " << i->getDescription() << "\n";
}
else if (typeid(**it)==typeid(TCLAP::UnlabeledValueArg<std::string>))
{
//ignored
}
else if (typeid(**it)==typeid(TCLAP::UnlabeledMultiArg<std::string>))
{
//ignored
}
else
{
std::clog << "oops, argument type not handled\n";
}
}
std::clog << "\n";
}
/**
* Parse multiple files from command line unlabeled arguments or -L denoted file list. If both are defined, the files are merged.
*
* @files - file names are stored in this vector
* @argument_files - vector of input files
* @file_list - file names stored in a file
*
*/
void Program::parse_files(std::vector<std::string>& files, const std::vector<std::string>& arg_files, std::string file_list)
{
files.clear();
if (arg_files.size()!=0)
{
files = arg_files;
}
if (file_list != "")
{
htsFile *file = hts_open(file_list.c_str(), "r");
if (file==NULL)
{
std::cerr << "cannot open " << file_list << "\n";
exit(1);
}
kstring_t *s = &file->line;
while (hts_getline(file, '\n', s) >= 0)
{
if (s->s[0]!='#')
{
files.push_back(std::string(s->s));
}
}
hts_close(file);
}
}
/**
* Parse intervals. Processes the interval list first followed by the interval string. Duplicates are dropped.
*
* @intervals - intervals stored in this vector
* @interval_list - file containing intervals
* @interval_string - comma delimited intervals in a string
*
* todo: merge overlapping sites?
*/
void Program::parse_intervals(std::vector<GenomeInterval>& intervals, std::string interval_list, std::string interval_string)
{
intervals.clear();
std::map<std::string, uint32_t> m;
if (interval_list!="")
{
htsFile *file = hts_open(interval_list.c_str(), "r");
if (file)
{
kstring_t *s = &file->line;
while (hts_getline(file, '\n', s)>=0)
{
std::string ss = std::string(s->s);
if (m.find(ss)==m.end())
{
m[ss] = 1;
GenomeInterval interval(ss);
intervals.push_back(interval);
}
}
hts_close(file);
}
}
std::vector<std::string> v;
if (interval_string!="")
split(v, ",", interval_string);
for (uint32_t i=0; i<v.size(); ++i)
{
if (m.find(v[i])==m.end())
{
m[v[i]] = 1;
GenomeInterval interval(v[i]);
intervals.push_back(interval);
}
}
}
/**
* Print reference FASTA file option.
*/
void Program::print_ref_op(const char* option_line, std::string ref_fasta_file)
{
if (ref_fasta_file!="")
{
std::clog << option_line << ref_fasta_file << "\n";
}
}
/**
* Print string option, hide if not present.
*/
void Program::print_str_op(const char* option_line, std::string str_value)
{
if (str_value!="")
{
std::clog << option_line << str_value << "\n";
}
}
/**
* Print intervals option.
*/
void Program::print_int_op(const char* option_line, std::vector<GenomeInterval>& intervals)
{
if (intervals.size()!=0)
{
std::clog << option_line;
for (uint32_t i=0; i<std::min((uint32_t)intervals.size(),(uint32_t)5); ++i)
{
if (i) std::clog << ",";
std::clog << intervals[i].to_string();
}
if (intervals.size()>5)
{
std::clog << " and " << (intervals.size()-5) << " other intervals\n";
}
}
}
/**
* Print input files.
*/
void Program::print_ifiles(const char* option_line, std::vector<std::string>& files)
{
if (files.size()!=0)
{
std::clog << option_line;
for (uint32_t i=0; i<std::min((uint32_t)files.size(),(uint32_t)2); ++i)
{
if (i) std::clog << ",";
std::clog << files[i];
}
std::clog << "\n";
if (files.size()>2)
{
std::clog << " and " << (files.size()-2) << " other files\n";
}
}
}
/**
* Parse samples. Processes the sample list. Duplicates are dropped.
*
* @samples - samples stored in this vector
* @sample_map - samples stored in this map
* @sample_list - file containing sample names
*/
void Program::read_sample_list(std::vector<std::string>& samples, std::string sample_list)
{
samples.clear();
std::map<std::string, int32_t> map;
if (sample_list!="")
{
htsFile *file = hts_open(sample_list.c_str(), "r");
if (file)
{
kstring_t *s = &file->line;
while (hts_getline(file, '\n', s)>=0)
{
std::string ss = std::string(s->s);
if (map.find(ss)==map.end())
{
map[ss] = 1;
samples.push_back(ss);
}
}
hts_close(file);
}
}
}