|
| 1 | +#[cfg(test)] |
| 2 | +mod tests { |
| 3 | + use sbml::prelude::*; |
| 4 | + |
| 5 | + #[test] |
| 6 | + fn test_sbmldoc_write_and_read() { |
| 7 | + // Arrange |
| 8 | + let doc = create_doc(); |
| 9 | + |
| 10 | + // Act |
| 11 | + let xml_string = doc.to_xml_string(); |
| 12 | + let doc2 = SBMLReader::from_xml_string(&xml_string); |
| 13 | + let xml_string2 = doc2.to_xml_string(); |
| 14 | + |
| 15 | + // Assert |
| 16 | + assert_eq!(xml_string, xml_string2); |
| 17 | + } |
| 18 | + |
| 19 | + fn create_doc() -> SBMLDocument<'static> { |
| 20 | + let doc = SBMLDocument::default(); |
| 21 | + let model = doc.create_model("test_model"); |
| 22 | + |
| 23 | + // Add a unit definition |
| 24 | + let ml = model |
| 25 | + .build_unit_definition("ml", "milliliter") |
| 26 | + .unit(UnitKind::Litre, Some(1), Some(-3), None, None) |
| 27 | + .build(); |
| 28 | + |
| 29 | + let mole_l = model |
| 30 | + .build_unit_definition("mole", "mole") |
| 31 | + .unit(UnitKind::Mole, Some(1), Some(0), Some(1.0), None) |
| 32 | + .unit(UnitKind::Litre, Some(-1), Some(0), Some(1.0), None) |
| 33 | + .build(); |
| 34 | + |
| 35 | + let kelvin = model |
| 36 | + .build_unit_definition("kelvin", "kelvin") |
| 37 | + .unit(UnitKind::Kelvin, Some(1), Some(0), Some(1.0), None) |
| 38 | + .build(); |
| 39 | + |
| 40 | + // Add a compartment |
| 41 | + let compartment = model |
| 42 | + .build_compartment("compartment") |
| 43 | + .name("compartment") |
| 44 | + .constant(true) |
| 45 | + .unit(&ml) |
| 46 | + .volume(1.0) |
| 47 | + .build(); |
| 48 | + |
| 49 | + // Add a species |
| 50 | + let substrate = model |
| 51 | + .build_species("species") |
| 52 | + .name("species") |
| 53 | + .constant(false) |
| 54 | + .has_only_substance_units(false) |
| 55 | + .compartment(&compartment) |
| 56 | + .initial_concentration(1.0) |
| 57 | + .unit(&mole_l) |
| 58 | + .build(); |
| 59 | + |
| 60 | + let product = model |
| 61 | + .build_species("product") |
| 62 | + .name("product") |
| 63 | + .constant(false) |
| 64 | + .has_only_substance_units(false) |
| 65 | + .compartment(&compartment) |
| 66 | + .initial_concentration(1.0) |
| 67 | + .unit(&mole_l) |
| 68 | + .build(); |
| 69 | + |
| 70 | + // Add a reaction |
| 71 | + let reaction = model |
| 72 | + .build_reaction("reaction") |
| 73 | + .name("reaction") |
| 74 | + .reactant(&substrate, 1.0) |
| 75 | + .product(&product, 1.0) |
| 76 | + .build(); |
| 77 | + |
| 78 | + // Add a kinetic law |
| 79 | + let kinetic_law = reaction.create_kinetic_law("substrate * kcat / (substrate + Km)"); |
| 80 | + |
| 81 | + // Add local parameters |
| 82 | + kinetic_law |
| 83 | + .build_local_parameter("kcat") |
| 84 | + .units(&mole_l.id()) |
| 85 | + .value(1.0) |
| 86 | + .build(); |
| 87 | + |
| 88 | + kinetic_law |
| 89 | + .build_local_parameter("Km") |
| 90 | + .units(&mole_l.id()) |
| 91 | + .value(1.0) |
| 92 | + .build(); |
| 93 | + |
| 94 | + // Add a parameter (global) |
| 95 | + model |
| 96 | + .build_parameter("T") |
| 97 | + .units(&kelvin) |
| 98 | + .value(310.0) |
| 99 | + .build(); |
| 100 | + |
| 101 | + model |
| 102 | + .build_parameter("Km") |
| 103 | + .units(&mole_l) |
| 104 | + .value(1.0) |
| 105 | + .build(); |
| 106 | + |
| 107 | + // Add a rate rule |
| 108 | + model |
| 109 | + .build_rate_rule(&product, "kcat * substrate / (substrate + Km)") |
| 110 | + .build(); |
| 111 | + |
| 112 | + // Add an assignment rule |
| 113 | + model |
| 114 | + .build_assignment_rule("x", "T * kcat * substrate / (T + Km)") |
| 115 | + .build(); |
| 116 | + |
| 117 | + doc |
| 118 | + } |
| 119 | +} |
0 commit comments