mod names;
mod namespace;
mod reference;
mod representation;
use std::fmt;
use std::path::PathBuf;
pub(crate) use names::english_list;
pub use names::{
Exactly, FoldsAsciiCase, IdentifierNamespace, Label, Matching, NameSet, Organism, Release,
SourceSet, Subject,
};
pub use namespace::{Inference, KnownNamespace};
pub use reference::{Contig, ContigOrder, Frame, ReferenceId, Relation, Verdict, relate};
pub use representation::{Identity, Representation, Standardisation};
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Source {
Inferred {
path: PathBuf,
detail: &'static str,
},
Declared,
Derived {
step: String,
},
}
impl Source {
#[must_use]
pub fn describe(&self) -> String {
match self {
Self::Inferred { path, detail } => {
format!("from {detail}: {}", display_path(path))
}
Self::Declared => "declared".to_owned(),
Self::Derived { step } => format!("from step `{step}`"),
}
}
}
pub(crate) fn display_path(path: &std::path::Path) -> String {
std::env::current_dir()
.ok()
.and_then(|cwd| path.strip_prefix(cwd).ok())
.unwrap_or(path)
.display()
.to_string()
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Dim<T> {
Known {
value: T,
source: Source,
},
Unknown {
reason: &'static str,
},
}
impl<T> Dim<T> {
pub const fn known(value: T, source: Source) -> Self {
Self::Known { value, source }
}
#[must_use]
pub const fn unknown(reason: &'static str) -> Self {
Self::Unknown { reason }
}
#[must_use]
pub const fn value(&self) -> Option<&T> {
match self {
Self::Known { value, .. } => Some(value),
Self::Unknown { .. } => None,
}
}
#[must_use]
pub const fn source(&self) -> Option<&Source> {
match self {
Self::Known { source, .. } => Some(source),
Self::Unknown { .. } => None,
}
}
#[must_use]
pub const fn reason(&self) -> Option<&'static str> {
match self {
Self::Known { .. } => None,
Self::Unknown { reason } => Some(reason),
}
}
#[must_use]
pub const fn is_known(&self) -> bool {
matches!(self, Self::Known { .. })
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Dimension {
Organism,
Reference,
ContigNaming,
Coordinates,
SampleSet,
Pairing,
QualityEncoding,
Release,
Subject,
SourceSet,
IdentifierNamespace,
Representation,
}
impl Dimension {
pub const ALL: [Self; 12] = [
Self::Organism,
Self::Reference,
Self::ContigNaming,
Self::Coordinates,
Self::SampleSet,
Self::Pairing,
Self::QualityEncoding,
Self::Release,
Self::Subject,
Self::SourceSet,
Self::IdentifierNamespace,
Self::Representation,
];
#[must_use]
pub const fn name(self) -> &'static str {
match self {
Self::Organism => "organism",
Self::Reference => "reference",
Self::ContigNaming => "contig_naming",
Self::Coordinates => "coordinates",
Self::SampleSet => "sample_set",
Self::Pairing => "pairing",
Self::QualityEncoding => "quality_encoding",
Self::Release => "release",
Self::Subject => "subject",
Self::SourceSet => "source_set",
Self::IdentifierNamespace => "identifier_namespace",
Self::Representation => "representation",
}
}
#[must_use]
pub fn from_name(name: &str) -> Option<Self> {
Self::ALL
.into_iter()
.find(|dimension| dimension.name() == name)
}
#[must_use]
pub const fn leading_question(self) -> &'static str {
match self {
Self::Organism => "These must match. Did you pass data from the wrong organism?",
Self::Reference => "These must match. Did you pass the wrong reference?",
Self::ContigNaming => {
"The contigs are otherwise identical. Add a renaming step, or use one \
naming style throughout."
}
Self::Coordinates => {
"These must match. Did you mean to convert between coordinate systems?"
}
Self::SampleSet => "These must match, in order. Did the sample order change upstream?",
Self::Pairing => "These must match. Did you mix single-end and paired-end reads?",
Self::QualityEncoding => "These must match. Did you mix Phred+33 and Phred+64 reads?",
Self::Release => "These must match. Did one input come from a different release?",
Self::Subject => "These must match. Did you join data about different subjects?",
Self::SourceSet => "These must match. Was a source added or removed upstream?",
Self::IdentifierNamespace => {
"These must match. Add a `map_identifiers` step; identifiers are never \
renamed implicitly."
}
Self::Representation => {
"These must match. Were the two sets standardised differently? Add a \
`standardize` step; molecules are never re-keyed implicitly."
}
}
}
}
impl fmt::Display for Dimension {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.name())
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ContigNaming {
Prefixed,
Bare,
Mixed,
}
impl ContigNaming {
#[must_use]
pub const fn describe(self) -> &'static str {
match self {
Self::Prefixed => "chr-prefixed (UCSC style)",
Self::Bare => "bare (Ensembl/NCBI style)",
Self::Mixed => "mixed",
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CoordinateSystem {
ZeroBasedHalfOpen,
OneBasedClosed,
}
impl CoordinateSystem {
pub const ALL: [Self; 2] = [Self::ZeroBasedHalfOpen, Self::OneBasedClosed];
#[must_use]
pub const fn describe(self) -> &'static str {
match self {
Self::ZeroBasedHalfOpen => "0-based half-open",
Self::OneBasedClosed => "1-based closed",
}
}
#[must_use]
pub fn from_name(name: &str) -> Option<Self> {
Self::ALL
.into_iter()
.find(|system| system.describe() == name.trim())
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SampleSet(Vec<String>);
impl SampleSet {
#[must_use]
pub const fn new(samples: Vec<String>) -> Self {
Self(samples)
}
#[must_use]
pub fn as_slice(&self) -> &[String] {
&self.0
}
#[must_use]
pub fn same_members(&self, other: &Self) -> bool {
let mut mine = self.0.clone();
let mut theirs = other.0.clone();
mine.sort_unstable();
theirs.sort_unstable();
mine == theirs
}
}
impl fmt::Display for SampleSet {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[{}]", self.0.join(", "))
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Pairing {
Single,
Paired,
}
impl Pairing {
#[must_use]
pub const fn describe(self) -> &'static str {
match self {
Self::Single => "single-end",
Self::Paired => "paired-end",
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum QualityEncoding {
Phred33,
Phred64,
}
impl QualityEncoding {
#[must_use]
pub const fn describe(self) -> &'static str {
match self {
Self::Phred33 => "Phred+33",
Self::Phred64 => "Phred+64",
}
}
}
#[derive(Clone, Debug)]
pub struct Dimensions {
pub organism: Dim<Organism>,
pub reference: Dim<ReferenceId>,
pub contig_naming: Dim<ContigNaming>,
pub coordinates: Dim<CoordinateSystem>,
pub sample_set: Dim<SampleSet>,
pub pairing: Dim<Pairing>,
pub quality_encoding: Dim<QualityEncoding>,
pub release: Dim<Release>,
pub subject: Dim<Subject>,
pub source_set: Dim<SourceSet>,
pub identifier_namespace: Dim<IdentifierNamespace>,
pub representation: Dim<Representation>,
}
impl Default for Dimensions {
fn default() -> Self {
const NOT_SET: &str = "not recorded in the file and not declared";
Self {
organism: Dim::unknown(NOT_SET),
reference: Dim::unknown(NOT_SET),
contig_naming: Dim::unknown(NOT_SET),
coordinates: Dim::unknown(NOT_SET),
sample_set: Dim::unknown(NOT_SET),
pairing: Dim::unknown(NOT_SET),
quality_encoding: Dim::unknown(NOT_SET),
release: Dim::unknown(NOT_SET),
subject: Dim::unknown(NOT_SET),
source_set: Dim::unknown(NOT_SET),
identifier_namespace: Dim::unknown(NOT_SET),
representation: Dim::unknown(NOT_SET),
}
}
}
impl Dimensions {
#[must_use]
pub fn render(&self, dimension: Dimension) -> Option<String> {
match dimension {
Dimension::Organism => self.organism.value().map(ToString::to_string),
Dimension::Reference => self.reference.value().map(ReferenceId::describe),
Dimension::ContigNaming => self
.contig_naming
.value()
.map(|naming| naming.describe().to_owned()),
Dimension::Coordinates => self
.coordinates
.value()
.map(|system| system.describe().to_owned()),
Dimension::SampleSet => self.sample_set.value().map(ToString::to_string),
Dimension::Pairing => self.pairing.value().map(|p| p.describe().to_owned()),
Dimension::QualityEncoding => self
.quality_encoding
.value()
.map(|encoding| encoding.describe().to_owned()),
Dimension::Release => self.release.value().map(ToString::to_string),
Dimension::Subject => self.subject.value().map(ToString::to_string),
Dimension::SourceSet => self.source_set.value().map(ToString::to_string),
Dimension::IdentifierNamespace => {
self.identifier_namespace.value().map(ToString::to_string)
}
Dimension::Representation => self.representation.value().map(ToString::to_string),
}
}
#[must_use]
pub const fn source(&self, dimension: Dimension) -> Option<&Source> {
match dimension {
Dimension::Organism => self.organism.source(),
Dimension::Reference => self.reference.source(),
Dimension::ContigNaming => self.contig_naming.source(),
Dimension::Coordinates => self.coordinates.source(),
Dimension::SampleSet => self.sample_set.source(),
Dimension::Pairing => self.pairing.source(),
Dimension::QualityEncoding => self.quality_encoding.source(),
Dimension::Release => self.release.source(),
Dimension::Subject => self.subject.source(),
Dimension::SourceSet => self.source_set.source(),
Dimension::IdentifierNamespace => self.identifier_namespace.source(),
Dimension::Representation => self.representation.source(),
}
}
#[must_use]
pub const fn reason(&self, dimension: Dimension) -> Option<&'static str> {
match dimension {
Dimension::Organism => self.organism.reason(),
Dimension::Reference => self.reference.reason(),
Dimension::ContigNaming => self.contig_naming.reason(),
Dimension::Coordinates => self.coordinates.reason(),
Dimension::SampleSet => self.sample_set.reason(),
Dimension::Pairing => self.pairing.reason(),
Dimension::QualityEncoding => self.quality_encoding.reason(),
Dimension::Release => self.release.reason(),
Dimension::Subject => self.subject.reason(),
Dimension::SourceSet => self.source_set.reason(),
Dimension::IdentifierNamespace => self.identifier_namespace.reason(),
Dimension::Representation => self.representation.reason(),
}
}
#[must_use]
pub const fn is_known(&self, dimension: Dimension) -> bool {
self.source(dimension).is_some()
}
}