1#![forbid(unsafe_code)]
58#![warn(missing_docs)]
59
60mod assurance;
61mod biacore;
62mod biacore_eval;
63mod gpr;
64mod itc;
65mod octet;
66mod seahorse;
67mod seahorse_rates;
68mod zetasizer;
69
70use std::path::Path;
71
72use openreadout_core::bytes::latin1;
73use openreadout_core::model::{DetectConfidence, FormatDescriptor};
74use openreadout_core::reader::{Dataset, Detection, FormatReader, has_extension};
75use openreadout_core::source::{ByteSource, Input};
76use openreadout_core::{Error, Result};
77
78pub use biacore::BiacoreDataset;
79pub use gpr::GprDataset;
80pub use itc::ItcDataset;
81pub use seahorse::SeahorseDataset;
82#[doc(hidden)]
83pub use seahorse_rates::{
84 OxygenModel, acidification, oxygen_consumption, oxygen_levels, ph_level, slope,
85};
86
87pub const ITC_FORMAT_ID: &str = itc::FORMAT_ID;
89
90pub const GPR_FORMAT_ID: &str = gpr::FORMAT_ID;
92
93#[derive(Debug, Default, Clone, Copy)]
95pub struct GprReader;
96
97impl FormatReader for GprReader {
98 fn assurance(&self) -> Option<&'static openreadout_core::assurance::AssuranceProfile> {
99 Some(&assurance::GENEPIX_GPR)
100 }
101
102 fn descriptor(&self) -> FormatDescriptor {
103 FormatDescriptor {
104 id: GPR_FORMAT_ID.into(),
105 name: "GenePix Results (.gpr)".into(),
106 vendor: "Molecular Devices (Axon GenePix)".into(),
107 extensions: vec!["gpr".into()],
108 family: "microarray".into(),
109 can_read: true,
110 can_write: false,
111 confidence: assurance::GENEPIX_GPR.confidence,
112 known_gaps: vec![
113 "GenePix array lists (.gal), settings (.gps) and the scan TIFF images are not read by this reader (the TIFF reader opens the images)".into(),
114 "Values are the text GenePix wrote; no normalization, background correction or flag filtering is applied".into(),
115 ],
116 }
117 }
118
119 fn sniff(&self, head: &[u8], path: &Path) -> Option<Detection> {
120 if gpr::atf_type(head).is_some_and(|t| t.starts_with("GenePix Results")) {
121 return Some(Detection {
122 format_id: GPR_FORMAT_ID,
123 confidence: DetectConfidence::Definite,
124 note: None,
125 });
126 }
127 None.or_else(|| {
128 (has_extension(path, &["gpr"]) && head.starts_with(b"ATF")).then_some(Detection {
129 format_id: GPR_FORMAT_ID,
130 confidence: DetectConfidence::Likely,
131 note: None,
132 })
133 })
134 }
135
136 fn open(&self, path: &Path) -> Result<Box<dyn Dataset>> {
137 self.open_input(&Input::local(path))
138 }
139
140 fn open_input(&self, input: &Input) -> Result<Box<dyn Dataset>> {
141 let (bytes, len) = read_all(input, GPR_FORMAT_ID, gpr::MAX_BYTES)?;
142 let text = match String::from_utf8(bytes) {
143 Ok(s) => s,
144 Err(e) => latin1(&e.into_bytes()),
145 };
146 let g = gpr::parse(&text)?;
147 Ok(Box::new(GprDataset::new(
148 self.descriptor(),
149 input.path().to_path_buf(),
150 len,
151 g,
152 )))
153 }
154
155 fn reads_any_source(&self) -> bool {
156 true
157 }
158}
159
160pub const OCTET_FORMAT_ID: &str = octet::FORMAT_ID;
162
163#[derive(Debug, Default, Clone, Copy)]
165pub struct OctetReader;
166
167impl FormatReader for OctetReader {
168 fn assurance(&self) -> Option<&'static openreadout_core::assurance::AssuranceProfile> {
169 Some(&assurance::SARTORIUS_OCTET_FRD)
170 }
171
172 fn descriptor(&self) -> FormatDescriptor {
173 FormatDescriptor {
174 id: OCTET_FORMAT_ID.into(),
175 name: "Sartorius Octet BLI result (.frd)".into(),
176 vendor: "Sartorius (ForteBio)".into(),
177 extensions: vec!["frd".into()],
178 family: "binding-kinetics".into(),
179 can_read: true,
180 can_write: false,
181 confidence: assurance::SARTORIUS_OCTET_FRD.confidence,
182 known_gaps: vec![
183 "One file is one biosensor; the experiment's other sensors are separate .frd files (read them together with batch)".into(),
184 "Kinetic fits (kon, koff, KD) are made by the Octet analysis software and are not in .frd files".into(),
185 "Only kinetics experiments (KineticsData) are read".into(),
186 ],
187 }
188 }
189
190 fn sniff(&self, head: &[u8], path: &Path) -> Option<Detection> {
191 octet::looks_like(head).then_some(Detection {
192 format_id: OCTET_FORMAT_ID,
193 confidence: if has_extension(path, &["frd"]) {
194 DetectConfidence::Definite
195 } else {
196 DetectConfidence::Likely
197 },
198 note: None,
199 })
200 }
201
202 fn open(&self, path: &Path) -> Result<Box<dyn Dataset>> {
203 self.open_input(&Input::local(path))
204 }
205
206 fn open_input(&self, input: &Input) -> Result<Box<dyn Dataset>> {
207 let bytes = openreadout_core::series::read_all(input, OCTET_FORMAT_ID, octet::MAX_BYTES)?;
208 let xml = String::from_utf8(bytes).map_err(|_| {
209 Error::corrupt(OCTET_FORMAT_ID, "the result document is not UTF-8 text")
210 })?;
211 let file = octet::parse(&xml)?;
212 Ok(Box::new(openreadout_core::series::SeriesDataset::new(
213 self.descriptor(),
214 input.path().to_path_buf(),
215 xml.len() as u64,
216 file,
217 )))
218 }
219
220 fn reads_any_source(&self) -> bool {
221 true
222 }
223}
224
225pub const SEAHORSE_FORMAT_ID: &str = seahorse::FORMAT_ID;
227
228#[derive(Debug, Default, Clone, Copy)]
230pub struct SeahorseReader;
231
232impl FormatReader for SeahorseReader {
233 fn assurance(&self) -> Option<&'static openreadout_core::assurance::AssuranceProfile> {
234 Some(&assurance::AGILENT_SEAHORSE_ASYR)
235 }
236
237 fn descriptor(&self) -> FormatDescriptor {
238 FormatDescriptor {
239 id: SEAHORSE_FORMAT_ID.into(),
240 name: "Agilent Seahorse XF assay result (.asyr)".into(),
241 vendor: "Agilent (Seahorse)".into(),
242 extensions: vec!["asyr".into()],
243 family: "cell-metabolism".into(),
244 can_read: true,
245 can_write: false,
246 confidence: assurance::AGILENT_SEAHORSE_ASYR.confidence,
247 known_gaps: vec![
248 "OCR, ECAR and PER are not stored; they are computed (published compartment model, the file's constants) only for standard 96-well plates with Wave's AKOS settings, where they agree with Wave's own rates (OCR within 0.2 pmol/min, ECAR within 0.001 mpH/min); other plates (24-well, spheroid) return the emissions and a note saying why".into(),
249 "PER uses kVol 1.6 (not stored; what Wave prints for the same plate) and is withheld by --strict".into(),
250 "Raw LED on/off emission and reference arrays and the calibration arrays are not exposed; templates (.asyt) are not read".into(),
251 ],
252 }
253 }
254
255 fn sniff(&self, head: &[u8], path: &Path) -> Option<Detection> {
256 if !openreadout_core::gzip::is_gzip(head) {
257 return None;
258 }
259 if openreadout_core::gzip::peek(head, 4096).is_some_and(|x| seahorse::looks_like(&x)) {
260 return Some(Detection {
261 format_id: SEAHORSE_FORMAT_ID,
262 confidence: DetectConfidence::Definite,
263 note: None,
264 });
265 }
266 has_extension(path, &["asyr"]).then_some(Detection {
267 format_id: SEAHORSE_FORMAT_ID,
268 confidence: DetectConfidence::Likely,
269 note: None,
270 })
271 }
272
273 fn open(&self, path: &Path) -> Result<Box<dyn Dataset>> {
274 self.open_input(&Input::local(path))
275 }
276
277 fn open_input(&self, input: &Input) -> Result<Box<dyn Dataset>> {
278 let src = input
279 .fs()
280 .source(input.path())
281 .map_err(|e| Error::io(input.path(), e))?;
282 let size = src.size().map_err(|e| Error::io(input.path(), e))?;
283 let gz = openreadout_core::gzip::GzipSource::open(
284 src,
285 input.path().display().to_string(),
286 SEAHORSE_FORMAT_ID,
287 )?;
288 let len = gz.size().map_err(|e| Error::io(input.path(), e))?;
289 if len > seahorse::MAX_XML {
290 return Err(Error::unsupported(
291 SEAHORSE_FORMAT_ID,
292 format!("a {} MiB assay document", len >> 20),
293 "Assay documents this large are not read; the largest seen is 17 MB.",
294 ));
295 }
296 let n =
297 usize::try_from(len).map_err(|_| Error::Other("document larger than memory".into()))?;
298 let mut buf = vec![0u8; n];
299 gz.read_exact_at(0, &mut buf)
300 .map_err(|e| Error::io(input.path(), e))?;
301 if let Some(p) = gz.problem() {
302 return Err(Error::corrupt(
303 SEAHORSE_FORMAT_ID,
304 format!("the gzip stream is damaged ({p}); the assay XML is incomplete"),
305 ));
306 }
307 let xml = String::from_utf8(buf).map_err(|_| {
308 Error::corrupt(SEAHORSE_FORMAT_ID, "the assay document is not UTF-8 text")
309 })?;
310 let assay = seahorse::parse(&xml)?;
311 Ok(Box::new(SeahorseDataset::new(
312 self.descriptor(),
313 input.path().to_path_buf(),
314 size,
315 assay,
316 )))
317 }
318
319 fn reads_any_source(&self) -> bool {
320 true
321 }
322}
323
324pub const BIACORE_FORMAT_ID: &str = biacore::FORMAT_ID;
326
327#[derive(Debug, Default, Clone, Copy)]
329pub struct BiacoreReader;
330
331impl FormatReader for BiacoreReader {
332 fn assurance(&self) -> Option<&'static openreadout_core::assurance::AssuranceProfile> {
333 Some(&assurance::CYTIVA_BIACORE_BLR)
334 }
335
336 fn descriptor(&self) -> FormatDescriptor {
337 FormatDescriptor {
338 id: BIACORE_FORMAT_ID.into(),
339 name: "Cytiva Biacore result file (.blr)".into(),
340 vendor: "Cytiva (Biacore)".into(),
341 extensions: vec!["blr".into()],
342 family: "spr".into(),
343 can_read: true,
344 can_write: false,
345 confidence: assurance::CYTIVA_BIACORE_BLR.confidence,
346 known_gaps: vec![
347 "Validated on Biacore T200 Control Software 2.0.1-2.0.2 result files (kinetics/affinity and immobilization wizards, manual runs); other Biacore systems (3000, X100, S200, 8K, Insight) and evaluation files (`.bme`, `.bie`) are not read".into(),
348 "Sensorgrams and the reference-subtracted curves are returned as stored; kinetic or affinity fits (ka, kd, KD) are the evaluation software's and are not computed".into(),
349 "Report points are the control software's; at 1 Hz storage they average data the file does not hold".into(),
350 "Event-log codes, the quality and time-correction streams and the wizard template are kept or listed, not interpreted".into(),
351 ],
352 }
353 }
354
355 fn sniff(&self, head: &[u8], path: &Path) -> Option<Detection> {
356 (has_extension(path, &["blr"]) && head.starts_with(&openreadout_core::cfb::CFB_MAGIC))
357 .then_some(Detection {
358 format_id: BIACORE_FORMAT_ID,
359 confidence: DetectConfidence::Likely,
360 note: None,
361 })
362 }
363
364 fn sniff_input(&self, head: &[u8], input: &Input) -> Option<Detection> {
365 match biacore::sniff_input(head, input) {
366 Some(biacore::Kind::Result) => Some(Detection {
367 format_id: BIACORE_FORMAT_ID,
368 confidence: DetectConfidence::Definite,
369 note: None,
370 }),
371 Some(biacore::Kind::Evaluation) => None,
373 None => self.sniff(head, input.path()),
374 }
375 }
376
377 fn open(&self, path: &Path) -> Result<Box<dyn Dataset>> {
378 self.open_input(&Input::local(path))
379 }
380
381 fn open_input(&self, input: &Input) -> Result<Box<dyn Dataset>> {
382 Ok(Box::new(BiacoreDataset::open_input(
383 self.descriptor(),
384 input,
385 biacore::Kind::Result,
386 )?))
387 }
388
389 fn reads_any_source(&self) -> bool {
390 true
391 }
392}
393
394#[derive(Debug, Default, Clone, Copy)]
396pub struct ItcReader;
397
398fn read_all(input: &Input, format: &'static str, cap: u64) -> Result<(Vec<u8>, u64)> {
400 let f = input.open()?;
401 let len = f.size().map_err(|e| Error::io(input.path(), e))?;
402 if len > cap {
403 return Err(Error::unsupported(
404 format,
405 format!("a {} MiB file", len >> 20),
406 "Files this large are not read; no instrument writes them.",
407 ));
408 }
409 let n = usize::try_from(len).map_err(|_| Error::Other("file larger than memory".into()))?;
410 let mut buf = vec![0u8; n];
411 f.read_exact_at(0, &mut buf)
412 .map_err(|e| Error::io(input.path(), e))?;
413 Ok((buf, len))
414}
415
416impl FormatReader for ItcReader {
417 fn assurance(&self) -> Option<&'static openreadout_core::assurance::AssuranceProfile> {
418 Some(&assurance::MICROCAL_ITC)
419 }
420
421 fn descriptor(&self) -> FormatDescriptor {
422 FormatDescriptor {
423 id: ITC_FORMAT_ID.into(),
424 name: "MicroCal ITC (.itc)".into(),
425 vendor: "Malvern Panalytical (MicroCal)".into(),
426 extensions: vec!["itc".into()],
427 family: "calorimetry".into(),
428 can_read: true,
429 can_write: false,
430 confidence: assurance::MICROCAL_ITC.confidence,
431 known_gaps: vec![
432 "Validated on VP-ITC (VPViewer2000 1.4.8-1.30), iTC200 (1.25-1.26) and MicroCal ITC software 1.29 files; PEAQ-ITC `.apitc` and analysis projects (`.apj`) are not read".into(),
433 "Time and differential power are validated against Origin's raw exports; the cell temperature column is inferred; further data columns (7- and 9-column files) are exposed unnamed".into(),
434 "Integrated injection heats are the analysis software's (baseline and integration) and are not stored in the file; they are not computed".into(),
435 "Calibration lines (`%`) are kept verbatim in the vendor tree, not interpreted".into(),
436 ],
437 }
438 }
439
440 fn sniff(&self, head: &[u8], path: &Path) -> Option<Detection> {
441 if itc::looks_like(head) {
442 return Some(Detection {
443 format_id: ITC_FORMAT_ID,
444 confidence: DetectConfidence::Definite,
445 note: None,
446 });
447 }
448 (has_extension(path, &["itc"]) && head.is_empty()).then_some(Detection {
449 format_id: ITC_FORMAT_ID,
450 confidence: DetectConfidence::ExtensionOnly,
451 note: None,
452 })
453 }
454
455 fn open(&self, path: &Path) -> Result<Box<dyn Dataset>> {
456 self.open_input(&Input::local(path))
457 }
458
459 fn open_input(&self, input: &Input) -> Result<Box<dyn Dataset>> {
460 let (bytes, len) = read_all(input, ITC_FORMAT_ID, itc::MAX_BYTES)?;
461 let text = match String::from_utf8(bytes) {
463 Ok(s) => s,
464 Err(e) => latin1(&e.into_bytes()),
465 };
466 let parsed = itc::parse(&text)?;
467 Ok(Box::new(ItcDataset::new(
468 self.descriptor(),
469 input.path().to_path_buf(),
470 len,
471 parsed,
472 )))
473 }
474
475 fn reads_any_source(&self) -> bool {
476 true
477 }
478}
479
480pub(crate) fn complete_provenance(e: &mut openreadout_core::experiment::Experiment) {
484 use openreadout_core::experiment::Origin;
485 use openreadout_core::provenance::Source;
486 let o = |from: &str| Origin {
487 source: Source::Inferred,
488 from: from.into(),
489 };
490 for k in 0..e.measurements.len() {
491 e.provenance
492 .entry(format!("measurements[{k}]"))
493 .or_insert_with(|| o("the reader's data layout (traces, tables, images)"));
494 }
495 if let Some(i) = &e.instrument {
496 if i.vendor.is_some() {
497 e.provenance
498 .entry("instrument.vendor".into())
499 .or_insert_with(|| o("the file format"));
500 }
501 if i.kind.is_some() {
502 e.provenance
503 .entry("instrument.kind".into())
504 .or_insert_with(|| o("the file format"));
505 }
506 }
507 if e.method.as_ref().is_some_and(|m| m.technique.is_some()) {
508 e.provenance
509 .entry("method.technique".into())
510 .or_insert_with(|| o("the file format and the recorded mode"));
511 }
512}
513
514pub const ZETASIZER_FORMAT_ID: &str = zetasizer::FORMAT_ID;
516
517#[derive(Debug, Default, Clone, Copy)]
519pub struct ZetasizerReader;
520
521impl FormatReader for ZetasizerReader {
522 fn assurance(&self) -> Option<&'static openreadout_core::assurance::AssuranceProfile> {
523 Some(&assurance::MALVERN_ZETASIZER_DTS)
524 }
525
526 fn descriptor(&self) -> FormatDescriptor {
527 FormatDescriptor {
528 id: ZETASIZER_FORMAT_ID.into(),
529 name: "Malvern Zetasizer measurement file (.dts)".into(),
530 vendor: "Malvern Panalytical".into(),
531 extensions: vec!["dts".into()],
532 family: "particle-sizing".into(),
533 can_read: true,
534 can_write: false,
535 confidence: assurance::MALVERN_ZETASIZER_DTS.confidence,
536 known_gaps: vec![
537 "Size and zeta results are returned as stored; the size distributions, correlation functions and phase plots are not decoded".into(),
538 "Size peak widths and the number and volume peaks are stored but withheld: no Zetasizer export in the corpus holds them to check against".into(),
539 "Size results are confirmed on Zetasizer software 7.10 and 7.12 only".into(),
540 "Number and volume means and the diffusion coefficient the software exports are computed at export and are not returned".into(),
541 "Molecular-weight, protein-mobility and other record kinds are listed, not decoded".into(),
542 "ZS Xplorer .zmes files are not read".into(),
543 ],
544 }
545 }
546
547 fn sniff(&self, head: &[u8], path: &Path) -> Option<Detection> {
548 (zetasizer::looks_like(head) && has_extension(path, &["dts"])).then_some(Detection {
549 format_id: ZETASIZER_FORMAT_ID,
550 confidence: DetectConfidence::Likely,
551 note: Some("a compound file named .dts".into()),
552 })
553 }
554
555 fn open(&self, path: &Path) -> Result<Box<dyn Dataset>> {
556 self.open_input(&Input::local(path))
557 }
558
559 fn open_input(&self, input: &Input) -> Result<Box<dyn Dataset>> {
560 let bytes =
561 openreadout_core::series::read_all(input, ZETASIZER_FORMAT_ID, zetasizer::MAX_BYTES)?;
562 let file = zetasizer::parse(&bytes, input.path())?;
563 Ok(Box::new(openreadout_core::series::SeriesDataset::new(
564 self.descriptor(),
565 input.path().to_path_buf(),
566 bytes.len() as u64,
567 file,
568 )))
569 }
570
571 fn reads_any_source(&self) -> bool {
572 true
573 }
574}
575
576pub const BIACORE_BME_FORMAT_ID: &str = biacore::BME_FORMAT_ID;
578
579#[derive(Debug, Default, Clone, Copy)]
582pub struct BiacoreEvaluationReader;
583
584impl FormatReader for BiacoreEvaluationReader {
585 fn assurance(&self) -> Option<&'static openreadout_core::assurance::AssuranceProfile> {
586 Some(&assurance::CYTIVA_BIACORE_BME)
587 }
588
589 fn descriptor(&self) -> FormatDescriptor {
590 FormatDescriptor {
591 id: BIACORE_BME_FORMAT_ID.into(),
592 name: "Cytiva Biacore T200 evaluation file (.bme)".into(),
593 vendor: "Cytiva (Biacore)".into(),
594 extensions: vec!["bme".into()],
595 family: "spr".into(),
596 can_read: true,
597 can_write: false,
598 confidence: assurance::CYTIVA_BIACORE_BME.confidence,
599 known_gaps: vec![
600 "Fits (kinetics, affinity) are the evaluation software's, returned as stored with their parameters, standard errors and Chi²; they are not recomputed".into(),
601 "The evaluation's own processed curves (aligned, blank-subtracted, in `EvaluationItemNBinary`), plots and report-point tables are not decoded; the sensorgrams are the result files' as the evaluation copied them".into(),
602 "Concentration-analysis items (calibration curves) are listed without their results".into(),
603 "Biacore 8K, S200 and Insight evaluation files are not read".into(),
604 ],
605 }
606 }
607
608 fn sniff(&self, head: &[u8], path: &Path) -> Option<Detection> {
609 (has_extension(path, &["bme"]) && head.starts_with(&openreadout_core::cfb::CFB_MAGIC))
610 .then_some(Detection {
611 format_id: BIACORE_BME_FORMAT_ID,
612 confidence: DetectConfidence::Likely,
613 note: None,
614 })
615 }
616
617 fn sniff_input(&self, head: &[u8], input: &Input) -> Option<Detection> {
618 match biacore::sniff_input(head, input) {
619 Some(biacore::Kind::Evaluation) => Some(Detection {
620 format_id: BIACORE_BME_FORMAT_ID,
621 confidence: DetectConfidence::Definite,
622 note: None,
623 }),
624 Some(biacore::Kind::Result) => None,
625 None => self.sniff(head, input.path()),
626 }
627 }
628
629 fn open(&self, path: &Path) -> Result<Box<dyn Dataset>> {
630 self.open_input(&Input::local(path))
631 }
632
633 fn open_input(&self, input: &Input) -> Result<Box<dyn Dataset>> {
634 Ok(Box::new(BiacoreDataset::open_input(
635 self.descriptor(),
636 input,
637 biacore::Kind::Evaluation,
638 )?))
639 }
640
641 fn reads_any_source(&self) -> bool {
642 true
643 }
644}