Debian Codegraph @

global-types-and-parameters.cpp

raw
1 /* BaitFisher (version 1.2.8) a program for designing DNA target enrichment baits
2 * BaitFilter (version 1.0.6) a program for selecting optimal bait regions
3 * Copyright 2013-2017 by Christoph Mayer
4 *
5 * This source file is part of the BaitFisher-package.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with BaitFisher. If not, see <http://www.gnu.org/licenses/>.
19 *
20 *
21 * For any enquiries send an Email to Christoph Mayer
22 * [email protected]
23 *
24 * When publishing work that is based on the results please cite:
25 * Mayer et al. 2016: BaitFisher: A software package for multi-species target DNA enrichment probe design
26 *
27 */
28 #include "global-types-and-parameters.h"
29 #include "tclap/CmdLine.h"
30 #include <string>
31 #include "faststring2.h"
32
33 using namespace TCLAP;
34 using namespace std;
35
36 faststring global_bait_filename;
37 char global_mode;
38 faststring global_blast_exe;
39 faststring global_output_filename;
40
41 double global_blast_min_hit_coverage_of_bait;
42 double global_blast_max_first_hit_evalue;
43 double global_blast_max_second_hit_evalue;
44 faststring global_blast_db;
45
46 faststring global_blast_extra_commandline;
47 faststring global_blast_evalue_commandline;
48
49 unsigned global_thinning_step_width;
50
51 bool global_use_GUI;
52 bool global_stats_mode;
53
54 unsigned global_conversion_mode;
55 faststring global_ProbeID_prefix;
56 unsigned global_verbosity;
57 faststring global_blast_result_file;
58
59
60 bool file_exists(const char* fn)
61 {
62 ifstream is;
63 is.open(fn);
64 if (is.fail())
65 {
66 return false;
67 }
68 is.close();
69 return true;
70 }
71
72
73 bool blast_exe_exists()
74 {
75 faststring cmd = global_blast_exe + " -version 1> /dev/null 2> /dev/null";
76 int err = system(cmd.c_str());
77
78 if (err == 0)
79 return true;
80 else
81 return false;
82 }
83
84 bool blast_db_files_exist(faststring &blast_db_base_name)
85 {
86 faststring tmp;
87
88 tmp = blast_db_base_name + ".nhr";
89 if (!file_exists(tmp.c_str()))
90 return false;
91 tmp = blast_db_base_name + ".nin";
92 if (!file_exists(tmp.c_str()))
93 return false;
94 tmp = blast_db_base_name + ".nsq";
95 if (!file_exists(tmp.c_str()))
96 return false;
97 return true;
98 }
99
100
101 void good_bye_and_exit(FILE *of, int error)
102 {
103 if (error == 0)
104 fprintf(of, "#\n#\n## Finished successfully. ##\n");
105 fprintf(of, "\n" PROGNAME " says goodbye.\n\n");
106 exit(error);
107 }
108
109 void good_bye_and_exit(ostream &os, int error)
110 {
111 if (error == 0)
112 os << "#\n#\n## Finished successfully. ##\n";
113 os << "\n" PROGNAME " says goodbye.\n\n";
114 exit(error);
115 }
116
117
118
119 void init_param()
120 {
121 global_blast_exe = "blastn";
122 global_blast_min_hit_coverage_of_bait = 0;
123 global_blast_max_first_hit_evalue = 0.000001;
124 global_blast_max_second_hit_evalue = 0.000001;
125 global_thinning_step_width = 0;
126
127 global_use_GUI = false;
128 global_stats_mode = false;
129
130 global_mode = 0; // 0: Undefined, 'c': conversion, ....
131 global_conversion_mode = 0; // 0: No conversion, 1: Agilent-Germany
132
133 global_verbosity = 2;
134 }
135
136
137
138 void read_and_init_parameters(int argc, char** argv, ostream &logerr)
139 {
140 init_param();
141
142 try
143 {
144 CmdLine cmd("The " PROGNAME " program has been designed to post process the output of the BaitFisher program in order select appropriate bait regions and to create the final bait set. "
145 "BaitFilter offers several filtering and conversion modes. If multiple filtering steps and a final conversion are required, BaitFilter will have to be started multiple times and the output "
146 "of the different runs are used as input in the next step.\n"
147 "The BaitFisher program designs baits for every locus for which a bait design is possible for a full bait region. A bait region can start at every nucleotide as long as the remaining sequence is long enough. "
148 "This output has to be reduced and the purpose of BaitFilter is to find for each feature, gene or alignment the optimal locus or the optimal loci for the bait regions. "
149 "Before determining the locus with the fewest number of baits or the largest sequence coverage, one might want to determine which baits are expected to bind specifically in a given reference genome. "
150 "This is achieved by conducting a Blast search of the baits against a genome. Baits which are highly similar to at least two loci of the genome can be determined and their bait regions can be removed. "
151 "The blast search result can also be used to specify a minimum hit coverage of the baits in a bait region against the reference genome. "
152 "After removing bait regions at inferior loci, the optimal bait region starting locus (start coordinate) can be inferred with the aid of different criteria in a subsequent run of BaitFilter. "
153 "As input, BaitFilter requires a bait file generated by the BaitFisher program or a BaitFile generated by a previous filtering run of BaitFilter. "
154 "This bait file is specified with the -i command line parameter (see below). Furthermore, the user has to specify an output file name with the -o parameter and a filter mode with "
155 "the -m parameter.\n"
156 "To convert a file to final and uploadable output format, see the -c option below.\n"
157 "To compute a bait file statistics of an input file, see the -S option below.\n"
158 "The different filter modes provided by BaitFilter are the following:\n"
159 "1a) Retain only the best bait locus per alignment file. Criterion: Minimize number of required baits.\n"
160 "1b) Retain only the best bait locus per alignment file. Criterion: Maximize number of sequences.\n"
161 "2a) Retain only best bait locus per feature (requires that features were selected in BaitFisher). Criterion: Minimize number of required baits.\n"
162 "2b) Retain only best bait locus per feature (requires that features were selected in BaitFisher). Criterion: Maximize number of sequences.\n"
163 "3) Use a blast search of the bait sequences against a reference genome to detect putative non-unique target loci. Non unique target sites will have multiple good hits against the reference genome. "
164 " Furthermore, a minimum coverage of the best blast hit of bait sequence against the genome can be specified. Note that all blast modes require additional command line parameters!"
165 " These modes remove bait regions for which multiple good blast hits where found or for which baits have insufficiently long hits. Different versions of this mode are available:\n"
166 " 3a) If a single bait is not unique, remove all bait regions from the current gene.\n"
167 " 3b) If a single bait is not unique, remove all bait regions from the current feature (if applicable).\n"
168 " 3c) If a single bait is not unique, remove only the bait region that contains this bait.\n"
169 " 4) Thin out the given bait file: Retain only every Nth bait region, where N has to be specified by the user. Two submodes are available:\n"
170 " 4a) Thin out bait regions by retaining only every Nth bait region in a bait file. The starting offset will by chosen such that the number of required baits is minimized.\n"
171 " 4b) Thin out bait regions by retaining only every Nth bait region in a bait file. The starting offset will by chosen such that the number of sequences the result is based on is maximized.\n",
172 ' ', VERSION);
173
174
175 ValueArg<string> blast_result_Arg("b", "blast-result-file",
176 "Conducting a blast analysis of all baits against a reference genome can take a long time. If different filtering parameters, "
177 "e.g. different coverage thresholds are to be compared, the same blast has to be done multiple times. "
178 "With this argument, the blast will be skipped and the specified blast result file will be used. "
179 "This option has to be used with caution! No checks are done (so far) to ensure that the blast result file "
180 "corresponds to the specified bait file. "
181 "If a BaitFilter run was conducted which did a blast search, BaitFilter will not delete the blast result file "
182 "after the run was completed. "
183 "The result file with the name blast_result.txt will remain in the working directory. "
184 "It can be moved or renamed and with this option it can be specified as the input file for further BaitFilter runs. "
185 "If you have the slightest doubt whether you are using the correct blast result file, you should not use this option. "
186 "This option is only allowed in modes that would normally do a blast search. This option cannot be specified together "
187 "with the blast-executable, blast-evalue-cutoff, blast-extra-commandline, ref-blast-db options, "
188 "since these are options specific to runs in which a blast search is conducted.",
189 false, "", "string");
190 cmd.add( blast_result_Arg );
191
192 ValueArg<unsigned> verbosity_Arg("", "verbosity",
193 "The verbosity option controls the amount of information " PROGNAME
194 " writes to the console while running. 0: Print only welcome message and essential error messages that lead to exiting the program. "
195 "1: report also warnings, 2: report also progress, 3: report more detailed progress, >10: debug output. Maximum 10000: write all possible diagnostic output."
196 " A value of 2 is required if startup parameters should be reported.\n",
197 false, global_verbosity, "unsigned integer");
198 cmd.add( verbosity_Arg );
199
200 SwitchArg global_stats_Arg("S", "stats", "Compute bait file characteristics for the input file and report these. This mode is automatically used for all modes specified with -m option or the conversion mode "
201 "specified with -c option. "
202 "The purpose of the -S option is to compute stats without having to filter or convert the input file. In particular, the -S mode does not require specifying an output file.\n"
203 "This option has no effect if combined with the -m or -c modes.\n", false);
204 cmd.add( global_stats_Arg );
205
206
207 ValueArg<string> conversion_IDprefix_Arg("", "ID-prefix",
208 "In the conversion mode to the four-column-upload file format, each converted file should get a unique ProbeID prefix, since even among multiple files, "
209 "ProbeIDs are not allowed to be identical. With this option the user is able to specify a prefix string to all probe IDs in the four-column-upload file created by BaitFilter.",
210 false, "", "string");
211 cmd.add( conversion_IDprefix_Arg );
212
213
214 ValueArg<unsigned> thinning_step_width_Arg("t", "thinning-step-width",
215 "Thin out the bait file by retaining only every Nth bait region. The integer after the option specifies the step width N. If one of the modes thin-b (thin-b-old), or"
216 " thin-s (thin-s-old) is active, this option is required, otherwise it is not allowed to set this parameter.",
217 false, global_thinning_step_width, "positive integer");
218 cmd.add( thinning_step_width_Arg );
219
220 ValueArg<string> blast_exe_Arg("B", "blast-executable",
221 "Name of or path+name to the blast executable. Default: blastn. Minimum blast version number: Blast+ 2.2.x. Default: blastn. Cannot be specified together with the blast-result-file option. ",
222 false, global_blast_exe.c_str(), "string");
223 cmd.add( blast_exe_Arg );
224
225 ValueArg<string> blast_evalue_extra_Arg("", "blast-evalue-cutoff",
226 "When conducting a blast search, a maximum E-value can be specified when calling the blast program. "
227 "The effect is that hits with a higher E-value are not reported. "
228 "BaitFilter always specifies such an E-value when calling the blast program. The default E-value passed by BaitFilter "
229 "to the blast program is twice the --blast-second-hit-evalue. "
230 "If a coverage filter is requested the default value is set to 0.001 if twice the value of "
231 "--blast-second-hit-evalue is smaller than 0.001. "
232 "This should guarantee that all hits necessary for the blast and/or coverage filter are found. "
233 "If the user wants to set a different E-value threshold, this can be specified with this option. "
234 "With version 1.0.6 of this program, the value is automatically changed to be larger or equal to 0.001 if the coverage filter is used. This makes the usage of this option unnecessary in most cases.",
235 false, "", "floating point number");
236 cmd.add( blast_evalue_extra_Arg );
237
238 ValueArg<string> blast_extra_commandline_Arg("", "blast-extra-commandline",
239 "When invoking the blast command, extra command line parameters can be passed to the blast program with the aid of this option. As an example, "
240 "this option allows you to specify the number of threads the blast program should use. Example: --blast-extra-commandline \"-num_threads 20\" sets the number of threads to 20. ",
241 false, "", "string");
242 cmd.add( blast_extra_commandline_Arg );
243
244 ValueArg<string> blast_db_Arg("", "ref-blast-db",
245 "Base name to a blast data base file. This name is passed to the blast command. This is the name of the fasta file of your reference genome."
246 " IMPORTANT: The makeblastdb program has to be called before starting the " PROGNAME " program. makeblastdb takes the fasta file and "
247 " creates data base files out of it. Cannot be specified together with the blast-result-file option.",
248 false, "", "string");
249 cmd.add( blast_db_Arg );
250
251 ValueArg<double> blast_hit_coverage_Arg("", "blast-min-hit-coverage-of-baits-in-tiling-stack",
252 "Can be specified together with the following modes (-m option): blast-a, blast-f, blast-l, blast-c. In all these modes, a blast analysis of all baits against a reference genome is conducted. "
253 "This option specifies a minimum query hit coverage which at least one bait has to have in each tiling stack (i.e. the column in the tiling design). Otherwise the bait region is discarded. "
254 "If not specified, no hit coverage is checked. The coverage is determined for each bait by dividing the length of the best hit of this bait against the specified genome "
255 "by the length of this bait. Then the highest coverage is determined for each bait stack of the tiling design. "
256
257 " If this option is used together with another filter, it is important to know the order in which the two are applied, since the order matters for the final result:"
258 // "For the mode options: ab, as, fb, fs the coverage is checked before "
259 // "determining the optimal bait region. "
260 "For the mode options: blast-a, blast-f, blast-l the hit coverage is checked after filtering for baits with multiple "
261 "good hits to the reference genome.", false, global_blast_min_hit_coverage_of_bait, "floating point number");
262 cmd.add( blast_hit_coverage_Arg );
263
264 ValueArg<double> blast_primary_hit_evalue_Arg("", "blast-first-hit-evalue",
265 "Maximum E-value for the first or best hit of the bait against the genome. A bait is characterized to bind ambiguously, if we have at least two good hits to different loci of the genome. "
266 "This option is the E-value threshold for the first/best hit. Default: 0.000001",
267 false, global_blast_max_first_hit_evalue, "floating point number");
268 cmd.add( blast_primary_hit_evalue_Arg );
269
270 ValueArg<double> blast_secondary_hit_evalue_Arg("", "blast-second-hit-evalue",
271 "Maximum E-value for the second or second best hit. A bait is characterised to bind ambiguously, if we have at least two good hits. This option is the E-value threshold for the second best hit to different loci of the genome."
272 "This option is the E-value threshold for the second best hit. Default: 0.000001",
273 false, global_blast_max_second_hit_evalue, "floating point number");
274 cmd.add( blast_secondary_hit_evalue_Arg );
275
276
277 // The most important options must be mentioned first. Not they are specified in (reverse) order:
278
279 // (#4) in option list
280 ValueArg<string> bait_filter_mode_Arg("m","mode",
281 "Apart from the input file option, the mode option is the most important option. This option specifies which filter mode BaitFilter uses. (See the user manual for more details):\n"
282 "\"ab\": Retain only the best bait locus for each alignment file when using the optimality criterion"
283 " to minimize the total number of required baits.\n"
284 "\"as\": Retain only the best bait locus for each alignment file when using the optimality criterion"
285 " to maximize the number of sequences the result is based on.\n"
286 "\"fb\": Retain only the best bait locus for each feature (e.g. CDS) when using the optimality criterion"
287 " to minimize the total number of required baits. Only applicable if alignment cutting has been used in BaitFisher.\n"
288 "\"fs\": Retain only the best bait locus for each feature (e.g. CDS) when using the optimality criterion"
289 " to maximize the number of sequences the result is based on. Only applicable if alignment cutting has been used in BaitFisher. \n"
290 "\"blast-a\": Remove all bait regions of all ALIGNMENTs for which one or more baits have at least two good hits to a reference genome. (Not recommended.)\n"
291 "\"blast-f\": Remove all bait regions of all FEATUREs for which one or more baits have at least two good hits to a reference genome. (Not recommended.)\n"
292 "\"blast-l\": Remove only the bait REGIONs that contain a bait that has multiple good hits to a reference genome. (Recommended over blast-f and blast-a.)\n"
293 "\"blast-c\": Conduct a coverage filter run without a search for multiple hits. Requires the blast-min-hit-coverage-of-baits-in-tiling-stack option to be specified.\n"
294 "\"thin-b\": Thin out a bait file to every Nth bait region, by finding the start position that minimizes the number of baits.\n"
295 "\"thin-s\": Thin out a bait file to every Nth bait region, by finding the start position that maximizes the number of sequences.\n"
296 "\"thin-b-old\": Similar to thin-b, but treats all loci as if they come from one alignment file. Identical to behaviour of thin-b in version 1.0.5 or earlier.\n"
297 "\"thin-s-old\": Similar to thin-s, but treats all loci as if they come from one alignment file. Identical to behaviour of thin-b in version 1.0.5 or earlier.\n",
298 false, "","string");
299 cmd.add(bait_filter_mode_Arg);
300
301 // (#3) in option list
302 ValueArg<string> conversion_mode_Arg("c", "convert",
303 "Allows the user to produce the final output file which can be uploaded at a bait producing company. "
304 "In this mode, BaitFilter reads the input bait file and instead of doing a filtering step, it produces a "
305 "custom bait file that can be uploaded at the baits producing company. In order to avoid confusion, "
306 "a filtering step cannot be done in the same run as the conversion. If you want to filter a bait file and "
307 "convert the output, you will need to call this program more than once, first to do the filtering and second to do "
308 "the conversion. Allowed conversion parameters currently are: \"four-column-upload\".\n"
309 "New output formats can be added upon request. Please contact the author: Christoph Mayer, Email: Mayer Christoph <[email protected]>",
310 false, "", "string");
311 cmd.add( conversion_mode_Arg );
312
313 // (#2) in option list
314 ValueArg<string> out_filename_Arg("o", "output-bait-file-name",
315 "Name of the output bait file. All modes, except the conversion mode, produce files in the BaitFisher format.",
316 false, "", "string");
317 cmd.add( out_filename_Arg );
318
319 // (#1) in option list
320 ValueArg<string> bait_filename_Arg("i", "input-bait-file-name",
321 "Name of the input bait locus file. This is the bait file obtained from the BaitFisher program or from a previous filter run with BaitFilter.",
322 true, "", "string");
323 cmd.add( bait_filename_Arg );
324
325
326
327
328 //************************************************
329 cmd.parse( argc, argv );
330 //************************************************
331
332 faststring tmp, tmp1;
333
334 // Assigning parameters to variables:
335 global_bait_filename = bait_filename_Arg.getValue().c_str();
336 tmp = bait_filter_mode_Arg.getValue().c_str();
337 global_output_filename = out_filename_Arg.getValue().c_str();
338 global_blast_exe = blast_exe_Arg.getValue().c_str();
339 global_blast_db = blast_db_Arg.getValue().c_str();
340 global_blast_min_hit_coverage_of_bait = blast_hit_coverage_Arg.getValue();
341 global_blast_max_first_hit_evalue = blast_primary_hit_evalue_Arg.getValue();
342 global_blast_max_second_hit_evalue = blast_secondary_hit_evalue_Arg.getValue();
343
344 global_blast_evalue_commandline = blast_evalue_extra_Arg.getValue().c_str();
345 global_blast_extra_commandline = blast_extra_commandline_Arg.getValue().c_str();
346 global_thinning_step_width = thinning_step_width_Arg.getValue();
347 tmp1 = conversion_mode_Arg.getValue().c_str();
348 global_ProbeID_prefix = conversion_IDprefix_Arg.getValue().c_str();
349 global_verbosity = verbosity_Arg.getValue();
350
351 global_stats_mode = global_stats_Arg.getValue();
352 global_blast_result_file = blast_result_Arg.getValue().c_str();
353
354 if (!global_blast_result_file.empty())
355 {
356 skip_blast = true;
357 if ( !file_exists(global_blast_result_file.c_str()) )
358 {
359 logerr << "Error: You specified a blast result file which does not exist or cannot be opened. Exiting.\n";
360 good_bye_and_exit(stdout, -2);
361 }
362 }
363
364 if (global_output_filename.empty() && (!tmp.empty() || !tmp1.empty()) )
365 {
366 logerr << "Error: The -o parameter, used to specify an output file, is a required parameter for all modes specified with the -m and -c parameter.\n";
367 good_bye_and_exit(stdout, -2);
368 }
369
370 if (tmp.empty() && tmp1.empty() && !global_stats_mode )
371 {
372 logerr << "Error: When using the BaitFilter program you have to specify either the -m (--mode) option, the -c (--convert) option or the -S option.\n";
373 logerr << "These are the three main running modes and one of them has to be chosen.\n";
374 logerr << "See the BaitFilter manual for help. ";
375 logerr << "Type " << PROGNAME << " -h for a quick online help.\n";
376 good_bye_and_exit(stdout, -1);
377 }
378
379 if (!tmp.empty() && !tmp1.empty() )
380 {
381 logerr << "Error: You specified a -m (--mode) option together with the -c (--convert) option.\n";
382 logerr << "Filtering runs and conversion runs cannot be combined in one run, but have to be done consecutively, by invoking " << PROGNAME << " twice.\n";
383 logerr << "See the BaitFilter manual for help.\n";
384 logerr << "Type " << PROGNAME << " -h for a quick online help.\n";
385 good_bye_and_exit(stdout, -1);
386 }
387
388 if (global_blast_max_first_hit_evalue > global_blast_max_second_hit_evalue)
389 {
390 logerr << "Error: You specified that the E-value threshold of the first (best) hit is larger than that of the second hit for a bait against a genome.\n"
391 << "This does not make sense. Equal E-value thresholds for the first and the second hit or a smaller E-value for the first hit are allowed.\n";
392 good_bye_and_exit(stdout, -1);
393 }
394
395 if (!global_blast_result_file.empty() && global_blast_exe != "blastn")
396 {
397 logerr << "Error: You specified a blast result file and a blast executable. However, specifying a blast result file has the effect that no blast analysis is done\n";
398 logerr << "Therefore the two parameters cannot be specified in the same run.\n";
399 good_bye_and_exit(stdout, -1);
400 }
401
402 if (!global_blast_result_file.empty() && !global_blast_db.empty())
403 {
404 logerr << "Error: You specified a blast result file and a blast data base file. However, specifying a blast result file has the effect that no blast analysis is done\n";
405 logerr << "Therefore the two parameters cannot be specified in the same run.\n";
406 good_bye_and_exit(stdout, -1);
407 }
408
409 if (!global_blast_result_file.empty() && !global_blast_extra_commandline.empty() )
410 {
411 logerr << "Error: You specified a blast result file and the blast-extra-commandline option. Since no blast analysis is done when specifying a blast result file, "
412 "no blast-extra-commandline option is allowed if a blast result file is specified.\n";
413 good_bye_and_exit(stdout, -1);
414 }
415
416 if (!global_blast_result_file.empty() && !global_blast_evalue_commandline.empty() )
417 {
418 logerr << "Error: You specified a blast result file and a blast-evalue-cutoff. Since no blast analysis is done when specifying a blast result file, "
419 "no blast-evalue-cutoff option is allowed if a blast result file is specified.\n";
420 good_bye_and_exit(stdout, -1);
421 }
422
423 if (global_stats_mode)
424 {
425 global_mode = 'S';
426 }
427 else if (!tmp.empty())
428 {
429 global_conversion_mode = 0;
430
431 if (tmp == "ab") // Best per alignment, number of baits
432 {
433 global_mode = 'a';
434 }
435 else if (tmp == "as")
436 {
437 global_mode = 'A';
438 }
439 else if (tmp == "fb")
440 {
441 global_mode = 'f';
442 }
443 else if (tmp == "fs")
444 {
445 global_mode = 'F';
446 }
447 else if (tmp == "blast-a")
448 {
449 global_mode = 'b';
450 }
451 else if (tmp == "blast-f")
452 {
453 global_mode = 'B';
454 }
455 else if (tmp == "blast-l")
456 {
457 global_mode = 'x';
458 }
459 else if (tmp == "blast-c")
460 {
461 global_mode = 'C';
462 }
463 else if (tmp == "thin-b-old")
464 {
465 global_mode = 't';
466 if (global_thinning_step_width == 0)
467 {
468 logerr << "Error: In \"thin-b\" mode, the --thinning-step-width is a required parameter and the specified value must be greater than 0,\n\n";
469 good_bye_and_exit(stdout, -11);
470 }
471 }
472 else if (tmp == "thin-s-old")
473 {
474 global_mode = 'T';
475 if (global_thinning_step_width == 0)
476 {
477 logerr << "Error: In \"thin-s-old\" mode, the --thinning-step-width is a required parameter and the specified value must be greater than 0,\n\n";
478 good_bye_and_exit(stdout, -11);
479 }
480 }
481 // BEGIN: Non permanent test code
482 else if (tmp == "thin-b")
483 {
484 global_mode = 'h';
485 if (global_thinning_step_width == 0)
486 {
487 logerr << "Error: In \"thin-b\" mode, the --thinning-step-width is a required parameter and the specified value must be greater than 0,\n\n";
488 good_bye_and_exit(stdout, -11);
489 }
490 }
491 else if (tmp == "thin-s")
492 {
493 global_mode = 'H';
494 if (global_thinning_step_width == 0)
495 {
496 logerr << "Error: In \"thin-s\" mode, the --thinning-step-width is a required parameter and the specified value must be greater than 0,\n\n";
497 good_bye_and_exit(stdout, -11);
498 }
499 }
500 // END: Non permanent test code
501 else
502 {
503 logerr << "Unknown option for parameter -m <string> : " << tmp << " encountered.\n";
504 logerr << "Allowed modes can be viewed with: " << PROGNAME << " -h\n";
505 good_bye_and_exit(stdout, -3);
506 }
507 } // END if (!tmp.empty())
508 else // if ( !tmp1.empty() ) // The if is not necessary, since either tmp or tmp1 are ensured to be non-empty.
509 {
510 global_mode = 'c';
511
512 tmp1.ToLower(); // This argument shall be case insensitive. It is still sufficiently sensitive.
513
514 if (tmp1 == "agilent-germany" || tmp1 == "four-column-upload")
515 {
516 global_conversion_mode = 1;
517 }
518 else
519 {
520 logerr << "Unknown option for parameter -c <string> : " << tmp1 << " encountered.\n";
521 logerr << "Allowed modes can be viewed with: " << PROGNAME << " -h\n";
522 good_bye_and_exit(stdout, -3);
523 }
524 }
525 }
526 catch (ArgException &e)
527 {
528 logerr << "Error: " << e.error() << " for arg " << e.argId() << "\n";
529 good_bye_and_exit(stdout, -1);
530 }
531
532 // Do some checks here:
533
534 // Check whether the blast executable can be found and can be executed.
535 // Check whether the data base file exists.
536
537 if (global_thinning_step_width > 0 && (tolower(global_mode) != 't' && tolower(global_mode) != 'h' ) )
538 {
539 logerr << "Error: The --thinning-step-width option only has an effect in the \"-m thin-b\", \"-m thin-s\", \"-m thin-b-old\" or \"-m thin-s-old\" modes. Specifying this value in other modes is not allowed.\n";
540 good_bye_and_exit(stdout, -12);
541 }
542
543 if (global_mode == 'C' && global_blast_min_hit_coverage_of_bait == 0)
544 {
545 logerr << "Error: You specified the mode -m blast-c but did not specify the blast-min-hit-coverage-of-baits-in-tiling-stack option which is required in this mode. Exiting.\n";
546 good_bye_and_exit(stdout, -14);
547 }
548
549 // In case we have to conduct a blast search:
550 if (global_mode == 'b' || global_mode == 'B' || global_mode == 'x' || global_mode == 'C' )
551 {
552 // #ifndef SKIPBLAST
553 if (!skip_blast)
554 {
555 bool good = blast_exe_exists();
556
557 if (!good)
558 {
559 logerr << "Error: You try to run a blast filter, but the blast executable does not seem to work.\n";
560 logerr << " I was not able to run the blast program with the following command line option:\n";
561 logerr << " " << global_blast_exe << " -version\n";
562 logerr << " This command should not give an error message, but display the version number of blast program.\n";
563 logerr << " Exiting.\n";
564 good_bye_and_exit(stdout, -1);
565 }
566
567 if (global_blast_db.empty())
568 {
569 logerr << "Error: You try to run a blast filter, but the index files of your reference genome is not specified.\n";
570 logerr << " Use the --ref-blast_db option to specify a blast data base index file.\n";
571 good_bye_and_exit(stdout, -1);
572 }
573
574 good = blast_db_files_exist(global_blast_db);
575
576 if (!good)
577 {
578 logerr << "Error: You try to run a blast filter, but the index files of your reference genome does not exist.\n";
579 logerr << " If you only have a fasta file at the moment, you have to use the makeblastdb program which comes with your blast installation.\n";
580 logerr << " The creation of the blast index was successful if you where able to created file with the extension: nin, nsq and nhr.\n";
581 logerr << " Exiting.\n";
582 good_bye_and_exit(stdout, -1);
583 }
584 }
585 }
586 // #endif
587
588 if (!(global_mode == 'b' || global_mode == 'B' || global_mode == 'x' || global_mode == 'C' )) // No blast mode
589 {
590 if (!global_blast_db.empty())
591 {
592 logerr << "Warning: You specified a blast database file, but you run " << PROGNAME
593 << " in a non blast mode. Thus, the specified data base file will not be used.\n";
594 }
595
596 if (!global_blast_result_file.empty())
597 {
598 logerr << "Error: You specified a blast result file together with a filtering mode that does require a blast search. Exiting.\n";
599 good_bye_and_exit(stdout, -1);
600 }
601 }
602
603
604 if ( !global_ProbeID_prefix.empty() && global_mode != 'c')
605 {
606 logerr << "Warning: You specified a ProbeID prefix string, which has an effect only in the conversion mode.\n"
607 << " In non conversion mode this prefix will be ignored.\n";
608 }
609 }
610
611
612
613 void print_parameters(FILE *of, const char *s)
614 {
615 fprintf(of, "%sBaitFilter has been started with the following command line parameters:\n", s);
616 fprintf(of, "%sInput bait file name: %s\n", s, global_bait_filename.c_str());
617 fprintf(of, "%sOutput bait file name: %s\n", s, global_output_filename.c_str());
618 if (global_mode == 'a')
619 {
620 fprintf(of, "%sFilter mode: Retain only the best locus per alignment file; criterion: minimize number of baits.\n", s);
621 }
622 else if (global_mode == 'A')
623 {
624 fprintf(of, "%sFilter mode: Retain only the best locus per alignment file; criterion: maximize sequence coverage in bait region.\n", s);
625 }
626 else if (global_mode == 'f')
627 {
628 fprintf(of, "%sFilter mode: Retain only the best locus per feature; criterion: minimize number of baits in bait region.\n", s);
629 }
630 else if (global_mode == 'F')
631 {
632 fprintf(of, "%sFilter mode: Retain only the best locus per feature; criterion: maximize sequence coverage in bait region.x\n", s);
633 }
634 else if (global_mode == 'b')
635 {
636 fprintf(of, "%sFilter mode: Blast filter: Remove all bait regions from output that belong to the ALIGNMENT in which one bait has at least two good hits to the reference genome.\n", s);
637 }
638 else if (global_mode == 'B')
639 {
640 fprintf(of, "%sFilter mode: Blast filter: Remove all bait regions from output that belong to the FEATURE in which one bait has at least two good hits to the reference genome.\n", s);
641 }
642 else if (global_mode == 'x')
643 {
644 fprintf(of, "%sFilter mode: Blast filter: Remove all bait REGIONs in which one bait has at least two good hits to the reference genome.\n", s);
645 }
646 else if (global_mode == 'C')
647 {
648 fprintf(of, "%sFilter mode: Blast filter: Conduct coverage filter without any other blast filter.\n", s);
649 }
650 else if (global_mode == 't')
651 {
652 fprintf(of, "%sFilter mode: Thinning mode: Retain only every %u th bait region. Choose the starting offset such that the number of baits is minimized.\n", s, global_thinning_step_width);
653 }
654 else if (global_mode == 'T')
655 {
656 fprintf(of, "%sFilter mode: Thinning mode: Retain only every %u th bait region. Choose the starting offset such that the number of sequences the result is based on is maximized.\n", s, global_thinning_step_width);
657 }
658 else if (global_mode == 'h')
659 {
660 fprintf(of, "%sFilter mode: Thinning mode new algorithm: Retain only every %u th bait region. Choose the starting offset such that the number of baits is minimized.\n", s, global_thinning_step_width);
661 }
662 else if (global_mode == 'H')
663 {
664 fprintf(of, "%sFilter mode: Thinning mode new algorithm: Retain only every %u th bait region. Choose the starting offset such that the number of sequences the result is based on is maximized.\n", s, global_thinning_step_width);
665 }
666 else if (global_mode == 'c')
667 {
668 if (global_conversion_mode == 1)
669 {
670 fprintf(of, "%sConversion mode: Convert bait file to four-column-upload format.\n", s);
671 }
672 else
673 {
674 fprintf(of, "%sERROR: Invalid internal conversion mode: %c.\nPlease report this bug.\n", s, global_mode);
675 good_bye_and_exit(stdout, -99);
676 }
677 }
678 else if (global_mode == 'S')
679 {
680 fprintf(of, "%sMode: Prints stats mode.\n", s);
681 }
682 else
683 {
684 fprintf(of, "%sERROR: Invalid internal mode: %c.\n", s, global_mode);
685 good_bye_and_exit(stdout, -99);
686 }
687
688 if (global_mode == 'c' && !global_ProbeID_prefix.empty())
689 {
690 fprintf(of, "%sProbeID suffix string in conversion mode: %s\n", s, global_ProbeID_prefix.c_str());
691 }
692
693 fprintf(of, "%sVerbosity: %d\n", s, global_verbosity);
694
695
696 if (global_mode == 'b' || global_mode == 'B' || global_mode == 'x' || global_mode == 'C')
697 {
698 faststring str;
699
700 fprintf(of, "%sBlast search settings and parameters:\n", s);
701
702 if (!skip_blast)
703 fprintf(of, "%sBlast executable: %s\n", s, global_blast_exe.c_str());
704
705 fprintf(of, "%sBlast data base file name: %s\n", s, global_blast_db.c_str());
706
707 fprintf(of, "%sBlast minimum hit coverage of bait in first hit: %lf\n", s, global_blast_min_hit_coverage_of_bait);
708
709 fprintf(of, "%sBlast maximum E-value of first/best hit: %lg\n", s, global_blast_max_first_hit_evalue);
710 fprintf(of, "%sBlast maximum E-value of second best hit: %lg\n", s, global_blast_max_second_hit_evalue);
711
712 if (!global_blast_extra_commandline.empty())
713 {
714 fprintf(of, "%sBlast extra command line argument: %s\n", s, global_blast_extra_commandline.c_str());
715 }
716 else
717 {
718 fprintf(of, "%sBlast extra command line argument: <not specified>\n", s);
719 }
720
721 if (!global_blast_evalue_commandline.empty())
722 {
723 fprintf(of, "%sBlast E-value cut-off: %s\n", s, global_blast_evalue_commandline.c_str());
724 }
725 else
726 {
727 fprintf(of, "%sBlast E-value cut-off: <not specified>\n", s);
728 }
729
730 if (!global_blast_result_file.empty())
731 {
732 fprintf(of, "%sDo not conduct a new blast search, but use the \n"
733 "%sfollowing existing blast result file: %s\n", s,s, global_blast_result_file.c_str() );
734 }
735
736
737 } // END if (global_mode == 'b' || global_mode == 'B' || global_mode == 'x' || global_mode == 'C')
738 }
739
740
741 void print_parameters(ostream &os, const char *s)
742 {
743 os << s << "BaitFilter has been started with the following command line parameters:\n";
744 os << s << "Input bait file name: " << global_bait_filename.c_str() << '\n';
745 os << s << "Output bait file name: " << global_output_filename.c_str() << '\n';
746 if (global_mode == 'a')
747 {
748 os << s << "Filter mode: Retain only the best locus per alignment file; criterion: minimize number of baits.\n";
749 }
750 else if (global_mode == 'A')
751 {
752 os << s << "Filter mode: Retain only the best locus per alignment file; criterion: maximize sequence coverage in bait region.\n";
753 }
754 else if (global_mode == 'f')
755 {
756 os << s << "Filter mode: Retain only the best locus per feature; criterion: minimize number of baits in bait region.\n";
757 }
758 else if (global_mode == 'F')
759 {
760 os << s << "Filter mode: Retain only the best locus per feature; criterion: maximize sequence coverage in bait region.x\n";
761 }
762 else if (global_mode == 'b')
763 {
764 os << s << "Filter mode: Blast filter: Remove all bait regions from output that belong to the ALIGNMENT in which one bait has at least two good hits to the reference genome.\n";
765 }
766 else if (global_mode == 'B')
767 {
768 os << s << "Filter mode: Blast filter: Remove all bait regions from output that belong to the FEATURE in which one bait has at least two good hits to the reference genome.\n";
769 }
770 else if (global_mode == 'x')
771 {
772 os << s << "Filter mode: Blast filter: Remove all bait REGIONs in which one bait has at least two good hits to the reference genome.\n";
773 }
774 else if (global_mode == 'C')
775 {
776 os << s << "Filter mode: Blast filter: Conduct coverage filter without any other blast filter.\n";
777 }
778 else if (global_mode == 't')
779 {
780 os << s << "Filter mode: Thinning mode: Retain only every " << global_thinning_step_width << " th bait region. Choose the starting offset such that the number of baits is minimized.\n";
781 }
782 else if (global_mode == 'T')
783 {
784 os << s << "Filter mode: Thinning mode: Retain only every " << global_thinning_step_width << " th bait region. Choose the starting offset such that the number of sequences the result is based on is maximized.\n";
785 }
786 else if (global_mode == 'h')
787 {
788 os << s << "Filter mode: Thinning mode new algorithm: Retain only every " << global_thinning_step_width << " th bait region. Choose the starting offset such that the number of baits is minimized.\n";
789 }
790 else if (global_mode == 'H')
791 {
792 os << s << "Filter mode: Thinning mode new algorithm: Retain only every " << global_thinning_step_width << " th bait region. Choose the starting offset such that the number of sequences the result is based on is maximized.\n";
793 }
794 else if (global_mode == 'c')
795 {
796 if (global_conversion_mode == 1)
797 {
798 os << s << "Conversion mode: Convert bait file to four-column-upload format.\n";
799 }
800 else
801 {
802 os << s << "ERROR: Invalid internal conversion mode: " << global_conversion_mode << " .\nPlease report this bug.\n";
803 good_bye_and_exit(cout, -99);
804 }
805 }
806 else if (global_mode == 'S')
807 {
808 os << s << "Mode: Prints stats mode.\n";
809 }
810 else
811 {
812 os << s << "ERROR: Invalid internal mode: " << global_mode << '\n';
813 good_bye_and_exit(cout, -99);
814 }
815
816 if (global_mode == 'c' && !global_ProbeID_prefix.empty())
817 {
818 os << s << "ProbeID suffix string in conversion mode: " << global_ProbeID_prefix.c_str() << '\n';
819 }
820
821 os << s << "Verbosity: " << global_verbosity << '\n';
822
823
824 if (global_mode == 'b' || global_mode == 'B' || global_mode == 'x' || global_mode == 'C')
825 {
826 faststring str;
827
828 os << s << "Blast search settings and parameters:\n";
829 os << s << "Blast executable: " << global_blast_exe.c_str() << '\n';
830 os << s << "Blast data base file name: " << global_blast_db.c_str() << '\n';
831
832 os << s << "Blast minimum hit coverage of bait in first hit: " << global_blast_min_hit_coverage_of_bait << '\n';
833
834 os << s << "Blast maximum E-value of first/best hit: " << global_blast_max_first_hit_evalue << '\n';
835 os << s << "Blast maximum E-value of second best hit: " << global_blast_max_second_hit_evalue << '\n';
836
837 if (!global_blast_extra_commandline.empty())
838 {
839 os << s << "Blast extra command line argument: " << global_blast_extra_commandline.c_str() << '\n';
840 }
841 else
842 {
843 os << s << "Blast extra command line argument: <not specified>\n";
844 }
845
846 if (!global_blast_evalue_commandline.empty())
847 {
848 os << s << "Blast E-value cut-off: " << global_blast_evalue_commandline.c_str() << '\n';
849 }
850 else
851 {
852 os << s << "Blast E-value cut-off: <not specified>\n";
853 }
854
855 if (!global_blast_result_file.empty())
856 {
857 os << s << "Do not conduct a new blast search, but use the " << '\n'
858 << s << "following existing blast result file: " << global_blast_result_file.c_str() << '\n';
859 }
860 } // END if (global_mode == 'b' || global_mode == 'B' || global_mode == 'x' || global_mode == 'C')
861 }
862
863
864
865 void print_calling_command_line(FILE *of, unsigned argc, char ** argv)
866 {
867 faststring cmd;
868
869 for (unsigned i=0; i<argc; ++i)
870 {
871 cmd += argv[i];
872 cmd += " ";
873 }
874
875 fprintf(of, "\nThe BaitFilter program was called with the following command line:\n");
876 fputs(cmd.c_str(), of);
877 fputc('\n',of);
878 }
879
880
881 void print_calling_command_line(ostream &os, unsigned argc, char ** argv)
882 {
883 faststring cmd;
884
885 for (unsigned i=0; i<argc; ++i)
886 {
887 cmd += argv[i];
888 cmd += " ";
889 }
890
891 os << "\nThe BaitFilter program was called with the following command line:\n";
892 os << cmd.c_str() << '\n';
893 }
894
895
896
897 void print_output_creation_message(FILE *of, char *s)
898 {
899 fprintf(of, "%sOutput created by " PROGNAME ", version " VERSION "\n", s);
900 fprintf(of, "%s\n", s);
901 print_parameters(of, s);
902 fprintf(of, "%s\n", s);
903 }
904
905 void print_output_creation_message(ostream &os, char *s)
906 {
907 os << s << "Output created by " PROGNAME ", version " VERSION "\n";
908 os << s << '\n';
909 print_parameters(os, s);
910 os << s << '\n';
911 }