molframe-core 0.6.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
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
//! Explicit selection of alternate conformations.
//!
//! Reading never collapses disorder. This module turns a declared analysis
//! policy into an atom selection while leaving the stored rows untouched.

mod compat;

use super::{AtomRef, ResidueRef, Structure};
use crate::contract::{AltlocPolicy, Analysis, AnalysisPolicy, Coverage, Indeterminacy, Quality};
use crate::diagnostic::{Code, Diagnostic};
use crate::hashing::IdentityBuildHasher;
use crate::selection::AtomSelection;
use crate::symbol::{AltId, SymbolId};
pub use compat::{altloc_compatible, compatible_pairs};
use hashbrown::HashMap;

/// Occupancy scratch keyed by an identifier the engine assigned itself.
///
/// The keys are altloc and symbol identifiers, never attacker-chosen, so the
/// keyed pseudo-random default hasher buys nothing and costs a lookup each.
type ScoreTable<K, V> = HashMap<K, V, IdentityBuildHasher>;

/// Accumulated occupancy for one alternate-conformation label.
///
/// `order` records when the label was first encountered so occupancy ties
/// preserve the original deterministic first-label behavior.
#[derive(Clone, Copy)]
struct LabelScore {
    occupancy: f32,
    order: usize,
}

/// The label selected for one residue or chain and whether disorder was seen.
#[derive(Clone, Copy)]
struct LabelChoice {
    selected: Option<AltId>,
    has_altlocs: bool,
}

/// Internal result of resolving one alternate-conformation policy.
///
/// Besides the selected atoms, resolution carries metadata already discovered
/// while traversing atoms so callers do not need additional full-structure
/// scans.
struct AltlocResolution {
    selection: AtomSelection,
    missing_label: bool,
    has_altlocs: bool,
}

/// Best retained row for one atom name under per-atom occupancy resolution.
#[derive(Clone, Copy)]
struct AtomChoice {
    atom: u32,
    occupancy: f32,
}

impl Structure {
    /// Selects atom rows under the policy's alternate-conformation rule.
    ///
    /// Blank-labelled atoms are retained under every rule because they belong
    /// to every conformation. The stored structure is never changed.
    ///
    /// # Panics
    ///
    /// This function does not intentionally panic when an internal selector
    /// reports an impossible cardinality; such a result is returned as an
    /// indeterminate analysis instead.
    #[must_use]
    pub fn resolve_altlocs(&self, policy: &AnalysisPolicy) -> Analysis<AtomSelection> {
        if let Err(finding) = self.require_resolved_model() {
            return Analysis::indeterminate(
                Indeterminacy::UnresolvedConformations,
                Coverage {
                    intended: 0,
                    used: 0,
                    missing: 0,
                    ambiguous: 0,
                },
                policy,
            )
            .with_warning(finding);
        }

        let total = self.atom_count();

        let AltlocResolution {
            selection,
            missing_label,
            has_altlocs,
        } = match &policy.altloc {
            AltlocPolicy::KeepAll => AltlocResolution {
                selection: AtomSelection::All(total),
                missing_label: false,
                has_altlocs: self.has_altlocs(),
            },
            AltlocPolicy::ConformerConsistent => self.conformer_consistent(),
            AltlocPolicy::Label(label) => self.named_conformer(label),
            AltlocPolicy::First => self.per_residue(&FirstLabel),
            AltlocPolicy::HighestOccupancyPerResidue => self.per_residue(&HighestOccupancy),
            AltlocPolicy::HighestOccupancyPerAtom => self.per_atom_occupancy(),
        };

        let selected_rows = selection.len();
        let used = match u32::try_from(selected_rows) {
            Ok(used) if used <= total => used,
            _ => {
                return Analysis::indeterminate(
                    Indeterminacy::UnresolvedConformations,
                    Coverage {
                        intended: total,
                        used: 0,
                        missing: total,
                        ambiguous: 0,
                    },
                    policy,
                )
                .with_warning(
                    Diagnostic::new(Code::E3001)
                        .with_context("selection rows", selected_rows.to_string())
                        .with_context("atoms", total.to_string()),
                );
            }
        };

        let mut result = Analysis::complete(
            selection,
            Coverage {
                intended: total,
                used,
                missing: 0,
                ambiguous: total - used,
            },
            policy,
        );

        if matches!(&policy.altloc, AltlocPolicy::KeepAll) && has_altlocs {
            result.degrade(Quality::Ambiguous);
        }

        if policy.altloc.is_hazardous() && has_altlocs {
            result.warnings.push(Diagnostic::new(Code::W6001));
        }

        if missing_label {
            result.warnings.push(
                Diagnostic::new(Code::W4003)
                    .with_context("altloc", named_policy_label(&policy.altloc)),
            );
        }

        result
    }

    /// Returns whether the structure contains any non-blank alternate label.
    ///
    /// This scan is needed only for policies such as `KeepAll` that otherwise
    /// do not inspect individual atom rows.
    fn has_altlocs(&self) -> bool {
        self.data()
            .atoms()
            .any(|atom| atom.alt_id().is_some_and(|label| !label.is_blank()))
    }

    /// Resolves a specifically named alternate conformation.
    ///
    /// Blank atoms are always retained. A label absent from either the symbol
    /// dictionary or the atom rows yields the blank-only selection and marks
    /// the requested label as missing.
    fn named_conformer(&self, label: &str) -> AltlocResolution {
        let wanted = self.data().dictionary.get(label).and_then(AltId::labelled);

        let requested_valid = wanted.is_some();
        let (selection, present, has_altlocs) = self.select_label(wanted);

        AltlocResolution {
            selection,
            missing_label: !requested_valid || !present,
            has_altlocs,
        }
    }

    /// Selects blank rows together with one optional alternate label.
    ///
    /// Returns the selection, whether the requested non-blank label was
    /// encountered, and whether any alternate conformations exist.
    fn select_label(&self, selected: Option<AltId>) -> (AtomSelection, bool, bool) {
        let mut positions = Vec::new();
        let mut selected_present = false;
        let mut has_altlocs = false;

        for atom in self.data().atoms() {
            let Some(label) = atom.alt_id() else {
                continue;
            };

            if !label.is_blank() {
                has_altlocs = true;
            }

            if Some(label) == selected {
                selected_present = true;
            }

            if label.is_blank() || Some(label) == selected {
                positions.push(atom.index().get());
            }
        }

        (
            AtomSelection::from_sorted(positions),
            selected_present,
            has_altlocs,
        )
    }

    /// Chooses one occupancy-maximizing label consistently for each chain.
    ///
    /// A reusable score table avoids allocating fresh aggregation storage for
    /// every chain while preserving deterministic first-seen tie breaking.
    fn conformer_consistent(&self) -> AltlocResolution {
        let mut positions = Vec::new();
        let mut scores = ScoreTable::default();
        let mut has_altlocs = false;

        for chain in self.data().chains() {
            let choice = best_label(chain.residues().flat_map(ResidueRef::atoms), &mut scores);

            has_altlocs |= choice.has_altlocs;

            for atom in chain.residues().flat_map(ResidueRef::atoms) {
                if keeps_label(atom, choice.selected) {
                    positions.push(atom.index().get());
                }
            }
        }

        AltlocResolution {
            selection: AtomSelection::from_sorted(positions),
            missing_label: false,
            has_altlocs,
        }
    }

    /// Resolves alternate conformations independently for every residue.
    ///
    /// The chooser receives reusable occupancy scratch storage so repeated
    /// residue resolution retains hash-table capacity instead of reallocating.
    fn per_residue(&self, chooser: &impl LabelChooser) -> AltlocResolution {
        let mut positions = Vec::new();
        let mut scores = ScoreTable::default();
        let mut has_altlocs = false;

        for residue in self.data().residues() {
            let choice = chooser.choose(residue, &mut scores);
            has_altlocs |= choice.has_altlocs;

            positions.extend(
                residue
                    .atoms()
                    .filter(|atom| keeps_label(*atom, choice.selected))
                    .map(|atom| atom.index().get()),
            );
        }

        AltlocResolution {
            selection: AtomSelection::from_sorted(positions),
            missing_label: false,
            has_altlocs,
        }
    }

    /// Selects the highest-occupancy alternate row independently per atom name.
    ///
    /// Choices are reused across residues and selected atoms are emitted during
    /// a second ordered residue traversal, avoiding a global sort of results.
    fn per_atom_occupancy(&self) -> AltlocResolution {
        let mut positions = Vec::new();
        let mut choices: ScoreTable<SymbolId, AtomChoice> = ScoreTable::default();
        let mut has_altlocs = false;

        for residue in self.data().residues() {
            choices.clear();

            for atom in residue.atoms() {
                let Some(label) = atom.alt_id() else {
                    continue;
                };

                if label.is_blank() {
                    continue;
                }

                has_altlocs = true;

                let Some(name) = atom.name_symbol() else {
                    continue;
                };

                record_atom_choice(&mut choices, name, atom);
            }

            for atom in residue.atoms() {
                let Some(label) = atom.alt_id() else {
                    continue;
                };

                if label.is_blank() {
                    positions.push(atom.index().get());
                    continue;
                }

                let Some(name) = atom.name_symbol() else {
                    continue;
                };

                let Some(choice) = choices.get(&name) else {
                    continue;
                };

                if choice.atom == atom.index().get() {
                    positions.push(choice.atom);
                }
            }
        }

        AltlocResolution {
            selection: AtomSelection::from_sorted(positions),
            missing_label: false,
            has_altlocs,
        }
    }
}

/// Chooses one alternate label for a residue.
///
/// Implementations may use the supplied score map as reusable scratch storage;
/// callers clear or overwrite it through the chooser implementation.
trait LabelChooser {
    /// Returns the selected label together with whether the residue is disordered.
    fn choose(
        &self,
        residue: ResidueRef<'_>,
        scores: &mut ScoreTable<AltId, LabelScore>,
    ) -> LabelChoice;
}

/// Chooses the first non-blank alternate label in atom order.
struct FirstLabel;

impl LabelChooser for FirstLabel {
    /// Returns the first non-blank alternate label present in the residue.
    fn choose(
        &self,
        residue: ResidueRef<'_>,
        _scores: &mut ScoreTable<AltId, LabelScore>,
    ) -> LabelChoice {
        let selected = residue
            .atoms()
            .filter_map(AtomRef::alt_id)
            .find(|label| !label.is_blank());

        LabelChoice {
            selected,
            has_altlocs: selected.is_some(),
        }
    }
}

/// Chooses the alternate label with the greatest accumulated occupancy.
struct HighestOccupancy;

impl LabelChooser for HighestOccupancy {
    /// Aggregates occupancy by label and preserves first-seen ordering on ties.
    fn choose(
        &self,
        residue: ResidueRef<'_>,
        scores: &mut ScoreTable<AltId, LabelScore>,
    ) -> LabelChoice {
        best_label(residue.atoms(), scores)
    }
}

/// Finds the occupancy-maximizing non-blank alternate label.
///
/// The reusable hash table gives expected constant-time label aggregation.
/// First-seen order is stored explicitly so hash iteration order cannot affect
/// ties or the original deterministic behavior.
fn best_label<'a>(
    atoms: impl Iterator<Item = AtomRef<'a>>,
    scores: &mut ScoreTable<AltId, LabelScore>,
) -> LabelChoice {
    scores.clear();

    for atom in atoms {
        let Some(label) = atom.alt_id() else {
            continue;
        };

        if label.is_blank() {
            continue;
        }

        let occupancy = occupancy(atom);
        let order = scores.len();

        scores
            .entry(label)
            .and_modify(|score| score.occupancy += occupancy)
            .or_insert(LabelScore { occupancy, order });
    }

    LabelChoice {
        selected: best_scored_label(scores),
        has_altlocs: !scores.is_empty(),
    }
}

/// Selects the winning label from accumulated occupancy scores.
///
/// Selection reproduces ordered `>` semantics exactly: ties retain the first
/// label encountered, and a NaN accumulated by the first label continues to
/// dominate exactly as it did under the original sequential comparison.
fn best_scored_label(scores: &ScoreTable<AltId, LabelScore>) -> Option<AltId> {
    let mut earliest: Option<(AltId, LabelScore)> = None;
    let mut best_finite: Option<(AltId, LabelScore)> = None;

    for (&label, &score) in scores {
        if earliest.is_none_or(|(_, current)| score.order < current.order) {
            earliest = Some((label, score));
        }

        if score.occupancy.is_nan() {
            continue;
        }

        if best_finite.is_none_or(|(_, current)| {
            score.occupancy > current.occupancy
                || (score.occupancy.to_bits() == current.occupancy.to_bits()
                    && score.order < current.order)
        }) {
            best_finite = Some((label, score));
        }
    }

    match earliest {
        Some((label, score)) if score.occupancy.is_nan() => Some(label),
        Some(_) => best_finite.map(|(label, _)| label),
        None => None,
    }
}

/// Retains the first atom of equal occupancy; only a strictly greater one replaces it.
fn record_atom_choice(
    choices: &mut ScoreTable<SymbolId, AtomChoice>,
    name: SymbolId,
    atom: AtomRef<'_>,
) {
    let candidate = AtomChoice {
        atom: atom.index().get(),
        occupancy: occupancy(atom),
    };

    choices
        .entry(name)
        .and_modify(|current| {
            if candidate.occupancy > current.occupancy {
                *current = candidate;
            }
        })
        .or_insert(candidate);
}

/// Returns the recorded atom occupancy, defaulting absent values to one.
fn occupancy(atom: AtomRef<'_>) -> f32 {
    let Some(value) = atom.occupancy() else {
        return 1.0;
    };
    value
}

/// Whether an atom belongs to the selected conformation (blank labels always do).
fn keeps_label(atom: AtomRef<'_>, selected: Option<AltId>) -> bool {
    atom.alt_id()
        .is_some_and(|label| label.is_blank() || Some(label) == selected)
}

/// Owned diagnostic context for a named policy; allocates only on the missing-label warning.
fn named_policy_label(policy: &AltlocPolicy) -> Box<str> {
    match policy {
        AltlocPolicy::Label(label) => label.clone(),
        _ => "".into(),
    }
}

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