def convert_file(filename, arguments):
    """Given an sbml file convert it into a corresponding facile model"""
    model = outline_sbml.get_model_from_sbml_file(filename)

    if arguments.output_file:
        eqn_filename = arguments.output_file
    else:
        eqn_filename = utils.change_filename_ext(filename, ".eqn")
    if eqn_filename == "stdout":
        output_file = sys.stdout
    else:
        output_file = open(eqn_filename, "w")

    # We also need to do the same for variables and parameters
    parameters = outline_sbml.get_list_of_parameters(model)
    for param in parameters:
        output_file.write(" ".join(["variable", param.name, "=", param.value, ";\n"]))
    # Reactions
    reactions = outline_sbml.get_list_of_reactions(model)
    for reaction in reactions:
        output_reaction(output_file, reaction)

    output_file.write("\nINIT\n")
    species = outline_sbml.get_list_of_species(model)
    init_assigns = outline_sbml.get_list_of_init_assigns(model)
    for component in species:
        init_string = component.initial_amount
        for init_assign in init_assigns:
            if init_assign.variable == component.name:
                init_string = format_math_element(init_assign.expression)
        output_file.write(component.name + " = " + init_string + " N;\n")

    if eqn_filename != "stdout":
        output_file.close()
Example #2
0
 def params(self):
   """Return the parameters of the associated model, may even involve the
      parsing of the model file if this has not yet occurred.
   """
   if self._params:
     return self._params
   else:
     params = outline_sbml.get_list_of_parameters(self.model)
     self._params = params
     return params
def translate_file(filename, arguments):
  """Given an sbml file convert it into a corresponding Bio-PEPA model"""
  model = outline_sbml.get_model_from_sbml_file(filename)

  if arguments.output_file:
    biopepa_filename = arguments.output_file
  else:
    biopepa_filename = utils.change_filename_ext(filename, ".biopepa")
  if biopepa_filename == "stdout":  
    output_file = sys.stdout
  else:
    output_file = open(biopepa_filename, "w")

  output_file.write("\n\n// Parameters\n")
  # We also need to do the same for variables and parameters
  parameters = outline_sbml.get_list_of_parameters(model)
  for param in parameters:
    output_file.write(" ".join([param.name, "=", param.value, ";\n"]))
    output_file.write("\n")

  # Reactions
  reactions = outline_sbml.get_list_of_reactions(model)

  output_file.write("\n\n// Rate Definitions\n")
  for reaction in reactions:
    output_file.write(reaction.name + " = [ ")
    output_file.write(format_math_element(reaction.kinetic_law))
    output_file.write(" ] ;\n")

  output_file.write("\n\n// Component Definitions\n")

  component_defs = calculate_component_defs(reactions)
  for component_def in component_defs:
    output_file.write(component_def.show_definition())
    output_file.write("\n")
 
  output_file.write("\n// System Equation\n")
  species = outline_sbml.get_list_of_species(model)
  init_assigns = outline_sbml.get_list_of_init_assigns(model)

  output_system_equation(output_file, species, init_assigns)

  if biopepa_filename != "stdout":
    output_file.close()
def convert_file(filename):
  """Given an sbml file convert it into the corresponding UserModel.{ch}
     files for use with sbsi numerics
  """
  dom = xml.dom.minidom.parse(filename)
  model = dom.getElementsByTagName("model")[0]
  species = outline_sbml.get_list_of_species(model)

  model_name = model.getAttribute("name")
  if not model_name:
    model_name = model.getAttribute("id")
  if not model_name:
    model_name = "nameless_model"

  c_file = open ("UserModel.C", "w")
  c_file.write(
  """#include <cmath>
using namespace std;

#include <cModel.h>
#include "UserModel.h"
#define pi M_PI 
  """)

  c_file.write("/**** The model has " + str(len(species)) + 
               " species ****/\n")
  c_file.write("void " + model_name + "::inputModel(){\n")

  # For each species 
  for component in species:
    if component.initial_amount:
      c_file.write ("  setStateByName(\"" + component.ident +
                    "\", " + str(component.initial_amount) + ");\n")
    else:
      c_file.write ("  // Setting the state to zero as it should be\n")
      c_file.write ("  // updated by an initial assignment\n")
      c_file.write ("  setStateByName(\"" + component.ident +
                    "\", 0);\n")

  # We also need to do the same for variables and parameters
  parameters = outline_sbml.get_list_of_parameters(model)
  for param in parameters:
    if param.value:
      c_file.write("  setParamByName(\"" + param.ident +
                   "\", " + param.value + ");\n")
    else:
      c_file.write("  // Parameter's value not set in model file\n")
      c_file.write("  // Presumably set elsewhere, eg initialAssignment\n")
      c_file.write("  setParamByName(\"" + param.ident +
                   "\", 0);\n")
      
   

  c_file.write(
"""
	/*  0th Species with Id A  in main */   
	// Setting the state to zero as it should be 
	// updated by an initial assinment
	setStateByName("A",0);

	/*  1th Species with Id B  in main */   
	// Setting the state to zero as it should be 
	// updated by an initial assinment
	setStateByName("B",0);

	/*  2th Species with Id AB  in main */   
	// Setting the state to zero as it should be 
	// updated by an initial assinment
	setStateByName("AB",0);


	setVarByName("AB",0);

	setParamByName("k1",1);
	setParamByName("k2",1);
	setParamByName("k3",1);
	setParamByName("main",1);
	};
""")


  c_file.write("\n")
  c_file.close()