1 /**
2  * \file    TestReadFromFile3.c
3  * \brief   Reads tests/l1v1-rules.xml into memory and tests it.
4  * \author  Ben Bornstein
5  *
6  * <!--------------------------------------------------------------------------
7  * This file is part of libSBML.  Please visit http://sbml.org for more
8  * information about SBML, and the latest version of libSBML.
9  *
10  * Copyright (C) 2020 jointly by the following organizations:
11  *     1. California Institute of Technology, Pasadena, CA, USA
12  *     2. University of Heidelberg, Heidelberg, Germany
13  *     3. University College London, London, UK
14  *
15  * Copyright (C) 2019 jointly by the following organizations:
16  *     1. California Institute of Technology, Pasadena, CA, USA
17  *     2. University of Heidelberg, Heidelberg, Germany
18  *
19  * Copyright (C) 2013-2018 jointly by the following organizations:
20  *     1. California Institute of Technology, Pasadena, CA, USA
21  *     2. EMBL European Bioinformatics Institute (EMBL-EBI), Hinxton, UK
22  *     3. University of Heidelberg, Heidelberg, Germany
23  *
24  * Copyright (C) 2009-2013 jointly by the following organizations:
25  *     1. California Institute of Technology, Pasadena, CA, USA
26  *     2. EMBL European Bioinformatics Institute (EMBL-EBI), Hinxton, UK
27  *
28  * Copyright (C) 2006-2008 by the California Institute of Technology,
29  *     Pasadena, CA, USA
30  *
31  * Copyright (C) 2002-2005 jointly by the following organizations:
32  *     1. California Institute of Technology, Pasadena, CA, USA
33  *     2. Japan Science and Technology Agency, Japan
34  *
35  * This library is free software; you can redistribute it and/or modify it
36  * under the terms of the GNU Lesser General Public License as published by
37  * the Free Software Foundation.  A copy of the license agreement is provided
38  * in the file named "LICENSE.txt" included with this software distribution
39  * and also available online as http://sbml.org/software/libsbml/license.html
40  * ---------------------------------------------------------------------- -->*/
41 
42 #include <sbml/common/common.h>
43 #include <sbml/SBMLReader.h>
44 #include <sbml/SBMLTypes.h>
45 
46 #include <check.h>
47 
48 
49 
50 #include <sbml/common/extern.h>
51 
52 LIBSBML_CPP_NAMESPACE_USE
53 
54 
55 BEGIN_C_DECLS
56 
57 extern char *TestDataDirectory;
58 
59 
START_TEST(test_read_l1v1_rules)60 START_TEST (test_read_l1v1_rules)
61 {
62   SBMLDocument_t             *d;
63   Model_t                    *m;
64   Compartment_t              *c;
65   KineticLaw_t               *kl;
66   Parameter_t                *p;
67   Rule_t                     *pr;
68   Reaction_t                 *r;
69   Species_t                  *s;
70   Rule_t                     *scr;
71   SpeciesReference_t         *sr;
72   UnitDefinition_t           *ud;
73 
74   char *filename = safe_strcat(TestDataDirectory, "l1v1-rules.xml");
75 
76 
77   d = readSBML(filename);
78 
79   if (d == NULL)
80   {
81     fail("readSBML(\"l1v1-rules.xml\") returned a NULL pointer.");
82   }
83 
84   safe_free(filename);
85 
86 
87   /**
88    * <sbml level="1" version="1" ...>
89    */
90   fail_unless( SBMLDocument_getLevel  (d) == 1, NULL );
91   fail_unless( SBMLDocument_getVersion(d) == 1, NULL );
92 
93 
94   /**
95    * <model>
96    */
97   m = SBMLDocument_getModel(d);
98 
99   /**
100    * <listOfCompartments>
101    *  <compartment name="cell" volume="1"/>
102    * </listOfCompartments>
103    */
104   fail_unless( Model_getNumCompartments(m) == 1, NULL );
105 
106   c = Model_getCompartment(m, 0);
107   fail_unless( !strcmp(Compartment_getName(c), "cell"), NULL );
108   fail_unless( Compartment_getVolume(c) == 1, NULL );
109 
110 
111   /**
112    * <listOfSpecies>
113    *   <specie name="s1" compartment="cell" initialAmount="4"/>
114    *   <specie name="s2" compartment="cell" initialAmount="2"/>
115    *   <specie name="x0" compartment="cell" initialAmount="1"/>
116    *   <specie name="x1" compartment="cell" initialAmount="0"/>
117    *   <specie name="x2" compartment="cell" initialAmount="1"/>
118    *   <specie name="x3" compartment="cell" initialAmount="0"/>
119    * </listOfSpecies>
120    */
121   fail_unless( Model_getNumSpecies(m) == 6, NULL );
122 
123   s = Model_getSpecies(m, 0);
124   fail_unless( !strcmp( Species_getName(s)       , "s1"   ), NULL );
125   fail_unless( !strcmp( Species_getCompartment(s), "cell" ), NULL );
126   fail_unless( Species_getInitialAmount    (s) == 4, NULL );
127   fail_unless( Species_getBoundaryCondition(s) == 0, NULL );
128 
129   s = Model_getSpecies(m, 1);
130   fail_unless( !strcmp( Species_getName(s)       , "s2"   ), NULL );
131   fail_unless( !strcmp( Species_getCompartment(s), "cell" ), NULL );
132   fail_unless( Species_getInitialAmount    (s) == 2, NULL );
133   fail_unless( Species_getBoundaryCondition(s) == 0, NULL );
134 
135   s = Model_getSpecies(m, 2);
136   fail_unless( !strcmp( Species_getName(s)       , "x0"   ), NULL );
137   fail_unless( !strcmp( Species_getCompartment(s), "cell" ), NULL );
138   fail_unless( Species_getInitialAmount    (s) == 1, NULL );
139   fail_unless( Species_getBoundaryCondition(s) == 0, NULL );
140 
141   s = Model_getSpecies(m, 3);
142   fail_unless( !strcmp( Species_getName(s)       , "x1"   ), NULL );
143   fail_unless( !strcmp( Species_getCompartment(s), "cell" ), NULL );
144   fail_unless( Species_getInitialAmount    (s) == 0, NULL );
145   fail_unless( Species_getBoundaryCondition(s) == 0, NULL );
146 
147   s = Model_getSpecies(m, 4);
148   fail_unless( !strcmp( Species_getName(s)       , "x2"   ), NULL );
149   fail_unless( !strcmp( Species_getCompartment(s), "cell" ), NULL );
150   fail_unless( Species_getInitialAmount    (s) == 1, NULL );
151   fail_unless( Species_getBoundaryCondition(s) == 0, NULL );
152 
153   s = Model_getSpecies(m, 5);
154   fail_unless( !strcmp( Species_getName(s)       , "x3"   ), NULL );
155   fail_unless( !strcmp( Species_getCompartment(s), "cell" ), NULL );
156   fail_unless( Species_getInitialAmount    (s) == 0, NULL );
157   fail_unless( Species_getBoundaryCondition(s) == 0, NULL );
158 
159 
160   /**
161    * <listOfParameters>
162    *   <parameter name="k1" value="1.2"/>
163    *   <parameter name="k2" value="1000"/>
164    *   <parameter name="k3" value="3000"/>
165    *   <parameter name="k4" value="4.5"/>
166    * </listOfParameters>
167    */
168   fail_unless( Model_getNumParameters(m) == 7, NULL );
169 
170   p = Model_getParameter(m, 0);
171   fail_unless( !strcmp(Parameter_getName(p), "k1"), NULL );
172   fail_unless( Parameter_getValue(p) == 1.2, NULL );
173 
174   p = Model_getParameter(m, 1);
175   fail_unless( !strcmp(Parameter_getName(p), "k2"), NULL );
176   fail_unless( Parameter_getValue(p) == 1000, NULL );
177 
178   p = Model_getParameter(m, 2);
179   fail_unless( !strcmp(Parameter_getName(p), "k3"), NULL );
180   fail_unless( Parameter_getValue(p) == 3000, NULL );
181 
182   p = Model_getParameter(m, 3);
183   fail_unless( !strcmp(Parameter_getName(p), "k4"), NULL );
184   fail_unless( Parameter_getValue(p) == 4.5, NULL );
185 
186 
187   /**
188    * <listOfRules>
189    *   <parameterRule name="t" formula="s1 + s2"/>
190    *   <parameterRule name="k" formula="k3/k2"/>
191    *   <specieConcentrationRule specie="s2" formula="k * t/(1 + k)"/>
192    *   <specieConcentrationRule specie="s1" formula="t - s2"/>
193    * </listOfRules>
194    */
195   fail_unless( Model_getNumRules(m) == 4, NULL );
196 
197   pr = Model_getRule(m, 0);
198   fail_unless( !strcmp(Rule_getVariable(pr), "t"), NULL );
199   fail_unless( !strcmp(Rule_getFormula( pr), "s1 + s2"), NULL );
200 
201   /**
202    * tests for the unit API functions
203    */
204   ud = Rule_getDerivedUnitDefinition(pr);
205   fail_unless (UnitDefinition_getNumUnits(ud) == 2, NULL);
206   fail_unless( Unit_getKind (UnitDefinition_getUnit(ud, 0)) == UNIT_KIND_MOLE, NULL );
207   fail_unless( Unit_getExponent(UnitDefinition_getUnit(ud, 0)) ==  1, NULL );
208   fail_unless( Unit_getKind (UnitDefinition_getUnit(ud, 1)) == UNIT_KIND_LITRE, NULL );
209   fail_unless( Unit_getExponent(UnitDefinition_getUnit(ud, 1)) ==  -1, NULL );
210 
211   fail_unless( Rule_containsUndeclaredUnits(pr) == 0, NULL);
212 
213   pr = Model_getRule(m, 1);
214   fail_unless( !strcmp(Rule_getVariable(pr), "k"), NULL );
215   fail_unless( !strcmp(Rule_getFormula( pr), "k3/k2"), NULL );
216 
217   /**
218    * tests for the unit API functions
219    */
220   ud = Rule_getDerivedUnitDefinition(pr);
221   fail_unless (UnitDefinition_getNumUnits(ud) == 0, NULL);
222 
223   fail_unless( Rule_containsUndeclaredUnits(pr) == 1, NULL);
224 
225   scr = Model_getRule(m, 2);
226   fail_unless( !strcmp(Rule_getVariable(scr), "x2"), NULL );
227   fail_unless( !strcmp(Rule_getFormula( scr), "k * (s1+s2)/(1 + k)"),
228                NULL );
229 
230   scr = Model_getRule(m, 3);
231   fail_unless( !strcmp(Rule_getVariable(scr), "x3"), NULL );
232   fail_unless( !strcmp(Rule_getFormula((Rule_t *) scr), "p*(t - s2)"), NULL );
233 
234 
235   /**
236    * <listOfReactions>
237    *   <reaction name="j1" > ... </reaction>
238    *   <reaction name="j3" > ... </reaction>
239    * </listOfReactions>
240    */
241   fail_unless( Model_getNumReactions(m) == 2, NULL );
242 
243   r = Model_getReaction(m, 0);
244   fail_unless( !strcmp(Reaction_getName(r), "j1"), NULL );
245   fail_unless( Reaction_getReversible(r) != 0, NULL );
246   fail_unless( Reaction_getFast(r)       == 0, NULL );
247 
248   r = Model_getReaction(m, 1);
249   fail_unless( !strcmp(Reaction_getName(r), "j3"), NULL );
250   fail_unless( Reaction_getReversible(r) != 0, NULL );
251   fail_unless( Reaction_getFast(r)       == 0, NULL );
252 
253 
254   /**
255    * <reaction name="j1">
256    *   <listOfReactants>
257    *     <specieReference specie="x0"/>
258    *   </listOfReactants>
259    *   <listOfProducts>
260    *     <specieReference specie="s1"/>
261    *   </listOfProducts>
262    *   <kineticLaw formula="k1 * x0"/>
263    * </reaction>
264    */
265   r = Model_getReaction(m, 0);
266 
267   fail_unless( Reaction_getNumReactants(r) == 1, NULL );
268   fail_unless( Reaction_getNumProducts(r)  == 1, NULL );
269 
270   sr = Reaction_getReactant(r, 0);
271   fail_unless( !strcmp(SpeciesReference_getSpecies(sr), "x0"), NULL );
272   fail_unless( SpeciesReference_getStoichiometry(sr) == 1, NULL );
273   fail_unless( SpeciesReference_getDenominator  (sr) == 1, NULL );
274 
275   sr = Reaction_getProduct(r, 0);
276   fail_unless( !strcmp(SpeciesReference_getSpecies(sr), "s1"), NULL );
277   fail_unless( SpeciesReference_getStoichiometry(sr) == 1, NULL );
278   fail_unless( SpeciesReference_getDenominator  (sr) == 1, NULL );
279 
280   kl = Reaction_getKineticLaw(r);
281   fail_unless( !strcmp(KineticLaw_getFormula(kl), "k1 * x0"), NULL );
282 
283 
284   /**
285    * <reaction name="j3">
286    *   <listOfReactants>
287    *     <specieReference specie="s2"/>
288    *   </listOfReactants>
289    *   <listOfProducts>
290    *     <specieReference specie="x1"/>
291    *   </listOfProducts>
292    *   <kineticLaw formula="k4 * s2"/>
293    * </reaction>
294    */
295   r = Model_getReaction(m, 1);
296 
297   fail_unless( Reaction_getNumReactants(r) == 1, NULL );
298   fail_unless( Reaction_getNumProducts(r)  == 1, NULL );
299 
300   sr = Reaction_getReactant(r, 0);
301   fail_unless( !strcmp(SpeciesReference_getSpecies(sr), "s2"), NULL );
302   fail_unless( SpeciesReference_getStoichiometry(sr) == 1, NULL );
303   fail_unless( SpeciesReference_getDenominator  (sr) == 1, NULL );
304 
305   sr = Reaction_getProduct(r, 0);
306   fail_unless( !strcmp(SpeciesReference_getSpecies(sr), "x1"), NULL );
307   fail_unless( SpeciesReference_getStoichiometry(sr) == 1, NULL );
308   fail_unless( SpeciesReference_getDenominator  (sr) == 1, NULL );
309 
310   kl = Reaction_getKineticLaw(r);
311   fail_unless( !strcmp(KineticLaw_getFormula(kl), "k4 * s2"), NULL );
312 
313   SBMLDocument_free(d);
314 }
315 END_TEST
316 
317 
318 Suite *
create_suite_TestReadFromFile3(void)319 create_suite_TestReadFromFile3 (void)
320 {
321   Suite *suite = suite_create("test-data/l1v1-rules.xml");
322   TCase *tcase = tcase_create("test-data/l1v1-rules.xml");
323 
324 
325   tcase_add_test(tcase, test_read_l1v1_rules);
326 
327   suite_add_tcase(suite, tcase);
328 
329   return suite;
330 }
331 
332 END_C_DECLS
333 
334 
335