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
//! Biological data types, and which dimensions each one can speak to.
use std::path::{Path, PathBuf};
use crate::dim::{Dimension, Dimensions};
/// What kind of biological data a dataset holds.
///
/// These are the types in the golden example, plus intervals: the reference
/// demo does not use them, but they are where the `coordinates` dimension
/// earns its keep.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DataKind {
/// Unaligned sequencing reads (FASTQ).
Reads,
/// A reference sequence (FASTA).
Reference,
/// Reads placed on a reference (SAM/BAM).
Alignment,
/// Variant calls (VCF).
Variants,
/// Genomic intervals (BED/GFF).
Intervals,
/// An interpreted drug-resistance report.
ResistanceReport,
}
impl DataKind {
/// Every kind, so the Python layer can enumerate them.
pub const ALL: [Self; 6] = [
Self::Reads,
Self::Reference,
Self::Alignment,
Self::Variants,
Self::Intervals,
Self::ResistanceReport,
];
/// The name used in messages and in the Python API.
#[must_use]
pub const fn name(self) -> &'static str {
match self {
Self::Reads => "Reads",
Self::Reference => "Reference",
Self::Alignment => "Alignment",
Self::Variants => "Variants",
Self::Intervals => "Intervals",
Self::ResistanceReport => "ResistanceReport",
}
}
/// Look a kind up by the name [`Self::name`] gives it.
///
/// Used by the Python layer to turn a step's type annotation into a
/// kind, so the two surfaces cannot drift apart.
#[must_use]
pub fn from_name(name: &str) -> Option<Self> {
Self::ALL.into_iter().find(|kind| kind.name() == name)
}
/// Whether a dimension means anything for this kind.
///
/// This encodes the `n/a` cells of the table in `docs/dimensions.md`. A
/// dimension that does not apply is never compared and never demanded:
/// asking a FASTA for its quality encoding is not a gap to be filled, it
/// is a category error.
#[must_use]
pub const fn applies(self, dimension: Dimension) -> bool {
match (self, dimension) {
// Organism and sample set are meaningful everywhere they can be
// stated; a report inherits both from what it was built from.
(_, Dimension::Organism) => true,
(Self::Reads, Dimension::SampleSet | Dimension::Pairing) => true,
(Self::Reads, Dimension::QualityEncoding) => true,
(Self::Reads, _) => false,
(Self::Reference, Dimension::Reference | Dimension::ContigNaming) => true,
(Self::Reference, _) => false,
// Pairing applies to an alignment (the SAM flags record it), but
// inferring it means reading records rather than headers, which
// M1 does not do. It therefore stays unknown for now.
(
Self::Alignment,
Dimension::Reference
| Dimension::ContigNaming
| Dimension::Coordinates
| Dimension::SampleSet
| Dimension::Pairing,
) => true,
(Self::Alignment, _) => false,
(
Self::Variants,
Dimension::Reference
| Dimension::ContigNaming
| Dimension::Coordinates
| Dimension::SampleSet,
) => true,
(Self::Variants, _) => false,
// An interval file describes regions, not samples: no sample
// set, no pairing, no quality scores.
(
Self::Intervals,
Dimension::Reference | Dimension::ContigNaming | Dimension::Coordinates,
) => true,
(Self::Intervals, _) => false,
(Self::ResistanceReport, Dimension::SampleSet) => true,
(Self::ResistanceReport, _) => false,
}
}
}
/// A piece of data, its kind, and the dimensions it carries.
#[derive(Clone, Debug)]
pub struct Dataset {
kind: DataKind,
path: Option<PathBuf>,
/// The other files of the same dataset: the second mate of a pair.
///
/// Kept beside `path` rather than replacing it because every other kind
/// of data is one file, and a dataset's *primary* file is what a
/// message names.
mates: Vec<PathBuf>,
dimensions: Dimensions,
}
impl Dataset {
/// Build a dataset from its kind and dimensions.
#[must_use]
pub const fn new(kind: DataKind, path: Option<PathBuf>, dimensions: Dimensions) -> Self {
Self {
kind,
path,
mates: Vec::new(),
dimensions,
}
}
/// The same dataset, with a second file belonging to it.
#[must_use]
pub fn with_mate(mut self, mate: PathBuf) -> Self {
self.mates.push(mate);
self
}
/// What kind of data this is.
#[must_use]
pub const fn kind(&self) -> DataKind {
self.kind
}
/// The file it was loaded from, if it came from one.
#[must_use]
pub fn path(&self) -> Option<&Path> {
self.path.as_deref()
}
/// Every file this dataset is, in order.
///
/// One file for everything except a paired sample, which is two — and
/// their order is the pairing, so a step is handed `r1` and `r2` the way
/// round they were read.
#[must_use]
pub fn files(&self) -> Vec<PathBuf> {
self.path.iter().chain(self.mates.iter()).cloned().collect()
}
/// The dimensions it carries.
#[must_use]
pub const fn dimensions(&self) -> &Dimensions {
&self.dimensions
}
}