This repository was archived by the owner on Dec 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathillumina_header_info.rs
168 lines (137 loc) · 4.6 KB
/
illumina_header_info.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
use crate::read_pair::{ReadPart, WhichRead};
use crate::read_pair_iter::{InputFastqs, ReadPairIter};
use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Clone, Debug)]
pub struct IlluminaHeaderInfo {
pub instrument: String,
pub run_number: u32,
pub flowcell: String,
pub lane: u32,
}
impl Default for IlluminaHeaderInfo {
fn default() -> IlluminaHeaderInfo {
IlluminaHeaderInfo {
instrument: "unknown_instrument".to_string(),
run_number: 0,
flowcell: "unknow_flowcell".to_string(),
lane: 0,
}
}
}
impl InputFastqs {
pub fn get_header_info(&self) -> Result<Option<IlluminaHeaderInfo>> {
let mut iter = ReadPairIter::from_fastq_files(self)?;
let read1 = iter
.next()
.transpose()?
.ok_or_else(|| anyhow!("Empty fastq file: {self:?}"))?;
let header = read1
.get(WhichRead::R1, ReadPart::Header)
.ok_or_else(|| anyhow!("No Read1 in FASTQ data"))?;
let header = std::str::from_utf8(header)?;
let header_prefix = header.split([' ', '/']).next();
if header_prefix.is_none() {
return Ok(None);
}
let header_prefix = header_prefix.unwrap();
let header_parts: Vec<&str> = header_prefix.split(':').collect();
if header_parts.len() < 4 {
Ok(None)
} else {
let instrument = header_parts[0].to_string();
// Just bail out with None if we can parse integer fields in header
let run_number = header_parts[1].parse();
if run_number.is_err() {
return Ok(None);
}
let run_number = run_number?;
let flowcell = header_parts[2].to_string();
let lane = header_parts[3].parse();
if lane.is_err() {
return Ok(None);
}
let lane = lane?;
let res = IlluminaHeaderInfo {
instrument,
run_number,
flowcell,
lane,
};
Ok(Some(res))
}
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::filenames::bcl2fastq::Bcl2FastqDef;
use crate::filenames::{FindFastqs, LaneSpec};
use crate::InputFastqs;
#[test]
fn test_parse_fastq_info() -> Result<()> {
let path = "tests/filenames/bcl2fastq";
let query = Bcl2FastqDef {
fastq_path: path.to_string(),
sample_name_spec: "Infected".into(),
lane_spec: LaneSpec::Any,
};
let mut fqs = query.find_fastqs()?;
fqs.sort();
let info = fqs[0].get_header_info()?;
let correct = Some(IlluminaHeaderInfo {
instrument: "A00419".to_string(),
run_number: 42,
flowcell: "H7CL3DRXX".to_string(),
lane: 1,
});
assert_eq!(info, correct);
Ok(())
}
#[test]
fn weird_header_info() -> Result<()> {
let fq = InputFastqs {
r1: "tests/read_pair_iter/weird-header-R1.fastq".to_string(),
r2: Some("tests/read_pair_iter/weird-header-R2.fastq".to_string()),
i1: None,
i2: None,
r1_interleaved: false,
};
let info = fq.get_header_info()?.unwrap();
println!("info: {info:?}");
// This is an example of a wierd customer FASTQ adapter from MGI
// this checks that we cna parse the 4th field correctly if
// it's the last field before a space or /
assert_eq!(info.instrument, "3");
assert_eq!(info.lane, 1000);
Ok(())
}
#[test]
fn weird_header2_info() -> Result<()> {
let fq = InputFastqs {
r1: "tests/read_pair_iter/weird-header2-R1.fastq".to_string(),
r2: Some("tests/read_pair_iter/weird-header2-R2.fastq".to_string()),
i1: None,
i2: None,
r1_interleaved: false,
};
let info = fq.get_header_info()?;
println!("info: {info:?}");
assert_eq!(info, None);
Ok(())
}
#[test]
fn weird_header_csi_1376() -> Result<()> {
let fq = InputFastqs {
r1: "tests/read_pair_iter/csi-1376-R1.fastq".to_string(),
r2: Some("tests/read_pair_iter/csi-1376-R2.fastq".to_string()),
i1: None,
i2: None,
r1_interleaved: false,
};
let info = fq.get_header_info()?;
println!("info: {info:?}");
assert_eq!(info, None);
Ok(())
}
}