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
use std::convert::Infallible;
use std::fmt::Display;
use std::str::FromStr;
use polars::prelude::*;
use crate::utils::{
hashmap_from_arrays,
schema_from_arrays,
};
/// Supported methylation report file formats.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ReportType {
/// Bismark methylation extractor output format
Bismark,
/// CG methylation map format
CgMap,
/// BedGraph methylation density format
BedGraph,
/// Coverage report format with methylated/unmethylated counts
Coverage,
}
impl FromStr for ReportType {
type Err = Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"bismark" => Ok(ReportType::Bismark),
"cgmap" => Ok(ReportType::CgMap),
"bedgraph" => Ok(ReportType::BedGraph),
"coverage" => Ok(ReportType::Coverage),
_ => unimplemented!(),
}
}
}
impl Display for ReportType {
fn fmt(
&self,
f: &mut std::fmt::Formatter<'_>,
) -> std::fmt::Result {
let str = match self {
ReportType::Bismark => String::from("bismark"),
ReportType::CgMap => String::from("cgmap"),
ReportType::BedGraph => String::from("bedgraph"),
ReportType::Coverage => String::from("coverage"),
};
write!(f, "{}", str)
}
}
impl ReportType {
/// Returns column names for this report format.
pub const fn col_names(&self) -> &[&'static str] {
match self {
Self::Bismark => {
&[
"chr", "position", "strand", "count_m", "count_um", "context",
"trinuc",
]
},
Self::Coverage => {
&["chr", "start", "end", "density", "count_m", "count_um"]
},
Self::CgMap => {
&[
"chr",
"nuc",
"position",
"context",
"dinuc",
"density",
"count_m",
"count_total",
]
},
Self::BedGraph => &["chr", "start", "end", "density"],
}
}
/// Returns data types for each column.
#[cfg_attr(coverage_nightly, coverage(off))]
pub const fn col_types(&self) -> &[DataType] {
match self {
Self::Bismark => {
&[
DataType::String, // chr
DataType::UInt32, // position
DataType::String, // strand
DataType::UInt16, // count_m
DataType::UInt16, // count_um
DataType::String, // context
DataType::String, // trinuc
]
},
Self::CgMap => {
&[
DataType::String, // chr
DataType::String, // nuc
DataType::UInt32, // position
DataType::String, // context
DataType::String, // dinuc
DataType::Float32, // density
DataType::UInt16, // count_m
DataType::UInt16, // count_total
]
},
Self::BedGraph => {
&[
DataType::String, // chr
DataType::UInt32, // start
DataType::UInt32, // end
DataType::Float32, // density
]
},
Self::Coverage => {
&[
DataType::String, // chr
DataType::UInt32, // start
DataType::UInt32, // end
DataType::Float32, // density
DataType::UInt16, // count_m
DataType::UInt16, // count_um
]
},
}
}
/// Returns chromosome column name.
#[cfg_attr(coverage_nightly, coverage(off))]
pub const fn chr_col(&self) -> &'static str {
match self {
Self::Bismark | Self::BedGraph | Self::Coverage | Self::CgMap => "chr",
}
}
/// Returns position column name.
#[cfg_attr(coverage_nightly, coverage(off))]
pub const fn position_col(&self) -> &'static str {
match self {
Self::Coverage | Self::BedGraph => "start",
Self::Bismark | Self::CgMap => "position",
}
}
/// Returns context column name if available.
#[cfg_attr(coverage_nightly, coverage(off))]
pub const fn context_col(&self) -> Option<&'static str> {
match self {
Self::Bismark | Self::CgMap => Some("context"),
Self::BedGraph | Self::Coverage => None,
}
}
/// Returns strand column name if available.
#[cfg_attr(coverage_nightly, coverage(off))]
pub const fn strand_col(&self) -> Option<&'static str> {
match self {
Self::Bismark | Self::CgMap => Some("strand"),
Self::BedGraph | Self::Coverage => None,
}
}
/// Whether this format needs alignment when processing.
#[cfg_attr(coverage_nightly, coverage(off))]
pub const fn need_align(&self) -> bool {
match self {
Self::Bismark | Self::CgMap => false,
Self::BedGraph | Self::Coverage => true,
}
}
/// Creates a Polars Schema for this format.
pub fn schema(&self) -> Schema {
schema_from_arrays(self.col_names(), self.col_types())
}
/// Creates a HashMap of column names to data types.
pub fn hashmap(&self) -> PlHashMap<&str, DataType> {
hashmap_from_arrays(self.col_names(), self.col_types())
}
/// Creates CSV read options for this format.
pub fn read_options(&self) -> CsvReadOptions {
let mut read_options = CsvReadOptions::default()
.with_has_header(false) // None of the supported formats have headers
.with_schema(Some(SchemaRef::from(self.schema())))
.with_parse_options({
CsvParseOptions::default()
.with_separator(b'\t')
.with_try_parse_dates(false)
.with_quote_char(Some(b'#'))
});
// BedGraph format has a header line that needs to be skipped
if let Self::BedGraph = self {
read_options = read_options.with_skip_rows(1);
};
read_options
}
}