def create_list_of_changes(sedml_rtask, settings, ranges, mdl_id): p1_change = sedml_rtask.createTaskChange() p1_change.setTarget("/sbml:sbml/sbml:model/sbml:listOfParameters/sbml:parameter[@id='{0}']" .format(settings['parameterOne']['name'])) p1_change.setRange(ranges[0]) p1_change.setModelReference(mdl_id) p1_change.setMath(libsedml.parseFormula(ranges[0])) if ranges[1] is not None: p2_change = sedml_rtask.createTaskChange() p2_change.setTarget("/sbml:sbml/sbml:model/sbml:listOfParameters/sbml:parameter[@id='{0}']" .format(settings['parameterTwo']['name'])) p2_change.setRange(ranges[1]) p2_change.setModelReference(mdl_id) p2_change.setMath(libsedml.parseFormula(ranges[1]))
def write(cfg, file_name): """ Exports simulation configuration to SED-ML file Args: cfg (:obj:`WCSimulationConfig`): simulation configuration file_name (:obj:`str`): path to write SED-ML file """ # initialize SED-ML document cfg_ml = libsedml.SedDocument() cfg_ml.setLevel(1) cfg_ml.setVersion(2) # write changes mdl = cfg_ml.createModel() for change in cfg.changes: change_ml = mdl.createChangeAttribute() change_ml.setTarget(change.attr_path_to_str()) change_ml.setNewValue(str(change.value)) # write perturbations for perturbation in cfg.perturbations: task_ml = cfg_ml.createRepeatedTask() change_ml = task_ml.createTaskChange() change_ml.setTarget(perturbation.change.attr_path_to_str()) change_ml.setMath( libsedml.parseFormula(str(perturbation.change.value))) if perturbation.end_time and not numpy.isnan( perturbation.end_time): range_ml = task_ml.createUniformRange() range_ml.setStart(perturbation.start_time) range_ml.setEnd(perturbation.end_time) range_ml.setType('linear') else: range_ml = task_ml.createVectorRange() range_ml.setValues([perturbation.start_time]) # simulation (maximum time, step size) sim = cfg_ml.createUniformTimeCourse() sim.setInitialTime(cfg.de_simulation_config.time_init) sim.setOutputStartTime(cfg.de_simulation_config.time_init) sim.setOutputEndTime(cfg.de_simulation_config.time_max) duration = cfg.de_simulation_config.time_max - cfg.de_simulation_config.time_init sim.setNumberOfPoints(int(duration / cfg.ode_time_step)) # simulation algorithm alg = sim.createAlgorithm() # .. todo:: add KISAO term for multi-algorithm method alg.setKisaoID("KISAO_0000352") # hybrid method # random number generator seed, state if cfg.random_seed is not None: param = alg.createAlgorithmParameter() param.setKisaoID("KISAO_0000488") param.setValue(str(cfg.random_seed)) # write to XML file libsedml.writeSedML(cfg_ml, file_name)
def create_data_generator(sedml_doc, name, task_id, code=None, formula=None, symbol=None, target=None): index = sedml_doc.getNumSimulations() dg_id = name if code is None else "{0}_{1}".format(name, code) dg_id += str(index) if not doc_has_data_generator(sedml_doc, dg_id): if formula is None: formula = name if symbol is None and target is None: raise StochSSExportCombineError("You must have a 'symbol' or a 'target' \ for data generators") if symbol is not None and target is not None: raise StochSSExportCombineError("You cannot have a 'symbol' and a 'target' \ in a data generators") # create the Sed-ML data generator sedml_dg = sedml_doc.createDataGenerator() sedml_dg.setId(dg_id) sedml_dg.setName(name) sedml_dg.setMath(libsedml.parseFormula(formula)) # create the Sed-ML data generator variable(s) dg_var = sedml_dg.createVariable() dg_var.setId(name) dg_var.setName(name) dg_var.setTaskReference(task_id) if target is None: dg_var.setSymbol(symbol) else: dg_var.setTarget(target) return dg_id
def main (args): """Usage: create_sedml output-filename """ if (len(args) != 2): print(main.__doc__) sys.exit(1); # create the document doc = libsedml.SedDocument(); doc.setLevel(1); doc.setVersion(1); # create a first model referencing an sbml file model = doc.createModel(); model.setId("model1"); model.setSource("file.xml"); model.setLanguage("urn:sedml:language:sbml"); # create a second model modifying a variable of that other sbml file model = doc.createModel(); model.setId("model2"); model.setSource("model1"); model.setLanguage("urn:sedml:sbml"); # change a paramerter 'k' to 0.1 change = model.createChangeAttribute(); change.setTarget("/sbml:sbml/sbml:model/sbml:listOfParameters/sbml:parameter[@id='k']/@value"); change.setNewValue("0.1"); # remove species 's1' remove = model.createRemoveXML(); remove.setTarget("/sbml:sbml/sbml:model/sbml:listOfSpecies/sbml:species[@id='S1']"); # now for something tricky we want to update the initialConcentration of 'S2' to be # half what it was in the original model compute = model.createComputeChange(); compute.setTarget("/sbml:sbml/sbml:model/sbml:listOfSpecies/sbml:species[@id="S2"]/@initialConcentration"); variable = compute.createVariable(); variable.setId("S2"); variable.setModelReference("model1"); variable.setTarget("/sbml:sbml/sbml:model/sbml:listOfSpecies/sbml:species[@id='S2']"); compute.setMath(libsedml.parseFormula("S2 / 2")); # create simulation tc = doc.createUniformTimeCourse(); tc.setId("sim1"); tc.setInitialTime(0.0); tc.setOutputStartTime(0.0); tc.setOutputEndTime(10.0); tc.setNumberOfPoints(1000); # need to set the correct KISAO Term alg = tc.createAlgorithm(); alg.setKisaoID("KISAO:0000019"); # create a task that uses the simulation and the model above task = doc.createTask(); task.setId("task1"); task.setModelReference("model1"); task.setSimulationReference("sim1"); # add a DataGenerator to hold the output for time dg = doc.createDataGenerator(); dg.setId("time"); dg.setName("time"); var = dg.createVariable(); var.setId("v0"); var.setName("time"); var.setTaskReference("task1"); var.setSymbol("urn:sedml:symbol:time"); dg.setMath(libsedml.parseFormula("v0")); # and one for S1 dg = doc.createDataGenerator(); dg.setId("S1"); dg.setName("S1"); var = dg.createVariable(); var.setId("v1"); var.setName("S1"); var.setTaskReference("task1"); var.setTarget("/sbml:sbml/sbml:model/sbml:listOfSpecies/sbml:species[@id='S1']"); dg.setMath(libsedml.parseFormula("v1")); # add a report report = doc.createReport(); report.setId("r1"); report.setName("report 1"); set = report.createDataSet(); set.setId("ds1"); set.setLabel("time"); set.setDataReference("time"); set = report.createDataSet(); set.setId("ds2"); set.setLabel("S1"); set.setDataReference("S1"); # add a 2d plot plot = doc.createPlot2D(); plot.setId("p1"); plot.setName("S1 Timecourse"); curve = plot.createCurve(); curve.setId("c1"); curve.setName("S1"); curve.setLogX(False); curve.setLogY(False); curve.setXDataReference("time"); curve.setYDataReference("S1"); # add a 3D Plot plot2 = doc.createPlot3D(); plot2.setId("p2"); plot2.setName("dunno"); surf = plot2.createSurface(); surf.setId("surf1"); surf.setName("S1"); surf.setLogX(False); surf.setLogY(False); surf.setLogZ(False); surf.setXDataReference("time"); surf.setYDataReference("S1"); surf.setZDataReference("S1"); # write the document libsedml.writeSedML(doc, args[1]);
def main (args): """Usage: create_sedml output-filename """ if (len(args) != 2): print(main.__doc__) sys.exit(1); # create the document doc = libsedml.SedDocument(); doc.setLevel(1); doc.setVersion(1); # create a first model referencing an sbml file model = doc.createModel(); model.setId("model1"); model.setSource("file.xml"); model.setLanguage("urn:sedml:sbml"); # create a second model modifying a variable of that other sbml file model = doc.createModel(); model.setId("model2"); model.setSource("model1"); model.setLanguage("urn:sedml:sbml"); # change a paramerter 'k' to 0.1 change = model.createChangeAttribute(); change.setTarget("/sbml:sbml/sbml:model/sbml:listOfParameters/sbml:parameter[@id='k']/@value"); change.setNewValue("0.1"); # remove species 's1' remove = model.createRemoveXML(); remove.setTarget("/sbml:sbml/sbml:model/sbml:listOfSpecies/sbml:species[@id='S1']"); # now for something tricky we want to update the initialConcentration of 'S2' to be # half what it was in the original model compute = model.createComputeChange(); compute.setTarget("/sbml:sbml/sbml:model/sbml:listOfSpecies/sbml:species[@id="S2"]/@initialConcentration"); variable = compute.createVariable(); variable.setId("S2"); variable.setModelReference("model1"); variable.setTarget("/sbml:sbml/sbml:model/sbml:listOfSpecies/sbml:species[@id='S2']"); compute.setMath(libsedml.parseFormula("S2 / 2")); # create simulation tc = doc.createUniformTimeCourse(); tc.setId("sim1"); tc.setInitialTime(0.0); tc.setOutputStartTime(0.0); tc.setOutputEndTime(10.0); tc.setNumberOfPoints(1000); # need to set the correct KISAO Term alg = tc.createAlgorithm(); alg.setKisaoID("KISAO:0000019"); # create a task that uses the simulation and the model above task = doc.createTask(); task.setId("task1"); task.setModelReference("model1"); task.setSimulationReference("sim1"); # add a DataGenerator to hold the output for time dg = doc.createDataGenerator(); dg.setId("time"); dg.setName("time"); var = dg.createVariable(); var.setId("v0"); var.setName("time"); var.setTaskReference("task1"); var.setSymbol("urn:sedml:symbol:time"); dg.setMath(libsedml.parseFormula("v0")); # and one for S1 dg = doc.createDataGenerator(); dg.setId("S1"); dg.setName("S1"); var = dg.createVariable(); var.setId("v1"); var.setName("S1"); var.setTaskReference("task1"); var.setTarget("/sbml:sbml/sbml:model/sbml:listOfSpecies/sbml:species[@id='S1']"); dg.setMath(libsedml.parseFormula("v1")); # add a report report = doc.createReport(); report.setId("r1"); report.setName("report 1"); set = report.createDataSet(); set.setId("ds1"); set.setLabel("time"); set.setDataReference("time"); set = report.createDataSet(); set.setId("ds2"); set.setLabel("S1"); set.setDataReference("S1"); # add a 2d plot plot = doc.createPlot2D(); plot.setId("p1"); plot.setName("S1 Timecourse"); curve = plot.createCurve(); curve.setId("c1"); curve.setName("S1"); curve.setLogX(False); curve.setLogY(False); curve.setXDataReference("time"); curve.setYDataReference("S1"); # add a 3D Plot plot2 = doc.createPlot3D(); plot2.setId("p2"); plot2.setName("dunno"); surf = plot2.createSurface(); surf.setId("surf1"); surf.setName("S1"); surf.setLogX(False); surf.setLogY(False); surf.setLogZ(False); surf.setXDataReference("time"); surf.setYDataReference("S1"); surf.setZDataReference("S1"); # write the document libsedml.writeSedML(doc, args[1]);
def create_sedml(filename, simulator, initial_time=0.0, report_output_start=0.0, report_output_end=10, no_of_time_points=101): # create the document doc = libsedml.SedDocument() doc.setLevel(1) doc.setVersion(1) # create a first model referencing an sbml file model = doc.createModel() model.setId(filename) model.setSource(f'{filename}.xml') model.setLanguage("urn:sedml:language:sbml") # create simulation # Hardcoding timepoints to its minimum tc = doc.createUniformTimeCourse() tc.setId(filename) tc.setInitialTime(float(initial_time)) tc.setOutputStartTime(float(report_output_start)) tc.setOutputEndTime(float(report_output_end)) tc.setNumberOfPoints(int(no_of_time_points)) # need to set the correct KISAO Term """ VCell testing use CVODE KISAOID: 0000019 COPASI testing use LSODA KISAOID: 0000088 for basic testing """ alg = tc.createAlgorithm() if simulator == 'copasi': alg.setKisaoID(f'KISAO:0000088') elif simulator == 'vcell': alg.setKisaoID(f'KISAO:0000019') else: print(f"{simulator} is still not supported to generate the SED-ML") # create a task that uses the simulation and the model above task = doc.createTask() task.setId(filename) task.setName(filename) task.setModelReference(filename) task.setSimulationReference(filename) # add a DataGenerator to hold the output for time dg = doc.createDataGenerator() dg.setId("time") dg.setName("time") var = dg.createVariable() var.setId("time") var.setName("time") var.setTaskReference(filename) var.setSymbol("urn:sedml:symbol:time") dg.setMath(libsedml.parseFormula("time")) full_sbml_path = os.path.join(model_files_path, f'{filename}.xml') reader = libsbml.SBMLReader() sbml_model_file = reader.readSBMLFromFile(full_sbml_path) sbml = sbml_model_file.getModel() specie_list = [] for species_sbml in sbml.getListOfSpecies(): species_id = species_sbml.getId() specie_list.append(species_id) # adding a DataGenerator to hold the output for all species from the model for specie in specie_list: # and for species dg = doc.createDataGenerator() dg.setId(f'{specie}') dg.setName(f'{specie}') var = dg.createVariable() var.setId(f'{specie}') var.setName(f'{specie}') var.setTaskReference(filename) var.setTarget( f"/sbml:sbml/sbml:model/sbml:listOfSpecies/sbml:species[@id='{specie}']" ) dg.setMath(libsedml.parseFormula(f"{specie}")) # add a report report = doc.createReport() report.setId(filename) report.setName(filename) set = report.createDataSet() set.setId("time") set.setLabel("time") set.setDataReference("time") for specie in specie_list: set = report.createDataSet() set.setId(f'{specie}') set.setLabel(f'{specie}') set.setDataReference(f'{specie}') # write the document libsedml.writeSedML( doc, os.path.join(sedml_doc_path, simulator, f'{filename}.sedml')) print( f"SED-ML Document created for {simulator} with filename {filename}.sedml" )