-
Notifications
You must be signed in to change notification settings - Fork 0
/
shortest_path_single_disease.py
201 lines (181 loc) · 7.8 KB
/
shortest_path_single_disease.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
import networkx as nx
import pickle
import random
import rdflib
import re
import pandas as pd
from rwr import partition
import os
import matplotlib.pyplot as plt
import seaborn as sns
url_to_common_name = {line.strip().split()[0]:line.strip().split()[1] for line in open('Edgelists/PKT_Master_Edge_List_NoOntologyData_LABELS.txt')}
G = nx.Graph()
# for each disease gene set
for line in open('Edgelists/PKT_Master_Edge_List_NoOntologyData.txt'):
edge = line.strip().split('\t')
if len(edge) != 4:
continue
subject = edge[1]
the_object = edge[3]
if subject in url_to_common_name:
subject = url_to_common_name[subject]
if the_object in url_to_common_name:
the_object = url_to_common_name[the_object]
G.add_edge(subject, the_object, predicate=edge[2], predicate_name=edge[0])
gene_sets = '2_disease.txt'
disease_gene_sets = {}
for line in open(gene_sets, 'r'):
row = line.strip().split('\t')
# the first item is the disease common name, everything else is genes
disease_gene_sets[row[0]] = row[1:]
paths = []
if True or not os.path.exists('AA_TOF_node_counts.pickle') or not os.path.exists('AA_TOF_counts.pickle') or not os.path.exists('AA_TOF_path_lengths.pickle'):
counts = {}
node_counts = {}
path_lengths = {'length': [], 'type': []}
d_keys = list(disease_gene_sets.keys())
disease = 'APLASTIC ANEMIA'
disease_genes = disease_gene_sets[disease]
nodes = partition(disease_genes, 2)
seeds, targets = nodes[0], nodes[1]
for i, seed in enumerate(seeds):
for target in targets:
try:
path = nx.shortest_path(G, seed, target)
except (nx.exception.NodeNotFound, nx.exception.NetworkXNoPath):
continue
paths.append(path)
path_lengths['length'].append(len(path))
path_lengths['type'].append('normal')
for i in range(len(path) - 1):
p = G[path[i]][path[i + 1]]['predicate_name']
# add proportional weight
if p in counts:
counts[p] += 1
else:
counts[p] = 1
# count the number times nodes occur in the paths
for node in path:
if node in node_counts:
node_counts[node] += 1
else:
node_counts[node] = 1
pickle.dump(counts, open('AA_TOF_counts.pickle', 'wb'))
pickle.dump(node_counts, open('AA_TOF_node_counts.pickle', 'wb'))
pickle.dump(path_lengths, open('AA_TOF_path_lengths.pickle', 'wb'))
counts = pickle.load(open('AA_TOF_counts.pickle', 'rb'))
node_counts = pickle.load(open('AA_TOF_node_counts.pickle', 'rb'))
path_lengths = pickle.load(open('AA_TOF_path_lengths.pickle', 'rb'))
df = pd.DataFrame({'predicate':list(counts.keys()),'count':list(counts.values())})
df = df.sort_values('predicate')
g = sns.barplot(df['predicate'], df['count'])
g.set_yscale("log")
plt.xticks(rotation=90)
plt.tight_layout()
plt.ylabel('Count')
plt.xlabel('Predicate')
plt.savefig('Figures/AA_TOF_predicate_counts_log.png')
plt.show()
g = sns.barplot(df['predicate'], df['count'])
plt.xticks(rotation=90)
plt.tight_layout()
plt.ylabel('Count')
plt.xlabel('Predicate')
plt.savefig('Figures/AA_TOF_predicate_counts.png')
plt.show()
# do it again with shuffled targets
shuffled_paths = []
if True or not os.path.exists('shuffled_AA_TOF_node_counts.pickle') or not os.path.exists('shuffled_AA_TOF_counts.pickle') or not os.path.exists('shuffled_AA_TOF_path_lengths.pickle'):
shuffled_counts = {}
shuffled_node_counts = {}
shuffled_path_lengths = {'length': [], 'type': []}
d_keys = list(disease_gene_sets.keys())
disease = 'APLASTIC ANEMIA'
disease_genes = disease_gene_sets[disease]
nodes = partition(disease_genes, 2)
seeds, targets = nodes[0], nodes[1]
shuffled_nodes = partition(disease_gene_sets['TETRALOGY OF FALLOT'], 2)
shuffled_seeds, shuffled_targets = shuffled_nodes[0], shuffled_nodes[1]
for i, seed in enumerate(seeds):
# use shuffled targets
for target in shuffled_targets:
try:
path = nx.shortest_path(G, seed, target)
except (nx.exception.NodeNotFound, nx.exception.NetworkXNoPath):
continue
shuffled_paths.append(path)
shuffled_path_lengths['length'].append(len(path))
shuffled_path_lengths['type'].append('shuffled')
for i in range(len(path) - 1):
p = G[path[i]][path[i + 1]]['predicate_name']
# add proportional weight
if p in shuffled_counts:
shuffled_counts[p] += 1
else:
shuffled_counts[p] = 1
# count the number times nodes occur in the paths
for node in path:
if node in shuffled_node_counts:
shuffled_node_counts[node] += 1
else:
shuffled_node_counts[node] = 1
pickle.dump(shuffled_counts, open('shuffled_AA_TOF_counts.pickle', 'wb'))
pickle.dump(shuffled_node_counts, open('shuffled_AA_TOF_node_counts.pickle', 'wb'))
pickle.dump(shuffled_path_lengths, open('shuffled_AA_TOF_path_lengths.pickle', 'wb'))
shuffled_counts = pickle.load(open('shuffled_AA_TOF_counts.pickle', 'rb'))
shuffled_node_counts = pickle.load(open('shuffled_AA_TOF_node_counts.pickle', 'rb'))
shuffled_path_lengths = pickle.load(open('shuffled_AA_TOF_path_lengths.pickle', 'rb'))
shuffled_df = pd.DataFrame({'predicate':list(shuffled_counts.keys()),'count':list(shuffled_counts.values())})
shuffled_df = shuffled_df.sort_values('predicate')
g = sns.barplot(shuffled_df['predicate'], shuffled_df['count'])
g.set_yscale("log")
plt.xticks(rotation=90)
plt.tight_layout()
plt.ylabel('Count')
plt.xlabel('Predicate')
plt.savefig('Figures/shuffled_AA_TOF_predicate_counts_log.png')
plt.show()
g = sns.barplot(shuffled_df['predicate'], shuffled_df['count'])
plt.xticks(rotation=90)
plt.tight_layout()
plt.ylabel('Count')
plt.xlabel('Predicate')
plt.savefig('Figures/shuffled_AA_TOF_predicate_counts.png')
plt.show()
# plot change
# get all predicates
all_preds = set(df['predicate']).union(set(shuffled_df['predicate']))
diff_dict = {'predicate': [], 'difference': [], 'normal':[]}
for p in all_preds:
norm_count = 0
shuf_count = 0
if p in counts:
norm_count = counts[p]
if p in shuffled_counts:
shuf_count = shuffled_counts[p]
diff_dict['predicate'].append(p)
diff_dict['normal'].append(norm_count)
diff_dict['difference'].append(norm_count - shuf_count)
sns.barplot(diff_dict['predicate'],diff_dict['difference'])
plt.xticks(rotation=90)
plt.tight_layout()
plt.ylabel('Difference (Normal Count - Shuffled Count)')
plt.xlabel('Predicate')
plt.savefig('Figures/AA_TOF_norm_shuffled_change.png')
plt.show()
sns.scatterplot(diff_dict['normal'],diff_dict['difference'])
plt.xticks(rotation=90)
plt.tight_layout()
plt.ylabel('Normal vs Difference')
plt.xlabel('Normal Count')
plt.savefig('Figures/AA_TOF_difference_v_norm_scatter.png')
plt.show()
# plot path lengths:
length_df = pd.concat([pd.DataFrame(path_lengths),pd.DataFrame(shuffled_path_lengths)])
sns.boxplot(length_df['type'],length_df['length'])
plt.savefig('Figures/AA_TOF_shuffled_length_boxplots.png')
plt.show()
filtered_node_counts = {n:node_counts[n] for n in node_counts if n not in disease_gene_sets['Inclusion Body Myopathy, Sporadic']}
node_counts_df = pd.DataFrame({'node':filtered_node_counts.keys(),'count':filtered_node_counts.values()}).sort_values('count',ascending=False)
filtered_shuffled_node_counts = {n:shuffled_node_counts[n] for n in shuffled_node_counts if n not in disease_gene_sets['Randomly Chosen Null Set']}
shuffled_node_counts_df = pd.DataFrame({'node':filtered_shuffled_node_counts.keys(),'count':filtered_shuffled_node_counts.values()}).sort_values('count',ascending=False)