knuckles_parse/records/
het.rs1#[cfg(feature = "serde")]
2use serde::{Deserialize, Serialize};
3
4#[cfg(feature = "python")]
5use knuckles_macro::pydefault;
6
7#[cfg(feature = "python")]
8use pyo3::prelude::*;
9
10#[derive(Debug, Clone)]
11#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
12#[cfg_attr(feature = "python", pyclass(get_all, set_all))]
13#[cfg_attr(feature = "python", pydefault)]
14pub struct HetRecord {
15 pub het_id: String,
16 pub chain_id: char,
17 pub seq_num: i32,
18 pub i_code: Option<char>,
19 pub num_het_atoms: i32,
20 pub text: Option<String>,
21}
22
23impl HetRecord {
24 pub fn new(line: &str) -> Self {
25 HetRecord {
26 het_id: line[7..10].trim().to_string(),
27 chain_id: line.chars().nth(12).unwrap(),
28 seq_num: line[13..17].trim().parse().unwrap(),
29 i_code: line[17..18].trim().parse().ok(),
30 num_het_atoms: line[21..26].trim().parse().unwrap(),
31 text: line
32 .get(31..71)
33 .map(|str| str.trim().to_string())
34 .filter(|item| !item.is_empty()),
35 }
36 }
37}
38
39impl From<&str> for HetRecord {
40 fn from(value: &str) -> Self {
41 HetRecord::new(value)
42 }
43}
44
45#[cfg(test)]
46mod tests {
47 use super::*;
48
49 #[test]
50 fn test_new_het_record() {
51 let line = "HET UDP A1457 25 ";
52 let record = HetRecord::new(line);
53 assert_eq!(record.het_id, "UDP");
54 assert_eq!(record.chain_id, 'A');
55 assert_eq!(record.seq_num, 1457);
56 assert_eq!(record.i_code, None);
57 assert_eq!(record.num_het_atoms, 25);
58 assert_eq!(record.text, None);
59
60 let line = "HET UNK A 161 1 ";
61 let record = HetRecord::new(line);
62 assert_eq!(record.het_id, "UNK");
63 assert_eq!(record.chain_id, 'A');
64 assert_eq!(record.seq_num, 161);
65 assert_eq!(record.i_code, None);
66 assert_eq!(record.num_het_atoms, 1);
67 assert_eq!(record.text, None);
68 }
69
70 #[test]
71 fn test_from_str() {
72 let line = "HET UDP A1457 25 ";
73 let record: HetRecord = line.into();
74 assert_eq!(record.het_id, "UDP");
75 assert_eq!(record.chain_id, 'A');
76 assert_eq!(record.seq_num, 1457);
77 assert_eq!(record.i_code, None);
78 assert_eq!(record.num_het_atoms, 25);
79 assert_eq!(record.text, None);
80 }
81}