-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblgrep.cpp
More file actions
305 lines (265 loc) · 10.1 KB
/
blgrep.cpp
File metadata and controls
305 lines (265 loc) · 10.1 KB
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
/*
* Seqan-based program for grepping sequence files. Right now it uses
* a regex approach to all the matching, but I would like to implement
* a biological matching approach too.
*
* It refuses to read genbank files with a ".gb" extension unless they are
* piped in. I can't seem to convince SeqFileIn that genbank is the format.
*
* TODO:
* - Ability to search within a range of sequence coordinates
* - Ability to spit out context around matches or the number of matches
* - Ability to split a fasta file into records using a GFF file -
* or maybe that should be a different tool?
* - Biologically-based search instead of regex; I think if I make a
* bl_search function that either takes the same arguments as
* regex_search or runs a biological pattern matching search, I
* could make it work. If it is a template, I might be able to do
* something about the problem of treating all sequences as Dna.
* - Fixed string match not using regex (just string1 == string2);
* probably best implemented with a wrapper function for matching
* that could deal with a regex, a string, or a biological pattern.
*
*/
#include <iostream>
#include <regex>
#include <string>
#include <vector>
#include <seqan/seq_io.h>
#include <seqan/translation.h>
#include <tclap/CmdLine.h>
#include <SeqFileInWrapper.h>
using std::cout;
using std::cerr;
using std::endl;
using std::regex;
using std::regex_match;
using std::string;
using std::vector;
using namespace seqan;
using namespace bltools;
int main(int argc, char * argv[]) {
/*
* Command line arguments.
*/
TCLAP::CmdLine cmd("Program to regex search sequence files", ' ', "0.0");
TCLAP::SwitchArg seq_regex_arg("S", "sequence-regex",
"use regex for sequences instead of name; sets -i",
cmd);
TCLAP::SwitchArg invert_regex_arg("v", "invert-match",
"Invert matching, like grep -v", cmd);
TCLAP::SwitchArg ignore_case_arg("i", "ignore-case",
"Ignore case in pattern and input", cmd);
TCLAP::SwitchArg case_sensitive_arg("I", "case-sensitive",
"Do not ignore case in pattern and input; only for -S",
cmd);
TCLAP::SwitchArg file_switch_arg("f", "file",
"PATTERN is actually a file containing one regex per line; leading and trailing whitespace will be considered part of the pattern",
cmd);
TCLAP::ValueArg<string> match_type_arg("M", "match-type",
"Match type: f=fwd, r=rev, c=compl., R=revcomp; ignored for names",
false, "f", "string", cmd);
TCLAP::ValueArg<int> frame_arg("F", "frame",
"Frame for translation: 0=fwd frame, 1=fwd + revcomp, 2=all 3 fwd, 3=all 6",
false, 0, "string", cmd);
TCLAP::ValueArg<string> format_arg("o", "output-format",
"Output format: fasta or fastq; fasta is default; will not print fastq if there aren't quality strings",
false, "fasta", "fast[aq]", cmd);
TCLAP::UnlabeledValueArg<string> regex_string_arg("PATTERN", "regex pattern",
true, "",
"regex", cmd, false);
TCLAP::UnlabeledMultiArg<string> infile_name("FILE(s)", "input file(s) use '-' for stdin or leave blank",
false, "file name(s)", cmd, false);
cmd.parse(argc, argv);
vector<string> infiles = infile_name.getValue();
if(infiles.size() == 0) infiles.push_back("-"); // stdin
bool seq_regex = seq_regex_arg.getValue();
bool inverted = invert_regex_arg.getValue();
string match_type = match_type_arg.getValue();
int frame = frame_arg.getValue();
string format = format_arg.getValue();
bool regex_in_file = file_switch_arg.getValue();
// Regex setup
vector<regex> regex_patterns;
std::regex_constants::syntax_option_type regex_flags =
regex::extended | regex::optimize;
std::regex_constants::match_flag_type regex_match_flags =
std::regex_constants::match_any | std::regex_constants::match_not_null;
if(ignore_case_arg.getValue() ||
(seq_regex && !case_sensitive_arg.getValue())) {
regex_flags |= regex::icase;
}
if(regex_in_file) {
// Read regex's from file
ifstream regex_stream(regex_string_arg.getValue());
if(!(regex_stream.is_open() && regex_stream.good())) {
cerr << "Could not open regex file " << regex_string_arg.getValue() <<
endl;
return 1;
}
for(string line; getline(regex_stream, line); ) {
regex regex_pattern(line, regex_flags);
regex_patterns.push_back(regex_pattern);
}
} else {
regex regex_pattern(regex_string_arg.getValue(), regex_flags);
regex_patterns.push_back(regex_pattern);
} // End regex setup
// Translation frame setup
TranslationFrames tframe;
switch(frame) {
case 0:
tframe = SINGLE_FRAME;
break;
case 1:
tframe = WITH_REVERSE_COMPLEMENT;
break;
case 2:
tframe = WITH_FRAME_SHIFTS;
break;
case 3:
tframe = SIX_FRAME;
break;
default:
tframe = SINGLE_FRAME;
break;
} // End translation frame setup
// Output file setup
SeqFileOut out_handle(cout, Fasta());
if(format == "fasta") {
setFormat(out_handle, Fasta());
} else if(format == "fastq") {
setFormat(out_handle, Fastq());
} else {
cerr << "Unrecognized output format";
return 1;
} // End output file setup
// Loop variables
CharString id;
CharString seq; // CharString more flexible than Dna5String
CharString qual;
SeqFileInWrapper seq_handle;
// Loop over input files
int nmatched = 0;
for(string& infile: infiles) {
try {
seq_handle.open(infile);
} catch(Exception const &e) {
cerr << "Could not open " << infile << endl;
seq_handle.close();
return 1;
}
bool matched = false;
while(!seq_handle.atEnd()) {
try {
readRecord(id, seq, qual, seq_handle.sqh);
} catch (Exception const &e) {
cerr << "Error: " << e.what() << endl;
seq_handle.close();
close(out_handle);
return 1;
} // End try-catch for record reading.
// Loop over patterns
matched = false;
for(regex rg: regex_patterns) {
// Regex
if(seq_regex) {
if(regex_search(match_type, regex("a"))) {
match_type = "frcR";
}
if(regex_search(match_type, regex("A"))) {
match_type = "frcRt";
}
// Due to some quirks of Seqan, I have to do a number of format
// conversions, so this isn't as elegant as I would like.
// Also this assumes DNA, not RNA, even though RNA could work
// fine. Note that any type of sequence will work with regular
// forward matching.
for(char& c: match_type) {
switch (c) {
case 'f':
{
CharString _seq = seq;
matched |= regex_search(toCString(_seq), rg,
regex_match_flags);
break;
}
case 'r':
{
ModifiedString<CharString, ModReverse> rseq(seq);
CharString _seq(rseq);
matched |= regex_search(toCString(_seq), rg,
regex_match_flags);
break;
}
case 'c':
{
Dna5String dseq(seq);
complement(dseq);
CharString _seq(dseq);
matched |= regex_search(toCString(_seq), rg,
regex_match_flags);
break;
}
case 'R':
{
Dna5String dseq(seq);
reverseComplement(dseq);
CharString _seq(dseq);
matched |= regex_search(toCString(_seq), rg,
regex_match_flags);
break;
}
case 't':
{
// template<typename T> trans_search(seq, pattern) ...
// use with <Dna5String> for DNA or <Rna5String>...
StringSet< String<AminoAcid> > aseqs;
Dna5String dseq(seq);
translate(aseqs, dseq, tframe);
// Loop over translation frames
for(String<AminoAcid>& _aseq: aseqs) {
CharString _seq(_aseq);
matched |= regex_search(toCString(_seq), rg,
regex_match_flags);
} // End loop over translation frames
break;
}
} // End switch statement
} // End match_type loop
} else {
// Simple regex on sequence IDs
matched = regex_search(toCString(id), rg,
regex_match_flags);
} // End regex if/else
// If a match was found, no need to check the rest of the
// regex patterns:
if(matched) break;
} // End loop over regex pattern
// Write out if matched
if((matched && !inverted) || (!matched && inverted)) {
nmatched++;
try {
writeRecord(out_handle, id, seq, qual);
} catch (Exception const &e) {
cerr << "Error: " << e.what() << endl;
cerr << "Error writing output" << endl;
seq_handle.close();
return 1;
}
} // End write out if matched
} // End single file reading loop
// Close the input handle and check for errors
if(!seq_handle.close()) {
cerr << "Problem closing " << infile << endl;
close(out_handle);
return 1;
}
} // End loop over files
close(out_handle);
if(nmatched) {
return 0;
} else {
return 1;
}
}