rust_sbml/
pyo.rs

1#![allow(non_snake_case)]
2
3use super::{Compartment, Model, Parameter, Reaction, Species, SpeciesReference};
4use pyo3::prelude::*;
5
6#[pymethods]
7impl Species {
8    #[getter]
9    fn id(&self) -> &str {
10        self.id.as_str()
11    }
12    fn getCompartment(&self) -> &str {
13        self.compartment.as_str()
14    }
15}
16
17#[pymethods]
18impl Compartment {
19    #[getter]
20    fn id(&self) -> &str {
21        self.id.as_str()
22    }
23    #[getter]
24    fn name(&self) -> &str {
25        match &self.name {
26            Some(s) => s,
27            _ => "",
28        }
29    }
30}
31
32#[pymethods]
33impl Reaction {
34    #[getter]
35    fn id(&self) -> &str {
36        self.id.as_str()
37    }
38    #[getter]
39    fn name(&self) -> &str {
40        match &self.name {
41            Some(s) => s,
42            _ => "",
43        }
44    }
45    fn getListOfReactants(&self) -> Vec<SpeciesReference> {
46        self.list_of_reactants.species_references.to_owned()
47    }
48    fn getListOfProducts(&self) -> Vec<SpeciesReference> {
49        self.list_of_products.species_references.to_owned()
50    }
51    fn getLowerFluxBound(&self) -> &str {
52        match &self.lower_bound {
53            Some(s) => s,
54            _ => "",
55        }
56    }
57    fn getUpperFluxBound(&self) -> &str {
58        match &self.upper_bound {
59            Some(s) => s,
60            None => "",
61        }
62    }
63}
64
65#[pymethods]
66impl SpeciesReference {
67    #[getter]
68    fn id(&self) -> &str {
69        self.species.as_str()
70    }
71    fn getStoichiometry(&self) -> Option<f64> {
72        self.stoichiometry
73    }
74}
75
76#[pymethods]
77impl Parameter {
78    fn getValue(&self) -> Option<f64> {
79        self.value
80    }
81    fn getConstant(&self) -> bool {
82        self.constant
83    }
84}
85
86#[pymethods]
87impl Model {
88    #[new]
89    fn new(doc: &str) -> Self {
90        let file_str = std::fs::read_to_string(doc).unwrap();
91        match Model::parse(&file_str) {
92            Ok(m) => m,
93            Err(e) => panic!("kai_sbml Couldn't parse {}. Error: {:?}", doc, e),
94        }
95    }
96    fn getListOfCompartments(&self) -> Vec<Compartment> {
97        self.compartments
98            .iter()
99            .map(|(_, n)| n.to_owned())
100            .collect()
101    }
102    fn getListOfSpecies(&self) -> Vec<Species> {
103        self.species.iter().map(|(_, n)| n.to_owned()).collect()
104    }
105    fn getListOfReactions(&self) -> Vec<Reaction> {
106        self.reactions.iter().map(|(_, n)| n.to_owned()).collect()
107    }
108    fn getParameter(&self, query: String) -> Option<Parameter> {
109        self.parameters.get(&query).cloned()
110    }
111    fn getObjectives(&self) -> Vec<String> {
112        self.objectives.to_owned().unwrap_or_default()
113    }
114    #[getter]
115    fn id(&self) -> Option<String> {
116        self.id.to_owned()
117    }
118    #[getter]
119    fn metaid(&self) -> Option<String> {
120        self.metaid.to_owned()
121    }
122    #[getter]
123    fn name(&self) -> Option<String> {
124        self.name.to_owned()
125    }
126}
127
128#[pymodule]
129fn rust_sbml(_py: Python, m: &PyModule) -> PyResult<()> {
130    m.add_class::<Model>()?;
131    m.add_class::<Reaction>()?;
132    m.add_class::<Species>()?;
133    m.add_class::<SpeciesReference>()?;
134    m.add_class::<Compartment>()?;
135    Ok(())
136}
137
138#[cfg(test)]
139mod tests {
140    #[test]
141    fn it_works() {
142        assert_eq!(2 + 2, 4);
143    }
144}