klamt-lab / straindesign

StrainDesign is a python package for the computational design of metabolic networks and based on COBRApy
Apache License 2.0
33 stars 6 forks source link

iMM904 Production Envelopes plot_flux_space #22

Open EmanuelGoncalves opened 3 months ago

EmanuelGoncalves commented 3 months ago

Hi, I got the following problem running plot_flux_space function on the iMM904 model (no modification from the BiGG). Am I missing any argument? Thank you in advance and beautiful module!

import cobra
import straindesign as sd

model = cobra.io.read_sbml_model("iMM904.xml")

product_id = "EX_etoh_e"
biomass_id = "BIOMASS_SC5_notrace"

sd.plot_flux_space(model, (biomass_id, product_id))

Empty plot and several warnings:

WARNING:root:Computing matrix rank or Delaunay simplices failed.

VonAlphaBisZulu commented 3 months ago

Hi Emanuel,

This error is produced when straindesign attempts to plot the flux space and then fails because it cannot connect the calculated outlines of the flux space projection into a shape. This either happens if there are no solutions to the model or if some number of calculated solutions are invalid. The latter is the case here. This is definitely a bug since both fluxes, ethanol exchange and growth, have upper and lower bounds and the flux space within these boundaries is convex.

I was able to reproduce your problem when using python 3.12. I tried it with python 3.10 instead, and all four solvers managed to produce a valid plot.

Can you test your code with python 3.10 and different solvers (at least glpk and scip) and/or with different solvers in 3.12 and tell me if the problem persists? I should try to resolve the issue and if not possible, pin the python version for strain design to 3.10 (or 3.11) and below.

EmanuelGoncalves commented 3 months ago

Hi Philipp,

Many thanks for your very prompt help!

I'm actually using Python 3.10.12 and the Optlang GLPK solver. Could it be the version of the packages? I leave bellow the details of my environment.

# packages in environment at /opt/miniconda3/envs/cobrapy:
#
# Name                    Version                   Build  Channel
annotated-types           0.6.0                    pypi_0    pypi
anyio                     3.7.1                    pypi_0    pypi
appdirs                   1.4.4                    pypi_0    pypi
appnope                   0.1.4              pyhd8ed1ab_0    conda-forge
asttokens                 2.4.1              pyhd8ed1ab_0    conda-forge
bzip2                     1.0.8                h80987f9_5  
ca-certificates           2024.2.2             hf0a4a13_0    conda-forge
certifi                   2024.2.2                 pypi_0    pypi
click                     8.1.7                    pypi_0    pypi
cobra                     0.26.2                   pypi_0    pypi
comm                      0.2.2              pyhd8ed1ab_0    conda-forge
cycler                    0.12.1                   pypi_0    pypi
debugpy                   1.6.7           py310h313beb8_0  
decorator                 5.1.1              pyhd8ed1ab_0    conda-forge
depinfo                   1.7.0                    pypi_0    pypi
diskcache                 5.6.3                    pypi_0    pypi
exceptiongroup            1.2.0              pyhd8ed1ab_2    conda-forge
executing                 2.0.1              pyhd8ed1ab_0    conda-forge
fonttools                 4.50.0                   pypi_0    pypi
future                    1.0.0                    pypi_0    pypi
h11                       0.12.0                   pypi_0    pypi
httpcore                  0.15.0                   pypi_0    pypi
httpx                     0.23.0                   pypi_0    pypi
idna                      3.6                      pypi_0    pypi
importlib-metadata        7.1.0              pyha770c72_0    conda-forge
importlib-resources       6.4.0                    pypi_0    pypi
importlib_metadata        7.1.0                hd8ed1ab_0    conda-forge
inspyred                  1.0.2                    pypi_0    pypi
ipykernel                 6.29.3             pyh3cd1d5f_0    conda-forge
ipython                   8.23.0                   pypi_0    pypi
jedi                      0.19.1             pyhd8ed1ab_0    conda-forge
jmetalpy                  1.5.5                    pypi_0    pypi
joblib                    1.3.2                    pypi_0    pypi
jpype1                    1.5.0                    pypi_0    pypi
jupyter_client            8.6.1              pyhd8ed1ab_0    conda-forge
jupyter_core              5.5.0           py310hca03da5_0  
kiwisolver                1.4.5                    pypi_0    pypi
libcxx                    16.0.6               h4653b0c_0    conda-forge
libffi                    3.4.4                hca03da5_0  
libsodium                 1.0.18               h27ca646_1    conda-forge
markdown-it-py            3.0.0                    pypi_0    pypi
matplotlib                3.5.0                    pypi_0    pypi
matplotlib-inline         0.1.6              pyhd8ed1ab_0    conda-forge
mdurl                     0.1.2                    pypi_0    pypi
mewpy                     0.1.34                   pypi_0    pypi
mpmath                    1.3.0                    pypi_0    pypi
ncurses                   6.4                  h313beb8_0  
nest-asyncio              1.6.0              pyhd8ed1ab_0    conda-forge
networkx                  3.2.1                    pypi_0    pypi
numpy                     1.23.5                   pypi_0    pypi
openssl                   3.2.1                h0d3ecfb_1    conda-forge
optlang                   1.8.1                    pypi_0    pypi
packaging                 24.0               pyhd8ed1ab_0    conda-forge
pandas                    1.5.3                    pypi_0    pypi
parso                     0.8.3              pyhd8ed1ab_0    conda-forge
patsy                     0.5.6                    pypi_0    pypi
pexpect                   4.9.0              pyhd8ed1ab_0    conda-forge
pickleshare               0.7.5                   py_1003    conda-forge
pillow                    10.3.0                   pypi_0    pypi
pip                       23.3.1          py310hca03da5_0  
platformdirs              4.2.0              pyhd8ed1ab_0    conda-forge
plotly                    5.20.0                   pypi_0    pypi
prompt-toolkit            3.0.43                   pypi_0    pypi
psutil                    5.9.0           py310h1a28f6b_0  
ptyprocess                0.7.0              pyhd3deb0d_0    conda-forge
pure_eval                 0.2.2              pyhd8ed1ab_0    conda-forge
pydantic                  1.10.14                  pypi_0    pypi
pydantic-core             2.16.3                   pypi_0    pypi
pygments                  2.17.2             pyhd8ed1ab_0    conda-forge
pyparsing                 3.1.2                    pypi_0    pypi
python                    3.10.12              hb885b13_0  
python-dateutil           2.9.0.post0              pypi_0    pypi
python-libsbml            5.20.2                   pypi_0    pypi
pytz                      2024.1                   pypi_0    pypi
pyzmq                     25.1.2          py310h313beb8_0  
readline                  8.2                  h1a28f6b_0  
reframed                  1.5.1                    pypi_0    pypi
rfc3986                   1.5.0                    pypi_0    pypi
rich                      13.7.1                   pypi_0    pypi
ruamel-yaml               0.18.6                   pypi_0    pypi
ruamel-yaml-clib          0.2.8                    pypi_0    pypi
scipy                     1.12.0                   pypi_0    pypi
setuptools                68.2.2          py310hca03da5_0  
setuptools-scm            8.0.4                    pypi_0    pypi
six                       1.16.0             pyh6c4a22f_0    conda-forge
sniffio                   1.3.1                    pypi_0    pypi
sqlite                    3.41.2               h80987f9_0  
stack-data                0.6.3                    pypi_0    pypi
stack_data                0.6.2              pyhd8ed1ab_0    conda-forge
statsmodels               0.14.1                   pypi_0    pypi
straindesign              1.11                     pypi_0    pypi
swiglpk                   5.0.10                   pypi_0    pypi
sympy                     1.12                     pypi_0    pypi
tdqm                      0.0.1                    pypi_0    pypi
tenacity                  8.2.3                    pypi_0    pypi
tk                        8.6.12               hb8d0fd4_0  
tomli                     2.0.1                    pypi_0    pypi
tornado                   6.3.3           py310h80987f9_0  
tqdm                      4.66.2                   pypi_0    pypi
traitlets                 5.14.2             pyhd8ed1ab_0    conda-forge
typing_extensions         4.10.0             pyha770c72_0    conda-forge
tzdata                    2024.1                   pypi_0    pypi
wcwidth                   0.2.13             pyhd8ed1ab_0    conda-forge
wheel                     0.41.2          py310hca03da5_0  
xz                        5.4.6                h80987f9_0  
zeromq                    4.3.5                hebf3989_1    conda-forge
zipp                      3.17.0             pyhd8ed1ab_0    conda-forge
zlib                      1.2.13               h5a0b063_0  
EmanuelGoncalves commented 3 months ago

Hi,

I was digging a bit more on this I found that:

  1. I had cobrapy 0.26.2, I created a new environment and pip installed straindesign with brings cobrapy 0.29.0. I still get the warnings:

sd.plot_flux_space(model, (rxnBiomass, rxnTarget))

WARNING:root:Computing matrix rank or Delaunay simplices failed.
WARNING:root:Computing matrix rank or Delaunay simplices failed.
WARNING:root:Computing matrix rank or Delaunay simplices failed.
(...)

and generates this plot:

envelope_straindesign
  1. I tried the production envelope function from cobrapy and get a different result, which to me makes more sense.
prod_env = production_envelope(model, [rxnTarget], objective=rxnBiomass)
prod_env.plot(kind="line", x="flux_maximum", y=rxnTarget)

envelope_cobra

Whole script:

import cobra
import straindesign as sd
import matplotlib.pyplot as plt
from cobra.flux_analysis import production_envelope
from cobra.flux_analysis import flux_variability_analysis

rxnBiomass = "BIOMASS_SC5_notrace"
rxnTarget = "EX_etoh_e"
rxnGlucose = "EX_glc__D_e"
rxnOxygen = "EX_o2_e"

model = cobra.io.read_sbml_model("iMM904.xml")

medium_changed = model.medium.copy()
medium_changed[rxnGlucose] = 10.0
medium_changed[rxnOxygen] = 1.0
model.medium = medium_changed

# Strain desgin
sd.plot_flux_space(model, (rxnBiomass, rxnTarget))

# Cobrapy
prod_env = production_envelope(model, [rxnTarget], objective=rxnBiomass)
prod_env.plot(kind="line", x="flux_maximum", y=rxnTarget)
plt.show()

Thank you!

VonAlphaBisZulu commented 3 months ago

Hi again. I just wanted to tell you that I'm working on a solution for this. There seem to be two problems, both with GLPK at their root. Problem one is that the solver selection for computing the production envelope is inconsistent and would pick GLPK for some things, even though a different solver was selected by the user. After fixing this, I would produce the right production envelope for gurobi, scip and cplex, but not yet for GLPK, so that's where I'm at.

image

For the meantime, you might want to install gurobi or cplex and make it your model solver https://anaconda.org/conda-forge/pyscipopt until I have fixed the GLPK issue.

VonAlphaBisZulu commented 3 months ago

This was fixed with my last commit, by simply increasing the bounding tolerance for GLPK. Hope that doesn't worsen other aspects of straindesign that involve GLPK. I will close this issue for now, fix the other issue, bump the version and make a new release. Please test and reopen this issue if the problem persists.

VonAlphaBisZulu commented 3 months ago

I'm reopening this. Loosening the tolerance for GLPK breaks other parts of the package. Consider using SCIP, instead, for the time being. I will try to find a solution without changing GLPK tolerances.

Thank you again for raising the issue. This really helps to improve StrainDesign :)