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
#![crate_type="lib"]
#![deny(warnings, missing_docs)]
use std::convert::From;
use std::result::Result;
use std::num;
pub struct SequenceIdentifier {
pub sequencer_id: String,
pub run_count: u16,
pub flow_cell_id: String,
pub lane: u8,
pub side: u8,
pub swath: u8,
pub tile: u8,
pub x: u16,
pub y: u16
}
#[derive(Debug)]
pub enum IlluminaError {
ParseIntError,
SplitError
}
impl From<num::ParseIntError> for IlluminaError {
fn from(_: num::ParseIntError) -> IlluminaError {
IlluminaError::ParseIntError
}
}
pub fn parse_sequence_identifier(text: &str) -> Result<SequenceIdentifier, IlluminaError> {
let cap: Vec<&str> = text.trim().split(':').collect();
if cap.len() != 7 {
return Err(IlluminaError::SplitError);
}
let sequencer_id = cap[0].split_at(1).1.to_string();
let run_count = cap[1].parse::<u16>()?;
let flow_cell_id = cap[2].to_string();
let lane = cap[3].parse::<u8>()?;
let (side, remainder) = cap[4].split_at(1);
let (swath, tile) = remainder.split_at(1);
let side = side.parse::<u8>()?;
let swath = swath.parse::<u8>()?;
let tile = tile.parse::<u8>()?;
let x = cap[5].parse::<u16>()?;
let y = cap[6].parse::<u16>()?;
Ok(SequenceIdentifier {
sequencer_id,
run_count,
flow_cell_id,
lane,
side,
swath,
tile,
x,
y
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse() {
let line = "@M03745:11:000000000-B54L5:1:2108:4127:8949";
let seq_id = parse_sequence_identifier(&line).unwrap();
assert_eq!(seq_id.sequencer_id, "M03745".to_string());
assert_eq!(seq_id.run_count, 11);
assert_eq!(seq_id.flow_cell_id, "000000000-B54L5".to_string());
assert_eq!(seq_id.lane, 1);
assert_eq!(seq_id.side, 2);
assert_eq!(seq_id.swath, 1);
assert_eq!(seq_id.tile, 8);
assert_eq!(seq_id.x, 4127);
assert_eq!(seq_id.y, 8949);
}
#[test]
fn test_parse_with_newline() {
let line = "@M03745:11:000000000-B54L5:1:2108:4127:8949\n";
let seq_id = parse_sequence_identifier(&line).unwrap();
assert_eq!(seq_id.sequencer_id, "M03745".to_string());
assert_eq!(seq_id.run_count, 11);
assert_eq!(seq_id.flow_cell_id, "000000000-B54L5".to_string());
assert_eq!(seq_id.lane, 1);
assert_eq!(seq_id.side, 2);
assert_eq!(seq_id.swath, 1);
assert_eq!(seq_id.tile, 8);
assert_eq!(seq_id.x, 4127);
assert_eq!(seq_id.y, 8949);
}
#[test]
fn test_parse_error() {
let result = parse_sequence_identifier("CACGACGACTAGCTACGGACGCGGCACGACGCAG");
assert!(result.is_err());
}
}