Skip to main content

ferritin_core/views/
model_atom.rs

1use crate::info::elements::Element;
2use crate::model::Model;
3
4/// Read-only view into a single atom from a [`Model`].
5pub struct ModelAtomView<'a> {
6    pub(crate) model: &'a Model,
7    pub(crate) idx: usize,
8}
9
10impl<'a> ModelAtomView<'a> {
11    pub(crate) fn new(model: &'a Model, idx: usize) -> Self {
12        ModelAtomView { model, idx }
13    }
14
15    /// `[x, y, z]` for this atom (AoS convenience; allocation-free from SoA).
16    pub fn coords(&self) -> [f32; 3] {
17        self.model.coord(self.idx)
18    }
19
20    /// Element parsed from the symbol stored in the hierarchy.
21    /// Returns `Element::C` for unrecognised symbols.
22    pub fn element(&self) -> Element {
23        let sym = &self.model.hierarchy.atoms.element[self.idx];
24        Element::from_symbol(sym).unwrap_or(Element::C)
25    }
26
27    pub fn atom_name(&self) -> &'a str {
28        &self.model.hierarchy.atoms.atom_name[self.idx]
29    }
30
31    pub fn residue_id(&self) -> i32 {
32        let res_idx = self.model.hierarchy.residue_of_atom(self.idx);
33        self.model.hierarchy.residues.auth_seq_id[res_idx]
34    }
35
36    pub fn residue_name(&self) -> &'a str {
37        let res_idx = self.model.hierarchy.residue_of_atom(self.idx);
38        &self.model.hierarchy.residues.comp_id[res_idx]
39    }
40
41    pub fn chain_id(&self) -> &'a str {
42        let res_idx = self.model.hierarchy.residue_of_atom(self.idx);
43        let chain_idx = self.model.hierarchy.chain_of_residue(res_idx);
44        &self.model.hierarchy.chains.auth_asym_id[chain_idx]
45    }
46
47    pub fn is_hetero(&self) -> bool {
48        let res_idx = self.model.hierarchy.residue_of_atom(self.idx);
49        self.model.hierarchy.residues.group[res_idx]
50            == crate::model::tables::ResidueGroup::NonPolymer
51    }
52
53    pub fn index(&self) -> usize {
54        self.idx
55    }
56}