-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenomic_filter.hpp
266 lines (213 loc) · 7.37 KB
/
genomic_filter.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
#ifndef genomic_filter_h
#define genomic_filter_h
#include <stdexcept>
#include <boost/program_options.hpp>
namespace po = boost::program_options;
#include "typedefs.h"
#include "global.hpp"
#include "SampleSets.hpp"
#include "genomic_common.hpp"
class Filter : public Command {
public:
Filter()
: Command("filter file based on reference") {
// Delcare options
opts.add_options()
("help", "print help message")
("input,i", po::value<string>(), "sample set file")
("reference,r", po::value<string>(), "reference set file (same format as sample set)")
("format,f", po::value<string>(), "input file format [default: determined from file extension]")
("reference_format,g", po::value<string>(), "reference file format [default: reference format corresponding to input]")
("output,o", po::value<string>(), "output file")
("threshold,t", po::value<float>(), "overlap threshold (only used for segmentation files) [default: 0.5]")
("score,s", po::value<string>(), "overlap score method, only used for segmentation files) [options: dice (default), query, reference, min, max]")
("inverse,v", po::value<bool>(), "select instead of filter overlapping segments")
("merge,m", po::value<bool>(), "merge filtered segments with upstream/downstream segments?")
("aberrant,a", po::value<bool>(), "filter only aberrant segments?")
("state_diff", po::value<rvalue>(), "threshold for difference from reference state")
("ref_state", po::value<rvalue>(), "reference state")
("optimize,O", po::value<bool>(), "optimize algorithm speed, assuming contiguity of reference segments")
;
popts.add("input", 1).add("reference", 1).add("output", 1);
}
template <bool> struct segmented {};
template <typename SampleSetType, typename ReferenceSetType>
void filter(segmented<false>) {
SampleSetType set;
set.read(inputFileName);
ReferenceSetType ref;
ref.read(referenceFileName);
set.filter(ref);
set.write(outputFileName);
}
template <typename SampleSetType, typename ReferenceSetType>
void filter(segmented<true>) {
SampleSetType set;
set.read(inputFileName);
ReferenceSetType ref;
ref.read(referenceFileName);
set.set(CNACriteria(refState, stateDiff));
if (score == "dice") {
dice_overlapper checker(threshold);
set.template filter<dice_overlapper>(ref, checker, inverse, merge, aberrant, optimize);
} else if (score == "query") {
query_overlapper checker(threshold);
set.template filter<query_overlapper>(ref, checker, inverse, merge, aberrant, optimize);
} else if (score == "reference") {
reference_overlapper checker(threshold);
set.template filter<reference_overlapper>(ref, checker, inverse, merge, aberrant, optimize);
} else if (score == "min") {
min_overlapper checker(threshold);
set.template filter<min_overlapper>(ref, checker, inverse, merge, aberrant, optimize);
} else if (score == "max") {
max_overlapper checker(threshold);
set.template filter<max_overlapper>(ref, checker, inverse, merge, aberrant, optimize);
} else {
throw runtime_error("Invalid overlap score method specified.");
}
set.write(outputFileName);
}
void run() {
if (vm.count("help")) {
cout << "usage: " << progname << " filter [options] <sample set file> <reference set file> <output file>" << endl;
cout << opts << endl;
return;
}
getOptions();
switch (inputType) {
case data::segmented:
switch(referenceType) {
case data::segmented_ref:
filter< SegmentedSampleSet<rvalue>, ReferenceSegmentedSampleSet<rvalue> >(segmented<true>());
break;
case data::segmented:
filter< SegmentedSampleSet<rvalue>, SegmentedSampleSet<rvalue> >(segmented<true>());
break;
default:
throw invalid_argument("Unsupported reference file format.");
}
break;
case data::segmented_ascn:
switch(referenceType) {
case data::segmented_ref:
filter< SegmentedSampleSet<alleles_cn>, ReferenceSegmentedSampleSet<alleles_cn> >(segmented<true>());
break;
case data::segmented_ascn:
filter< SegmentedSampleSet<alleles_cn>, SegmentedSampleSet<alleles_cn> >(segmented<true>());
break;
default:
throw invalid_argument("Unsupported reference file format.");
}
break;
case data::raw:
switch(referenceType) {
case data::raw_ref:
filter< RawSampleSet<rvalue>, ReferenceRawSampleSet<rvalue> >(segmented<false>());
break;
case data::raw:
filter< RawSampleSet<rvalue>, RawSampleSet<rvalue> >(segmented<false>());
break;
default:
throw invalid_argument("Unsupported reference file format.");
}
break;
case data::raw_ascn:
switch(referenceType) {
case data::raw_ref:
filter< RawSampleSet<alleles_cn>, ReferenceRawSampleSet<alleles_cn> >(segmented<false>());
break;
case data::raw_ascn:
filter< RawSampleSet<alleles_cn>, RawSampleSet<alleles_cn> >(segmented<false>());
break;
default:
throw invalid_argument("Unsupported reference file format.");
}
break;
}
}
private:
string inputFileName, referenceFileName, outputFileName;
data::Type inputType, referenceType;
float threshold;
bool merge, aberrant;
float stateDiff, refState;
bool optimize;
bool inverse;
string score;
void getOptions() {
if (vm.count("input") && vm.count("reference")) {
inputFileName = vm["input"].as<string>();
referenceFileName = vm["reference"].as<string>();
} else {
throw invalid_argument("Both input and reference files must be specified.");
}
if (vm.count("format")) {
inputType = mapping::extension[ vm["format"].as<string>() ];
} else {
inputType = mapping::extension[ name::fileext(inputFileName) ];
}
if (inputType == data::invalid) {
throw invalid_argument("Invalid input format type.");
}
if (vm.count("reference_format")) {
referenceType = mapping::extension[ vm["reference_format"].as<string>() ];
} else {
if (inputType == data::raw || inputType == data::raw_ascn || inputType == data::raw_lrrbaf) {
referenceType = data::raw_ref;
} else {
referenceType = data::segmented_ref;
}
}
if (referenceType == data::invalid) {
throw invalid_argument("Invalid reference format type.");
}
if (vm.count("output")) {
outputFileName = vm["output"].as<string>();
} else {
outputFileName = name::filestem(inputFileName) + ".filtered." + name::fileext(inputFileName);
}
if (vm.count("threshold")) {
threshold = vm["threshold"].as<float>();
} else {
threshold = 0.5;
}
if (vm.count("score")) {
score = vm["score"].as<string>();
} else {
score = "dice";
}
if (vm.count("merge")) {
merge = vm["merge"].as<bool>();
} else {
merge = false;
}
if (vm.count("aberrant")) {
aberrant = vm["aberrant"].as<bool>();
} else {
aberrant = false;
}
//TODO automatically determine stateDiff and refState
// current settings are suitable for LRR data
if (vm.count("state_diff")) {
stateDiff = vm["state_diff"].as<float>();
} else {
stateDiff = 0.1;
}
if (vm.count("ref_state")) {
refState = vm["ref_state"].as<float>();
} else {
refState = 0;
}
if (vm.count("optimize")) {
optimize = vm["optimize"].as<bool>();
} else {
optimize = true;
}
if (vm.count("inverse")) {
inverse = vm["inverse"].as<bool>();
} else {
inverse = false;
}
}
};
#endif