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
use super::math::MathTag;
use super::model::Model;
use super::tag::Tag;
use super::tag::TagIndex;
use std::collections::HashMap;

#[derive(Clone, Debug, Default)]
pub struct ListOfReactions {
    pub reactions: Vec<TagIndex>,
    pub parent: Option<TagIndex>,
}

#[derive(Debug, Clone, Default)]
pub struct Reaction {
    pub id: Option<String>,
    pub list_of_reactants: Option<TagIndex>,
    pub list_of_products: Option<TagIndex>,
    pub list_of_modifiers: Option<TagIndex>,
    pub reversible: Option<bool>,
    pub kinetic_law: Option<TagIndex>,
    pub compartment: Option<String>,
    pub name: Option<String>,
    pub sbo_term: Option<String>,
    pub parent: Option<TagIndex>,
}

impl Reaction {
    pub fn reactants(&self, model: &Model) -> Vec<SpeciesReference> {
        let mut result = Vec::new();
        if let Some(reactants_idx) = self.list_of_reactants {
            if let Tag::ListOfReactants(list_of_reactants) = &model.nodes[reactants_idx] {
                for reactant_idx in &list_of_reactants.species_references {
                    if let Tag::SpeciesReference(sp_ref) = &model.nodes[reactant_idx.to_owned()] {
                        result.push(sp_ref.clone());
                    }
                }
            }
        }
        result
    }

    pub fn reactant_ids(&self, model: &Model) -> Vec<String> {
        let reactants = &self.reactants(&model);
        reactants
            .iter()
            .map(|r| r.species.as_ref().unwrap().to_owned())
            .collect::<Vec<String>>()
    }

    pub fn products(&self, model: &Model) -> Vec<SpeciesReference> {
        let mut result = Vec::new();
        if let Some(products_idx) = self.list_of_products {
            if let Tag::ListOfProducts(list_of_products) = &model.nodes[products_idx] {
                for products_idx in &list_of_products.species_references {
                    if let Tag::SpeciesReference(sp_ref) = &model.nodes[products_idx.to_owned()] {
                        result.push(sp_ref.clone());
                    }
                }
            }
        }
        result
    }

    pub fn modifiers(&self, model: &Model) -> Vec<String> {
        let mut result: Vec<String> = Vec::new();
        if let Some(lo_modifier_idx) = self.list_of_modifiers {
            if let Tag::ListOfModifiers(list_of_modifiers) = &model.nodes[lo_modifier_idx] {
                for modifier_idx in &list_of_modifiers.modifier_species_references {
                    if let Tag::ModifierSpeciesReference(modifier) =
                        &model.nodes[modifier_idx.to_owned()]
                    {
                        result.push(modifier.species.clone().expect(
                            "Species attribute is mandatory for ModifierSpeciesReferences.",
                        ));
                    }
                }
            }
        }
        result
    }

    pub fn product_ids(&self, model: &Model) -> Vec<String> {
        let products = &self.products(&model);
        products
            .iter()
            .map(|p| p.species.as_ref().unwrap().to_owned())
            .collect::<Vec<String>>()
    }

    pub fn kinetic_law(&self, model: &Model) -> Option<MathTag> {
        let mut result = None;
        if let Some(kinetic_law_idx) = self.kinetic_law {
            if let Tag::KineticLaw(kinetic_law) = &model.nodes[kinetic_law_idx] {
                if let Some(math_tag_idx) = kinetic_law.math {
                    if let Tag::MathTag(math_tag) = &model.nodes[math_tag_idx] {
                        result = Some(math_tag.clone());
                    }
                }
            }
        }
        result
    }

    pub fn local_parameters(&self, model: &Model) -> Vec<LocalParameter> {
        let mut result: Vec<LocalParameter> = Vec::new();
        if let Some(kinetic_law_idx) = self.kinetic_law {
            if let Tag::KineticLaw(kinetic_law) = &model.nodes[kinetic_law_idx] {
                result = kinetic_law.local_parameters(model);
            }
        }
        result
    }

    pub fn local_parameter_values(&self, model: &Model) -> HashMap<String, f64> {
        let mut result = HashMap::new();
        if let Some(kinetic_law_idx) = self.kinetic_law {
            if let Tag::KineticLaw(kinetic_law) = &model.nodes[kinetic_law_idx] {
                result = kinetic_law.local_parameter_values(model);
            }
        }
        result
    }
}

#[derive(Clone, Debug, Default)]
pub struct ListOfReactants {
    pub species_references: Vec<TagIndex>,
    pub parent: Option<TagIndex>,
}

#[derive(Clone, Debug, Default)]
pub struct ListOfProducts {
    pub species_references: Vec<TagIndex>,
    pub parent: Option<TagIndex>,
}

#[derive(Clone, Debug, Default)]
pub struct SpeciesReference {
    pub id: Option<String>,
    pub name: Option<String>,
    pub species: Option<String>,
    pub constant: Option<bool>,
    pub sbo_term: Option<String>,
    pub stoichiometry: Option<f64>,
    pub parent: Option<TagIndex>,
}

#[derive(Clone, Debug, Default)]
pub struct ListOfModifiers {
    pub modifier_species_references: Vec<TagIndex>,
    pub parent: Option<TagIndex>,
}

#[derive(Clone, Debug, Default)]
pub struct ModifierSpeciesReference {
    pub id: Option<String>,
    pub name: Option<String>,
    pub species: Option<String>,
    pub sbo_term: Option<String>,
    pub parent: Option<TagIndex>,
}

#[derive(Clone, Debug, Default)]
pub struct KineticLaw {
    pub math: Option<TagIndex>,
    pub list_of_local_parameters: Option<TagIndex>,
    pub parent: Option<TagIndex>,
    pub sbo_term: Option<String>,
}

impl KineticLaw {
    pub fn local_parameters(&self, model: &Model) -> Vec<LocalParameter> {
        let mut result = Vec::<LocalParameter>::new();
        if let Some(idx) = self.list_of_local_parameters {
            if let Tag::ListOfLocalParameters(list_of_local_parameters) = &model.nodes[idx] {
                for param_idx in &list_of_local_parameters.local_parameters {
                    if let Tag::LocalParameter(param) = &model.nodes[param_idx.to_owned()] {
                        result.push(param.clone());
                    }
                }
            }
        }
        result
    }

    pub fn local_parameter_values(&self, model: &Model) -> HashMap<String, f64> {
        let mut hm = HashMap::<String, f64>::new();
        for param in self.local_parameters(model) {
            if let Some(id) = param.id.to_owned() {
                if let Some(value) = param.value {
                    hm.insert(id, value);
                }
            }
        }
        hm
    }
}

#[derive(Clone, Debug, Default)]
pub struct ListOfLocalParameters {
    pub local_parameters: Vec<TagIndex>,
    pub parent: Option<TagIndex>,
}

#[derive(Clone, Debug, Default)]
pub struct LocalParameter {
    pub id: Option<String>,
    pub value: Option<f64>,
    pub units: Option<String>,
    pub sbo_term: Option<String>,
    pub parent: Option<TagIndex>,
}

#[derive(Debug)]
// used in a reaction matrix
// specifies whether a particular species
// is a reactant or a product in a particular reaction
// along with its stoichiometry
pub enum SpeciesStatus {
    Reactant(f64),
    Product(f64),
    None,
}

impl Default for SpeciesStatus {
    fn default() -> Self {
        SpeciesStatus::None
    }
}