knuckles_parse/records/
nummdl.rs

1#[cfg(feature = "serde")]
2use serde::{Deserialize, Serialize};
3
4#[cfg(feature = "python")]
5use pyo3::prelude::*;
6
7#[cfg(feature = "python")]
8use knuckles_macro::pydefault;
9
10/// Represents a NUMMDL record specifying the number of models in a PDB file.
11///
12/// NUMMDL records specify the total number of MODEL/ENDMDL groups in the file.
13/// This is particularly useful for NMR structures and molecular dynamics trajectories.
14///
15/// # Fields
16///
17/// - `count`: Total number of models in the file
18#[derive(Debug, Clone)]
19#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
20#[cfg_attr(feature = "python", pyclass(get_all, set_all))]
21#[cfg_attr(feature = "python", pydefault)]
22pub struct NummdlRecord {
23    /// Total number of models in the file
24    pub count: u32,
25}
26
27impl NummdlRecord {
28    /// Create a new NummdlRecord by parsing a NUMMDL line.
29    pub fn new(str: &str) -> NummdlRecord {
30        NummdlRecord {
31            count: str[10..14].trim().parse::<u32>().unwrap_or_default(),
32        }
33    }
34}
35
36impl From<&str> for NummdlRecord {
37    fn from(str: &str) -> Self {
38        NummdlRecord::new(str)
39    }
40}
41
42impl std::fmt::Display for NummdlRecord {
43    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44        write!(f, "{:<1$}", format!("NUMMDL    {:>4}", self.count), 80)
45    }
46}
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51    #[test]
52    fn test_model_record_new() {
53        let line = "NUMMDL       1";
54        let model = NummdlRecord::new(line);
55        assert_eq!(model.count, 1);
56    }
57    #[test]
58    fn test_model_record_from() {
59        let line = "NUMMDL       1";
60        let model = NummdlRecord::from(line);
61        assert_eq!(model.count, 1);
62    }
63    #[test]
64    fn test_model_record_display() {
65        let model = NummdlRecord { count: 1 };
66        let result =
67            "NUMMDL       1                                                                  ";
68        assert_eq!(format!("{}", model), result);
69    }
70}