use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::path::{Path, PathBuf};
use openreadout_core::model::{ChannelInfo, InstrumentInfo, ObjectiveInfo};
use openreadout_core::{Error, PixelType, Result};
use serde_json::Value;
pub const MAX_PLATE_PLANES: u64 = 20_000_000;
pub const MAX_PLATE_ROWS: u32 = 256;
pub const MAX_PLATE_COLUMNS: u32 = 256;
#[derive(Debug, Clone, PartialEq)]
pub enum PlaneSlot {
NotAcquired,
NotRecorded,
Missing {
name: Option<String>,
},
File(PlaneFile),
}
impl PlaneSlot {
pub fn state(&self) -> &'static str {
match self {
PlaneSlot::NotAcquired => "not_acquired",
PlaneSlot::NotRecorded => "not_recorded",
PlaneSlot::Missing { .. } => "missing",
PlaneSlot::File(_) => "present",
}
}
pub fn file_name(&self) -> Option<&str> {
match self {
PlaneSlot::File(f) => Some(&f.name),
PlaneSlot::Missing { name } => name.as_deref(),
_ => None,
}
}
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct PlaneFile {
pub name: String,
pub page: u32,
pub acquired_at: Option<String>,
pub position_um: [Option<f64>; 3],
pub exposure_ms: Option<f64>,
}
#[derive(Debug, Clone)]
pub struct RawPlane {
pub row: u32,
pub column: u32,
pub field: u32,
pub channel: usize,
pub z: u32,
pub t: u32,
pub slot: PlaneSlot,
pub field_position_um: [Option<f64>; 2],
}
#[derive(Debug, Clone)]
pub struct HcsField {
pub row: u32,
pub column: u32,
pub field: u32,
pub field_index: u32,
pub position_um: [Option<f64>; 2],
pub acquired_at: Option<String>,
pub planes: Vec<PlaneSlot>,
}
#[derive(Debug, Clone, Default)]
pub struct HcsPlate {
pub format_id: &'static str,
pub source: PathBuf,
pub root: PathBuf,
pub sidecars: Vec<PathBuf>,
pub index_bytes: u64,
pub format_version: Option<String>,
pub id: Option<String>,
pub name: Option<String>,
pub plate_type: Option<String>,
pub rows: u32,
pub columns: u32,
pub declared_wells: Vec<(u32, u32)>,
pub channels: Vec<ChannelInfo>,
pub channel_extra: Vec<BTreeMap<String, Value>>,
pub size_z: u32,
pub size_t: u32,
pub size_x: u32,
pub size_y: u32,
pub pixel_type: Option<PixelType>,
pub pixel_source: Option<String>,
pub pixel_size_um: [Option<f64>; 2],
pub z_step_um: Option<f64>,
pub time_increment_s: Option<f64>,
pub objective: Option<ObjectiveInfo>,
pub instrument: InstrumentInfo,
pub operator: Option<String>,
pub started_at: Option<String>,
pub ended_at: Option<String>,
pub method: Option<String>,
pub description: Option<String>,
pub fields: Vec<HcsField>,
pub notes: Vec<String>,
pub plate_extra: BTreeMap<String, Value>,
pub vendor: Value,
pub unindexed_files: Vec<String>,
}
impl HcsPlate {
pub fn planes_per_field(&self) -> usize {
self.channels.len() * self.size_z as usize * self.size_t as usize
}
pub fn slot(&self, c: u32, z: u32, t: u32) -> usize {
let (sc, sz) = (self.channels.len(), self.size_z as usize);
c as usize + sc * (z as usize + sz * t as usize)
}
pub fn plane_of(&self, slot: usize) -> (u32, u32, u32) {
let sc = self.channels.len().max(1);
let sz = self.size_z.max(1) as usize;
(
(slot % sc) as u32,
((slot / sc) % sz) as u32,
(slot / (sc * sz)) as u32,
)
}
pub fn path_of(&self, name: &str) -> PathBuf {
let mut p = self.root.clone();
for part in name
.split(['/', '\\'])
.filter(|s| !s.is_empty() && *s != "..")
{
p.push(part);
}
p
}
}
#[derive(Debug)]
pub struct PlateBuilder {
format: &'static str,
planes: Vec<RawPlane>,
}
impl PlateBuilder {
pub fn new(format: &'static str) -> Self {
PlateBuilder {
format,
planes: Vec::new(),
}
}
pub fn push(&mut self, p: RawPlane) -> Result<()> {
if self.planes.len() as u64 >= MAX_PLATE_PLANES {
return Err(Error::unsupported(
self.format,
format!("an index with more than {MAX_PLATE_PLANES} planes"),
"Plates this large are not supported; split the index or report the file.",
));
}
self.planes.push(p);
Ok(())
}
pub fn len(&self) -> usize {
self.planes.len()
}
pub fn is_empty(&self) -> bool {
self.planes.is_empty()
}
pub fn finish(self, plate: &mut HcsPlate) -> Result<()> {
let nc = plate.channels.len();
if nc == 0 {
return Err(Error::corrupt(
self.format,
"the index lists no channels (no plane records)",
));
}
let zs: BTreeSet<u32> = self.planes.iter().map(|p| p.z).collect();
let ts: BTreeSet<u32> = self.planes.iter().map(|p| p.t).collect();
let z_of: HashMap<u32, u32> = zs.iter().enumerate().map(|(i, &z)| (z, i as u32)).collect();
let t_of: HashMap<u32, u32> = ts.iter().enumerate().map(|(i, &t)| (t, i as u32)).collect();
plate.size_z = (zs.len() as u32).max(1);
plate.size_t = (ts.len() as u32).max(1);
let per_field = nc
.checked_mul(plate.size_z as usize)
.and_then(|v| v.checked_mul(plate.size_t as usize))
.filter(|&v| v as u64 <= MAX_PLATE_PLANES)
.ok_or_else(|| Error::corrupt(self.format, "too many planes per field"))?;
let mut keys: BTreeMap<(u32, u32, u32), usize> = BTreeMap::new();
for p in &self.planes {
let n = keys.len();
keys.entry((p.row, p.column, p.field)).or_insert(n);
}
let total = (keys.len() as u64).saturating_mul(per_field as u64);
if total > MAX_PLATE_PLANES {
return Err(Error::unsupported(
self.format,
format!("{} fields x {per_field} planes", keys.len()),
"Plates this large are not supported; report the file.",
));
}
let mut fields: Vec<HcsField> = Vec::with_capacity(keys.len());
let mut order: HashMap<(u32, u32, u32), usize> = HashMap::with_capacity(keys.len());
for (i, &(row, column, field)) in keys.keys().enumerate() {
order.insert((row, column, field), i);
fields.push(HcsField {
row,
column,
field,
field_index: 0,
position_um: [None, None],
acquired_at: None,
planes: vec![PlaneSlot::NotAcquired; per_field],
});
}
let mut duplicates = 0u64;
for p in self.planes {
let Some(&fi) = order.get(&(p.row, p.column, p.field)) else {
continue;
};
if p.channel >= nc {
continue;
}
let (z, t) = (z_of[&p.z], t_of[&p.t]);
let slot = p.channel + nc * (z as usize + plate.size_z as usize * t as usize);
let f = &mut fields[fi];
if f.position_um[0].is_none() {
f.position_um = p.field_position_um;
}
if let PlaneSlot::File(pf) = &p.slot
&& let Some(ts) = &pf.acquired_at
&& f.acquired_at.as_ref().is_none_or(|cur| earlier(ts, cur))
{
f.acquired_at = Some(ts.clone());
}
let cur = &mut f.planes[slot];
match cur {
PlaneSlot::NotAcquired => *cur = p.slot,
PlaneSlot::File(_) => duplicates += 1,
_ => {
duplicates += 1;
if matches!(p.slot, PlaneSlot::File(_)) {
*cur = p.slot;
}
}
}
}
let mut last = (u32::MAX, u32::MAX);
let mut k = 0u32;
for f in &mut fields {
if (f.row, f.column) != last {
last = (f.row, f.column);
k = 0;
}
f.field_index = k;
k += 1;
}
if duplicates > 0 {
plate.notes.push(format!(
"{duplicates} plane records repeat a (well, field, channel, z, t) already listed; the first record with a file is used"
));
}
plate.fields = fields;
Ok(())
}
}
pub fn earlier(a: &str, b: &str) -> bool {
match (
openreadout_core::time::iso8601_to_unix(a),
openreadout_core::time::iso8601_to_unix(b),
) {
(Some(x), Some(y)) => x < y,
_ => a < b,
}
}
pub fn mark_missing<S: std::hash::BuildHasher>(
plate: &mut HcsPlate,
present: &HashSet<String, S>,
) -> u64 {
let mut n = 0;
for f in &mut plate.fields {
for s in &mut f.planes {
if let PlaneSlot::File(pf) = s
&& !present.contains(&pf.name)
{
*s = PlaneSlot::Missing {
name: Some(pf.name.clone()),
};
n += 1;
}
}
}
n
}
pub fn list_names(fs: &openreadout_core::Fs, dir: &Path) -> HashSet<String> {
let mut out = HashSet::new();
if let Ok(rd) = fs.read_dir(dir) {
for e in rd.flatten() {
if e.file_type().is_ok_and(|t| t.is_file()) {
out.insert(e.file_name().to_string_lossy().into_owned());
}
}
}
out
}
pub fn number(s: &str) -> Option<f64> {
let t = s.trim();
t.parse::<f64>()
.ok()
.or_else(|| t.replace(',', ".").parse::<f64>().ok())
.filter(|v| v.is_finite())
}
pub fn to_micrometres(v: f64, unit: &str) -> Option<f64> {
let f = match unit.trim() {
"m" => 1e6,
"mm" => 1e3,
"um" | "µm" | "μm" | "micron" | "microns" => 1.0,
"nm" => 1e-3,
_ => return None,
};
Some(v * f)
}
pub(crate) fn has_extension(name: &str, exts: &[&str]) -> bool {
Path::new(name)
.extension()
.and_then(|e| e.to_str())
.is_some_and(|e| exts.iter().any(|x| e.eq_ignore_ascii_case(x)))
}
#[cfg(test)]
mod tests {
use super::*;
fn ch(name: &str) -> ChannelInfo {
ChannelInfo {
name: Some(name.into()),
..ChannelInfo::default()
}
}
fn file(name: &str, t: &str) -> PlaneSlot {
PlaneSlot::File(PlaneFile {
name: name.into(),
acquired_at: Some(t.into()),
..PlaneFile::default()
})
}
#[test]
fn builder_sorts_wells_fields_and_planes() {
let mut plate = HcsPlate {
channels: vec![ch("a"), ch("b")],
..HcsPlate::default()
};
let mut b = PlateBuilder::new("test");
for (row, col, field, c, z, name) in [
(1, 0, 2, 0, 5, "b1"),
(0, 1, 1, 1, 6, "a2c1z6"),
(0, 1, 1, 0, 5, "a2c0z5"),
(0, 1, 1, 0, 6, "a2c0z6"),
] {
b.push(RawPlane {
row,
column: col,
field,
channel: c,
z,
t: 0,
slot: file(name, "2020-01-01T00:00:00Z"),
field_position_um: [Some(1.0), None],
})
.unwrap();
}
b.finish(&mut plate).unwrap();
assert_eq!((plate.size_z, plate.size_t), (2, 1));
assert_eq!(plate.fields.len(), 2);
let f0 = &plate.fields[0];
assert_eq!((f0.row, f0.column, f0.field, f0.field_index), (0, 1, 1, 0));
assert_eq!(f0.planes[plate.slot(0, 0, 0)].file_name(), Some("a2c0z5"));
assert_eq!(f0.planes[plate.slot(1, 1, 0)].file_name(), Some("a2c1z6"));
assert_eq!(f0.planes[plate.slot(1, 0, 0)], PlaneSlot::NotAcquired);
assert_eq!(plate.plane_of(plate.slot(1, 1, 0)), (1, 1, 0));
let present: HashSet<String> = ["a2c0z5".to_string()].into();
assert_eq!(mark_missing(&mut plate, &present), 3);
}
#[test]
fn builder_rejects_empty_channel_list() {
let mut plate = HcsPlate::default();
assert!(PlateBuilder::new("test").finish(&mut plate).is_err());
}
#[test]
fn units_and_numbers() {
assert_eq!(to_micrometres(1.5e-6, "m"), Some(1.5));
assert_eq!(to_micrometres(2.0, "furlong"), None);
assert_eq!(number(" 0,5 "), Some(0.5));
assert_eq!(number("NaN"), None);
assert!(earlier("2020-01-01T00:00:00+01:00", "2020-01-01T00:30:00Z"));
}
}