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()
def create_init_params_file(filename, arguments): """Get the parameters of an sbml file which we think are likely to be 'optimisable' and based on that create an initparams file""" output_filename = utils.change_filename_ext(filename, ".initparams") output_file = open (output_filename, "w") params = init_params_model_file(filename, arguments) for param in params: output_file.write (param.format_init(arguments.factor) + "\n") output_file.close()
def run(): """Perform the banalities of command line processing then get on with the actual work""" parser = create_arguments_parser(True) # Might want to make the type of this 'FileType('r')' parser.add_argument('filenames', metavar='F', nargs='+', help="an sbml file to solve numerically") parser.add_argument('--param-file', action='store', help="Provide a parameter file to override parameters in the sbml file") arguments = parser.parse_args() # stop_time cannot have a default because the optimiser needs to know # if the user has explicitly set the stop time or not, see the # add_argument call for 'stop_time' in create_arguments_parser. # So instead of a default value we check if it has been # set and if not we set it: if not arguments.stop_time: arguments.stop_time = 1.0 initialise_logger(arguments) configuration = arguments for filename in arguments.filenames: try: if not os.path.exists(filename): logging.error("Model file: " + filename + " does not exist") continue solver = get_solver(filename, arguments) solver.initialise_solver() solver.set_parameter_file(arguments.param_file) timecourse = solver.solve_model(configuration) all_columns = timecourse.get_column_names() used_names = utils.get_non_ignored(all_columns, arguments.column, arguments.mcolumn) if timecourse: # First remove any columns which are going to be plotted. for tc_column in all_columns: if tc_column not in used_names: timecourse.remove_column(tc_column) results_filename = utils.change_filename_ext(filename, ".csv") results_file = open(results_filename, "w") timecourse.write_to_file(results_file) results_file.close() if arguments.plot_results: plotcsv.run(argument_strings=[results_filename]) else: logging.error ("solving the model failed, no timeseries to report") continue except SolverError: # We should have already logged the error, now we just wish to # allow ourselves to continue and solve the remaining models continue
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 __init__(self, model_file, cflags_prefix): self.model_file = model_file self.model_exec = utils.change_filename_ext(model_file, ".exe") self.param_filename = None self.cflags_prefix = cflags_prefix
def results_filename (self): """Return a suitable filename for the results contained here in""" model_file = self.model_data.model_file basename = os.path.basename(model_file) result_filename = utils.change_filename_ext(basename, ".csv") return result_filename