-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfastq.rs
276 lines (240 loc) · 8.79 KB
/
fastq.rs
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
use std::fs::File;
use std::path::Path;
use std::io::{Write, BufWriter, BufReader};
use indicatif::{ProgressBar, ProgressStyle, ProgressDrawTarget};
use anyhow::{Context, Result, anyhow};
use flate2::read::MultiGzDecoder;
use seq_io::fastq::Record;
use seq_io::parallel::read_parallel;
use crate::scan::find_concensus;
use crate::logger::info;
fn check_fastq(
infile: &str,
flag_t: &str,
) -> Result<u64> {
let n_threads = flag_t.parse::<u32>()
.with_context(|| format!("Unvalid -t parameters"))?;
// Input file
let in_file = Path::new(&infile);
if !in_file.exists() {
return Err(anyhow!("Input {} does not exist!", &infile));
}
// Open input
let fastq_fn = File::open(in_file).unwrap();
let buf_reader = BufReader::with_capacity(1<<22, fastq_fn);
let reader = seq_io::fastq::Reader::new(buf_reader);
let mut total_n = 0u64;
read_parallel(reader, n_threads, n_threads as usize, |record_set| {
let mut chunk_n = 0u64;
for _ in record_set.into_iter().enumerate() {
chunk_n += 1;
}
chunk_n
}, |record_sets| {
while let Some(result) = record_sets.next() {
let (_, tmp_ret) = result.unwrap();
total_n += tmp_ret;
}
});
Ok(total_n)
}
fn check_fastq_gz(
infile: &str,
flag_t: &str,
) -> Result<u64> {
let n_threads = flag_t.parse::<u32>()
.with_context(|| format!("Unvalid -t parameters"))?;
// Input file
let in_file = Path::new(&infile);
if !in_file.exists() {
return Err(anyhow!("Input {} does not exist!", &infile));
}
// Open input
let fastq_fn = File::open(in_file).unwrap();
let gz = MultiGzDecoder::new(fastq_fn);
let buf_reader = BufReader::with_capacity(1<<22, gz);
let reader = seq_io::fastq::Reader::new(buf_reader);
let mut total_n = 0u64;
read_parallel(reader, n_threads, n_threads as usize, |record_set| {
let mut chunk_n = 0u64;
for _ in record_set.into_iter().enumerate() {
chunk_n += 1;
}
chunk_n
}, |record_sets| {
while let Some(result) = record_sets.next() {
let (_, tmp_ret) = result.unwrap();
total_n += tmp_ret;
}
});
Ok(total_n)
}
pub fn scan_fastq(
infile: &str,
ccsfile: &str,
rawfile: &str,
flag_t: &str,
) -> Result<()> {
let n_reads = check_fastq(&infile, &flag_t)?;
info(format!("{} total reads", n_reads));
let n_threads = flag_t.parse::<u32>()?;
let in_file = Path::new(&infile);
// CCS output
let ccs_f = File::create(ccsfile)
.with_context(|| format!("Unvalid output CCS file"))?;
let mut ccs_writer = BufWriter::new(ccs_f);
// Raw output
let raw_f = File::create(rawfile)
.with_context(|| format!("Unvalid output CCS file"))?;
let mut raw_writer = BufWriter::new(raw_f);
// Process
info("Step 1 - Scanning raw reads to find circRNA candidates");
let fastx_fn = File::open(in_file).unwrap();
let buf_reader = BufReader::with_capacity(1<<22, fastx_fn);
let reader = seq_io::fastq::Reader::new(buf_reader);
let bar = ProgressBar::new(n_reads);
bar.set_style(ProgressStyle::default_bar()
.template("[{elapsed_precise}] [{bar:40./}] {pos:>7}/{len:7} ({eta})")
.progress_chars("#>-"));
bar.set_draw_target(ProgressDrawTarget::stderr());
let mut total_ccs = 0u64;
read_parallel(reader, n_threads, n_threads as usize, |record_set| {
let mut chunk_n = 0u64;
let mut chunk_ret: Vec<(usize, String, String)> = Vec::with_capacity(100);
for (i, record) in record_set.into_iter().enumerate() {
chunk_n += 1;
let seq = record.seq();
let _read_id = record.id().unwrap();
// let is_circ = filter_read_id(&read_id);
// if !is_circ {
// continue;
// }
match find_concensus(seq) {
Err(_) => continue,
Ok(x) => {
let ccs_seq = x.1;
let mut segments: Vec<String> = Vec::default();
for (s, e) in x.0 {
segments.push(String::from(vec![s.to_string(), e.to_string()].join("-")));
}
let segment_str = segments.join(";");
chunk_ret.push((i, segment_str, ccs_seq));
}
};
}
(chunk_n, chunk_ret)
}, |record_sets| {
while let Some(result) = record_sets.next() {
let (record_set, tmp_ret) = result.unwrap();
let chunk_n = tmp_ret.0;
let chunk_ret: Vec<_> = tmp_ret.1;
let chunk_reads: Vec<_> = record_set.into_iter().collect();
for (i, segment_str, ccs_seq) in chunk_ret {
write!(
raw_writer,
">{}\n{}\n",
String::from_utf8(chunk_reads[i].head().to_vec()).unwrap(),
String::from_utf8(chunk_reads[i].seq().to_vec()).unwrap()
).unwrap();
write!(
ccs_writer,
">{}\t{}\t{}\n{}\n",
chunk_reads[i].id().unwrap(),
segment_str,
ccs_seq.len(),
ccs_seq
).unwrap();
total_ccs += 1;
}
bar.inc(chunk_n);
};
});
bar.finish();
info(format!("{}/{} circular candidate reads", total_ccs, n_reads));
Ok(())
}
pub fn scan_fastq_gz(
infile: &str,
ccsfile: &str,
rawfile: &str,
flag_t: &str,
) -> Result<()> {
let n_reads = check_fastq_gz(&infile, &flag_t)?;
info(format!("{} total reads", n_reads));
let n_threads = flag_t.parse::<u32>()?;
let in_file = Path::new(&infile);
// CCS output
let ccs_f = File::create(ccsfile)
.with_context(|| format!("Unvalid output CCS file"))?;
let mut ccs_writer = BufWriter::new(ccs_f);
// Raw output
let raw_f = File::create(rawfile)
.with_context(|| format!("Unvalid output CCS file"))?;
let mut raw_writer = BufWriter::new(raw_f);
// Process
info("Step 1 - Scanning raw reads to find circRNA candidates");
let fastx_fn = File::open(in_file).unwrap();
let gz = MultiGzDecoder::new(fastx_fn);
let buf_reader = BufReader::with_capacity(1<<22, gz);
let reader = seq_io::fastq::Reader::new(buf_reader);
let bar = ProgressBar::new(n_reads);
bar.set_style(ProgressStyle::default_bar()
.template("[{elapsed_precise}] [{bar:40./}] {pos:>7}/{len:7} ({eta})")
.progress_chars("#>-"));
bar.set_draw_target(ProgressDrawTarget::stderr());
let mut total_ccs = 0u64;
read_parallel(reader, n_threads, n_threads as usize, |record_set| {
let mut chunk_n = 0u64;
let mut chunk_ret: Vec<(usize, String, String)> = Vec::with_capacity(100);
for (i, record) in record_set.into_iter().enumerate() {
chunk_n += 1;
let seq = record.seq();
let _read_id = record.id().unwrap();
// let is_circ = filter_read_id(&read_id);
// if !is_circ {
// continue;
// }
match find_concensus(seq) {
Err(_) => continue,
Ok(x) => {
let ccs_seq = x.1;
let mut segments: Vec<String> = Vec::default();
for (s, e) in x.0 {
segments.push(String::from(vec![s.to_string(), e.to_string()].join("-")));
}
let segment_str = segments.join(";");
chunk_ret.push((i, segment_str, ccs_seq));
}
};
}
(chunk_n, chunk_ret)
}, |record_sets| {
while let Some(result) = record_sets.next() {
let (record_set, tmp_ret) = result.unwrap();
let chunk_n = tmp_ret.0;
let chunk_ret: Vec<_> = tmp_ret.1;
let chunk_reads: Vec<_> = record_set.into_iter().collect();
for (i, segment_str, ccs_seq) in chunk_ret {
write!(
raw_writer,
">{}\n{}\n",
String::from_utf8(chunk_reads[i].head().to_vec()).unwrap(),
String::from_utf8(chunk_reads[i].seq().to_vec()).unwrap()
).unwrap();
write!(
ccs_writer,
">{}\t{}\t{}\n{}\n",
chunk_reads[i].id().unwrap(),
segment_str,
ccs_seq.len(),
ccs_seq
).unwrap();
total_ccs += 1;
}
bar.inc(chunk_n);
};
});
bar.finish();
info(format!("{}/{} circular candidate reads", total_ccs, n_reads));
Ok(())
}