-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLegacyCode.py
More file actions
237 lines (218 loc) · 7.95 KB
/
LegacyCode.py
File metadata and controls
237 lines (218 loc) · 7.95 KB
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
Fracs = True
from Utilities import *
from ModelProcessing import findFeasible
from math import gcd
def findUnidirectionalOld(N, Irrev, option = 'null', verbose = False):
# This function finds all unidirectional (effectively irreversible) reactions.
# NOTE: It assumes that all the reactions in the network can have nonzero flux;
# otherwise may incorrectly classify blocked reactions as only negative.
# The option can be 'null' for nullspace (default) or 'row' for rowspace.
m, n = getSize(N)
onlyPos, onlyNeg = [], []
for i in range(n):
if verbose and i % 100 == 0:
print(('Processed ' + str(i) + ' reactions so far'))
if i not in Irrev:
# test for effective irreversibility of the reaction
(val1, vec1) = findFeasible(N, i, Irrev, True, 'sub+' + str(i) + 'sets.lp', option = option)
if (type(val1) == type([]) and len(val1) == 0): # infeasible
onlyNeg.append(i)
else:
(val0, vec0) = findFeasible(N, i, Irrev, False, 'sub-' + str(i) + 'sets.lp', option = option)
if (type(val0) == type([]) and len(val0) == 0): # infeasible
onlyPos.append(i)
return (onlyPos, onlyNeg)
def extremeElement(vector, Max = False):
# This function finds the smallest element of a vector.
# If Max is True, finds the maximum element instead
if Fracs:
if Max:
return max(vector)
else:
return min(vector)
else:
curmin = vector[0]
for i in range(1, len(vector)):
if (Max and compare(vector[i], curmin) == 1) or (not Max and compare(vector[i], curmin) == -1):
curmin = vector[i]
return curmin
def filterList(List):
# This function removes the zero entries from a list of fractions
zero = convertToFraction(0)
myNone = lambda x:compare(x,zero)
return list(filter(myNone, List))
def representable(frac):
# This function decides whether a fraction is representable as an exact decimal
den = getden(frac)
while den % 2 == 0:
den /= 2
while den % 5 == 0:
den /= 5
if den == 1:
return True
else:
return False
def getnum(frac):
# Returns the numerator of a fraction
if Fracs:
return frac.numerator
else:
return frac[0]
def getden(frac):
# Returns the denominator of a fraction
if Fracs:
return frac.denominator
else:
return frac[1]
def makeFloat(frac):
# This function returns the floating point number corresponding to a fraction
if Fracs:
return float(frac)
else:
return float(frac[0])/frac[1]
def negate(frac):
# This function returns the negative of a fractional number
if Fracs:
return -frac
else:
return [-frac[0], frac[1]]
def absValue(frac):
# This function returns the absolute value of a fractional number
if Fracs:
return abs(frac)
else:
return [abs(frac[0]), abs(frac[1])]
def compare(frac0, frac1):
# This function defines the comparison of two fractional numbers, frac0 & frac1.
# Returns 1 if frac0 is bigger, -1 if frac1 is bigger, and 0 if they are equal.
if Fracs:
#return cmp(frac0, frac1) #replaced with ((frac0 > frac1) - (frac0 < frac1))
#taken from Python 3 website: https://docs.python.org/3.0/whatsnew/3.0.html
return ((frac0 > frac1) - (frac0 < frac1))
else:
diff = subtract(frac0, frac1)
if diff[0]*diff[1] > 0:
return 1
elif diff[0]*diff[1] < 0:
return -1
else: # the numerator is 0
return 0
def multiply(frac0, frac1):
# This function defines the multiplication of two fractional numbers.
if Fracs:
return frac0*frac1
else:
[num0, den0] = frac0
[num1, den1] = frac1
num = num0*num1
den = den0*den1
gcd = GCD(num, den)
return [num/gcd, den/gcd]
def add(frac0, frac1):
# This function defines the addition of two fractional numbers.
if Fracs:
return frac0+frac1
else:
[num0, den0] = frac0
[num1, den1] = frac1
num = num0*den1 + num1*den0
den = den0*den1
gcd = GCD(num, den)
return [num/gcd, den/gcd]
def divide(frac0, frac1):
# This function defines the division of two fractional numbers.
if Fracs:
return frac0/frac1
else:
[num0, den0] = frac0
[num1, den1] = frac1
num = num0*den1
den = num1*den0
gcd = GCD(num, den)
return [num/gcd, den/gcd]
def subtract(frac0, frac1):
# This function defines the subtraction of two fractional numbers.
if Fracs:
return frac0-frac1
else:
[num0, den0] = frac0
[num1, den1] = frac1
num = num0*den1 - num1*den0
den = den0*den1
gcd = GCD(num, den)
return [num/gcd, den/gcd]
def GCD(a,b):
# This function computes the greatest common divisor of two integers
if Fracs:
return gcd(a,b)
else:
while b:
a,b = b, a%b
return a
def dotProduct(List1, List2):
# This function computes a dot product between two lists of numbers
if len(List1) != len(List2):
print('Error: incompatible vectors!')
else:
return sum([List1[x] * List2[x] for x in range(len(List1))])
def correlationRank(values1, values2, option = 'Spearman'):
# This function computes the correlation of the ranks between two variables.
# The possible options are Spearman and Kendall; both give a value in [-1,1].
# NOTE: we assume that there are no ties for the current implementation!
m, n = len(values1), len(values2)
if m != n:
print('Error: the lists do not have the same dimension!')
return
if option == 'Spearman':
ranks1, ranks2 = rankList(values1), rankList(values2)
deltas = [ranks1[x] - ranks2[x] for x in range(n)]
sumsqrs = float(sum([x*x for x in deltas]))
return 1 - 6*sumsqrs/(n*(n*n-1))
elif option == 'Kendall':
Sum = 0
for i in range(n):
for j in range(i+1, n):
Sum += (compare(values1[i], values1[j]) * compare(values2[i], values2[j]))
return 2*float(Sum)/(n*(n-1))
else:
print('Error: unrecognized option!')
return
def rankList(values, reverse = False):
# This function returns the ranks of the values; the smallest gets rank 1.
# Ties are broken in a stable way; if reverse is True, ranks are reversed.
myList = [(values[x],x) for x in range(len(values))]
myList = sorted(myList, reverse = reverse)
return [x[1] for x in myList]
def classifyExchange(FullNetwork, externalMetabs, Irrev):
# This function finds all the exchange reactions in a given full network
# (with the external metabolites included) and classifies them into 3 types:
# input only, output only, and mixed, and additionally by irreversibility.
m = len(FullNetwork)
n = len(FullNetwork[0])
Exchange = [y for y in range(n) if [_f for _f in [FullNetwork[x][y] for x in externalMetabs] if _f]]
InputIrr, InputRev, OutputIrr, OutputRev, MixedIrr, MixedRev = [], [], [], [], [], []
for react in Exchange:
In, Out = False, False
for x in externalMetabs:
if FullNetwork[x][react] < 0:
In = True
elif FullNetwork[x][react] > 0:
Out = True
if In and Out:
if react in Irrev:
MixedIrr.append(react)
else:
MixedRev.append(react)
elif In and (not Out):
if react in Irrev:
InputIrr.append(react)
else:
InputRev.append(react)
elif (not In) and Out:
if react in Irrev:
OutputIrr.append(react)
else:
OutputRev.append(react)
else:
print('This should never happen!')
return (InputIrr, InputRev, OutputIrr, OutputRev, MixedIrr, MixedRev)