-
Notifications
You must be signed in to change notification settings - Fork 81
/
commander.hpp
899 lines (872 loc) · 31.3 KB
/
commander.hpp
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
// This file is part of PRSice-2, copyright (C) 2016-2019
// Shing Wan Choi, Paul F. O’Reilly
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifndef COMMANDER_H
#define COMMANDER_H
#include "enumerators.h"
#include "gzstream.h"
#include "misc.hpp"
#include "storage.hpp"
#include <chrono>
#include <cmath>
#include <cstring>
#include <ctime>
#include <fstream>
#include <getopt.h>
#include <iostream>
#include <map>
#include <random>
#include <reporter.hpp>
#include <stdexcept>
#include <string>
#include <unistd.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <zlib.h>
#ifdef _WIN32
#include <windows.h>
#endif
const std::string version = "2.3.1.b";
const std::string date = "2020-05-26";
class Commander
{
public:
Commander();
virtual ~Commander();
bool process_command(int argc, char* argv[], Reporter& reporter);
std::string get_base_name() const
{
return misc::remove_extension<std::string>(
misc::base_name<std::string>(m_base_info.file_name));
}
const BaseFile& get_base() const { return m_base_info; }
const CalculatePRS& get_prs_instruction() const { return m_prs_info; }
const Clumping& get_clump_info() const { return m_clump_info; }
const GeneSets& get_set() const { return m_prset; }
const GenoFile& get_target() const { return m_target; }
const GenoFile& get_reference() const { return m_reference; }
const Permutations& get_perm() const { return m_perm_info; }
const Phenotype& get_pheno() const { return m_pheno_info; }
const PThresholding& get_p_threshold() const { return m_p_thresholds; }
const QCFiltering& get_base_qc() const { return m_base_filter; }
const QCFiltering& get_target_qc() const { return m_target_filter; }
const QCFiltering& get_ref_qc() const { return m_ref_filter; }
bool use_ref() const
{
return !(m_reference.file_list.empty()
&& m_reference.file_name.empty());
}
bool need_ref() const
{
return !m_clump_info.no_clump || m_prs_info.use_ref_maf;
}
bool use_inter() const { return m_allow_inter; }
std::string delim() const { return m_id_delim; }
std::string out() const { return m_out_prefix; }
std::string exclusion_range() const { return m_exclusion_range; }
bool all_scores() const { return m_print_all_scores; }
bool print_snp() const { return m_print_snp; }
unsigned long long max_memory(const unsigned long long detected) const
{
return (!m_provided_memory || m_memory > detected) ? detected
: m_memory;
}
unsigned long long memory() const { return m_memory; }
std::string exclude_file() const { return m_exclude_file; }
std::string extract_file() const { return m_extract_file; }
std::string chr_id_formula() const { return m_chr_id_formula; }
bool keep_ambig() const { return m_keep_ambig; }
bool nonfounders() const { return m_include_nonfounders; }
bool ultra_aggressive() const { return m_ultra_aggressive; }
protected:
const std::vector<std::string> supported_types = {"bed", "ped", "bgen"};
std::string m_id_delim = " ";
std::string m_out_prefix = "PRSice";
std::string m_exclusion_range = "";
std::string m_exclude_file = "";
std::string m_extract_file = "";
std::string m_help_message;
std::string m_chr_id_formula;
size_t m_memory = 1e10;
int m_allow_inter = false;
int m_include_nonfounders = false;
int m_keep_ambig = false;
int m_print_all_scores = false;
int m_print_snp = false;
int m_ultra_aggressive = false;
int m_user_no_default = false;
bool m_provided_memory = false;
bool m_set_delim = false;
bool m_ran_base_check = false;
BaseFile m_base_info;
CalculatePRS m_prs_info;
Clumping m_clump_info;
GeneSets m_prset;
GenoFile m_target;
GenoFile m_reference;
Permutations m_perm_info;
QCFiltering m_target_filter;
QCFiltering m_ref_filter;
QCFiltering m_base_filter;
Phenotype m_pheno_info;
PThresholding m_p_thresholds;
std::map<std::string, std::string> m_parameter_log;
std::string m_error_message = "";
////////////////////////////////////////////
//
// end of struct definition
//
////////////////////////////////////////////
/*!
* \brief Parsing all command line arguments
* \param argc
* \param argv
* \param optString is the string containing the short flags
* \param longOpts is the struct containing all the options
* \param reporter is the object to report all messages
* \return true if we want to continue the program
*/
bool init(int argc, char* argv[], bool& early_termination,
Reporter& reporter);
bool parse_command(int argc, char* argv[], const char* optString,
const struct option longOpts[], bool& early_termination,
Reporter& reporter);
void set_help_message();
bool need_target_as_reference();
bool clump_check();
bool ref_check();
bool covariate_check();
std::unordered_set<std::string> get_cov_names();
void
prepare_header_cov_check(const std::vector<std::string>& cov_header,
std::unordered_map<std::string, size_t>& ref_index,
std::unordered_set<std::string>& included);
std::vector<std::string> get_covariate_header();
size_t
find_cov_idx(const std::unordered_set<std::string>& included,
const std::unordered_map<std::string, size_t>& ref_index,
std::string& missing);
void reorganize_cov_name(const std::vector<std::string>& cov_header);
bool
process_factor_cov(const std::unordered_set<std::string>& included,
const std::unordered_map<std::string, size_t>& ref_index,
const std::unordered_set<std::string> ori_input);
bool filter_check();
bool misc_check();
bool prset_check();
bool prsice_check();
bool target_check();
bool pheno_check();
static std::vector<std::string>
transform_covariate(const std::string& cov_in);
static size_t find_first_end(const std::string_view& cov, const size_t idx);
static std::vector<size_t> parse_range(const std::string_view& cov);
static std::vector<size_t> get_range(const std::string_view& cov,
const size_t start, const size_t end);
static void update_covariate_range(const std::vector<size_t>& range,
std::vector<std::string>& res);
/////////////////////////////////////////////////
/// REFACTORED FUNCTIONS
/////////////////////////////////////////////////
inline void set_string(const std::string& input, const std::string& c,
size_t base_index)
{
set_string<int>(input, c, m_base_info.column_name[base_index],
m_base_info.has_column[base_index]);
}
inline void set_string(const std::string& input, const std::string& c,
std::string& target)
{
bool dumy;
set_string<bool>(input, c, target, dumy);
}
template <typename T>
inline void set_string(const std::string& input, const std::string& c,
std::string& target, T& target_boolean,
bool add_quote = false)
{
check_duplicate(c);
std::string in = input;
if (add_quote) { in = "\"" + input + "\""; }
m_parameter_log[c] = in;
target = input;
target_boolean = true;
}
template <typename T>
inline bool convert_to_numeric_vector(const std::vector<std::string>& token,
std::vector<T>& target)
{
if (target.empty())
target.reserve(token.size());
else
target.reserve(target.size() + token.size());
try
{
for (auto&& bar : token)
target.push_back(misc::Convertor::convert<T>(bar));
}
catch (...)
{
return false;
}
return true;
}
template <typename T>
inline bool load_numeric_vector(const std::string& input,
const std::string& c,
std::vector<T>& target)
{
// should always have an input
if (input.empty()) return false;
append_log(c, input);
if (!input.empty() && input.back() == ',')
{
m_error_message.append(
"Warning: , detected at end of input: " + input
+ ". Have you accidentally included space in "
"your input? (Space is not allowed)\n");
}
std::vector<std::string> token = misc::split(input, ",");
if (!convert_to_numeric_vector<T>(token, target))
{
m_error_message.append("Error: Non numeric argument passed to " + c
+ ": " + input + "!\n");
return false;
}
return true;
}
inline bool check_true_false_ending(const std::string& in, size_t& length)
{
length = 1;
if (misc::hasEnding(in, "F")) { return false; }
else if (misc::hasEnding(in, "T"))
{
return true;
}
else if (misc::hasEnding(in, "TRUE"))
{
length = 4;
return true;
}
else if (misc::hasEnding(in, "FALSE"))
{
length = 5;
return false;
}
throw std::runtime_error("Error: Undefined input");
}
size_t number_boolean(const std::string& input, bool& result)
{
size_t bool_length = 0;
try
{
result = check_true_false_ending(input, bool_length);
}
catch (const std::runtime_error& er)
{
throw er;
}
try
{
if (bool_length != input.length())
{
// if the boolean string doesn't take up the whole of the input
// string
size_t num_repeat = misc::convert<size_t>(
input.substr(0, input.length() - bool_length).c_str());
if (static_cast<int>(num_repeat) < 0)
{
throw std::runtime_error(
"Error: Negative number of boolean required. ");
}
return num_repeat;
}
else
{
return 1;
}
}
catch (const std::runtime_error&)
{
throw std::runtime_error("Error: None Numeric Pattern");
}
}
inline bool validate_command(Reporter& reporter);
inline bool parse_binary_vector(const std::string& input,
const std::string& c,
std::vector<bool>& target)
{
if (input.empty()) return false;
std::string comma = "";
if (m_parameter_log.find(c) != m_parameter_log.end()) { comma = ","; }
m_parameter_log[c].append(comma + input);
if (!input.empty() && input.back() == ',')
{
m_error_message.append(
"Error: , detected at end of input: " + input
+ ". Have you accidentally included space in "
"your input? (Space is not allowed)");
return false;
}
std::vector<std::string> token = misc::split(input, ",");
try
{
for (auto&& bin : token)
{
// check if this is true or false, if, not, try parsing
misc::to_upper(bin);
try
{
bool value = false;
size_t num_repeat = number_boolean(bin, value);
for (size_t i = 0; i < num_repeat; ++i)
{ target.push_back(value); }
}
catch (const std::runtime_error&)
{
m_error_message.append(
"Error: Invalid argument passed to " + c + ": " + input
+ "! Require binary arguments e.g. T/F, "
"True/False, or numerical combination e.g. "
"10T,3F etc\n");
return false;
}
}
}
catch (...)
{
m_error_message.append(
"Error: Invalid argument passed to " + c + ": " + input
+ "! Require binary arguments e.g. T/F, True/False\n");
return false;
}
return true;
}
// return false when we can't extract the unit
inline bool extract_unit(const std::string& input, double& value,
std::string& unit)
{
bool valid = false;
size_t unit_length = 0;
for (; unit_length <= 2; ++unit_length)
{
try
{
value = misc::convert<double>(
input.substr(0, input.length() - unit_length));
valid = true;
break;
}
catch (const std::runtime_error&)
{
}
}
if (!valid)
return false;
else if (unit_length == 0)
{
unit = "";
}
else
{
unit = input.substr(input.length() - unit_length);
}
return true;
}
inline size_t unit_power(const std::string& unit)
{
const std::unordered_map<std::string, size_t> unit_map = {
{"b", 0}, {"bp", 0}, {"k", 1}, {"kb", 1}, {"m", 2},
{"mb", 2}, {"g", 3}, {"gb", 3}, {"t", 4}, {"tb", 4}};
return unit_map.at(unit);
}
inline bool parse_unit_value(const std::string& input, const std::string& c,
const size_t default_power, size_t& target,
bool memory = false)
{
check_duplicate(c);
std::string in = input;
misc::to_lower(in);
m_parameter_log[c] = in;
const size_t weight = memory ? 1024 : 1000;
double value;
std::string unit;
if (!extract_unit(in, value, unit))
{
m_error_message.append("Error: Invalid input: " + in + "\n");
return false;
}
if (value <= 0)
{
m_error_message.append("Error: Non-zero positive number required. "
+ input
+ " provided, please check if you have "
"provided the correct input\n");
return false;
}
size_t unit_power_level;
try
{
// only use default when unit isn't provided
if (unit.empty()) { unit_power_level = default_power; }
else
unit_power_level = unit_power(unit);
}
catch (...)
{
m_error_message.append("Error: Invalid input: " + in + "\n");
return false;
}
double power = pow(weight, unit_power_level);
if (value > std::numeric_limits<size_t>::max()
|| power > std::numeric_limits<size_t>::max()
|| misc::overflow<double>(value, power))
{
m_error_message.append("Error: Value input is exceptionally large. "
"PRSice won't be able to handle this\n");
return false;
}
value *= power;
if (value > std::numeric_limits<size_t>::max())
{
m_error_message.append("Error: Value input is exceptionally large. "
"PRSice won't be able to handle this\n");
return false;
}
if (trunc(value) != value)
{
m_error_message.append("Error: Non-integer value obtained: "
+ misc::to_string(target) + "\n");
return false;
}
target = static_cast<size_t>(value);
return true;
}
void check_duplicate(const std::string& c)
{
if (m_parameter_log.find(c) != m_parameter_log.end())
{
m_error_message.append("Warning: Duplicated argument --" + c
+ "\n");
}
}
template <typename Type>
inline bool set_numeric(const std::string& input, const std::string& c,
Type& target)
{
bool dumy;
return set_numeric<Type>(input, c, target, dumy);
}
template <typename Type>
inline bool set_numeric(const std::string& input, const std::string& c,
Type& target, bool& target_boolean)
{
check_duplicate(c);
m_parameter_log[c] = input;
try
{
target = misc::convert<Type>(input);
target_boolean = true;
return true;
}
catch (...)
{
m_error_message.append("Error: Invalid numeric argument passed to "
+ c + ": " + input + "!\n");
return false;
}
}
inline void append_log(const std::string& c, const std::string& input)
{
if (m_parameter_log.find(c) == m_parameter_log.end())
{ m_parameter_log[c] = input; }
else
{
m_parameter_log[c] = "," + input;
}
}
inline void load_string_vector(const std::string& input,
const std::string& c,
std::vector<std::string>& target)
{
if (input.empty()) return;
append_log(c, input);
if (!input.empty() && input.back() == ',')
{
m_error_message.append(
"Warning: , detected at end of input: " + input
+ ". Have you accidentally included space in "
"your input? (Space is not allowed)\n");
}
std::vector<std::string> token = misc::split(input, ",");
target.insert(target.end(), token.begin(), token.end());
}
inline bool set_memory(const std::string& input)
{
return parse_unit_value(input, "memory", 2, m_memory, true);
}
inline bool set_info(const std::string& in)
{
std::string input = in;
check_duplicate("info-type");
misc::to_lower(input);
switch (input.at(0))
{
case 'm':
input = "mach";
m_target_filter.info_type = INFO::MACH;
break;
case 'i':
input = "impute";
m_target_filter.info_type = INFO::IMPUTE2;
break;
default:
m_error_message.append("Error: Unrecognized info type: " + in
+ "!\n");
return false;
}
m_ref_filter.info_type = m_target_filter.info_type;
m_parameter_log["info-type"] = input;
return true;
}
inline bool set_missing(const std::string& in)
{
std::string input = in;
check_duplicate("missing");
misc::to_lower(input);
switch (input.at(0))
{
case 'c':
input = "centre";
m_prs_info.missing_score = MISSING_SCORE::CENTER;
break;
case 'm':
input = "mean_impute";
m_prs_info.missing_score = MISSING_SCORE::MEAN_IMPUTE;
break;
case 's':
input = "set_zero";
m_prs_info.missing_score = MISSING_SCORE::SET_ZERO;
break;
case 'i':
input = "impute_control";
m_prs_info.missing_score = MISSING_SCORE::IMPUTE_CONTROL;
break;
default:
m_error_message.append(
"Error: Unrecognized Missing handling method: " + in + "!\n");
return false;
}
m_parameter_log["missing"] = input;
return true;
}
inline bool set_model(const std::string& in)
{
std::string input = in;
misc::to_lower(input);
check_duplicate("model");
switch (input.at(0))
{
case 'a':
input = "add";
m_prs_info.genetic_model = MODEL::ADDITIVE;
break;
case 'd':
input = "dom";
m_prs_info.genetic_model = MODEL::DOMINANT;
break;
case 'r':
input = "rec";
m_prs_info.genetic_model = MODEL::RECESSIVE;
break;
case 'h':
input = "het";
m_prs_info.genetic_model = MODEL::HETEROZYGOUS;
break;
default:
m_error_message.append("Error: Unrecognized model: " + in + "!\n");
return false;
}
m_parameter_log["model"] = input;
return true;
}
inline bool set_score(const std::string& in)
{
std::string input = in;
misc::to_lower(input);
check_duplicate("score");
if (input == "avg") { m_prs_info.scoring_method = SCORING::AVERAGE; }
else if (input == "std")
{
m_prs_info.scoring_method = SCORING::STANDARDIZE;
}
else if (input == "sum")
{
m_prs_info.scoring_method = SCORING::SUM;
}
else if (input == "con-std")
{
m_prs_info.scoring_method = SCORING::CONTROL_STD;
}
else
{
m_error_message.append("Error: Unrecognized scoring method: " + in
+ "!\n");
return false;
}
m_parameter_log["score"] = input;
return true;
}
void add_base_to_log();
bool in_file(const std::vector<std::string>& column_names,
const size_t index, const std::string& warning,
bool no_default, bool case_sensitive = true,
bool print_error = true)
{
if ((no_default && !m_base_info.has_column[index])
|| m_base_info.column_name[index].empty())
{
m_base_info.has_column[index] = false;
return false;
}
size_t col_index;
bool has_col = index_check(m_base_info.column_name[index], column_names,
col_index, case_sensitive);
if (has_col) { m_base_info.column_index[index] = col_index; }
else if (m_base_info.has_column[index] && print_error)
{
// cannot find column but user has provided a column name
m_error_message.append(warning + ": "
+ m_base_info.column_name[index]
+ " not found in base file\n");
}
m_base_info.has_column[index] = has_col;
return has_col;
}
/**
* @return True if we don't need to error out. False otherwise
*/
inline bool set_base_info_threshold(const std::vector<std::string>& ref)
{
const std::vector<std::string> info =
misc::split(m_base_info.column_name[+BASE_INDEX::INFO], ":");
if (info.size() != 2)
{
m_error_message.append("Error: Invalid format of "
"--base-info. Should be "
"ColName:Threshold.\n");
return false;
}
const bool has_input = m_base_info.has_column[+BASE_INDEX::INFO];
size_t index;
// first, try and see if the header can be found in base
const bool found = index_check(info[0], ref, index);
if (found) m_base_info.column_index[+BASE_INDEX::INFO] = index;
m_base_info.has_column[+BASE_INDEX::INFO] = found;
if (!has_input && !found)
{
// do nothing, because we can't find the default
}
else
{
if (!found)
{
m_error_message.append("Warning: INFO field not found in base "
"file, will ignore INFO filtering\n");
}
else
{
// we have found valid formatted info, now check if threshold is
// correct
try
{
m_base_filter.info_score = misc::convert<double>(info[1]);
if (!misc::within_bound<double>(m_base_filter.info_score,
0.0, 1.0))
{
m_error_message.append("Error: Base INFO threshold "
"must be within 0 and 1!\n");
return false;
}
}
catch (...)
{
m_error_message.append(
"Error: Invalid argument passed to --base-info: "
+ m_base_info.column_name[+BASE_INDEX::INFO]
+ "! Second argument must be numeric\n");
return false;
}
}
}
return true;
}
/**
* @return True if we don't need to error out. False otherwise
*/
bool process_maf(const std::vector<std::string>& ref,
const std::vector<std::string>& detail,
size_t& column_index, int& has_column, double& maf)
{
if (detail.size() != 2)
{
throw std::runtime_error(
"Error: Invalid format of --base-maf. "
"Should be ColName:Threshold."
"or ColName:Threshold,ColName:Threshold.\n");
}
size_t index = 0;
bool found = index_check(detail[0], ref, index);
has_column = found;
if (found) column_index = index;
if (!found)
{
m_error_message.append(
"Warning: MAF field not found in base file. "
"Will not perform MAF filtering on the base file\n");
return false;
}
double cur_maf;
try
{
cur_maf = misc::convert<double>(detail[1]);
if (!misc::within_bound<double>(cur_maf, 0.0, 1.0))
{
throw std::runtime_error("Error: Base MAF threshold must "
"be within 0 and 1!\n");
}
}
catch (...)
{
throw std::runtime_error(
"Error: Invalid argument passed to --base-maf: "
+ m_base_info.column_name[+BASE_INDEX::MAF]
+ "! Threshold must be numeric\n");
}
maf = cur_maf;
return true;
}
// return true if valid
inline bool set_base_maf_filter(const std::vector<std::string>& ref)
{
const std::string maf_error =
"Error: Invalid format of --base-maf. "
"Should be ColName:Threshold."
"or ColName:Threshold,ColName:Threshold.\n";
std::vector<std::string> case_control =
misc::split(m_base_info.column_name[+BASE_INDEX::MAF], ",");
const bool user_require_maf_filter =
m_base_info.has_column[+BASE_INDEX::MAF];
// only process the maf filter if it is provided
if (!user_require_maf_filter) return true;
if (case_control.size() > 2)
{
if (user_require_maf_filter) { m_error_message.append(maf_error); }
return false;
}
std::vector<std::string> detail;
// process the control filter threshold
detail = misc::split(case_control.front(), ":");
try
{
// TODO: Still think the best course of action is to error out
// instead of silently dropping a filtering option
bool parse_control_ok = process_maf(
ref, detail, m_base_info.column_index[+BASE_INDEX::MAF],
m_base_info.has_column[+BASE_INDEX::MAF], m_base_filter.maf);
// if we can't parse the control, we will return true (say
// everything is ok) but will ignore MAF filtering
if (!parse_control_ok)
{
m_base_info.has_column[+BASE_INDEX::MAF] = false;
m_base_info.has_column[+BASE_INDEX::MAF_CASE] = false;
return true;
}
if (case_control.size() == 2)
{
detail = misc::split(case_control.back(), ":");
bool parse_case_ok =
process_maf(ref, detail,
m_base_info.column_index[+BASE_INDEX::MAF_CASE],
m_base_info.has_column[+BASE_INDEX::MAF_CASE],
m_base_filter.maf_case);
// if we can't parse the case MAF filtering threshold, we will
// also disable the fitering of the control
if (!parse_case_ok)
{
m_base_info.has_column[+BASE_INDEX::MAF] = false;
m_base_info.has_column[+BASE_INDEX::MAF_CASE] = false;
}
return true;
}
return true;
}
catch (const std::runtime_error& er)
{
m_base_info.has_column[+BASE_INDEX::MAF] = false;
m_base_info.has_column[+BASE_INDEX::MAF_CASE] = false;
m_error_message.append(er.what());
return false;
}
}
/*!
* \brief Get the column index based on file header and the input string
* \param target is the input string
* \param ref is the vector containing the column header
* \return the index to the column containing the column name. -1 if it is
* not found
*/
inline bool index_check(const std::string& target,
const std::vector<std::string>& ref, size_t& index,
bool case_sensitive = true) const
{
std::string tmp = "";
for (size_t i = 0; i < ref.size(); ++i)
{
tmp = ref[i];
if (!case_sensitive) { misc::to_upper(tmp); }
if (target == tmp)
{
index = i;
return true;
}
}
return false;
}
bool get_statistic_column(const std::vector<std::string>& column_names);
bool base_check();
bool base_column_check(std::vector<std::string>& column_names);
bool get_statistic_flag();
std::string get_program_header(const std::string& name);
int32_t maximum_thread()
{
int32_t max_threads = 1;
#if defined(WIN32) || defined(_WIN32) \
|| defined(__WIN32) && !defined(__CYGWIN__)
// max thread estimation using windows
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
max_threads = sysinfo.dwNumberOfProcessors;
int32_t known_procs = max_threads;
#else
int32_t known_procs =
static_cast<int32_t>(sysconf(_SC_NPROCESSORS_ONLN));
max_threads = (known_procs == -1) ? 1 : known_procs;
#endif
return max_threads;
}
};
#endif // COMMANDER_H