const MODES: &[(&str, &str)] = &[
("WideField", "Widefield"),
("LaserScanningConfocalMicroscopy", "Laser Scanning Confocal"),
("SpinningDiskConfocal", "Spinning Disk Confocal"),
("SlitScanConfocal", "Slit Scan Confocal"),
("SweptFieldConfocal", "Swept Field Confocal"),
("MultiPhotonMicroscopy", "Multiphoton"),
("StructuredIllumination", "Structured Illumination"),
("SingleMoleculeImaging", "Single Molecule Imaging"),
("TIRF", "TIRF"),
("TotalInternalReflection", "TIRF"),
("FluorescenceLifetime", "Fluorescence Lifetime"),
("SpectralImaging", "Spectral Imaging"),
(
"FluorescenceCorrelationSpectroscopy",
"Fluorescence Correlation Spectroscopy",
),
(
"NearFieldScanningOpticalMicroscopy",
"Near-Field Scanning Optical Microscopy",
),
(
"SecondHarmonicGenerationImaging",
"Second Harmonic Generation",
),
("PALM", "PALM"),
("STORM", "STORM"),
("STED", "STED"),
("FSM", "FSM"),
("LCM", "LCM"),
("SPIM", "SPIM"),
("BrightField", "Brightfield"),
("Other", "Other"),
];
const CONTRASTS: &[(&str, &str)] = &[
("Fluorescence", "Fluorescence"),
("Brightfield", "Brightfield"),
("Phase", "Phase Contrast"),
("DIC", "DIC"),
("HoffmanModulation", "Hoffman Modulation Contrast"),
("ObliqueIllumination", "Oblique Illumination"),
("PolarizedLight", "Polarized Light"),
("Darkfield", "Darkfield"),
("Other", "Other"),
];
fn lookup(table: &'static [(&'static str, &'static str)], token: &str) -> Option<&'static str> {
table
.iter()
.find(|(t, _)| t.eq_ignore_ascii_case(token))
.map(|(_, l)| *l)
}
pub fn label(raw: &str) -> Option<String> {
let t = raw.trim();
if t.is_empty() {
return None;
}
Some(
lookup(MODES, t)
.or_else(|| lookup(CONTRASTS, t))
.map_or_else(|| t.to_string(), str::to_string),
)
}
pub fn from_ome(mode: Option<&str>, contrast: Option<&str>) -> Option<String> {
let m = mode.and_then(label);
let k = contrast.and_then(label).filter(|k| k != "Other");
match (m, k) {
(m, None) => m,
(None, k) => k,
(Some(m), Some(k)) => Some(
if m == k || (m.to_ascii_lowercase().contains("fluorescence") && k == "Fluorescence") {
m
} else if k == "Fluorescence" {
format!("{m} {k}")
} else if m == "Widefield" || m == "Brightfield" || m == "Other" {
k
} else {
format!("{m} {k}")
},
),
}
}
fn has_word(l: &str, w: &str) -> bool {
l.split(|c: char| !c.is_ascii_alphanumeric())
.any(|x| x == w)
}
pub fn to_ome(label_text: &str) -> (Option<&'static str>, Option<&'static str>) {
let Some(l) = label(label_text) else {
return (None, None);
};
let l = l.to_ascii_lowercase();
let has = |s: &str| l.contains(s);
let mode = if has("spinning") {
Some("SpinningDiskConfocal")
} else if has("swept field") || has("sweptfield") {
Some("SweptFieldConfocal")
} else if has("slit scan") {
Some("SlitScanConfocal")
} else if has("confocal") {
Some("LaserScanningConfocalMicroscopy")
} else if has("multiphoton") || has("multi-photon") || has("two-photon") || has("2-photon") {
Some("MultiPhotonMicroscopy")
} else if has_word(&l, "tirf") {
Some("TIRF")
} else if has("total internal reflection") {
Some("TotalInternalReflection")
} else if has("fluorescence lifetime") || has_word(&l, "flim") {
Some("FluorescenceLifetime")
} else if has("fluorescence correlation") {
Some("FluorescenceCorrelationSpectroscopy")
} else if has("spectral imaging") {
Some("SpectralImaging")
} else if has("structured illumination") {
Some("StructuredIllumination")
} else if has("single molecule") {
Some("SingleMoleculeImaging")
} else if has("near-field") || has("near field") {
Some("NearFieldScanningOpticalMicroscopy")
} else if has("second harmonic") {
Some("SecondHarmonicGenerationImaging")
} else if has("light sheet") || has("lightsheet") || has_word(&l, "spim") {
Some("SPIM")
} else if let Some(t) = ["palm", "storm", "sted", "fsm", "lcm"]
.iter()
.find(|w| has_word(&l, w))
{
MODES
.iter()
.find(|(tok, _)| tok.eq_ignore_ascii_case(t))
.map(|(tok, _)| *tok)
} else if has("widefield") || has("wide field") || has("wide-field") {
Some("WideField")
} else if has("brightfield") || has("bright field") {
Some("BrightField")
} else if l == "other" {
Some("Other")
} else {
None
};
let contrast = if l.ends_with("fluorescence") {
Some("Fluorescence")
} else if has("phase") {
Some("Phase")
} else if has_word(&l, "dic") || has("differential interference") {
Some("DIC")
} else if has("hoffman") {
Some("HoffmanModulation")
} else if has("oblique") {
Some("ObliqueIllumination")
} else if has("polariz") || has("polaris") {
Some("PolarizedLight")
} else if has("darkfield") || has("dark field") {
Some("Darkfield")
} else if has("brightfield") || has("bright field") {
Some("Brightfield")
} else {
None
};
(mode, contrast)
}
pub fn round_trips(label_text: &str) -> bool {
let (m, k) = to_ome(label_text);
from_ome(m, k).as_deref() == Some(label_text.trim())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn tokens_become_labels() {
assert_eq!(label("WideField").as_deref(), Some("Widefield"));
assert_eq!(
label("LaserScanningConfocalMicroscopy").as_deref(),
Some("Laser Scanning Confocal")
);
assert_eq!(label(" Phase ").as_deref(), Some("Phase Contrast"));
assert_eq!(
label("Brightfield (RGB)").as_deref(),
Some("Brightfield (RGB)")
);
assert_eq!(label(" "), None);
}
#[test]
fn composition() {
assert_eq!(
from_ome(
Some("LaserScanningConfocalMicroscopy"),
Some("Fluorescence")
)
.as_deref(),
Some("Laser Scanning Confocal Fluorescence")
);
assert_eq!(
from_ome(Some("WideField"), Some("Phase")).as_deref(),
Some("Phase Contrast")
);
assert_eq!(
from_ome(Some("BrightField"), Some("Brightfield")).as_deref(),
Some("Brightfield")
);
assert_eq!(from_ome(None, Some("Other")), None);
assert_eq!(
from_ome(Some("FluorescenceLifetime"), Some("Fluorescence")).as_deref(),
Some("Fluorescence Lifetime")
);
}
#[test]
fn every_reader_label_round_trips() {
let mut labels: Vec<&str> = vec![
"Laser Scanning Confocal",
"Widefield",
"Brightfield",
"Phase Contrast",
"DIC",
"Fluorescence",
"Widefield Fluorescence",
"Spinning Disk Confocal Fluorescence",
"Multiphoton Fluorescence",
"Swept Field Confocal Fluorescence",
"Laser Scanning Confocal Fluorescence",
"TIRF Fluorescence",
];
labels.extend(MODES.iter().map(|(_, l)| *l));
labels.extend(CONTRASTS.iter().map(|(_, l)| *l).filter(|l| *l != "Other"));
for l in labels {
assert!(round_trips(l), "{l}: {:?} → {:?}", to_ome(l), {
let (m, k) = to_ome(l);
from_ome(m, k)
});
}
assert!(!round_trips("Brightfield (RGB)"));
assert_eq!(
to_ome("Brightfield (RGB)"),
(Some("BrightField"), Some("Brightfield"))
);
}
}