Skip to content

Commit

Permalink
Merge pull request #149 from Fxe/dev
Browse files Browse the repository at this point in the history
msatpcorrection.py default path + reactions pathways
  • Loading branch information
Fxe authored Sep 26, 2024
2 parents 1a0a562 + 67957d3 commit 87143dd
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 48 deletions.
1 change: 1 addition & 0 deletions modelseedpy/biochem/modelseed_biochem.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ def _load_reactions(
None,
o.get("status"),
o.get("source"),
pathways=o.get("pathways"),
)
rxn.add_metabolites(reaction_metabolites)
if rxn.id in aliases:
Expand Down
3 changes: 3 additions & 0 deletions modelseedpy/biochem/modelseed_reaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ def __init__(
status=None,
source=None,
flags=None,
pathways=None,
):

super().__init__(rxn_id, name, subsystem, lower_bound, upper_bound)
Expand Down Expand Up @@ -165,6 +166,8 @@ def __init__(
if flags:
self.flags |= set(flags)

self.pathways = pathways

@property
def compound_ids(self):
return None
Expand Down
58 changes: 19 additions & 39 deletions modelseedpy/core/msatpcorrection.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,14 @@
# -*- coding: utf-8 -*-
import logging
import cobra
import copy
import json
import time
import pandas as pd
from os.path import abspath as _abspath
from os.path import dirname as _dirname
from optlang.symbolics import Zero, add
from modelseedpy.core.rast_client import RastClient
from modelseedpy.core.msgenome import normalize_role
from modelseedpy.core.msmodel import (
get_gpr_string,
get_reaction_constraints_from_direction,
)
from cobra.core import Gene, Metabolite, Model, Reaction
from modelseedpy.core.msmodelutl import MSModelUtil
from modelseedpy.core.mstemplate import MSTemplateBuilder
from modelseedpy.core import FBAHelper, MSGapfill, MSMedia
from modelseedpy.fbapkg.mspackagemanager import MSPackageManager
from modelseedpy.helpers import get_template

logger = logging.getLogger(__name__)
logger.setLevel(
logging.INFO
) # When debugging - set this to INFO then change needed messages below from DEBUG to INFO

_path = _dirname(_abspath(__file__))

min_gap = {
"Glc.O2": 5,
Expand Down Expand Up @@ -90,12 +72,6 @@ def __init__(
self.modelutl = MSModelUtil.get(model_or_mdlutl)
# Setting atpcorrection attribute in model utl so link is bidirectional
self.modelutl.atputl = self

if default_media_path:
self.default_media_path = default_media_path
else:
self.default_media_path = _path + "/../data/atp_medias.tsv"

self.compartment = compartment

if atp_hydrolysis_id and atp_hydrolysis_id in self.model.reactions:
Expand All @@ -106,21 +82,20 @@ def __init__(

self.media_hash = {}
self.atp_medias = []

if load_default_medias:
self.load_default_medias()
self.load_default_medias(default_media_path)

media_ids = set()
for media in atp_medias:
if isinstance(media, list):
if media[0].id in media_ids:
raise ValueError("media ids not unique")
media_ids.add(media[0].id)
self.atp_medias.append(media)
else:
if media.id in media_ids:
raise ValueError("media ids not unique")
media_ids.add(media.id)
self.atp_medias.append([media, 0.01])
for media_or_list in atp_medias:
media = (
media_or_list[0] if isinstance(media_or_list, list) else media_or_list
)
min_obj = media_or_list[1] if isinstance(media_or_list, list) else 0.01
if media.id in media_ids:
raise ValueError("media ids not unique")
media_ids.add(media.id)
self.atp_medias.append((media, min_obj))
self.media_hash[media.id] = media
if "empty" not in self.media_hash:
media = MSMedia.from_dict({})
Expand Down Expand Up @@ -164,8 +139,13 @@ def load_default_template(self):
get_template("template_core"), None
).build()

def load_default_medias(self):
filename = self.default_media_path
def load_default_medias(self, default_media_path=None):
if default_media_path is None:
import os.path as _path

current_file_path = _path.dirname(_path.abspath(__file__))
default_media_path = f"{current_file_path}/../data/atp_medias.tsv"
filename = default_media_path
medias = pd.read_csv(filename, sep="\t", index_col=0).to_dict()
for media_id in medias:
media_d = {}
Expand All @@ -179,7 +159,7 @@ def load_default_medias(self):
media.id = media_id
media.name = media_id
min_obj = 0.01
self.atp_medias.append([media, min_gap.get(media_d, min_obj)])
self.atp_medias.append((media, min_gap.get(media_d, min_obj)))

@staticmethod
def find_reaction_in_template(model_reaction, template, compartment):
Expand Down
10 changes: 1 addition & 9 deletions modelseedpy/core/msgenome.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,7 @@ def add_features(self, feature_list: list):
self.features += feature_list

@staticmethod
def from_fasta(
filename, contigs=0, split="|", h_func=None
): # !!! the contigs argument is never used
genome = MSGenome()
genome.features += read_fasta(filename, split, h_func)
return genome

@staticmethod
def from_fasta2(filename, split=" ", h_func=None):
def from_fasta(filename, split=" ", h_func=None):
genome = MSGenome()
genome.features += read_fasta2(filename, split, h_func)
return genome
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
install_requires=[
"networkx >= 2.4",
"cobra >= 0.29.0",
"pandas >= 2.2.2",
"scikit-learn == 1.2.0", # version lock for pickle ML models
"scipy >= 1.5.4",
"chemicals >= 1.0.13",
Expand Down

0 comments on commit 87143dd

Please sign in to comment.