예제 #1
0
def execute_simulation_experiment(sedml_file, output_directory):
    """ Execute the simulation experiment defined in the given SED-ML document and save the outputs.

    Args:
        sedml_file (:obj:`str`): path to SED-ML document
        output_directory (:obj:`str`): directory to store the outputs of the experiment
    """
    print("Executing the simulation experiment: " + sedml_file +
          "; with output stored in the directory: " + output_directory)

    doc = libsedml.readSedML(sedml_file)

    if doc.getErrorLog().getNumFailsWithSeverity(
            libsedml.LIBSEDML_SEV_WARNING) > 0:
        print(doc.getErrorLog().toString())
        sys.exit(2)

    # The base location to use when resolving relative locations
    model_base = os.path.abspath(os.path.dirname(sedml_file))

    manifest = ExperimentManifest()
    manifest.build(doc, model_base)
    if not manifest.instantiate():
        print("There was an error instantiating the experiment manifest.")
        sys.exit(3)
예제 #2
0
def sedml_to_python(fullPathName):      # full path name to SedML model
    from os.path import basename
    global modelname

    modelName = os.path.splitext(basename(fullPathName))[0]
    extension = os.path.splitext(basename(fullPathName))[1]
    path = fullPathName.rsplit(basename(fullPathName),1)[0]
    class Tee(object):
        def __init__(self, *files):
            self.files = files
        def write(self, obj):
            for f in self.files:
                f.write(obj)

    if extension == ".sedx":
        import unzipy as uz
        zip = zipfile.ZipFile(fullPathName, 'r')
        path = path + modelName
        uz.unZip(path, zip)
        zip.close()
        fullPathName = uz.readManifest(path + "/manifest.xml")
        k = fullPathName.rfind("/")
        fullPathName = fullPathName[k+1:]
        fullPathName = path + "/" + fullPathName;

    sedmlDoc = libsedml.readSedML(fullPathName)
    if sedmlDoc.getErrorLog().getNumFailsWithSeverity(libsedml.LIBSEDML_SEV_ERROR) > 0:
        print sedmlDoc.getErrorLog().toString()
        sys.exit(2)

    import StringIO
    f = StringIO.StringIO()
    original = sys.stdout
    #sys.stdout = Tee(sys.stdout, f)   # output to console and file
    sys.stdout = Tee(f)              # output to file only

    print "# Beginning of generated script"
    print "import roadrunner"
    print ""
    for i in range(0, sedmlDoc.getNumModels()):
        currentModel = sedmlDoc.getModel(i)
        print "# Execute the tasks of model " + currentModel.getName()
        rrName = "rr" + str(i)
        print rrName + " = roadrunner.RoadRunner()"
        generateTasks(rrName, sedmlDoc, currentModel, path)
        print ""
    print "# The Data Generators"
    dataGeneratorsList = []
    for i in range(0, sedmlDoc.getNumModels()):
        currentModel = sedmlDoc.getModel(i)
        generateData(sedmlDoc, currentModel, dataGeneratorsList)
    print "# The Plots"
    generatePlots(sedmlDoc, dataGeneratorsList)
    print "# End of generated script\n"

    contents = f.getvalue()
    sys.stdout = original  # restore print to stdout only
    f.close()
    return contents
예제 #3
0
def main (args):
  """Usage: print_sedml input-filename
  """
  if len(args) != 2:
    print(main.__doc__)
    sys.exit(1)

  doc = libsedml.readSedML(args[1]);
  if ( doc.getErrorLog().getNumFailsWithSeverity(libsedml.LIBSEDML_SEV_ERROR) > 0):  
    print doc.getErrorLog().toString();
    sys.exit(2); 
  
  print 'The document has {0}" simulation(s).'.format(doc.getNumSimulations());
  for i  in range(0, doc.getNumSimulations()):
    current = doc.getSimulation(i);
    if (current.getTypeCode() == libsedml.SEDML_SIMULATION_UNIFORMTIMECOURSE):
      tc = current;
      kisaoid="none"
      if tc.isSetAlgorithm():
		kisaoid=tc.getAlgorithm().getKisaoID()
      print "\tTimecourse id=", tc.getId()," start=",tc.getOutputStartTime()," end=",tc.getOutputEndTime()," numPoints=",tc.getNumberOfPoints()," kisao=",kisaoid,"\n";
    else:
	  print "\tUncountered unknown simulation. ",current.getId(),"\n";
  
  print "\n"
  print "The document has ",doc.getNumModels() , " model(s)." , "\n";
  for i in range(0,doc.getNumModels()):
    current = doc.getModel(i);
    print "\tModel id=" , current.getId() , " language=" , current.getLanguage() , " source=" , current.getSource() , " numChanges=" , current.getNumChanges() , "\n";
  
  print "\n";
  print "The document has " , doc.getNumTasks() , " task(s)." , "\n";
  for i in range(0,doc.getNumTasks()):
    current = doc.getTask(i);
    print "\tTask id=" , current.getId() , " model=" , current.getModelReference() , " sim=" , current.getSimulationReference() , "\n";

  print "\n";
  print "The document has " , doc.getNumDataGenerators() , " datagenerators(s)." , "\n";
  for i in range( 0,  doc.getNumDataGenerators()):
    current = doc.getDataGenerator(i);
    print "\tDG id=" , current.getId() , " math=" , libsedml.formulaToString(current.getMath()) , "\n";
  
  print "\n";
  print "The document has " , doc.getNumOutputs() , " output(s)." , "\n";
  for i in range (0, doc.getNumOutputs()):
    current = doc.getOutput(i);
    tc = current.getTypeCode();
    if tc == libsedml.SEDML_OUTPUT_REPORT:
      r = (current);
      print "\tReport id=" , current.getId() , " numDataSets=" , r.getNumDataSets() , "\n";
    elif tc == libsedml.SEDML_OUTPUT_PLOT2D:
      p = (current);
      print "\tPlot2d id=" , current.getId() , " numCurves=" , p.getNumCurves() , "\n";
    elif tc == libsedml.SEDML_OUTPUT_PLOT3D:
      p = (current);
      print "\tPlot3d id=" , current.getId() , " numSurfaces=" , p.getNumSurfaces() , "\n";
    else: 
      print "\tEncountered unknown output " , current.getId() , "\n";
예제 #4
0
def _check_sedml_submodels(sedml_file, sbml_files,
                           model_name, model_year, model_info):
    """Checks which sbml models of one sedml group belong together"""
    sedml_path = os.path.abspath(sedml_file)
    sedml_doc = libsedml.readSedML(sedml_file)
    sedml_task_models = [task.getModelReference() for task in sedml_doc.getListOfTasks()]

    for sbml_file in sbml_files:
        # we want to find a simulation task for this SBML model
        # first, get the name of the file (identifier in SED-ML)
        sbml_path = os.path.abspath(sbml_file)
        sbml_file_short = (sbml_file.split('/')[-1]).split('.')[0]

        # Find the first task for this model (and warn if no task was found)
        try:
            task_number = sedml_task_models.index(sbml_file_short)
        except ValueError:
            print('SBML file not found in list of tasks: ' + sbml_file_short)
            return model_info

        # get the corresponding simulation setting
        current_sim = sedml_doc.getSimulation(sedml_doc.getTask(
            task_number).getSimulationReference())
        # remember simulation settings
        # out_start = current_sim.getOutputStartTime()
        out_end = current_sim.getOutputEndTime()
        # n_timepoints = current_sim.getNumberOfPoints()

        # get info about the SBML file
        sbml_model = libsbml.readSBML(sbml_file).getModel()
        if sbml_model is None:
            # check for SBML model
            print(f'No sbml model found for file {sbml_file}, '
                  f'in model name f{model_name}, '
                  f'model year {model_year}')
            return model_info
        n_species = len(sbml_model.getListOfSpecies())
        n_reactions = len(sbml_model.getListOfReactions())

        # add model information to the list
        model_info.append({
            'name': model_name,
            'year': model_year,
            'n_species': n_species,
            'n_reactions': n_reactions,
            'sbml_path': sbml_path,
            'sedml_path': sedml_path,
            'regrouped_path': '',   # to be filled later
            'start_time': 0,        # will only use t_end to have a rough timeframe
            'end_time': out_end,
            'n_timepoints': 101,    # will only use t_end to have a rough timeframe
            'long_id': (model_name, model_year, n_species, n_reactions),
            'short_id': '',
            'sedml_task': task_number,
        })

    return model_info
def getTimeCourse(iModel, iFile):
    # important paths
    BioModels_path = './BioModelsDatabase_models'
    xml_path = './sedml_models/' + iModel + '/sbml_models/' + iFile + '.xml'
    sedml_path = './sedml_models/' + iModel + '/' + iModel + '.sedml'
    sbml_path = './sedml_models/' + iModel + '/sbml_models/' + iFile + '.sbml'

    # get time curse depending on type of model
    if os.path.exists(BioModels_path + '/' + iModel):
        sim_start_time, sim_end_time, sim_num_time_points, y_bound = timePointsBioModels(iModel)
        xml_model = libsbml.readSBML(xml_path)
        model_name = xml_model.getModel().getName()
    else:
        sedml_file = libsedml.readSedML(sedml_path)
        sbml_file = libsbml.readSBML(sbml_path)
        model_name = sbml_file.getModel().getName()

        for iTask in range(0, sedml_file.getNumTasks()):
            all_tasks = sedml_file.getTask(iTask)
            tsk_Id = all_tasks.getId()
            task_name = all_tasks.getName()
            task_modRef = all_tasks.getModelReference()
            task_simReference = all_tasks.getSimulationReference()

            # time courses
            try:
                all_simulations = sedml_file.getSimulation(iTask)
                sim_Id = all_simulations.getId()
            except:  # need 'except' clause if more models have same time period
                if all_simulations == None:
                    all_simulations = sedml_file.getSimulation(0)
                    sim_Id = all_simulations.getId()
            try:
                while task_simReference != sim_Id:
                    iTask = iTask + 1
                    all_simulations = sedml_file.getSimulation(iTask)
                    sim_Id = all_simulations.getId()
            except:
                iTask = 0
                while task_simReference != sim_Id:
                    all_simulations = sedml_file.getSimulation(iTask)
                    sim_Id = all_simulations.getId()
                    iTask = iTask + 1

            sim_start_time = all_simulations.getOutputStartTime()
            sim_end_time = all_simulations.getOutputEndTime()
            sim_num_time_points = all_simulations.getNumberOfPoints()

            # replace 'sim_num_time_points' by default value 100 for trajectory comparison in the end
            sim_num_time_points = 100

    return model_name, sim_start_time, sim_end_time, sim_num_time_points
예제 #6
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
예제 #7
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
예제 #8
0
def download_all_sbml_models_for_sedml_model(sedml_file, sbml_folder):
    """
    Download all sbml models used in a sedml model to a subfolder.
    """
    # read sedml file
    sedml_model = libsedml.readSedML(sedml_file)

    # create sbml model folder if not exists
    if not os.path.exists(sbml_folder):
        os.makedirs(sbml_folder)

    # extract sbml entries
    n_sbml_models = sedml_model.getNumModels()
    for i_sbml_model in range(n_sbml_models):
        sbml_entry = sedml_model.getModel(i_sbml_model)
        sbml_id = sbml_entry.getId()
        sbml_url = sbml_entry.getSource()
        sbml_file = sbml_folder + "/" + sbml_id + ".sbml"
        download_sbml_model(sbml_url, sbml_file)
예제 #9
0
파일: cli.py 프로젝트: virtualcell/vcell
def gen_plots_for_sed2d_only(sedml_path, result_out_dir):
    all_plot_curves = {}
    all_report_dataref = {}

    sedml = lsed.readSedML(sedml_path)
    # print(sedml)

    for output in sedml.getListOfOutputs():
        # print(output)
        if type(output) == SedPlot2D:
            all_curves = {}
            for curve in output.getListOfCurves():
                # print(curve)
                all_curves[curve.getId()] = {
                    'x': curve.getXDataReference(),
                    'y': curve.getYDataReference()
                }
            all_plot_curves[output.getId()] = all_curves

    all_plots = dict(all_plot_curves)
    for plot, curve_dat in all_plots.items():
        dims = (12, 8)
        fig, ax = plt.subplots(figsize=dims)
        for curve, data in curve_dat.items():
            df = pd.read_csv(os.path.join(result_out_dir, plot + '.csv'),
                             header=None).T
            df.columns = df.iloc[1]  # use labels
            df.drop(0, inplace=True)
            df.drop(1, inplace=True)
            df.drop(2, inplace=True)
            df.reset_index(inplace=True)
            df.drop('index', axis=1, inplace=True)

            sns.lineplot(x=df[data['x']].astype(float),
                         y=df[data['y']].astype(float),
                         ax=ax,
                         label=curve)
            ax.set_ylabel('')
            #             plt.show()
            plt.savefig(os.path.join(result_out_dir, plot + '.pdf'), dpi=300)
예제 #10
0
파일: cli.py 프로젝트: virtualcell/vcell
def get_all_dataref_and_curves(sedml_path):
    all_plot_curves = {}
    all_report_dataref = {}

    sedml = lsed.readSedML(sedml_path)

    for output in sedml.getListOfOutputs():
        if type(output) == SedPlot2D:
            all_curves = {}
            for curve in output.getListOfCurves():
                all_curves[curve.getId()] = {
                    'x': curve.getXDataReference(),
                    'y': curve.getYDataReference()
                }
            all_plot_curves[output.getId()] = all_curves
        if type(output) == SedReport:
            for dataset in output.getListOfDataSets():

                ######
                if output.getId() in all_report_dataref:

                    all_report_dataref[output.getId()].append({
                        'data_reference':
                        dataset.getDataReference(),
                        'data_label':
                        dataset.getLabel()
                    })
                else:
                    all_report_dataref[output.getId()] = []
                    all_report_dataref[output.getId()].append({
                        'data_reference':
                        dataset.getDataReference(),
                        'data_label':
                        dataset.getLabel()
                    })

    return all_report_dataref, all_plot_curves
예제 #11
0
def getAllObservables(iSEDML, core_iSbml):

    # list of all directories + SBML files
    #list_directory_sedml = sorted(os.listdir(base_path_sedml))

    # create new sbml file with observables
    #for iSEDML in list_directory_sedml:

    # iSEDML = 'kouril2017_fig1a'

    #   list_files = sorted(os.listdir(base_path_sedml + '/' + iSEDML + '/sbml_models'))

    #  for iSBML in list_files:

    # split iSBML
    #core_iSbml, XML = iSBML.split('.')

    #iSEDML = 'bachmann2011'
    #core_iSbml = 'bachmann'

    # important paths
    base_path_sedml = './sedml_models'
    sedml_path = './sedml_models/' + iSEDML + '/' + iSEDML + '.sedml'
    sbml_path = './sedml_models/' + iSEDML + '/sbml_models/' + core_iSbml + '.sbml'
    new_sbml_path = base_path_sedml + '/' + iSEDML + '/sbml_models_with_observables/' + core_iSbml + '_with_observabels.xml'
    if not os.path.exists(sedml_path):
        print('No Observables!')
    else:
        # new folder for all sbml models with observables
        if not os.path.exists(base_path_sedml + '/' + iSEDML +
                              '/sbml_models_with_observables'):
            os.makedirs(base_path_sedml + '/' + iSEDML +
                        '/sbml_models_with_observables')
        # read in sedml file
        sedml_file = libsedml.readSedML(sedml_path)
        # get number of tasks and observables
        num_task = sedml_file.getNumTasks()
        num_obs = sedml_file.getNumDataGenerators()
        # read in sbml model
        reader = libsbml.SBMLReader()
        sbml_file = reader.readSBML(sbml_path)
        sbml_model = sbml_file.getModel()
        for iTask in range(0, num_task):
            task_id = sedml_file.getTask(iTask).getId()

            # create list with all parameter-ids to check for uniqueness
            # all_par_id = []
            almost_all_par_id = []
            '''
            # get all species-Ids
            num_spec = len(model.getListOfSpecies())
            list_spec = []
            for iSpec in range(0, num_spec):
                spec_Id = model.getSpecies(iSpec).getId()
                list_spec.append(spec_Id)

            # get all parameter-Ids
            num_par_old = len(model.getListOfParameters())
            list_par_old = []
            for iPar in range(0, num_par_old):
                par_Id = model.getParameter(iPar).getId()
                list_par_old.append(par_Id)
            '''

            for iObservable in range(0, num_obs):
                # get important formula
                obs_Formula = libsedml.formulaToString(
                    sedml_file.getDataGenerator(iObservable).getMath())
                obs_Id = sedml_file.getDataGenerator(iObservable).getId()
                # SBML_model_Id,Observable_Id = obs_Id.split('_',1)
                new_obs_Id = 'observable_' + obs_Id
                # get variables
                num_var = sedml_file.getDataGenerator(
                    iObservable).getNumVariables()
                list_var_id = []
                for iVar in range(0, num_var):
                    var_Id = sedml_file.getDataGenerator(
                        iObservable).getVariable(iVar).getId()
                    target_Id = sedml_file.getDataGenerator(
                        iObservable).getVariable(iVar).getTarget()
                    if not target_Id == '':
                        try:
                            _, new_var_Id, _ = target_Id.split("'")
                            if var_Id in obs_Formula:
                                obs_Formula = obs_Formula.replace(
                                    var_Id, new_var_Id)
                        except:
                            print(
                                'target_Id can not be splitted / does not need to be splitted!'
                            )
                    else:
                        print('target_Id does not exists!')
                    # only if it was not defined earlier
                    #if not var_Id in list_spec:
                    #   if not var_Id in list_par_old:
                    #      if not var_Id in list_var_id:
                    #         list_var_id.append(var_Id)
                # get right compartment
                #compartment = model.getCompartment(0).getId()
                # get list of parameters
                list_par_id = []
                list_par_value = []
                num_par = sedml_file.getDataGenerator(
                    iObservable).getNumParameters()
                if num_par == 0:
                    print(obs_Id + ' has no parameters as observables!')

                else:
                    for iCount in range(0, num_par):
                        list_par_id.append(
                            sedml_file.getDataGenerator(
                                iObservable).getParameter(iCount).getId())
                        # list_par_value.append(sedml_file.getDataGenerator(iObservable).getParameter(iCount).getValue())
                        # all_par_id.append(sedml_file.getDataGenerator(iObservable).getParameter(iCount).getId())
                        list_par_value.append(
                            sedml_file.getDataGenerator(
                                iObservable).getParameter(iCount).getValue())
                        almost_all_par_id.append(
                            sedml_file.getDataGenerator(
                                iObservable).getParameter(iCount).getId())

                        # check for uniqueness of parameter-ids
                        # for iNum in range(0, len(all_par_id)):
                        #   almost_all_par_id = all_par_id.remove(all_par_id[len(all_par_id) - 1])
                        for iNum in range(0, len(almost_all_par_id)):
                            all_par_id = almost_all_par_id[iNum]
                            almost_all_par_id.remove(
                                almost_all_par_id[len(almost_all_par_id) - 1])
                            last_element = list(all_par_id[len(all_par_id) -
                                                           1])
                            intersection = [
                                i for i in last_element
                                if i in almost_all_par_id
                            ]
                            if len(intersection) != 0:
                                print(
                                    'Two or more parameters have the same Id!')
                                # sys.exit(1)
                # look for correct observables: with target and with task_id == task_ref
                if sedml_file.getDataGenerator(iObservable).getVariable(
                        0).getTarget() != '':
                    # get task reference
                    task_target = sedml_file.getDataGenerator(
                        iObservable).getVariable(0).getTarget()
                    task_ref = sedml_file.getDataGenerator(
                        iObservable).getVariable(0).getTaskReference()
                    if task_id == task_ref:
                        # create formula
                        assignmentRule = sbml_model.createAssignmentRule()
                        assignmentRule.setId(new_obs_Id)
                        assignmentRule.setVariable(new_obs_Id)
                        assignmentRule.setFormula(obs_Formula)
                        # create parameter to formula for observables
                        obs_parameter = sbml_model.createParameter()
                        obs_parameter.setId(new_obs_Id)
                        obs_parameter.setConstant(False)
                        # create parameter to formula for parameters
                        for iCount in range(0, num_par):
                            iPar_id = list_par_id[iCount]
                            iPar_value = list_par_value[iCount]
                            parameter = sbml_model.createParameter()
                            parameter.setId(iPar_id)
                            parameter.setConstant(False)
                            parameter.setValue(iPar_value)
                        # create species to formula
                        #for iCount in range(0, len(list_var_id)):
                        #   iVar_id = list_var_id[iCount]
                        #   variable = model.createSpecies()
                        #   variable.setId(iVar_id)
                        #   variable.setCompartment(compartment)
                        #   variable.setHasOnlySubstanceUnits(False)
                        #   variable.setBoundaryCondition(False)
                        #   variable.setConstant(False)
        libsbml.writeSBMLToFile(sbml_file, new_sbml_path)
def download_jws_reference_trajectory(i_model, sedml_file, jws_model_infos):
    # Read sedml document
    sedml_doc: libsedml.SedDocument = libsedml.readSedML(sedml_file)
    sedml_id = (sedml_file.split('/')[-1]).split('.')[0]

    sbml_file = model_info.loc[i_model, 'sbml_path']
    # The actual sbml id
    sbml_id = (sbml_file.split('/')[-1]).split('.')[0]

    logger.info(f"Downloading {sedml_id} {sbml_id}")

    # Target referencet trajectory file
    ref_folder = os.path.join(DIR_TRAJ_REF_JWS, sedml_id, sbml_id)
    ref_file = os.path.join(ref_folder, 'JWS_simulation.csv')
    if os.path.exists(ref_file):
        # Model has been downloaded already
        return

    # Simulation time points
    start_time = model_info.loc[i_model, 'start_time']
    end_time = model_info.loc[i_model, 'end_time']
    # TODO JWS does not seem to support the definition of n_timepoints
    #n_timepoints = model_info.loc[i_model, 'n_timepoints']

    # Get the used simulation task
    sedml_task: libsedml.SedTask = \
        sedml_doc.getTask(int(model_info.loc[i_model, 'sedml_task']))
    # Short consistency check that sbml model and sedml task fit together
    assert sbml_id == sedml_task.getModelReference()

    # Get sbml model definition in the sedml file, containing a change list
    sedml_sbml_model = sedml_doc.getModel(sedml_task.getModelReference())

    # Iterate over all changes defined in the sedml file simulation task
    #  for the sbml model.
    #  Like in the AMICI simulation, we overwrite species, parameters,
    #  and compartments.
    changes_list = []
    for change in sedml_sbml_model.getListOfChanges():
        # Change target and value
        target = change.getTarget()
        value = change.getNewValue()

        # Target type
        # TODO this could be improved, if expression matching problems occur
        target_type = (target.split('[')[0]).split(':')[4]
        if target_type not in {'species', 'parameter', 'compartment'}:
            logger.warning(
                f"Requesting a {target_type} change in {sbml_file}, "
                "which AMICI does not support. Keeping the change, but "
                "this is likely to cause non-comparable trajectories.")

        # Target id
        target_id = target.split("'")[1]
        changes_list.append(f'{target_id}={value};')

    # Get slug for JWS model simulation (heuristic, but seems to work)
    sbml_model = libsbml.readSBML(sbml_file).getModel()
    sbml_slug = None
    for jws_model_info in jws_model_infos:
        if sbml_model.getName() == jws_model_info['name']:
            sbml_slug = jws_model_info['slug']
            break
    if sbml_slug is None:
        raise ValueError(
            f"Extraction of SBML url for {sedml_id} {sbml_id} failed.")

    # Get Url with all changes
    # <species i>=<amount>
    # <parameter i>=<value>, compartment == parameter (in this case)
    url = f'https://jjj.bio.vu.nl/rest/models/{sbml_slug}' \
          f'/time_evolution?time_start={start_time};time_end={end_time};' \
          'species=all;' + ''.join(changes_list)
    logger.info(f"JWS simulation URL: {url}")

    # Create a temporary file for the json simulation
    _, tmp_json_file = tempfile.mkstemp()
    # JWS sometimes just returns an error output, then just retry
    while True:
        logger.info("Downloading")
        urllib.request.urlretrieve(url, tmp_json_file)
        with open(tmp_json_file, 'r') as f:
            if f.read() != '{"error_msg": "No result was returned."}':
                break

    # Convert to and save as csv file
    os.makedirs(ref_folder, exist_ok=True)
    pd.read_json(tmp_json_file).to_csv(ref_file, sep='\t', index=False)

    # Remove temporary json file
    os.remove(tmp_json_file)
def all_settings(iModel, iFile):

    # insert specific model properties as strings, e.g.:
    base_path_sbml2amici = '../sbml2amici/correct_amici_models'
    base_path_sedml = './sedml_models'
    BioModels_path = './BioModelsDatabase_models'
    benchmark_path = '../benchmark-models/hackathon_contributions_new_data_format'

    # iFile without extension
    #iFile,_ = iFile.split('.',1)

    # run function
    model = load_specific_model(iModel,
                                iFile)  # call function from 'loadModels.py'

    if os.path.exists(
            BioModels_path + '/' + iModel
    ):  #(benchmark_path + '/' + iModel):   #(BioModels_path + '/' + iModel)
        sim_start_time, sim_end_time, sim_num_time_points = timePointsBioModels(
            iModel
        )  #time_array = timePointsBenchmarkModels(iModel, iFile) #sim_start_time, sim_end_time, sim_num_time_points = timePointsBioModels(iModel)                 # call function from 'setTime_BioModels.py'
    else:
        # change parameter and species according to SEDML file
        model = changeValues(model, iModel,
                             iFile)  # call function from 'changeValues.py'

        # open sedml to get tasks + time courses
        sedml_path = './sedml_models/' + iModel + '/' + iModel + '.sedml'

        # tasks
        sedml_file = libsedml.readSedML(sedml_path)

        for iTask in range(0, sedml_file.getNumTasks()):
            all_tasks = sedml_file.getTask(iTask)
            tsk_Id = all_tasks.getId()
            task_name = all_tasks.getName()
            task_modRef = all_tasks.getModelReference()
            task_simReference = all_tasks.getSimulationReference()

            # time courses
            try:
                all_simulations = sedml_file.getSimulation(iTask)
                sim_Id = all_simulations.getId()
            except:  # need 'except' clause if more models have same time period
                if all_simulations == None:
                    all_simulations = sedml_file.getSimulation(0)
                    sim_Id = all_simulations.getId()
            try:
                while task_simReference != sim_Id:
                    iTask = iTask + 1
                    all_simulations = sedml_file.getSimulation(iTask)
                    sim_Id = all_simulations.getId()
            except:
                iTask = 0
                while task_simReference != sim_Id:
                    all_simulations = sedml_file.getSimulation(iTask)
                    sim_Id = all_simulations.getId()
                    iTask = iTask + 1

            sim_start_time = all_simulations.getOutputStartTime()
            sim_end_time = all_simulations.getOutputEndTime()
            sim_num_time_points = all_simulations.getNumberOfPoints()

            # load script 'changeValues'
            # model = changeValues(iModel, iFile)

    # set timepoints for which we want to simulate the model
    model.setTimepoints(
        np.linspace(sim_start_time, sim_end_time, sim_num_time_points)
    )  #model.setTimepoints(time_array)      #(np.linspace(sim_start_time, sim_end_time, sim_num_time_points))

    return model
예제 #14
0
                ###### species
                new_species = []
                for iNumber in range(0, len(time_data)):
                    new_species.append(columns[iDataFrame + counter])
                new_species = pd.Series(new_species)

                ###### measurment + reindex from e.g. [1:14] to [0:13]
                new_measurment = expdata_file[columns[iDataFrame + counter]]
                if str(new_measurment[0]).isdigit() == False:
                    del new_measurment[0]
                    new_measurment.reset_index(inplace=True, drop=True)

                ###### observable parameters
                new_observables = []

                sedml_file = libsedml.readSedML('./sedml_models/' + iModel + '/' + iModel + '.sedml')

                # get number of tasks and observables
                num_task = sedml_file.getNumTasks()
                num_obs = sedml_file.getNumDataGenerators()

                #for iTask in range(0, num_task):                                                                        # each task with task_referance [no tasks needed]
                    #task_id = sedml_file.getTask(iTask).getId()

                # create list with all parameter-ids to check for uniqueness
                almost_all_par_id = []

                # check if parameter in data generators even exist + if not, write pd.Series(NaN)
                all_observables = []
                for iObservable in range(0, num_obs):
                    num_par_all = sedml_file.getDataGenerator(iObservable).getNumParameters()
def compStaTraj_JWS(delete_counter, Algorithm, Iterative, Linear, Tolerances):
    # create new folder for all json files
    folder_path = './Whole_state_trajectory_comparison'
    if not os.path.exists(folder_path):
        os.makedirs(folder_path)
    #if not os.path.exists('./json_folder'):
    #    os.makedirs('./json_folder')

    # set settings for simulation
    algorithm = [2]  #[1, 2]
    if Algorithm != '':
        Index_correct_algorithm = algorithm.index(Algorithm)
        algorithm = algorithm[Index_correct_algorithm:]
    for solAlg in algorithm:

        iterative = [2]  #[1]
        if Iterative != '':
            Index_correct_nonlinsol = iterative.index(Iterative)
            iterative = iterative[Index_correct_nonlinsol:]
        for nonLinSol in iterative:

            linearsol = [6, 7, 8, 9]  #[1]
            if Linear != '':
                Index_correct_linearsol = linearsol.index(Linear)
                linearsol = linearsol[Index_correct_linearsol:]
            for linSol in linearsol:

                Tolerance_combination = [[1e-6, 1e-8], [1e-8, 1e-6],
                                         [1e-8, 1e-16], [1e-16, 1e-8],
                                         [1e-10, 1e-12], [1e-12, 1e-10],
                                         [1e-14, 1e-14]]
                if Tolerances != '':
                    Index_correct_tolerances = Tolerance_combination.index(
                        Tolerances)
                    Tolerance_combination = Tolerance_combination[
                        Index_correct_tolerances:]
                for iTolerance in Tolerance_combination:
                    start = time.time()

                    # split atol and rtol for naming purposes
                    _, atol_exp = str('{:.1e}'.format(
                        iTolerance[0])).split('-')
                    _, rtol_exp = str('{:.1e}'.format(
                        iTolerance[1])).split('-')
                    if len(atol_exp) != 2:
                        atol_exp = '0' + atol_exp
                    if len(rtol_exp) != 2:
                        rtol_exp = '0' + rtol_exp

                    # get name of jws reference
                    url = "https://jjj.bio.vu.nl/rest/models/?format=json"
                    view_source = requests.get(url)
                    json_string = view_source.text
                    json_dictionary = json.loads(json_string)

                    # get all models
                    list_directory_amici = sorted(
                        os.listdir('../sbml2amici/0.10.19_without_correct'))
                    if delete_counter != 0:
                        del list_directory_amici[0:delete_counter]

                    for iMod in range(0, len(list_directory_amici)):

                        iModel = list_directory_amici[iMod]
                        list_files = sorted(
                            os.listdir(
                                '../sbml2amici/0.10.19_without_correct/' +
                                iModel))

                        for iFile in list_files:

                            # important paths
                            json_save_path = folder_path + '/' + f'{solAlg}_{nonLinSol}_{linSol}_{atol_exp}_{rtol_exp}' \
                                             + '/json_files/' + iModel + '/' + iFile
                            sedml_path = './sedml_models/' + iModel + '/' + iModel + '.sedml'
                            sbml_path = './sedml_models/' + iModel + '/sbml_models/' + iFile + '.sbml'
                            BioModels_path = './BioModelsDatabase_models'

                            if os.path.exists(BioModels_path + '/' + iModel):
                                print('Model is not part of JWS-database!')
                            else:
                                # Open SBML file
                                sbml_model = libsbml.readSBML(sbml_path)

                                # get right model reference from sbml model
                                parse_name_model = sbml_model.getModel().getId(
                                )
                                for iCount in range(0, len(json_dictionary)):
                                    parse_name_jws = json_dictionary[iCount][
                                        'slug']
                                    if parse_name_model == parse_name_jws:
                                        model_reference = json_dictionary[
                                            iCount]['slug']
                                        break
                                # elements in json_dictionary are only lower case --- the sbml model has upper case models
                                try:
                                    model_reference
                                except:
                                    wrong_model_name = [
                                        "".join(x)
                                        for _, x in itertools.groupby(
                                            parse_name_model, key=str.isdigit)
                                    ]
                                    if wrong_model_name[0].islower() == False:
                                        correct_model_letters = wrong_model_name[
                                            0].lower()
                                        correct_model_name = correct_model_letters + wrong_model_name[
                                            1]
                                        for iCount in range(
                                                0, len(json_dictionary)):
                                            parse_name_jws = json_dictionary[
                                                iCount]['slug']
                                            if correct_model_name == parse_name_jws:
                                                model_reference = json_dictionary[
                                                    iCount]['slug']
                                                break
                                # check if 'all_settings' works
                                try:
                                    # Get whole model
                                    model = all_settings(iModel, iFile)

                                    # create folder
                                    if not os.path.exists(json_save_path):
                                        os.makedirs(json_save_path)
                                except:
                                    print('Model ' + iModel +
                                          ' extension is missing!')
                                    continue

                                ######### jws simulation
                                # Get time data with num_time_points == 100
                                t_data = model.getTimepoints()
                                sim_start_time = t_data[0]
                                sim_end_time = t_data[len(t_data) - 1]
                                sim_num_time_points = 101
                                model.setTimepoints(
                                    np.linspace(sim_start_time, sim_end_time,
                                                sim_num_time_points))

                                # Open sedml file
                                sedml_model = libsedml.readSedML(sedml_path)

                                # import all changes from SEDML
                                list_of_strings = JWS_changeValues(
                                    iFile, sedml_model)

                                # Get Url with all changes
                                # <species 1>=<amount>
                                # <parameter 1>=<value>, compartment == parameter (in this case)
                                url = 'https://jjj.bio.vu.nl/rest/models/' + model_reference + '/time_evolution?time_end=' + \
                                      str(sim_end_time) + ';species=all;'

                                for iStr in list_of_strings:
                                    url = url + iStr

                                #### Save .json file
                                urllib.request.urlretrieve(
                                    url, json_save_path + '/' + iFile +
                                    '_JWS_simulation.json')

                                #### write as .csv file
                                json_2_csv = pd.read_json(
                                    json_save_path + '/' + iFile +
                                    '_JWS_simulation.json')
                                json_2_csv.to_csv(json_save_path + '/' +
                                                  iFile +
                                                  '_JWS_simulation.csv',
                                                  sep='\t',
                                                  index=False)

                                # open new .csv file
                                tsv_file = pd.read_csv(json_save_path + '/' +
                                                       iFile +
                                                       '_JWS_simulation.csv',
                                                       sep='\t')

                                # columns names of .tsv file
                                column_names = list(tsv_file.columns)
                                column_names.remove('time')
                                del tsv_file['time']

                                ########## model simulation
                                # Create solver instance
                                solver = model.getSolver()

                                # set all settings
                                solver.setAbsoluteTolerance(iTolerance[0])
                                solver.setRelativeTolerance(iTolerance[1])
                                solver.setLinearSolver(linSol)
                                solver.setNonlinearSolverIteration(nonLinSol)
                                solver.setLinearMultistepMethod(solAlg)

                                # set stability flag for Adams-Moulton
                                if solAlg == 1:
                                    solver.setStabilityLimitFlag(False)

                                # Simulate model
                                sim_data = amici.runAmiciSimulation(
                                    model, solver)

                                # print some values
                                for key, value in sim_data.items():
                                    print('%12s: ' % key, value)

                                # Get state trajectory
                                state_trajectory = sim_data['x']

                                # Delete all trajectories for boundary conditions
                                delete_counter = 0
                                all_properties = sbml_model.getModel()
                                for iSpec in range(
                                        0, all_properties.getNumSpecies()):
                                    all_species = all_properties.getSpecies(
                                        iSpec)
                                    if all_species.getBoundaryCondition(
                                    ) == True:
                                        state_trajectory = state_trajectory.transpose(
                                        )
                                        if delete_counter == 0:
                                            state_trajectory = np.delete(
                                                state_trajectory, iSpec, 0)
                                        else:
                                            state_trajectory = np.delete(
                                                state_trajectory,
                                                iSpec - delete_counter, 0)
                                        state_trajectory = state_trajectory.transpose(
                                        )
                                        delete_counter = delete_counter + 1

                                # Convert ndarray 'state-trajectory' to data frame and save it
                                try:
                                    df_state_trajectory = pd.DataFrame(
                                        columns=column_names,
                                        data=state_trajectory)
                                except:
                                    print('Try again for model ' +
                                          list_directory_amici[iMod] + '_' +
                                          iFile)
                                    compStaTraj_JWS(iMod, solAlg, nonLinSol,
                                                    linSol, iTolerance)
                                df_state_trajectory.to_csv(
                                    json_save_path + '/' + iFile +
                                    '_model_simulation.csv',
                                    sep='\t')
                    end = time.time()
                    print(end - start)

                # reset values - linear solver
                Algorithm = ''
                Iterative = ''
                Linear = ''
                Tolerances = ''

            # reset values - nonlinear solver
            Algorithm = ''
            Iterative = ''
            Linear = ''
            Tolerances = ''

        # reset values - solver algorithm
        Algorithm = ''
        Iterative = ''
        Linear = ''
        Tolerances = ''

    print('All combinations have been satisfied!')
    sys.exit()
예제 #16
0
    print(phrasedml.getLastPhrasedError())
print(sedml_str)


# In[2]:

# Create the temporary files and execute the code
import tempfile
f_sbml = tempfile.NamedTemporaryFile(prefix="myModel", suffix=".xml")
f_sbml.write(sbml_str)
f_sbml.flush()
print(f_sbml.name)

f_sedml = tempfile.NamedTemporaryFile(suffix=".sedml")
f_sedml.write(sedml_str)
f_sedml.flush()
print(f_sedml.name)

import libsedml
sedml_doc = libsedml.readSedML(f_sedml.name)
if sedml_doc.getErrorLog().getNumFailsWithSeverity(libsedml.LIBSEDML_SEV_ERROR) > 0:
    print(sedml_doc.getErrorLog().toString())

f_sbml.close()
f_sedml.close()

# Create executable python code sedml with roadrunner
# import tellurium.tesedml as s2p
# py_code = s2p.sedml_to_python(s2p)

예제 #17
0
def _adapt_and_save_model(model_details):
    """
    This routine takes model information from the dataframe created during
    model regrouping, adapts the SBML file, and saves it in the model folder
    """

    # get and create info about the paths
    sbml_file_name = model_details['sbml_path'].split('/')[-1]
    final_folder = os.path.join(DIR_MODELS_REGROUPED, model_details['short_id'])
    sedml_file_name = (model_details['sedml_path'].split('/')[-1]).split('.')[0]
    if sedml_file_name == '':
        final_file_name = os.path.join(final_folder, sbml_file_name)
    else:
        final_file_name = os.path.join(final_folder,
                                       sedml_file_name.replace('-', '_') +
                                       '__' + sbml_file_name)

    # if we have no SED-ML file, the model is a biomodels model consisting of
    # only one SBML sheet. Move this file to the new location
    if model_details['sedml_path'] == '':
        shutil.copy(model_details['sbml_path'], final_file_name)
        return final_file_name

    # if the model is from the JWS model collection, we must apply the changes
    # from the SED-ML file
    sedml_file = libsedml.readSedML(model_details['sedml_path'])
    sedml_task = sedml_file.getTask(model_details['sedml_task'])
    sbml_file = libsbml.readSBML(model_details['sbml_path'])
    sbml_model = sbml_file.getModel()

    # short consistency check that SBML model and SED-ML task fit together
    assert sbml_file_name.split('.')[0] == sedml_task.getModelReference()
    sedml_submodel = sedml_file.getModel(sedml_task.getModelReference())

    logfile = open(os.path.join(DIR_BASE, 'sedml_change.log'), 'a')
    for change in sedml_submodel.getListOfChanges():
        # We iterate over all changes for this model in the SED-ML
        target = change.getTarget()
        value = change.getNewValue()

        # decide for species or parameter
        div = (target.split('[')[0]).split(':')[4]
        target_id = target.split("'", )[1]

        if div == 'species':
            try:
                sbml_model.getSpecies(target_id).initial_concentration = \
                    float(value)
            except AttributeError:
                if sbml_model.getSpecies(target_id) is None:
                    logfile.write(f'Trying to change initial concentration of species '
                          f'{target_id} via SED-ML file, but this species does '
                          f'not exist in the current SBML model '
                          f'({sbml_file_name}). Omitting!\n')
        elif div == 'parameter':
            try:
                sbml_model.getParameter(target_id).value = float(value)
            except AttributeError:
                if sbml_model.getParameter(target_id) is None:
                    logfile.write(f'Trying to change sbml_model parameter {target_id}, '
                          f'via SED-ML file, but this parameter does not exist '
                          f'in the current SBML model ({sbml_file_name}).'
                          f'Omitting!\n')
        elif div == 'compartment':
            sbml_model.getCompartment(target_id).size = float(value)
        else:
            logfile.write(f'Trying to change a {div} in sbml_model '
                  f'{sbml_file_name}, which is currently not supported.'
                  f'Omitting!\n')

    # save changed sbml files and the accompaniying sedml file
    libsbml.writeSBMLToFile(sbml_model.getSBMLDocument(), final_file_name)
    logfile.close()
    return final_file_name
예제 #18
0
def main(args):
    """Usage: print_sedml input-filename
  """
    if len(args) != 2:
        print(main.__doc__)
        sys.exit(1)

    doc = libsedml.readSedML(args[1])
    if (doc.getErrorLog().getNumFailsWithSeverity(libsedml.LIBSEDML_SEV_ERROR)
            > 0):
        print doc.getErrorLog().toString()
        sys.exit(2)

    print 'The document has {0}" simulation(s).'.format(
        doc.getNumSimulations())
    for i in range(0, doc.getNumSimulations()):
        current = doc.getSimulation(i)
        if (current.getTypeCode() ==
                libsedml.SEDML_SIMULATION_UNIFORMTIMECOURSE):
            tc = current
            kisaoid = "none"
            if tc.isSetAlgorithm():
                kisaoid = tc.getAlgorithm().getKisaoID()
            print "\tTimecourse id=", tc.getId(
            ), " start=", tc.getOutputStartTime(
            ), " end=", tc.getOutputEndTime(
            ), " numPoints=", tc.getNumberOfPoints(), " kisao=", kisaoid, "\n"
        else:
            print "\tUncountered unknown simulation. ", current.getId(), "\n"

    print "\n"
    print "The document has ", doc.getNumModels(), " model(s).", "\n"
    for i in range(0, doc.getNumModels()):
        current = doc.getModel(i)
        print "\tModel id=", current.getId(
        ), " language=", current.getLanguage(), " source=", current.getSource(
        ), " numChanges=", current.getNumChanges(), "\n"

    print "\n"
    print "The document has ", doc.getNumTasks(), " task(s).", "\n"
    for i in range(0, doc.getNumTasks()):
        current = doc.getTask(i)
        print "\tTask id=", current.getId(
        ), " model=", current.getModelReference(
        ), " sim=", current.getSimulationReference(), "\n"

    print "\n"
    print "The document has ", doc.getNumDataGenerators(
    ), " datagenerators(s).", "\n"
    for i in range(0, doc.getNumDataGenerators()):
        current = doc.getDataGenerator(i)
        print "\tDG id=", current.getId(), " math=", libsedml.formulaToString(
            current.getMath()), "\n"

    print "\n"
    print "The document has ", doc.getNumOutputs(), " output(s).", "\n"
    for i in range(0, doc.getNumOutputs()):
        current = doc.getOutput(i)
        tc = current.getTypeCode()
        if tc == libsedml.SEDML_OUTPUT_REPORT:
            r = (current)
            print "\tReport id=", current.getId(
            ), " numDataSets=", r.getNumDataSets(), "\n"
        elif tc == libsedml.SEDML_OUTPUT_PLOT2D:
            p = (current)
            print "\tPlot2d id=", current.getId(
            ), " numCurves=", p.getNumCurves(), "\n"
        elif tc == libsedml.SEDML_OUTPUT_PLOT3D:
            p = (current)
            print "\tPlot3d id=", current.getId(
            ), " numSurfaces=", p.getNumSurfaces(), "\n"
        else:
            print "\tEncountered unknown output ", current.getId(), "\n"
예제 #19
0
def plotObservables(iModel, iFile):

    # call function 'getObservables()'
    #getAllObservables(iModel,iFile)

    # create folder for models with observables
    if not os.path.exists('../sbml2amici/amici_models_with_observables'):
        os.makedirs('../sbml2amici/amici_models_with_observables')

    # loop over all models
    #list_directory_sedml = os.listdir('./sedml_models')

    #for iModel in list_directory_sedml:

    #   list_directory_sbml = os.listdir('./sedml_models' + iModel + '/sbml_models')

    #  for iFile in list_directory_sbml:

    if os.path.exists(
            './BioModelsDatabase_models/' + iModel
    ):  # no comparison for observables available, so no simulation needed
        # important paths
        model_output_dir = '../sbml2amici/amici_models_with_observables/' + iModel + '/' + iFile
        model_path = './sedml_models/' + iModel + '/sbml_models_with_observables/' + iFile + '_with_observabels.xml'  ##### needs automatisation

        # sbml2amici import
        sbml_importer = amici.SbmlImporter(model_path)
        sbml_importer.sbml2amici(iModel, model_output_dir, verbose=False)

    else:
        # important paths
        model_output_dir = '../sbml2amici/amici_models_with_observables/' + iModel + '/' + iFile
        model_path = './sedml_models/' + iModel + '/sbml_models_with_observables/' + iFile + '_with_observabels.xml'
        sedml_path = './sedml_models/' + iModel + '/' + iModel + '.sedml'

        # get observables
        sbml_doc = libsbml.readSBML(model_path)
        model = sbml_doc.getModel()
        observables = get_observables(model, False)

        # sbml2amici import
        sbml_importer = amici.SbmlImporter('./sedml_models/' + iModel +
                                           '/sbml_models_with_observables/' +
                                           iFile + '_with_observabels.xml')
        sbml_importer.sbml2amici(iModel,
                                 model_output_dir,
                                 observables=observables,
                                 verbose=False)

        # load specific model
        sys.path.insert(0, os.path.abspath(model_output_dir))
        model_module = importlib.import_module(iModel)
        model = model_module.getModel()

        # set time points
        sedml_file = libsedml.readSedML(sedml_path)
        for iTask in range(0, sedml_file.getNumTasks()):
            all_tasks = sedml_file.getTask(iTask)
            tsk_Id = all_tasks.getId()
            task_name = all_tasks.getName()
            task_modRef = all_tasks.getModelReference()
            task_simReference = all_tasks.getSimulationReference()

            # time courses
            try:
                all_simulations = sedml_file.getSimulation(iTask)
                sim_Id = all_simulations.getId()
            except:  # need 'except' clause if more models have same time period
                if all_simulations == None:
                    all_simulations = sedml_file.getSimulation(0)
                    sim_Id = all_simulations.getId()
            try:
                while task_simReference != sim_Id:
                    iTask = iTask + 1
                    all_simulations = sedml_file.getSimulation(iTask)
                    sim_Id = all_simulations.getId()
            except:
                iTask = 0
                while task_simReference != sim_Id:
                    all_simulations = sedml_file.getSimulation(iTask)
                    sim_Id = all_simulations.getId()
                    iTask = iTask + 1

            sim_start_time = all_simulations.getOutputStartTime()
            sim_end_time = all_simulations.getOutputEndTime()
            sim_num_time_points = all_simulations.getNumberOfPoints()

        model.setTimepoints(
            np.linspace(sim_start_time, sim_end_time, sim_num_time_points))

        # print observables
        #print("Model observables:   ", model.getObservableIds())

        # get solver instance
        #solver = model.getSolver()

        # simulate model once
        #sim_data = amici.runAmiciSimulation(model,solver)

        # np.set_printoptions(threshold=8, edgeitems=2)
        #for key, value in sim_data.items():
        #   print('%12s: ' % key, value)

        # plot observable trajectories
        #amici.plotting.plotObservableTrajectories(sim_data)
        # amici.plotting.plotStateTrajectories(sim_data)

        # show plot
        #plt.show()

        return model
예제 #20
0
def sedml_to_python(fullPathName):  # full path name to SedML model
    from os.path import basename
    global modelname

    modelName = os.path.splitext(basename(fullPathName))[0]
    extension = os.path.splitext(basename(fullPathName))[1]
    path = fullPathName.rsplit(basename(fullPathName), 1)[0]

    class Tee(object):
        def __init__(self, *files):
            self.files = files

        def write(self, obj):
            for f in self.files:
                f.write(obj)

    if extension == ".sedx":
        import unzipy as uz
        zip = zipfile.ZipFile(fullPathName, 'r')
        path = path + modelName
        uz.unZip(path, zip)
        zip.close()
        fullPathName = uz.readManifest(path + "/manifest.xml")
        k = fullPathName.rfind("/")
        fullPathName = fullPathName[k + 1:]
        fullPathName = path + "/" + fullPathName

    sedmlDoc = libsedml.readSedML(fullPathName)
    if sedmlDoc.getErrorLog().getNumFailsWithSeverity(
            libsedml.LIBSEDML_SEV_ERROR) > 0:
        print sedmlDoc.getErrorLog().toString()
        sys.exit(2)

    import StringIO
    f = StringIO.StringIO()
    original = sys.stdout
    #sys.stdout = Tee(sys.stdout, f)   # output to console and file
    sys.stdout = Tee(f)  # output to file only

    print "# Translated SED-ML"
    print "# Beginning of generated script"
    print "import roadrunner"
    print "import numpy as np"
    print "import matplotlib.pyplot as plt"
    print ""
    for i in range(0, sedmlDoc.getNumModels()):
        currentModel = sedmlDoc.getModel(i)
        print "# Execute the tasks of model: " + currentModel.getId()
        rrName = currentModel.getId()
        #rrName = "rr" + str(i)
        print rrName + " = roadrunner.RoadRunner()"
        generateTasks(rrName, sedmlDoc, currentModel, path)
        print ""
    print "# List of Data Generators"
    dataGeneratorsList = []
    for i in range(0, sedmlDoc.getNumModels()):
        currentModel = sedmlDoc.getModel(i)
        generateData(sedmlDoc, currentModel, dataGeneratorsList)
    print "# List of Outputs"
    generateOutputs(sedmlDoc, dataGeneratorsList)
    print "# End of generated script"

    contents = f.getvalue()
    sys.stdout = original  # restore print to stdout only
    f.close()
    return contents
예제 #21
0
def changeValues(model, model_name, explicit_model):

    # important paths
    sedml_path = './sedml_models/' + model_name + '/' + model_name + '.sedml'
    sbml_path = './sedml_models/' + model_name + '/sbml_models/' + explicit_model + '.sbml'

    # do a case study if the sedml file exists or not (it doesn't exist for models of the BioModelsDatabase)
    if os.path.exists(sedml_path):

        # load SBML && SEDML models
        sbml_file = libsbml.readSBML(sbml_path)
        sedml_file = libsedml.readSedML(sedml_path)

        # old species settings of SBML model that have to be replaced
        all_properties = sbml_file.getModel()
        spcs = all_properties.getListOfSpecies()
        spcs_id = []
        spcs_num = []
        for iCount in range(0, len(spcs)):
            spcs_id.append(spcs[iCount].id)
            spcs_num.append(spcs[iCount].initial_concentration)

        # old parameter settings of SBML model that have to be replaced
        par_id = model.getParameterIds()
        par_id = list(par_id)  # probelm: some IDs get changed to 'amici_'
        for iCount in range(0, len(par_id)):
            try:
                a, b = par_id[iCount].split('_', 1)
                if a == 'amici':
                    par_id[iCount] = b
            except:
                'No split necessary!'
        par_num = model.getParameters()
        par_num = list(par_num)

        # old compartment settings of SBML model that have to be replaced
        all_properties = sbml_file.getModel()
        comp = all_properties.getListOfCompartments()
        comp_id = []
        comp_num = []
        for iCount in range(0, len(comp)):
            comp_id.append(comp[iCount].id)
            comp_num.append(comp[iCount].size)

        # new settings of SEDML parameters
        for iModels in range(0, sedml_file.getNumModels()):
            all_models = sedml_file.getModel(iModels)
            for iAttribute in range(0, all_models.getNumChanges()):
                all_changes = all_models.getChange(iAttribute)
                new_targ = all_changes.getTarget()
                new_val = all_changes.getNewValue()

                # decide for species or parameter
                div = new_targ.split('[')
                div = div[0]
                div = div.split(':')
                div = div[4]

                if div == 'species':
                    # parse right id out of new_targ string
                    id = new_targ.split('\'', )
                    id = id[1]

                    # counter
                    counter = 0

                    try:  # SBML model sometimes doesn't have the SEDML ID
                        # swap species settings
                        while id != spcs_id[counter]:
                            counter = counter + 1
                        spcs_num[counter] = new_val
                    except:
                        # species id from SEDML not in SBML
                        'Species-ID can be omitted'

                elif div == 'parameter':
                    # parse right id out of new_targ string
                    id = new_targ.split('\'', )
                    id = id[1]

                    # counter
                    counter = 0

                    try:
                        # swap parameter settings
                        while id != par_id[counter]:
                            counter = counter + 1
                        par_num[counter] = new_val
                    except:
                        # parameter id from SEDML not in SBML
                        'Parameter-ID can be omitted'

                elif div == 'compartment':
                    # parse right id out of new_targ string
                    id = new_targ.split('\'', )
                    id = id[1]

                    # counter
                    counter = 0

                    try:  # SBML model sometimes doesn't have the SEDML ID
                        # swap compartment settings
                        while id != comp_id[counter]:
                            counter = counter + 1
                        comp_num[counter] = new_val
                    except:
                        # species id from SEDML not in SBML
                        'Compartment-ID can be omitted'

        # transform par_num into an array
        par_num = np.asarray(par_num)  # is never being reached
        p_num = []
        for iCount in range(0, len(par_num)):
            p_num.append(float(par_num[iCount]))

        # transform spcs_id into an array
        spcs_num = np.asarray(spcs_num)
        s_num = []
        for iCount in range(0, len(spcs_num)):
            s_num.append(float(spcs_num[iCount]))

        # replace old vector by new one
        model.setParameters(p_num)
        model.setInitialStates(s_num)

    else:
        'Model is part of the BioModelsDatabase!'

    return model
def changeValues_for_Copasi(model, model_name, explicit_model):

    # important paths
    biomodels_path = './sedml_models/' + model_name + '/sbml_models/' + explicit_model + '.xml'
    sedml_path = './sedml_models/' + model_name + '/' + model_name + '.sedml'
    sbml_path = './sedml_models/' + model_name + '/sbml_models/' + explicit_model + '.sbml'
    save_path = '../copasi_sim'

    # create folder
    if not os.path.exists(save_path + '/' + model_name + '/' + explicit_model):
        os.makedirs(save_path + '/' + model_name + '/' + explicit_model)


    # do a case study if the sedml file exists or not (it doesn't exist for models of the BioModelsDatabase)
    if os.path.exists(sedml_path):

        # load SBML && SEDML models
        sbml_file = libsbml.readSBML(sbml_path)
        sedml_file = libsedml.readSedML(sedml_path)

        # old species settings of SBML model that have to be replaced
        all_properties = sbml_file.getModel()
        spcs = all_properties.getListOfSpecies()
        spcs_id = []
        spcs_num = []
        for iCount in range(0, len(spcs)):
            spcs_id.append(spcs[iCount].id)
            spcs_num.append(spcs[iCount].initial_concentration)

        # old parameter settings of SBML model that have to be replaced
        par_id = model.getParameterIds()
        par_id = list(par_id)                                                                         # probelm: some IDs get changed to 'amici_'
        for iCount in range(0, len(par_id)):
            try:
                a, b = par_id[iCount].split('_', 1)
                if a == 'amici':
                    par_id[iCount] = b
            except:
                'No split necessary!'
        par_num = model.getParameters()
        par_num = list(par_num)

        # old compartment settings of SBML model that have to be replaced
        comp = all_properties.getListOfCompartments()
        comp_id = []
        comp_num = []
        for iCount in range(0, len(comp)):
            comp_id.append(comp[iCount].id)
            comp_num.append(comp[iCount].size)

        # new settings of SEDML parameters
        for iModels in range(0, sedml_file.getNumModels()):
            all_models = sedml_file.getModel(iModels)
            for iAttribute in range(0, all_models.getNumChanges()):
                all_changes = all_models.getChange(iAttribute)
                new_targ = all_changes.getTarget()
                new_val = all_changes.getNewValue()

                # decide for species or parameter
                div = new_targ.split('[')
                div = div[0]
                div = div.split(':')
                div = div[4]

                if div == 'species':
                    # parse right id out of new_targ string
                    id = new_targ.split('\'', )
                    id = id[1]

                    # counter
                    counter = 0

                    try:                                                                    # SBML model sometimes doesn't have the SEDML ID
                        # swap species settings
                        while id != spcs_id[counter]:
                            counter = counter + 1
                        all_properties.getSpecies(counter).initial_concentration = float(new_val)
                    except:
                        # species id from SEDML not in SBML
                        'Species-ID can be omitted'

                elif div == 'parameter':
                    # parse right id out of new_targ string
                    id = new_targ.split('\'',)
                    id = id[1]

                    # counter
                    counter = 0

                    try:
                        # swap parameter settings
                        while id != par_id[counter]:
                            counter = counter + 1
                        all_properties.getParameter(counter).value = float(new_val)
                    except:
                        # parameter id from SEDML not in SBML
                        'Parameter-ID can be omitted'

                elif div == 'compartment':
                    # parse right id out of new_targ string
                    id = new_targ.split('\'', )
                    id = id[1]

                    # counter
                    counter = 0

                    try:  # SBML model sometimes doesn't have the SEDML ID
                        # swap compartment settings
                        while id != comp_id[counter]:
                            counter = counter + 1
                        all_properties.getCompartment(counter).size = float(new_val)
                    except:
                        # species id from SEDML not in SBML
                        'Compartment-ID can be omitted'

        # save changed sbml files and the accompaniying sedml file
        libsbml.writeSBMLToFile(sbml_file, save_path + '/' + model_name + '/' + explicit_model + '/' + model_name + '.sbml')
        libsedml.writeSedMLToFile(sedml_file, save_path + '/' + model_name + '/' + explicit_model + '/' + model_name + '.sedml')

    elif os.path.exists(biomodels_path):
        'Model is part of the BioModelsDatabase! - no changes possible since no sedml file exists!'
        copy2(biomodels_path, save_path + '/' + model_name + '/' + explicit_model)

    else:
        print('Something with the paths went wrong!')
        sys.exit()
예제 #23
0
    def read(file_name):
        """ Imports simulation configuration from SED-ML file

        Args:
            file_name (:obj:`str`): path to SED-ML file from which to import simulation configuration

        Returns:
            :obj:`WCSimulationConfig`: simulation configuration
        """

        # initialize optional configuration
        opt_config = {}
        """ read SED-ML """
        # read XML file
        cfg_ml = libsedml.readSedML(file_name)
        """ validate SED-ML """
        # one model with
        # - zero or more attribute changes
        if cfg_ml.getNumModels() != 1:
            raise SedMlError('SED-ML configuration must have one model')

        mdl = cfg_ml.getModel(0)
        if mdl.getId() or mdl.getName() or mdl.getLanguage() or mdl.getSource(
        ):
            warnings.warn(
                'SED-ML import ignoring all model metadata (id, name, language, source)',
                SedMlWarning)

        for change_ml in mdl.getListOfChanges():
            if not isinstance(change_ml, libsedml.SedChangeAttribute):
                raise SedMlError(
                    'Cannot import model changes except attribute changes')

        # 1 simulation with
        #- Initial time = 0
        #- Output start time = 0
        #- Algorithm = KISAO_0000352 (hybrid)
        #- 1 algorithm parameters
        #  - random_seed (KISAO_0000488): int
        if cfg_ml.getNumSimulations() != 1:
            raise SedMlError('SED-ML configuration must have 1 simulation')

        sim_ml = cfg_ml.getSimulation(0)
        if sim_ml.getInitialTime() != sim_ml.getOutputStartTime():
            raise SedMlError(
                'Simulation initial time and output start time must be equal')

        alg_ml = sim_ml.getAlgorithm()
        if alg_ml.getKisaoID() != 'KISAO_0000352':
            raise SedMlError(
                'Unsupported algorithm. The only supported algorithm is KISAO_0000352 (hybrid)'
            )

        param_kisao_ids = set(
            param_ml.getKisaoID()
            for param_ml in alg_ml.getListOfAlgorithmParameters())
        if len(param_kisao_ids) < alg_ml.getNumAlgorithmParameters():
            raise SedMlError('Algorithm parameter KISAO ids must be unique')
        unsupported_param_kisao_ids = param_kisao_ids - set(['KISAO_0000488'])
        if unsupported_param_kisao_ids:
            raise SedMlError(
                'Algorithm parameters ({}) are not supported'.format(
                    ', '.join(unsupported_param_kisao_ids)))

        # zero or more repeated tasks each with
        # - 1 set value task change
        #   - math equal to constant floating point value
        # - 1 uniform or vector range of 1 value
        # - no subtasks
        for task_ml in cfg_ml.getListOfTasks():
            if isinstance(task_ml, libsedml.SedRepeatedTask):
                if task_ml.getNumTaskChanges() != 1:
                    raise SedMlError(
                        'Each repeated task must have one task change')
                change_ml = task_ml.getTaskChange(0)
                try:
                    float(libsedml.formulaToString(change_ml.getMath()))
                except ValueError:
                    raise SedMlError(
                        'Set value maths must be floating point values')

                if task_ml.getNumRanges() != 1:
                    raise SedMlError('Each repeated task must have one range')
                range_ml = task_ml.getRange(0)
                if not isinstance(
                        range_ml,
                    (libsedml.SedUniformRange, libsedml.SedVectorRange)):
                    raise SedMlError(
                        'Task ranges must be uniform or vector ranges')
                if isinstance(range_ml, libsedml.SedUniformRange
                              ) and range_ml.getNumberOfPoints() != 2**31 - 1:
                    raise SedMlError('Cannot import number of points')
                if isinstance(range_ml, libsedml.SedUniformRange
                              ) and range_ml.getType() != 'linear':
                    raise SedMlError('Only linear ranges are supported')
                if isinstance(range_ml, libsedml.SedVectorRange) and len(
                        list(range_ml.getValues())) != 1:
                    raise SedMlError('Task vector ranges must have length 1')

                if task_ml.getNumSubTasks() > 0:
                    raise SedMlError('Cannot import subtasks')
            else:
                raise SedMlError('Cannot import tasks except repeated tasks')

        # no data generators
        if cfg_ml.getNumDataGenerators() > 0:
            raise SedMlError('Cannot import data generator information')

        # no data descriptions
        if cfg_ml.getNumDataDescriptions() > 0:
            raise SedMlError('Cannot import data description information')

        # no outputs
        if cfg_ml.getNumOutputs() > 0:
            raise SedMlError('Cannot import output information')
        """ Read simulation configuration information from SED-ML document """

        # changes
        changes = []
        mdl = cfg_ml.getModel(0)
        for change_ml in mdl.getListOfChanges():
            target = change_ml.getTarget()
            change = Change()
            change.attr_path = Change.attr_path_from_str(change_ml.getTarget())
            change.value = float(change_ml.getNewValue())
            changes.append(change)

        # perturbations
        perturbations = []
        for task_ml in cfg_ml.getListOfTasks():
            change_ml = task_ml.getTaskChange(0)

            change = Change()
            change.attr_path = Change.attr_path_from_str(change_ml.getTarget())
            change.value = float(libsedml.formulaToString(change_ml.getMath()))

            range_ml = task_ml.getRange(0)
            if isinstance(range_ml, libsedml.SedUniformRange):
                start_time = range_ml.getStart()
                end_time = range_ml.getEnd()
            else:
                start_time = range_ml.getValues()[0]
                end_time = float('nan')

            perturbations.append(Perturbation(change, start_time, end_time))

        # time
        sim = cfg_ml.getSimulation(0)
        time_init = sim.getOutputStartTime()
        time_max = sim.getOutputEndTime()
        ode_time_step = (time_max - time_init) / sim.getNumberOfPoints()

        # algorithm parameters
        alg = sim.getAlgorithm()
        for param in alg.getListOfAlgorithmParameters():
            if param.getKisaoID() == 'KISAO_0000488':
                try:
                    opt_config['random_seed'] = float(param.getValue())
                except ValueError:
                    raise SedMlError('random_seed must be an integer')

                if opt_config['random_seed'] != math.ceil(
                        opt_config['random_seed']):
                    raise SedMlError('random_seed must be an integer')

                opt_config['random_seed'] = int(opt_config['random_seed'])
        """ build simulation configuration object """
        de_simulation_config = SimulationConfig(time_max=time_max,
                                                time_init=time_init)
        cfg = WCSimulationConfig(de_simulation_config=de_simulation_config,
                                 ode_time_step=ode_time_step,
                                 changes=changes,
                                 perturbations=perturbations,
                                 **opt_config)

        return cfg
예제 #24
0
if sedml_str == None:
    print(phrasedml.getLastPhrasedError())
print(sedml_str)

# In[2]:

# Create the temporary files and execute the code
import tempfile
f_sbml = tempfile.NamedTemporaryFile(prefix="myModel", suffix=".xml")
f_sbml.write(sbml_str)
f_sbml.flush()
print(f_sbml.name)

f_sedml = tempfile.NamedTemporaryFile(suffix=".sedml")
f_sedml.write(sedml_str)
f_sedml.flush()
print(f_sedml.name)

import libsedml
sedml_doc = libsedml.readSedML(f_sedml.name)
if sedml_doc.getErrorLog().getNumFailsWithSeverity(
        libsedml.LIBSEDML_SEV_ERROR) > 0:
    print(sedml_doc.getErrorLog().toString())

f_sbml.close()
f_sedml.close()

# Create executable python code sedml with roadrunner
# import tellurium.tesedml as s2p
# py_code = s2p.sedml_to_python(s2p)
                    os.makedirs('./json_files/' + iModel + '/' + iFile)
            except:
                print('Model ' + iModel + ' extension is missing!')  # error 2
                continue

            ######### jws simulation
            # Get time data with num_time_points == 100
            t_data = model.getTimepoints()
            sim_start_time = t_data[0]
            sim_end_time = t_data[len(t_data) - 1]
            sim_num_time_points = 101  #len(t_data)
            model.setTimepoints(
                np.linspace(sim_start_time, sim_end_time, sim_num_time_points))

            # Open sedml file
            sedml_model = libsedml.readSedML(sedml_path)

            # import all changes from SEDML
            list_of_strings = JWS_changeValues(iFile, sedml_model)

            # Get Url with all changes
            # <species 1>=<amount>
            # <parameter 1>=<value>, compartment == parameter (in this case)
            url = 'https://jjj.bio.vu.nl/rest/models/' + model_reference + '/time_evolution?time_end=' + str(
                sim_end_time) + ';species=all;'

            for iStr in list_of_strings:
                url = url + iStr

            #### Save .json file
            urllib.request.urlretrieve(
        list_files = os.listdir(base_path_sbml2amici + '/' + iModel)
        list_files = sorted(
            list_files
        )  # sorted() could maybe change the order needed for later

        for iFile in list_files:

            try:
                # run function
                model = load_specific_model(iModel, iFile)

                # open sedml to get tasks + time courses
                sedml_path = './sedml_models/' + iModel + '/' + iModel + '.sedml'

                # tasks
                sedml_file = libsedml.readSedML(sedml_path)
                for iSBMLModel in range(0, sedml_file.getNumTasks()):
                    all_tasks = sedml_file.getTask(iSBMLModel)
                    tsk_Id = all_tasks.getId()
                    task_name = all_tasks.getName()
                    task_modRef = all_tasks.getModelReference()
                    task_simReference = all_tasks.getSimulationReference()

                    # time courses
                    all_simulations = sedml_file.getSimulation(iSBMLModel)
                    sim_Id = all_simulations.getId()

                    while task_simReference != sim_Id:
                        iSBMLModel = iSBMLModel + 1  # only works if the list of models are somehow chronological and not random [iff task1 appears before task2]
                        all_simulations = sedml_file.getSimulation(iSBMLModel)
                        sim_Id = all_simulations.getId()