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
extern crate fasten;
extern crate regex;
use fasten::fasten_base_options;
use fasten::logmsg;
use regex::Regex;
use std::fs::File;
use std::io::BufReader;
use std::io::BufRead;
use std::env;
fn main() {
let args: Vec<String> = env::args().collect();
let mut opts = fasten_base_options();
opts.optflag("","print-reads","Print each read. Useful for Unix pipes.");
let matches = opts.parse(&args[1..]).expect("ERROR: could not parse parameters");
if matches.opt_present("help") {
println!("Determine paired-end-ness in an interleaved file. Exit code of 0 indicates PE. Exit code > 0 indicates SE.\n{}",
opts.usage(&opts.short_usage(&args[0]))
);
std::process::exit(0);
}
if matches.opt_present("paired-end") {
logmsg("WARNING: --paired-end was supplied but this script is supposed to determine whether the input is paired-end.");
}
let should_print = matches.opt_present("print-reads");
let mut id1_vec :Vec<String>=Vec::new();
let mut id2_vec :Vec<String>=Vec::new();
let my_file = File::open("/dev/stdin").expect("Could not open file");
let my_buffer=BufReader::new(my_file);
let mut lines = my_buffer.lines();
while let Some(line) = lines.next() {
let id1 = line.expect("ERROR parsing id line in R1");
let mut entry=id1.clone();
entry.push('\n');
for _ in 1..4 {
let other_line = lines.next()
.expect("ERROR getting next line")
.expect("ERROR parsing next line");
entry.push_str(&other_line);
entry.push('\n');
}
let id2 = lines.next()
.expect("ERROR getting R2. This is not a paired end file.")
.expect("ERROR parsing next line in R2");
entry.push_str(&id2);
entry.push('\n');
for _ in 1..4 {
let other_line = lines.next()
.expect("ERROR getting next line in R2. This is not a paired end file.")
.expect("ERROR parsing next line in R2");
entry.push_str(&other_line);
entry.push('\n');
}
id1_vec.push(id1);
id2_vec.push(id2);
if should_print {
print!("{}",entry);
}
}
let mut is_paired_end :u8 = 0;
is_paired_end += is_paired_end_slash12(&id1_vec, &id2_vec);
is_paired_end += is_paired_end_miseq(&id1_vec, &id2_vec);
if is_paired_end == 0 {
if matches.opt_present("verbose") {
logmsg("I do not think this is interleaved paired end");
}
std::process::exit(1);
}
if matches.opt_present("verbose") {
logmsg("The fastq input seems to be interleaved paired-end");
}
}
fn is_paired_end_slash12(id1_ref: &Vec<String>, id2_ref: &Vec<String>) -> u8 {
let id1_vec = id1_ref.clone();
let id2_vec = id2_ref.clone();
let slash_r1r2_regex = Regex::new(r"(.+)/([12])$").expect("malformed qual regex");
let mut id2_iter = id2_vec.iter();
for id1 in id1_vec.iter(){
let id2 = id2_iter.next()
.expect("ERROR getting next id2");
let caps1_result = slash_r1r2_regex.captures(&id1);
let caps2_result = slash_r1r2_regex.captures(&id2);
if caps1_result.is_none() || caps2_result.is_none() {
return 0;
}
let caps1 = caps1_result
.expect("ERROR: could not regex against id1");
let caps2 = caps2_result
.expect("ERROR: could not regex against id2");
if caps1[1] != caps2[1] {
return 0;
}
if &caps1[2] != "1" || &caps2[2] != "2" {
return 0;
}
}
return 1;
}
fn is_paired_end_miseq(id1_ref: &Vec<String>, id2_ref: &Vec<String>) -> u8 {
let id1_vec = id1_ref.clone();
let id2_vec = id2_ref.clone();
let mut id2_iter = id2_vec.iter();
for id1 in id1_vec.iter(){
let id2 = id2_iter.next()
.expect("ERROR getting next id2");
let id1_tmp = id1.split(":").nth(6);
let id2_tmp = id2.split(":").nth(6);
if id1_tmp.is_none() || id2_tmp.is_none() {
return 0;
}
let id1_read_number = id1_tmp.unwrap().split(" ").nth(1);
let id2_read_number = id2_tmp.unwrap().split(" ").nth(1);
if id1_read_number.is_none() || id2_read_number.is_none() {
return 0;
}
if id1_read_number.unwrap() != "1" || id2_read_number.unwrap() != "2" {
return 0;
}
}
return 1;
}