-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtil_Methods.py
230 lines (214 loc) · 5.74 KB
/
Util_Methods.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
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
def Generate_Random_Networks(inputfile, outputfile):
from math import floor, log
import random
R = []
with open(inputfile, 'r') as f:
count = 0
for line in f:
if count == 0:
count += 1
continue
l = line.strip().split('\t')
R.append([l[0].strip(), l[1].strip(), l[2].strip()])
R_new = []
E = []
for j in range(len(R)):
R_new.append([R[j][0], R[j][1], R[j][2]])
E.append(R[j][0] + '**>' + R[j][1])
N = int(floor(log(1e7)*len(R_new) / 2))
N = N*10
for j in range(N):
edge1 = random.randint(0, len(R_new) - 1)
edge2 = edge1
while edge2 == edge1:
if len(R) == 1:
break
edge2 = random.randint(0, len(R_new) - 1)
target1 = R_new[edge1][1]
target2 = R_new[edge2][1]
old_edge_1 = R_new[edge1][0] + '**>' + R_new[edge1][1]
old_edge_2 = R_new[edge2][0] + '**>' + R_new[edge2][1]
new_edge_1 = R_new[edge1][0] + '**>' + target2
new_edge_2 = R_new[edge2][0] + '**>' + target1
if new_edge_1 not in E and new_edge_2 not in E:
R_new[edge1][1] = target2
R_new[edge2][1] = target1
E.remove(old_edge_1)
E.remove(old_edge_2)
E.append(new_edge_1)
E.append(new_edge_2)
if len(R) != len(E):
print('Was I supposed to tackle her?')
with open(outputfile, 'w') as f:
f.write('Source\tTarget\tType\n')
for j in range(len(R_new)):
f.write(R_new[j][0] + '\t' + R_new[j][1] + '\t' + R_new[j][2] + '\n')
def Process_RACIPE_Output(filename):
IDs = {}
Names = {}
flag = 0
numlines = 0
with open(filename + '.cfg', 'r') as f:
for line in f:
if flag == 1:
l = line.strip().split('\t')
geneid = int(l[0].strip())
if geneid > numlines:
break
gene = l[1].strip()
IDs[geneid - 1] = gene
Names[gene] = geneid - 1
if 'NumberOfGenes' in line:
flag = 1
l = line.strip().split('\t')
numlines = int(l[1].strip())
with open(filename + '.ids', 'w') as f:
for i in range(len(IDs)):
f.write(IDs[i] + '\t' + str(i) + '\n')
count = 0
T = []
for i in range(len(IDs)):
T.append([])
for j in range(len(IDs)):
T[i].append(0)
Links = []
with open(filename + '.topo', 'r') as f:
for line in f:
if count == 0:
count += 1
continue
l = line.strip().split('\t')
source = l[0].strip()
target = l[1].strip()
T[Names[source]][Names[target]] = int(l[2].strip())
L = []
for i in range(len(IDs)):
L.append([])
for j in range(len(IDs)):
L[i].append(0)
linkindex = 2*len(IDs)
with open(filename + '.prs', 'r') as f:
for line in f:
if 'Trd_of_' not in line:
continue
l = line.strip().split('\t')
linkname = l[0].strip()
index1 = linkname.index('_of_')
index2 = linkname.index('To')
source = linkname[index1 + 4:index2]
target = linkname[index2 + 2:]
L[Names[source]][Names[target]] = linkindex
linkindex += 3
with open(filename + '_T_matrix.log', 'w') as f:
for i in range(len(T)):
for j in range(len(T[i])):
if j == len(T[i]) - 1:
f.write(str(T[i][j]))
else:
f.write(str(T[i][j]) + '\t')
f.write('\n')
with open(filename + '_L_matrix.log', 'w') as f:
for i in range(len(L)):
for j in range(len(L[i])):
if j == len(L[i]) - 1:
f.write(str(L[i][j]))
else:
f.write(str(L[i][j]) + '\t')
f.write('\n')
def Write_Steady_States(RACIPE_Folder, networkname, outputfolder, outputname):
import os
nodes = set()
linecount = 0
with open(RACIPE_Folder + '/' + networkname + '.topo', 'r') as f:
for line in f:
if linecount == 0:
linecount += 1
continue
l = line.strip().split('\t')
nodes.add(l[0].strip())
nodes.add(l[1].strip())
numnodes = len(nodes)
S = []
numstates = []
setid = []
count = 0
for filename in os.listdir(RACIPE_Folder):
if 'solution' not in filename:
continue
with open(RACIPE_Folder + '/' + filename, 'r') as f:
for line in f:
if 'nan' in line.strip():
continue
l = line.strip().split('\t')
if (len(l) - 2) % numnodes != 0:
print('A pharmacist.')
return(1)
numstates.append((len(l) - 2) / numnodes)
setid.append(int(l[0].strip()))
S.append([])
for i in range(2, len(l)):
S[count].append(float(l[i]))
if len(S[count]) == numnodes and i != len(l) - 1:
count += 1
S.append([])
setid.append(int(l[0].strip()))
count += 1
if len(setid) != len(S):
print('Goliath National Bank')
with open(outputfolder + '/' + outputname, 'w') as f:
for i in range(len(S)):
f.write(str(setid[i]) + ' ')
for j in range(len(S[i])):
f.write(str(S[i][j]) + ' ')
f.write('\n')
return(0)
def Steadystates_PCA(filename, skipcount):
import numpy as np
from sklearn.decomposition import PCA
S = []
count = 0
with open(filename, 'r') as f:
for line in f:
l = line.strip().split(' ')
S.append([])
for i in range(skipcount, len(l)):
S[count].append(float(l[i].strip()))
count += 1
states = np.array(S)
pca = PCA(n_components = min(len(S), len(S[0])))
pca.fit(states)
return(pca.explained_variance_ratio_)
def Steadystates_PCA_Norm(filename, skipcount):
import numpy as np
from sklearn.decomposition import PCA
from scipy.stats import zscore
S = []
count = 0
with open(filename, 'r') as f:
for line in f:
l = line.strip().split(' ')
S.append([])
for i in range(skipcount, len(l)):
S[count].append(float(l[i].strip()))
count += 1
states = np.array(S)
states = zscore(S)
pca = PCA(n_components = min(len(S), len(S[0])))
pca.fit(states)
return(pca.explained_variance_ratio_)
def Write_IDS_File(topofile, outputfile):
IDs = []
with open(topofile, 'r') as f:
count = 0
for line in f:
if count == 0:
count += 1
continue
l = line.strip().split('\t')
if l[0].strip() not in IDs:
IDs.append(l[0].strip())
if l[1].strip() not in IDs:
IDs.append(l[1].strip())
with open(outputfile, 'w') as f:
for i in range(len(IDs)):
f.write(IDs[i] + '\t' + str(i) + '\n')