示例#1
0
END_TEST


START_TEST (test_Reaction_setId)
{
  const char *id = "J1";


  Reaction_setId(R, id);

  fail_unless( !strcmp(Reaction_getId(R), id) );
  fail_unless( Reaction_isSetId(R) );

  if (Reaction_getId(R) == id)
  {
    fail("Reaction_setId(...) did not make a copy of string.");
  }

  /* Reflexive case (pathological) */
  Reaction_setId(R, Reaction_getId(R));
  fail_unless( !strcmp(Reaction_getId(R), id) );

  Reaction_setId(R, NULL);
  fail_unless( !Reaction_isSetId(R) );

  if (Reaction_getId(R) != NULL)
  {
    fail("Reaction_setId(R, NULL) did not clear string.");
  }
}
void prepare_reversible_fast_reaction(Model_t *m, myReaction *re[], mySpecies *sp[], myParameter *param[], myCompartment *comp[], double sim_time, double dt, double *time, myInitialAssignment *initAssign[], char *time_variant_target_id[], unsigned int num_of_time_variant_targets, timeVariantAssignments *timeVarAssign, allocated_memory *mem, copied_AST *cp_AST){
  unsigned int i;
  unsigned int num_of_reactions = Model_getNumReactions(m);
  ASTNode_t *node, *cp_node1, *cp_node2;
  myASTNode *myNode = NULL;
  myASTNode *copied_myAST[MAX_COPIED_AST];
  unsigned int num_of_copied_myAST = 0;
  for(i=0; i<num_of_reactions; i++){
    if(re[i]->is_fast && re[i]->is_reversible){
      node = (ASTNode_t*)KineticLaw_getMath(Reaction_getKineticLaw(re[i]->origin));
      node = ASTNode_deepCopy(node);
      TRACE(("original math of %s: ", Reaction_getId(re[i]->origin)));
      check_AST(node, NULL);
      /* alter_tree_structure(m, &node, cp_AST); */
      alter_tree_structure(m, &node, NULL, 0, cp_AST);
      set_local_para_as_value(node, Reaction_getKineticLaw(re[i]->origin));
      TRACE(("alterated math of %s : ", Reaction_getId(re[i]->origin)));
      check_AST(node, NULL);
      cp_node1 = ASTNode_deepCopy(node);
      cp_node2 = ASTNode_deepCopy(node);
      /* get products numerator */
      myNode = (myASTNode*)malloc(sizeof(myASTNode));
      copied_myAST[num_of_copied_myAST++] = myNode;
      myNode->origin = cp_node1;
      myNode->parent = NULL;
      myNode->left = NULL;
      myNode->right = NULL;
      myASTNode_create(myNode, cp_node1, copied_myAST, &num_of_copied_myAST);
      re[i]->products_equili_numerator = (equation*)malloc(sizeof(equation));
      TRACE(("target_id is %s\n", Species_getId(re[i]->reactants[0]->mySp->origin)));
      check_AST(cp_node1, NULL);
      _prepare_reversible_fast_reaction(m, myNode, re[i], sp, param, comp, re, sim_time, dt, time, initAssign, time_variant_target_id, num_of_time_variant_targets, timeVarAssign, (char*)Species_getId(re[i]->reactants[0]->mySp->origin), 0, mem);
      /* get reactants numerator */
      myNode = (myASTNode*)malloc(sizeof(myASTNode));
      copied_myAST[num_of_copied_myAST++] = myNode;
      myNode->origin = cp_node2;
      myNode->parent = NULL;
      myNode->left = NULL;
      myNode->right = NULL;
      re[i]->reactants_equili_numerator = (equation*)malloc(sizeof(equation));
      myASTNode_create(myNode, cp_node2, copied_myAST, &num_of_copied_myAST);
      TRACE(("target_id is %s\n", Species_getId(re[i]->products[0]->mySp->origin)));
      check_AST(cp_node2, NULL);
      _prepare_reversible_fast_reaction(m, myNode, re[i], sp, param, comp, re, sim_time, dt, time, initAssign, time_variant_target_id, num_of_time_variant_targets, timeVarAssign, (char*)Species_getId(re[i]->products[0]->mySp->origin), 1, mem);
      myASTNode_free(copied_myAST, num_of_copied_myAST);
    }
  }
}
示例#3
0
/**
 * \fn void SBML_score(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 reactions
 * \param  nbEspeces Number of molecules
 */
void SBML_score(Model_t *mod, pEspeces molecules, pScore result, double *reactions_ratio, int nbReactions, int nbEspeces)
{
  /* Fonction de score */
  int  i, resultat=OK;
  Reaction_t *react=NULL;
  const char *kf=NULL;
  KineticLaw_t *kl=NULL;

  /* Enregistre le score des especes */
  Especes_scoreSpecies(molecules, nbEspeces, result->name, result->quantite);

  /* Enregistre le ratio des reactions */
  for (i = 0; i < (nbReactions); i++) {
      react = Model_getReaction(mod, i);
      if(result->name[nbEspeces+i]==NULL){
          result->name[nbEspeces+i]=(char*)malloc(((int)strlen((char *)Reaction_getId(react))+1)*sizeof(char));
          assert(result->name[nbEspeces+i]!=NULL);
          strcpy(result->name[nbEspeces+i],(char *)Reaction_getId(react));
      }

      /* Recherche si la reaction est etudiee*/
      resultat = SBML_findReaction(result->reaction, result->name[nbEspeces+i], result->nb_reaction);
      /* Recuperation de la loi cinetique sur la */
      kl = Reaction_getKineticLaw(react);
      /* Reaction dont on recherche le bon ratio*/
      if(resultat!=OK) result->quantite[nbEspeces+i]=reactions_ratio[resultat];
      else if(KineticLaw_isSetFormula(kl)){
          kf = KineticLaw_getFormula(kl);
          result->quantite[nbEspeces+i]=SBML_evalExpression(kf);
      }
      else {  /*Sinon on n'utilise pas ces informations*/
          fprintf(stderr,"SBML equation are taken into account, use instead the parameter file\n");
          exit(EXIT_FAILURE);
      }
  }
}
END_TEST


START_TEST (test_Reaction_setId2)
{
  int i = Reaction_setId(R, "cell");

  fail_unless( i == LIBSBML_OPERATION_SUCCESS );
  fail_unless( Reaction_isSetId(R) );
  fail_unless( !strcmp(Reaction_getId(R), "cell" ));

  i = Reaction_setId(R, NULL);

  fail_unless( i == LIBSBML_OPERATION_SUCCESS );
  fail_unless( !Reaction_isSetId(R) );
}
示例#5
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);*/
          }
      }
  }
}
示例#6
0
void
printReactionMath (unsigned int n, Reaction_t *r)
{
  char         *formula;
  KineticLaw_t *kl;


  if (Reaction_isSetKineticLaw(r))
  {
    kl = Reaction_getKineticLaw(r);

    if ( KineticLaw_isSetMath(kl) )
    {
      formula = SBML_formulaToDot( KineticLaw_getMath(kl) );
      fprintf(fout, "subgraph cluster%u {\n", noClusters);
      fprintf(fout, "label=\"Reaction: %s\";\n%s\n", Reaction_getId(r), formula);
      free(formula);
      noClusters++;
    }
  }
}
示例#7
0
int
main (int argc, char* argv[])
{
  unsigned int i,j,errors;
  const char* filename;
  SBMLDocument_t* document;
  Model_t* m;

  if (argc != 2)
  {
    printf("\nUsage: printNotes filename\n\n");
    return 1;
  }

  filename  = argv[1];
  document  = readSBML(filename);

  errors = SBMLDocument_getNumErrors( document);

  printf("\n%s\n\n", filename);

  if(errors > 0)
  {
    SBMLDocument_printErrors(document, stderr);
    SBMLDocument_free(document);
    return errors;
  }


  /* Model */

  m = SBMLDocument_getModel(document);
  printNotes((SBase_t*)m, Model_getId(m));

  for(i=0; i < Model_getNumReactions(m); i++)
  {
    Reaction_t* re = Model_getReaction( m, i);
    printNotes((SBase_t*)re, Reaction_getId(re));

    /* SpeciesReference (Reactant) */

    for(j=0; j < Reaction_getNumReactants( re); j++)
    {
      SpeciesReference_t* rt =  Reaction_getReactant(re, j);
      if (SBase_isSetNotes((SBase_t*) rt)) printf("   ");
      printNotes((SBase_t*)rt, SpeciesReference_getSpecies( rt ) );
    }

    /* SpeciesReference (Product) */

    for(j=0; j < Reaction_getNumProducts( re ); j++)
    {
      SpeciesReference_t* rt = Reaction_getProduct( re, j);
      if (SBase_isSetNotes((SBase_t*) rt)) printf("   ");
      printNotes((SBase_t*)rt, SpeciesReference_getSpecies( rt ) );
    }

    /* ModifierSpeciesReference (Modifiers) */

    for(j=0; j < Reaction_getNumModifiers( re ); j++)
    {
      SpeciesReference_t* md = Reaction_getModifier(re, j);
      if (SBase_isSetNotes((SBase_t*) md)) printf("   ");
      printNotes((SBase_t*)md, SpeciesReference_getSpecies( md ) );
    }

    /* KineticLaw */

    if(Reaction_isSetKineticLaw( re ))
    {
      KineticLaw_t* kl = Reaction_getKineticLaw( re );
      if (SBase_isSetNotes((SBase_t*) kl)) printf("   ");
      printNotes((SBase_t*)kl, "");

      /* Parameter */

      for(j=0; j < KineticLaw_getNumParameters( kl ); j++)
      {
        Parameter_t* pa = KineticLaw_getParameter( kl, j);
        if (SBase_isSetNotes((SBase_t*) pa)) printf("   ");
        printNotes((SBase_t*)pa, Parameter_getId(pa));
      }
    }

  }

  /* Species */

  for(i=0; i < Model_getNumSpecies(m); i++)
  {
    Species_t* sp = Model_getSpecies(m, i);
    printNotes((SBase_t*)sp, Species_getId(sp));
  }

  /* Compartments */

  for(i=0; i < Model_getNumCompartments( m ); i++)
  {
    Compartment_t* sp = Model_getCompartment(m, i);
    printNotes((SBase_t*)sp, Compartment_getId(sp));
  }

  /* FunctionDefinition */

  for(i=0; i < Model_getNumFunctionDefinitions(m); i++)
  {
    FunctionDefinition_t* sp = Model_getFunctionDefinition(m, i);
    printNotes((SBase_t*)sp, FunctionDefinition_getId(sp));
  }

  /* UnitDefinition */

  for(i=0; i < Model_getNumUnitDefinitions(m); i++)
  {
    UnitDefinition_t* sp = Model_getUnitDefinition( m, i);
    printNotes((SBase_t*)sp, UnitDefinition_getId(sp));
  }

  /* Parameter */

  for(i=0; i < Model_getNumParameters( m ); i++)
  {
    Parameter_t* sp = Model_getParameter( m, i);
    printNotes((SBase_t*)sp, Parameter_getId(sp));
  }

  /* Rule */

  for(i=0; i < Model_getNumReactions( m ); i++)
  {
    Rule_t* sp = Model_getRule(m, i);
    printNotes((SBase_t*)sp, "");
  }

  /* InitialAssignment */

  for(i=0; i < Model_getNumInitialAssignments(m); i++)
  {
    InitialAssignment_t* sp = Model_getInitialAssignment(m, i);
    printNotes((SBase_t*)sp, "");
  }

  /* Event */

  for(i=0; i < Model_getNumEvents(m); i++)
  {
    Event_t* sp = Model_getEvent(m, i);
    printNotes((SBase_t*)sp, Event_getId(sp));

    /* Trigger */

    if(Event_isSetTrigger( sp ))
    {
      Trigger_t* tg = Event_getTrigger(sp);
      if (SBase_isSetNotes( (SBase_t*) tg)) printf( "   " );
      printNotes((SBase_t*)tg, "");
    }

    /* Delay */

    if(Event_isSetDelay(sp))
    {
      Delay_t* dl = Event_getDelay(sp);
      if (SBase_isSetNotes( (SBase_t*) dl)) printf( "   " );
      printNotes((SBase_t*) dl, "");
    }

    /* EventAssignment */

    for(j=0; j < Event_getNumEventAssignments(sp); j++)
    {
      EventAssignment_t* ea = Event_getEventAssignment(sp, j);
      if (SBase_isSetNotes( (SBase_t*) ea)) printf( "   " );      
      printNotes((SBase_t*)ea, "");
    }
  }

  /* SpeciesType */

  for(i=0; i < Model_getNumSpeciesTypes(m); i++)
  {
    SpeciesType_t* sp = Model_getSpeciesType(m, i);
    printNotes((SBase_t*)sp, SpeciesType_getId(sp));
  }

  /* Constraints */

  for(i=0; i < Model_getNumConstraints(m); i++)
  {
    Constraint_t* sp = Model_getConstraint(m, i);
    printNotes((SBase_t*)sp, "");
  }

  SBMLDocument_free( document );
  return errors;
}
static int
drawModelTxt(Model_t *m, char *file) {

  Species_t *s;
  Reaction_t *re;
  const ASTNode_t *math;
  SpeciesReference_t *sref;
  ModifierSpeciesReference_t *mref;
  int i,j;
  int reversible;
  char filename[WORDSIZE];
  FILE *f;
  
  sprintf(filename, "%s.dot", file);
  f = fopen(filename, "w");

  fprintf(f ,"digraph reactionnetwork {\n");
  fprintf(f ,"label=\"%s\";\n",
	  Model_isSetName(m) ?
	  Model_getName(m) : (Model_isSetId(m) ? Model_getId(m) : "noId") );
  fprintf(f ,"overlap=scale;\n");
 
  for ( i=0; i<Model_getNumReactions(m); i++ ) {
    
    re = Model_getReaction(m,i);
    reversible = Reaction_getReversible(re);
    
    for ( j=0; j<Reaction_getNumModifiers(re); j++ ) {
      mref = Reaction_getModifier(re,j);
      fprintf(f ,"%s->%s [style=dashed arrowhead=odot];\n",
	      ModifierSpeciesReference_getSpecies(mref), Reaction_getId(re));
    }
    for ( j=0; j<Reaction_getNumReactants(re); j++ ) {
      sref = Reaction_getReactant(re,j);
      fprintf(f ,"%s->%s [label=\"",
	      SpeciesReference_getSpecies(sref), Reaction_getId(re));
      
      if ( (SpeciesReference_isSetStoichiometryMath(sref)) ) {
	math = SpeciesReference_getStoichiometryMath(sref);
	if ( (strcmp(SBML_formulaToString(math),"1") !=
	      0) ) {
	  fprintf(f ,"%s", SBML_formulaToString(math));
	}	
      }
      else {
	if ( SpeciesReference_getStoichiometry(sref) != 1) {
	  fprintf(f ,"%g",SpeciesReference_getStoichiometry(sref));
	}
      }
      if ( reversible == 1 ) {
	fprintf(f ,"\" arrowtail=onormal];\n");
      }
      else {
	fprintf(f ,"\" ];\n");
      }
    }
    for ( j=0; j<Reaction_getNumProducts(re); j++ ) {
      sref = Reaction_getProduct(re,j);
      fprintf(f ,"%s->%s [label=\"",
	      Reaction_getId(re), SpeciesReference_getSpecies(sref));
      if ( (SpeciesReference_isSetStoichiometryMath(sref)) ) {
	math = SpeciesReference_getStoichiometryMath(sref);
	if ( (strcmp(SBML_formulaToString(math),"1") !=
	      0) ) {
	  fprintf(f ,"%s ", SBML_formulaToString(math));
	}
      }
      else {
	if ( SpeciesReference_getStoichiometry(sref) != 1) {
	  fprintf(f ,"%g ",SpeciesReference_getStoichiometry(sref));
	}
      }
      if ( reversible == 1 ) {
	fprintf(f ,"\" arrowtail=onormal];\n");
      }
      else {
	fprintf(f ,"\" ];\n");
      }    

    }
    
  }
  for ( i=0; i<Model_getNumReactions(m); i++ ) {
    re = Model_getReaction(m,i);
    fprintf(f ,"%s [label=\"%s\" shape=box];\n",
	    Reaction_getId(re),
	    Reaction_isSetName(re) ?
	    Reaction_getName(re) : Reaction_getId(re));
  }

  for ( i=0; i<Model_getNumSpecies(m); i++) {
    s = Model_getSpecies(m, i);
    fprintf(f ,"%s [label=\"%s\"];",
	    Species_getId(s),
	    Species_isSetName(s) ? Species_getName(s) : Species_getId(s));
  }  
  fprintf(f ,"}\n");
  return 1;
}
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;

}
static int printXMGReactionTimeCourse ( cvodeData_t *data )
{

  int i, j, k, n;
  double maxY, minY, result;
  
  Model_t *m;
  Reaction_t *r;
  KineticLaw_t *kl;
  ASTNode_t **kls;
  
  odeModel_t *om = data->model;
  cvodeResults_t *results = data->results;

  maxY = 0.0;
  minY = 0.0;

  fprintf(stderr,
	  "Printing time development of reaction fluxes to XMGrace!\n");


  if ( om->m == NULL ) {
    fprintf(stderr, "Error: No reaction model availabe\n");
    return 1;
  }
  else m = om->m;

  if ( openXMGrace(data) > 0 ) {
    fprintf(stderr,  "Error: Couldn't open XMGrace\n");
    return 1;
  }
  
  GracePrintf("yaxis label \"%s\"", "flux [substance/time]");
  if ( Model_isSetName(m) )
    GracePrintf("subtitle \"%s, %s\"", Model_getName(m),
		"reaction flux time courses");
  else if  ( Model_isSetId(m) )
    GracePrintf("subtitle \"%s, %s\"", Model_getId(m),
		"reaction flux time courses");
  else 
    GracePrintf("subtitle \"model has no name, %s/id\"",
		"reaction flux time courses");


  /* print legend */  
  for ( i=0; i<Model_getNumReactions(m); i++ ) {
    r = Model_getReaction(m, i);
    if ( Reaction_isSetName(r) )
      GracePrintf("g0.s%d legend  \"%s: %s \"\n", i+1,
		  Reaction_getId(r), Reaction_getName(r));
    else
      GracePrintf("g0.s%d legend  \"%s \"\n", i+1, Reaction_getId(r));      
  }  
  GracePrintf("legend 1.155, 0.85");
  GracePrintf("legend font 8");
  GracePrintf("legend char size 0.6");

  if(!(kls = (ASTNode_t **)calloc(Model_getNumReactions(m),
				  sizeof(ASTNode_t *)))) 
    fprintf(stderr, "failed!\n");
  
  for ( i=0; i<Model_getNumReactions(m); i++ ) {
    r = Model_getReaction(m, i);
    kl = Reaction_getKineticLaw(r);
    kls[i] = copyAST(KineticLaw_getMath(kl));
    AST_replaceNameByParameters(kls[i], KineticLaw_getListOfParameters(kl));
    AST_replaceConstants(m, kls[i]);
  }
  
  /* evaluate flux for each time point and print to XMGrace */
  
  for ( i=0; i<=results->nout; i++ ) {  
    n = 1;
    /* set time and variable values to values at time[k] */
    data->currenttime = results->time[i];
    for ( j=0; j<data->model->neq; j++ )
      data->value[j] = results->value[j][i];

    /* evaluate kinetic law expressions */
    for ( j=0; j<Model_getNumReactions(m); j++ ) {
      result = evaluateAST(kls[j], data);
      if ( result > maxY ) {
	maxY = result;
	GracePrintf("world ymax %g", 1.25*maxY);
      }
      if ( result < minY ) {
	minY = result;
	GracePrintf("world ymin %g", 1.25*minY);
      }
      GracePrintf("g0.s%d point %g, %g",
		  n, results->time[i], result);
      n++;
    }
  }

  GracePrintf("yaxis tick major %g", 1.25*(fabs(maxY)+fabs(minY))/10);
  GracePrintf("redraw");
  closeXMGrace(data, "flux");

  /* free temporary ASTNodes */
  for ( i=0; i<Model_getNumReactions(m); i++ ) 
    ASTNode_free(kls[i]);
  free(kls);

  return 0;
  
}
void printReactionTimeCourse(cvodeData_t *data, Model_t *m, FILE *f)
{
  int i, j;
  cvodeResults_t *results;
  Reaction_t *r;
  KineticLaw_t *kl;
  ASTNode_t **kls;

  if ( data == NULL || data->results == NULL ) {
    Warn(stderr, "No results, please integrate first.\n");
    return;
  }

#if USE_GRACE
  if ( Opt.Xmgrace == 1 ) {
    printXMGReactionTimeCourse(data);
    return;
  }
#endif  
  
  results = data->results;
  if ( Opt.PrintMessage )
    fprintf(stderr,
	    "\nPrinting time course of the reactions (kinetic laws).\n\n");

  if(!(kls =
       (ASTNode_t **)calloc(Model_getNumReactions(m),
			    sizeof(ASTNode_t *)))) {
    fprintf(stderr, "failed!\n");
  }

  fprintf(f, "#t ");
  for ( i=0; i<Model_getNumReactions(m); i++ ) {
    r = Model_getReaction(m, i);
    kl = Reaction_getKineticLaw(r);
    kls[i] = copyAST(KineticLaw_getMath(kl));
    AST_replaceNameByParameters(kls[i], KineticLaw_getListOfParameters(kl));
    AST_replaceConstants(m, kls[i]);
    fprintf(f, "%s ", Reaction_getId(r));
  }
  fprintf(f, "\n");
  fprintf(f, "##REACTION RATES\n"); 
  for ( i=0; i<=results->nout; ++i ) {
    fprintf(f, "%g ", results->time[i]);
    data->currenttime = results->time[i];
    for ( j=0; j<data->model->neq; j++ ) {
      data->value[j] = results->value[j][i];
    }
    for ( j=0; j<Model_getNumReactions(m); j++ ) {      
      
      fprintf(f, "%g ", evaluateAST(kls[j], data));
    }
    fprintf(f, "\n");
  }
  fprintf(f, "##REACTION RATES\n"); 
  fprintf(f, "#t ");
  for ( i=0; i<Model_getNumReactions(m); i++ ) {
    r = Model_getReaction(m, i);
    fprintf(f, "%s ", Reaction_getId(r));
    ASTNode_free(kls[i]);
  }
  free(kls);
  fprintf(f, "\n");
  fflush(f);
  
#if !USE_GRACE
  if ( Opt.Xmgrace == 1 ) {
    fprintf(stderr,
	    "odeSolver has been compiled without XMGRACE functionality.\n");
    fprintf(stderr,
	    "The requested data have been printed to stdout instead.\n");
  }
#endif
  
}
void printReactions(Model_t *m, FILE *f)
{
  
  int i,j,k;
  Reaction_t *r;
  SpeciesReference_t *sref;
  KineticLaw_t *kl;
  Rule_t *rl;
  AssignmentRule_t *asr;
  AlgebraicRule_t *alr;
  RateRule_t *rr;
  Event_t *e;
  EventAssignment_t *ea;
  Parameter_t *p;
  FunctionDefinition_t *fd;
  SBMLTypeCode_t type;
  const ASTNode_t *math;

  math = NULL;
  
  fprintf(f, "\n");
  for(i=0;i<Model_getNumParameters(m);i++){
    if(i==0)
      fprintf(f, "# Global parameters:\n");
    p = Model_getParameter(m,i);
    if(Parameter_isSetId(p))
      fprintf(f, "%s ",  Parameter_getId(p));
    if(Parameter_isSetName(p))
      fprintf(f, "(%s) ", Parameter_getName(p));
    if(Parameter_isSetValue(p))
      fprintf(f, "= %g; ", Parameter_getValue(p));
    if(Parameter_isSetUnits(p))
      fprintf(f, "[%s]; ", Parameter_getUnits(p));
    if(!Parameter_getConstant(p))
      fprintf(f, "(variable);");
    fprintf(f, "\n");
    
    if ( i==Model_getNumParameters(m)-1 )
      fprintf(f, "\n");
  }

  fprintf(f, "# Reactions:\n");
  for ( i=0; i<Model_getNumReactions(m); i++ ) {    
    r = Model_getReaction(m,i);
  
    fprintf(f, "%s: %s",
	   Reaction_isSetName(r) ? Reaction_getName(r) : Reaction_getId(r),
	   Reaction_getFast(r) ? "(fast)" : "");
    for ( k=0; k<Reaction_getNumReactants(r); k++ ) {
      sref = Reaction_getReactant(r,k);

      if ( SpeciesReference_isSetStoichiometryMath(sref) )	
	fprintf(f, "%s ",
		SBML_formulaToString(\
		SpeciesReference_getStoichiometryMath(sref)));
      else 
	if ( SpeciesReference_getStoichiometry(sref) != 1. )
	  fprintf(f, "%g ",  SpeciesReference_getStoichiometry(sref));
	
      fprintf(f, "%s", SpeciesReference_getSpecies(sref));
      if(k+1<Reaction_getNumReactants(r))
	fprintf(f, "%s", " + ");
    }
    
    fprintf(f, "%s", Reaction_getReversible(r) ? " <-> " : " -> ");
    for ( k=0; k<Reaction_getNumProducts(r); k++ ) {
      sref = Reaction_getProduct(r,k);
      if ( SpeciesReference_isSetStoichiometryMath(sref) )
	fprintf(f, "%s ",
	       SBML_formulaToString(\
		   SpeciesReference_getStoichiometryMath(sref)));
      else
	if ( SpeciesReference_getStoichiometry(sref) != 1. )
	  fprintf(f, "%g ", SpeciesReference_getStoichiometry(sref));
      
      fprintf(f, "%s", SpeciesReference_getSpecies(sref));
      if(k+1<Reaction_getNumProducts(r))
	fprintf(f, "%s", " + ");
    }
    fprintf(f, ";  ");
    if(Reaction_isSetKineticLaw(r)){
      kl = Reaction_getKineticLaw(r);
      math = KineticLaw_getMath(kl);
      fprintf(f, "%s;", SBML_formulaToString(math));
      for(k=0;k<KineticLaw_getNumParameters(kl);k++){
	
	p = KineticLaw_getParameter(kl,k);
	fprintf(f, " %s",  Parameter_getId(p));
	if(Parameter_isSetName(p))
	  fprintf(f, " (%s)", Parameter_getName(p));
	if(Parameter_isSetValue(p))
	  fprintf(f, " = %g", Parameter_getValue(p));
	if(Parameter_isSetUnits(p))
	  fprintf(f, " [%s]", Parameter_getUnits(p));
	if ( !Parameter_getConstant(p) )
	  fprintf(f, " (variable)");
	fprintf(f, ";");
      }
     /*  fprintf(f, "\n"); */
    }else
      fprintf(f, "#   no rate law is set for this reaction.");
    fprintf(f, "\n");
  }
    
  for(i=0;i<Model_getNumRules(m);i++){
    rl = Model_getRule(m,i);
    if ( i == 0 ) {
      fprintf(f, "# Rules:\n");
    }
    type = SBase_getTypeCode((SBase_t *)rl);
     
     
    if ( type == SBML_RATE_RULE ) {
      rr =  (RateRule_t *) rl;
      fprintf(f, " rateRule:       d%s/dt = ", RateRule_getVariable(rr));
    }
    if ( type == SBML_ALGEBRAIC_RULE ) {
      alr = (AlgebraicRule_t *) rl;
      fprintf(f, " algebraicRule:       0 = ");
    }
    if ( type == SBML_ASSIGNMENT_RULE ) {
      asr = (AssignmentRule_t *) rl;
      fprintf(f, " assignmentRule (%s): %s = ",
	     RuleType_toString(AssignmentRule_getType(asr)),
	     AssignmentRule_getVariable(asr));
    }
    if(!Rule_isSetMath(rl)){
      if(Rule_isSetFormula(rl)){
	Rule_setMathFromFormula(rl);
	
      }
    }
    if(Rule_isSetMath(rl))
      fprintf(f, "%s\n", SBML_formulaToString(Rule_getMath(rl)));
	     
  }
  fprintf(f, "\n");

  for(i=0;i<Model_getNumEvents(m);i++){
    if(i==0)
      fprintf(f, "# Events:\n");
    e = Model_getEvent(m,i);
    if(Event_isSetId(e))
      fprintf(f, "%s: ", Event_getId(e));
    if(Event_isSetName(e))
      fprintf(f, "(%s) ", Event_getName(e));   
    if(Event_isSetTrigger(e)) {
      math = Event_getTrigger(e);
      fprintf(f, "trigger: %s\n", SBML_formulaToString(math));
    }
    if(Event_isSetDelay(e))
      fprintf(f, "delay: %s;\n", SBML_formulaToString(Event_getDelay(e)));
    if(Event_isSetTimeUnits(e))
      fprintf(f, "time Units: %s;\n", Event_getTimeUnits(e));
    for(k=0;k<Event_getNumEventAssignments(e);k++){      
      ea = Event_getEventAssignment(e,k);
      if(EventAssignment_isSetVariable(ea))
	fprintf(f, "  event:  %s = %s;\n",
	       EventAssignment_getVariable(ea),
	       EventAssignment_isSetMath(ea) ?
	       SBML_formulaToString(EventAssignment_getMath(ea)) :
	       "# no math set;\n");
    }

    if(i==Model_getNumEvents(m)-1)
       fprintf(f, "\n"); 
  }  


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

    if ( i==0 ) fprintf(f, "# Functions:\n");

    fd = Model_getFunctionDefinition(m,i);
    if ( FunctionDefinition_isSetName(fd) )
      fprintf(f, "%s: ", FunctionDefinition_getName(fd));
    if(FunctionDefinition_isSetId(fd) && FunctionDefinition_isSetMath(fd)){
      fprintf(f, "%s( ", FunctionDefinition_getId(fd));
      math = FunctionDefinition_getMath(fd);
	for(j=0;j<ASTNode_getNumChildren(math)-1;j++){
	  fprintf(f, "%s", SBML_formulaToString(ASTNode_getChild(math, j)));
	  if(j<ASTNode_getNumChildren(math)-2)
	    fprintf(f, ", ");
	  if(j==ASTNode_getNumChildren(math)-2)
	    fprintf(f, ") = ");
	}
      fprintf(f, "%s;", SBML_formulaToString(ASTNode_getRightChild(math)));
    }
    fprintf(f, "\n");
  } 
  
}