From 8cb62151ec8d9ff39722765baa5a645057719657 Mon Sep 17 00:00:00 2001 From: Sarah Keating Date: Thu, 12 Dec 2024 09:15:54 +0000 Subject: [PATCH] committing these as I need to backup my hard drive --- src/sbml/conversion/ExpressionAnalyser.cpp | 181 +++--------------- .../test/TestExpressionAnalyser.cpp | 81 +++++++- src/sbml/conversion/test/TestRunner.c | 7 +- src/sbml/conversion/test/test-data/test | 36 +++- 4 files changed, 143 insertions(+), 162 deletions(-) diff --git a/src/sbml/conversion/ExpressionAnalyser.cpp b/src/sbml/conversion/ExpressionAnalyser.cpp index a8a454147..f626f94a3 100644 --- a/src/sbml/conversion/ExpressionAnalyser.cpp +++ b/src/sbml/conversion/ExpressionAnalyser.cpp @@ -312,6 +312,28 @@ bool ExpressionAnalyser::expressionExists(SubstitutionValues_t* current, matchesXValue(current, mightAdd) && matchesDxdtExpression(current, mightAdd); + if (alreadyExists && current->type == mightAdd->type) + { + switch (current->type) + { + case TYPE_K_MINUS_X_MINUS_Y: + alreadyExists = alreadyExists && matchesYValue(current, mightAdd) && + matchesDydtExpression(current, mightAdd); + break; + case TYPE_K_PLUS_V_MINUS_X_MINUS_Y: + alreadyExists = alreadyExists && matchesVExpression(current, mightAdd) && + matchesYValue(current, mightAdd); + break; + case TYPE_K_MINUS_X_PLUS_W_MINUS_Y: + alreadyExists = alreadyExists && matchesWExpression(current, mightAdd) && + matchesYValue(current, mightAdd); + break; + default: + break; + } + return alreadyExists; + } + //switch (current->type) //{ //case TYPE_K_MINUS_X_MINUS_Y: @@ -567,8 +589,8 @@ ExpressionAnalyser::analyseNode(ASTNode* node, SubstitutionValues_t *value) ASTNodeType_t type = node->getType(); ASTNode* rightChild = node->getRightChild(); ASTNode* leftChild = node->getLeftChild(); - cout << "RIGHT CHILD: " << SBML_formulaToL3String(rightChild) << endl; - cout << "left CHILD: " << SBML_formulaToL3String(leftChild) << endl; + //cout << "RIGHT CHILD: " << SBML_formulaToL3String(rightChild) << endl; + //cout << "left CHILD: " << SBML_formulaToL3String(leftChild) << endl; if (isTypeKminusX(numChildren, rightChild, leftChild, type, value) || isTypeKminusXminusY(numChildren, rightChild, leftChild, type, value) || @@ -583,150 +605,6 @@ ExpressionAnalyser::analyseNode(ASTNode* node, SubstitutionValues_t *value) } - - //switch (type) - //{ - ////case AST_PLUS: - // // -x+y node binary; plus; left child type minus; rightchild var/const - // // + - // // - y - // // x - // // we might have encountered w+(k-x)as part of k-x+w-y but it might not be part of it - // if (rightChild->getNumChildren() == 2 && analyseNode(rightChild, value) && value->type == TYPE_K_MINUS_X) - // { - // value->w_expression = leftChild; - // value->type = TYPE_K_MINUS_X_PLUS_W_MINUS_Y; - // value->current = node; - // return true; - // } - // if (numChildren != 2 || rightChild->getType() != AST_NAME - // || leftChild->getType() != AST_MINUS - // || leftChild->getNumChildren() != 1) - // return false; - - // // if we get to this point, the only thing left to check is - // // whether the ->left->right grandchild (the x in -x+y) is a variable species. - // if (isVariableSpeciesOrParameter(leftChild->getChild(0))) - // { - // value->x_value = leftChild->getChild(0)->getName(); - // value->y_value = rightChild->getName(); - // value->dydt_expression = getODEFor(rightChild->getName()); - // value->dxdt_expression = getODEFor(leftChild->getChild(0)->getName()); - // value->type = TYPE_MINUS_X_PLUS_Y; - // value->current = node; - // return true; - // } - // break; - - //case AST_MINUS: - // // - - - // // k x - y - // // k x - // // k-x or k-x-y node binary; right child (x,y) is variable - // // - - - - // // + x - y + y - // // k v + x - w - // // k v k x - // // - // // // k+v-x; right child x var; left child plus node with left child k constant - // // k+v-x-y; rightchild var y; left child minus == k+v-x - // // k-x+w-y; rightchild var y; left child plus with left child == k-x - // if (numChildren != 2 || !isVariableSpeciesOrParameter(rightChild)) - // return false; - // // if left child is numerical constant or a parameter and right child variable, it IS k-x - // if (isNumericalConstantOrConstantParameter(leftChild, isNumber) - // && isVariableSpeciesOrParameter(rightChild)) - // { - // - // if (isNumber) - // { - // value->k_value = "number"; - // value->k_real_value = leftChild->getValue(); - // } - // else - // { - // value->k_value = leftChild->getName(); - // } - // value->x_value = rightChild->getName(); - // value->dxdt_expression = getODEFor(rightChild->getName()); - // value->type = TYPE_K_MINUS_X; - // value->current = node; - // return true; - // } - // // left child + with it's left child const we have k+v-x - // // left child + with it's left child k-x+w-y we have already finished - // else if (leftChild->getType() == AST_PLUS) - // { - - // // TO DO fix this for k Or w being a number - // if (value->type == TYPE_K_MINUS_X_PLUS_W_MINUS_Y) - // { - // return true; - // } - // else if (analyseNode(leftChild, value)) - // { - // if (value->type == TYPE_K_MINUS_X_PLUS_W_MINUS_Y) - // { - // value->y_value = rightChild->getName(); - // value->dydt_expression = getODEFor(rightChild->getName()); - // value->current = node; - // return true; - // } - // else if (value->type == TYPE_K_MINUS_X) - // { - // value->y_value = rightChild->getName(); - // value->dydt_expression = getODEFor(rightChild->getName()); - // value->w_expression = leftChild->getChild(1); - // value->type = TYPE_K_MINUS_X_PLUS_W_MINUS_Y; - // value->current = node; - // return true; - - // } - // } - // else if (isNumericalConstantOrConstantParameter(leftChild->getChild(0), isNumber)) - // { - // value->k_value = leftChild->getChild(0)->getName(); - // value->x_value = rightChild->getName(); - // value->dxdt_expression = getODEFor(rightChild->getName()); - // value->v_expression = leftChild->getChild(1); - // value->type = TYPE_K_PLUS_V_MINUS_X; - // value->current = node; - // return true; - // } - // } - // else if (leftChild->getType() == AST_MINUS - // && isVariableSpeciesOrParameter(leftChild->getRightChild())) - // { - // // if left child is k+v-x we have k+v-x-ymean you'll have to come againthat you can pay3 - // // or if left child is k-x we have k-x-y - // if (analyseNode(leftChild, value)) - // { - // if (value->type == TYPE_K_PLUS_V_MINUS_X) - // { - // value->type = TYPE_K_PLUS_V_MINUS_X_MINUS_Y; - // value->y_value = rightChild->getName(); - // value->dydt_expression = getODEFor(value->y_value); - // value->current = node; - // return true; - // } - // else if (value->type == TYPE_K_MINUS_X) - // { - // value->y_value = rightChild->getName(); - // value->dydt_expression = getODEFor(rightChild->getName()); - // value->type = TYPE_K_MINUS_X_MINUS_Y; - // value->current = node; - // return true; - // } - // } - // return false; - // } - // break; - //default: - // return false; - //} - //return false; - - /* * Return the ODE for the given variable * or an ASTNode representing zero if there is no time derivative @@ -824,21 +702,12 @@ ExpressionAnalyser::analyse(bool minusXPlusYOnly) while (it != operators->end()) { ASTNode* currentNode = (ASTNode*)*it; - if (minusXPlusYOnly) - cout << "current node in -x+y analyze: " << SBML_formulaToL3String(currentNode) << endl; - else - cout << "current node in not -x+y analyze: " << SBML_formulaToL3String(currentNode) << endl; - if (minusXPlusYOnly && currentNode->getType() != AST_PLUS) - { - it++; - continue; - } SubstitutionValues_t* value = createBlankSubstitutionValues(); if (analyseNode(currentNode, value)) { value->odeIndex = odeIndex; - if (!hasExpressionAlreadyBeenRecorded(value)) + if (hasExpressionAlreadyBeenRecorded(value) == false) { //printSubstitutionValues(value); mExpressions.push_back(value); diff --git a/src/sbml/conversion/test/TestExpressionAnalyser.cpp b/src/sbml/conversion/test/TestExpressionAnalyser.cpp index df2df3ee5..6af29f8ca 100644 --- a/src/sbml/conversion/test/TestExpressionAnalyser.cpp +++ b/src/sbml/conversion/test/TestExpressionAnalyser.cpp @@ -1,5 +1,5 @@ /** - * @file TestExpressionAnalyser.cpp + +* @file TestExpressionAnalyser.cpp * @brief Tests for raterule to reaction converter * @author Sarah Keating * @author Alessandro Felder @@ -174,6 +174,80 @@ START_TEST(test_analyse) } END_TEST +START_TEST(test_analyse_same_expression) +{ + RateRule* rr = d->getModel()->createRateRule(); + rr->setVariable("a"); + rr->setMath(SBML_parseFormula("k-x-y")); + + RateRule* rr1 = d->getModel()->createRateRule(); + rr1->setVariable("b"); + rr1->setMath(SBML_parseFormula("k-x-y")); + + + converter->populateInitialODEinfo(); + ExpressionAnalyser* analyser = new ExpressionAnalyser(m, converter->getOdePairs()); + + fail_unless(analyser->getNumExpressions() == 0); + + analyser->analyse(); + + fail_unless(analyser->getNumExpressions() == 1); + SubstitutionValues_t* value = analyser->getExpression(0); + fail_unless(value->k_value == "k"); + fail_unless(value->x_value == "x"); + fail_unless(value->y_value == "y"); + fail_unless(value->z_value.empty()); + fail_unless(value->type == TYPE_K_MINUS_X_MINUS_Y); + fail_unless(formulas_equal("k - x - y", value->current)); + fail_unless(formulas_equal("0", value->dxdt_expression)); + fail_unless(formulas_equal("0", value->dydt_expression)); + fail_unless(value->v_expression == NULL); + fail_unless(value->w_expression == NULL); + fail_unless(value->z_expression == NULL); + fail_unless(value->odeIndex == 0); + fail_unless(util_isNaN(value->k_real_value)); +} +END_TEST + + +START_TEST(test_analyse_different_expression) +{ + // the second expression is the same type but has a different variable + RateRule* rr = d->getModel()->createRateRule(); + rr->setVariable("a"); + rr->setMath(SBML_parseFormula("k-x-y")); + + RateRule* rr1 = d->getModel()->createRateRule(); + rr1->setVariable("b"); + rr1->setMath(SBML_parseFormula("k-x-a")); + + + converter->populateInitialODEinfo(); + ExpressionAnalyser* analyser = new ExpressionAnalyser(m, converter->getOdePairs()); + + fail_unless(analyser->getNumExpressions() == 0); + + analyser->analyse(); + + fail_unless(analyser->getNumExpressions() == 2); + SubstitutionValues_t* value = analyser->getExpression(1); + fail_unless(value->k_value == "k"); + fail_unless(value->x_value == "x"); + fail_unless(value->y_value == "c"); + fail_unless(value->z_value.empty()); + fail_unless(value->type == TYPE_K_MINUS_X_MINUS_Y); + fail_unless(formulas_equal("k - x - c", value->current)); + fail_unless(formulas_equal("0", value->dxdt_expression)); + fail_unless(formulas_equal("0", value->dydt_expression)); + fail_unless(value->v_expression == NULL); + fail_unless(value->w_expression == NULL); + fail_unless(value->z_expression == NULL); + fail_unless(value->odeIndex == 0); + fail_unless(util_isNaN(value->k_real_value)); +} +END_TEST + START_TEST(test_analyse_1) { RateRule* rr = d->getModel()->createRateRule(); @@ -524,7 +598,7 @@ END_TEST Suite * create_suite_TestExpressionAnalyser (void) { - bool testing = false; + bool testing = true; Suite *suite = suite_create("ExpressionAnalyser"); TCase *tcase = tcase_create("ExpressionAnalyser"); tcase_add_checked_fixture(tcase, @@ -532,7 +606,7 @@ Suite *suite = suite_create("ExpressionAnalyser"); if (testing) { - tcase_add_test(tcase, test_analyse_2); + tcase_add_test(tcase, test_analyse_different_expression); } else { @@ -541,6 +615,7 @@ Suite *suite = suite_create("ExpressionAnalyser"); tcase_add_test(tcase, test_analyse_2); //k-x+w-y tcase_add_test(tcase, test_analyse_3); //k-x tcase_add_test(tcase, test_analyse_4); //k+v-x + tcase_add_test(tcase, test_analyse_same_expression); //k-x-y //tcase_add_test(tcase, test_order_of_replacements); //tcase_add_test(tcase, test_order_of_replacements1); //tcase_add_test(tcase, test_order_of_replacements2); diff --git a/src/sbml/conversion/test/TestRunner.c b/src/sbml/conversion/test/TestRunner.c index 7e6a99a19..564c98ff0 100644 --- a/src/sbml/conversion/test/TestRunner.c +++ b/src/sbml/conversion/test/TestRunner.c @@ -75,6 +75,7 @@ Suite *create_suite_TestLevelVersionConverter(void); Suite *create_suite_TestRateOfConverter(void); Suite *create_suite_TestSBMLRateRuleConverter(void); +Suite* create_suite_TestExpressionAnalyser(void); /** * Global. @@ -118,7 +119,8 @@ main (void) int num_failed; setTestDataDirectory(); - SRunner *runner = srunner_create(create_suite_TestSBMLRateRuleConverter()); + SRunner *runner = srunner_create(create_suite_TestExpressionAnalyser()); + // srunner_add_suite(runner, create_suite_TestSBMLRateRuleConverter()); /*SRunner *runner = srunner_create( create_suite_TestConversionOption() ); srunner_add_suite( runner, create_suite_TestSBMLRuleConverter () ); @@ -131,7 +133,8 @@ main (void) srunner_add_suite( runner, create_suite_TestStripPackageConverter () ); srunner_add_suite( runner, create_suite_TestLevelVersionConverter () ); srunner_add_suite( runner, create_suite_TestRateOfConverter () ); - srunner_add_suite( runner, create_suite_TestSBMLRateRuleConverter () );*/ + srunner_add_suite( runner, create_suite_TestSBMLRateRuleConverter () ); + srunner_add_suite( runner, create_suite_TestExpressionAnalyser () );*/ /* srunner_set_fork_status(runner, CK_NOFORK); */ diff --git a/src/sbml/conversion/test/test-data/test b/src/sbml/conversion/test/test-data/test index 83e56beae..e64e063d2 100644 --- a/src/sbml/conversion/test/test-data/test +++ b/src/sbml/conversion/test/test-data/test @@ -98,25 +98,59 @@ reconstructModel analyzeNode k-x-y right: y left: k-x + minus analyzeNode k-x right: x left: k + minus values: k x dx type=k-x values: y dy type=k-x-y , TYPE_K_PLUS_V_MINUS_X_MINUS_Y analyzeNode k+v-x-y right: y left: k+v-x + minus analyzeNode: k+v-x right: x left: k+v + minus analyzeNode: k+v right: v left: k + plus return False values: k x dx v type=k+v-x values: y dy type=k+v-x-y , TYPE_K_MINUS_X_PLUS_W_MINUS_Y + analyzeNode (k-x)+w-y IT BECOMES w+(k-x)-y + right: y + left: w+(k-x) + minus + analyzeNode w+(k-x) + right: k-x + left: w + plus + analyzeNode k-x + right: x + left: k + minus + values: k x dx type=k-x + values: w type=k-x+w-y + values: y dy , TYPE_K_MINUS_X + analyzeNode k-x + right: x + left: k + minus + values: k x dx type=k-x , TYPE_K_PLUS_V_MINUS_X -ne \ No newline at end of file + analyzeNode: k+v-x + right: x + left: k+v + minus + analyzeNode: k+v + right: v + left: k + plus + return False + values: k x dx v type=k+v-x