ferritin_core/views/
residue.rs1use super::atom::AtomView;
2use crate::AtomCollection;
3use crate::info::constants::{is_amino_acid, is_carbohydrate, is_nucleotide};
4
5pub struct ResidueView<'a> {
7 pub(crate) data: &'a AtomCollection,
8 pub(crate) start_atom_idx: usize,
9 pub(crate) end_atom_idx: usize,
10}
11
12impl<'a> ResidueView<'a> {
13 pub fn new(data: &'a AtomCollection, start_atom_idx: usize, end_atom_idx: usize) -> Self {
14 ResidueView {
15 data,
16 start_atom_idx,
17 end_atom_idx,
18 }
19 }
20
21 pub fn atom_count(&self) -> usize {
22 self.end_atom_idx - self.start_atom_idx
23 }
24 pub fn chain_id(&self) -> &str {
25 self.data.get_chain_id(self.start_atom_idx)
26 }
27 pub fn is_amino_acid(&self) -> bool {
28 is_amino_acid(self.residue_name())
29 }
30 pub fn is_nucleotide(&self) -> bool {
32 is_nucleotide(self.residue_name())
33 }
34 pub fn is_carbohydrate(&self) -> bool {
35 is_carbohydrate(self.residue_name())
36 }
37 pub fn iter_atoms(&self) -> impl Iterator<Item = AtomView<'_>> + '_ {
38 (self.start_atom_idx..self.end_atom_idx).map(move |idx| AtomView::new(self.data, idx))
39 }
40 pub fn residue_id(&self) -> i32 {
41 *self.data.get_res_id(self.start_atom_idx)
42 }
43 pub fn residue_name(&self) -> &str {
44 &self.data.get_res_name(self.start_atom_idx)
45 }
46
47 pub fn find_atom_by_name(&self, name: &str) -> Option<AtomView<'a>> {
53 (self.start_atom_idx..self.end_atom_idx)
54 .find(|&i| self.data.get_atom_name(i) == name)
55 .map(|idx| AtomView::new(self.data, idx))
56 }
57}