diff --git a/.gitignore b/.gitignore index 6c373c00..46bca744 100644 --- a/.gitignore +++ b/.gitignore @@ -104,4 +104,5 @@ uv.lock .DS_Store cnapy/cnapy_server.py fvacache* -_FVA* +*_FVA* +*.json.maps diff --git a/README.md b/README.md index f17ab591..d49e0322 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ There are 4 alternative ways to install CNApy: 1. The easiest way for any user to install CNApy is by downloading its installer, which is provided for Windows, Linux and MacOS, see [Using CNApy installer](#using-cnapy-installer) for more. 2. If you already have installed Python 3.10 (no other version) on your system, you can install CNApy simply through ```pip install cnapy``` in your console. Afterwards, you can start CNApy's GUI by running either ```cnapy``` or, if this doesn't work, ```python -m cnapy``` where "python" must call your Python 3.10 installation. -3. If you already use conda or mamba (for mamba, just change the "conda" command to "mamba"), you can create a CNApy environment named ```cnapy-1.2.8``` as follows: 1) Run ```conda create --name cnapy-1.2.8 python=3.10 pip openjdk -c conda-forge```, 2) run ```conda activate cnapy-1.2.8```, 3) run ```pip install cnapy```. Then, you can start CNApy in the cnapy-1.2.8 conda environment by running either ```cnapy``` or, if this doesn't work, ```python -m cnapy```. Note that the [cnapy conda package](https://anaconda.org/cnapy/cnapy) is currently *not* being updated due to licensing uncertainties. +3. If you already use conda or mamba (for mamba, just change the "conda" command to "mamba"), you can create a CNApy environment named ```cnapy-1.2.9``` as follows: 1) Run ```conda create --name cnapy-1.2.9 python=3.10 pip openjdk -c conda-forge```, 2) run ```conda activate cnapy-1.2.9```, 3) run ```pip install cnapy```. Then, you can start CNApy in the cnapy-1.2.9 conda environment by running either ```cnapy``` or, if this doesn't work, ```python -m cnapy```. Note that the [cnapy conda package](https://anaconda.org/cnapy/cnapy) is currently *not* being updated due to licensing uncertainties. 4. If you want to develop CNApy, follow the instruction for the cloning and setup of the CNApy repository using git and conda or mamba in section [Setup the CNApy development environment](#setup-the-cnapy-development-environment). ## Documentation and Tutorials @@ -56,18 +56,18 @@ This installer lets you create a local installation of CNApy under Windows, Linu *If you use Windows:* -- Download the Windows installer [from here](https://github.com/cnapy-org/CNApy/releases/download/v1.2.8/install_cnapy_here.bat) +- Download the Windows installer [from here](https://github.com/cnapy-org/CNApy/releases/download/v1.2.9/install_cnapy_here.bat) - Put this file into a folder where you want CNApy to be installed. - Double click on the file and let the CNApy installation run -- Afterwards, you can run CNApy by either double-clicking on the newly created CNApy desktop icon, or by double-clicking "RUN_CNApy.bat" in the newly created cnapy-1.2.8 subfolder. +- Afterwards, you can run CNApy by either double-clicking on the newly created CNApy desktop icon, or by double-clicking "RUN_CNApy.bat" in the newly created cnapy-1.2.9 subfolder. *If you use Linux or MacOS*: -- Download the Linux & MacOS installer [from here](https://github.com/cnapy-org/CNApy/releases/download/v1.2.8/install_cnapy_here.sh). +- Download the Linux & MacOS installer [from here](https://github.com/cnapy-org/CNApy/releases/download/v1.2.9/install_cnapy_here.sh). - Put this file into a folder where you want CNApy to be installed. - Make the script executable by opening your console in the folder and run ```chmod u+x ./install_cnapy_here.sh```. Alternatively, if supported on your system, right-click on the file, go the file's settings and mark it as executable. - Now, either run ```./install_cnapy_here.sh``` in your console or, if supported on your system, double-click on install_cnapy_here.sh. -- Finally, you can run CNApy by calling ```./run_cnapy.sh``` in your console (for this without another path beforehand, your console must point to the folder where run_cnapy.sh is located, e.g. if you are in the folder where install_cnapy_here.sh is located, through ```cd cnapy-1.2.8```). Alternatively, if supported by your system, double-click on "run_cnapy.sh" in the newly created cnapy-1.2.8 subfolder. +- Finally, you can run CNApy by calling ```./run_cnapy.sh``` in your console (for this without another path beforehand, your console must point to the folder where run_cnapy.sh is located, e.g. if you are in the folder where install_cnapy_here.sh is located, through ```cd cnapy-1.2.9```). Alternatively, if supported by your system, double-click on "run_cnapy.sh" in the newly created cnapy-1.2.9 subfolder. Technical side note: CNApy's installer is utilizing [micromamba](https://mamba.readthedocs.io/en/latest/installation/micromamba-installation.html). diff --git a/cnapy/appdata.py b/cnapy/appdata.py index eeb44fa6..24b43130 100644 --- a/cnapy/appdata.py +++ b/cnapy/appdata.py @@ -31,7 +31,7 @@ class AppData(QObject): def __init__(self): QObject.__init__(self) - self.version = "cnapy-1.2.8" + self.version = "cnapy-1.2.9" self.format_version = 2 self.unsaved = False self.project = ProjectData() diff --git a/cnapy/gui_elements/annotation_widget.py b/cnapy/gui_elements/annotation_widget.py index a78ec05e..c42e57e6 100644 --- a/cnapy/gui_elements/annotation_widget.py +++ b/cnapy/gui_elements/annotation_widget.py @@ -65,11 +65,11 @@ def apply_annotation(self, model_element): value = "" else: value = self.annotation.item(i, 1).text() - if value.startswith("["): - try: - value = ast.literal_eval(value) - except: # if parsing as list does not work keep the raw text - pass + # if value.startswith("["): + # try: + # value = ast.literal_eval(value) + # except: # if parsing as list does not work keep the raw text + # pass model_element.annotation[key] = value diff --git a/cnapy/gui_elements/thermodynamics_dialog.py b/cnapy/gui_elements/thermodynamics_dialog.py index 5c7ac496..e9a70cd5 100644 --- a/cnapy/gui_elements/thermodynamics_dialog.py +++ b/cnapy/gui_elements/thermodynamics_dialog.py @@ -1,4 +1,5 @@ """The CNApy OptMDFpathway dialog""" +from cobrak.utilities import delete_orphaned_metabolites_and_enzymes import cobra import cobra.util.solver from numpy import exp @@ -18,10 +19,10 @@ from cnapy.gui_elements.central_widget import CentralWidget from cnapy.gui_elements.solver_buttons import get_solver_buttons from enum import Enum -from cobrak.constants import LNCONC_VAR_PREFIX, DF_VAR_PREFIX, MDF_VAR_ID, ALL_OK_KEY, OBJECTIVE_VAR_NAME, TERMINATION_CONDITION_KEY +from cobrak.constants import LNCONC_VAR_PREFIX, DF_VAR_PREFIX, MDF_VAR_ID, ALL_OK_KEY, OBJECTIVE_VAR_NAME, TERMINATION_CONDITION_KEY, REAC_ENZ_SEPARATOR from cobrak.dataclasses import ExtraLinearConstraint, Solver from cobrak.lps import perform_lp_optimization, perform_lp_thermodynamic_bottleneck_analysis -from cobrak.io import load_annotated_cobrapy_model_as_cobrak_model +from cobrak.io import load_annotated_cobrapy_model_as_cobrak_model, json_write from cobrak.cobrapy_model_functionality import get_fullsplit_cobra_model @@ -223,6 +224,23 @@ def compute_optmdf(self): ) return + cobrak_model = delete_orphaned_metabolites_and_enzymes(load_annotated_cobrapy_model_as_cobrak_model( + get_fullsplit_cobra_model( + model, + fwd_suffix=self.FWDID, + rev_suffix=self.REVID, + add_cobrak_sbml_annotation=True, + cobrak_default_min_conc=float(self.min_default_conc.text()), + cobrak_default_max_conc=float(self.max_default_conc.text()), + cobrak_extra_linear_constraints=extra_linear_constraints, + cobrak_kinetic_ignored_metabolites=[], + cobrak_no_extra_versions=True, + reac_lb_ub_cap=1_000.0, + ) + )) + cobrak_model = delete_orphaned_metabolites_and_enzymes(cobrak_model) + + objective: dict[str, int | float] = {} match self.analysis_type: case ThermodynamicAnalysisTypes.OPTMDFPATHWAY: objective = {MDF_VAR_ID: 1} @@ -231,32 +249,22 @@ def compute_optmdf(self): objective = {"bottleneck_z_sum": 1} direction = -1 case ThermodynamicAnalysisTypes.THERMODYNAMIC_FBA: - objective = {} for ( reaction, coefficient, ) in cobra.util.solver.linear_reaction_coefficients(model).items(): - if reaction.reversibility: - objective[reaction.id + self.FWDID] = coefficient - objective[reaction.id + self.REVID] = -coefficient - else: - objective[reaction.id] = coefficient + split_reac_ids = [ + reac_id + for reac_id in cobrak_model.reactions.keys() + if reac_id.startswith(reaction.id + REAC_ENZ_SEPARATOR) or reac_id == reaction.id + ] + for split_reac_id in split_reac_ids: + if split_reac_id.endswith(self.REVID): + objective[split_reac_id] = -coefficient + else: + objective[split_reac_id] = +coefficient direction = +1 - cobrak_model = load_annotated_cobrapy_model_as_cobrak_model( - get_fullsplit_cobra_model( - model, - fwd_suffix=self.FWDID, - rev_suffix=self.REVID, - add_cobrak_sbml_annotation=True, - cobrak_default_min_conc=float(self.min_default_conc.text()), - cobrak_default_max_conc=float(self.max_default_conc.text()), - cobrak_extra_linear_constraints=extra_linear_constraints, - cobrak_kinetic_ignored_metabolites=[], - cobrak_no_extra_versions=True, - reac_lb_ub_cap=1_000.0, - ) - ) if all(cobrak_model.reactions[reac_id].dG0 is None for reac_id in cobrak_model.reactions): QMessageBox.warning( @@ -280,7 +288,7 @@ def compute_optmdf(self): verbose=True, ) elif self.analysis_type == ThermodynamicAnalysisTypes.BOTTLENECK_ANALYSIS: - bottlenecks, solution = perform_lp_thermodynamic_bottleneck_analysis( + self.bottlenecks, solution = perform_lp_thermodynamic_bottleneck_analysis( cobrak_model=cobrak_model, with_enzyme_constraints=False, min_mdf=min_mdf, @@ -296,20 +304,40 @@ def compute_optmdf(self): def set_boxes(self, solution: dict[str, float]): # Combine FWD and REV flux solutions - combined_solution = {} - for var_id in solution.keys(): - if var_id.endswith(self.FWDID): - key = var_id.replace(self.FWDID, "") - multiplier = 1.0 - elif var_id.endswith(self.REVID): - key = var_id.replace(self.REVID, "") - multiplier = -1.0 - else: - key = var_id - multiplier = 1.0 - if key not in combined_solution.keys(): - combined_solution[key] = 0.0 - combined_solution[key] += multiplier * solution[var_id] + model: cobra.Model = self.appdata.project.cobra_py_model + # Split IDs look like [_ENZ_][_FWDCNAPY|_REVCNAPY] + def get_split_id_suffix_free(split_reac_id: str) -> str: + for suffix in (self.FWDID, self.REVID): + if split_reac_id.endswith(suffix): + return split_reac_id[:-len(suffix)] + return split_reac_id + + def belongs_to(split_reac_id: str, reac_id: str) -> bool: + base_id = get_split_id_suffix_free(split_reac_id) + return base_id == reac_id or base_id.startswith(reac_id + REAC_ENZ_SEPARATOR) + + combined_solution: dict[str, float] = {} + combined_dfs: dict[str, float] = {} + for reaction in model.reactions: + split_reac_ids = [ + var_id + for var_id in solution + if not var_id.startswith(DF_VAR_PREFIX) and belongs_to(var_id, reaction.id) + ] + if not split_reac_ids: + continue + combined_solution[reaction.id] = 0.0 + for split_reac_id in split_reac_ids: + multiplier = -1.0 if split_reac_id.endswith(self.REVID) else +1.0 + combined_solution[reaction.id] += solution[split_reac_id] * multiplier + + # The driving force is taken from the split reaction that carries the most flux + # (raw value of the active direction, i.e. not flipped for REV versions). + # No driving force is shown for reactions without flux. + active_split_reac_id = max(split_reac_ids, key=lambda var_id: solution[var_id]) + df_var_id = DF_VAR_PREFIX + active_split_reac_id + if (df_var_id in solution) and (solution[active_split_reac_id] > 1e-9): + combined_dfs[reaction.id] = solution[df_var_id] # write results into comp_values for search_key in self.reac_ids: @@ -318,16 +346,16 @@ def set_boxes(self, solution: dict[str, float]): float(combined_solution[search_key]), float(combined_solution[search_key]), ) - if DF_VAR_PREFIX + search_key in combined_solution.keys(): + if search_key in combined_dfs.keys(): rounded_df = round( - combined_solution[DF_VAR_PREFIX + search_key], self.appdata.rounding + combined_dfs[search_key], self.appdata.rounding ) self.appdata.project.df_values[search_key] = rounded_df # Write metabolite concentrations for metabolite_id in self.metabolite_ids: var_id = f"{LNCONC_VAR_PREFIX}{metabolite_id}" - if var_id in solution.keys(): + if var_id in solution: rounded_conc = round(exp(solution[var_id]), 9) self.appdata.project.conc_values[var_id[len(LNCONC_VAR_PREFIX):]] = rounded_conc @@ -343,13 +371,9 @@ def set_boxes(self, solution: dict[str, float]): console_text += f"Reached objective value: {solution[OBJECTIVE_VAR_NAME]}" console_text += f"\\nReached MDF @ optimum of objective: {solution[MDF_VAR_ID]} kJ/mol" elif self.analysis_type == ThermodynamicAnalysisTypes.BOTTLENECK_ANALYSIS: - combined_bottleneck_sum = 0 - for key in combined_solution.keys(): - if key.startswith("zb_var_"): - if combined_solution[key] > 0.1: - console_text += f"\\n* {key.replace('zb_var_', '')}" - combined_bottleneck_sum += 1 - console_text += f"\\n↳Total number of thermodynamically deactivated bottlenecks to reach minimal MDF: {combined_bottleneck_sum}" + for reac_id in self.bottlenecks: + console_text += f"\\n* {reac_id.split(REAC_ENZ_SEPARATOR)[0].replace(self.FWDID, '').replace(self.REVID, '')}" + console_text += f"\\n↳Total number of thermodynamically deactivated bottlenecks to reach minimal MDF: {len(self.bottlenecks)}" console_text += "')" self.central_widget.kernel_client.execute(console_text) self.central_widget.show_bottom_of_console() diff --git a/docs/index.md b/docs/index.md index 8b3c307d..77451795 100644 --- a/docs/index.md +++ b/docs/index.md @@ -61,13 +61,13 @@ Everyone is welcome to contribute to CNApy's development. [See our contribution 3. Create a conda environment with all dependencies ```sh - conda create -n cnapy-1.2.8 -c conda-forge -c cnapy cnapy=1.2.8 + conda create -n cnapy-1.2.9 -c conda-forge -c cnapy cnapy=1.2.9 ``` 4. Activate the cnapy conda environment ```sh - conda activate cnapy-1.2.8 + conda activate cnapy-1.2.9 ``` 5. Run CNApy within you activated conda environment diff --git a/installers/install_cnapy_here.bat b/installers/install_cnapy_here.bat index 51d06374..12eea31e 100644 --- a/installers/install_cnapy_here.bat +++ b/installers/install_cnapy_here.bat @@ -7,7 +7,7 @@ set "psFile=install_cnapy.ps1" :: Write the PowerShell script to a file echo # Adapted from https://raw.githubusercontent.com/mamba-org/micromamba-releases/main/install.ps1 > "%psFile%" echo. >> "%psFile%" -echo $CNAPY_VERSION = "1.2.8" ^# Replace with the actual version if needed >> "%psFile%" +echo $CNAPY_VERSION = "1.2.9" ^# Replace with the actual version if needed >> "%psFile%" echo $RELEASE_URL="https://github.com/mamba-org/micromamba-releases/releases/latest/download/micromamba-win-64" >> "%psFile%" echo. >> "%psFile%" echo Write-Output "Downloading micromamba from $RELEASE_URL" >> "%psFile%" @@ -26,7 +26,7 @@ echo Move-Item -Force micromamba.exe $MAMBA_INSTALL_PATH ^| out-null >> "%psFile echo. >> "%psFile%" echo ^# Use ^& to execute the micromamba commands stored in the variable >> "%psFile%" echo ^& $MAMBA_INSTALL_PATH create -y -p "./cnapy-$CNAPY_VERSION/cnapy-environment" python=3.10 pip openjdk -r "./cnapy-$CNAPY_VERSION/" -c conda-forge >> "%psFile%" -echo Copy-Item -Path "cnapy-1.2.8/condabin/mamba.bat" -Destination "cnapy-1.2.8/condabin/micromamba.bat" >> "%psFile%" +echo Copy-Item -Path "cnapy-1.2.9/condabin/mamba.bat" -Destination "cnapy-1.2.9/condabin/micromamba.bat" >> "%psFile%" echo ^& $MAMBA_INSTALL_PATH run -p "./cnapy-$CNAPY_VERSION/cnapy-environment" -r "./cnapy-$CNAPY_VERSION/" pip install --no-cache-dir uv >> "%psFile%" echo ^& $MAMBA_INSTALL_PATH run -p "./cnapy-$CNAPY_VERSION/cnapy-environment" -r "./cnapy-$CNAPY_VERSION/" uv --no-cache pip install --no-cache-dir cnapy >> "%psFile%" echo. >> "%psFile%" @@ -68,9 +68,9 @@ if exist "%psFile%" ( :: Congratulate the user echo Congratulations! CNApy was successfully installed! - echo To run CNApy, double-click on the newly created CNApy-1.2.8 desktop icon or, - echo alternatively, double-click on the RUN_CNApy.bat file in the newly created cnapy-1.2.8 subfolder. - echo To deinstall CNApy later, simply delete the newly created cnapy-1.2.8 subfolder. + echo To run CNApy, double-click on the newly created CNApy-1.2.9 desktop icon or, + echo alternatively, double-click on the RUN_CNApy.bat file in the newly created cnapy-1.2.9 subfolder. + echo To deinstall CNApy later, simply delete the newly created cnapy-1.2.9 subfolder. pause ) else ( echo PowerShell script file not found: %psFile% diff --git a/installers/install_cnapy_here.sh b/installers/install_cnapy_here.sh index 641cd9e4..2dc18d2c 100755 --- a/installers/install_cnapy_here.sh +++ b/installers/install_cnapy_here.sh @@ -4,7 +4,7 @@ set -eu # CNApy version -CNAPY_VERSION="1.2.8" +CNAPY_VERSION="1.2.9" # Folders BIN_FOLDER="${BIN_FOLDER:-./cnapy-${CNAPY_VERSION}}" diff --git a/pyproject.toml b/pyproject.toml index 1f36ac66..69c86606 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "cnapy" -version = "1.2.8" +version = "1.2.9" authors = [ { name="Sven Thiele" }, { name="Axel von Kamp" }, @@ -34,7 +34,7 @@ dependencies = [ "openpyxl", "jpype1==1.5.0", "setuptools", - "cobrak==0.0.10", + "cobrak==0.0.11", "highspy>=1.14.0" ]