molframe-core 0.2.1

Core types, storage and analysis contracts for molframe
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
//! The normalised structure: what a file means, rather than how it was written.
//!
//! A structure is immutable and shared by reference. Editing produces a new one
//! rather than changing this one in place, which is what makes handing out a
//! pointer to the coordinates safe: a buffer given to a caller belongs to a
//! snapshot that can never change underneath it, and every derived index stays
//! valid for as long as the snapshot it was built against.
//!
//! Formatting is not preserved here. Reproducing whitespace, column widths and
//! quoting is the document's job; this layer preserves meaning.

use super::ExtensionStore;
use crate::annotation::AtomAnnotations;
use crate::chunk::AtomChunk;
use crate::coords::{CoordinateBlock, CoordinateGeneration};
use crate::index::ModelIndex;
use crate::symbol::{Interner, SymbolId};
use crate::topology::Topology;
use std::fmt;
use std::sync::Arc;

const PLACEHOLDER_TOLERANCE: f64 = 1e-6;

/// What the entry as a whole says about itself.
///
/// The identifier is a variable-length string. Four characters was a property of
/// one file format, not of the archive, and extended identifiers are already
/// being issued.
#[derive(Clone, Debug, Default)]
pub struct EntryMetadata {
    /// The entry identifier, as deposited.
    pub id: Option<Box<str>>,
    /// The entry title.
    pub title: Option<Box<str>>,
    /// The experimental method.
    pub method: Option<Box<str>>,
    /// Resolution in ångström, where the method reports one.
    pub resolution: Option<f32>,
}

/// The crystallographic cell.
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct UnitCell {
    /// Cell edge lengths in ångström.
    pub lengths: [f64; 3],
    /// Cell angles in degrees.
    pub angles: [f64; 3],
}

impl UnitCell {
    /// Returns true when the cell is the placeholder some files write when they
    /// have no cell at all.
    ///
    /// A unit cube with right angles is what a structure determined without
    /// crystallography carries, and treating it as a real cell would produce
    /// periodic images of a molecule that was never in a crystal.
    #[must_use]
    pub fn is_placeholder(&self) -> bool {
        values_are_near(&self.lengths, 1.0) && values_are_near(&self.angles, 90.0)
    }
}

/// Where the positions of each model live.
#[derive(Clone, Debug)]
pub enum CoordinateStore {
    /// One model.
    Single(CoordinateBlock),
    /// Several models over identical topology, so only positions differ.
    ///
    /// This covers ensembles and trajectory frames, where annotations are shared
    /// and repeating them per frame would multiply the memory by the frame count.
    Dense {
        /// One block per model, each with the same atom count.
        frames: Vec<CoordinateBlock>,
    },
    /// Models whose atom content genuinely differs.
    ///
    /// Padding these into one rectangular block would fabricate atoms that were
    /// never modelled and destroy the difference between absent and unrecorded,
    /// so they are held as separate structures instead.
    Ragged {
        /// One structure per model.
        models: Vec<Structure>,
    },
}

impl CoordinateStore {
    pub(crate) fn make_unique_in(
        &mut self,
        context: &crate::ExecutionContext,
    ) -> Result<(), crate::MemoryBudgetError> {
        match self {
            Self::Single(block) => block.make_unique_in(context),
            Self::Dense { frames } => {
                for frame in frames {
                    frame.make_unique_in(context)?;
                }
                Ok(())
            }
            Self::Ragged { .. } => Ok(()),
        }
    }

    /// The number of models.
    #[must_use]
    pub fn model_count(&self) -> usize {
        match self {
            Self::Single(_) => 1,
            Self::Dense { frames } => frames.len(),
            Self::Ragged { models } => models.len(),
        }
    }

    /// The positions of one model, where they are held directly.
    ///
    /// A ragged ensemble answers `None`: its models are structures in their own
    /// right and are reached through [`CoordinateStore::Ragged`].
    #[must_use]
    pub fn block(&self, model: ModelIndex) -> Option<&CoordinateBlock> {
        match self {
            Self::Single(block) => (model.get() == 0).then_some(block),
            Self::Dense { frames } => frames.get(model.as_usize()),
            // A ragged ensemble's models are structures in their own right, and
            // a single-model store has no second model to offer.
            Self::Ragged { .. } => None,
        }
    }

    /// Returns true when every model shares one topology.
    #[must_use]
    pub const fn is_dense(&self) -> bool {
        matches!(self, Self::Single(_) | Self::Dense { .. })
    }

    /// The independent structures of a ragged ensemble.
    #[must_use]
    pub fn ragged_models(&self) -> Option<&[Structure]> {
        match self {
            Self::Ragged { models } => Some(models),
            Self::Single(_) | Self::Dense { .. } => None,
        }
    }
}

/// Everything a structure holds.
#[derive(Clone, Debug)]
pub struct StructureData {
    /// What the entry says about itself.
    pub entry: EntryMetadata,
    /// The hierarchy.
    pub topology: Topology,
    /// The atoms, in chunks.
    pub chunks: Arc<Vec<AtomChunk>>,
    /// Chemical connectivity as compact edge columns.
    pub bonds: crate::bond::BondTable,
    /// Structure-local typed columns aligned exactly to atom rows.
    pub annotations: AtomAnnotations,
    /// Cold, typed domain metadata attached to this snapshot.
    pub extensions: ExtensionStore,
    /// The positions.
    pub coords: CoordinateStore,
    /// The identifiers this structure interned.
    pub dictionary: Interner,
    /// The crystallographic cell, where the file carried one.
    pub cell: Option<UnitCell>,
    /// How many times the positions have changed.
    pub generation: CoordinateGeneration,
    pub(crate) reservation: Option<Arc<crate::MemoryReservation>>,
}

/// An immutable structure, shared by reference.
///
/// Cloning one is a reference count, not a copy, so passing a structure to
/// another thread or holding several versions at once is cheap.
///
/// # Examples
///
/// ```
/// use molframe_core::structure::{Structure, StructureData};
///
/// let structure = Structure::new(StructureData::empty());
/// assert_eq!(structure.atom_count(), 0);
/// assert_eq!(structure.model_count(), 1);
/// ```
#[repr(C)]
#[derive(Clone, Debug)]
pub struct Structure(Arc<StructureData>);

impl StructureData {
    /// A structure with nothing in it.
    #[must_use]
    pub fn empty() -> Self {
        Self {
            reservation: None,
            entry: EntryMetadata::default(),
            topology: Topology::default(),
            chunks: Arc::new(Vec::new()),
            bonds: crate::bond::BondTable::default(),
            annotations: AtomAnnotations::default(),
            extensions: ExtensionStore::new(),
            coords: CoordinateStore::Single(CoordinateBlock::new()),
            dictionary: Interner::new(),
            cell: None,
            generation: CoordinateGeneration::INITIAL,
        }
    }

    pub(crate) fn atom_count(&self) -> u32 {
        match self.chunks.last() {
            Some(chunk) => chunk.atoms().end,
            None => 0,
        }
    }
}

impl Structure {
    /// Wraps structure data for sharing.
    #[must_use]
    pub fn new(data: StructureData) -> Self {
        Self(Arc::new(data))
    }

    pub(crate) fn retain_reservation(mut self, reservation: Arc<crate::MemoryReservation>) -> Self {
        let data = Arc::make_mut(&mut self.0);
        if let CoordinateStore::Ragged { models } = &mut data.coords {
            for model in models {
                *model = model.clone().retain_reservation(Arc::clone(&reservation));
            }
        }
        data.reservation = Some(reservation);
        self
    }

    /// The data behind the reference.
    #[must_use]
    pub fn data(&self) -> &StructureData {
        &self.0
    }

    /// The shared handle, for a view that must outlive this reference.
    #[must_use]
    pub fn shared(&self) -> Arc<StructureData> {
        Arc::clone(&self.0)
    }

    /// The number of atoms in one model.
    #[must_use]
    pub fn atom_count(&self) -> u32 {
        self.0.atom_count()
    }

    /// A lightweight handle on one atom.
    #[must_use]
    pub fn atom(&self, atom: crate::index::AtomIndex) -> Option<super::AtomRef<'_>> {
        self.0.atom(atom)
    }

    /// A lightweight handle on one model.
    #[must_use]
    pub fn model(&self, model: ModelIndex) -> Option<super::ModelRef<'_>> {
        self.0.model(model)
    }

    /// The structure and local model position that represent one model.
    ///
    /// Dense models share this structure and retain their position. Ragged
    /// models are independent single-model structures, so their local position
    /// is zero. This keeps callers from padding unlike topologies.
    #[must_use]
    pub fn model_snapshot(&self, model: ModelIndex) -> Option<(Self, ModelIndex)> {
        match &self.0.coords {
            CoordinateStore::Ragged { models } => models
                .get(model.as_usize())
                .cloned()
                .map(|structure| (structure, ModelIndex::new(0))),
            CoordinateStore::Single(_) | CoordinateStore::Dense { .. } => self
                .model(model)
                .map(|_| (self.clone(), model))
                .or_else(|| {
                    (model.get() == 0 && self.model_count() == 1).then(|| (self.clone(), model))
                }),
        }
    }

    /// A lightweight handle on one chain.
    #[must_use]
    pub fn chain(&self, chain: crate::index::ChainIndex) -> Option<super::ChainRef<'_>> {
        self.0.chain(chain)
    }

    /// A lightweight handle on one residue.
    #[must_use]
    pub fn residue(&self, residue: crate::index::ResidueIndex) -> Option<super::ResidueRef<'_>> {
        self.0.residue(residue)
    }

    /// The number of models.
    #[must_use]
    pub fn model_count(&self) -> usize {
        self.0.coords.model_count()
    }

    /// The independent model structures when topology differs between models.
    #[must_use]
    pub fn ragged_models(&self) -> Option<&[Self]> {
        self.0.coords.ragged_models()
    }

    /// The number of chains.
    #[must_use]
    pub fn chain_count(&self) -> usize {
        self.0.topology.chains.len()
    }

    /// The number of residues.
    #[must_use]
    pub fn residue_count(&self) -> usize {
        self.0.topology.residues.len()
    }

    /// The number of distinct chemical species.
    #[must_use]
    pub fn entity_count(&self) -> usize {
        self.0.topology.entities.len()
    }

    /// The positions of the first model, contiguously.
    ///
    /// This slice is the buffer itself, not a copy of it, which is what makes
    /// handing coordinates to a numeric array free.
    #[must_use]
    pub fn positions(&self) -> &[[f32; 3]] {
        match self.model_positions(ModelIndex::new(0)) {
            Some(positions) => positions,
            None => &[],
        }
    }

    /// The positions of one model.
    #[must_use]
    pub fn model_positions(&self, model: ModelIndex) -> Option<&[[f32; 3]]> {
        self.0.coords.block(model).map(CoordinateBlock::as_slice)
    }

    /// How many times the positions have changed.
    #[must_use]
    pub fn generation(&self) -> CoordinateGeneration {
        self.0.generation
    }

    /// Structure-local typed columns aligned to atom rows.
    #[must_use]
    pub fn annotations(&self) -> &AtomAnnotations {
        &self.0.annotations
    }

    /// Cold, typed domain metadata attached to this snapshot.
    #[must_use]
    pub fn extensions(&self) -> &ExtensionStore {
        &self.0.extensions
    }

    /// Returns a new snapshot with one typed domain extension attached.
    #[must_use]
    pub fn with_extension<T>(&self, key: &'static str, value: T) -> Self
    where
        T: std::any::Any + Send + Sync + std::panic::RefUnwindSafe,
    {
        let mut data = self.data().clone();
        data.extensions.insert(key, value);
        Self::new(data)
    }

    /// Returns a new snapshot without domain extensions.
    #[must_use]
    pub fn without_extensions(&self) -> Self {
        if self.0.extensions.is_empty() {
            return self.clone();
        }
        let mut data = self.data().clone();
        data.extensions.clear();
        Self::new(data)
    }

    /// The string an interned identifier names.
    #[must_use]
    pub fn resolve(&self, symbol: SymbolId) -> Option<&str> {
        self.0.dictionary.resolve(symbol)
    }
}

impl From<StructureData> for Structure {
    fn from(data: StructureData) -> Self {
        Self::new(data)
    }
}

fn values_are_near(values: &[f64], expected: f64) -> bool {
    values
        .iter()
        .all(|value| (value - expected).abs() < PLACEHOLDER_TOLERANCE)
}

impl fmt::Display for Structure {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "Structure(models={}, chains={}, residues={}, atoms={})",
            self.model_count(),
            self.chain_count(),
            self.residue_count(),
            self.atom_count(),
        )
    }
}

#[cfg(test)]
#[path = "data_tests.rs"]
mod tests;