Esempio n. 1
0
/**
 * \fn void SBML_reaction(Model_t *mod, pEspeces molecules, Reaction_t *react, int nbEspeces)
 * \author Amine Ghozlane
 * \brief  Simulation of a discrete transision
 * \param  mod Model of the SBML file
 * \param  molecules Struct Especes
 * \param  react Reaction id
 * \param  nbEspeces Number of molecules
 */
void SBML_reaction(Model_t *mod, pEspeces molecules, Reaction_t *react, int nbEspeces)
{
  /* Simulation d'une transision discrete */
  SpeciesReference_t *reactif;
  Species_t *especeId;
  int i, ref = 0;

  /*boucle pour retirer des reactifs*/
  for (i = 0; i < (int)Reaction_getNumReactants(react); i++) {
      /* Indentification du reactif */
      reactif = Reaction_getReactant(react, i);
      especeId = Model_getSpeciesById(mod, SpeciesReference_getSpecies(reactif));
      ref = Especes_find(molecules, Species_getId(especeId), nbEspeces);
      /* Modification de sa quantite */
      Especes_setQuantite(molecules, ref, (Especes_getQuantite(molecules, ref)- SpeciesReference_getStoichiometry(reactif)));
  }

  /*boucle pour ajouter des produits */
  for (i = 0; i < (int)Reaction_getNumProducts(react); i++) {
      /* Indentification du reactif */
      reactif = Reaction_getProduct(react, i);
      especeId = Model_getSpeciesById(mod, SpeciesReference_getSpecies(reactif));
      ref = Especes_find(molecules, Species_getId(especeId), nbEspeces);
      /* Modification de sa quantite */
      Especes_setQuantite(molecules, ref, (Especes_getQuantite(molecules, ref)+ SpeciesReference_getStoichiometry(reactif)));
  }
}
SBML_ODESOLVER_API void CvodeData_initializeValues(cvodeData_t *data)
{
  int i;
  Parameter_t *p;
  Species_t *s;
  Compartment_t *c;
  odeModel_t *om = data->model;
  Model_t *ode = om->simple;

  /* First, fill cvodeData_t  structure with data from
     the derived SBML model  */

  for ( i=0; i<data->nvalues; i++ ) {
    if ( (s = Model_getSpeciesById(ode, om->names[i])) )
      data->value[i] = Species_getInitialConcentration(s);
    else if ( (c = Model_getCompartmentById(ode, om->names[i])) )
      data->value[i] = Compartment_getSize(c);
    else if ((p = Model_getParameterById(ode, om->names[i])) )
      data->value[i] = Parameter_getValue(p);
  }
  /* initialize assigned parameters */
  for ( i=0; i<om->nass; i++ ) 
    data->value[om->neq+i] = evaluateAST(om->assignment[i],data);
  /* set current time to 0 */
  data->currenttime = 0.0;

}
SBML_ODESOLVER_API int IntegratorInstance_updateModel(integratorInstance_t *engine)
{
  int i;
  Species_t *s;
  Compartment_t *c;
  Parameter_t *p;
  
  odeModel_t *om = engine->om;
  cvodeData_t *data = engine->data;
  cvodeResults_t *results = engine->results;

  int nout = results->nout;
  int nvalues = data->nvalues;
  Model_t *m = om->m;

  
  for ( i=0; i<nvalues; i++ ) {
    if ( (s = Model_getSpeciesById(m, om->names[i]))
	 != NULL ) {
      Species_setInitialConcentration(s, results->value[i][nout]);
    }
    else if ( (c = Model_getCompartmentById(m, om->names[i])) != NULL ) {
      Compartment_setSize(c, results->value[i][nout]);
    }
    else if ( (p = Model_getParameterById(m, om->names[i])) !=  NULL ) {
      Parameter_setValue(p, results->value[i][nout]);
    }
    else
      return 0;
  }

  return 1;

}
Esempio n. 4
0
int SBML_checkQuantite(Model_t *mod, Reaction_t *react, int nbEspeces, pEspeces molecules)
{
  /* Determine le nombre de reaction possible a partir de la quantite des reactifs */
  int  ref = 0, i;
  double quantite = 0.0, minStep = 0.0, temp = 0.0;
  SpeciesReference_t *reactif;
  Species_t *especeId;
  /*TODO Il est possible de reprogrammer ca plus proprement */
  /* Recupere la quantite de la premiere molecule */
  reactif = Reaction_getReactant(react, 0);
  especeId = Model_getSpeciesById(mod, SpeciesReference_getSpecies(reactif));
  ref = Especes_find(molecules, Species_getId(especeId), nbEspeces);
  /* Cas ou la quantite de la molecule est egale a zero */
  if ((quantite = Especes_getQuantite(molecules, ref)) <= 0.0) return END;
  /* Calcul du nombre de pas minimum qu'il sera possible d'effectuer */
  /* C'est le nombre minimum d'etat de tous les reactifs qui determine le nombre de pas */
  minStep = floor(quantite / SpeciesReference_getStoichiometry(reactif));

  /*fprintf(stderr,"round species %s minstep %f quantite %f stochiometry %f\n",Species_getId(especeId),round(quantite / SpeciesReference_getStoichiometry(reactif)),quantite,SpeciesReference_getStoichiometry(reactif));
  fprintf(stderr,"ceil species %s minstep %f quantite %f stochiometry %f\n",Species_getId(especeId),ceil(quantite / SpeciesReference_getStoichiometry(reactif)),quantite,SpeciesReference_getStoichiometry(reactif));
  fprintf(stderr,"floor species %s minstep %f quantite %f stochiometry %f\n",Species_getId(especeId),floor(quantite / SpeciesReference_getStoichiometry(reactif)),quantite,SpeciesReference_getStoichiometry(reactif));*/
  /* Cas ou le nombre de pas est egal a 0 */
  if(minStep==0.0) return END;

  /* On calcule pour les autres reactifs */
  for (i = 1; i < (int)Reaction_getNumReactants(react); i++) {
      /* Recupere la quantite d'une molecule */
      reactif = Reaction_getReactant(react, i);
      especeId = Model_getSpeciesById(mod, SpeciesReference_getSpecies(reactif));
      ref = Especes_find(molecules, Species_getId(especeId), nbEspeces);
      quantite= Especes_getQuantite(molecules, ref);
      /* La quantite est egale a 0 */
      if(quantite<=0.0) return END;
      /* On calcule le nombre de pas minimum qu'il sera possible d'effectuer */
      temp = floor(Especes_getQuantite(molecules, ref)/SpeciesReference_getStoichiometry(reactif));
      /*fprintf(stderr,"species %s minstep %f temp %f\n",Species_getId(especeId),minStep,temp);*/
      /*fprintf(stderr,"round species %s minstep %f quantite %f stochiometry %f\n",Species_getId(especeId),round(quantite / SpeciesReference_getStoichiometry(reactif)),quantite,SpeciesReference_getStoichiometry(reactif));
      fprintf(stderr,"ceil species %s minstep %f quantite %f stochiometry %f\n",Species_getId(especeId),ceil(quantite / SpeciesReference_getStoichiometry(reactif)),quantite,SpeciesReference_getStoichiometry(reactif));
      fprintf(stderr,"floor species %s minstep %f quantite %f stochiometry %f\n",Species_getId(especeId),floor(quantite / SpeciesReference_getStoichiometry(reactif)),quantite,SpeciesReference_getStoichiometry(reactif));*/
      /* Cas ou le nombre de pas est egal a 0 */
      if(temp==0.0) return END;
      /* Si le nouveau nombre est inferieur au precedent, on change la valeur de minStep */
      if (minStep > temp) minStep = temp;
  }
  /*fprintf(stderr,"minStep returned %d\n",(int)minStep);*/
  return (int)minStep;
}
static int printXMGLegend(cvodeData_t *data, int nvalues)
{
  int i, found;
  odeModel_t *om = data->model;
  Model_t *m = om->simple;
  Species_t *s;
  Parameter_t *p;
  Compartment_t *c;

  
  for ( i=0; i<nvalues; i++ ) {
    found = 0;
    if ( (s = Model_getSpeciesById(m, om->names[i])) != NULL ) {
      if ( Species_isSetName(s) ) {
	GracePrintf("g0.s%d legend  \"%s: %s\"\n", i+1,
		    om->names[i], Species_getName(s));
	found++;
      }
    }
    else if ( (c = Model_getCompartmentById(m, om->names[i])) ) {
      if ( Compartment_isSetName(c) ) {
	GracePrintf("g0.s%d legend  \"%s: %s\"\n", i+1,
		    om->names[i], Compartment_getName(c));
	found++;
      }
    }
    else if ( (p = Model_getParameterById(m, om->names[i])) ) {
      if ( Parameter_isSetName(p) ) {
	GracePrintf("g0.s%d legend  \"%s: %s\"\n", i+1,
		    om->names[i], Parameter_getName(p));
	found++;
      }
    }
    if ( found == 0 )
      GracePrintf("g0.s%d legend  \"%s\"\n", i+1, om->names[i]);
  }


  GracePrintf("legend 1.155, 0.85");
  GracePrintf("legend font 8");
  GracePrintf("legend char size 0.6");

  return 0;
}
Esempio n. 6
0
/**
 * \fn void SBML_setReactions(Model_t *mod, pEspeces molecules, pScore result, double *reactions_ratio, int nbReactions, int nbEspeces)
 * \author Amine Ghozlane
 * \brief  Alloc memory and initialize the struct Especes
 * \param  mod Model of the SBML file
 * \param  molecules Struct Especes
 * \param  result Struct Score
 * \param  reactions_ratio List of computed reaction ratio
 * \param  nbReactions Number of reaction
 * \param  nbEspeces Number of molecules
 */
void SBML_setReactions(Model_t *mod, pEspeces molecules, pScore result, double *reactions_ratio, int nbReactions, int nbEspeces)
{
  /* Initialisation les reactions auquelles participent chaque espece */
  int ref = 0, i, j, resultat=OK;
  SpeciesReference_t *reactif=NULL;
  Species_t *especeId=NULL;
  Reaction_t *react=NULL;
  const char *kf=NULL;
  /*const ASTNode_t *km;*/
  KineticLaw_t *kl=NULL;

  /* Recherche les reactions ou apparaissent chaque espece */
  for (i = 0; i < nbReactions; i++) {
      react = Model_getReaction(mod, i);
      /* Recherche si la reaction est etudiee */
      resultat = SBML_findReaction(result->reaction,Reaction_getId(react), result->nb_reaction);
      /* Etude des reactifs */
      for (j = 0; j < (int)Reaction_getNumReactants(react); j++) {
          /* Recuperation d'un reactif */
          reactif = Reaction_getReactant(react, j);
          /* Recuperation du nom de l'espece */
          especeId = Model_getSpeciesById(mod, SpeciesReference_getSpecies(reactif));
          /* Recherche la reference de cette molecule dans la struture molecule */
          ref = Especes_find(molecules, Species_getId(especeId), nbEspeces);
          /* Recuperation de la loi cinetique sur la */
          kl = Reaction_getKineticLaw(react);
          /* Reaction dont on recherche le bon ratio */
          if(resultat!=OK){
              Especes_allocReactions(molecules, ref, react, reactions_ratio[resultat]);
          }
          /* Si la loi cinetique est au format : formule on recupere le ratio en question */
          else if (KineticLaw_isSetFormula(kl)) {
              kf = KineticLaw_getFormula(kl);
              Especes_allocReactions(molecules, ref, react, SBML_evalExpression(kf));
          } else { /* Sinon on n'utilise pas ces informations */
              /*km = KineticLaw_getMath(kl);*/
              fprintf(stderr,"SBML equation are taken into account, use instead the parameter file\n");
              /*exit(EXIT_FAILURE);*/
          }
      }
  }
}
Esempio n. 7
0
/*
  setValues: the user can enter a species name and
  change its initial condition (amount or concentration)
*/
static void setValues(Model_t *m) {

  char *species;
  char *newIA;
  char *newIC;
  Species_t *s;

  printf("Please enter the id of the species to change: ");
  species = get_line(stdin);
  species = util_trim(species);
  
  if ( (s = Model_getSpeciesById(m,species) ) ) {      
    printf("\n");
    printf("Id:                    %s\n", Species_getId(s));
    if ( Species_isSetName(s) ) {
      printf("Name:                  %s\n", Species_getName(s));
    }
    if ( Species_isSetInitialAmount(s) ) {
      printf("Initial Amount:        %g", Species_getInitialAmount(s));
    }
    else if (Species_isSetInitialConcentration(s) ) {
      printf("Initial Concentration: %g", Species_getInitialConcentration(s));
    }
   

    if ( Species_getHasOnlySubstanceUnits(s) ) {
      if ( Species_isSetSubstanceUnits(s) ) {
	printf("%s ", Species_getSubstanceUnits(s));
      }
    } else {
      if ( Species_isSetSubstanceUnits(s) ) {
	printf("%s ", Species_getSubstanceUnits(s));
      }
      if ( Species_isSetSpatialSizeUnits(s) ) {
	printf("%s%s", "/", Species_getSpatialSizeUnits(s));
      }
    }
    if ( Species_getHasOnlySubstanceUnits(s) ) {
	printf(" (has only substance units)");
    }
    printf("\n");
    if ( Species_isSetCharge(s) ) {
      printf("Charge: %-10d", Species_getCharge(s));
    }
    printf("\n");   
    printf("%s       ", Species_getBoundaryCondition(s) ?
	   "Species is a Boundary\n" : "\n");
    printf("%s       ", Species_getConstant(s) ?
	   "Species is set constant" : "\n");
    printf("\n");
   
    if ( Species_isSetInitialAmount(s) ) {
      printf("Please enter new initial Amount: ");
      newIA = get_line(stdin);
      newIA = util_trim(newIA);
      Species_setInitialAmount(s, (float) atof(newIA));
    }
    else if ( Species_isSetInitialConcentration(s) ) {
      printf("Please enter new initial Concentration: ");
      newIC = get_line(stdin);
      newIC = util_trim(newIC);
      Species_setInitialConcentration(s, (float) atof(newIC));
    }
  }
  else {
    printf("%s not found.\n", species);
  }
  
}
SBML_ODESOLVER_API int drawModel(Model_t *m, char* file, char *format) {
  
#if !USE_GRAPHVIZ

  SolverError_error(
		    WARNING_ERROR_TYPE,
		    SOLVER_ERROR_NO_GRAPHVIZ,
		    "odeSolver has been compiled without GRAPHIZ functionality. ",
		    "Graphs are printed to stdout in the graphviz' .dot format.");
  drawModelTxt(m, file);
  
#else

  GVC_t *gvc;
  Agraph_t *g;
  Agnode_t *r;
  Agnode_t *s;  
  Agedge_t *e;
  Agsym_t *a;
  Species_t *sp;
  Reaction_t *re;
  const ASTNode_t *math;  
  SpeciesReference_t *sref;
  ModifierSpeciesReference_t *mref;
  char *output[4];
  char *command = "dot";
  char *formatopt;
  char *outfile;
  int i,j;
  int reversible;
  char name[WORDSIZE];
  char label[WORDSIZE];

  /* setting name of outfile */
  ASSIGN_NEW_MEMORY_BLOCK(outfile, strlen(file)+ strlen(format)+7, char, 0);
  sprintf(outfile, "-o%s_rn.%s", file, format);

  /* setting output format */
  ASSIGN_NEW_MEMORY_BLOCK(formatopt, strlen(format)+3, char, 0);
  sprintf(formatopt, "-T%s", format);

  /* construct command-line */
  output[0] = command;
  output[1] = formatopt;
  output[2] = outfile;
  output[3] = NULL;

  /* set up renderer context */
  gvc = (GVC_t *) gvContext();
#if GRAPHVIZ_MAJOR_VERSION == 2 && GRAPHVIZ_MINOR_VERSION < 4
  dotneato_initialize(gvc, 3, output);
#elif GRAPHVIZ_MAJOR_VERSION == 2 && GRAPHVIZ_MINOR_VERSION == 4
  parse_args(gvc, 3, output);
#elif GRAPHVIZ_MAJOR_VERSION == 2 && GRAPHVIZ_MINOR_VERSION >= 6 || GRAPHVIZ_MAJOR_VERSION >= 3
  gvParseArgs(gvc, 3, output);  
#endif  

  g = agopen("G", AGDIGRAPH);
  
  /* avoid overlapping nodes, for graph embedding by neato */ 
  a = agraphattr(g, "overlap", "");
  agxset(g, a->index, "scale");

  for ( i=0; i<Model_getNumReactions(m); i++ ) {

    re = Model_getReaction(m,i);
    reversible = Reaction_getReversible(re);
    sprintf(name, "%s", Reaction_getId(re));
    r = agnode(g,name);
    a = agnodeattr(g, "shape", "ellipse");    
    agxset(r, a->index, "box");
    
    sprintf(label, "%s", Reaction_isSetName(re) ?
	    Reaction_getName(re) : Reaction_getId(re));
    agset(r, "label", label);
    
    sprintf(label, "%s.htm", Reaction_getId(re));
    a = agnodeattr(g, "URL", "");
    agxset(r, a->index, label);
    
    for ( j=0; j<Reaction_getNumModifiers(re); j++ ) {

      mref = Reaction_getModifier(re,j);
      sp = Model_getSpeciesById(m, ModifierSpeciesReference_getSpecies(mref));
      
      sprintf(name,"%s", Species_getId(sp));
      s = agnode(g,name);
      sprintf(label, "%s", Species_isSetName(sp) ? 
	      Species_getName(sp) : Species_getId(sp));
      agset(s, "label", label);

      if ( Species_getBoundaryCondition(sp) ) {
	a = agnodeattr(g, "color", "");
	agxset(s, a->index, "blue");
      }
      if ( Species_getConstant(sp) ) {
	a = agnodeattr(g, "color", "");
	agxset(s, a->index, "green4");
      }

      sprintf(label, "%s.htm", Species_getId(sp));
      a = agnodeattr(g, "URL", "");
      agxset(s, a->index, label);
	
      e = agedge(g,s,r);
      a = agedgeattr(g, "style", "");
      agxset(e, a->index, "dashed");
      a = agedgeattr(g, "arrowhead", "");
      agxset(e, a->index, "odot");
    }

    for ( j=0; j<Reaction_getNumReactants(re); j++ ) {

      sref = Reaction_getReactant(re,j);
      sp = Model_getSpeciesById(m, SpeciesReference_getSpecies(sref));
      
      sprintf(name,"%s", Species_getId(sp));
      s = agnode(g, name);
      sprintf(label, "%s", Species_isSetName(sp) ? 
	      Species_getName(sp) : Species_getId(sp));
      agset(s, "label", label);

      if ( Species_getBoundaryCondition(sp) ) {
	a = agnodeattr(g, "color", "");
	agxset(s, a->index, "blue");
      }
      if ( Species_getConstant(sp) ) {
	a = agnodeattr(g, "color", "");
	agxset(s, a->index, "green4");
      }

      sprintf(label, "%s.htm", Species_getId(sp));
      a = agnodeattr(g, "URL", "");
      agxset(s, a->index, label);
      
      e = agedge(g,s,r);
      a = agedgeattr(g, "label", "");
      
      if ( (SpeciesReference_isSetStoichiometryMath(sref)) ) {
	math = SpeciesReference_getStoichiometryMath(sref);
	if ( (strcmp(SBML_formulaToString(math),"1") !=
	      0) ) {
	  agxset (e, a->index, SBML_formulaToString(math));
	}
      }
      else {
	if ( SpeciesReference_getStoichiometry(sref) != 1 ) {
	  sprintf(name, "%g", SpeciesReference_getStoichiometry(sref));
	  agxset (e, a->index, name);
	}
      }
      if ( reversible == 1 ) {
	a = agedgeattr(g, "arrowtail", "");
	agxset(e, a->index, "onormal");
      }      
    }
    
    for ( j=0; j<Reaction_getNumProducts(re); j++ ) {
      sref = Reaction_getProduct(re,j);
      sp = Model_getSpeciesById(m, SpeciesReference_getSpecies(sref));
      sprintf(name,"%s", Species_getId(sp));
      s = agnode(g,name);
      sprintf(label, "%s", Species_isSetName(sp) ? 
	      Species_getName(sp) : Species_getId(sp));
      agset(s, "label", label);

      if ( Species_getBoundaryCondition(sp) ) {
	a = agnodeattr(g, "color", "");
	agxset(s, a->index, "blue");
      }
      if ( Species_getConstant(sp) ) {
	a = agnodeattr(g, "color", "");
	agxset(s, a->index, "green4");
      }

      sprintf(label, "%s.htm", Species_getId(sp));
      a = agnodeattr(g, "URL", "");
      agxset(s, a->index, label);
            
      e = agedge(g,r,s);
      a = agedgeattr(g, "label", "");
      if ( SpeciesReference_isSetStoichiometryMath(sref) ) {
	math = SpeciesReference_getStoichiometryMath(sref);
	if ( (strcmp(SBML_formulaToString(math),"1") !=
	      0) ) {
	  agxset (e, a->index, SBML_formulaToString(math));
	}
      }
      else {
	if ( SpeciesReference_getStoichiometry(sref) != 1 ) {
	  sprintf(name, "%g",SpeciesReference_getStoichiometry(sref));
	  agxset (e, a->index,name);
	}
      }
      if ( reversible == 1 ) {
	a = agedgeattr(g, "arrowtail", "");
	agxset(e, a->index, "onormal");
      }      
    }   
  }

  /* Compute a layout */
#if GRAPHVIZ_MAJOR_VERSION == 2 && GRAPHVIZ_MINOR_VERSION <= 2
  gvBindContext(gvc, g);
  dot_layout(g);
#elif GRAPHVIZ_MAJOR_VERSION == 2 && GRAPHVIZ_MINOR_VERSION == 4
  gvlayout_layout(gvc, g);
#elif GRAPHVIZ_MAJOR_VERSION == 2 && GRAPHVIZ_MINOR_VERSION >= 6 || GRAPHVIZ_MAJOR_VERSION >= 3
  gvLayoutJobs(gvc, g);
#endif

  /* Write the graph according to -T and -o options */
#if GRAPHVIZ_MAJOR_VERSION == 2 && GRAPHVIZ_MINOR_VERSION <= 2
  dotneato_write(gvc);
#elif GRAPHVIZ_MAJOR_VERSION == 2 && GRAPHVIZ_MINOR_VERSION == 4
  emit_jobs(gvc, g);
#elif GRAPHVIZ_MAJOR_VERSION == 2 && GRAPHVIZ_MINOR_VERSION >= 6 || GRAPHVIZ_MAJOR_VERSION >= 3
  gvRenderJobs(gvc, g);
#endif
  
  /* Clean out layout data */
#if GRAPHVIZ_MAJOR_VERSION == 2 && GRAPHVIZ_MINOR_VERSION <= 2
  dot_cleanup(g);
#elif GRAPHVIZ_MAJOR_VERSION == 2 && GRAPHVIZ_MINOR_VERSION == 4
  gvlayout_cleanup(gvc, g);
#elif GRAPHVIZ_MAJOR_VERSION == 2 && GRAPHVIZ_MINOR_VERSION >= 6 || GRAPHVIZ_MAJOR_VERSION >= 3
  gvFreeLayout(gvc, g);
#endif
  
  /* Free graph structures */
#if GRAPHVIZ_MAJOR_VERSION == 2 && GRAPHVIZ_MINOR_VERSION <= 2
  dot_cleanup(g);
#else
  agclose(g);
#endif

  /* Clean up output file and errors */
#if GRAPHVIZ_MAJOR_VERSION == 2 && GRAPHVIZ_MINOR_VERSION <= 2
  gvFREEcontext(gvc);
  dotneato_eof(gvc);
#elif GRAPHVIZ_MAJOR_VERSION == 2 && GRAPHVIZ_MINOR_VERSION == 4
  dotneato_terminate(gvc);
#elif GRAPHVIZ_MAJOR_VERSION == 2 && GRAPHVIZ_MINOR_VERSION >= 6 || GRAPHVIZ_MAJOR_VERSION >= 3
  gvFreeContext(gvc); 
#endif  

  xfree(formatopt);  
  xfree(outfile);
  
#endif
  
  return 1;

}