Spaces:
Runtime error
Runtime error
File size: 11,812 Bytes
66061aa |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
import pandas as pd
import pulp
import pdb
import os
import json
from rdkit import Chem
# pulp_solver = pulp.solvers.CPLEX_CMD(path=None, keepFiles=0, mip=1, msg=1,
# options=['mip tolerances mipgap 0', 'mip tolerances absmipgap 0',
# 'mip tolerances integrality 0', 'simplex tolerances optimality 1E-9',
# 'simplex tolerances feasibility 1E-9',], timelimit=1200)
def count_substructures(radius,molecule):
"""Helper function for get the information of molecular signature of a
metabolite. The relaxed signature requires the number of each substructure
to construct a matrix for each molecule.
Parameters
----------
radius : int
the radius is bond-distance that defines how many neighbor atoms should
be considered in a reaction center.
molecule : Molecule
a molecule object create by RDkit (e.g. Chem.MolFromInchi(inchi_code)
or Chem.MolToSmiles(smiles_code))
Returns
-------
dict
dictionary of molecular signature for a molecule,
{smiles: molecular_signature}
"""
m = molecule
smi_count = dict()
atomList = [atom for atom in m.GetAtoms()]
for i in range(len(atomList)):
env = Chem.FindAtomEnvironmentOfRadiusN(m,radius,i)
atoms=set()
for bidx in env:
atoms.add(m.GetBondWithIdx(bidx).GetBeginAtomIdx())
atoms.add(m.GetBondWithIdx(bidx).GetEndAtomIdx())
# only one atom is in this environment, such as O in H2O
if len(atoms) == 0:
atoms = {i}
smi = Chem.MolFragmentToSmiles(m,atomsToUse=list(atoms),
bondsToUse=env,canonical=True)
if smi in smi_count:
smi_count[smi] = smi_count[smi] + 1
else:
smi_count[smi] = 1
return smi_count
def novoStoic_minFlux_relaxedRule(exchange_mets, novel_mets,project,iterations,pulp_solver,use_direction):
"""apply reaction rules generated from a more relaxed manner to search for
reaction rules that are able to fill the gap between the source and sink
metabolites.
- rePrime procedure is more similar to a morgan fingerprints
- the relaxed rule is generated from substructures without considering the
bond that connect the atoms at the edge of the substructure to the rest
of the molecules
Parameters
----------
exchange_mets : dict
overall stoichiometry of source and sink metabolites, {met: stoic,...}
This is a important input for novoStoic to run correctly because the
method requires that overall moieties are balanced.
novel_mets : list
list of novel metabolites that are not in the database (novoStoic/data/
metanetx_universal_model_kegg_metacyc_rhea_seed_reactome.json)
filtered_rules : list
list of rules that are filtered by the user (based on expert knowldedge)
to reduce the running time of the novoStoic search process
project : string
a path to store the tmp information of result from running novoStoic
iterations : int
the number of iterations of searching for alternative solutions
data_dir : type
Description of parameter `data_dir`.
Returns
-------
None
all the outputs are saved in the project folder.
"""
if not os.path.exists(project):
os.makedirs(project)
# the maximum flux of a reaction
M = 2
data_dir = './data'
# read csv files with molecular signatures and reaction rules
molecular_signature = json.load(open(
os.path.join(data_dir, 'decompose_vector_ac.json')))
molsigs = pd.DataFrame.from_dict(molecular_signature).fillna(0)
rules = pd.read_csv(
os.path.join(data_dir, "relaxed_rule_noduplic.csv"), index_col=0
)
###### sets ############
moiety_index = rules.index.tolist() # moiety sets
rules_index = rules.columns.values.tolist()
print("Number of rules used in this search:",len(rules_index))
exchange_index = exchange_mets.keys()
###### parameters ######
# T(m,r) contains atom stoichiometry for each rule
T = rules.to_dict(orient="index")
# C(m,i) contains moiety cardinality for each metabolite
C = molsigs.to_dict(orient="index")
for m in moiety_index:
C[m]["C00080"] = 0
C[m]["C00282"] = 0
# add metabolites that are not present in current database
for met in novel_mets:
# molsigs_product = pd.read_csv(
# project + "/relaxed_molsig_" + met + "_1.csv", index_col=0
# )
# molsigs_product_dict = molsigs_product.to_dict(orient="index")
smiles = novel_mets[met]
mol = Chem.MolFromSmiles(smiles)
mol = Chem.RemoveHs(mol)
molsigs_product_dict = count_substructures(1,mol)
for m in moiety_index:
if m in molsigs_product_dict.keys():
C[m][met] = molsigs_product_dict[m]
else:
C[m][met] = 0
###### variables ######
v_rule = pulp.LpVariable.dicts(
"v_rule", rules_index, lowBound=-M, upBound=M, cat="Integer"
)
v_rule_obj = pulp.LpVariable.dicts(
"v_rule_obj", rules_index, lowBound=0, upBound=M, cat="Continuous"
)
v_EX = pulp.LpVariable.dicts(
"v_EX", exchange_index, lowBound=-M, upBound=M, cat="Continuous"
)
y_rule = pulp.LpVariable.dicts(
"y", rules_index, lowBound=0, upBound=1, cat="Binary"
)
# create MILP problem
lp_prob = pulp.LpProblem("novoStoic", pulp.LpMinimize)
####### objective function ####
lp_prob += pulp.lpSum([v_rule_obj[j] for j in rules_index])
####### constraints ####
# constraint 1: moiety change balance
for m in moiety_index:
lp_prob += (
pulp.lpSum([T[m][r] * v_rule[r] for r in rules_index if T[m][r] !=0])
== pulp.lpSum([C[m][i] * v_EX[i] for i in exchange_index if C[m][i] != 0]),
"moiety_balance_" + str(moiety_index.index(m)),
)
# constraint 2: constraint for exchange reactions
for i, stoic in exchange_mets.items():
lp_prob += v_EX[i] == stoic, "exchange" + i
# constraint 3: control the number of rules
direction_df = pd.read_csv(
os.path.join(data_dir, "direction.csv"), index_col=0
)
direction_df.index = direction_df['reaction']
# direction: 0-reversible, 1-backward, 2-forward
direction = direction_df['direction'].to_dict()
if use_direction:
soln_file = os.path.join(project, "solution_use_direction.txt")
for j in rules_index:
if direction[j] == 0:
lp_prob += v_rule[j] >= y_rule[j] * -M, "cons1_%s" % j
lp_prob += v_rule[j] <= y_rule[j] * M, "cons2_%s" % j
if direction[j] == 1:
lp_prob += v_rule[j] >= y_rule[j] * -M, "cons1_%s" % j
lp_prob += v_rule[j] <= 0, "cons2_%s" % j
if direction[j] == 2:
lp_prob += v_rule[j] >= 0, "cons1_%s" % j
lp_prob += v_rule[j] <= y_rule[j] * M, "cons2_%s" % j
else:
soln_file = os.path.join(project, "solution_no_direction.txt")
for j in rules_index:
lp_prob += v_rule[j] >= y_rule[j] * -M, "cons1_%s" % j
lp_prob += v_rule[j] <= y_rule[j] * M, "cons2_%s" % j
for j in rules_index:
lp_prob += v_rule_obj[j] >= v_rule[j]
lp_prob += v_rule_obj[j] >= -v_rule[j]
# constraint 5: customized constraints
# the number of steps of the pathway
lp_prob += pulp.lpSum([v_rule_obj[j] for j in rules_index]) == 2
### solve
integer_cuts(lp_prob,pulp_solver,iterations,rules_index,y_rule,v_rule,soln_file,direction)
def integer_cuts(lp_prob,pulp_solver,iterations,rules_index,y_rule,v_rule,soln_file,direction):
"""add integer cut constraints to a mixed-integer linear programming problem
(MILP). The aim of such constraints is to find alternative solutions by
adding constraints to exclude the already explored solutions.
Reference: Optimization Methods in Metabolic Networks By Costas D. Maranas,
Ali R. Zomorrodi, Chapter 4.2.2 Finding alternative optimal integer
solutions
Returns
-------
type
Description of returned object.
"""
for sol_num in range(1, iterations + 1):
integer_cut_rules = []
# optinal output: lp file for debug
lp_prob.writeLP('./test.lp')
# if pulp_solver = "SCIP":
# status, values = pulp_solver.solve(lp_prob)
lp_prob.solve(pulp_solver)
# pulp_solver.solve(lp_prob)
print("Status:", pulp.LpStatus[lp_prob.status])
if pulp.LpStatus[lp_prob.status] != 'Optimal':
break
print('-----------rules--------------')
with open(soln_file,'a') as f:
f.write('iteration,' + str(sol_num))
f.write('\n')
for r in rules_index:
if (v_rule[r].varValue >= 0.1 or v_rule[r].varValue <=-0.1):
dG_info = ''
if (v_rule[r].varValue > 0 and direction[r] == 1) or (v_rule[r].varValue < 0 and direction[r] == 2):
# print("##### Found ####: " + str(r))
# with open(soln_file,'a') as f:
# f.write('##### Found ####: ' + str(r))
# f.write('\n')
dG_info = ' * Thermodynamically infeasible'
print("##### Found ####: " + str(r) + dG_info)
integer_cut_rules.append(r)
print(r,v_rule[r].varValue)
with open(soln_file,'a') as f:
f.write(r + ',' + str(v_rule[r].varValue) + dG_info)
f.write('\n')
length = len(integer_cut_rules) - 1
lp_prob += (
pulp.lpSum([y_rule[r] for r in integer_cut_rules]) <= length,
"integer_cut_" + str(sol_num),
)
def test_bdo():
exchange_mets = {
'C00091': -1, # Succinyl-CoA
'C00004': -4, # NADH
'C00003': 4, # NAD+
'C00010': 1, # coa
'C00001':1, # h2O
'14bdo': 1,
}
novel_mets = {
'14bdo': 'OCCCCO'
}
iterations = 50
project = './novoStoic_result'
# path_to_cplex = '/Users/linuswang/Applications/IBM/ILOG/CPLEX_Studio1261/cplex/bin/x86-64_osx/cplex'
# pulp_solver = pulp.CPLEX_CMD(path=path_to_cplex,keepFiles=0, mip=1, msg=1)
pulp_solver = pulp.CPLEX_CMD(path=None,keepFiles=0, mip=1, msg=1)
# pulp_solver = pulp.solvers.GUROBI_CMD()
# pulp_solver = pulp.solvers.GLPK_CMD()
use_direction=True
novoStoic_minFlux_relaxedRule(exchange_mets, novel_mets,project,iterations,pulp_solver,use_direction)
use_direction=False
novoStoic_minFlux_relaxedRule(exchange_mets, novel_mets,project,iterations,pulp_solver,use_direction)
def test_isovalarate():
exchange_mets = {
'C00141': -1, # 2-keto isovalarate
'C00004': -1, # NADH
'C00003': 1, # NAD+
"C14710": 1, # isobutanol C4H10O
'C00011': 1, # co2
}
novel_mets = {}
iterations = 50
project = './novoStoic_isovalarate'
# path_to_cplex = '/Users/linuswang/Applications/IBM/ILOG/CPLEX_Studio1261/cplex/bin/x86-64_osx/cplex'
# pulp_solver = pulp.CPLEX_CMD(path=path_to_cplex,keepFiles=0, mip=1, msg=1)
pulp_solver = pulp.CPLEX_CMD(path=None,keepFiles=0, mip=1, msg=1)
# pulp_solver = pulp.solvers.GUROBI_CMD()
# pulp_solver = pulp.GLPK_CMD()
# use_direction=True
# novoStoic_minFlux_relaxedRule(exchange_mets, novel_mets,project,iterations,pulp_solver,use_direction)
use_direction=False
novoStoic_minFlux_relaxedRule(exchange_mets, novel_mets,project,iterations,pulp_solver,use_direction)
if __name__ == '__main__':
test_isovalarate()
|