-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.rs
286 lines (241 loc) · 10.9 KB
/
mod.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
277
278
279
280
281
282
283
284
285
286
use std::collections::HashMap;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use log::{warn, debug, trace};
pub mod entry;
pub use entry::MaskEntry;
pub use entry::MaskEntryError;
#[cfg(test)] pub use entry::dummy;
mod threshold;
pub use threshold::MaskThreshold;
mod error;
pub use error::MasksError;
use crate::misincorporation::Misincorporations;
use crate::genome::Orientation;
/// A [`HashMap`] collection of [`MaskThreshold`]s, mapped according to their respective [`MaskEntry`].
///
/// [`crate::mask::Masks`] contains all the entries contained within a [mapDamage-v2](https://github.com/ginolhac/mapDamage)'s
/// [`misincorporation.txt`](https://ginolhac.github.io/mapDamage/#a4) output file. These are structured through an internal
/// [`HashMap`] where:
/// - keys are [`MaskEntry`] (themselves, containing the chromosome and Strand information of the entry)
/// - values are [`MaskThreshold`]s (themselves, containing the relative threshold positions for the 5p and 3p end of a read.)
///
#[derive(Debug)]
pub struct Masks {inner: HashMap<MaskEntry, MaskThreshold>}
impl TryFrom<&Misincorporations> for Masks {
type Error = MasksError;
/// Generate a structured [`Masks`] collection from a [`Misincorporations`] reference.
///
/// # Usage
///
/// ```
/// use pmd_mask::misincorporation::Misincorporations;
/// use pmd_mask::mask::Masks;
///
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let reader = "tests/test-data/bam/dummy-MTonly/misincorporation.txt";
/// let misincorporations = Misincorporations::from_path(reader, 0.01)?;
/// let masks = Masks::try_from(&misincorporations)?;
/// Ok(())
/// }
/// ```
fn try_from(value: &Misincorporations) -> Result<Self, Self::Error> {
// ---- Restructure Misincorporation records as a HashMap<MaskEntry, MaskThreshold>
let mut masks = Self{ inner: HashMap::with_capacity(value.len()) };
for position in value.iter() {
let record = MaskEntry{chromosome: position.chromosome.clone(), strand: position.strand};
masks.inner.entry(record)
.or_insert(MaskThreshold::default())
.set_threshold(position.end, position.position);
}
masks.validate()?;
Ok(masks)
}
}
impl Masks {
/// Instantiate a [`Masks`] struct from a [mapDamage-v2](https://github.com/ginolhac/mapDamage)'s
/// [`misincorporation.txt`](https://ginolhac.github.io/mapDamage/#a4) output file and a set, user-defined
/// threshold value.
///
/// # Usage
/// ```
/// use pmd_mask::mask::Masks;
/// use std::error::Error;
/// fn main() -> Result<(), Box<dyn Error>> {
/// let file = "tests/test-data/bam/dummy-MTonly/misincorporation.txt";
/// let masks = Masks::from_path(&file, 0.01)?;
/// Ok(())
/// }
/// ```
///
/// # Errors
///
/// May return:
/// - a [`MasksError::OpenFile`] if the method failed to open the provided `misincorporations` file.
/// - any [`MasksError`] spat out from the private [`Masks::from_reader()`](Masks::from_reader) function
///
pub fn from_path(misincorporations: impl AsRef<Path>, threshold: f32) -> Result<Self, MasksError> {
let file = File::open(&misincorporations)
.map_err(|e| MasksError::OpenFile{source: e})?;
Self::from_reader(file, threshold)
}
/// Instantiate a [`Masks`] struct from a generic Reader and a set threshold. Used by [`Masks::from_path()`](Masks::from_path)
///
/// # Errors
/// - May bubble out any errors arising from [`Misincorporations::from_reader()`](Misincorporations::from_reader)
/// - May bubble out any errors arising from [`Masks::try_from::<&Misincorporations>()`]
fn from_reader<R: std::io::Read>(misincorporations: R, threshold: f32) -> Result<Self, MasksError> {
let mut threshold_positions = Misincorporations::from_reader(misincorporations, threshold)?;
// ---- Validate misincorporation and issue warnings for any 'abnormal' frequency found.
let mut abnormal_frequencies = threshold_positions
.extrude_invalid_frequencies()
.iter()
.fold(String::new(), |abnormal_freqs, pos| abnormal_freqs + &format!("{pos}\n"));
abnormal_frequencies.pop();
if !abnormal_frequencies.is_empty() {
warn!(
"Found abnormal misincorporation frequencies (NaN, Infinite values, etc.). \
Masking will apply along the full length of the sequence for those:\n{abnormal_frequencies}"
);
}
debug!("Using the following positions as threshold for masking:\n{}",
threshold_positions.iter().fold(String::new(), |acc, val| {
acc + &format!("{val}\n")
})
);
Masks::try_from(&threshold_positions)
}
/// Return the [`MaskThreshold`] of a provided [`MaskEntry`].
///
/// # Usage
/// ```
/// use std::error::Error;
/// use pmd_mask::mask::{Masks, MaskEntry};
/// use pmd_mask::genome::{ChrName, Strand};
/// fn main() -> Result<(), Box<dyn Error>> {
/// let file = "tests/test-data/bam/dummy-MTonly/misincorporation.txt";
/// let masks = Masks::from_path(&file, 0.01)?;
/// let entry = MaskEntry{ chromosome: ChrName::new("5"), strand: Strand::Reverse };
///
/// let threshold = masks.get(&entry);
/// assert!(threshold.is_some());
/// Ok(())
/// }
/// ```
pub fn get(&self, entry: &MaskEntry) -> Option<&MaskThreshold> {
self.inner.get(entry)
}
/// Iteratively validate every contained [`MaskThreshold`]s found within the [`Masks`] struct,
/// i.e. each record must contain a [`HashMap`] w/ two keys: [`Orientation::FivePrime`] and [`Orientation::ThreePrime`]
///
/// # Errors
/// May return a [`MasksError::ValidateThresholds`] if any threshold is invalid (i.e. [`MaskThreshold::validate()`] fails)
fn validate(&self) -> Result<(), MasksError> {
for (entry, threshold) in self.inner.iter() {
trace!("Validating {entry} {threshold}");
threshold.validate().map_err(|e|MasksError::ValidateThresholds(entry.clone(), e))?
};
Ok(())
}
/// Serialize the contents of this struct within a writer.
/// Output is headed and Fields are '<Chr> <Std> <5p> <3p>'
/// ```
/// use std::error::Error;
/// use pmd_mask::mask::{Masks, MaskEntry};
/// use pmd_mask::genome::{ChrName, Strand};
/// fn main() -> Result<(), Box<dyn Error>> {
/// let file = "tests/test-data/bam/dummy-MTonly/misincorporation.txt";
/// let masks = Masks::from_path(&file, 0.01)?;
///
/// // ---- Write contents into cursor (or any output file)
/// let mut output = std::io::Cursor::new(Vec::new());
/// masks.write(&mut output)?;
///
/// // ---- Ensure utf8 validity
/// let raw_output = String::from_utf8(output.into_inner())?;
/// Ok(())
/// }
/// ```
pub fn write(&self, writer: &mut impl Write) -> std::io::Result<()> {
use threshold::ORIENTATIONS;
// ---- Write header
let header = format!("Chr\tStd\t{}\t{}\n", ORIENTATIONS[0], ORIENTATIONS[1]);
writer.write_all(header.as_bytes())?;
// ---- Sort entries according to Entry
let mut sorted = self.inner.iter().collect::<Vec<_>>();
sorted.sort_by(|a, b| a.0.cmp(b.0));
// ---- Write entries in order.
for (entry, threshold) in sorted.iter() {
let mut output_string = format!("{}\t{}", entry.chromosome, entry.strand);
for end in [Orientation::FivePrime, Orientation::ThreePrime] {
let position = match threshold.get_threshold(&end).expect("Missing threshold").inner() {
usize::MAX => "\tNA".to_string(),
position => format!("\t{position}"),
};
output_string.push_str(&position);
}
output_string.push('\n');
writer.write_all(output_string.as_bytes())?;
}
writer.flush()?;
Ok(())
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::mask::threshold::MaskThresholdError;
use crate::misincorporation::MisincorporationRecord;
use crate::genome::{Strand::*, Orientation::*, ChrName, Position};
use crate::mis_record;
fn dummy_misincorporations() -> Misincorporations {
Misincorporations::from_iter(vec![
mis_record!("chr1", Forward, FivePrime, 10, 500, 0.01),
mis_record!("chr1", Forward, ThreePrime, 12, 800, 0.01),
mis_record!("chr1", Reverse, FivePrime, 8, 1000, 0.01),
mis_record!("chr1", Reverse, ThreePrime, 6, 600, 0.01),
].into_iter())
}
#[test]
pub fn try_from_misincorporations() {
let misincorporations = dummy_misincorporations();
let mask = Masks::try_from(&misincorporations).expect("Invalid Masks");
for i in 0..misincorporations.len() {
let target_record = misincorporations.get(i).expect("Missing misincorporation record");
let expected_mask = MaskEntry{chromosome: target_record.chromosome.clone(), strand: target_record.strand};
let got = &mask.inner[&expected_mask];
assert_eq!(got.get_threshold(&target_record.end).expect("Missing Mask threshold"), &target_record.position);
}
}
#[test]
fn get_threshold() {
let mut masks = Masks{inner: HashMap::new()};
let entry = MaskEntry{chromosome: ChrName::new("chr1"), strand: Forward};
masks.inner.insert(entry.clone(), MaskThreshold::default());
assert_eq!(masks.get(&entry), Some(&MaskThreshold::default()));
masks.inner.get_mut(&entry).expect("Missing entry").set_threshold(FivePrime, Position::new(10));
assert_ne!(masks.get(&entry), Some(&MaskThreshold::default()));
}
#[test]
fn validate() {
let mut masks = Masks::try_from(&dummy_misincorporations()).expect("Invalid Misincorporations");
match masks.validate() {
Ok(()) => {},
_ => panic!("Invalid mask")
};
let bad_entry = MaskEntry{chromosome: ChrName::new("chr1"), strand: Reverse};
let funky_threshold = masks.inner.get_mut(&bad_entry).expect("Entry not found");
*funky_threshold = MaskThreshold{inner: HashMap::new()};
match masks.validate() {
Ok(()) => panic!("Masks should be invalid at this point."),
Err(e) => match e {
MasksError::ValidateThresholds(entry, treshold_err) => {
assert_eq!(bad_entry, entry);
assert_eq!(treshold_err, MaskThresholdError::ValidateThresh { got: 0, want: 2 })
},
_ => panic!("Invalid error type.")
}
}
}
}