-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInputParser.cpp
196 lines (174 loc) · 6.89 KB
/
InputParser.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
// GAGS
// Developed by Andrew R Wood
#include <string>
#include <iostream>
#include <stdlib.h>
#include <vector>
#include "InputParser.h"
#include "StringOperations.h"
using namespace std;
void ShowHeader() {
cout << endl << "==================== GAGS ===============" << endl;
cout << " Genetic Algorithm Group Selection " << endl;
cout << " Written by Andrew R Wood " << endl;
cout << " [email protected] " << endl;
cout << " University of Exeter Medical School " << endl;
cout << " Version 0.1: 20-09-2018 " << endl;
cout << "=======================================" << endl;
cout << endl;
}
void ShowOptions() {
cout << "Usage:" << endl;
cout << " --pheno -p [phenotype file]" << endl;
cout << " --ns -n [list of group sizes]" << endl;
cout << " --means -m [list of respective means]" << endl;
cout << " --sds -s [list of respective SDs]" << endl;
cout << " --seed -r [seed - default 10000]" << endl;
cout << " --mrate -y [max mutation rate - default 0.01]" << endl;
cout << " --popsize -z [initial solutions - default 10]" << endl;
cout << " --exclusions -e [optional: file of IDs to exclude]" << endl;
cout << " --iterations -i [max iterations - default 50000]" << endl;
cout << " --out -o [output file prefix]" << endl;
cout << endl;
}
void ParseCommand(int argc, char** argv, ARGS *a) {
if (argc > 1) {
// defaults
a->seed = 10000;
a->popsize = 10;
a->mRate = 0.01;
a->iterations = 50000;
a->ok = true;
for (int i = 1; i != argc; ++i) {
if (string(argv[i]) == "--pheno" || string(argv[i]) == "-p") {
if (i+1 <= argc-1 && string(argv[i+1]).substr(0,2) != "--") {
a->phenoString = argv[i+1];
}
else {
cout << "Not entered --pheno (-p) argument" << endl << endl;
a->ok = false;
}
}
else if (string(argv[i]) == "--ns" || string(argv[i]) == "-n") {
if (i+1 <= argc-1 && string(argv[i+1]).substr(0,2) != "--") {
a->nsString = argv[i+1];
}
else {
cout << "Not entered --ns (-n) argument" << endl << endl;
a->ok = false;
}
}
else if (string(argv[i]) == "--means" || string(argv[i]) == "-m") {
if (i+1 <= argc-1 && string(argv[i+1]).substr(0,2) != "--") {
a->meansString = argv[i+1];
}
else {
cout << "Not entered --means (-m) argument" << endl << endl;
a->ok = false;
}
}
else if (string(argv[i]) == "--sds" || string(argv[i]) == "-s") {
if (i+1 <= argc-1 && string(argv[i+1]).substr(0,2) != "--") {
a->sdsString = argv[i+1];
}
else {
cout << "Not entered --sds (-s) argument" << endl << endl;
a->ok = false;
}
}
else if (string(argv[i]) == "--seed" || string(argv[i]) == "-r") {
if (i+1 <= argc-1 && string(argv[i+1]).substr(0,2) != "--") {
a->seed = atoi(argv[i+1]);
}
else {
cout << "Not entered --seed (-r) argument" << endl << endl;
a->ok = false;
}
}
else if (string(argv[i]) == "--mrate" || string(argv[i]) == "-y") {
if (i+1 <= argc-1 && string(argv[i+1]).substr(0,2) != "--") {
a->mRate = atof(argv[i+1]);
}
else {
cout << "Not entered --mrate (-y) argument" << endl << endl;
a->ok = false;
}
}
else if (string(argv[i]) == "--popsize" || string(argv[i]) == "-z") {
if (i+1 <= argc-1 && string(argv[i+1]).substr(0,2) != "--") {
a->popsize = atoi(argv[i+1]);
}
else {
cout << "Not entered --popsize (-z) argument" << endl << endl;
a->ok = false;
}
}
else if (string(argv[i]) == "--exclusions" || string(argv[i]) == "-e") {
if (i+1 <= argc-1 && string(argv[i+1]).substr(0,2) != "--") {
a->exclusionsString = argv[i+1];
}
else {
cout << "Not entered --exclusions (-e) argument" << endl << endl;
a->ok = false;
}
}
else if (string(argv[i]) == "--iterations" || string(argv[i]) == "-i") {
if (i+1 <= argc-1 && string(argv[i+1]).substr(0,2) != "--") {
a->iterations = atoi(argv[i+1]);
}
else {
cout << "Not entered --iterations (-i) argument" << endl << endl;
a->ok = false;
}
}
else if (string(argv[i]) == "--out" || string(argv[i]) == "-o") {
if (i+1 <= argc-1 && string(argv[i+1]).substr(0,2) != "--") {
a->outString = argv[i+1];
}
else {
cout << "Not entered --out (-o) argument" << endl << endl;
a->ok = false;
}
}
else if (string(argv[i]) == "--help" || string(argv[i]) == "-h") {
a->ok = false;
}
}
if (a->phenoString.empty() || a->meansString.empty() || a->sdsString.empty() || a->outString.empty() ) {
a->ok = false;
}
}
else {
a->ok = false;
}
}
void SplitStringArgs(string& s, vector<string>& v) {
split(s, ",", v);
}
void SplitDoubleArgs(string& s, vector<double>& v) {
vector<string> vec;
split(s, ",", vec);
for (int i = 0; i < vec.size(); ++i) {
v.push_back(stringToDouble(vec[i]));
}
}
void SplitIntArgs(string& s, vector<int>& v) {
vector<string> vec;
split(s, ",", vec);
for (int i = 0; i < vec.size(); ++i) {
v.push_back(stringToInt(vec[i]));
}
}
void DetermineDoubleArgsPrecision(string& s, vector<int>& v) {
vector<string> vec;
split(s, ",", vec);
for (int i = 0; i < vec.size(); ++i) {
if (vec[i].find(".") != -1) {
v.push_back(vec[i].size() - (vec[i].find(".")+1));
}
else {
v.push_back(0);
}
}
}