コード例 #1
0
ファイル: DORreaction.cpp プロジェクト: SysSynBio/nfsim
void DORRxnClass::remove(Molecule *m, unsigned int reactantPos)
{
	//cout<<"removing from a DOR!!"<<endl;
	if(reactantPos==(unsigned)this->DORreactantIndex) {
		//if(DEBUG_MESSAGE)cout<<" ... as a DOR"<<endl;

		// handle the DOR reactant
		int rxnIndex = m->getMoleculeType()->getRxnIndex(this,reactantPos);
		if(m->getRxnListMappingId(rxnIndex)>=0) {
			//cout<<"was in the tree, so we should remove"<<endl;
			reactantTree->removeMappingSet(m->getRxnListMappingId(rxnIndex));
			m->setRxnListMappingId(rxnIndex,Molecule::NOT_IN_RXN);
		}
	} else {

		// handle it normally...
		//if(DEBUG_MESSAGE)cout<<" ... as a normal reactant"<<endl;
		ReactantList *rl = reactantLists[reactantPos];
		int rxnIndex = m->getMoleculeType()->getRxnIndex(this,reactantPos);
		if(m->getRxnListMappingId(rxnIndex)>=0) {
			rl->removeMappingSet(m->getRxnListMappingId(rxnIndex));
			m->setRxnListMappingId(rxnIndex,Molecule::NOT_IN_RXN);
		}
	}
	//if(DEBUG_MESSAGE)cout<<"finished removing"<<endl;
}
コード例 #2
0
void Module::LoadSBML(const SBMLDocument* sbmldoc)
{
  //m_sbml = *sbmldoc;
  const Model* sbml = sbmldoc->getModel();
  string sbmlname = "";

  //Function Definitions
  //This is a bit weird, since functions exist outside of modules, since they can be used in any model.  So we have to go to the registry to save them.
  for (unsigned int func=0; func<sbml->getNumFunctionDefinitions(); func++) {
    const FunctionDefinition* function = sbml->getFunctionDefinition(func);
    sbmlname = getNameFromSBMLObject(function, "_F");
    g_registry.NewUserFunction(&sbmlname);
    for (unsigned int arg=0; arg<function->getNumArguments(); arg++) {
      string argument(parseASTNodeToString(function->getArgument(arg)));
      Variable* expvar = g_registry.AddVariableToCurrent(&argument);
      g_registry.AddVariableToCurrentExportList(expvar);
    }
    string formulastring(parseASTNodeToString(function->getBody()));
    Formula* formula = g_registry.NewBlankFormula();
    setFormulaWithString(formulastring, formula, this);
    g_registry.SetUserFunction(formula);
    g_registry.GetNthUserFunction(g_registry.GetNumUserFunctions()-1)->FixNames();
  }

  set<string> defaultcompartments;
  //Compartments
  for (unsigned int comp=0; comp<sbml->getNumCompartments(); comp++) {
    const Compartment* compartment = sbml->getCompartment(comp);
    sbmlname = getNameFromSBMLObject(compartment, "_C");
    if (compartment->getSBOTerm() == 410) {
      //The 'implicit compartment'
      defaultcompartments.insert(sbmlname);
      continue;
    }
    if (sbmlname == DEFAULTCOMP && compartment->getConstant() && compartment->isSetSize() && compartment->getSize() == 1.0) {
      defaultcompartments.insert(sbmlname);
      continue;
      //LS NOTE: we assume this was created with Antimony, and ignore the auto-generated 'default compartment'
      // Later versions of antimony now set the SBO terms to 410, so we might not need this code very long.
    }
    Variable* var = AddOrFindVariable(&sbmlname);
    if (compartment->isSetName()) {
      var->SetDisplayName(compartment->getName());
    }
    var->SetType(varCompartment);
    Formula* formula = g_registry.NewBlankFormula();
    if (compartment->isSetSize()) {
      formula->AddNum(compartment->getSize());
      var->SetFormula(formula);
    }
    if (compartment->isSetUnits()) {
      var->SetUnits(compartment->getUnits());
    }
  }

  //Species
  for (unsigned int spec=0; spec<sbml->getNumSpecies(); spec++) {
    const Species* species = sbml->getSpecies(spec);
    sbmlname = getNameFromSBMLObject(species, "_S");
    Variable* var = AddOrFindVariable(&sbmlname);
    if (species->isSetName()) {
      var->SetDisplayName(species->getName());
    }
    var->SetType(varSpeciesUndef);

    //Setting the formula
    Formula* formula = g_registry.NewBlankFormula();
    if (species->isSetInitialAmount()) {
      double amount = species->getInitialAmount();
      formula->AddNum(amount);
      if (amount != 0 && defaultcompartments.find(species->getCompartment()) == defaultcompartments.end()) {
        Variable* compartment = AddOrFindVariable(&(species->getCompartment()));
        Formula* compform = compartment->GetFormula();
        if (!compform->IsOne()) {
          formula->AddMathThing('/');
          formula->AddVariable(compartment);
        }
      }
      var->SetFormula(formula);
    }
    else if (species->isSetInitialConcentration()) {
      formula->AddNum(species->getInitialConcentration());
      var->SetFormula(formula);
    }
    //Anything more complicated is set in a Rule, which we'll get to later.

    if (species->getConstant() || species->getBoundaryCondition()) {
      //Since all species are variable by default, we only set this explicitly if true.
      var->SetIsConst(true);
    }
    if (defaultcompartments.find(species->getCompartment()) == defaultcompartments.end()) {
      Variable* compartment = AddOrFindVariable(&(species->getCompartment()));
      compartment->SetType(varCompartment);
      var->SetCompartment(compartment);
    }
    if (species->isSetUnits()) {
      var->SetUnits(species->getUnits());
    }
  }
  
  //Events:
  for (unsigned int ev=0; ev<sbml->getNumEvents(); ev++) {
    const Event* event = sbml->getEvent(ev);
    sbmlname = getNameFromSBMLObject(event, "_E");
    Variable* var = AddOrFindVariable(&sbmlname);
    if (event->isSetName()) {
      var->SetDisplayName(event->getName());
    }
    var->SetType(varEvent);

    //Set the trigger:
    string triggerstring(parseASTNodeToString(event->getTrigger()->getMath()));
    Formula trigger;
    setFormulaWithString(triggerstring, &trigger, this);
    Formula delay;
    const Delay* sbmldelay = event->getDelay();
    if (sbmldelay != NULL) {
      string delaystring(parseASTNodeToString(sbmldelay->getMath()));
      setFormulaWithString(delaystring, &delay, this);
    }
    AntimonyEvent antevent(delay, trigger,var);
    var->SetEvent(&antevent);

    //Set the assignments:
    for (unsigned int asnt=0; asnt<event->getNumEventAssignments(); asnt++) {
      const EventAssignment* assignment = event->getEventAssignment(asnt);
      string name = assignment->getVariable();
      Variable* asntvar = AddOrFindVariable(&name);
      Formula*  asntform = g_registry.NewBlankFormula();
      setFormulaWithString(parseASTNodeToString(assignment->getMath()), asntform, this);
      var->GetEvent()->AddResult(asntvar, asntform);
    }
  }

  //LS DEBUG:  Add constraints?

  //Parameters
  for (unsigned int param=0; param<sbml->getNumParameters(); param++) {
    const Parameter* parameter = sbml->getParameter(param);
    sbmlname = getNameFromSBMLObject(parameter, "_P");
    Variable* var = AddOrFindVariable(&sbmlname);
    if (parameter->isSetName()) {
      var->SetDisplayName(parameter->getName());
    }
    if (parameter->isSetValue()) {
      Formula* formula = g_registry.NewBlankFormula();
      formula->AddNum(parameter->getValue());
      var->SetFormula(formula);
      //LS NOTE:  If a parameter has both a value and an 'initial assignment', the initial assignment will override the value.
    }
    if (parameter->isSetUnits()) {
      var->SetUnits(parameter->getUnits());
    }
  }

  //Initial Assignments:  can override 'getValue' values.
  for (unsigned int ia=0; ia<sbml->getNumInitialAssignments(); ia++) {
    const InitialAssignment* initasnt = sbml->getInitialAssignment(ia);
    if (initasnt->isSetSymbol()) {
      sbmlname = initasnt->getSymbol();
      Variable* var = AddOrFindVariable(&sbmlname);
      if (initasnt->isSetName()) {
        var->SetDisplayName(initasnt->getName());
      }
      Formula* formula = g_registry.NewBlankFormula();
      string formulastring(parseASTNodeToString(initasnt->getMath()));
      setFormulaWithString(formulastring, formula, this);
      var->SetFormula(formula);
    }
    else {
      //LS DEBUG:  error?  The 'symbol' is supposed to be required.
    }
  }
    
  //Rules:
  for (unsigned int rulen=0; rulen<sbml->getNumRules(); rulen++) {
    const Rule* rule = sbml->getRule(rulen);
    if (rule->isAlgebraic()) {
      //LS DEBUG:  error message?  Unable to process algebraic rules
      continue;
    }
    sbmlname = rule->getVariable();
    assert(sbmlname != "");
    if (sbmlname == "") {
      sbmlname = getNameFromSBMLObject(rule, "_R");
    }
    Variable* var = AddOrFindVariable(&sbmlname);
    if (rule->isSetName()) {
      var->SetDisplayName(rule->getName());
    }
    Formula* formula = g_registry.NewBlankFormula();
    string formulastring(parseASTNodeToString(rule->getMath()));
    setFormulaWithString(formulastring, formula, this);
    if (IsSpecies(var->GetType())) {
      //Any species in any rule must be 'const' (in Antimony), because this means it's a 'boundary species'
      var->SetIsConst(true);
    }
    else {
      //For other parameters, assignment and rate rules always mean the variable in question is not constant.
      var->SetIsConst(false);
    }

    if (rule->isAssignment()) {
      var->SetAssignmentRule(formula);
    }
    else if (rule->isRate()) {
      var->SetRateRule(formula);
    }
    else {
      assert(false); //should be caught above
    }
  }

  //Reactions
  for (unsigned int rxn=0; rxn<sbml->getNumReactions(); rxn++) {
    const Reaction* reaction = sbml->getReaction(rxn);
    sbmlname = getNameFromSBMLObject(reaction, "_J");
    Variable* var = AddOrFindVariable(&sbmlname);
    if (reaction->isSetName()) {
      var->SetDisplayName(reaction->getName());
    }
    //reactants
    ReactantList reactants;
    for (unsigned int react=0; react<reaction->getNumReactants(); react++) {
      const SpeciesReference* reactant = reaction->getReactant(react);
      double stoichiometry = 1;
      if (reactant->isSetStoichiometryMath()) {
        //LS DEBUG:  error message?
      }
      else {
        stoichiometry = reactant->getStoichiometry();
      }
      sbmlname = reactant->getSpecies();
      if (sbmlname == "") {
        sbmlname = getNameFromSBMLObject(reactant, "_S");
      }
      Variable* rvar = AddOrFindVariable(&sbmlname);
      reactants.AddReactant(rvar, stoichiometry);
    }
    //products
    ReactantList products;
    for (unsigned int react=0; react<reaction->getNumProducts(); react++) {
      const SpeciesReference* product = reaction->getProduct(react);
      double stoichiometry = 1;
      if (product->isSetStoichiometryMath()) {
        //LS DEBUG:  error message?
      }
      else {
        stoichiometry = product->getStoichiometry();
      }
      sbmlname = product->getSpecies();
      if (sbmlname == "") {
        sbmlname = getNameFromSBMLObject(product, "_S");
      }
      Variable* rvar = AddOrFindVariable(&sbmlname);
      products.AddReactant(rvar, stoichiometry);
    }
    //formula
    string formulastring = "";
    Formula formula;
    if (reaction->isSetKineticLaw()) {
      const KineticLaw* kl = reaction->getKineticLaw();
      var->SetUnits(kl->getSubstanceUnits() + "/(" + kl->getTimeUnits() + ")");
      formulastring = parseASTNodeToString(kl->getMath());
      setFormulaWithString(formulastring, &formula, this);
      for (unsigned int localp=0; localp<kl->getNumParameters(); localp++) {
        const Parameter* localparam = kl->getParameter(localp);
        vector<string> fullname;
        //Find the variable with the original name:
        string origname = getNameFromSBMLObject(localparam, "_P");
        fullname.push_back(origname);
        Variable* origvar = GetVariable(fullname);

        //Create a new variable with a new name:
        fullname.clear();
        sbmlname = var->GetNameDelimitedBy('_') + "_" + origname;
        fullname.push_back(sbmlname);
        Variable* foundvar = GetVariable(fullname);
        while (foundvar != NULL) {
          //Just in case something weird happened and there was another one of *this* name, too.
          sbmlname = var->GetNameDelimitedBy('_') + "_" + sbmlname;
          fullname.clear();
          fullname.push_back(sbmlname);
          foundvar = GetVariable(fullname);
        }
        Variable* localvar = AddOrFindVariable(&sbmlname);

        //Replace the variable in the formula:
        if(origvar != NULL) {
          formula.ReplaceWith(origvar, localvar);
        }
        else {
          //If origvar is NULL, nothing needs to be replaced: if the original formula had included the parameter, the earlier setFormulaWithString would have created one.  But since there wasn't one, this means the original formula didn't include the parameter at all!  Meaning this local parameter has no use whatsoever!  What the heck, dude.  Oh, well.
          //cout << "Unused local variable for reaction " << var->GetNameDelimitedBy('.') << ":  " << origname << endl;
        }

        //Set the value for the new variable:
        Formula localformula;
        localformula.AddNum(localparam->getValue());
        localvar->SetFormula(&localformula);
      }
    }
    else if (reaction->getNumModifiers() > 0) {
      //If the kinetic law is empty, we can set some interactions, if there are any Modifiers.
      ReactantList right;
      right.AddReactant(var);
      ReactantList left;
      for (unsigned int mod=0; mod<reaction->getNumModifiers(); mod++) {
        const ModifierSpeciesReference* msr = reaction->getModifier(mod);
        string species = msr->getSpecies();
        Variable* specvar = AddOrFindVariable(&species);
        left.AddReactant(specvar);
        sbmlname = getNameFromSBMLObject(msr, "_I");
      }
      Variable* interaction = AddOrFindVariable(&sbmlname);
      Formula blankform;
      AddNewReaction(&left, rdInfluences, &right, &blankform, interaction);
    }
    rd_type rxntype = rdBecomes;
    if (!reaction->getReversible()) {
      rxntype = rdBecomesIrreversibly;
    }
    //Put reactants, products, and the formula together:
    AddNewReaction(&reactants, rxntype, &products, &formula, var);
  }
  //Finally, fix the fact that 'time' used to be OK in functions (l2v1), but is no longer (l2v2).
  g_registry.FixTimeInFunctions();
  //And that some SBML-OK names are not OK in Antimony
  FixNames();
}
コード例 #3
0
ファイル: DORreaction.cpp プロジェクト: SysSynBio/nfsim
bool DOR2RxnClass::tryToAdd(Molecule *m, unsigned int reactantPos) {

	// adding molecule to DOR2RxnClass
	//if(DEBUG_MESSAGE)m->printDetails();
	if (reactantPos==(unsigned)this->DORreactantIndex1) {

		// handle the DOR reactant
		int rxnIndex = m->getMoleculeType()->getRxnIndex(this,reactantPos);

		if(reactantTree1->getHasClonedMappings()) {
			if(m->getRxnListMappingId(rxnIndex)>=0) {
				reactantTree1->removeMappingSet(m->getRxnListMappingId(rxnIndex));
				m->setRxnListMappingId(rxnIndex,Molecule::NOT_IN_RXN);
			}
		}

		if(m->getRxnListMappingId(rxnIndex)>=0) {
			// was in the tree, so checking if we should remove
			if(!reactantTemplates[reactantPos]->compare(m)) {
				// removing
				reactantTree1->removeMappingSet(m->getRxnListMappingId(rxnIndex));
				m->setRxnListMappingId(rxnIndex,Molecule::NOT_IN_RXN);
			} else {}
		} else {
			// wasn't in the tree, so trying to push and compare
			ms=reactantTree1->pushNextAvailableMappingSet();
			if(!reactantTemplates[reactantPos]->compare(m,reactantTree1,ms)) {
				//cout<<"shouldn't be in the tree, so we pop"<<endl;
				reactantTree1->removeMappingSet(ms->getId());
			} else {
				//we are keeping it, so evaluate the function and confirm the push
				double localFunctionValue = evaluateLocalFunctions1(ms);
				//if(DEBUG_MESSAGE)cout<<"local function value is: "<<localFunctionValue<<endl;
				reactantTree1->confirmPush(ms->getId(),localFunctionValue);
				m->setRxnListMappingId(rxnIndex,ms->getId());
			}
		}
	}
	else if (reactantPos==(unsigned)this->DORreactantIndex2) {

		// handle the DOR reactant
		int rxnIndex = m->getMoleculeType()->getRxnIndex(this,reactantPos);

		if(reactantTree2->getHasClonedMappings()) {
			if(m->getRxnListMappingId(rxnIndex)>=0) {
				reactantTree2->removeMappingSet(m->getRxnListMappingId(rxnIndex));
				m->setRxnListMappingId(rxnIndex,Molecule::NOT_IN_RXN);
			}
		}

		if(m->getRxnListMappingId(rxnIndex)>=0) {
			// was in the tree, so checking if we should remove
			if(!reactantTemplates[reactantPos]->compare(m)) {
				// removing
				reactantTree2->removeMappingSet(m->getRxnListMappingId(rxnIndex));
				m->setRxnListMappingId(rxnIndex,Molecule::NOT_IN_RXN);
			} else {}
		} else {
			// wasn't in the tree, so trying to push and compare
			ms=reactantTree2->pushNextAvailableMappingSet();
			if(!reactantTemplates[reactantPos]->compare(m,reactantTree2,ms)) {
				//cout<<"shouldn't be in the tree, so we pop"<<endl;
				reactantTree2->removeMappingSet(ms->getId());
			} else {
				//we are keeping it, so evaluate the function and confirm the push
				double localFunctionValue = this->evaluateLocalFunctions2(ms);
				//if(DEBUG_MESSAGE)cout<<"local function value is: "<<localFunctionValue<<endl;
				reactantTree2->confirmPush(ms->getId(),localFunctionValue);
				m->setRxnListMappingId(rxnIndex,ms->getId());
			}
		}
	}
	else {
		//Get the specified reactantList
		ReactantList *rl = reactantLists[reactantPos];
		int rxnIndex = m->getMoleculeType()->getRxnIndex(this,reactantPos);

		if(rl->getHasClonedMappings()) {
			if(m->getRxnListMappingId(rxnIndex)>=0) {
				rl->removeMappingSet(m->getRxnListMappingId(rxnIndex));
				m->setRxnListMappingId(rxnIndex,Molecule::NOT_IN_RXN);
			}
		}

		//Here we get the standard update...
		if(m->getRxnListMappingId(rxnIndex)>=0) //If we are in this reaction...
		{
			if(!reactantTemplates[reactantPos]->compare(m)) {
				//cout<<"Removing molecule "<<m->getUniqueID()<<" which was at mappingSet: "<<m->getRxnListMappingId(rxnIndex)<<endl;
				rl->removeMappingSet(m->getRxnListMappingId(rxnIndex));
				m->setRxnListMappingId(rxnIndex,Molecule::NOT_IN_RXN);
			}

		} else {
			//Try to map it!
			ms = rl->pushNextAvailableMappingSet();
			if(!reactantTemplates[reactantPos]->compare(m,rl,ms)) {
				//we must remove, if we did not match.  This will also remove
				//everything that was cloned off of the mapping set
				rl->removeMappingSet(ms->getId());
			} else {
				m->setRxnListMappingId(rxnIndex,ms->getId());
			}
		}
	}
	return true;
}
コード例 #4
0
ファイル: DORreaction.cpp プロジェクト: SysSynBio/nfsim
bool DORRxnClass::tryToAdd(Molecule *m, unsigned int reactantPos) {

	//if(DEBUG_MESSAGE)cout<<endl<<endl<<"adding molecule to DORRxnClass"<<endl;
	//if(DEBUG_MESSAGE)m->printDetails();
	if(reactantPos==(unsigned)this->DORreactantIndex) {
	//	if(DEBUG_MESSAGE)cout<<" ... as a DOR"<<endl;
		//cout<<"RxnListMappingId: "<<m->getRxnListMappingId(m->getMoleculeType()->getRxnIndex(this,reactantPos))<<endl;

		// handle the DOR reactant
		int rxnIndex = m->getMoleculeType()->getRxnIndex(this,reactantPos);

		//cout<<"trying to add to the tree:"<<endl;
		//m->printDetails();

		if(reactantTree->getHasClonedMappings()) {
			if(m->getRxnListMappingId(rxnIndex)>=0) {
				//cout<<"removing"<<endl;
				reactantTree->removeMappingSet(m->getRxnListMappingId(rxnIndex));
				m->setRxnListMappingId(rxnIndex,Molecule::NOT_IN_RXN);
			}
		}

		if(m->getRxnListMappingId(rxnIndex)>=0) {
			//if(DEBUG_MESSAGE)cout<<"was in the tree, so checking if we should remove"<<endl;
			if(!reactantTemplates[reactantPos]->compare(m)) {
				//if(DEBUG_MESSAGE)cout<<"removing..."<<endl;
				reactantTree->removeMappingSet(m->getRxnListMappingId(rxnIndex));
				m->setRxnListMappingId(rxnIndex,Molecule::NOT_IN_RXN);
			} else {}
		} else {
			//if(DEBUG_MESSAGE)cout<<"wasn't in the tree, so trying to push and compare"<<endl;
			ms=reactantTree->pushNextAvailableMappingSet();
			if(!reactantTemplates[reactantPos]->compare(m,reactantTree,ms)) {
				//cout<<"shouldn't be in the tree, so we pop"<<endl;
				reactantTree->removeMappingSet(ms->getId());
			} else {
				//cout<<"should be in the tree, so confirm push."<<endl;
				//m->printDetails();
				//ms->printDetails();
				//we are keeping it, so evaluate the function and confirm the push
				double localFunctionValue = this->evaluateLocalFunctions(ms);
				//if(DEBUG_MESSAGE)cout<<"local function value is: "<<localFunctionValue<<endl;
				reactantTree->confirmPush(ms->getId(),localFunctionValue);
				m->setRxnListMappingId(rxnIndex,ms->getId());
			}
		}
	} else {

		//Get the specified reactantList
		ReactantList *rl = reactantLists[reactantPos];
		int rxnIndex = m->getMoleculeType()->getRxnIndex(this,reactantPos);

		if(rl->getHasClonedMappings()) {
			if(m->getRxnListMappingId(rxnIndex)>=0) {
				rl->removeMappingSet(m->getRxnListMappingId(rxnIndex));
				m->setRxnListMappingId(rxnIndex,Molecule::NOT_IN_RXN);
			}
		}

		//Here we get the standard update...
		if(m->getRxnListMappingId(rxnIndex)>=0) //If we are in this reaction...
		{
			if(!reactantTemplates[reactantPos]->compare(m)) {
				//cout<<"Removing molecule "<<m->getUniqueID()<<" which was at mappingSet: "<<m->getRxnListMappingId(rxnIndex)<<endl;
				rl->removeMappingSet(m->getRxnListMappingId(rxnIndex));
				m->setRxnListMappingId(rxnIndex,Molecule::NOT_IN_RXN);
			}

		} else {
			//Try to map it!
			ms = rl->pushNextAvailableMappingSet();
			if(!reactantTemplates[reactantPos]->compare(m,rl,ms)) {
				//we must remove, if we did not match.  This will also remove
				//everything that was cloned off of the mapping set
				rl->removeMappingSet(ms->getId());
			} else {
				m->setRxnListMappingId(rxnIndex,ms->getId());
			}
		}



//		// handle it normally...
//		//if(DEBUG_MESSAGE)cout<<" ... as a normal reactant"<<endl;
//		ReactantList *rl = reactantLists[reactantPos];
//		int rxnIndex = m->getMoleculeType()->getRxnIndex(this,reactantPos);
//		if(m->getRxnListMappingId(rxnIndex)>=0) {
//			if(!reactantTemplates[reactantPos]->compare(m)) {
//				rl->removeMappingSet(m->getRxnListMappingId(rxnIndex));
//				m->setRxnListMappingId(rxnIndex,Molecule::NOT_IN_RXN);
//			}
//		} else {
//			//try to map it.
//			ms = rl->pushNextAvailableMappingSet();
//			if(!reactantTemplates[reactantPos]->compare(m,rl,ms)) {
//				rl->popLastMappingSet();
//				//we just pushed, then popped, so molecule has not changed...
//			} else {
//				m->setRxnListMappingId(rxnIndex,ms->getId());
//			}
//		}
	}
	//if(DEBUG_MESSAGE)cout<<"finished adding"<<endl;
	return true;
}