示例#1
0
bool variesIn(string id, Model* model,  const map<string, vector<double> >& results)
{
  if (variesIn(id, results)) return true;
  Rule* rule = model->getRule(id);
  if (rule != NULL) {
    if (rule->getTypeCode() == SBML_RATE_RULE) return true;
    return variesIn(rule->getMath(), model, results);
  }
  if (!getConstant(id, model, results)) {
    //The variable might be set by an algebraic rule
    for (unsigned long r=0; r<model->getNumRules(); r++) {
      if (model->getRule(r)->getTypeCode() == SBML_ALGEBRAIC_RULE) {
        if (appearsIn(id, model->getRule(r)->getMath()) &&
            variesBesides(id, model->getRule(r)->getMath(), model, results)) return true;
      }
    }
  }
  for (unsigned long e=0; e<model->getNumEvents(); e++) {
    Event* event = model->getEvent(e);
    for (unsigned long ea=0; ea<event->getNumEventAssignments(); ea++) {
      if (event->getEventAssignment(ea)->getVariable() == id) return true;
    }
  }
  Species* species = model->getSpecies(id);
  if (species==NULL) return false;
  if (species->isSetBoundaryCondition() && species->getBoundaryCondition()==true) return false;
  for (unsigned long r=0; r<model->getNumReactions(); r++) {
    Reaction* rxn = model->getReaction(r);
    if (variesIn(id, rxn->getListOfReactants(), results)) return true;
    if (variesIn(id, rxn->getListOfProducts(), results)) return true;
  }
  return false;
}
示例#2
0
文件: crf_saver.cpp 项目: cambDI/camb
void CrfSaver::_writeReactionMolecule (Reaction &reaction, int i)
{
   _atom_stereo_flags = reaction.getInversionArray(i).ptr();
   _bond_rc_flags = reaction.getReactingCenterArray(i).ptr();
   _aam = reaction.getAAMArray(i).ptr();
   _writeMolecule(reaction.getMolecule(i));
}
示例#3
0
文件: crf_saver.cpp 项目: cambDI/camb
void CrfSaver::saveReaction (Reaction &reaction)
{
   _writeReactionInfo(reaction);

   int i;

   _atom_stereo_flags = 0;
   _bond_rc_flags = 0;
   _aam = 0;

   for (i = reaction.reactantBegin(); i < reaction.reactantEnd(); i = reaction.reactantNext(i))
         _writeReactionMolecule(reaction, i);

   for (i = reaction.productBegin(); i < reaction.productEnd(); i = reaction.productNext(i))
         _writeReactionMolecule(reaction, i);

   if (reaction.catalystCount() > 0)
   {
      for (i = reaction.catalystBegin(); i < reaction.catalystEnd(); i = reaction.catalystNext(i))
         _writeReactionMolecule(reaction, i);
   }

   if (_encoder.get() != 0)
      _encoder->finish();
}
示例#4
0
文件: Gro.cpp 项目: fmenol/gro
Value * add_reaction ( std::list<Value *> * args, Scope * s ) {

    World * world = current_gro_program->get_world();

    // args are: reactants, products, rate
    std::list<Value *>::iterator i = args->begin();
    Value * R = *i; i++;
    Value * P = *i; i++;
    Value * k = *i;

    Reaction r (k->num_value());

    for ( i=R->list_value()->begin(); i != R->list_value()->end(); i++ ) {
        if ( (*i)->int_value() < 0 || (*i)->int_value() >= world->num_signals() )
            throw std::string ( "Reaction refers to a non-existant reactant." );
        r.add_reactant( (*i)->int_value() );
    }

    for ( i=P->list_value()->begin(); i != P->list_value()->end(); i++ ) {
        if ( (*i)->int_value() < 0 || (*i)->int_value() >= world->num_signals() )
            throw std::string ( "Reaction refers to a non-existant product." );
        r.add_product( (*i)->int_value() );
    }

    world->add_reaction(r);

    return new Value ( Value::UNIT );

}
void 
AssignmentCycles::addReactionDependencies(const Model& m, const Reaction& object)
{
  unsigned int ns;
  std::string thisId = object.getId();

  /* loop thru the list of names in the Math
    * if they refer to a Reaction, an Assignment Rule
    * or an Initial Assignment add to the map
    * with the variable as key
    */
  List* variables = object.getKineticLaw()->getMath()
                                      ->getListOfNodes( ASTNode_isName );
  for (ns = 0; ns < variables->getSize(); ns++)
  {
    ASTNode* node = static_cast<ASTNode*>( variables->get(ns) );
    string   name = node->getName() ? node->getName() : "";

    if (m.getReaction(name))
    {
      mIdMap.insert(pair<const std::string, std::string>(thisId, name));
    }
    else if (m.getRule(name) && m.getRule(name)->isAssignment())
    {
      mIdMap.insert(pair<const std::string, std::string>(thisId, name));
    }
    else if (m.getInitialAssignment(name))
    {
      mIdMap.insert(pair<const std::string, std::string>(thisId, name));
    }
  }

  delete variables;
}
示例#6
0
/* convert from L1 to L3 */
void 
Model::convertL3ToL1 ()
{
  //
  // Level 3 allows a model to be specified without a Compartment.  However
  // this is not valid in Level 1.  Thus if a L3 model has no Compartment
  // one must be included 
  //
  if (getNumCompartments() == 0)
  {
    createCompartment()->setId(ASSIGNED_COMPARTMENT);

  }
  dealWithModelUnits();
  
  dealWithAssigningL1Stoichiometry(*this, false);
  for (unsigned int i = 0; i < getNumReactions(); i++)
  {
    Reaction *r = getReaction(i);
    if (r->isSetKineticLaw())
    {
      KineticLaw *kl = r->getKineticLaw();
      for (unsigned int j = 0; j < kl->getNumLocalParameters(); j++)
      {
        Parameter *lp = new Parameter(getLevel(), getVersion());
        (*lp) = *(kl->getLocalParameter(j));
        kl->addParameter(lp);
      }
    }
  }
}
示例#7
0
bool getConstant(string id, Model* model,  const map<string, vector<double> >& results)
{
  if (variesIn(id, results)) return false;
  SBase* element = model->getElementBySId(id);
  Compartment* comp = static_cast<Compartment*>(element);
  Species* species = static_cast<Species*>(element);
  Parameter* param = static_cast<Parameter*>(element);
  Reaction* rxn = static_cast<Reaction*>(element);
  SpeciesReference* sr= static_cast<SpeciesReference*>(element);
  switch (element->getTypeCode()) {
  case SBML_COMPARTMENT:
    if (!comp->getConstant()) return false;
    break;
  case SBML_SPECIES:
    if (!species->getConstant()) return false;
    break;
  case SBML_PARAMETER:
    if (!param->getConstant()) return false;
    break;
  case SBML_REACTION:
    if (rxn->isSetKineticLaw()) {
      if (variesIn(rxn->getKineticLaw()->getMath(), model, results)) return false;
    }
    break;
  case SBML_SPECIES_REFERENCE:
    if (!sr->getConstant()) return false;
    break;
  default:
    assert(false); //Uncaught type!
    return true;
    break;
  }
  return true;
}
void ReactionAtomNeighbourhoodCounters::calculate(Reaction &reac) {
   int i;

   _counters.resize(reac.count());

   for (i = reac.begin(); i < reac.end(); i = reac.next(i))
      _counters[i].calculate(reac.getMolecule(i));
}
	unsigned int AtomicCompositionTable::CheckStoichiometry(std::ostream& fOut, const Reaction& reaction, const double epsilon)
	{
		Eigen::VectorXd reactant_side(element_weights_list_.size());
		Eigen::VectorXd product_side(element_weights_list_.size());
		reactant_side.setZero();
		product_side.setZero();
		for(unsigned int i=0;i<reaction.reactant_nu_indices().size();i++)
			for(int j=0;j<reactant_side.size();j++)
				reactant_side(j) += element_coefficients_list_(reaction.reactant_nu_indices()[i],j)*reaction.reactant_nu()[i];
		for(unsigned int i=0;i<reaction.product_nu_indices().size();i++)
			for(int j=0;j<product_side.size();j++)
				product_side(j) += element_coefficients_list_(reaction.product_nu_indices()[i],j)*reaction.product_nu()[i];

		Eigen::VectorXd relative_errors(element_weights_list_.size());
		for(int i=0;i<reactant_side.size();i++)
			relative_errors(i) = std::fabs(reactant_side(i)-product_side(i))/std::max((reactant_side(i)+product_side(i))*0.5, 1.e-32);
		
		for(int i=0;i<reactant_side.size();i++)
			if (relative_errors(i)>epsilon)
			{
				fOut << "Error in reaction stoichiometry" << std::endl;
				fOut << "Atom      Reactants       Products        Rel. error(%)" << std::endl;
				for(int k=0;k<reactant_side.size();k++)
				{
					if (relative_errors(k)*100. > epsilon)
					{
						fOut << std::left << std::setw(10) << element_names_list_[k];
						fOut << std::left << std::setw(16) << reactant_side(k);
						fOut << std::left << std::setw(16) << product_side(k);
						fOut << std::left << std::setw(16) << relative_errors(k)*100.;
						fOut << std::endl;
					}
				}
				return 1;
			}

		double max_relative_errors = relative_errors.maxCoeff();
		if (max_relative_errors>1.e-12)
		{
			fOut << "Warning: the reaction is not perfectly balanced!" << std::endl;
			fOut << "Atom      Reactants       Products        Rel. error(%)" << std::endl;
			for(int k=0;k<reactant_side.size();k++)
			{
				if (relative_errors(k)*100. > 1e-10)
				{
					fOut << std::left << std::setw(10) << element_names_list_[k];
					fOut << std::left << std::setw(16) << reactant_side(k);
					fOut << std::left << std::setw(16) << product_side(k);
					fOut << std::left << std::setw(16) << relative_errors(k)*100.;
					fOut << std::endl;
				}
			}

			return 2;
		}

		return 0;
	}
示例#10
0
void CrfLoader::_loadReactionMolecule (Reaction &reaction, int index, bool have_aam)
{
   _bond_rc_flags = &reaction.getReactingCenterArray(index);
   _atom_stereo_flags = &reaction.getInversionArray(index);
   if (have_aam)
      _aam = &reaction.getAAMArray(index);

   _loadMolecule(reaction.getMolecule(index));
}
示例#11
0
void CrfSaver::_writeReactionInfo (Reaction &reaction)
{
   _output.writeByte(reaction.reactantsCount());
   _output.writeByte(reaction.productsCount());

   byte have_aam = 1;

   _output.writeByte(have_aam);
}
END_TEST

START_TEST ( test_Reaction )
{
  Reaction* r = new Reaction(2, 4);
  
  fail_unless (r->hasRequiredElements());

  delete r;
}
示例#13
0
void checkReactions(Model* model, set<string>& components, set<string>& tests,  const map<string, vector<double> >& results, int type)
{
  if (model->getNumReactions() > 0) {
    components.insert("Reaction");
    for (unsigned int r=0; r<model->getNumReactions(); r++) {
      Reaction* rxn = model->getReaction(r);
      if (rxn->isSetFast() && rxn->getFast()) {
        tests.insert("FastReaction");
      }
      if (rxn->isSetReversible() && rxn->getReversible()) {
        if (type!=1) {
          tests.insert("ReversibleReaction [?]");
        }
      }
      ListOfSpeciesReferences* reactants = rxn->getListOfReactants();
      checkSpeciesRefs(model, reactants, components, tests, results);
      ListOfSpeciesReferences* products = rxn->getListOfProducts();
      checkSpeciesRefs(model, products, components, tests, results);
      if (rxn->isSetKineticLaw()) {
        KineticLaw* kl = rxn->getKineticLaw();
        if (kl->getNumParameters() > 0) {
          tests.insert("LocalParameters");
        }
        if (kl->isSetMath() == false) {
          tests.insert("NoMathML");
        }
      }
    }
  }
}
END_TEST


START_TEST ( test_KineticLaw_parent_create )
{
    Reaction * r = new Reaction(2, 4);
    KineticLaw* kl = r->createKineticLaw();

    fail_unless(r == kl->getParentSBMLObject());

    delete r;
}
示例#15
0
文件: crf_saver.cpp 项目: cambDI/camb
void CrfSaver::_writeReactionInfo (Reaction &reaction)
{
   _output.writePackedUInt(reaction.reactantsCount());
   _output.writePackedUInt(reaction.productsCount());

   byte features = CrfFeatureFlags::CRF_AAM;
   if (reaction.catalystCount() > 0)
      features |= CrfFeatureFlags::CRF_CATALYST;

   _output.writeByte(features);
   if (reaction.catalystCount() > 0)
      _output.writePackedUInt(reaction.catalystCount());
}
END_TEST


START_TEST ( test_Reaction_parent_create )
{
    Model *m = new Model(2, 4);
    Reaction *r = m->createReaction();

    ListOf *lo = m->getListOfReactions();

    fail_unless(lo == m->getReaction(0)->getParentSBMLObject());
    fail_unless(lo == r->getParentSBMLObject());
    fail_unless(m == lo->getParentSBMLObject());
}
END_TEST


START_TEST ( test_KineticLaw_parent_create_model )
{
    Model *m = new Model(2, 4);
    Reaction * r = m->createReaction();
    KineticLaw* kl = r->createKineticLaw();

    fail_unless(r == kl->getParentSBMLObject());
    fail_unless(r == r->getKineticLaw()->getParentSBMLObject());

    delete r;
}
END_TEST

START_TEST ( test_Reaction )
{
  Reaction* r = new Reaction(2, 4);
  
  fail_unless (!(r->hasRequiredAttributes()));

  r->setId("r");

  fail_unless (r->hasRequiredAttributes());

  delete r;
}
END_TEST


START_TEST ( test_SpeciesReference_Reactant_parent_create )
{
    Reaction *r = new Reaction(2, 4);
    SpeciesReference *sr = r->createReactant();

    ListOf *lo = r->getListOfReactants();

    fail_unless(lo == r->getReactant(0)->getParentSBMLObject());
    fail_unless(lo == sr->getParentSBMLObject());
    fail_unless(r == lo->getParentSBMLObject());
}
示例#20
0
END_TEST


START_TEST (test_WriteL3SBML_Reaction_compartment)
{
  const char* expected = "<reaction id=\"r\" reversible=\"false\""
    " fast=\"true\" compartment=\"c\"/>";

  const char* expected1 = "<reaction id=\"r\" reversible=\"false\""
    " fast=\"true\"/>";

  Reaction *r = D->createModel()->createReaction();
  r->setId("r");
  r->setReversible(false);
  r->setFast(true);
  r->setCompartment("c");

  char* sbml = r->toSBML();
  fail_unless( equals(expected, sbml) );
  safe_free(sbml);

  r->unsetCompartment();

  sbml = r->toSBML();
  fail_unless( equals(expected1, sbml) );
  safe_free(sbml);
}
END_TEST


START_TEST ( test_SpeciesReference_Modifier_parent_create )
{
    Reaction *r = new Reaction(2, 4);
    ModifierSpeciesReference *sr = r->createModifier();

    ListOf *lo = r->getListOfModifiers();

    fail_unless(lo == sr->getParentSBMLObject());
    fail_unless(lo == r->getModifier(0)->getParentSBMLObject());
    fail_unless(r == lo->getParentSBMLObject());
}
void 
convertPow(SBMLDocument* doc, 
           bool shouldChangePow, 
           bool inlineCompartmentSizes)
{

  Model* model = SBMLDocument_getModel(doc);
  if (model == NULL)
  {
    return;
  }

  std::map<string, double> compartmentValueMap;
  if (inlineCompartmentSizes)
  {
    for(unsigned int i = 0; i < model->getNumCompartments(); ++i)
    {
      Compartment* c = model->getCompartment(i);
      compartmentValueMap[c->getId()] = c->getSize();
    }
  }

  for(unsigned int i = 0; i < model->getNumReactions(); ++i)
  {
    Reaction* r  = model->getReaction(i);
    KineticLaw* kl = r->getKineticLaw();
    const char* strKineticFormula;
    if (kl == NULL)
    {
      strKineticFormula = "";
    }
    else
    {
      strKineticFormula = KineticLaw_getFormula(kl);
      if (strKineticFormula == NULL)
      {
        continue;
      }
    }

    ASTNode* ast_Node	= SBML_parseFormula (strKineticFormula);
    if (ast_Node != NULL)
    {
      changePow(ast_Node, compartmentValueMap, shouldChangePow);
      kl->setMath (ast_Node);
    }
    delete ast_Node;
  }
}
示例#23
0
END_TEST


START_TEST (test_WriteL3SBML_Reaction_full)
{
  const char* expected = 
    "<reaction id=\"v1\" reversible=\"true\" fast=\"false\">\n"
    "  <listOfReactants>\n"
    "    <speciesReference species=\"x0\"/>\n"
    "  </listOfReactants>\n"
    "  <listOfProducts>\n"
    "    <speciesReference species=\"s1\"/>\n"
    "  </listOfProducts>\n"
    "  <listOfModifiers>\n"
    "    <modifierSpeciesReference species=\"m1\"/>\n"
    "  </listOfModifiers>\n"
    "  <kineticLaw>\n"
    "    <math xmlns=\"http://www.w3.org/1998/Math/MathML\">\n"
    "      <apply>\n"
    "        <divide/>\n"
    "        <apply>\n"
    "          <times/>\n"
    "          <ci> vm </ci>\n"
    "          <ci> s1 </ci>\n"
    "        </apply>\n"
    "        <apply>\n"
    "          <plus/>\n"
    "          <ci> km </ci>\n"
    "          <ci> s1 </ci>\n"
    "        </apply>\n"
    "      </apply>\n"
    "    </math>\n"
    "  </kineticLaw>\n"
    "</reaction>";


  D->createModel();

  Reaction* r = D->getModel()->createReaction();

  r->setId("v1");
  r->setReversible(true);
  r->setFast(false);

  r->createReactant()->setSpecies("x0");
  r->createProduct ()->setSpecies("s1");
  r->createModifier()->setSpecies("m1");

  r->createKineticLaw()->setFormula("(vm * s1)/(km + s1)");

  char* sbml = r->toSBML();
  fail_unless( equals(expected, sbml) );
  safe_free(sbml);
}
END_TEST


START_TEST ( test_KineticLaw_parent_add )
{
    KineticLaw* kl=new KineticLaw(2, 4);

    Reaction * r = new Reaction(2, 4);

    r->setKineticLaw(kl);

    fail_unless(r == r->getKineticLaw()->getParentSBMLObject());

    delete r;
}
END_TEST


START_TEST ( test_SpeciesReference_Product_parent_create_model )
{
    Model *m = new Model(2, 4);
    Reaction *r = m->createReaction();
    SpeciesReference *sr = m->createProduct();

    ListOf *lo = r->getListOfProducts();

    fail_unless(lo == r->getProduct(0)->getParentSBMLObject());
    fail_unless(lo == sr->getParentSBMLObject());
    fail_unless(r == lo->getParentSBMLObject());
}
示例#26
0
void IcrLoader::loadReaction (Reaction &reaction)
{
   char id[3];

   _scanner.readCharsFix(3, id);

   int version = -1;
   if (strncmp(id, IcrSaver::VERSION2, 3) == 0)
      version = 2;
   else if (strncmp(id, IcrSaver::VERSION1, 3) == 0)
      version = 1;
   else
      throw Error("expected '%s' or '%s', got %.*s. Resave your reaction with new format.", 
         IcrSaver::VERSION1, IcrSaver::VERSION2, 3, id);

   char bits = _scanner.readChar();

   bool have_xyz = ((bits & ICM_XYZ) != 0);
   bool have_bond_dirs = ((bits & ICM_BOND_DIRS) != 0);

   CrfLoader loader(_scanner);

   if (have_xyz)
      loader.xyz_scanner = &_scanner;

   loader.version = version;
   loader.loadReaction(reaction);

   if (have_xyz)
      if (!have_bond_dirs)
         reaction.markStereocenterBonds();
}
void ReactionCdxLoader::loadReaction (Reaction &rxn)
{
   rxn.clear();

   QS_DEF(Array<char>, buf);
   _scanner.readAll(buf);
   buf.push(0);
}
示例#28
0
	ReactionTest():
		myReaction(5.0), 
		emptyReaction(5.0),
		calcium("Ca"), 
		carbon("C"), 
		oxygen("O"), 
		calcium_carbonate("CaCO3")
	{
		calcium.SetConcentration(2.0);
		carbon.SetConcentration(3.0);
		oxygen.SetConcentration(5.0);
		calcium_carbonate.SetConcentration(7.0);
		myReaction.AddReactant(calcium);
		myReaction.AddReactant(carbon);
		myReaction.AddReactant(oxygen);
		myReaction.AddProduct(calcium_carbonate);
	};
/** @cond doxygenLibsbmlInternal */
IdList*
SBMLLevelVersionConverter::collectSpeciesReferenceIds()
{
  IdList* srids = new IdList();

  for (unsigned int i = 0; i < mDocument->getModel()->getNumReactions(); i++)
  {
    Reaction *r = mDocument->getModel()->getReaction(i);
    for (unsigned int j = 0; j < r->getNumReactants(); j++)
    {
      if (r->getReactant(j)->isSetId())
      {
        srids->append(r->getReactant(j)->getId());
      }
    }
    for (unsigned int j = 0; j < r->getNumProducts(); j++)
    {
      if (r->getProduct(j)->isSetId())
      {
        srids->append(r->getProduct(j)->getId());
      }
    }
  }

  return srids;
}
示例#30
0
void
Model::convertParametersToLocals(unsigned int level, unsigned int version)
{
  for (unsigned int i = 0; i < getNumReactions(); i++)
  {
    Reaction *r = getReaction(i);
    if (r->isSetKineticLaw())
    {
      KineticLaw *kl = r->getKineticLaw();
      for (unsigned int j = 0; j < kl->getNumParameters(); j++)
      {
        LocalParameter *lp = new LocalParameter(level, version);
        (*lp) = *(kl->getParameter(j));
        kl->addLocalParameter(lp);
      }
    }
  }
}