use crate::Dataset;
use crate::processed::ProcessedValidationError;
use crate::raw::{RawDataset, RawFormat};
use crate::read_error::{ReadError, ReadErrorReason};
use crate::reading::resolver::{ResolvedCandidate, resolve_and_select};
use crate::{ReadLimits, processed};
use std::path::{Path, PathBuf};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum DatasetKind {
Raw,
Processed,
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum FormatFamily {
BrukerTopSpin,
Varian,
JeolDelta,
JcampDx,
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum Format {
Raw(RawFormat),
Processed(processed::Format),
}
impl Format {
pub fn kind(self) -> DatasetKind {
match self {
Self::Raw(_) => DatasetKind::Raw,
Self::Processed(_) => DatasetKind::Processed,
}
}
pub fn family(self) -> FormatFamily {
match self {
Self::Raw(RawFormat::BrukerRaw) | Self::Processed(processed::Format::BrukerTopSpin) => {
FormatFamily::BrukerTopSpin
}
Self::Raw(RawFormat::VarianRaw) => FormatFamily::Varian,
Self::Raw(RawFormat::JeolDelta) | Self::Processed(processed::Format::JeolDelta) => {
FormatFamily::JeolDelta
}
Self::Processed(processed::Format::JcampDx) => FormatFamily::JcampDx,
}
}
}
impl From<RawFormat> for Format {
fn from(value: RawFormat) -> Self {
Self::Raw(value)
}
}
impl From<processed::Format> for Format {
fn from(value: processed::Format) -> Self {
Self::Processed(value)
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct DatasetIdentity {
subject: Option<String>,
acquisition: Option<String>,
source_label: Option<String>,
}
impl DatasetIdentity {
pub(crate) fn new(
subject: Option<String>,
acquisition: Option<String>,
source_label: Option<String>,
) -> Self {
Self {
subject: clean(subject),
acquisition: clean(acquisition),
source_label: clean(source_label),
}
}
pub fn subject(&self) -> Option<&str> {
self.subject.as_deref()
}
pub fn acquisition(&self) -> Option<&str> {
self.acquisition.as_deref()
}
pub fn source_label(&self) -> Option<&str> {
self.source_label.as_deref()
}
}
fn clean(value: Option<String>) -> Option<String> {
value
.map(|text| text.trim().to_owned())
.filter(|text| !text.is_empty())
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum MetadataField {
Subject,
Acquisition,
SourceLabel,
Nucleus,
SpectralWidth,
ObserveFrequency,
ReferenceFrequency,
Offset,
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum WarningImpact {
Identity,
AxisCalibration,
AxisAnnotation,
}
#[non_exhaustive]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ReadWarning {
#[non_exhaustive]
ExperimentalVendorSemantics {
format: Format,
details: Vec<String>,
},
#[non_exhaustive]
MissingOptionalSource {
kind: crate::provenance::SourceKind,
role: String,
path: PathBuf,
impact: WarningImpact,
},
#[non_exhaustive]
MissingMetadata {
field: MetadataField,
axis: Option<usize>,
impact: WarningImpact,
},
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum ReadPreference {
PreferProcessed,
PreferRaw,
#[default]
RequireUnique,
}
#[derive(Clone, Debug, Default)]
pub struct ReadOptions {
format: Option<Format>,
preference: ReadPreference,
limits: ReadLimits,
varian: Option<crate::formats::varian::ReadAssertions>,
experimental_vendor_semantics: bool,
sampling: Option<std::sync::Arc<crate::SamplingDeclaration>>,
}
impl ReadOptions {
pub fn new() -> Self {
Self::default()
}
pub fn allow_experimental_vendor_semantics(mut self, value: bool) -> Self {
self.experimental_vendor_semantics = value;
self
}
pub fn format(mut self, value: Format) -> Self {
self.format = Some(value);
self
}
pub fn preference(mut self, value: ReadPreference) -> Self {
self.preference = value;
self
}
pub fn limits(mut self, value: ReadLimits) -> Self {
self.limits = value;
self
}
pub fn varian_assertions(mut self, value: crate::formats::varian::ReadAssertions) -> Self {
self.varian = Some(value);
self
}
pub fn sampling_declaration(mut self, value: crate::SamplingDeclaration) -> Self {
self.sampling = Some(std::sync::Arc::new(value));
self
}
pub fn detect(&self, path: impl AsRef<Path>) -> Result<Format, ReadError> {
Ok(resolve_and_select(path.as_ref(), self.format, self.preference)?.format)
}
pub fn read(&self, path: impl AsRef<Path>) -> Result<Dataset, ReadError> {
self.read_with_context(path, &mut crate::ExecutionContext::default())
}
pub fn read_with_context(
&self,
path: impl AsRef<Path>,
control: &mut crate::ExecutionContext<'_>,
) -> Result<Dataset, ReadError> {
control.begin(crate::execution::ExecutionStage::Reading, None, None)?;
let selected_path = path.as_ref().to_path_buf();
let candidate = resolve_and_select(path.as_ref(), self.format, self.preference)?;
self.read_candidate(candidate, selected_path, control)
}
fn read_candidate(
&self,
candidate: ResolvedCandidate,
selected_path: PathBuf,
control: &mut crate::ExecutionContext<'_>,
) -> Result<Dataset, ReadError> {
control.check_cancelled()?;
candidate.validate_required()?;
let limits = if let Some(sampling) = &self.sampling {
if !matches!(
candidate.format,
Format::Raw(RawFormat::BrukerRaw | RawFormat::JeolDelta)
) {
return Err(ReadError::sampling_declaration(
"sampling declarations require supported raw Bruker or JEOL NUS input",
));
}
super::sampling::limits(sampling, self.limits)?
} else {
self.limits
};
let (dataset, identity, mut warnings) = match candidate.format {
Format::Raw(_) => {
let opened = candidate.open_raw(
&limits,
self.varian.as_ref(),
self.experimental_vendor_semantics,
self.sampling.as_ref(),
control.cancellation(),
)?;
let value = opened.into_dataset_with_context(control)?;
let identity = raw_identity(&candidate, &value);
let warnings = Vec::new();
(
Dataset::from_raw_with_context(value, control)?,
identity,
warnings,
)
}
Format::Processed(format) => {
let result = crate::reading::processed::read(
control,
&candidate,
format,
self.limits,
self.experimental_vendor_semantics,
)?;
(
Dataset::from_processed(result.dataset),
candidate.identity(result.acquisition),
result.warnings,
)
}
};
if candidate.format.family() == FormatFamily::JeolDelta {
let parameters = if let Some(raw) = dataset.as_raw() {
raw.provenance().source_metadata().as_jeol()
} else {
dataset.as_processed().and_then(|processed| {
processed
.provenance()
.source_metadata()
.jeol_delta()
.map(|metadata| metadata.parameters())
})
};
warnings.push(ReadWarning::ExperimentalVendorSemantics {
format: candidate.format,
details: crate::formats::jeol::experimental_details(
parameters.expect("JEOL readers retain vendor metadata"),
candidate.format.kind(),
dataset
.as_raw()
.is_some_and(|raw| raw.sampling_schedule().is_some()),
),
});
}
control.check_cancelled()?;
dataset.attach_read_context(candidate.format, identity, selected_path, warnings)
}
}
fn raw_identity(candidate: &ResolvedCandidate, dataset: &RawDataset) -> DatasetIdentity {
match candidate.format {
Format::Raw(RawFormat::BrukerRaw) => {
let acquisition = dataset
.provenance()
.source_metadata()
.as_bruker()
.and_then(|parameters| parameters.direct().get("EXP"))
.map(clean_vendor_text);
candidate.identity(acquisition)
}
Format::Raw(RawFormat::VarianRaw) => {
let subject = dataset
.provenance()
.source_metadata()
.as_varian()
.and_then(|parameters| {
["samplename", "sample", "name", "filename"]
.into_iter()
.find_map(|name| {
parameters
.get(name)
.and_then(|record| record.values().first())
.and_then(crate::formats::varian::ParameterValue::as_str)
.map(str::to_owned)
})
})
.or_else(|| {
candidate
.root
.extension()
.is_some_and(|value| value.eq_ignore_ascii_case("fid"))
.then(|| {
candidate
.root
.file_stem()
.map(|value| value.to_string_lossy().into_owned())
})
.flatten()
});
DatasetIdentity::new(subject, None, None)
}
Format::Raw(RawFormat::JeolDelta) => {
let acquisition =
dataset
.provenance()
.source_metadata()
.as_jeol()
.and_then(|parameters| {
["experiment", "content"].into_iter().find_map(|name| {
parameters
.get(name)
.and_then(|record| match record.value() {
crate::formats::jeol::ParameterValue::String(value) => {
Some(clean_jxp(value))
}
_ => None,
})
})
});
DatasetIdentity::new(None, acquisition, None)
}
Format::Processed(_) => unreachable!(),
}
}
fn clean_jxp(value: &str) -> String {
let value = value.trim();
value
.get(..value.len().saturating_sub(4))
.filter(|_| {
value
.get(value.len().saturating_sub(4)..)
.is_some_and(|suffix| suffix.eq_ignore_ascii_case(".jxp"))
})
.unwrap_or(value)
.to_owned()
}
fn clean_vendor_text(value: &str) -> String {
value
.trim()
.trim_matches(|character| character == '<' || character == '>')
.trim()
.to_owned()
}
pub fn detect(path: impl AsRef<Path>) -> Result<Format, ReadError> {
ReadOptions::new().detect(path)
}
pub fn read(path: impl AsRef<Path>) -> Result<Dataset, ReadError> {
ReadOptions::new().read(path)
}
impl From<ProcessedValidationError> for ReadError {
fn from(error: ProcessedValidationError) -> Self {
ReadError::new(None, ReadErrorReason::ProcessedModel(error))
}
}
impl DatasetIdentity {
#[allow(clippy::type_complexity)]
pub(crate) fn model_parts(&self) -> (&Option<String>, &Option<String>, &Option<String>) {
(&self.subject, &self.acquisition, &self.source_label)
}
#[allow(clippy::type_complexity)]
pub(crate) fn from_model_parts(
parts: (Option<String>, Option<String>, Option<String>),
) -> Result<Self, crate::internal::ModelError> {
let (subject, acquisition, source_label) = parts;
let value = Self {
subject,
acquisition,
source_label,
};
Ok(value)
}
}
pub fn read_with_context(
path: impl AsRef<Path>,
control: &mut crate::ExecutionContext<'_>,
) -> Result<Dataset, ReadError> {
ReadOptions::new().read_with_context(path, control)
}