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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
mod base_types;
use std::collections::HashMap;

pub use base_types::{
    Compartment, Constraint, InitialAssignment, ModelUnits, Parameter, Reaction, Specie, Unit,
    UnitSId, UnitSidRef,
};

type HL<T> = HashMap<String, T>;
/// Struct that holds the entire SBML document (non-coprehensive)
#[derive(Debug, Default, PartialEq)]
pub struct Model {
    pub model_units: ModelUnits,
    pub initial_assignments: HL<InitialAssignment>,
    pub parameters: HL<Parameter>,
    pub species: HL<Specie>,
    pub reactions: HL<Reaction>,
    pub compartments: HL<Compartment>,
    pub unit_definitions: HL<HashMap<UnitSId, Unit>>,
    pub constraints: Vec<Constraint>,
}

impl Model {
    pub fn get_list_of_compartments(&self) -> Vec<&Compartment> {
        self.compartments.iter().map(|(_key, val)| val).collect()
    }
    /// Emulating the API of libSBML
    ///
    /// # Example
    ///
    /// ```
    /// use rust_sbml::Model;
    /// use std::fs;
    ///
    /// let ecoli = fs::read_to_string("examples/EcoliCore.xml").unwrap();
    /// let document = Model::parse(&ecoli).unwrap();
    /// println!("{:?}", document.get_list_of_compartments())
    /// ```
    pub fn get_list_of_species(&self) -> Vec<&Specie> {
        self.species.iter().map(|(_key, val)| val).collect()
    }
    /// Emulating the API of libSBML
    ///
    /// # Example
    ///
    /// ```
    /// use rust_sbml::Model;
    /// use std::fs;
    ///
    /// let ecoli = fs::read_to_string("examples/EcoliCore.xml").unwrap();
    /// let document = Model::parse(&ecoli).unwrap();
    /// println!("{:?}", document.get_list_of_species())
    /// ```
    pub fn get_list_of_reactions(&self) -> Vec<&Reaction> {
        self.reactions.iter().map(|(_key, val)| val).collect()
    }
    /// Emulating the API of libSBML
    ///
    /// # Example
    ///
    /// ```
    /// use rust_sbml::Model;
    /// use std::fs;
    ///
    /// let ecoli = fs::read_to_string("examples/EcoliCore.xml").unwrap();
    /// let document = Model::parse(&ecoli).unwrap();
    /// println!("{:?}", document.get_list_of_reactions())
    /// ```
    pub fn parse(doc: &str) -> Result<Self, roxmltree::Error> {
        let res = roxmltree::Document::parse(doc)?;
        let raw_model = res
            .descendants()
            .find(|n| n.tag_name().name() == "model")
            .unwrap();

        // Units used by the model itself
        let model_units: ModelUnits = ModelUnits::from(raw_model.attributes());

        // Unit definitions
        let unit_definitions: HashMap<String, HashMap<UnitSId, Unit>> = raw_model
            .descendants()
            .filter(|n| n.tag_name().name() == "unitDefinition")
            .map(|r| {
                (
                    r.attribute("id").unwrap().to_owned(),
                    r.descendants()
                        .filter(|n| n.tag_name().name() == "unit")
                        .map(|r| {
                            (
                                r.attribute("kind")
                                    .map(serde_plain::from_str)
                                    .unwrap()
                                    .unwrap(),
                                Unit::from(r),
                            )
                        })
                        .collect(),
                )
            })
            .collect();
        // Compartments
        let compartments: HashMap<String, Compartment> = raw_model
            .descendants()
            .filter(|n| n.tag_name().name() == "compartment")
            .map(|n| (n.attribute("id").unwrap().to_owned(), Compartment::from(n)))
            .collect();
        // Species
        let species: HashMap<String, Specie> = raw_model
            .descendants()
            .filter(|n| n.tag_name().name() == "species")
            .map(|n| (n.attribute("id").unwrap().to_owned(), Specie::from(n)))
            .collect();
        // Parameters
        let parameters: HashMap<String, Parameter> = raw_model
            .descendants()
            .filter(|n| n.tag_name().name() == "parameter")
            .map(|n| (n.attribute("id").unwrap().to_owned(), Parameter::from(n)))
            .collect();
        // Initial assignments
        let initial_assignments: HashMap<String, InitialAssignment> = raw_model
            .descendants()
            .filter(|n| n.tag_name().name() == "initialAssignment")
            .map(|n| {
                (
                    n.attribute("id").unwrap().to_owned(),
                    InitialAssignment {
                        symbol: n.attribute("symbol").unwrap().to_owned(),
                    },
                )
            })
            .collect();
        // Reactions
        let reactions: HashMap<String, Reaction> = raw_model
            .descendants()
            .filter(|n| n.tag_name().name() == "reaction")
            .map(|n| (n.attribute("id").unwrap().to_owned(), Reaction::from(n)))
            .collect();

        // Constraints
        let constraints: Vec<Constraint> = raw_model
            .descendants()
            .filter(|n| n.tag_name().name() == "constraint")
            .map(|n| Constraint {
                math: n
                    .descendants()
                    .find(|n| n.tag_name().name() == "math")
                    .map(mathml::parse_node),
                message: n
                    .descendants()
                    .find(|n| n.tag_name().name() == "message")
                    .unwrap()
                    .children()
                    .map(|n| n.text().unwrap().trim().to_owned())
                    .collect::<String>(),
            })
            .collect();
        Ok(Model {
            model_units,
            parameters,
            initial_assignments,
            species,
            reactions,
            compartments,
            unit_definitions,
            constraints,
        })
    }
}

pub fn parse_document(doc: &str) -> Result<Model, roxmltree::Error> {
    Model::parse(doc)
}