-
Notifications
You must be signed in to change notification settings - Fork 1
/
graph.py
306 lines (255 loc) · 7.01 KB
/
graph.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
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
import numpy as np
import os
import sys
import matplotlib.pyplot as plt
import networkx as nx
from datetime import datetime
import itertools
from numba import jit
#start
start=datetime.now()
print (datetime.now()-start)
#import .star file
class read_relion(object):
def __init__(self, file):
self.file = file
def getRdata(self):
Rvar = [] #read the variables
Rdata = [] # read the data
for star_line in open(self.file).readlines():
if star_line.find("_rln") != -1:
var = star_line.split()
Rvar.append(var[0])
# Rvar_len = Rvar_len+1
elif star_line.find("data_") != -1 or star_line.find("loop_") != -1 or len(star_line.strip()) == 0:
continue
else:
Rdata.append(star_line.split())
return Rvar, Rdata
def column(matrix, i):
return [row[i] for row in matrix]
# combination of 0 and 1
def combination(n):
loop=[]
for i in range(1<<n):
s=bin(i)[2:]
s='0'*(n-len(s))+s
loop.append(list(s))
return loop
def permutation(iterable, r=None):
pool = tuple(iterable)
n = len(pool)
r = n if r is None else r
if r > n:
return 'r is more than n'
indices = list(range(n))
cycles = list(range(n, n-r, -1))
yield list(pool[i] for i in indices[:r])
while True:
for i in reversed(range(r)):
cycles[i] -= 1
if cycles[i] == 0:
indices[i:] = indices[i+1:] + indices[i:i+1]
cycles[i] = n - i
else:
j = cycles[i]
indices[i], indices[-j] = indices[-j], indices[i]
yield list(pool[i] for i in indices[:r])
break
else:
return
def factorial(n):
a=1
i=0
while i<=n:
i+=1
a*=i
return a
def get_index(lst,item):
return [i for i in range(len(lst)) if lst[i] == item]
def find_array(arr,lst):
for i, j in enumerate(arr):
if list(j)==lst:
return True
return False
def all_loop(node,root):
loop=[]
for value in itertools.permutations(node):
if value[0]<value[-1]:
value=list(value)
value.append(root)
loop.append(value)
return loop
relion_data = read_relion(sys.argv[1])
#average_data=read_relion(sys.argv[2])
# create empty class matrix
classgroup=[]
for i in range(50):
classgroup.append(str(i+1))
#average=average_data.getRdata()[1]
#for i in average:
# classgroup.append(i[-1])
matrix = np.zeros([len(classgroup),len(classgroup)],dtype=int)
print(classgroup)
# read particle data
data=relion_data.getRdata()[1]
M= relion_data.getRdata()[0].index( '_rlnImageName' )
H= relion_data.getRdata()[0].index( '_rlnHelicalTubeID' )
C= relion_data.getRdata()[0].index( '_rlnClassNumber' )
print('finish reading')
# extract helical parameters
helicaldic={}
helicalnum=[]
count=-1
for particle in data:
ID = particle[M][7:]+'-'+str(particle[H])
if ID in helicalnum:
n=str(count)
lst=helicaldic[n]
lst.append(particle[C])
helicaldic[n]=lst
else:
helicalnum.append(ID)
n=str(helicalnum.index(ID))
count+=1
helicaldic[n]=[particle[C]]
print('finish converting')
# produce weight matrix
for n in range(len(helicalnum)):
helix=helicaldic[str(n)]
for i in range(len(helix)-1):
a=classgroup.index(helix[i])
b=classgroup.index(helix[i+1])
matrix[a][b]=matrix[a][b]+1
M=matrix
#delete empty column
delgroup=[]
for i in range(len(classgroup)):
n=1
for j in matrix[i]:
if j!=0:
print('out')
break
else:
if n<len(classgroup):
n=n+1
continue
else:
print('del')
delgroup.append(i)
break
delgroup.sort()
print(delgroup)
n=0
for i in delgroup:
M = np.delete(M,i-n,0)
M = np.delete(M,i-n,1)
n=n+1
classgroup.remove(str(i+1))
print(M)
print(classgroup)
#n=0
#for i in range(1,4):
# M = np.delete(M,i-n,0)
# M = np.delete(M,i-n,1)
# n=n+1
#classgroup.remove('2')
#classgroup.remove('7')
#classgroup.remove('11')
NS=M
# produce symmetric matrix
S=np.tril(NS)+np.tril(NS.T, -1)
print(S)
# select M or S
Mclean=S
#produce weight matrix
MW = np.zeros([len(classgroup),len(classgroup)],dtype=float)
for i in range(len(classgroup)):
sum = np.sum(Mclean[i])
for j in range(len(Mclean[i])):
MW[i][j]=Mclean[i][j]/sum
print(MW)
# produce adjacency matrix
MA = np.zeros([len(classgroup),len(classgroup)],dtype=int)
for i in range(len(classgroup)):
lst=Mclean[i]
l=len(lst)
keep=np.argsort(lst)
lst=np.zeros((l),dtype=int)
for j in range(len(classgroup)):
lst[keep[-(j+1)]]=1
MA[i]=lst
# delete the self cycle
for i in range(len(classgroup)):
MA[i][i]=0
print(MA)
print (datetime.now()-start)
# find all the cycles
comb=combination(len(classgroup))
print('combination')
pweight=[]
all_cycles=[]
all_weights=[]
names=[]
for i in range(len(comb)):
lst=comb[i]
node=get_index(lst, str(1))
n=lst.count(str(1))
if n<3:
continue
root=min(node) # find start node
all_node=node
print(all_node,'remove nodes')
node.remove(root)
print(all_node)
print (datetime.now()-start)
weight=[]
loop=all_loop(node,root)
#make name for each combination of nodes
all_node.append(root)
name=''
for i in range(len(all_node)):
name=name+','+str(classgroup[all_node[i]])
names.append(name)
for i in range(len(loop)):
wi=0
tem=loop[i]
print(tem)
for j in range(len(tem)):
j_0=tem[1]
j_n=tem[j]
if j <=len(tem)-2:
j_n1=tem[j+1]
wi+=MW[j_n][j_n1]+MW[j_n1][j_n]
else:
wi+=MW[j_n][j_0]+MW[j_0][j_n]
relative_wi=wi/n
weight.append(relative_wi)
print('all the loops are detected for {} nodes'.format(all_node))
all_cycles.append(loop)
all_weights.append(weight)
print (datetime.now()-start)
ks=[]
ls=[]
for i in all_weights:
k=max(i)
ks.append(k)
l=i.index(max(i))
ls.append(l)
h=max(ks)
hi=ks.index(h)
print(h,names[hi])
#Statements
print (datetime.now()-start)
# produce graph from adjacency marix
G= nx.Graph()
G.add_nodes_from(classgroup)
for i in range(len(classgroup)):
for j in range(len(classgroup)):
if (MA[i][j]!=0):
G.add_edge(classgroup[i],classgroup[j], weight=MW[i][j])
else:
continue
# draw image
nx.draw_networkx(G, with_labels=True, arrows=True, font_weight='bold')
plt.show()