Coder Social home page Coder Social logo

epfl-lcsb / pytfa Goto Github PK

View Code? Open in Web Editor NEW
37.0 8.0 26.0 3.79 MB

A Python 3 implementation of Thermodynamics-based Flux Analysis

Home Page: https://lcsb.epfl.ch/

License: Apache License 2.0

Shell 2.57% Batchfile 0.04% Python 96.87% Dockerfile 0.51%

pytfa's Introduction

pyTFA

PyPI Documentation Status Build Status Codecov Codacy branch grade license

Thermodynamics-based Flux Analysis, in Python. Paper : Pierre Salvy, Georgios Fengos, Meric Ataman, Thomas Pathier, Keng C Soh, Vassily Hatzimanikatis. "pyTFA and matTFA: a Python package and a Matlab toolbox for Thermodynamics-based Flux Analysis" Bioinformatics (2018), bty499, DOI: https://doi.org/10.1093/bioinformatics/bty499

Implements: Christopher S. Henry, Linda J. Broadbelt, and Vassily Hatzimanikatis. "Thermodynamics-based metabolic flux analysis." Biophysical journal 92.5 (2007): 1792-1805. DOI: https://doi.org/10.1529/biophysj.106.093138

Requirements

You will need to have Git-LFS in order to properly download some binary files:

git clone https://github.com/EPFL-LCSB/pytfa.git /path/to/pytfa
cd /path/to/pytfa
git lfs install
git lfs pull

This module was developed in Python 3.5, and it is recommended to run Python 3.5 to run commercial solvers such as Gurobi and CPLEX. Other Python versions (2.7, 3.4) should also work (see the CI builds)

This module requires COBRApy, as well as optlang to work properly. The installer should take care of that for you. You might also want to install a dedicated solver. GLPK, CPLEX and Gurobi are supported.

Container-based install

You might want to use this program inside of a container. The docker/ subfolder has all the necessary information and source files to set it up.

Setup

This step is not required if you're using the container, which bundles all this.

You can install this module with pip:

For Python 3, you might have to use pip3 instead of pip

pip3 install pytfa

or from source

git clone https://github.com/EPFL-LCSB/pytfa.git /path/to/pytfa
pip3 install -e /path/to/pytfa

Quick start

Three tutorial files detail thoroughly normal usages of the pytfa package. They can be found at:

pytfa
└── tutorials
    ├── figure_paper.py
    ├── tutorial_basics.py
    └── tutorial_sampling.py

More information can be found here.

Documentation

Documentation is hosted at Read the Docs

Alternatively you can also generate the docs locally.

Make sure sphinx is installed, and install as well the theme (this is already bundled with the container):

pip install sphinx sphinx-rtd-theme

You can then generate the documentation with this command:

cd work/pytfa/doc && make html

The resulting HTML files will be located in work/pytfa/doc/_build.

Testing the code

We recommend using the Docker container for testing the code, as it comes with everything bundled.

Install pytest if you don't already have it (pip install pytest, already included in the container), then start the tests with the pytest command.

Usage

First, create your COBRApy model. Make sure to define the additional values required by pyTFA, as said in the "Models" page of the documentation.

If you already have a Matlab model with thermodynamic data, you might want to use pytfa.io.import_matlab_model. Otherwise, have a look at the COBRApy documentation, then add the required properties.

If you're using a specific solver, don't forget to tell COBRApy about it by setting the solver property of your model to the name of your solver. See the COBRApy documentation for more information about this.

Thermodynamic database

You also need a thermodynamic database. Use thermoDBconverter.py if you have a thermodynamic database from Matlab you wish to import to Python.

Thermodynamic databases are stored in .thermodb files and can be easily loaded with pytfa.io.load_thermoDB.

Example script

Here is an example script :

import pytfa
from pytfa.io import import_matlab_model, load_thermoDB


cobra_model = import_matlab_model('../models/small_yeast.mat')

thermo_data = load_thermoDB('../data/thermo_data.thermodb')

mytfa = pytfa.ThermoModel(thermo_data, cobra_model)
mytfa.solver = 'optlang-cplex'

## TFA conversion
mytfa.prepare()
mytfa.convert()

## Info on the model
mytfa.print_info()

## Optimality
tfa_solution = mytfa.optimize()

License

The software in this repository is put under an APACHE-2.0 licensing scheme - please see the LICENSE file for more details.

pytfa's People

Contributors

carrascomj avatar embt avatar omidoftadeh avatar psalvy avatar reallcsb avatar redlinkk avatar remidhum avatar wealing avatar weilandtd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pytfa's Issues

KeyError and potential solution in sampling tutorial

Dear Pierre and colleagues,

First of all, thank you for the great tool, the neat support in the docs and the informative articles.

A minor issue/suggestion:
In tutorial_sampling.py, using model iJO1366.json, I got a KeyError on line 72, while applying flux directionality. Going into the code in /analysis/manipulation.py, I found that if I change the end of the code on line 70 from
backward_use.variable.lb = round(solution.x_dict[backward_use.name])
to
backward_use.variable.lb = round(solution.x_dict[backward_use.id])
and similarly for the other three lines as well, I can enforce the use of the raw id name, without the "BU_" and "FU_" prefixes.

Not sure if it was a common issue, but in case anyone else would run into the same, this solved my problem.

Keep up the great work!

Best, Adam

Thermo DB as SQLite

While a compressed pickle is not a bad way to distribute data. It's not very future proof. You might consider creating database models with SQLAlchemy and storing everything in a SQLite database. I'm happy to help if you have any questions about that.

metabolomics integration

Hi,

do you plan to implement the integration of metabolomics (absolute metabolite concentrations) for dG calculation?

tutorial_basics lots of warnings when mytfa.prepare()

Dear pytfa team,
I'd like to try pytfa for some analysis.

I started by working through the tutorial_basics.py. When running the script, it throws a lot of 'Warning : NULL/U' and 'Warning : Null/L' messages at me during the mytfa.prepare() step.

Can you explain the meaning of these warnings, is it expected behavior?

Thanks a lot.

Cheers,
Michael

deltaGerr automatically sets to 2.0 after loading thermo model

Uppon loading the model, the deltaGerr values are set to 2.0:

tmodel = load_json_model('../tmodels/outputs/tmodel.json')

In the .json file, the deltaGerr values are correctly set to 0.0.

The model is loaded after setting the deltaGerr of reactions to 0.0 and exporting the model:

for rxn in model.reactions:
    rxn.thermo['deltaGRerr'] = 0.0

#...

save_json_model(tmodel, './outputs/tmodel.json')

KeyError on .convert()

Hello,

I'm trying to convert my model to TFA, and I'm getting an unmanaged KeyError for some of my metabolites. It usually looks like that:

mytfa.prepare()
mytfa.convert()

2021-04-23 14:02:02,924 - thermomodel_ - INFO - # Model preparation starting...
2021-04-23 14:02:04,180 - thermomodel_ - WARNING - Warning : Pb/Pb
2021-04-23 14:02:04,181 - thermomodel_ - WARNING - Warning : Pb/Pb
2021-04-23 14:02:04,296 - thermomodel_ - INFO - # Model preparation done.
2021-04-23 14:02:04,297 - thermomodel_ - INFO - # Model conversion starting...
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-9-ebc2fae6d451> in <module>
      1 ## TFA conversion
      2 mytfa.prepare()
----> 3 mytfa.convert()#add_displacement = True)
      4 
      5 ## Info on the cobra_model

/media/data/mdz/bio_projects/tac125_metabolic_modelling/analysis/14_tfa_model_construction/pytfa/pytfa/thermo/tmodel.py in convert(self, add_potentials, add_displacement, verbose)
    658         for rxn in self.reactions:
    659             self._convert_reaction(
--> 660                 rxn, add_potentials, add_displacement, verbose
    661             )
    662 

/media/data/mdz/bio_projects/tac125_metabolic_modelling/analysis/14_tfa_model_construction/pytfa/pytfa/thermo/tmodel.py in _convert_reaction(self, rxn, add_potentials, add_displacement, verbose)
    519                             # changes in deltaGFs in the RHS term
    520                             LC_ChemMet += (
--> 521                                 self.LC_vars[met] * RT * rxn.metabolites[met]
    522                             )
    523 

KeyError: <Metabolite cpd15684_c at 0x7f3762226518>

I tried to understand what is going on, but without any success. I just know that self.LC_vars[met] is raising the error. Does it have something to do with how my metabolites/reactions are defined?

SEED identifiers in thermo DB

Since I wasn't able to use the models distributed with the package (cf. #2), I tried to use another model but then discovered that the compounds in the thermodynamic database are all SEED identifiers. While this is not a problem, of course, using MetaNetX identifiers would be a great improvement as it easily maps to a whole range of databases.

Permission denied when running the docker image

So, my local user is part of the docker group. That leads to the following situation when using the run command that mounts my local work and pytfa directories into the container:

My local user cannot be mapped to a user in the docker container. Thus /home/pytfa/work and /src/pytfa are owned by 1001:1001 and access is denied to the pytfa user.

Although it can be useful, of course, to mount volumes into the container I'd say it shouldn't be necessary for the functioning of the image. Unfortunately, it is necessary as pytfa won't be installed.

If you really want to keep this behavior, an entrypoint script might be the right way to go. See this comment.

tutorial_basic.py ERROR - Relaxation could not complete (no DeltaG relaxation found)

Dear pyTFA team,
I tried running the tutorial_basics.py script (after you helped me understand the warnings) which gave me a result for the regular thermodynamics FBA. However, when trying to run the second part of the script where a relaxed model is generated the script throws the following error:

thermomodel_new - ERROR - Relaxation could not complete (no DeltaG relaxation found)

According to the annotation in the source code this either means the model is infeasible or something else went wrong. Could you hint at what I'm doing wrong here?

Thanks a lot.

Cheers,
Michael

Problem with pytfa.relax_dgo due to sympy

Hello,
first of all big thanks for supporting the package! I had a problem with the function relax_dgo and also a possible solution:
from pytfa.optim.relaxation import relax_dgo
relaxed_model, slack_model, relax_table = relax_dgo(mytfa)

The follwing Error occours:

~\AppData\Roaming\Python\Python38\site-packages\pytfa\optim\utils.py in symbol_sum(variables)
108 k=0
109 # If we encounter a zero, which is a special type, increase k
--> 110 while isinstance(variables[k], sympy.numbers.Zero) and k<len(variables):
111 k+=1
112 if k == len(variables):

AttributeError: module 'sympy' has no attribute 'numbers'

By changing sympy.numbers.Zero to sympy.core.numbers.Zero in ~\AppData\Roaming\Python\Python38\site-packages\pytfa\optim\utils.py the Error is gone.
This seems like sympy changed something there.

I'm using Python Python 3.8.5 and sympy version 1.6.2

Regards,
Emil

Unable to open Matlab models

I tried to run the tutorials and with each one of them I get the same ValueError when the scripts try to read the models. As an example, python tutorial_basics.py causes the following traceback:

Traceback (most recent call last):
  File "tutorial_basics.py", line 18, in <module>
    cobra_model = import_matlab_model('../models/small_ecoli.mat')
  File ".virtualenvs/tfa/lib/python3.6/site-packages/pytfa/io/base.py", line 39, in import_matlab_model
    mat_data = loadmat(path)
  File ".virtualenvs/tfa/lib/python3.6/site-packages/scipy/io/matlab/mio.py", line 135, in loadmat
    MR = mat_reader_factory(file_name, appendmat, **kwargs)
  File ".virtualenvs/tfa/lib/python3.6/site-packages/scipy/io/matlab/mio.py", line 59, in mat_reader_factory
    mjv, mnv = get_matfile_version(byte_stream)
  File ".virtualenvs/tfa/lib/python3.6/site-packages/scipy/io/matlab/miobase.py", line 241, in get_matfile_version
    raise ValueError('Unknown mat file type, version %s, %s' % ret)
ValueError: Unknown mat file type, version 50, 56

I'm using Python 3.6, cobra==0.11.2 , and scipy==0.19.0. I get exactly the same error with scipy==1.0.0, though.

Two comments:

  1. There is a function in cobrapy cobra.io.load_matlab_model which may or may not be the same as pytfa.io.import_matlab_model. Internally they use the same scipy function. So same error.
  2. I would have thought that, inside Matlab, you can export models to SBML by now. SBML is the universal exchange format for models and distributing Matlab models with a Python package doesn't make a lot of sense to me.

Use displacement or not?

Hi!

In the basic tutorial the ThermoModel is converted without thermodynamic displacement, while you use add_displacement = True in some of the other tutorials and in the scripts creating the figures. Could you please explain the recommended use and how this affects the TFA / Flux variability calculations?

Integration with eQuilibrator

As an user, I would like to use the eQuilibrator-API as the thermodynamic database. Thus, eQuilibrator would parse the reactions, calculate ΔGr for each reaction and use the data to prepare and convert a cobra.Model into a pyTFA.ThermoModel. Is there any reason that hampers this integration?

I would be happy to help with the implementation, maybe as an extra dependency. I have used the tutorial in tutorials/figure_paper.py to try to reproduce the results just by adding reaction data, but the results differ (at the end of this notebook).

EDIT: after adding the pH and ionic strength information about each metabolite (without running the built-in .prepare() method), the results are exactly reproduced.

Problem with installation

Hello.

When trying to build the docker container an error appears:

> [10/21] RUN /utils/install_cplex.sh:
#14 0.481 /bin/sh: 1: /utils/install_cplex.sh: not found
------
executor failed running [/bin/sh -c /utils/install_cplex.sh]: exit code: 127

There seems to be a utils/install_cplex.sh file. I have also put the necessary files in the solvers folder. Maybe it has something to do with me using Windows?

Relaxation of log concentration error

Hi! Thanks for supporting this package,

I am trying to use relax_lc (same is in the example with relax_dgo). I am using EColi_BL21 from the package replicating the relax_dgo example. Is it possible to fix on my side?

Description of the error:

If I don't constrain the biomass function

mytfa.reactions.get_by_id(biomass_rxn).lower_bound = 0.0

I get the following error

ValueError                                Traceback (most recent call last)
<ipython-input-17-78e5fa8cc847> in <module>
      6 
      7     mytfa.reactions.get_by_id(biomass_rxn).lower_bound = 0.0*fba_value
----> 8     relaxed_model, slack_model, relax_table = relax_lc(mytfa)
      9 
     10     original_model, mytfa = mytfa, relaxed_model

/miniconda3/envs/rbcenv/lib/python3.7/site-packages/pytfa/optim/relaxation.py in relax_lc(tmodel, metabolites_to_ignore, solver)
    418                             'ub_change',
    419                             'lb_out',
--> 420                             'ub_out']
    421 
    422     tmodel.logger.info('\n' + relax_table.__str__())

/miniconda3/envs/rbcenv/lib/python3.7/site-packages/pandas/core/generic.py in __setattr__(self, name, value)
   5078         try:
   5079             object.__getattribute__(self, name)
-> 5080             return object.__setattr__(self, name, value)
   5081         except AttributeError:
   5082             pass

pandas/_libs/properties.pyx in pandas._libs.properties.AxisProperty.__set__()

/miniconda3/envs/rbcenv/lib/python3.7/site-packages/pandas/core/generic.py in _set_axis(self, axis, labels)
    636 
    637     def _set_axis(self, axis, labels):
--> 638         self._data.set_axis(axis, labels)
    639         self._clear_item_cache()
    640 

/miniconda3/envs/rbcenv/lib/python3.7/site-packages/pandas/core/internals/managers.py in set_axis(self, axis, new_labels)
    153             raise ValueError(
    154                 'Length mismatch: Expected axis has {old} elements, new '
--> 155                 'values have {new} elements'.format(old=old_len, new=new_len))
    156 
    157         self.axes[axis] = new_labels

ValueError: Length mismatch: Expected axis has 0 elements, new values have 6 elements

If I constrain the biomass reaction

mytfa.reactions.get_by_id(biomass_rxn).lower_bound = 0.5*fba_value

I got the following error

2020-10-12 13:09:42,209 - thermomodel_None - INFO - # Model initialized with units kcal/mol and temperature 298.15 K
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-18-a9a6b332d808> in <module>
      6 
      7     mytfa.reactions.get_by_id(biomass_rxn).lower_bound = 0.5*fba_value
----> 8     relaxed_model, slack_model, relax_table = relax_lc(mytfa)
      9 
     10     original_model, mytfa = mytfa, relaxed_model

/miniconda3/envs/rbcenv/lib/python3.7/site-packages/pytfa/optim/relaxation.py in relax_lc(tmodel, metabolites_to_ignore, solver)
    282     # Do not relax if cobra_model is already optimal
    283     try:
--> 284         solution = tmodel.optimize()
    285     except SolverError as SE:
    286         status = tmodel.solver.status

/miniconda3/envs/rbcenv/lib/python3.7/site-packages/pytfa/core/model.py in optimize(self, objective_sense, **kwargs)
    393         try:
    394             # self._hidden_optimize_call(kwargs)
--> 395             Model.optimize(self, **kwargs)
    396             solution = self.get_solution()
    397             self.solution = solution

/miniconda3/envs/rbcenv/lib/python3.7/site-packages/cobra/core/model.py in optimize(self, objective_sense, raise_error)
   1078                 objective_sense, original_direction)
   1079         self.slim_optimize()
-> 1080         solution = get_solution(self, raise_error=raise_error)
   1081         self.objective.direction = original_direction
   1082         return solution

/miniconda3/envs/rbcenv/lib/python3.7/site-packages/cobra/core/solution.py in get_solution(model, reactions, metabolites, raise_error)
    229     fluxes = empty(len(reactions))
    230     reduced = empty(len(reactions))
--> 231     var_primals = model.solver.primal_values
    232     shadow = empty(len(metabolites))
    233     if model.solver.is_integer:

/miniconda3/envs/rbcenv/lib/python3.7/site-packages/optlang/interface.py in primal_values(self)
   1234         """
   1235         return collections.OrderedDict(
-> 1236             zip(self._get_variables_names(), self._get_primal_values())
   1237         )
   1238 

/miniconda3/envs/rbcenv/lib/python3.7/site-packages/optlang/interface.py in _get_primal_values(self)
   1245         """
   1246         # Fallback, if nothing faster is available
-> 1247         return [variable.primal for variable in self.variables]
   1248 
   1249     @property

/miniconda3/envs/rbcenv/lib/python3.7/site-packages/optlang/interface.py in <listcomp>(.0)
   1245         """
   1246         # Fallback, if nothing faster is available
-> 1247         return [variable.primal for variable in self.variables]
   1248 
   1249     @property

/miniconda3/envs/rbcenv/lib/python3.7/site-packages/optlang/interface.py in primal(self)
    272         """The primal of variable (None if no solution exists)."""
    273         if self.problem:
--> 274             return self._get_primal()
    275         else:
    276             return None

/miniconda3/envs/rbcenv/lib/python3.7/site-packages/optlang/gurobi_interface.py in _get_primal(self)
    156 
    157     def _get_primal(self):
--> 158         return self._internal_variable.getAttr('X')
    159 
    160     @property

var.pxi in gurobipy.Var.getAttr()

attrutil.pxi in gurobipy.__getattr()

AttributeError: Unable to retrieve attribute 'X'

Problem with building Docker container

Hi,

I just cloned the repository and tried building the docker container following the instructions in pytfa/docker.
During step [ 6/21] RUN pip install -r requirements.txt, I get the following error:

ERROR: Command errored out with exit status 1: /usr/local/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-2ysj1c0r/python-libsbml_450b9997bf4247338ffe25308f09a29e/setup.py'"'"'; file='"'"'/tmp/pip-install-2ysj1c0r/python-libsbml_450b9997bf4247338ffe25308f09a29e/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(file) if os.path.exists(file) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, file, '"'"'exec'"'"'))' install --record /tmp/pip-record-hc4w98os/install-record.txt --single-version-externally-managed --compile --install-headers /usr/local/include/python3.6m/python-libsbml Check the logs for full command output.

I would attach the log, but I cannot find it.

Trying to remove python-libsbml from the requirements file did not fix the issue.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.