Skip to content
This repository was archived by the owner on Feb 9, 2024. It is now read-only.

Commit 87bdd46

Browse files
committed
Addiing files to run model building on sequoia
1 parent b84f317 commit 87bdd46

File tree

4 files changed

+427
-0
lines changed

4 files changed

+427
-0
lines changed

CarbonSources/GapfillGenomeModels.py

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import platform
2+
import sys
3+
import json
4+
import cobra
5+
import cplex
6+
import re
7+
import os
8+
from os.path import exists
9+
import logging
10+
from configparser import ConfigParser
11+
config = ConfigParser()
12+
config.read("config.cfg")
13+
paths = config.get("script", "syspaths").split(";")
14+
for path in paths:
15+
sys.path.append(path)
16+
import cobrakbase
17+
from escher import Builder
18+
from optlang.symbolics import Zero, add
19+
from modelseedpy import MSPackageManager, MSGapfill, MSGrowthPhenotypes, MSModelUtil, MSATPCorrection
20+
from cobrakbase.core.kbasefba.newmodeltemplate_builder import NewModelTemplateBuilder
21+
from annotation_ontology_api.annotation_ontology_apiServiceClient import annotation_ontology_api
22+
from modelseedpy.helpers import get_template
23+
import pandas as pd
24+
25+
kbase_api = cobrakbase.KBaseAPI()
26+
anno_api = annotation_ontology_api()
27+
glc_o2_atp_media = kbase_api.get_from_ws("Glc.O2.atp",94026)
28+
gmm = kbase_api.get_from_ws("Carbon-D-Glucose","KBaseMedia")
29+
template = kbase_api.get_from_ws("GramNegModelTemplateV4","NewKBaseModelTemplates")
30+
core = NewModelTemplateBuilder.from_dict(get_template('template_core'), None).build()
31+
types = ["RAST","DRAM","DRAM.RAST"]
32+
genomeid = sys.argv[1]
33+
genomews = int(sys.argv[2])
34+
modelws = int(sys.argv[3])
35+
#Computing reaction scores
36+
reaction_genes = {}
37+
output = anno_api.get_annotation_ontology_events({
38+
"input_ref" : str(genomews)+"/"+genomeid,
39+
})
40+
events = output["events"]
41+
for event in events:
42+
for gene in event["ontology_terms"]:
43+
for term in event["ontology_terms"][gene]:
44+
if "modelseed_ids" in term:
45+
for rxn in term["modelseed_ids"]:
46+
newrxn = re.sub("^MSRXN:","",rxn)
47+
if newrxn not in reaction_genes:
48+
reaction_genes[newrxn] = {}
49+
if gene not in reaction_genes[newrxn]:
50+
reaction_genes[newrxn][gene] = 0
51+
reaction_genes[newrxn][gene] += 1
52+
#Gapfilling RAST, DRAM, and RAST.DRAM models
53+
for mdltype in types:
54+
mdlid = genomeid+"."+mdltype+".mdl"
55+
if not exists("GFModels/"+mdltype+"/"+genomeid+".json"):
56+
#Getting model, media, templates
57+
model = kbase_api.get_from_ws(mdlid,modelws)
58+
model.solver = 'optlang-cplex'
59+
#Computing ATP to build tests to ensure gapfilling doesn't break ATP
60+
atpmethod = MSATPCorrection(model,core,[glc_o2_atp_media])
61+
evaluation = atpmethod.evaluate_growth_media()
62+
atpmethod.restore_noncore_reactions()
63+
threshold = 4*evaluation[glc_o2_atp_media.id]
64+
if threshold > 50:
65+
threshold = 50
66+
tests = [{"media":glc_o2_atp_media,"objective":atpmethod.atp_hydrolysis.id,"is_max_threshold":True,"threshold":threshold}]
67+
#Gapfilling
68+
msgapfill = MSGapfill(model,[template],[],tests,reaction_genes,[])
69+
gfresults = msgapfill.run_gapfilling(gmm,"bio1")
70+
if gfresults == None:
71+
print("Gapfilling failed on "+mdlid)
72+
else:
73+
model = msgapfill.integrate_gapfill_solution(gfresults)
74+
pkgmgr = MSPackageManager.get_pkg_mgr(model)
75+
pkgmgr.getpkg("KBaseMediaPkg").build_package(gmm)
76+
solution = model.optimize()
77+
print(mdltype+"\t"+genomeid+"\t"+str(solution.objective_value))
78+
if solution.objective_value > 0.01:
79+
cobra.io.save_json_model(model,"GFModels/"+mdltype+"/"+genomeid+".json")
80+
else:
81+
print("Model failed validation and will not be saved!")

0 commit comments

Comments
 (0)