@@ -235,7 +235,7 @@ SBMLRateRuleConverter::getDefaultProperties() const
235235 {
236236 prop.addOption (" inferReactions" , true ,
237237 " Infer reactions from rateRules in the model" );
238- prop.addOption (" useStoichiometryFromMath" , false ,
238+ prop.addOption (" useStoichiometryFromMath" , true ,
239239 " If a number appears in the math use it as the stoichiometry" );
240240
241241 init = true ;
@@ -278,7 +278,7 @@ SBMLRateRuleConverter::setDocument(SBMLDocument* doc)
278278{
279279 if (SBMLConverter::setDocument (doc) == LIBSBML_OPERATION_SUCCESS)
280280 {
281- if (mDocument != NULL )
281+ if (mDocument != NULL && mDocument -> getModel () != NULL )
282282 {
283283 mOriginalModel = mDocument ->getModel ()->clone ();
284284 return LIBSBML_OPERATION_SUCCESS;
@@ -468,11 +468,11 @@ void SBMLRateRuleConverter::populateTerms()
468468 // Fages algo 3.6 Step 2
469469 createTerms (node);
470470 }
471- for (unsigned int n = 0 ; n < mTerms .size (); n++)
472- {
473- ASTNode* node = mTerms .at (n);
474- cout << " Term " << n << " : " << SBML_formulaToL3String (node) << endl;
475- }
471+ // for (unsigned int n = 0; n < mTerms.size(); n++)
472+ // {
473+ // ASTNode* node = mTerms.at(n);
474+ // cout << "Term " << n << ": " << SBML_formulaToL3String(node) << endl;
475+ // }
476476 // print_vectors(mCoefficients);
477477}
478478
@@ -561,18 +561,15 @@ SBMLRateRuleConverter::determineCoefficient(ASTNode* ode, unsigned int termN, do
561561 // first child should be a number
562562 // take it out of the term
563563 // it will be used as a coefficient
564- // if (ode_node->getType() == AST_REAL)
565- // {
566- // coeff = ode_node->getValue();
567- // found = true;
568- // }
564+ // unless we do not want it used as stoichiometry
565+
569566 if (ode_node->getType () == AST_TIMES && ode_node->getNumChildren () > 0 )
570567 {
571568 if (ode_node->getChild (0 )->isNumber ())
572569 {
573570 coeff = ode_node->getChild (0 )->getValue ();
574571 ode_node->removeChild (0 , true );
575- // we don't want to be loved with the times node if it has only one child
572+ // we don't want to be left with the times node if it has only one child
576573 if (ode_node->getNumChildren () == 1 )
577574 {
578575 ASTNode* child = ode_node->getChild (0 )->deepCopy ();
@@ -951,10 +948,10 @@ SBMLRateRuleConverter::populateInitialODEinfo()
951948 }
952949 }
953950
954- for (unsigned int odeIndex = 0 ; odeIndex < mODEs .size (); odeIndex++)
955- {
956- cout << mODEs [odeIndex].first << " : " << SBML_formulaToL3String (mODEs [odeIndex].second ) << endl;
957- }
951+ // for (unsigned int odeIndex = 0; odeIndex < mODEs.size(); odeIndex++)
952+ // {
953+ // cout << mODEs[odeIndex].first << ": " << SBML_formulaToL3String(mODEs[odeIndex].second) << endl;
954+ // }
958955}
959956
960957
@@ -1019,7 +1016,7 @@ SBMLRateRuleConverter::populateReactionCoefficients()
10191016
10201017bool SBMLRateRuleConverter::useStoichiometryFromMath ()
10211018{
1022- bool value = false ;
1019+ bool value = true ;
10231020 if (getProperties () == NULL || getProperties ()->hasOption (" useStoichiometryFromMath" ) == false )
10241021 {
10251022 return value;
@@ -1149,10 +1146,53 @@ SBMLRateRuleConverter::dealWithSpecies()
11491146 }
11501147}
11511148
1152- void
1153- SBMLRateRuleConverter::dealWithStoichiometry ( )
1149+ bool
1150+ SBMLRateRuleConverter::needToAdjustStoichiometryAndMath ( unsigned int odeNumber )
11541151{
1155- // TO DO
1152+ bool adjust = false ;
1153+ if (mODEs .at (odeNumber).second ->getType () == AST_TIMES &&
1154+ mODEs .at (odeNumber).second ->getNumChildren () > 0 &&
1155+ mODEs .at (odeNumber).second ->getChild (0 )->isNumber () &&
1156+ (mODEs .at (odeNumber).second ->getChild (0 )->getValue () > 1.0 ||
1157+ mODEs .at (odeNumber).second ->getChild (0 )->getValue () < -1.0 )
1158+ )
1159+ {
1160+ adjust = true ;
1161+ }
1162+ return adjust;
1163+ }
1164+
1165+ double
1166+ SBMLRateRuleConverter::dealWithStoichiometry (double stoichiometry, ASTNode& math, unsigned int odeNumber)
1167+ {
1168+ if (useStoichiometryFromMath ())
1169+ {
1170+ return stoichiometry;
1171+ }
1172+ else
1173+ {
1174+ if (stoichiometry > 1.0 && needToAdjustStoichiometryAndMath (odeNumber) )
1175+ {
1176+
1177+ ASTNode* newMath = mODEs .at (odeNumber).second ->deepCopy ();
1178+ if (newMath->getType () == AST_TIMES && newMath->getNumChildren () > 0 &&
1179+ newMath->getChild (0 )->isNumber ())
1180+ {
1181+ double value = newMath->getChild (0 )->getValue ();
1182+ if (value < 0.0 )
1183+ {
1184+ // we don't want a value that is less than zero
1185+ ASTNode* child = newMath->getChild (0 )->deepCopy ();
1186+ child->setValue (-1.0 * value);
1187+ newMath->replaceChild (0 , child);
1188+ }
1189+ }
1190+ math = *newMath;
1191+ delete newMath;
1192+ stoichiometry = 1.0 ;
1193+ }
1194+ }
1195+ return stoichiometry;
11561196}
11571197
11581198void
@@ -1162,6 +1202,7 @@ SBMLRateRuleConverter::createReactions()
11621202 char number[4 ];
11631203 for (setCoeffIt it = mCoefficients .begin (); it != mCoefficients .end (); ++it)
11641204 {
1205+ ASTNode* math = (it->first )->deepCopy ();
11651206 Reaction *r = mDocument ->getModel ()->createReaction ();
11661207 r->setReversible (false );
11671208 r->setFast (false );
@@ -1175,7 +1216,7 @@ SBMLRateRuleConverter::createReactions()
11751216 double stoichiometry = 1.0 ;
11761217 if (mReactants [i][j] > 0 )
11771218 {
1178- stoichiometry = mReactants [i][j];
1219+ stoichiometry = dealWithStoichiometry ( mReactants [i][j], *math, j) ;
11791220 SpeciesReference *sr = r->createReactant ();
11801221 sr->setSpecies (mODEs [j].first );
11811222 sr->setStoichiometry (stoichiometry);
@@ -1184,7 +1225,7 @@ SBMLRateRuleConverter::createReactions()
11841225 }
11851226 if (mProducts [i][j] > 0 )
11861227 {
1187- stoichiometry = mProducts [i][j];
1228+ stoichiometry = dealWithStoichiometry ( mProducts [i][j], *math, j) ;
11881229 SpeciesReference *sr = r->createProduct ();
11891230 sr->setSpecies (mODEs [j].first );
11901231 sr->setStoichiometry (stoichiometry);
@@ -1201,7 +1242,7 @@ SBMLRateRuleConverter::createReactions()
12011242 if (itemAdded && !r->isSetKineticLaw ())
12021243 {
12031244 KineticLaw *kl = r->createKineticLaw ();
1204- kl->setMath (it-> first );
1245+ kl->setMath (math );
12051246 }
12061247
12071248 // check whetherkinetic law uses a species not listed as p/r/m
0 commit comments