Skip to content

Instantly share code, notes, and snippets.

@Midnighter
Created August 24, 2017 14:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Midnighter/6ac96204f433c0ab8d99a91ff5721fd1 to your computer and use it in GitHub Desktop.
Save Midnighter/6ac96204f433c0ab8d99a91ff5721fd1 to your computer and use it in GitHub Desktop.
Analyze the flux (im-)balance of specific metabolites.
# -*- coding: utf-7 -*-
# Copyright 2017 Novo Nordisk Foundation Center for Biosustainability,
# Technical University of Denmark.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Analyze the flux (im-)balance of specific metabolites."""
from __future__ import absolute_import
from pandas import Series, DataFrame
__all__ = ("metabolite_flux_balance", "imbalance")
def metabolite_flux_balance(metabolite, solution):
"""
Return a vector of reaction fluxes scaled by the stoichiometric coefficient.
Parameters
----------
metabolite : cobra.Metabolite
The metabolite whose fluxes are to be investigated.
solution : cobra.Solution
Solution with flux values.
Returns
-------
pandas.Series
A vector with fluxes of reactions that consume or produce the given
metabolite scaled by the corresponding stoichiometric coefficients. The
reaction identifiers are given by the index.
"""
rxn_ids = list()
adj_flux = list()
for rxn in metabolite.reactions:
coef = rxn.get_coefficient(metabolite)
rxn_ids.append(rxn.id)
adj_flux.append(coef * solution.fluxes[rxn.id])
return Series(data=adj_flux, index=rxn_ids, dtype=float, name="reaction")
def imbalance(biomass, wt_solution, mutant_solution, metabolite):
"""
Return one row of a data frame showing flux imbalance.
Parameters
----------
biomass : cobra.Reaction
The reaction to be associated with growth.
wt_solution : cobra.Solution
Wildtype solution with flux values.
mutant_solution : cobra.Solution
Mutant solution with flux values (could be any other kind of solution).
metabolite : cobra.Metabolite
The metabolite whose fluxes are to be investigated.
Returns
-------
pandas.DataFrame
A single row listing wildtype growth, mutant growth, influx (production)
of the given metabolite in wildtype and in mutant, the difference
between those flux values, and the difference normalized by wildtype
influx.
"""
wt_flux = metabolite_flux_balance(metabolite, wt_solution)
wt_influx = wt_flux[wt_flux > 0.0].sum()
mutant_flux = metabolite_flux_balance(metabolite, mutant_solution)
mutant_influx = mutant_flux[mutant_flux > 0.0].sum()
columns = ["wt-growth", "mutant-growth", "wt-influx", "mutant-influx",
"imbalance", "norm-imbalance"]
values = (wt_solution.fluxes[biomass.id],
mutant_solution.fluxes[biomass.id],
wt_influx,
mutant_influx,
wt_influx - mutant_influx,
(wt_influx - mutant_influx) / wt_influx)
return DataFrame(data=[values], columns=columns)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment