nmr 0.1.3

NMR data tools for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
use super::*;

/// Supported raw acquisition formats.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum RawFormat {
    /// Bruker TopSpin raw `fid`/`ser` plus acquisition status parameters.
    BrukerRaw,
    /// JEOL Delta JDF raw acquisition.
    JeolDelta,
    /// Varian raw `fid` plus `procpar`.
    VarianRaw,
}
/// Typed diffusion acquisition facts projected from vendor parameters.
///
/// This value does not contain a fitted diffusion coefficient, an inverse
/// Laplace transform, a derived b-value, or an assumed gradient-shape factor.
#[derive(Clone, Debug, PartialEq)]
pub struct DiffusionAcquisition {
    gradient_axis: usize,
    gradient_parameter: String,
    gradient_pulse_duration_seconds: f64,
    gradient_pulse_duration_parameter: String,
    diffusion_time_seconds: f64,
    diffusion_time_parameter: String,
    recovery_delay_seconds: Option<f64>,
    recovery_delay_parameter: Option<String>,
    gradient_shape: Option<String>,
    gradient_shape_parameter: Option<String>,
}

impl DiffusionAcquisition {
    /// Creates checked diffusion acquisition facts with exact source parameter names.
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        gradient_axis: usize,
        gradient_parameter: String,
        gradient_pulse_duration_seconds: f64,
        gradient_pulse_duration_parameter: String,
        diffusion_time_seconds: f64,
        diffusion_time_parameter: String,
        recovery_delay: Option<(f64, String)>,
        gradient_shape: Option<(String, String)>,
    ) -> Result<Self, ValidationError> {
        let (recovery_delay_seconds, recovery_delay_parameter) = recovery_delay.unzip();
        let (gradient_shape, gradient_shape_parameter) = gradient_shape.unzip();
        let value = Self {
            gradient_axis,
            gradient_parameter,
            gradient_pulse_duration_seconds,
            gradient_pulse_duration_parameter,
            diffusion_time_seconds,
            diffusion_time_parameter,
            recovery_delay_seconds,
            recovery_delay_parameter,
            gradient_shape,
            gradient_shape_parameter,
        };
        value.validate()?;
        Ok(value)
    }
    /// Returns the zero-based gradient parameter axis.
    pub fn gradient_axis(&self) -> usize {
        self.gradient_axis
    }
    /// Returns the vendor parameter supplying gradient coordinates.
    pub fn gradient_parameter(&self) -> &str {
        &self.gradient_parameter
    }
    /// Returns the gradient pulse duration in seconds.
    pub fn gradient_pulse_duration_seconds(&self) -> f64 {
        self.gradient_pulse_duration_seconds
    }
    /// Returns the vendor parameter supplying the gradient pulse duration.
    pub fn gradient_pulse_duration_parameter(&self) -> &str {
        &self.gradient_pulse_duration_parameter
    }
    /// Returns the diffusion interval in seconds.
    pub fn diffusion_time_seconds(&self) -> f64 {
        self.diffusion_time_seconds
    }
    /// Returns the vendor parameter supplying the diffusion interval.
    pub fn diffusion_time_parameter(&self) -> &str {
        &self.diffusion_time_parameter
    }
    /// Returns an additional recovery delay in seconds when typed evidence exists.
    pub fn recovery_delay_seconds(&self) -> Option<f64> {
        self.recovery_delay_seconds
    }
    /// Returns the vendor parameter supplying the recovery delay.
    pub fn recovery_delay_parameter(&self) -> Option<&str> {
        self.recovery_delay_parameter.as_deref()
    }
    /// Returns the source gradient-shape label without assigning a coefficient.
    pub fn gradient_shape(&self) -> Option<&str> {
        self.gradient_shape.as_deref()
    }
    /// Returns the vendor parameter supplying the gradient-shape label.
    pub fn gradient_shape_parameter(&self) -> Option<&str> {
        self.gradient_shape_parameter.as_deref()
    }

    fn validate(&self) -> Result<(), ValidationError> {
        if !self.gradient_pulse_duration_seconds.is_finite()
            || self.gradient_pulse_duration_seconds <= 0.0
            || !self.diffusion_time_seconds.is_finite()
            || self.diffusion_time_seconds <= 0.0
            || self
                .recovery_delay_seconds
                .is_some_and(|v| !v.is_finite() || v < 0.0)
        {
            return Err(ValidationError::InvalidNumber(
                "diffusion acquisition metadata",
            ));
        }
        for text in [
            Some(self.gradient_parameter.as_str()),
            Some(self.gradient_pulse_duration_parameter.as_str()),
            Some(self.diffusion_time_parameter.as_str()),
            self.recovery_delay_parameter.as_deref(),
            self.gradient_shape.as_deref(),
            self.gradient_shape_parameter.as_deref(),
        ]
        .into_iter()
        .flatten()
        {
            if text.trim().is_empty() {
                return Err(ValidationError::EmptySourceParameter);
            }
        }
        if self.recovery_delay_seconds.is_some() != self.recovery_delay_parameter.is_some()
            || self.gradient_shape.is_some() != self.gradient_shape_parameter.is_some()
        {
            return Err(ValidationError::IncompleteSourceEvidence);
        }
        Ok(())
    }
}
/// Common acquisition metadata.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct RawMetadata {
    pub(crate) title: Option<String>,
    pub(crate) solvent: Option<String>,
    pub(crate) temperature_kelvin: Option<f64>,
    pub(crate) scans: Option<u64>,
    pub(crate) pulse_program: Option<String>,
    pub(crate) diffusion: Option<DiffusionAcquisition>,
}
impl RawMetadata {
    /// Creates checked portable acquisition metadata.
    pub fn new(
        title: Option<String>,
        solvent: Option<String>,
        temperature_kelvin: Option<f64>,
        scans: Option<u64>,
        pulse_program: Option<String>,
    ) -> Result<Self, ValidationError> {
        let value = Self {
            title: normalize_text(title),
            solvent: normalize_text(solvent),
            temperature_kelvin,
            scans,
            pulse_program: normalize_text(pulse_program),
            diffusion: None,
        };
        value.validate()?;
        Ok(value)
    }
    /// Returns the experiment title when supplied by the vendor metadata.
    pub fn title(&self) -> Option<&str> {
        self.title.as_deref()
    }
    /// Returns the solvent label when known.
    pub fn solvent(&self) -> Option<&str> {
        self.solvent.as_deref()
    }
    /// Returns the finite non-negative sample temperature in kelvin.
    pub fn temperature_kelvin(&self) -> Option<f64> {
        self.temperature_kelvin
    }
    /// Returns the positive acquisition scan count without normalizing samples.
    pub fn scans(&self) -> Option<u64> {
        self.scans
    }
    /// Returns the vendor pulse-program or sequence name when known.
    pub fn pulse_program(&self) -> Option<&str> {
        self.pulse_program.as_deref()
    }
    /// Adds typed diffusion acquisition facts without deriving analysis results.
    pub fn with_diffusion(
        mut self,
        diffusion: Option<DiffusionAcquisition>,
    ) -> Result<Self, ValidationError> {
        self.diffusion = diffusion;
        self.validate()?;
        Ok(self)
    }
    /// Returns typed diffusion acquisition facts when source evidence is complete.
    pub fn diffusion(&self) -> Option<&DiffusionAcquisition> {
        self.diffusion.as_ref()
    }
    /// Validates numeric acquisition metadata.
    pub fn validate(&self) -> Result<(), ValidationError> {
        if self
            .temperature_kelvin
            .is_some_and(|value| !value.is_finite() || value < 0.0)
        {
            return Err(ValidationError::InvalidNumber("temperature"));
        }
        if self.scans == Some(0) {
            return Err(ValidationError::InvalidNumber("scans"));
        }
        if let Some(diffusion) = &self.diffusion {
            diffusion.validate()?;
        }
        Ok(())
    }
}

pub(super) fn normalize_text(value: Option<String>) -> Option<String> {
    value
        .map(|text| text.trim().to_owned())
        .filter(|text| !text.is_empty())
}
/// Opaque vendor metadata retained alongside canonical data.
#[derive(Clone, Debug, PartialEq)]
pub struct VendorMetadata(VendorMetadataInner);

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum VendorMetadataInner {
    Bruker(bruker::Parameters),
    Jeol(jeol::Parameters),
    Varian(varian::Parameters),
    None,
}

impl VendorMetadata {
    /// Creates empty metadata for a caller-constructed acquisition.
    pub fn none() -> Self {
        Self(VendorMetadataInner::None)
    }

    /// Returns Bruker metadata when the input was a Bruker acquisition.
    pub fn as_bruker(&self) -> Option<&bruker::Parameters> {
        match &self.0 {
            VendorMetadataInner::Bruker(value) => Some(value),
            _ => None,
        }
    }

    /// Returns JEOL metadata when the input was a JEOL acquisition.
    pub fn as_jeol(&self) -> Option<&jeol::Parameters> {
        match &self.0 {
            VendorMetadataInner::Jeol(value) => Some(value),
            _ => None,
        }
    }

    /// Returns Varian metadata when the input was a Varian acquisition.
    pub fn as_varian(&self) -> Option<&varian::Parameters> {
        match &self.0 {
            VendorMetadataInner::Varian(value) => Some(value),
            _ => None,
        }
    }

    pub(crate) fn bruker(value: bruker::Parameters) -> Self {
        Self(VendorMetadataInner::Bruker(value))
    }

    pub(crate) fn jeol(value: jeol::Parameters) -> Self {
        Self(VendorMetadataInner::Jeol(value))
    }

    pub(crate) fn varian(value: varian::Parameters) -> Self {
        Self(VendorMetadataInner::Varian(value))
    }

    pub(super) fn sample_normalization(&self) -> crate::provenance::SampleNormalization {
        match &self.0 {
            VendorMetadataInner::Varian(parameters) => {
                let factors = if parameters.block_headers().is_empty() {
                    vec![1.0]
                } else {
                    parameters
                        .block_headers()
                        .iter()
                        .map(|header| 2f64.powi(i32::from(header.scale())))
                        .collect()
                };
                let stored_imaginary_multiplier = parameters.file_header().map_or(1, |header| {
                    let complex = if header.version() == 0 {
                        header.status() & 0x40 != 0
                    } else {
                        header.status() & 0x10 != 0
                    };
                    if complex { -1 } else { 1 }
                });
                crate::provenance::SampleNormalization::reader(factors, stored_imaginary_multiplier)
            }
            VendorMetadataInner::Jeol(parameters) => {
                crate::provenance::SampleNormalization::reader(
                    vec![1.0],
                    parameters.sample_transform().direct_imaginary_multiplier(),
                )
            }
            VendorMetadataInner::Bruker(_) | VendorMetadataInner::None => {
                crate::provenance::SampleNormalization::identity()
            }
        }
    }
}

// Crate-private model decomposition; no wire tags or encoding policy.
impl DiffusionAcquisition {
    #[allow(clippy::type_complexity)]
    pub(crate) fn model_parts(
        &self,
    ) -> (
        &usize,
        &String,
        &f64,
        &String,
        &f64,
        &String,
        &Option<f64>,
        &Option<String>,
        &Option<String>,
        &Option<String>,
    ) {
        (
            &self.gradient_axis,
            &self.gradient_parameter,
            &self.gradient_pulse_duration_seconds,
            &self.gradient_pulse_duration_parameter,
            &self.diffusion_time_seconds,
            &self.diffusion_time_parameter,
            &self.recovery_delay_seconds,
            &self.recovery_delay_parameter,
            &self.gradient_shape,
            &self.gradient_shape_parameter,
        )
    }
    #[allow(clippy::type_complexity)]
    pub(crate) fn from_model_parts(
        parts: (
            usize,
            String,
            f64,
            String,
            f64,
            String,
            Option<f64>,
            Option<String>,
            Option<String>,
            Option<String>,
        ),
    ) -> Result<Self, crate::internal::ModelError> {
        let (
            gradient_axis,
            gradient_parameter,
            gradient_pulse_duration_seconds,
            gradient_pulse_duration_parameter,
            diffusion_time_seconds,
            diffusion_time_parameter,
            recovery_delay_seconds,
            recovery_delay_parameter,
            gradient_shape,
            gradient_shape_parameter,
        ) = parts;
        let value = Self {
            gradient_axis,
            gradient_parameter,
            gradient_pulse_duration_seconds,
            gradient_pulse_duration_parameter,
            diffusion_time_seconds,
            diffusion_time_parameter,
            recovery_delay_seconds,
            recovery_delay_parameter,
            gradient_shape,
            gradient_shape_parameter,
        };

        Ok(value)
    }
}

// Crate-private model decomposition; no wire tags or encoding policy.
impl RawMetadata {
    #[allow(clippy::type_complexity)]
    pub(crate) fn model_parts(
        &self,
    ) -> (
        &Option<String>,
        &Option<String>,
        &Option<f64>,
        &Option<u64>,
        &Option<String>,
        &Option<DiffusionAcquisition>,
    ) {
        (
            &self.title,
            &self.solvent,
            &self.temperature_kelvin,
            &self.scans,
            &self.pulse_program,
            &self.diffusion,
        )
    }
    #[allow(clippy::type_complexity)]
    pub(crate) fn from_model_parts(
        parts: (
            Option<String>,
            Option<String>,
            Option<f64>,
            Option<u64>,
            Option<String>,
            Option<DiffusionAcquisition>,
        ),
    ) -> Result<Self, crate::internal::ModelError> {
        let (title, solvent, temperature_kelvin, scans, pulse_program, diffusion) = parts;
        let value = Self {
            title,
            solvent,
            temperature_kelvin,
            scans,
            pulse_program,
            diffusion,
        };

        value
            .validate()
            .map_err(|e| crate::internal::ModelError::Validation(e.to_string()))?;

        Ok(value)
    }
}

// Crate-private model decomposition; no wire tags or encoding policy.
impl VendorMetadata {
    #[allow(clippy::type_complexity)]
    pub(crate) fn model_parts(&self) -> (&VendorMetadataInner,) {
        (&self.0,)
    }
    #[allow(clippy::type_complexity)]
    pub(crate) fn from_model_parts(
        parts: (VendorMetadataInner,),
    ) -> Result<Self, crate::internal::ModelError> {
        let (f0,) = parts;
        let value = Self(f0);

        Ok(value)
    }
}