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
use anyhow::{bail, Context, Result};
use flate2::read::GzDecoder;
use num_format::{Locale, ToFormattedString};
use std::{
collections::VecDeque,
fmt,
fs::File,
io::{BufRead, BufReader, Write},
sync::{
atomic::{AtomicBool, AtomicU32, Ordering},
Arc, Mutex,
},
};
use crate::parse::RawSequenceRead;
pub fn read_fastq(
fastq: String,
seq_clone: Arc<Mutex<VecDeque<String>>>,
exit_clone: Arc<AtomicBool>,
total_reads_arc: Arc<AtomicU32>,
) -> Result<()> {
let fastq_file = File::open(&fastq).context(format!("Failed to open file: {}", fastq))?;
let mut fastq_line_reader = FastqLineReader::new(seq_clone, exit_clone);
if !fastq.ends_with("fastq.gz") {
if !fastq.ends_with("fastq") {
bail!("This program only works with *.fastq files and *.fastq.gz files. The latter is still experimental")
}
for line_result in BufReader::new(fastq_file).lines() {
let mut line =
line_result.context(format!("Bufread could not read line for file: {}", fastq))?;
line.push('\n');
fastq_line_reader.read(line);
if fastq_line_reader.line_num == 4 {
fastq_line_reader.post()?;
}
if fastq_line_reader.total_reads % 10000 == 0 {
print!("{}", fastq_line_reader);
std::io::stdout().flush()?;
}
}
} else {
println!("If this program stops reading before the expected number of sequencing reads, unzip the gzipped fastq and rerun.");
println!();
let mut reader = BufReader::new(GzDecoder::new(fastq_file));
let mut read_response = 10;
while read_response != 0 {
let mut line = String::new();
read_response = reader.read_line(&mut line)?;
fastq_line_reader.read(line);
if fastq_line_reader.line_num == 4 {
fastq_line_reader.post()?;
}
if fastq_line_reader.total_reads % 10000 == 0 {
print!("{}", fastq_line_reader);
std::io::stdout().flush()?;
}
}
}
print!("{}", fastq_line_reader);
total_reads_arc.store(fastq_line_reader.total_reads, Ordering::Relaxed);
println!();
Ok(())
}
struct FastqLineReader {
test: bool,
line_num: u8,
total_reads: u32,
raw_sequence_read_string: String,
seq_clone: Arc<Mutex<VecDeque<String>>>,
exit_clone: Arc<AtomicBool>,
}
impl FastqLineReader {
pub fn new(seq_clone: Arc<Mutex<VecDeque<String>>>, exit_clone: Arc<AtomicBool>) -> Self {
FastqLineReader {
test: true,
line_num: 0,
total_reads: 0,
raw_sequence_read_string: String::new(),
seq_clone,
exit_clone,
}
}
pub fn read(&mut self, line: String) {
while self.seq_clone.lock().unwrap().len() >= 10000 {
if self.exit_clone.load(Ordering::Relaxed) {
break;
}
}
self.line_num += 1;
if self.line_num == 5 {
self.line_num = 1
}
if self.line_num == 1 {
self.total_reads += 1;
self.raw_sequence_read_string = line;
} else {
self.raw_sequence_read_string.push_str(&line);
}
}
pub fn post(&mut self) -> Result<()> {
self.raw_sequence_read_string.pop();
if self.test {
RawSequenceRead::unpack(self.raw_sequence_read_string.clone())?.check_fastq_format()?;
self.test = false;
}
self.seq_clone
.lock()
.unwrap()
.push_front(self.raw_sequence_read_string.clone());
Ok(())
}
}
impl fmt::Display for FastqLineReader {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"Total sequences: {}\r",
self.total_reads.to_formatted_string(&Locale::en)
)
}
}