diff --git a/ArchiveEditor.ipynb b/ArchiveEditor.ipynb new file mode 100644 index 000000000..3b836088b --- /dev/null +++ b/ArchiveEditor.ipynb @@ -0,0 +1,417 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [], + "authorship_tag": "ABX9TyNnt18sNKtoldKNrwotloXQ", + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "jyZetQ55uWF6", + "outputId": "17c3586f-27e1-45ef-a9c6-c7082a2ef87e" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Requirement already satisfied: biosimulators-utils[sbml] in /usr/local/lib/python3.10/dist-packages (0.1.187)\n", + "Requirement already satisfied: appdirs in /usr/local/lib/python3.10/dist-packages (from biosimulators-utils[sbml]) (1.4.4)\n", + "Requirement already satisfied: biopython in /usr/local/lib/python3.10/dist-packages (from biosimulators-utils[sbml]) (1.83)\n", + "Requirement already satisfied: cement in /usr/local/lib/python3.10/dist-packages (from biosimulators-utils[sbml]) (3.0.10)\n", + "Requirement already satisfied: evalidate>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from biosimulators-utils[sbml]) (2.0.2)\n", + "Requirement already satisfied: h5py in /usr/local/lib/python3.10/dist-packages (from biosimulators-utils[sbml]) (3.9.0)\n", + "Requirement already satisfied: kisao>=2.33 in /usr/local/lib/python3.10/dist-packages (from biosimulators-utils[sbml]) (2.34)\n", + "Requirement already satisfied: lxml in /usr/local/lib/python3.10/dist-packages (from biosimulators-utils[sbml]) (4.9.4)\n", + "Requirement already satisfied: matplotlib in /usr/local/lib/python3.10/dist-packages (from biosimulators-utils[sbml]) (3.7.1)\n", + "Requirement already satisfied: mpmath in /usr/local/lib/python3.10/dist-packages (from biosimulators-utils[sbml]) (1.3.0)\n", + "Requirement already satisfied: natsort in /usr/local/lib/python3.10/dist-packages (from biosimulators-utils[sbml]) (8.4.0)\n", + "Requirement already satisfied: networkx>=2.6 in /usr/local/lib/python3.10/dist-packages (from biosimulators-utils[sbml]) (3.2.1)\n", + "Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (from biosimulators-utils[sbml]) (1.25.2)\n", + "Requirement already satisfied: openpyxl in /usr/local/lib/python3.10/dist-packages (from biosimulators-utils[sbml]) (3.1.2)\n", + "Requirement already satisfied: pandas in /usr/local/lib/python3.10/dist-packages (from biosimulators-utils[sbml]) (1.5.3)\n", + "Requirement already satisfied: pronto>=2.4 in /usr/local/lib/python3.10/dist-packages (from biosimulators-utils[sbml]) (2.5.6)\n", + "Requirement already satisfied: pyomexmeta>=1.2.13 in /usr/local/lib/python3.10/dist-packages (from biosimulators-utils[sbml]) (1.2.14)\n", + "Requirement already satisfied: python-dateutil in /usr/local/lib/python3.10/dist-packages (from biosimulators-utils[sbml]) (2.8.2)\n", + "Requirement already satisfied: python-libcombine>=0.2.11 in /usr/local/lib/python3.10/dist-packages (from biosimulators-utils[sbml]) (0.2.20)\n", + "Requirement already satisfied: python-libsedml>=2.0.16 in /usr/local/lib/python3.10/dist-packages (from biosimulators-utils[sbml]) (2.0.32)\n", + "Requirement already satisfied: pyyaml in /usr/local/lib/python3.10/dist-packages (from biosimulators-utils[sbml]) (6.0.1)\n", + "Requirement already satisfied: rdflib in /usr/local/lib/python3.10/dist-packages (from biosimulators-utils[sbml]) (7.0.0)\n", + "Requirement already satisfied: regex in /usr/local/lib/python3.10/dist-packages (from biosimulators-utils[sbml]) (2023.12.25)\n", + "Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from biosimulators-utils[sbml]) (2.31.0)\n", + "Requirement already satisfied: setuptools in /usr/local/lib/python3.10/dist-packages (from biosimulators-utils[sbml]) (67.7.2)\n", + "Requirement already satisfied: simplejson in /usr/local/lib/python3.10/dist-packages (from biosimulators-utils[sbml]) (3.19.2)\n", + "Requirement already satisfied: termcolor in /usr/local/lib/python3.10/dist-packages (from biosimulators-utils[sbml]) (2.4.0)\n", + "Requirement already satisfied: uritools in /usr/local/lib/python3.10/dist-packages (from biosimulators-utils[sbml]) (4.0.2)\n", + "Requirement already satisfied: yamldown in /usr/local/lib/python3.10/dist-packages (from biosimulators-utils[sbml]) (0.1.8)\n", + "Requirement already satisfied: python-libsbml in /usr/local/lib/python3.10/dist-packages (from biosimulators-utils[sbml]) (5.20.2)\n", + "Requirement already satisfied: chardet~=5.0 in /usr/local/lib/python3.10/dist-packages (from pronto>=2.4->biosimulators-utils[sbml]) (5.2.0)\n", + "Requirement already satisfied: fastobo~=0.12.2 in /usr/local/lib/python3.10/dist-packages (from pronto>=2.4->biosimulators-utils[sbml]) (0.12.3)\n", + "Requirement already satisfied: graphviz>=0.15 in /usr/local/lib/python3.10/dist-packages (from pyomexmeta>=1.2.13->biosimulators-utils[sbml]) (0.20.1)\n", + "Requirement already satisfied: pydot>=1.4.1 in /usr/local/lib/python3.10/dist-packages (from pyomexmeta>=1.2.13->biosimulators-utils[sbml]) (1.4.2)\n", + "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil->biosimulators-utils[sbml]) (1.16.0)\n", + "Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib->biosimulators-utils[sbml]) (1.2.0)\n", + "Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.10/dist-packages (from matplotlib->biosimulators-utils[sbml]) (0.12.1)\n", + "Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib->biosimulators-utils[sbml]) (4.49.0)\n", + "Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib->biosimulators-utils[sbml]) (1.4.5)\n", + "Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib->biosimulators-utils[sbml]) (24.0)\n", + "Requirement already satisfied: pillow>=6.2.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib->biosimulators-utils[sbml]) (9.4.0)\n", + "Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib->biosimulators-utils[sbml]) (3.1.2)\n", + "Requirement already satisfied: et-xmlfile in /usr/local/lib/python3.10/dist-packages (from openpyxl->biosimulators-utils[sbml]) (1.1.0)\n", + "Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas->biosimulators-utils[sbml]) (2023.4)\n", + "Requirement already satisfied: isodate<0.7.0,>=0.6.0 in /usr/local/lib/python3.10/dist-packages (from rdflib->biosimulators-utils[sbml]) (0.6.1)\n", + "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->biosimulators-utils[sbml]) (3.3.2)\n", + "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->biosimulators-utils[sbml]) (3.6)\n", + "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests->biosimulators-utils[sbml]) (2.0.7)\n", + "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->biosimulators-utils[sbml]) (2024.2.2)\n" + ] + } + ], + "source": [ + "!pip install biosimulators-utils[sbml] process-bigraph" + ] + }, + { + "cell_type": "markdown", + "source": [ + "### Upload your archive" + ], + "metadata": { + "id": "gjqFHc5bufpY" + } + }, + { + "cell_type": "code", + "source": [ + "from google.colab import files" + ], + "metadata": { + "id": "YRw-DUavuZqr" + }, + "execution_count": 2, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "files.upload()" + ], + "metadata": { + "id": "YSodaaEkuibL" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "!pip install libsbml" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "zC4_vt4Dz5os", + "outputId": "d7744d78-47e0-4d3d-fd63-d487955be1b6" + }, + "execution_count": 16, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "\u001b[31mERROR: Could not find a version that satisfies the requirement libsbml (from versions: none)\u001b[0m\u001b[31m\n", + "\u001b[0m\u001b[31mERROR: No matching distribution found for libsbml\u001b[0m\u001b[31m\n", + "\u001b[0m" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "# API source\n", + "import os\n", + "import tempfile\n", + "from dataclasses import dataclass, asdict\n", + "from typing import List, Tuple, Dict, Union\n", + "from process_bigraph import pp\n", + "from biosimulators_utils.combine.data_model import CombineArchive, CombineArchiveContent\n", + "from biosimulators_utils.combine.io import CombineArchiveWriter, CombineArchiveReader\n", + "from biosimulators_utils.sedml.io import SedmlSimulationReader, SedmlSimulationWriter\n", + "from biosimulators_utils.sedml.utils import get_all_sed_objects # change this\n", + "from biosimulators_utils.sedml.model_utils import get_parameters_variables_outputs_for_simulation\n", + "from biosimulators_utils.sedml.data_model import *\n", + "\n", + "\n", + "# datamodel\n", + "@dataclass\n", + "class ParameterValue:\n", + " default: Union[int, float]\n", + " new_value: Union[int, float] = None\n", + "\n", + "\n", + "@dataclass\n", + "class EditableSimulationParameter:\n", + " target: str\n", + " value: Union[ParameterValue, Dict[str, Union[float, int]]]\n", + "\n", + " def to_dict(self):\n", + " return asdict(self)\n", + "\n", + "\n", + "# exec\n", + "class ArchiveEditorApi:\n", + " @classmethod\n", + " def upload_archive(cls):\n", + " return files.upload()\n", + "\n", + " @classmethod\n", + " def get_uploaded_omex_fp(cls, root: str) -> str:\n", + " \"\"\"Get content from colab env\"\"\"\n", + " fpaths = []\n", + " for f in os.listdir(root):\n", + " if '.omex' in f:\n", + " fpaths.append(f)\n", + " # if len(fpaths) > 1:\n", + " # raise OSError('You can only edit one omex archive at a time.')\n", + " # else:\n", + " return fpaths[0]\n", + "\n", + " @classmethod\n", + " def read_omex(cls, omex_fp: str, out_dir: str) -> CombineArchive:\n", + " return CombineArchiveReader().run(in_file=omex_fp, out_dir=out_dir)\n", + "\n", + " @classmethod\n", + " def is_sedml(cls, content: CombineArchiveContent) -> bool:\n", + " return 'simulation' in content.location\n", + "\n", + " @classmethod\n", + " def get_sedml(cls, fp: str) -> SedDocument:\n", + " return SedmlSimulationReader().run(filename=fp)\n", + "\n", + " @classmethod\n", + " def get_editable_params(\n", + " cls,\n", + " fp: str,\n", + " model_lang: str,\n", + " sim_type: Simulation,\n", + " kisao_id: str\n", + " ) -> Tuple[List[ModelAttributeChange], List[Simulation], List[Variable], List[Plot]]:\n", + " attrb_changes, sim, variables, plots = get_parameters_variables_outputs_for_simulation(\n", + " model_filename=fp,\n", + " model_language=model_lang,\n", + " simulation_type=sim_type,\n", + " change_level=SedDocument,\n", + " algorithm_kisao_id=kisao_id)\n", + " return attrb_changes, sim, variables, plots\n", + "\n", + " @classmethod\n", + " def parse_editable_params(cls, attributes: List[ModelAttributeChange]) -> List[EditableSimulationParameter]:\n", + " params = []\n", + " for attribute in attributes:\n", + " # new_value is default and target is target\n", + " param_value = ParameterValue(default=attribute.new_value)\n", + " editable_param = EditableSimulationParameter(target=attribute.target, value=param_value)\n", + " params.append(editable_param)\n", + " return params\n", + "\n", + "\n", + " @classmethod\n", + " def run(cls, working_dir: str, colab: bool = False, kisao_id: str = None):\n", + " if colab:\n", + " from google.colab import files\n", + " cls.upload_archive()\n", + "\n", + " omex_fp: str = cls.get_uploaded_omex_fp(working_dir)\n", + " temp_extraction_dir = tempfile.mkdtemp()\n", + " uploaded_archive: CombineArchive = cls.read_omex(omex_fp, temp_extraction_dir)\n", + "\n", + " for content in uploaded_archive.contents:\n", + " if 'sedml' in content.location:\n", + " sed_fp = content.location.replace('./', '')\n", + " sed_doc: SedDocument = cls.get_sedml(\n", + " os.path.join(temp_extraction_dir, sed_fp))\n", + " sed_doc_objects = get_all_sed_objects(sed_doc)\n", + " for obj in sed_doc_objects:\n", + " if isinstance(obj, Task):\n", + " sim_type = type(obj.simulation)\n", + " sim_model = obj.model\n", + " model_lang = sim_model.language\n", + " model_fp = os.path.join(temp_extraction_dir, sim_model.source)\n", + "\n", + " attributes, sim, variables, plots = cls.get_editable_params(\n", + " fp=model_fp,\n", + " model_lang=model_lang,\n", + " sim_type=sim_type,\n", + " kisao_id=kisao_id)\n", + "\n", + " editable_params = cls.parse_editable_params(attributes)\n", + "\n", + "\n", + " pp(editable_params[0].to_dict())\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "ArchiveEditorApi.run('/content')" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "nhzHa4j_uj9y", + "outputId": "151b18a1-91be-4a6b-b767-93bdce930629" + }, + "execution_count": 83, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "{ 'target': \"/sbml:sbml/sbml:model/sbml:listOfSpecies/sbml:species[@id='Trim']/@initialConcentration\",\n", + " 'value': {'default': '0.084410675', 'new_value': None}}\n" + ] + }, + { + "output_type": "stream", + "name": "stderr", + "text": [ + "/usr/local/lib/python3.10/dist-packages/biosimulators_utils/warnings.py:31: BioSimulatorsWarning: The SED document is potentially incorrect.\n", + " - This tool validates XPaths for model changes and variables against their unmodified target models. As a result, this tool does not fully validate these XPaths. These XPaths could be invalidated by changes to their target models.\n", + " - Model `Ciliberto2003_Morphogenesis` has warnings.\n", + " - The model file `BIOMD0000000297_url.xml` has warnings.\n", + " - 3 warnings of type SBO term consistency (10713). The following is the first warning at line 211, column 6:\n", + " - The value of the 'sboTerm' attribute on a is expected to be an SBO identifier (http://www.biomodels.net/SBO/). In SBML Level 2 prior to Version 4 it is expected to refer to a participant physical type (i.e., terms derived from SBO:0000236, \"participant physical type\"); in Versions 4 and above it is expected to refer to a material entity (i.e., terms derived from SBO:0000240, \"material entity\").\n", + " Reference: L2V4 Section 5\n", + " SBO term 'SBO:0000014' on the is not in the appropriate branch.\n", + " - 9 warnings of type SBO term consistency (10703). The following is the first warning at line 405, column 6:\n", + " - The value of the 'sboTerm' attribute on a is expected to be an SBO identifier (http://www.biomodels.net/SBO/) referring to a quantitative parameter defined in SBO (i.e., terms derived from SBO:0000002, \"quantitative systems description parameter\"). \n", + " Reference: L2V4 Sections 4.9.5 and 5\n", + " SBO term 'SBO:0000336' on the is not in the appropriate branch.\n", + " - 67 warnings of type SBML unit consistency (99505). The following is the first warning at line 440, column 6:\n", + " - In situations where a mathematical expression contains literal numbers or parameters whose units have not been declared, it is not possible to verify accurately the consistency of the units in the expression. \n", + " The units of the expression 'kswe_prime * Swe1 + kswe_doubleprime * Swe1M + kswe_tripleprime * PSwe1' cannot be fully checked. Unit consistency reported as either no errors or further unit errors related to this object may not be accurate.\n", + " - 81 warnings of type Modeling practice (80701). The following is the first warning at line 357, column 6:\n", + " - As a principle of best modeling practice, the units of a should be declared rather than be left undefined. Doing so improves the ability of software to check the consistency of units and helps make it easier to detect potential errors in models.\n", + " The with the id 'kswe' does not have a 'units' attribute.\n", + " warnings.warn(termcolor.colored(message, Colors.warning.value), category)\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "def f(*args):\n", + " return args" + ], + "metadata": { + "id": "eEWgsNSLyg06" + }, + "execution_count": 56, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "a, b = f(2, 3)# API server" + ], + "metadata": { + "id": "pLdOaeHJv9RE" + }, + "execution_count": 57, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "a# client" + ], + "metadata": { + "id": "hmsnScT5wA2Q", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "4b0635f0-ad1e-40eb-e1b3-3e007210f393" + }, + "execution_count": 58, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "2" + ] + }, + "metadata": {}, + "execution_count": 58 + } + ] + }, + { + "cell_type": "code", + "source": [ + "b" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "lHP47jfF-RpL", + "outputId": "7feee280-7faa-4d57-efa6-1cb3ef37702c" + }, + "execution_count": 59, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "3" + ] + }, + "metadata": {}, + "execution_count": 59 + } + ] + }, + { + "cell_type": "code", + "source": [], + "metadata": { + "id": "4jWAQ038-SE7" + }, + "execution_count": null, + "outputs": [] + } + ] +} \ No newline at end of file