/
CobraExtract.py
186 lines (149 loc) · 4.73 KB
/
CobraExtract.py
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
import numpy as np
#import pandas as pd
import copy
import cobra
import cobra.test
import cobra.util
def Integer(s,negReaction=[]):
""" Make a stoichiometric matrix with integer elements
"""
N = s["N"] # Stoichiometric matrix
reaction = s["reaction"]
species = s["species"]
NT = N.T # Transpose
for j,row in enumerate(NT):
# I = np.nonzero(row)
# Coeff = row[I]
I_nonInt = np.nonzero(row-row.astype(int))
if len(I_nonInt[0])>0:
if len(I_nonInt[0])>1:
print("Extract.Integer only handles one non-integer per reaction")
i = I_nonInt[0][0]
# print(i,reaction[j],species[i],row[i])
#print(row[np.nonzero(row)])
factor = 1/abs(row[i])
row *= factor
print("Multiplying reaction",reaction[j], "(",j,")",
"by",factor,"to avoid non-integer species",
species[i],"(",i,")")
#print(row[np.nonzero(row)])
if reaction[j] in negReaction:
print("Multiplying reaction",reaction[j], "(",j,") by -1")
row *= -1
N = NT.T
s['N'] = N.astype(int)
return s
def remove(s,Rem=[]):
for rem in Rem:
s = s.replace(rem,'')
return s
def extract(cobraname='textbook',name=None,Remove=[],Ignore=[],negReaction=[],quiet=False):
print('Extracting stoichiometric matrix from:',cobraname)
## Get the Cobra model
model = cobra.test.create_test_model(cobraname)
## Set the bgt name
if name is None:
name = model.id+'_abg'
print('Cobra Model name:', model.id, 'BondGraphTools name:', name)
## Get the species
species = []
for meta in model.metabolites:
species.append(meta.id)
## Get the reactions
reaction = []
for reac in model.reactions:
reaction.append(reac.id)
nX = len(species)
nV = len(reaction)
if not quiet:
print("nX:", nX)
print("nV:", nV)
bad = ['(',')','[',']','-']
## Remove () and [] and - from the names
for i,spec in enumerate(species):
species[i] = remove(spec.upper(),bad+Remove)
for i,reac in enumerate(reaction):
reaction[i] = remove(reac.upper(),bad+Remove)
## Get the stoichiometric matrix
N = cobra.util.create_stoichiometric_matrix(model)
## Look for reactions with no RHS and replace by chemostats
Nf = -((N<0)*N)
Nr = (N>0)*N
NfT = Nf.T
NrT = Nr.T
J = []
chemostats = []
reac = []
#zap_reac = ['ATPM','ADK1']
zap_reac = []
for j in range(nV-1,-1,-1):
if np.max(NrT[j]) == 0:
i = np.flatnonzero(NfT[j])[0]
if not quiet:
print('Deleting reaction:',reaction[j],'and adding chemostat:', species[i])
chemostats.append(species[i])
N = np.delete(N,j,1)
elif reaction[j] in zap_reac:
print('Deleting reaction:',reaction[j])
N = np.delete(N,j,1)
else:
reac.append(reaction[j])
## Reactions now in wrong order: reverse them
reac = list(reversed(reac))
nX = len(species)
nV = len(reac)
if not quiet:
print("nX:", nX)
print("nV:", nV)
if not quiet:
print(len(chemostats),'chemostats')
#print(len(chemostats),'chemostats:',chemostats)
s = {}
s['N'] = N
s['species'] = species
s['reaction'] = reac
s['chemostats'] = chemostats
s['name'] = name
return Integer(s,negReaction=negReaction)
def getSpecies(s,reaction):
""" Extract the species associated with a list of reactions
"""
N = s["N"]
NT = N.T
Species = s["species"]
Reaction = s["reaction"]
species = []
for reac in reaction:
if not reac in Reaction:
print("Warning: reaction", reac, "not available")
Nj = NT[Reaction.index(reac)]
I = np.nonzero(Nj)[0]
for i in I:
species.append(Species[i])
species = list(set(species)) # Make unique
species.sort() # Sort
return species
def choose(s,reaction=[]):
""" Choose subset reactions.
"""
NN = s["N"]
Species = s["species"]
Reaction = s["reaction"]
species = getSpecies(s,reaction)
n_X = len(species)
n_V = len(reaction)
N = np.zeros((n_X,n_V))
for i,spec in enumerate(species):
for j,reac in enumerate(reaction):
N[i,j] = NN[Species.index(spec),Reaction.index(reac)]
N = N.astype(int)
## Forward and reverse
Nf = -((N<0)*N)
Nr = (N>0)*N
sub = {}
sub["N"] = N
sub["Nf"] = Nf
sub["Nr"] = Nr
sub["species"] = species
sub["reaction"] = reaction
return sub