-
Notifications
You must be signed in to change notification settings - Fork 0
/
MCSTree.py
212 lines (198 loc) · 6.17 KB
/
MCSTree.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
'''
Created on Dec 14, 2014
@author: ebadr
'''
'''
Created on Oct 10, 2014
@author: ebadr
'''
'''
Created on Oct 1, 2014
@author: ebadr
'''
import sys
#import math
import networkx as nx
#import matplotlib
#import numpy as np
import operator
#from operator import itemgetter
#try:
# import matplotlib.pyplot as plt
#except:
# raise
sys.setrecursionlimit(10000)
##########################################################
def Readseqs(GraphW):
fh = None
#ESE1
try:
fh = open(GraphW+'attr.txt', encoding="utf8")
seqs=[]
for lino, line in enumerate(fh, start=1):
line = line.rstrip()
if not line:
continue
else:
L=[]
L = line.split(",")
A = [x for x in L if x != '']
L2=[int(i) for i in A]
seqs.append(L2)
#return seqs
finally:
if fh is not None:
fh.close()
#ESE2
try:
fh = open(GraphW1+'attr.txt', encoding="utf8")
#seqs=[]
for lino, line in enumerate(fh, start=1):
line = line.rstrip()
if not line:
continue
else:
L=[]
L = line.split(",")
A = [x for x in L if x != '']
L2=[int(i) for i in A]
seqs.append(L2)
return seqs
finally:
if fh is not None:
fh.close()
#######################################################
def Find_Graphs2(atr,prev_mcs,MCSt):
mcs=[]
exons=MCSt[atr]
for e in exons:
if (e in prev_mcs):
mcs.append(e)
return mcs
#$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
def check(name,mcs,G):
curr = name.split('_') # current path
for node in G:
prev=node.split('_')
if set(curr)<set(prev): # these nodes are subset of a previously discovered nodes
if set(mcs) == set(G.node[node]['Exons']):
return True
return False
#$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$4
def add_one_tree(MH,G,i,attri,MCSt,name,Theta):
prev_name=name
for k in range(i+1,len(attri)):
prev_mcs = list(G.node[prev_name]['Exons'])
mcs = Find_Graphs2(attri[k],prev_mcs,MCSt)
if (len(mcs)>= Theta):
name=prev_name+'_'+str(attri[k])
f = check(name,mcs,G)
if(f):
return
#print(name)
#w= MH[attri[k]]
#print(w.nodes())
G.add_node(name,graph = attri[k],Exons=mcs)
G.add_edge(prev_name,name)
add_one_tree(MH,G,k,attri,MCSt,name,Theta)
return
#$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$4
def Find_Graphs(atr,MCSt):
#mcs=[]
#for Graph in MCSt:
#if atr in MCSt[Graph]:
#mcs.append(Graph)
mcs=MCSt[atr]
return mcs
#$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$4
def Build_Prefix_Tree(MH,attri,MCSt,Theta):
#build the directed graph
G=nx.DiGraph()
G.add_node('#',graph=-1, Exons=[])
for i in range(len(attri)):
mcs= Find_Graphs(attri[i],MCSt) # find attributes associated with graphs
if (len(mcs)> Theta): # number of shared exons
#print('first->'+str(attri[i]))
name=str(attri[i])
G.add_node(name,graph = attri[i],Exons=mcs)
G.add_edge('#',name)
add_one_tree(MH,G,i,attri,MCSt,name,Theta)
return G
#$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$4
def Generate_Prefix(MH,MCSt,Theta):
# Get all attributes
#attri=[]
#for Graph in MCSt:
#attri=list(set(MCSt[Graph])| set(attri))
attri=MCSt.keys()
# sort the unique attributes
attri = sorted(attri)
#start building the prefix tree
G = Build_Prefix_Tree(MH,attri,MCSt,Theta)
# nx.write_dot(G,GraphW+'test.dot')
#nx.draw_spring(G,node_size=1000,node_color='r',font_size=10)
#plt.show()
#nx.write_gml(G,GraphW+'graph.gml')
return G
#$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$4
def checked(MsetGraphs,MsetExons,L,M):
for i in MsetGraphs:
if set(L) < set(MsetGraphs[i]):
if set(M)== set(MsetExons[i]):
return False
return True
#$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$4
def Generate_MCSs (G):
MsetGraphs={}
MsetExons={}
k=1
length=nx.single_source_shortest_path_length(G,'#')
sorted_x = sorted(length.items(), key=operator.itemgetter(1))
for i in range(len(sorted_x)-1,0,-1):#elem in length:
elem,tall=sorted_x[i]
if tall >= beta:
L = elem.split('_')
M = list(G.node[elem]['Exons'])
# f = checked(MsetGraphs,MsetExons,L,M)
f=True
if (f):
MsetGraphs[k] = L
MsetExons[k] = M
k+=1
#for i in MsetGraphs:
#print('number of MCSs:', length[elem],'|', 'number of Exons:',len(M) ,'|', 'MCSs are:', L,'|', 'Exons are:',M)
#print(i,'|',len(MsetGraphs[i]),'|',len(MsetExons[i]) ,'|', list(MsetGraphs[i]))#,'|',list(MsetExons[i]))
return MsetGraphs,MsetExons
def Write_output(F,MsetGraphs, MsetExons):
fh = open(F+'MCSsets.txt', "w", encoding="utf8")
# write MCSset ID | subgraph IDs | Exons IDs
for i in range(0,len(MsetGraphs)):
fh.write(str(i+1))
fh.write('|')
L = list(MsetGraphs[i+1])
M = list(MsetExons[i+1])
for k in L:
fh.write(str(k))
fh.write(',')
fh.write('|')
for k in M:
fh.write(str(k))
fh.write(',')
fh.write("\n")
return
#######################################################
#GraphW="/home/ebadr/Writing/py3eg/WCCs/1000-100/"
GraphW1="/home/ebadr/Writing/py3eg/WCCs/1000-100/"
global beta
beta = 2 # of graphs in one MCS set
#global theta
#theta = 100 # of shared exons
def MainG(MH,MCSt,Theta,GraphW):
#Calling Functions
print("Calling Functions")
#Generate MCS tree
G = Generate_Prefix(MH,MCSt,Theta)
# Get the depths in order to get only the paths with length more than theta
MsetGraphs, MsetExons = Generate_MCSs(G)
Write_output(GraphW, MsetGraphs, MsetExons)
return MsetGraphs, MsetExons