use std::fmt;
const DATA_EXTENSIONS: [&str; 13] = [
"bam", "cram", "sam", "vcf", "bcf", "fastq", "fq", "fasta", "fa", "fna", "bed", "gff", "gtf",
];
const COMPRESSION: [&str; 3] = ["gz", "bgz", "bz2"];
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
enum Shape {
Absolute,
Relative,
Name,
}
impl Shape {
const fn describe(self) -> &'static str {
match self {
Self::Absolute => "an absolute path",
Self::Relative => "a file path",
Self::Name => "a file name",
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Note {
SampleIsAFile {
sample: String,
},
}
impl Note {
#[must_use]
pub fn about_sample(sample: &str) -> Option<Self> {
shape(sample).map(|_| Self::SampleIsAFile {
sample: sample.to_owned(),
})
}
#[must_use]
pub const fn code(&self) -> &'static str {
match self {
Self::SampleIsAFile { .. } => "sample-is-a-file",
}
}
#[must_use]
pub fn value(&self) -> &str {
match self {
Self::SampleIsAFile { sample } => sample,
}
}
#[must_use]
pub fn parse(code: &str, value: &str) -> Option<Self> {
match code {
"sample-is-a-file" => Some(Self::SampleIsAFile {
sample: value.to_owned(),
}),
_ => None,
}
}
#[must_use]
pub const fn advice(&self) -> &'static str {
match self {
Self::SampleIsAFile { .. } => {
"Write the read group — `samtools addreplacerg -r ID:<sample> -r SM:<sample>` — \
so the calls below it name the sample."
}
}
}
#[must_use]
pub const fn consequence(&self) -> &'static str {
match self {
Self::SampleIsAFile { .. } => {
"A sample set is a dimension, so the identifier is hashed exactly as the file \
writes it. The canonical hash of this output therefore depends on the file it \
was named after, which is not the same file in two places: a bundle written \
here cannot verify elsewhere, and a methods section built from it names a \
file where it should name a sample."
}
}
}
}
impl fmt::Display for Note {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::SampleIsAFile { sample } => {
let shape = shape(sample).map_or("a file", Shape::describe);
write!(
f,
"the sample identifier `{sample}` is {shape}, not a sample"
)
}
}
}
}
fn shape(sample: &str) -> Option<Shape> {
let name = sample.trim();
if name.is_empty() {
return None;
}
if name.starts_with('/') {
return Some(Shape::Absolute);
}
if name.contains('/') || name.contains('\\') {
return Some(Shape::Relative);
}
names_a_format(name).then_some(Shape::Name)
}
fn names_a_format(name: &str) -> bool {
let lowered = name.to_ascii_lowercase();
let mut stem = lowered.as_str();
for suffix in COMPRESSION {
if let Some(trimmed) = stem.strip_suffix(suffix)
&& trimmed.ends_with('.')
{
stem = &trimmed[..trimmed.len() - 1];
break;
}
}
stem.rsplit_once('.')
.is_some_and(|(_, extension)| DATA_EXTENSIONS.contains(&extension))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_path_inside_the_store_is_a_file() {
let sample = "/Users/kelyn/ligase/.ligase/store/62/7db9fa0cbac.bam";
assert_eq!(shape(sample), Some(Shape::Absolute));
assert!(Note::about_sample(sample).is_some());
}
#[test]
fn a_relative_path_and_a_bare_file_name_are_files_too() {
assert_eq!(shape("alignments/sample_a.bam"), Some(Shape::Relative));
assert_eq!(shape(r"C:\data\sample_a.bam"), Some(Shape::Relative));
assert_eq!(shape("sample_a.bam"), Some(Shape::Name));
assert_eq!(shape("calls.vcf.gz"), Some(Shape::Name));
assert_eq!(shape("READS.FASTQ.GZ"), Some(Shape::Name));
}
#[test]
fn an_ordinary_sample_identifier_is_left_alone() {
for sample in [
"SRR8525904",
"sample_a",
"HG002",
"patient-04",
"K03455.1",
"HG002.final",
"bam",
"",
" ",
] {
assert_eq!(shape(sample), None, "{sample} is a sample identifier");
assert_eq!(Note::about_sample(sample), None, "{sample}");
}
}
#[test]
fn a_note_round_trips_through_its_code_and_value() {
let note = Note::SampleIsAFile {
sample: "/tmp/x.bam".to_owned(),
};
assert_eq!(Note::parse(note.code(), note.value()), Some(note));
}
#[test]
fn a_code_from_a_later_version_is_not_a_note_here() {
assert_eq!(Note::parse("something-else", "x"), None);
}
#[test]
fn a_note_says_what_it_found_and_what_to_do() {
let note = Note::SampleIsAFile {
sample: "/tmp/x.bam".to_owned(),
};
assert!(note.to_string().contains("/tmp/x.bam"));
assert!(note.to_string().contains("an absolute path"));
assert!(note.advice().contains("addreplacerg"));
assert!(note.consequence().contains("dimension"));
}
}