Commit 03fa2ed6 authored by Moritz E. Beber's avatar Moritz E. Beber

feat: translate COBRA reactions

parent a42f90b7
Pipeline #100415741 failed with stages
in 4 minutes and 55 seconds
# 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.
"""Provide functions for compatibility with COBRA."""
import logging
from operator import attrgetter
from typing import List, Tuple
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(
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."""
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 = []
for rxn in reactions:
try:
stoichiometry = {
met2cmpnd[met]: coef for met, coef in rxn.metabolites
}
except KeyError:
logger.warning("Incomplete compound stoichiometry in '%s'.", rxn.id)
continue
PhasedReaction(
sparse=stoichiometry,
arrow="<=>" if rxn.reversibility else "=>",
rid=rxn.id,
)
return eq_reactions
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