-
Notifications
You must be signed in to change notification settings - Fork 0
/
degree_combined.py
194 lines (166 loc) · 5.19 KB
/
degree_combined.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
import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
from decimal import Decimal
import math
import os
import pandas
from collections import Counter
import matplotlib.patches as mpatches
import operator
from scipy.stats import hypergeom
def readGwasAssociationFile(disease):
genes = []
g_p = {}
insignificants = []
total_genes = []
#f = open(os.path.join('./Diseases', 'ulcerative_colitis.tsv'))
f = open(os.path.join('./Diseases', disease))
first_line = 0
num_genes = 0;
num_assoc = 0;
for line in f:
if (first_line == 1):
num_assoc += 1;
x = line.split('\t')
p_value = Decimal(x[27])
associated_gene = x[14]
if (disease == 'gwas_schizophrenia.tsv' or disease == 'gwas_Alzheimer.tsv'):
if (p_value < 0.0000001):
formatted_gene = associated_gene.replace('"', '').replace(',', '').replace(';', '').replace('-', '')
mapped_genes = formatted_gene.split(' ')
for gene in mapped_genes:
if gene:
genes.append(gene)
total_genes.append(gene)
num_genes += 1
g_p[gene] = p_value
elif (disease == 'gwas_Dementia.tsv'):
if (p_value < 0.00000005):
formatted_gene = associated_gene.replace('"', '').replace(',', '').replace(';', '').replace('-', '')
mapped_genes = formatted_gene.split(' ')
for gene in mapped_genes:
if gene:
genes.append(gene)
total_genes.append(gene)
num_genes += 1
g_p[gene] = p_value
else:
if (p_value < 0.000005):
formatted_gene = associated_gene.replace('"', '').replace(',', '').replace(';', '').replace('-', '')
mapped_genes = formatted_gene.split(' ')
for gene in mapped_genes:
if gene:
genes.append(gene)
total_genes.append(gene)
num_genes += 1
g_p[gene] = p_value
first_line = 1
#print("Number of mapped genes: ", num_genes, "\nNumber of associations: ", num_assoc)
return genes, g_p
def get_centrality(G, genes):
H = G.subgraph(genes)
Gcc=sorted(nx.connected_component_subgraphs(H), key = len, reverse=True)
G0 = Gcc[0]
G1 = Gcc[1]
F = nx.compose(G0, G1)
return F
def visualize(G, genes):
sm = []
sm_1 = []
mid = []
lg_1 = []
lg_2 = []
lg_3 = []
degrees = {}
H = G.subgraph(genes)
Gcc=sorted(nx.connected_component_subgraphs(H), key = len, reverse=True)
G0 = Gcc[0]
G1 = Gcc[1]
F = nx.compose(G0, G1)
nodes = F.nodes()
print len(list(nodes))
pos=nx.spring_layout(F)
for n in nodes:
degrees[n] = G.degree(n)
for n in nodes:
if G.degree(n) < 50:
sm.append(n)
# 5 x 10e-9
elif G.degree(n) < 100:
sm_1.append(n)
elif G.degree(n) < 150:
mid.append(n)
elif G.degree(n) < 200:
lg_1.append(n)
elif G.degree(n) < 250:
lg_2.append(n)
else:
lg_3.append(n)
#print "DEGREE DICTIONARY"
#print degrees
b0 = mpatches.Patch(color='#F8CDC1', label='d < 50')
b1 = mpatches.Patch(color='#F9A58A', label='d < 100')
b2 = mpatches.Patch(color='#F75927', label='d < 150')
b3 = mpatches.Patch(color='#F73E04', label='d < 200')
b4 = mpatches.Patch(color='#B02F07', label='d < 250')
b5 = mpatches.Patch(color='#750504', label='d > 250')
l = [b0, b1, b2, b3, b4, b5]
fig, ax = plt.subplots()
ax.legend(l, ['d < 50', 'd < 100', 'd < 150', 'd < 200', "d < 250", "d > 250"])
plt.legend(handles=l)
nx.draw_networkx_nodes(F,pos,nodelist=sm,node_color='#F8CDC1',node_size=300,alpha=0.9)
nx.draw_networkx_nodes(F,pos,nodelist=sm_1,node_color='#F9A58A',node_size=300,alpha=0.9)
nx.draw_networkx_nodes(F,pos,nodelist=mid,node_color='#F75927',node_size=300,alpha=0.9)
nx.draw_networkx_nodes(F,pos,nodelist=lg_1,node_color='#F73E04',node_size=300,alpha=0.9)
nx.draw_networkx_nodes(F,pos,nodelist=lg_2,node_color='#B02F07',node_size=300,alpha=0.9)
nx.draw_networkx_nodes(F,pos,nodelist=lg_3,node_color='#750504',node_size=300,alpha=0.9)
nx.draw_networkx_edges(F,pos=pos)
nx.draw_networkx_labels(F, pos=pos)
#plt.legend(handles=[b0, b1, b2, b3, b4, b5], loc='upper left')
#plt.colorbar(l)
#plt.axis('off')
plt.show()
def geometric_test(G, list1, list2, genes):
H = G.subgraph(genes)
overlap = 119
M = len(list(G.subgraph(list1)))
n = len(list(G.subgraph(list2)))
N = 10261
print overlap, M, n, N
pval = hypergeom.pmf(overlap-1, M, n, N)
print pval
# of 353 nodes,
def main():
fh = open(os.path.join('./PPI_Graphs', 'reactomefi2015.tsv'), 'rb')
G = nx.read_edgelist(fh, delimiter='\t')
fh.close()
file = 'gwas_depression.tsv'
mdd, g_p_mdd = readGwasAssociationFile(file)
list1 = mdd
#print "\n"
#print "ASSOCIATIONS DICT for DEPRESSION"
#print g_p_mdd
file = 'gwas_bipolar_disorder.tsv'
bp, g_p_bp = readGwasAssociationFile(file)
list2 = bp
#print "\n"
#print "ASSOCIATIONS DICT for BIPOLAR"
#print g_p_bp
file = 'gwas_schizophrenia.tsv'
schizo, g_p_schizo = readGwasAssociationFile(file)
#print "\n"
genes = mdd + bp + schizo
print len(genes)
H = G.subgraph(genes)
Gcc=sorted(nx.connected_component_subgraphs(H), key = len, reverse=True)
G0 = Gcc[0]
G1 = Gcc[1]
print len(Gcc)
#F = get_centrality(G, genes)
#nodes = nx.betweenness_centrality(F)
#sorted_nodes = sorted(nodes.items(), key=operator.itemgetter(1))
#print sorted_nodes
visualize(G, genes)
#geometric_test(G, list1, list2, genes)
if __name__ =='__main__':main()