Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 99 additions & 4 deletions src/sbml/conversion/SBMLRateRuleConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,10 +414,15 @@ SBMLRateRuleConverter::isDocumentAppropriate(OperationReturnValues_t& returnValu

if (mModel->getNumCompartments() > 1)
{
mDocument->getErrorLog()->logError(ModelContainsMultipleCompartments, mDocument->getLevel(),
mDocument->getVersion(), "There are multiple compartments.");
returnValue = LIBSBML_OPERATION_FAILED;
return false;
if (speciesFromMultipleCompartmentsInSameRateRule())
{
mDocument->getErrorLog()->logError(ModelContainsMultipleCompartments, mDocument->getLevel(),
mDocument->getVersion(), "There are multiple compartments with species in the same rate rule.");
returnValue = LIBSBML_OPERATION_FAILED;
return false;
}
returnValue = LIBSBML_OPERATION_SUCCESS;
return true;
}

// 3. the document is invalid
Expand All @@ -434,6 +439,96 @@ SBMLRateRuleConverter::isDocumentAppropriate(OperationReturnValues_t& returnValu

/** @cond doxygenIgnored */

bool
SBMLRateRuleConverter::speciesFromMultipleCompartmentsInSameRateRule()
{
listPairString compartmentSpeciesPairs = getCompartmentSpeciesPairs();
listPairString variablesRateRulePairs = getVariablesRateRulePairs();
for (listPairStringIt it_c = compartmentSpeciesPairs.begin();
it_c != compartmentSpeciesPairs.end(); ++it_c)
{
for (listPairStringIt it_r = variablesRateRulePairs.begin();
it_r != variablesRateRulePairs.end(); ++it_r)
{
if (it_c->second == it_r->second)
{
// species from compartment it_c->first is a participant in a rule it_r
std::string ruleVar = it_r->first;
std::string compartmentId = it_c->first;

// check that the variable for this rule is in a same compartment
for (listPairStringIt it_c1 = compartmentSpeciesPairs.begin();
it_c1 != compartmentSpeciesPairs.end(); ++it_c1)
{
if (it_c1 == it_c)
{
// skip the pair we have already considered
continue;
}
if (it_c1->second == ruleVar)
{
if (it_c1->first != compartmentId)
{
// variable in different compartment
return true;
}
}
}
}
}
}
return false;
}

listPairString
SBMLRateRuleConverter::getCompartmentSpeciesPairs()
{
listPairString compartmentSpeciesPairs;
for (unsigned int n = 0; n < mDocument->getModel()->getNumCompartments(); n++)
{
Compartment* comp = mDocument->getModel()->getCompartment(n);
std::string compId = comp->getId();
for (unsigned int m = 0; m < mDocument->getModel()->getNumSpecies(); m++)
{
Species* spec = mDocument->getModel()->getSpecies(m);
if (spec->getCompartment() != compId)
{
continue;
}
pairString pair(comp->getId(), spec->getId());
compartmentSpeciesPairs.push_back(pair);
}
}

return compartmentSpeciesPairs;
}

listPairString
SBMLRateRuleConverter::getVariablesRateRulePairs()
{
listPairString variablesRateRulePairs;
for (unsigned int n = 0; n < mDocument->getModel()->getNumRules(); n++)
{
Rule* rule = mDocument->getModel()->getRule(n);
if (rule->getType() != RULE_TYPE_RATE)
{
continue;
}
std::string varId = rule->getVariable();
const ASTNode* math = rule->getMath();
List* variables = math->getListOfNodes(ASTNode_isName);
for (ListIterator it = variables->begin(); it != variables->end(); ++it)
{
ASTNode* m = static_cast<ASTNode*>(*it);
std::string mId = m->getName();
pairString pair(varId, mId);
variablesRateRulePairs.push_back(pair);
}
}
return variablesRateRulePairs;
}


void
SBMLRateRuleConverter::addODEPair(std::string id, Model* model)
{
Expand Down
8 changes: 8 additions & 0 deletions src/sbml/conversion/SBMLRateRuleConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ typedef std::vector<pairCoeff > setCoeff;
typedef std::vector<std::pair<ASTNode*, std::vector<double> > >::iterator setCoeffIt;

typedef std::pair<std::string, std::string > pairString;
typedef std::vector< pairString > listPairString;
typedef std::vector< pairString >::iterator listPairStringIt;
typedef std::vector< std::vector<double> > setRnCoeffs;


Expand Down Expand Up @@ -227,6 +229,12 @@ class LIBSBML_EXTERN SBMLRateRuleConverter : public SBMLConverter

bool useStoichiometryFromMath();

// functions to deal with multiple compartments
bool speciesFromMultipleCompartmentsInSameRateRule();

listPairString getCompartmentSpeciesPairs();

listPairString getVariablesRateRulePairs();

// functions for populateODEinfo()

Expand Down
65 changes: 62 additions & 3 deletions src/sbml/conversion/test/TestSBMLRateRuleConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1113,7 +1113,7 @@ START_TEST(test_converter_errors_3)
delete d;
}
END_TEST
START_TEST(test_converter_errors_4)
START_TEST(test_converter_errors_4)
{
std::string raterule_file(TestDataDirectory);
raterule_file += "rn_rr_fail_90104.xml";
Expand All @@ -1134,6 +1134,62 @@ START_TEST(test_converter_errors_4)
delete d;
}
END_TEST
START_TEST(test_converter_errors_5)
{
std::string raterule_file(TestDataDirectory);
raterule_file += "rn_rr_pass_90104.xml";

SBMLDocument* d = readSBMLFromFile(raterule_file.c_str());

rule_rn_props.addOption("useStoichiometryFromMath", true);
rule_rn_converter->setProperties(&rule_rn_props);


rule_rn_converter->setDocument(d);
int ret = rule_rn_converter->convert();
fail_unless(ret == LIBSBML_OPERATION_SUCCESS);
fail_unless(d->getNumErrors() == 0);

delete d;
}
END_TEST

START_TEST(test_converter_errors_6)
{
std::string raterule_file(TestDataDirectory);
raterule_file += "rn_rr_fail_90104_1.xml";

SBMLDocument* d = readSBMLFromFile(raterule_file.c_str());

rule_rn_props.addOption("useStoichiometryFromMath", true);
rule_rn_converter->setProperties(&rule_rn_props);


rule_rn_converter->setDocument(d);
int ret = rule_rn_converter->convert();
fail_unless(ret == LIBSBML_OPERATION_FAILED);
fail_unless(d->getNumErrors() == 1);
fail_unless(d->getError(0)->getErrorId() == 90104);
fail_unless(d->getError(0)->getCategory() == LIBSBML_CAT_RATE_RULE_CONVERSION);

delete d;
}
END_TEST


START_TEST(test_rule_reaction_multi_compartment)
{
std::string raterule_file(TestDataDirectory);
raterule_file += "rr_rn_multi_compartments.xml";

std::string reaction_file(TestDataDirectory);
reaction_file += "rr_rn_multi_compartments_reactions.xml";

bool result = test_rule_to_reaction(raterule_file, reaction_file);

fail_unless(result == true);
}
END_TEST

Suite *
create_suite_TestSBMLRateRuleConverter (void)
Expand All @@ -1143,10 +1199,10 @@ Suite *suite = suite_create("SBMLRateRuleConverter");
TCase *tcase = tcase_create("SBMLRateRuleConverter");
tcase_add_checked_fixture(tcase, RateRuleConverter_setup,
RateRuleConverter_teardown);

if (testing)
{
tcase_add_test(tcase, test_rule_reaction_07);
tcase_add_test(tcase, test_converter_errors_6);
}
else
{
Expand Down Expand Up @@ -1184,6 +1240,9 @@ Suite *suite = suite_create("SBMLRateRuleConverter");
tcase_add_test(tcase, test_converter_errors_2);
tcase_add_test(tcase, test_converter_errors_3);
tcase_add_test(tcase, test_converter_errors_4);
tcase_add_test(tcase, test_converter_errors_5);
tcase_add_test(tcase, test_converter_errors_6);
tcase_add_test(tcase, test_rule_reaction_multi_compartment);



Expand Down
28 changes: 10 additions & 18 deletions src/sbml/conversion/test/test-data/rn_rr_fail_90104.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,21 @@
<compartment id="compartmentTwo" spatialDimensions="3" size="1" constant="true"/>
</listOfCompartments>
<listOfSpecies>
<species id="y_1" name="y_1" compartment="compartmentOne" initialConcentration="2" hasOnlySubstanceUnits="false" boundaryCondition="false" constant="false"/>
<species id="y_1" name="y_1" compartment="compartmentOne" initialConcentration="0" hasOnlySubstanceUnits="false" boundaryCondition="false" constant="false"/>
<species id="y_2" name="y_2" compartment="compartmentTwo" initialConcentration="0" hasOnlySubstanceUnits="false" boundaryCondition="false" constant="false"/>
<species id="y_3" compartment="compartmentTwo" initialConcentration="0" hasOnlySubstanceUnits="false" boundaryCondition="false" constant="false"/>
</listOfSpecies>
<listOfParameters>
<parameter id="a" name="a" value="0.5" constant="true"/>
<parameter id="b" name="b" constant="false"/>
<parameter id="t" name="t" value="0" constant="false"/>
</listOfParameters>
<listOfRules>
<rateRule variable="b">
<rateRule variable="y_1">
<math xmlns="http://www.w3.org/1998/Math/MathML">
<apply>
<times/>
<cn> 2 </cn>
<ci> t </ci>
</apply>
<ci> y_3 </ci>
</math>
</rateRule>
<assignmentRule variable="t">
<math xmlns="http://www.w3.org/1998/Math/MathML">
<csymbol encoding="text" definitionURL="http://www.sbml.org/sbml/symbols/time"> time </csymbol>
<rateRule variable="y_2">
<math xmlns="http://www.w3.org/1998/Math/MathML">
<cn> 22 </cn>
</math>
</assignmentRule>
</listOfRules>

</rateRule>
</listOfRules>
</model>
</sbml>
25 changes: 25 additions & 0 deletions src/sbml/conversion/test/test-data/rn_rr_fail_90104_1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<sbml xmlns="http://www.sbml.org/sbml/level3/version1/core" level="3" version="1">
<model>
<listOfCompartments>
<compartment id="compartmentOne" spatialDimensions="3" size="1" constant="true"/>
<compartment id="compartmentTwo" spatialDimensions="3" size="1" constant="true"/>
</listOfCompartments>
<listOfSpecies>
<species id="y_1" name="y_1" compartment="compartmentOne" initialConcentration="0" hasOnlySubstanceUnits="false" boundaryCondition="false" constant="false"/>
<species id="y_2" name="y_2" compartment="compartmentOne" initialConcentration="0" hasOnlySubstanceUnits="false" boundaryCondition="false" constant="false"/>
<species id="y_3" compartment="compartmentTwo" initialConcentration="0" hasOnlySubstanceUnits="false" boundaryCondition="false" constant="false"/>
</listOfSpecies>
<listOfRules>
<rateRule variable="y_1">
<math xmlns="http://www.w3.org/1998/Math/MathML">
<apply>
<plus/>
<ci> y_2 </ci>
<ci> y_3 </ci>
</apply>
</math>
</rateRule>
</listOfRules>
</model>
</sbml>
34 changes: 34 additions & 0 deletions src/sbml/conversion/test/test-data/rn_rr_pass_90104.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<sbml xmlns="http://www.sbml.org/sbml/level3/version1/core" level="3" version="1">
<model>
<listOfCompartments>
<compartment id="compartmentOne" spatialDimensions="3" size="1" constant="true"/>
<compartment id="compartmentTwo" spatialDimensions="3" size="1" constant="true"/>
</listOfCompartments>
<listOfSpecies>
<species id="y_1" name="y_1" compartment="compartmentOne" initialConcentration="2" hasOnlySubstanceUnits="false" boundaryCondition="false" constant="false"/>
</listOfSpecies>
<listOfParameters>
<parameter id="a" name="a" value="0.5" constant="true"/>
<parameter id="b" name="b" constant="false"/>
<parameter id="t" name="t" value="0" constant="false"/>
</listOfParameters>
<listOfRules>
<rateRule variable="b">
<math xmlns="http://www.w3.org/1998/Math/MathML">
<apply>
<times/>
<cn> 2 </cn>
<ci> t </ci>
</apply>
</math>
</rateRule>
<assignmentRule variable="t">
<math xmlns="http://www.w3.org/1998/Math/MathML">
<csymbol encoding="text" definitionURL="http://www.sbml.org/sbml/symbols/time"> time </csymbol>
</math>
</assignmentRule>
</listOfRules>

</model>
</sbml>
25 changes: 25 additions & 0 deletions src/sbml/conversion/test/test-data/rr_rn_multi_compartments.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<sbml xmlns="http://www.sbml.org/sbml/level3/version1/core" level="3" version="1">
<model>
<listOfCompartments>
<compartment id="compartmentOne" spatialDimensions="3" size="1" constant="true"/>
<compartment id="compartmentTwo" spatialDimensions="3" size="1" constant="true"/>
</listOfCompartments>
<listOfSpecies>
<species id="y_1" name="y_1" compartment="compartmentOne" initialConcentration="0" hasOnlySubstanceUnits="false" boundaryCondition="false" constant="false"/>
<species id="y_2" name="y_2" compartment="compartmentTwo" initialConcentration="0" hasOnlySubstanceUnits="false" boundaryCondition="false" constant="false"/>
</listOfSpecies>
<listOfRules>
<rateRule variable="y_1">
<math xmlns="http://www.w3.org/1998/Math/MathML">
<cn> 11 </cn>
</math>
</rateRule>
<rateRule variable="y_2">
<math xmlns="http://www.w3.org/1998/Math/MathML">
<cn> 22 </cn>
</math>
</rateRule>
</listOfRules>
</model>
</sbml>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<sbml xmlns="http://www.sbml.org/sbml/level3/version1/core" level="3" version="1">
<model>
<listOfCompartments>
<compartment id="compartmentOne" spatialDimensions="3" size="1" constant="true"/>
<compartment id="compartmentTwo" spatialDimensions="3" size="1" constant="true"/>
</listOfCompartments>
<listOfSpecies>
<species id="y_1" name="y_1" compartment="compartmentOne" initialConcentration="0" hasOnlySubstanceUnits="false" boundaryCondition="false" constant="false"/>
<species id="y_2" name="y_2" compartment="compartmentTwo" initialConcentration="0" hasOnlySubstanceUnits="false" boundaryCondition="false" constant="false"/>
</listOfSpecies>
<listOfReactions>
<reaction id="J1" reversible="false" fast="false">
<listOfProducts>
<speciesReference species="y_1" stoichiometry="1" constant="true"/>
</listOfProducts>
<kineticLaw>
<math xmlns="http://www.w3.org/1998/Math/MathML">
<cn> 11 </cn>
</math>
</kineticLaw>
</reaction>
<reaction id="J2" reversible="false" fast="false">
<listOfProducts>
<speciesReference species="y_2" stoichiometry="1" constant="true"/>
</listOfProducts>
<kineticLaw>
<math xmlns="http://www.w3.org/1998/Math/MathML">
<cn> 22 </cn>
</math>
</kineticLaw>
</reaction>
</listOfReactions>
</model>
</sbml>
Loading