mod alignment;
mod entity_set;
mod fasta;
mod graph;
mod intervals;
mod knowledge_graph;
mod molecule_table;
mod named_list;
mod ranked_list;
mod reads;
mod table;
mod variants;
use std::error::Error;
use std::fmt;
use std::fs::File;
use std::io::{self, BufRead, BufReader};
use std::path::{Path, PathBuf};
use flate2::bufread::MultiGzDecoder;
use crate::dim::{Dim, Organism, Release, Source, SourceSet, Subject};
pub use alignment::alignment;
pub use entity_set::entity_set;
pub use fasta::reference;
pub use intervals::{IntervalFormat, intervals};
pub use knowledge_graph::knowledge_graph;
pub use molecule_table::{molecule_ranking, molecule_set};
pub use ranked_list::ranked_list;
pub use reads::{reads, samples};
pub use variants::variants;
pub(crate) use graph::GraphReader;
pub(crate) use molecule_table::{REPRESENTATION_LINE, spelled_on_a_line};
pub(crate) use table::{Row, Table};
#[derive(Clone, Debug, Default)]
pub struct Declared {
pub organism: Option<Vec<String>>,
pub build: Option<String>,
pub samples: Option<Vec<String>>,
pub frame: Option<(String, u64)>,
pub release: Option<String>,
pub identifier_namespace: Option<String>,
pub subject: Option<String>,
pub source_set: Option<Vec<String>>,
pub representation: Option<String>,
}
pub(crate) fn organism(declared: &Declared, reason: &'static str) -> Dim<Organism> {
match declared.organism.as_deref().map(Organism::of) {
Some(organisms) if !organisms.is_empty() => Dim::known(organisms, Source::Declared),
_ => Dim::unknown(reason),
}
}
pub(crate) fn release(declared: &Declared, reason: &'static str) -> Dim<Release> {
match declared.release.as_deref().map(Release::new) {
Some(release) if !release.is_empty() => Dim::known(release, Source::Declared),
_ => Dim::unknown(reason),
}
}
pub(crate) fn subject(declared: &Declared, reason: &'static str) -> Dim<Subject> {
match declared.subject.as_deref().map(Subject::new) {
Some(subject) if !subject.is_empty() => Dim::known(subject, Source::Declared),
_ => Dim::unknown(reason),
}
}
pub(crate) fn source_set(
declared: &Declared,
found: Vec<&str>,
where_from: Source,
reason: &'static str,
) -> Dim<SourceSet> {
if let Some(sources) = declared.source_set.as_deref().map(SourceSet::of)
&& !sources.is_empty()
{
return Dim::known(sources, Source::Declared);
}
let read = SourceSet::of(found);
if read.is_empty() {
Dim::unknown(reason)
} else {
Dim::known(read, where_from)
}
}
#[derive(Debug)]
pub enum LoadError {
Io {
path: PathBuf,
source: io::Error,
},
Pairing {
detail: String,
},
Frame {
path: PathBuf,
detail: String,
},
Format {
path: PathBuf,
detail: String,
},
}
impl LoadError {
pub(crate) fn io(path: &Path, source: io::Error) -> Self {
Self::Io {
path: path.to_path_buf(),
source,
}
}
pub(crate) fn format(path: &Path, detail: impl Into<String>) -> Self {
Self::Format {
path: path.to_path_buf(),
detail: detail.into(),
}
}
pub(crate) fn frame(path: &Path, detail: impl Into<String>) -> Self {
Self::Frame {
path: path.to_path_buf(),
detail: detail.into(),
}
}
}
impl fmt::Display for LoadError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Io { path, source } => write!(f, "cannot read {}: {source}", path.display()),
Self::Format { path, detail } => {
write!(f, "cannot parse {}: {detail}", path.display())
}
Self::Frame { path, detail } => {
write!(f, "cannot interpret against {}: {detail}", path.display())
}
Self::Pairing { detail } => {
write!(f, "cannot group reads into samples: {detail}")
}
}
}
}
impl Error for LoadError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::Io { source, .. } => Some(source),
Self::Format { .. } | Self::Frame { .. } | Self::Pairing { .. } => None,
}
}
}
pub(crate) fn open(path: &Path) -> Result<Box<dyn BufRead>, LoadError> {
let file = File::open(path).map_err(|error| LoadError::io(path, error))?;
let buffered = BufReader::new(file);
if path.extension().is_some_and(|extension| extension == "gz") {
Ok(Box::new(BufReader::new(MultiGzDecoder::new(buffered))))
} else {
Ok(Box::new(buffered))
}
}
pub(crate) fn stem(path: &Path) -> String {
let mut name = path
.file_name()
.map(|name| name.to_string_lossy().into_owned())
.unwrap_or_default();
for suffix in [".gz", ".bgz"] {
if let Some(trimmed) = name.strip_suffix(suffix) {
name = trimmed.to_owned();
}
}
match name.rsplit_once('.') {
Some((base, _)) => base.to_owned(),
None => name,
}
}