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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
use std::collections::HashMap;

use crate::{
    AssignmentRule, Compartment, FunctionDefinition, InitialAssignment, MathNode, MathTag,
    Parameter, RateRule, Reaction, Species, SpeciesReference, SpeciesStatus, Tag, UnitDefinition,
};

// An SBML Model container
#[derive(Clone, Debug, Default)]
pub struct Model {
    pub id: Option<String>,
    pub name: Option<String>,
    pub meta_id: Option<String>,
    pub substance_units: Option<String>,
    pub time_units: Option<String>,
    pub volume_units: Option<String>,
    pub area_units: Option<String>,
    pub length_units: Option<String>,
    pub extent_units: Option<String>,
    pub conversion_factor: Option<String>,
    pub nodes: Vec<Tag>,
}

macro_rules! objects_from_list {
    ($parent_type: ident, $parent_field: ident, $node_type: ident, $node_field: ident) => {
        pub fn $node_field(&self) -> Vec<$node_type> {
            let mut result = Vec::new();
            let mut list_idx = None;
            if let Tag::Root(root) = &self.nodes[0] {
                list_idx = root.$parent_field;
            }
            if let Some(idx) = list_idx {
                if let Tag::$parent_type(list_tag) = &self.nodes[idx] {
                    for node_idx in &list_tag.$node_field {
                        if let Tag::$node_type(node) = &self.nodes[node_idx.to_owned()] {
                            result.push(node.clone());
                        }
                    }
                }
            }
            result
        }
    };
}

impl Model {
    pub fn new(nodes: Vec<Tag>, attributes: HashMap<String, String>) -> Self {
        let mut model = Model {
            id: None,
            name: None,
            meta_id: None,
            substance_units: None,
            volume_units: None,
            time_units: None,
            extent_units: None,
            area_units: None,
            length_units: None,
            conversion_factor: None,
            nodes,
        };
        for (key, value) in attributes {
            match key.as_str() {
                "id" => model.id = Some(value),
                "name" => model.name = Some(value),
                "metaid" => model.meta_id = Some(value),
                "substanceUnits" => model.substance_units = Some(value),
                "timeUnits" => model.time_units = Some(value),
                "areaUnits" => model.area_units = Some(value),
                "lengthUnits" => model.length_units = Some(value),
                "extentUnits" => model.extent_units = Some(value),
                "volumeUnits" => model.volume_units = Some(value),
                "conversion_factor" => model.conversion_factor = Some(value),
                _ => panic!("Invalid attribute {} for model.", key),
            }
        }
        model
    }

    objects_from_list!(ListOfSpecies, list_of_species, Species, species);
    objects_from_list!(ListOfReactions, list_of_reactions, Reaction, reactions);
    objects_from_list!(
        ListOfUnitDefinitions,
        list_of_unit_definitions,
        UnitDefinition,
        unit_definitions
    );
    objects_from_list!(
        ListOfCompartments,
        list_of_compartments,
        Compartment,
        compartments
    );
    objects_from_list!(ListOfParameters, list_of_parameters, Parameter, parameters);
    objects_from_list!(
        ListOfFunctionDefinitions,
        list_of_function_definitions,
        FunctionDefinition,
        function_definitions
    );
    objects_from_list!(ListOfRules, list_of_rules, AssignmentRule, assignment_rules);
    objects_from_list!(ListOfRules, list_of_rules, RateRule, rate_rules);
    objects_from_list!(
        ListOfInitialAssignments,
        list_of_initial_assignments,
        InitialAssignment,
        initial_assignments
    );

    pub fn function_definition_math(&self) -> HashMap<String, Vec<MathNode>> {
        let mut tags = HashMap::new();
        for function_definition in self.function_definitions() {
            let id = function_definition.id.as_ref().unwrap().to_owned();
            let math_tag = function_definition.math_tag(&self).unwrap();
            tags.insert(id, math_tag.nodes);
        }
        tags
    }

    pub fn assignment_rule_math(&self) -> HashMap<String, Vec<MathNode>> {
        let mut tags = HashMap::new();
        for assignment_rule in self.assignment_rules() {
            let variable = assignment_rule.variable.as_ref().unwrap().to_owned();
            let math_tag = assignment_rule.math_tag(&self).unwrap();
            tags.insert(variable, math_tag.nodes);
        }
        tags
    }

    //Creates a HashMap from Parameters, Species and Compartments
    //pub fn bindings(&self) -> HashMap<String, BindingValue> {
    //let mut bindings = HashMap::<String, BindingValue>::new();

    //Compartments
    //let mut lo_comp_idx = None;
    //if let Tag::Root(root) = &self.nodes[0] {
    //lo_comp_idx = root.list_of_compartments;
    //}
    //if let Some(idx) = lo_comp_idx {
    //if let Tag::ListOfCompartments(list_of_compartments) = &self.nodes[idx] {
    //for comp_idx in &list_of_compartments.compartments {
    //if let Tag::Compartment(comp) = &self.nodes[comp_idx.to_owned()] {
    //if let Some(id) = comp.id.to_owned() {
    //let mut binding_value = BindingValue::new(BindingType::Compartment);
    //if let Some(size) = comp.size {
    //binding_value.set(size);
    //}
    //hm.insert(id, binding_value);
    //}
    //}
    //}
    //}
    //}

    //Species
    //let mut lo_sp_idx = None;
    //if let Tag::Root(root) = &self.nodes[0] {
    //lo_sp_idx = root.list_of_species;
    //}
    //if let Some(idx) = lo_sp_idx {
    //if let Tag::ListOfSpecies(list_of_species) = &self.nodes[idx] {
    //for sp_idx in &list_of_species.species {
    //if let Tag::Species(sp) = &self.nodes[sp_idx.to_owned()] {
    //if let Some(id) = sp.id.to_owned() {
    //let mut binding_value = BindingValue::new(BindingType::SpeciesConc);
    //A species can only have one of initial_amount and
    //initial_concentration, "setting both is an error"
    //Store whatever is set here, don't convert to conc
    //Conversion is left to the simulator because compartment
    //size can change
    //if let Some(initial_amount) = sp.initial_amount {
    //if sp.initial_concentration.is_none() {
    //binding_value = BindingValue::new(BindingType::SpeciesAmt);
    //binding_value.set(initial_amount);
    //}
    //} else if let Some(initial_concentration) = sp.initial_concentration {
    //binding_value = BindingValue::new(BindingType::SpeciesConc);
    //binding_value.set(initial_concentration);
    //}
    //if none of these is set, check the hasOnlySubstanceUnits attr
    //else if let Some(true) = sp.has_only_substance_units {
    //binding_value = BindingValue::new(BindingType::SpeciesAmt);
    //}

    //set constant attribute if appropriate
    //if let Some(true) = sp.constant {
    //binding_value.constant = true;
    //}

    //store value
    //hm.insert(id, binding_value);
    //}
    //}
    //}
    //}
    //}

    //Parameters
    //let mut lo_param_idx = None;
    //if let Tag::Root(root) = &self.nodes[0] {
    //lo_param_idx = root.list_of_parameters;
    //}
    //if let Some(idx) = lo_param_idx {
    //if let Tag::ListOfParameters(list_of_parameters) = &self.nodes[idx] {
    //for param_idx in &list_of_parameters.parameters {
    //if let Tag::Parameter(param) = &self.nodes[param_idx.to_owned()] {
    //if let Some(id) = param.id.to_owned() {
    //let mut binding_value = BindingValue::new(BindingType::Parameter);
    //if let Some(value) = param.value {
    //binding_value.set(value);
    //}
    //hm.insert(id, binding_value);
    //}
    //}
    //}
    //}
    //}

    //let function_definitions = self.function_definition_math();

    //Get values from hm
    //let mut hm_values = HashMap::new();
    //for (id, binding_value) in &hm {
    //if let Some(value) = binding_value.value {
    //hm_values.insert(id.clone(), value);
    //}
    //}

    //Initial Assignments
    //let mut lo_init_assignment_idx = None;
    //if let Tag::Root(root) = &self.nodes[0] {
    //lo_init_assignment_idx = root.list_of_initial_assignments;
    //}
    //if let Some(idx) = lo_init_assignment_idx {
    //if let Tag::ListOfInitialAssignments(list_of_initial_assignments) = &self.nodes[idx] {
    //for init_assignment_idx in &list_of_initial_assignments.initial_assignments {
    //if let Tag::InitialAssignment(init_assignment) =
    //&self.nodes[init_assignment_idx.to_owned()]
    //{
    //if let Some(symbol) = init_assignment.symbol.to_owned() {
    //if let Some(math_tag) = init_assignment.math_tag(self) {
    //let value = evaluate_node(
    //&math_tag.nodes,
    //0,
    //&hm_values,
    //&function_definitions,
    //)
    //.expect("Evaluation failed for initial assignment.");
    //update values in HM
    //hm_values.entry(symbol.clone()).and_modify(|v| *v = value);
    //hm.entry(symbol).and_modify(|v| v.value = Some(value));
    //}
    //}
    //}
    //}
    //}
    //}

    //hm
    //}

    pub fn all_reactants(&self) -> HashMap<String, Vec<SpeciesReference>> {
        let mut result = HashMap::new();
        let reactions = self.reactions();
        for reaction in reactions {
            let rxn_id = reaction.id.as_ref().unwrap().to_owned();
            result.insert(rxn_id, reaction.reactants(&self));
        }

        result
    }

    pub fn all_reactant_ids(&self) -> HashMap<String, Vec<String>> {
        let mut result = HashMap::new();
        let reactions = self.reactions();
        for reaction in reactions {
            let rxn_id = reaction.id.as_ref().unwrap().to_owned();
            result.insert(rxn_id, reaction.reactant_ids(&self));
        }

        result
    }

    pub fn all_products(&self) -> HashMap<String, Vec<SpeciesReference>> {
        let mut result = HashMap::new();
        let reactions = self.reactions();
        for reaction in reactions {
            let rxn_id = reaction.id.as_ref().unwrap().to_owned();
            result.insert(rxn_id, reaction.products(&self));
        }

        result
    }

    pub fn all_product_ids(&self) -> HashMap<String, Vec<String>> {
        let mut result = HashMap::new();
        let reactions = self.reactions();
        for reaction in reactions {
            let rxn_id = reaction.id.as_ref().unwrap().to_owned();
            result.insert(rxn_id, reaction.product_ids(&self));
        }

        result
    }

    pub fn all_kinetic_laws(&self) -> HashMap<String, MathTag> {
        let mut result = HashMap::new();
        let reactions = self.reactions();
        for reaction in reactions {
            let rxn_id = reaction.id.as_ref().unwrap().to_owned();
            result.insert(rxn_id, reaction.kinetic_law(&self).unwrap());
        }

        result
    }

    pub fn local_parameter_values(&self) -> HashMap<String, HashMap<String, f64>> {
        let mut hm: HashMap<String, HashMap<String, f64>> = HashMap::new();
        for reaction in self.reactions() {
            let rxn_id = reaction.id.as_ref().unwrap().to_owned();
            let local_parameters = reaction.local_parameter_values(self);
            hm.insert(rxn_id, local_parameters);
        }
        hm
    }

    pub fn reaction_matrix(&self) -> HashMap<(String, String), Vec<SpeciesStatus>> {
        let mut rxn_matrix: HashMap<(String, String), Vec<SpeciesStatus>> = HashMap::new();

        let species = &self.species();
        let reactions = &self.reactions();
        let all_reactants = &self.all_reactants();
        let all_products = &self.all_products();

        for sp in species {
            let sp_id = sp.id.as_ref().unwrap().to_owned();
            for reaction in reactions {
                let rxn_id = reaction.id.as_ref().unwrap().to_owned();
                let reactants = all_reactants.get(&rxn_id).unwrap();
                let products = all_products.get(&rxn_id).unwrap();

                rxn_matrix.insert((sp_id.clone(), rxn_id.clone()), Vec::new());

                for reactant in reactants {
                    if sp.id == reactant.species {
                        let stoichiometry = reactant.stoichiometry.unwrap();
                        rxn_matrix
                            .entry((sp_id.clone(), rxn_id.clone()))
                            .and_modify(|v| v.push(SpeciesStatus::Reactant(stoichiometry)));
                    }
                }

                for product in products {
                    if sp.id == product.species {
                        let stoichiometry = product.stoichiometry.unwrap();
                        rxn_matrix
                            .entry((sp_id.clone(), rxn_id.clone()))
                            .and_modify(|v| v.push(SpeciesStatus::Product(stoichiometry)));
                    }
                }
            }
        }

        rxn_matrix
    }
}