Ejemplo n.º 1
0
def output_reaction(output_file, reaction):
    """Output the facile format of an sbml reaction"""

    def names_of_participants(participants):
        """ We would like to get the names of the participants, ie
        reactants, modifiers and products by simply mapping the
        participants to their names. But some of them have different
        stoichiometeries, so this turns a list of participants into
        a list of names, for example if A has a stoich of three then
        it generates A,A,A, such that it can be formatted as: A + A + A.
    """
        return [item for p in participants for item in [p.name] * p.stoich]

    reactant_names = names_of_participants(reaction.reactants)
    # modifiers shouldn't have stoichiometries
    modifier_names = [m.name for m in reaction.modifiers]
    product_names = names_of_participants(reaction.products)
    left = " + ".join(reactant_names + modifier_names)
    right = " + ".join(product_names + modifier_names)
    output_file.write(left)
    output_file.write(" -> ")
    output_file.write(right)
    output_file.write(" ; ")

    output_file.write(format_math_element(reaction.kinetic_law))
    output_file.write("\n")
Ejemplo n.º 2
0
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()
Ejemplo n.º 3
0
def output_system_equation(output_file, species, init_assigns):
  """Output the species and their initial concentrations as
     the system equation
  """

  # This prefix is a little trick to put <*> in between
  # all the component initialisers. We could do this via
  # a "<*>".join(...) but this way is a bit more efficient.
  prefix = ""
  for component in species:
    init_string = component.initial_amount
    if not init_string:
      # This is NOT correct, but I'm not quite sure how to deal with
      # concentrations as of yet.
      init_string = component.initial_conc
    for init_assign in init_assigns:
      if init_assign.variable == component.name:
        init_string = format_math_element(init_assign.expression)
        break

    # Essentially we could check if it is an assignment rule, when
    # a species is defined by an assignment rule essentially it is
    # a variable, rather than actual species in which case we do not
    # want it in the system equation, we should of course though have
    # it in the biopepa file as a dynamic variable
    if init_string != None and init_string != "":
      output_file.write(prefix)
      prefix = "<*> "
      output_file.write(component.ident)
      output_file.write("[" + init_string + "]")
      output_file.write("\n")
Ejemplo n.º 4
0
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()