Пример #1
0
def parseDataDescriptions(sedml_path):
    """ Test helper functions.

    Tries to parse all DataDescriptions in the SED-ML file.
    """
    print('parseDataDescriptions:', sedml_path)
    # load sedml document
    assert os.path.exists(sedml_path)

    doc_sedml = libsedml.readSedMLFromFile(sedml_path)
    SEDMLTools.checkSEDMLDocument(doc_sedml)

    # parse DataDescriptions
    list_dd = doc_sedml.getListOfDataDescriptions()
    # print(list_dd)
    # print(len(list_dd))

    assert len(list_dd) > 0

    for dd in list_dd:
        data_sources = DataDescriptionParser.parse(dd, workingDir=BASE_DIR)
        assert data_sources is not None
        assert type(data_sources) == dict
        assert len(data_sources) > 0
    return data_sources
Пример #2
0
def parseDataDescriptions(sedml_path):
    """ Test helper functions.

    Tries to parse all DataDescriptions in the SED-ML file.
    """
    print('parseDataDescriptions:', sedml_path)
    # load sedml document
    assert os.path.exists(sedml_path)

    doc_sedml = libsedml.readSedMLFromFile(sedml_path)
    SEDMLTools.checkSEDMLDocument(doc_sedml)

    # parse DataDescriptions
    list_dd = doc_sedml.getListOfDataDescriptions()
    # print(list_dd)
    # print(len(list_dd))

    assert len(list_dd) > 0

    for dd in list_dd:
        data_sources = DataDescriptionParser.parse(dd, workingDir=BASE_DIR)
        assert data_sources is not None
        assert type(data_sources) == dict
        assert len(data_sources) > 0
    return data_sources
Пример #3
0
def check_files_exist(sedml_file_name):
    """ Utility function that checks whether all the model sources do exist

    :param sedml_file_name: full path to a sedml file
    :return: True, if all model sources exist, False otherwise.
    """
    doc = libsedml.readSedMLFromFile(sedml_file_name)
    assert (isinstance(doc, libsedml.SedDocument))

    if doc.getNumErrors(libsedml.LIBSEDML_SEV_ERROR) > 0:
        logging.error('invalid SED-ML document: ' +
                      doc.getErrorLog().toString())

    elif doc.getNumErrors(libsedml.LIBSEDML_SEV_WARNING) > 0:
        logging.warning('warnings in SED-ML document: ' +
                        doc.getErrorLog().toString())

    name = os.path.splitext(os.path.basename(sedml_file_name))[0]
    directory = os.path.dirname(sedml_file_name)

    model_ids = [model.getId() for model in doc.getListOfModels()]
    result = True
    model_file_name = ''
    for i in range(doc.getNumModels()):
        model = doc.getModel(i)
        assert (isinstance(model, libsedml.SedModel))

        if model.getSource() in model_ids:
            # derived model skip
            continue

        model_file_name = os.path.join(directory, model.getSource())

        if not os.path.exists(model_file_name):
            logging.error(
                f'missing model file "{model.getSource()}" in sedml-file "{name}"'
            )
            result = False

        if ':' in model_file_name and not model_file_name.startswith('urn:'):
            logging.warning(
                f'source {model.getSource()} contains colon, and is likely not to resolve on all platforms'
            )

    check_for_duplicated_ids(doc, True)

    xpath_expressions = get_xpath_expressions_from(doc)

    return result, xpath_expressions, model_file_name
Пример #4
0
def rename_model(sedml_file_name, old_name, new_name, output_file=None):
    """ renames a model source in the SED-ML file

    :param sedml_file_name: full path to the sedml file
    :param old_name: old model source to be renamed
    :param new_name: new model source
    :param output_file: optional target filename where the new sed-ml file will be saved (otherwise the original
          one will be overwritten)
    :return: None
    """
    doc = libsedml.readSedMLFromFile(sedml_file_name)
    assert (isinstance(doc, libsedml.SedDocument))
    for i in range(doc.getNumModels()):
        model = doc.getModel(i)
        assert (isinstance(model, libsedml.SedModel))

        if model.getSource() == old_name:
            model.setSource(new_name)

    if output_file is None:
        output_file = sedml_file_name

    libsedml.writeSedMLToFile(doc, output_file)
Пример #5
0
import libsedml

if __name__ == "__main__":
    path = "test-sedml.xml"
    doc = libsedml.readSedMLFromFile(path)
    print(doc)
Пример #6
0
def status_yml(omex_file: str, out_dir: str):
    yaml_dict = []
    for sedml in extract_omex_archive(omex_file):
        outputs_dict = {"outputs": []}
        tasks_dict = {"tasks": []}
        # add temp dir path
        sedml_path = os.path.join(tmp_dir, sedml)
        sedml_doc = libsedml.readSedMLFromFile(sedml_path)

        # Get all the required metadata
        tasks = sedml_doc.getListOfTasks()
        plots = sedml_doc.getListOfOutputs()

        # Convert into the list
        task_list = [task.getId() for task in tasks]
        plots_dict = {}
        reports_dict = {}
        other_list = []

        for plot in plots:
            if type(plot) == libsedml.SedPlot2D:
                plots_dict[plot.getId()] = [
                    curve.getId() for curve in plot.getListOfCurves()
                ]
            elif type(plot) == libsedml.SedReport:
                reports_dict[plot.getId()] = [
                    dataSet.getId() for dataSet in plot.getListOfDataSets()
                ]
            else:
                other_list.append(plot.getId())

        for plot in list(plots_dict.keys()):
            curves_list = []
            for curve in plots_dict[plot]:
                curves_list.append({"id": curve, "status": "SUCCEEDED"})
            outputs_dict["outputs"].append({
                "id": plot,
                "status": "SUCCEEDED",
                "exception": None,
                "skipReason": None,
                "output": None,
                "duration": None,
                "curves": curves_list
            })

        for report in list(reports_dict.keys()):
            dataset_list = []
            #dataset_dict = {}
            for dataset in reports_dict[report]:
                dataset_list.append({"id": dataset, "status": "FAILED"})
            exc_dict = {'type': "", 'message': ""}
            outputs_dict["outputs"].append({
                "id": report,
                "status": "FAILED",
                "exception": None,
                "skipReason": None,
                "output": None,
                "duration": None,
                "dataSets": dataset_list
            })
            #outputs_dict["outputs"][report].update({"status": "QUEUED"})

        for task in task_list:
            exc_dict = {'type': "", 'message': ""}
            tasks_dict["tasks"].append({
                "id": task,
                "status": "QUEUED",
                "exception": None,
                "skipReason": None,
                "output": None,
                "duration": None,
                "algorithm": None,
                "simulatorDetails": None
            })

        exc_dict = {'type': "", 'message': ""}
        sed_doc_dict = {
            "location": sedml,
            "status": "QUEUED",
            "exception": None,
            "skipReason": None,
            "output": None,
            "duration": None
        }
        sed_doc_dict.update(outputs_dict)
        sed_doc_dict.update(tasks_dict)
        yaml_dict.append(sed_doc_dict)
    exc_dict = {'type': "", 'message': ""}
    final_dict = {}
    final_dict['sedDocuments'] = yaml_dict
    final_dict['status'] = "QUEUED"
    final_dict['exception'] = None
    final_dict['skipReason'] = None
    final_dict['duration'] = None
    final_dict['output'] = None

    status_yaml_path = os.path.join(out_dir, "log.yml")

    with open(status_yaml_path, 'w', encoding="utf-8") as sy:
        sy.write(yaml.dump(final_dict))
    # return final_dict
    shutil.rmtree(tmp_dir)
Пример #7
0
def exec_combine_archive(archive_file, out_dir):
    """Execute the SED tasks defined in a COMBINE archive and save the outputs

    :param archive_file: path to COMBINE archive
    :type archive_file: str
    :param out_dir: directory to store the outputs of the tasks
    :type out_dir: str
    :raises FileNotFoundError: When the combine archive is not found
    :raises IOError: When file is not an OMEX combine archive
    """
    # check that archive exists and is in zip format
    if not os.path.isfile(archive_file):
        raise FileNotFoundError("File does not exist: {}".format(archive_file))

    if not zipfile.is_zipfile(archive_file):
        raise IOError("File is not an OMEX Combine Archive in zip format: {}".format(archive_file))

    try:
        archive_file = os.path.abspath(archive_file)
        out_dir = os.path.abspath(out_dir)
        # Create temp directory
        tmp_dir = tempfile.mkdtemp()

        # Get list of contents from Combine Archive
        archive = libcombine.CombineArchive()
        is_initialised = archive.initializeFromArchive(archive_file)
        is_extracted = archive.extractTo(tmp_dir)
        manifest = archive.getManifest()
        contents = manifest.getListOfContents()

        if not is_initialised or not is_extracted:
            sys.exit("Problem while initialising/extract combine archive")

        # Get location of all SEDML files
        sedml_locations = list()
        for content in contents:
            if content.isFormat('sedml'):
                sedml_locations.append(content.getLocation())

        # run all sedml files
        for sedml_location in sedml_locations:
            sedml_path = os.path.join(tmp_dir, sedml_location)
            sedml_out_dir = os.path.join(out_dir, os.path.splitext(sedml_location)[0])

            sedml_doc = libsedml.readSedMLFromFile(sedml_path)

            tasks = sedml_doc.getListOfTasks()
            task_name_list = [task.getId() for task in tasks]

            for sim in range(0, sedml_doc.getNumSimulations()):
                current_doc = sedml_doc.getSimulation(sim)
                if current_doc.getTypeCode() == libsedml.SEDML_SIMULATION_UNIFORMTIMECOURSE:
                    tc = current_doc
                    if current_doc.isSetAlgorithm():
                        kisao_id = current_doc.getAlgorithm().getKisaoID()
                        print("timeCourseID={}, outputStartTime={}, outputEndTime={}, numberOfPoints={}, kisaoID={} " \
                              .format(tc.getId(), tc.getOutputStartTime(), tc.getOutputEndTime(),
                                      tc.getNumberOfPoints(), kisao_id))
                else:
                    print(f"Encountered unknown simulation {current_doc.getId()}")

            if not os.path.isdir(sedml_out_dir):
                os.makedirs(sedml_out_dir)

            # Create a base Copasi container to hold all the Tasks
            try:
                data_model = copasi.CRootContainer.addDatamodel()
            except BaseException:
                data_model = copasi.CRootContainer.getUndefinedFunction()
            data_model.importSEDML(sedml_path)

            report = create_time_course_report(data_model)
            # Run all Tasks
            task_name_index = 0
            for task_index in range(0, len(data_model.getTaskList())):
                task = data_model.getTaskList().get(task_index)
                # Get Name and Class of task as string
                task_str = str(task)
                try:
                    # Get name of Task
                    task_name = task_str.split('"')[1]
                except IndexError:
                    # Get Class name if Task name is not present
                    task_name = task_str.split("'")[1].split("*")[0]
                    task_name = task_name[:len(task_name) - 1]
                # Set output file for the task
                if task_name == 'Time-Course':
                    task.setScheduled(True)
                    # task.getReport().setReportDefinition(report)
                    report_def = task.getReport().compile('')
                    if not report_def:
                        print('No Report definition found in SEDML, setting to a default definition')
                        task.getReport().setReportDefinition(report)
                    sedml_task_name = task_name_list[task_name_index]
                    report_path = os.path.join(sedml_out_dir, f'{sedml_task_name}.csv')
                    task.getReport().setTarget(report_path)
                    task_name_index = task_name_index + 1
                    print(f'Generated report for Simulation "{sedml_task_name}": {report_path}')
                    # If file exists, don't append in it, overwrite it.
                    task.getReport().setAppend(False)
                    # Initialising the task with default values
                    task.initialize(119)
                    task.process(True)
                    try:
                        pd.read_csv(report_path).drop(" ", axis=1).to_csv(report_path, index=False)
                    except KeyError:
                        print(f"No trailing commas were found in {report_path}\n")
                    df = pd.read_csv(report_path)
                    cols = list(df.columns)
                    new_cols = list()
                    for col in cols:
                        new_cols.append(col.split()[-1])
                    df.columns = new_cols
                    df.to_csv(report_path, index=False)

    finally:
        shutil.rmtree(tmp_dir)
Пример #8
0
def status_yml(omex_file: str, out_dir: str):
    yaml_dict = {}

    for sedml in extract_omex_archive(omex_file):
        outputs_dict = {"outputs": {}}
        tasks_dict = {"tasks": {}}
        # add temp dir path
        sedml_path = os.path.join(tmp_dir, sedml)
        sedml_doc = libsedml.readSedMLFromFile(sedml_path)

        # Get all the required metadata
        tasks = sedml_doc.getListOfTasks()
        plots = sedml_doc.getListOfOutputs()

        # Convert into the list
        task_list = [task.getId() for task in tasks]

        plots_dict = {}
        reports_dict = {}
        other_list = []

        for plot in plots:
            if type(plot) == libsedml.SedPlot2D:
                plots_dict[plot.getId()] = [curve.getId() for curve in plot.getListOfCurves()]
            elif type(plot) == libsedml.SedReport:
                reports_dict[plot.getId()] = [dataSet.getId() for dataSet in plot.getListOfDataSets()]
            else:
                other_list.append(plot.getId())

        for plot in list(plots_dict.keys()):
            curves_dict = {}
            for curve in plots_dict[plot]:
                curves_dict[curve] = 'SUCCEEDED'
            outputs_dict["outputs"].update({plot: {"curves": curves_dict}})
            outputs_dict["outputs"][plot].update({"status": "SUCCEEDED"})

        for report in list(reports_dict.keys()):
            dataset_dict = {}
            for dataset in reports_dict[report]:
                dataset_dict[dataset] = 'QUEUED'
            outputs_dict["outputs"].update({report: {"dataSets": dataset_dict}})
            outputs_dict["outputs"][report].update({"status": "QUEUED"})

        for task in task_list:
            tasks_dict["tasks"].update({task: {"status": "QUEUED"}})

        sed_doc_dict = {sedml: {}}
        sed_doc_dict[sedml].update(outputs_dict)
        sed_doc_dict[sedml].update(tasks_dict)
        sed_doc_dict[sedml].update({"status": "QUEUED"})
        yaml_dict[sedml] = sed_doc_dict[sedml]
    final_dict = {}
    final_dict['sedDocuments'] = dict(yaml_dict)
    final_dict['status'] = "QUEUED"

    status_yaml_path = os.path.join(out_dir, "status.yml")

    with open(status_yaml_path, 'w' , encoding="utf-8") as sy:
        sy.write(yaml.dump(final_dict))
    # return final_dict
    shutil.rmtree(tmp_dir)
Пример #9
0
    def setUp(self):
        self.file_name = test_utils.get_example('Wilson2012.sedml')

        self.assertTrue(os.path.exists(self.file_name))
        self.doc = libsedml.readSedMLFromFile(self.file_name)
        self.assertTrue(self.doc.getNumErrors(libsedml.LIBSEDML_SEV_ERROR) == 0)