Esempio n. 1
0
    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)
Esempio n. 2
0
def main(args):
    """Usage: echo_sedml input-filename output-filename
  """
    if len(args) != 3:
        print (main.__doc__)
        sys.exit(1)

    d = libsedml.readSedML(args[1])
    if d.getErrorLog().getNumFailsWithSeverity(libsedml.LIBSEDML_SEV_ERROR) > 0:
        print d.getErrorLog().toString()
    else:
        libsedml.writeSedML(d, args[2])

    return 0
Esempio n. 3
0
def main(args):
    """Usage: echo_sedml input-filename output-filename
  """
    if len(args) != 3:
        print(main.__doc__)
        sys.exit(1)

    d = libsedml.readSedML(args[1])
    if (d.getErrorLog().getNumFailsWithSeverity(libsedml.LIBSEDML_SEV_ERROR) >
            0):
        print(d.getErrorLog().toString())
    else:
        libsedml.writeSedML(d, args[2])

    return 0
def main (args):
  """Usage: create_sedml2 output-filename
  """
  if (len(args) != 2):
    print(main.__doc__)
    sys.exit(1);

  # create the document
  doc = libsedml.SedDocument();
  doc.setLevel(1);
  doc.setVersion(3);

  # create a data description
  
  ddesc = doc.createDataDescription()
  ddesc.setId('data1')
  ddesc.setName('Oscli Timecourse data')
  ddesc.setSource('foo.numl')
  
  # create data source 
  dsource = ddesc.createDataSource()
  dsource.setId('dataS1')
  
  # create slice 
  slice = dsource.createSlice()
  slice.setReference('SpeciesIds')
  slice.setValue('S1')
  
  # specify mapping
  timeDesc = libsedml.CompositeDescription()
  timeDesc.setIndexType('double')
  timeDesc.setId('time')
  timeDesc.setName('time')
  
  speciesDesc = timeDesc.createCompositeDescription()
  speciesDesc.setIndexType('string')
  speciesDesc.setId('SpeciesIds')
  speciesDesc.setName('SpeciesIds')
  
  concentrationDesc = speciesDesc.createAtomicDescription()
  concentrationDesc.setValueType("double")
  concentrationDesc.setName("Concentrations")
  
  dimDesc = ddesc.createDimensionDescription()
  dimDesc.append(timeDesc)
  
  # write the document
  libsedml.writeSedML(doc, args[1]);
Esempio n. 5
0
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]);
Esempio n. 6
0
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]);
Esempio n. 7
0
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"
    )