Commit 91e86f6d authored by Moritz E. Beber's avatar Moritz E. Beber

refactor: make the function call more flexible

parent 03fa2ed6
Pipeline #102087556 failed with stages
in 3 minutes and 36 seconds
......@@ -27,47 +27,60 @@
import logging
from operator import attrgetter
from typing import List, Tuple
from typing import Dict, List
from equilibrator_cache import CompoundCache
from equilibrator_cache.compatibility import map_cobra_metabolites
from equilibrator_api import ccache
from equilibrator_api.phased_reaction import PhasedReaction
logger = logging.getLogger(__name__)
def translate_cobra_reactions(
def map_cobra_reactions(
cache: CompoundCache,
reactions: List["cobra.Reaction"],
annotation_preference: Tuple[str] = (
"inchikey",
"inchi",
"metanetx.chemical",
"seed.compound",
"bigg.metabolite",
"metacyc",
),
) -> List[PhasedReaction]:
"""Translate COBRA reactions to eQuilibrator phased reactions."""
**kwargs
) -> Dict["cobra.Reaction", PhasedReaction]:
"""
Translate COBRA reactions to eQuilibrator phased reactions.
Parameters
----------
cache : equilibrator_cache.CompoundCache
reactions : iterable of cobra.Reaction
A list of reactions to map to equilibrator phased reactions.
Other Parameters
----------------
kwargs :
Any further keyword arguments are passed to the underlying function
for mapping metabolites.
Returns
-------
dict
A mapping from COBRA to equilibrator phased reactions where such a
mapping could be established.
"""
metabolites = sorted(
{m for r in reactions for m in r.metabolites}, key=attrgetter("id")
)
met2cmpnd = map_cobra_metabolites(
ccache, metabolites, annotation_preference
)
eq_reactions = []
met2cmpnd = map_cobra_metabolites(cache, metabolites, **kwargs)
mapping = {}
for rxn in reactions:
try:
stoichiometry = {
met2cmpnd[met]: coef for met, coef in rxn.metabolites
met2cmpnd[met]: coef for met, coef in rxn.metabolites.items()
}
except KeyError:
logger.warning("Incomplete compound stoichiometry in '%s'.", rxn.id)
continue
PhasedReaction(
mapping[rxn] = PhasedReaction(
sparse=stoichiometry,
arrow="<=>" if rxn.reversibility else "=>",
rid=rxn.id,
)
return eq_reactions
return mapping
# The MIT License (MIT)
#
# Copyright (c) 2019 Novo Nordisk Foundation Center for Biosustainability,
# Technical University of Denmark.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""Test that COBRA reactions are translated correctly."""
from pathlib import Path
import cobra
import pytest
from cobra.io import read_sbml_model
from equilibrator_api import ccache
from equilibrator_api import compatibility as compat
@pytest.fixture(scope="module")
def core_model() -> cobra.Model:
"""Return the E. coli core model."""
model_path = Path(__file__).parent / "data" / "e_coli_core.xml.gz"
return read_sbml_model(str(model_path))
@pytest.mark.parametrize(
"reaction_ids, expected", [([], 0), (["PDH", "PFK"], 2)]
)
def test_map_cobra_reactions(core_model, reaction_ids, expected):
"""Expect that reactions can be mapped."""
reactions = core_model.reactions.get_by_any(reaction_ids)
mapping = compat.map_cobra_reactions(
ccache,
reactions,
annotation_preference=("inchi_key",),
inchi_key_namespace="inchi_key",
)
assert len(mapping) == expected
......@@ -5,6 +5,7 @@ envlist = isort, black, flake8, safety, py3{6,7}
setenv =
PYTHONUNBUFFERED=1
deps =
cobra
pytest
pytest-cov
pytest-raises
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment