knuckles_parse/
lib.rs

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
pub mod records;
use records::Record;

#[cfg(feature = "python")]
use pyo3::prelude::*;

#[cfg(feature = "parallel")]
pub fn pdbreader_parallel(contents: &str) -> Vec<Record> {
    use rayon::prelude::*;

    let lines: Vec<&str> = contents.lines().collect();
    let mut record: Vec<Record> = lines
        .par_iter()
        .filter_map(|line| {
            if line.len() < 6 {
                return None;
            }
            Record::try_from(*line).ok()
        })
        .collect();

    // We then comb through the records and assign serial numbers to atoms that
    // don't have them. This is necessary for some PDB files, which have more than 99999 atoms.
    // NOTE: We don't need to use a second pass in the single threaded version because we can do it
    // in the same pass.
    let mut last = 0;
    for atom in record.iter_mut() {
        if let Record::Atom(atom) = atom {
            if atom.serial == 0 {
                last += 1;
                atom.serial = last;
            } else {
                last = atom.serial;
            }
        }
    }
    record
}

pub fn pdbreader_single(contents: &str) -> Vec<Record> {
    let mut last = 0;
    contents
        .lines()
        .filter_map(|line| {
            if line.len() < 6 {
                return None;
            }
            let record = Record::try_from(line);
            if let Ok(Record::Atom(mut atom)) = record {
                if atom.serial == 0 {
                    last += 1;
                    atom.serial = last;
                } else {
                    last = atom.serial;
                }
                Some(Record::Atom(atom))
            } else {
                None
            }
            // Record::try_from(line).ok()
        })
        .collect()
}

#[cfg(feature = "python")]
#[pymodule(name = "knuckles_parse")]
mod knuckles_parse {
    use super::*;
    #[pymodule_export]
    use crate::records::anisotropic::AnisotropicRecord;
    #[pymodule_export]
    use crate::records::atom::AtomRecord;
    #[pymodule_export]
    use crate::records::connect::ConnectRecord;
    #[pymodule_export]
    use crate::records::crystal::CrystalRecord;
    #[pymodule_export]
    use crate::records::dbref::DBRefRecord;
    #[pymodule_export]
    use crate::records::het::HetRecord;
    #[pymodule_export]
    use crate::records::hetnam::HetnamRecord;
    #[pymodule_export]
    use crate::records::model::ModelRecord;
    #[pymodule_export]
    use crate::records::modres::ModresRecord;
    #[pymodule_export]
    use crate::records::mtrixn::MtrixnRecord;
    #[pymodule_export]
    use crate::records::nummdl::NummdlRecord;
    #[pymodule_export]
    use crate::records::origxn::OrigxnRecord;
    #[pymodule_export]
    use crate::records::scalen::ScalenRecord;
    #[pymodule_export]
    use crate::records::seqadv::SeqAdvRecord;
    #[pymodule_export]
    use crate::records::seqres::SeqresRecord;
    #[pymodule_export]
    use crate::records::term::TermRecord;
    #[pymodule_export]
    use crate::records::Record;

    /// Creates a list of PDB records from a string
    #[pyfunction]
    fn pdbreader(contents: &str) -> Vec<Record> {
        pdbreader_parallel(contents)
    }

    #[pyfunction]
    fn version() -> String {
        env!("CARGO_PKG_VERSION").to_string()
    }
}