Skip to content

Commit

Permalink
the refactoring is causing some issues with how the equations become
Browse files Browse the repository at this point in the history
you need to look at my refactoring functions and see if I can avoid situations I don't want
  • Loading branch information
skeating committed Dec 22, 2024
1 parent 30eb874 commit 7ec9f90
Show file tree
Hide file tree
Showing 9 changed files with 240 additions and 45 deletions.
27 changes: 17 additions & 10 deletions src/sbml/conversion/ExpressionAnalyser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ LIBSBML_CPP_NAMESPACE_BEGIN

bool compareExpressions(SubstitutionValues_t* values1, SubstitutionValues_t* values2)
{
if (values1->type >= values2->type)
if (values1->type <= values2->type)
return true;
return false;
}
Expand Down Expand Up @@ -160,9 +160,9 @@ ExpressionAnalyser::setModel(Model* model)
return LIBSBML_OPERATION_SUCCESS;
}

void ExpressionAnalyser::substituteParametersForExpressions(List* hiddenSpecies)
void ExpressionAnalyser::substituteParametersForExpressions()
{
if (hiddenSpecies == NULL || mExpressions.empty())
if (mHiddenSpecies == NULL || mExpressions.empty())
return;

// need to actually address the expressions in the correct order
Expand Down Expand Up @@ -202,7 +202,7 @@ void ExpressionAnalyser::substituteParametersForExpressions(List* hiddenSpecies)
}
}

void ExpressionAnalyser::substituteParameters(List* hiddenSpecies, SubstitutionValues_t* exp)
void ExpressionAnalyser::substituteParameters(SubstitutionValues_t* exp)
{
for (unsigned int j = 0; j < mODEs.size(); j++)
{
Expand All @@ -219,7 +219,7 @@ void ExpressionAnalyser::substituteParameters(List* hiddenSpecies, SubstitutionV
exp->z_value = zName;
mNewVarCount++;
// replaceExpressionWithNewParameter(odeRHS, exp);
// addParametersAndRateRules(hiddenSpecies, exp);
// addParametersAndRateRules(exp);
}
//cout << "ode in main: " << SBML_formulaToL3String(odeRHS) << endl;
}
Expand Down Expand Up @@ -502,6 +502,12 @@ ExpressionAnalyser::printSubstitutionValues(const SubstitutionValues_t* values)
else
cout << "current: NULL" << endl;
cout << "odeIndex: " << values->odeIndex << endl;
cout << "levelInExpression: " << values->levelInExpression << endl;
}

List* ExpressionAnalyser::getHiddenSpecies()
{
return mHiddenSpecies;
}

SubstitutionValues_t* ExpressionAnalyser::createBlankSubstitutionValues()
Expand Down Expand Up @@ -713,7 +719,7 @@ ExpressionAnalyser::analyse(bool minusXPlusYOnly)
value->levelInExpression = currentNode.first;
if (shouldAddExpression(value, currentNode))
{
//printSubstitutionValues(value);
printSubstitutionValues(value);
mExpressions.push_back(value);
}
}
Expand All @@ -728,7 +734,7 @@ void ExpressionAnalyser::orderExpressions()
}

void
ExpressionAnalyser::detectHiddenSpecies(List * hiddenSpecies)
ExpressionAnalyser::detectHiddenSpecies()
{
// find -x+y and replace with y-x
// actually don't have to do this if we decompose the AST
Expand All @@ -748,7 +754,8 @@ ExpressionAnalyser::detectHiddenSpecies(List * hiddenSpecies)
//{
// cout << mODEs[odeIndex].first << ": " << SBML_formulaToL3String(mODEs[odeIndex].second) << endl;
//}
substituteParametersForExpressions(hiddenSpecies);
orderExpressions();
substituteParametersForExpressions();
}

/*
Expand Down Expand Up @@ -809,7 +816,7 @@ ExpressionAnalyser::getUniqueNewParameterName()


void
ExpressionAnalyser::addParametersAndRateRules(List* hiddenSpecies, SubstitutionValues_t* exp)
ExpressionAnalyser::addParametersAndRateRules(SubstitutionValues_t* exp)
{
if (exp->z_value.empty()) return;
//for (unsigned int i = 0; i < mExpressions.size(); i++)
Expand Down Expand Up @@ -892,7 +899,7 @@ ExpressionAnalyser::addParametersAndRateRules(List* hiddenSpecies, SubstitutionV
zParam->setId(exp->z_value);
zParam->setConstant(false);
zParam->setValue(SBMLTransforms::evaluateASTNode(zNode, mModel));
hiddenSpecies->add(zParam);
mHiddenSpecies->add(zParam);

delete zNode;
delete math; //its children dxdt and minus1 deleted as part of this.
Expand Down
11 changes: 6 additions & 5 deletions src/sbml/conversion/ExpressionAnalyser.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,13 @@ class LIBSBML_EXTERN ExpressionAnalyser
*/
int setModel(Model* m);


List* getHiddenSpecies();
SubstitutionValues_t* createBlankSubstitutionValues();
unsigned int getNumExpressions();
SubstitutionValues_t* getExpression(unsigned int index);


void detectHiddenSpecies(List * hiddenSpecies);
void detectHiddenSpecies();

bool analyseNode(ASTNode* node, SubstitutionValues_t* value);

Expand All @@ -174,7 +174,7 @@ class LIBSBML_EXTERN ExpressionAnalyser
void printSubstitutionValues(const SubstitutionValues_t* values1);
bool areIdenticalSubstitutionValues(SubstitutionValues_t* values1, SubstitutionValues_t* values2);

void substituteParametersForExpressions(List* hiddenSpecies);
void substituteParametersForExpressions();

/*
* identify instances of - x + y within formula and create expressions
Expand All @@ -200,7 +200,7 @@ class LIBSBML_EXTERN ExpressionAnalyser
bool matchesType(SubstitutionValues_t* values1, SubstitutionValues_t* values2);


void substituteParameters(List* hiddenSpecies, SubstitutionValues_t* values);
void substituteParameters(SubstitutionValues_t* values);

SubstitutionValues_t* getSubstitutionValuesByType(ExpressionType_t type, size_t index = 0);

Expand All @@ -223,7 +223,7 @@ class LIBSBML_EXTERN ExpressionAnalyser
* ALSO NEED TO TAKE THE MATCHING OF THE ODES OUT OF THE COMPARISON OF EXPRESSIONS
* AND CHECK THAT THE MATCHES VARIABLES INCLUDES ALL THE VARIABLES
*/
void addParametersAndRateRules(List* hiddenSpecies, SubstitutionValues_t* exp = NULL);
void addParametersAndRateRules(SubstitutionValues_t* exp = NULL);

void replaceExpressionInNodeWithNode(ASTNode* node, ASTNode* replaced, ASTNode* replacement);

Expand Down Expand Up @@ -335,3 +335,4 @@ LIBSBML_CPP_NAMESPACE_END
#endif /* !SWIG */
#endif /* ExpressionAnalyser_h */


7 changes: 3 additions & 4 deletions src/sbml/conversion/SBMLRateRuleConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -880,12 +880,11 @@ SBMLRateRuleConverter::populateODEinfo()
ExpressionAnalyser *ea = new ExpressionAnalyser(model, mODEs);


List hiddenSpecies;
ea->detectHiddenSpecies(&hiddenSpecies);
ea->detectHiddenSpecies();
// add all hidden species to the model
for (unsigned int hs=0; hs < hiddenSpecies.getSize(); hs++)
for (unsigned int hs=0; hs < (*ea->getHiddenSpecies()).getSize(); hs++)
{
Parameter* hidden = (Parameter*) hiddenSpecies.get(hs);
Parameter* hidden = (Parameter*) (*ea->getHiddenSpecies()).get(hs);
addODEPair(hidden->getId(), model);
}
cout << "After\n";
Expand Down
54 changes: 49 additions & 5 deletions src/sbml/conversion/test/TestExpressionAnalyser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,11 +488,11 @@ END_TEST
START_TEST(test_order_expressions_1)
{
RateRule* rr = d->getModel()->createRateRule();
rr->setVariable("b");
rr->setVariable("a");
rr->setMath(SBML_parseFormula("k - x + w - y"));

RateRule* rrr = d->getModel()->createRateRule();
rrr->setVariable("a");
rrr->setVariable("b");
rrr->setMath(SBML_parseFormula("k-x-y"));
converter->populateInitialODEinfo();
ExpressionAnalyser* analyser = new ExpressionAnalyser(m, converter->getOdePairs());
Expand All @@ -503,9 +503,9 @@ START_TEST(test_order_expressions_1)

fail_unless(analyser->getNumExpressions() == 2);
SubstitutionValues_t* value = analyser->getExpression(0);
//fail_unless(value->type == TYPE_K_MINUS_X_PLUS_W_MINUS_Y);
fail_unless(value->type == TYPE_K_MINUS_X_PLUS_W_MINUS_Y);
SubstitutionValues_t* value1 = analyser->getExpression(1);
//fail_unless(value1->type == TYPE_K_MINUS_X_MINUS_Y);
fail_unless(value1->type == TYPE_K_MINUS_X_MINUS_Y);

analyser->orderExpressions();
fail_unless(analyser->getNumExpressions() == 2);
Expand All @@ -516,6 +516,49 @@ START_TEST(test_order_expressions_1)
}
END_TEST

START_TEST(test_order_expressions_2)
{
ConversionProperties props;
props.addOption("inferReactions", true);

SBMLRateRuleConverter* converter = new SBMLRateRuleConverter();
converter->setProperties(&props);

std::string filename(TestDataDirectory);
filename += "mraterules5.xml";


SBMLDocument* d = readSBMLFromFile(filename.c_str());
Model* model = d->getModel();
fail_unless(model != NULL);
fail_unless(model->getNumParameters() == 2);

converter->setDocument(d);
converter->populateInitialODEinfo();
converter->populateInitialODEinfo();
ExpressionAnalyser* analyser = new ExpressionAnalyser(model, converter->getOdePairs());

fail_unless(analyser->getNumExpressions() == 0);

analyser->analyse();

fail_unless(analyser->getNumExpressions() == 2);
SubstitutionValues_t* value = analyser->getExpression(0);
fail_unless(value->type == TYPE_K_MINUS_X_PLUS_W_MINUS_Y);
SubstitutionValues_t* value1 = analyser->getExpression(1);
fail_unless(value1->type == TYPE_K_MINUS_X_MINUS_Y);

analyser->orderExpressions();
fail_unless(analyser->getNumExpressions() == 2);
SubstitutionValues_t* value2 = analyser->getExpression(0);
fail_unless(value2->type == TYPE_K_MINUS_X_MINUS_Y);
SubstitutionValues_t* value3 = analyser->getExpression(1);
fail_unless(value3->type == TYPE_K_MINUS_X_PLUS_W_MINUS_Y);

delete converter;
delete d;
}
END_TEST

START_TEST(test_reorder_minusXplusYIteratively_simple)
{
Expand Down Expand Up @@ -757,7 +800,7 @@ Suite *suite = suite_create("ExpressionAnalyser");

if (testing)
{
tcase_add_test(tcase, test_order_expressions_1);
tcase_add_test(tcase, test_order_expressions_2);
}
else
{
Expand All @@ -772,6 +815,7 @@ Suite *suite = suite_create("ExpressionAnalyser");
tcase_add_test(tcase, test_analyse_1_two_terms); //(k+v-x-y)+(k-x)
tcase_add_test(tcase, test_analyse_1_different); //k+v-x-y
tcase_add_test(tcase, test_reorder_minusXplusYIteratively_simple);
tcase_add_test(tcase, test_order_expressions_1);
//tcase_add_test(tcase, test_order_of_replacements1);
//tcase_add_test(tcase, test_order_of_replacements2);

Expand Down
37 changes: 19 additions & 18 deletions src/sbml/conversion/test/test-data/mraterules5.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,58 +5,59 @@
<compartment metaid="COPASI1" id="compartment" name="compartment" spatialDimensions="3" size="1" constant="true"/>
</listOfCompartments>
<listOfSpecies>
<species id="c" name="c" compartment="compartment" initialConcentration="1" boundaryCondition="false" constant="false"/>

<species id="a" name="a" compartment="compartment" initialConcentration="1" boundaryCondition="false" constant="false"/>
<species id="b" name="b" compartment="compartment" initialConcentration="1" boundaryCondition="false" constant="false"/>
</listOfSpecies>
<species id="c" name="c" compartment="compartment" initialConcentration="1" boundaryCondition="false" constant="false"/>
</listOfSpecies>
<listOfParameters>
<parameter id="k" value="2.1"/>
<parameter id="v" value="2.1" constant="false"/>
</listOfParameters>
<listOfRules>
<rateRule variable="c">
<rateRule variable="a">
<math xmlns="http://www.w3.org/1998/Math/MathML">
<apply>
<apply>
<times/>
<cn> 1 </cn>
<cn> -1 </cn>
<apply>
<minus/>
<apply>
<minus/>
<apply>
<plus/>
<ci> k </ci>
<ci> v </ci>
</apply>
<ci> a </ci>
</apply>
<ci> b </ci>
</apply>
</apply>
</math>
</rateRule>
<rateRule variable="a">
<rateRule variable="b">
<math xmlns="http://www.w3.org/1998/Math/MathML">
<apply>
<ci> c </ci>
</math>
</rateRule>
<rateRule variable="c">
<math xmlns="http://www.w3.org/1998/Math/MathML">
<apply>
<times/>
<cn> -1 </cn>
<cn> 1 </cn>
<apply>
<minus/>
<apply>
<minus/>
<apply>
<plus/>
<ci> k </ci>
<ci> v </ci>
</apply>
<ci> a </ci>
</apply>
<ci> b </ci>
</apply>
</apply>
</math>
</rateRule>
<rateRule variable="b">
<math xmlns="http://www.w3.org/1998/Math/MathML">
<ci> c </ci>
</math>
</rateRule>
</listOfRules>
</listOfRules>
</model>
</sbml>
13 changes: 13 additions & 0 deletions src/sbml/math/ASTNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,7 @@ ASTNode::getListOfNodesWithLevel() const

fillListOfNodesWithLevel((ASTNodePredicate)ASTNode_isOperator, vector_pairs, 0);
fillListOfNodesWithLevel((ASTNodePredicate)ASTNode_isName, vector_pairs, 0);
fillListOfNodesWithLevel((ASTNodePredicate)ASTNode_isNumber, vector_pairs, 0);

return vector_pairs;
}
Expand Down Expand Up @@ -4719,6 +4720,18 @@ ASTNode::unsetDeclaredNamespaces()

/** @cond doxygenIgnored */

LIBSBML_EXTERN
void
printNodeLevels(ASTNodeLevels vector_pairs)
{
ASTNodeLevels::iterator it;
for (it = vector_pairs.begin(); it != vector_pairs.end(); it++)
{
cout << "level:" << it->first << " " << SBML_formulaToL3String(it->second) << endl;
}
}


LIBSBML_EXTERN
ASTNode_t *
ASTNode_create (void)
Expand Down
9 changes: 7 additions & 2 deletions src/sbml/math/ASTNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ typedef ASTNodeLevels::iterator ASTNodeLevelsIterator;

#endif // !SWIG


class List;
class ASTBasePlugin;
class ExtendedMathList;
Expand Down Expand Up @@ -2373,7 +2372,6 @@ setValue(value, 0);
/** @endcond */


protected:

friend class SBMLRateRuleConverter;

Expand Down Expand Up @@ -2448,6 +2446,8 @@ setValue(value, 0);
*/
void convertRootToPower();

protected:

/*
* returns derivativeof particular function
* i.e. derivativePlus gets a function A + B and returns d(A+B)/dx
Expand Down Expand Up @@ -2562,6 +2562,11 @@ LIBSBML_CPP_NAMESPACE_END
#ifndef SWIG

LIBSBML_CPP_NAMESPACE_BEGIN
LIBSBML_EXTERN
void
printNodeLevels(ASTNodeLevels vector_pairs);


BEGIN_C_DECLS


Expand Down
Loading

0 comments on commit 7ec9f90

Please sign in to comment.