{ "cells": [ { "cell_type": "code", "execution_count": 2, "id": "8ef47cb2-812a-4cd1-87d3-0a6573ab0a5c", "metadata": {}, "outputs": [], "source": [ "#import set_path # Using MyBinder? Uncomment this before running the next cell!" ] }, { "cell_type": "code", "execution_count": 1, "id": "3924c013", "metadata": { "tags": [] }, "outputs": [], "source": [ "#import sys\n", "#sys.path.append(\"C:/some_path/my_env_or_install\") # CHANGE to the folder containing your venv or libraries installation!\n", "# NOTE: If any of the imports below can't find a module, uncomment the lines above, or try: import set_path\n", "\n", "import ipynbname\n", "\n", "from life123 import check_version, ChemData, UniformCompartment, PlotlyHelper, GraphicLog, DisplayNetwork" ] }, { "cell_type": "code", "execution_count": 2, "id": "83c3cc5f-de21-4f66-9988-2806fbf0666d", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-> Output will be LOGGED into the file 'cascade_test.log.htm'\n" ] } ], "source": [ "# Initialize the HTML logging (for the graphics)\n", "log_file = ipynbname.name() + \".log.htm\" # Use the notebook base filename for the log file\n", "\n", "# Set up the use of some specified graphic (Vue) components\n", "GraphicLog.config(filename=log_file,\n", " components=[\"vue_cytoscape_2\"],\n", " extra_js=\"https://cdnjs.cloudflare.com/ajax/libs/cytoscape/3.21.2/cytoscape.umd.js\")" ] }, { "cell_type": "code", "execution_count": 3, "id": "72b4245c-de4e-480d-a501-3495b7ed8bc4", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Number of reactions: 2\n" ] } ], "source": [ "# Specify the chemicals and the reactions; this data structure will get re-used in \n", "# the various simulations below\n", "chem_data = ChemData()\n", "\n", "uc = UniformCompartment(chem_data=chem_data, preset=\"slower\")\n", "\n", "# Reaction A <-> B (fast)\n", "uc.add_reaction(reactants=\"A\", products=\"B\",\n", " kF=64., kR=8.) \n", "\n", "# Reaction B <-> C (slow)\n", "uc.add_reaction(reactants=\"B\", products=\"C\",\n", " kF=12., kR=2.) \n", "\n", "print(\"Number of reactions: \", uc.number_of_reactions())" ] }, { "cell_type": "code", "execution_count": 4, "id": "00ea560d-9a49-4041-b119-6de11bfcc7af", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Number of reactions: 2\n", "0: A <-> B (Elementary Unimolecular reaction) (kF = 64 / kR = 8 / delta_G = -5,154.8 / K = 8 / Temp = 25 C)\n", "1: B <-> C (Elementary Unimolecular reaction) (kF = 12 / kR = 2 / delta_G = -4,441.7 / K = 6 / Temp = 25 C)\n", "Chemicals involved in the above reactions: ['A', 'B', 'C']\n" ] } ], "source": [ "uc.describe_reactions()" ] }, { "cell_type": "code", "execution_count": 5, "id": "cb582868-431c-4022-aa0e-a2f554f80d6c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[GRAPHIC ELEMENT SENT TO LOG FILE `cascade_test.log.htm`]\n" ] } ], "source": [ "# Send a plot of the network of reactions to the HTML log file\n", "rnxs = uc.get_reactions()\n", "\n", "rnxs.plot_reaction_network(\"vue_cytoscape_2\")" ] }, { "cell_type": "code", "execution_count": null, "id": "c9ac12d6-481e-4181-84a8-a4d0b251c3d7", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 6, "id": "74a1e2bd-4e82-4aaa-a68f-7cee52b2ebbf", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'structure': [{'name': 'RXN',\n", " 'kF': '64',\n", " 'kR': '8',\n", " 'delta_G': '-5,154.85',\n", " 'K': '8',\n", " 'id': 'R-0',\n", " 'labels': ['Reaction']},\n", " {'name': 'B', 'diff_rate': None, 'id': 'C-1', 'labels': ['Chemical']},\n", " {'name': 'produces',\n", " 'source': 'R-0',\n", " 'target': 'C-1',\n", " 'id': 'edge-1',\n", " 'stoich': 1,\n", " 'rxn_order': 1},\n", " {'name': 'A', 'diff_rate': None, 'id': 'C-0', 'labels': ['Chemical']},\n", " {'name': 'reacts',\n", " 'source': 'C-0',\n", " 'target': 'R-0',\n", " 'id': 'edge-2',\n", " 'stoich': 1,\n", " 'rxn_order': 1},\n", " {'name': 'RXN',\n", " 'kF': '12',\n", " 'kR': '2',\n", " 'delta_G': '-4,441.69',\n", " 'K': '6',\n", " 'id': 'R-1',\n", " 'labels': ['Reaction']},\n", " {'name': 'C', 'diff_rate': None, 'id': 'C-2', 'labels': ['Chemical']},\n", " {'name': 'produces',\n", " 'source': 'R-1',\n", " 'target': 'C-2',\n", " 'id': 'edge-3',\n", " 'stoich': 1,\n", " 'rxn_order': 1},\n", " {'name': 'reacts',\n", " 'source': 'C-1',\n", " 'target': 'R-1',\n", " 'id': 'edge-4',\n", " 'stoich': 1,\n", " 'rxn_order': 1}],\n", " 'color_mapping': {'Chemical': '#8DCC92', 'Reaction': '#D9C8AD'},\n", " 'caption_mapping': {'Chemical': 'name', 'Reaction': 'name'}}" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rxn_graph_data = rnxs.prepare_graph_network()\n", "rxn_graph_data" ] }, { "cell_type": "code", "execution_count": null, "id": "79e454e2-576e-479c-a500-9715acc8d62a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 7, "id": "e22d5b3f-ae06-49e2-8e44-133d1a162691", "metadata": { "tags": [] }, "outputs": [], "source": [ "structure = [{'id': 'n1', 'labels': ['label_1'], 'name': 'A', 'field_1': 123},\n", " {'id': 'n2', 'labels': ['label_2'], 'name': 'B', 'field_1': 8},\n", "\n", " {'id': 'edge-1', 'name': 'produces', 'source': 'n1', 'target': 'n2', 'link_prop': \"hello\"}\n", " ]" ] }, { "cell_type": "code", "execution_count": 8, "id": "d59bdf36-861e-4389-a818-c2ec6e9a03a7", "metadata": { "tags": [] }, "outputs": [], "source": [ "color_mapping = {'label_1': '#8DCC92', 'label_2': '#D9C8AD'}\n", "caption_mapping = {'label_1': 'name', 'label_2': 'name'}" ] }, { "cell_type": "code", "execution_count": 9, "id": "4982af32-64e2-4cfa-b03a-df8abdf8ea37", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "{'structure': [{'id': 'n1',\n", " 'labels': ['label_1'],\n", " 'name': 'A',\n", " 'field_1': 123},\n", " {'id': 'n2', 'labels': ['label_2'], 'name': 'B', 'field_1': 8},\n", " {'id': 'edge-1',\n", " 'name': 'produces',\n", " 'source': 'n1',\n", " 'target': 'n2',\n", " 'link_prop': 'hello'}],\n", " 'color_mapping': {'label_1': '#8DCC92', 'label_2': '#D9C8AD'},\n", " 'caption_mapping': {'label_1': 'name', 'label_2': 'name'}}" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fig = {\"structure\": structure, \"color_mapping\": color_mapping, \"caption_mapping\": caption_mapping}\n", "fig" ] }, { "cell_type": "code", "execution_count": null, "id": "3b0e89d1-4cc3-4ee8-bbd6-1e0cd98814d3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 10, "id": "66efca42-2d3a-4775-9cf3-a2edc7b35dae", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[GRAPHIC ELEMENT SENT TO LOG FILE `cascade_test.log.htm`]\n" ] } ], "source": [ "# Send a plot of the network of reactions to the HTML log file\n", "GraphicLog.export_plot(graph_data=fig, graphic_component=\"vue_cytoscape_2\", unpack=False)" ] }, { "cell_type": "code", "execution_count": null, "id": "cd8a210b-9c62-4062-b207-dd5be3813dae", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 2, "id": "29adac9a-d452-4eb1-8e26-af0a1e5d6fe7", "metadata": { "tags": [] }, "outputs": [], "source": [ "simplified = {'structure': \n", " [\n", " {'id': 'n-0',\n", " 'labels': ['label_1'],\n", " 'name': 'Company A',\n", " 'my_field': 1234\n", " },\n", "\n", " {'id': 'n-1', \n", " 'labels': ['label_2'],\n", " 'name': 'Customer B',\n", " 'my_field': 88\n", " },\n", " \n", " {'name': 'SELLS_TO',\n", " 'source': 'n-0',\n", " 'target': 'n-1',\n", " 'id': 'edge-1',\n", " 'edge_prop': \"some value for the link\"\n", " }\n", " ],\n", " \n", " 'color_mapping': {'label_1': '#8DCC92', 'label_2': '#D9C8AD'},\n", " 'caption_mapping': {'label_1': 'name', 'label_2': 'name'}\n", " }" ] }, { "cell_type": "code", "execution_count": null, "id": "6ea02ecc-6376-4389-9eee-4050bebc03ae", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 7, "id": "6d90ab7d-1ed4-4ed8-be69-8c15cfd5a7ba", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[GRAPHIC ELEMENT SENT TO LOG FILE `cascade_test.log.htm`]\n" ] } ], "source": [ "GraphicLog.export_plot(simplified, \"vue_cytoscape_2\", unpack=False)" ] }, { "cell_type": "code", "execution_count": null, "id": "db81ec34-2023-4bb8-96aa-6356380ed8a0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "156cde41-c234-447d-8fda-fe42614469c2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "6b34f996-9862-4fc1-aebf-772b72aaf2ef", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 23, "id": "ca23d014-dd1f-44f1-84e1-f6f017145d1a", "metadata": { "tags": [] }, "outputs": [], "source": [ "class SimpleGraphic:\n", " \n", " @classmethod\n", " def _write_to_file(cls, file_handler, text :str) -> None:\n", " file_handler.write(text)\n", " file_handler.flush() # To avoid weird buffering issues seen in JupyterLab\n", " \n", " \n", " \n", " @classmethod \n", " def _html_header(cls) -> str:\n", " return '''\n", "\n", "\n", "
\n", " \n", "