Repository navigation
Expand file tree
/
Copy pathtool_interactions.py
More file actions
executable file
·294 lines (268 loc) · 12.3 KB
/
Copy pathtool_interactions.py
File metadata and controls
executable file
·294 lines (268 loc) · 12.3 KB
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
#!/usr/bin/env python
###########################################################################################################################################
#1. user defined root(may or may not be a mutated gene for the sample); get all interacting partners from the variant list of the sample
#2. For a root fixed to a lof gene in each sample, get the interacting partners
#3. a network graph of mutated genes for each sample, a subnetwork for any gene in that list (not executed here)
###########################################################################################################################################
import os
import sys
import sqlite3
import numpy as np
import zlib
import cPickle
from gemini.config import read_gemini_config
from pygraph.classes.graph import graph
from pygraph.readwrite.dot import write
from pygraph.algorithms.searching import breadth_first_search
from pygraph.algorithms.minmax import shortest_path
from pygraph.classes.exceptions import AdditionError
from pygraph.algorithms.filters.radius import radius
from pygraph.classes.digraph import digraph
import gemini_utils as util
from gemini_constants import *
from collections import defaultdict
def get_variant_genes(c, args, idx_to_sample):
samples = defaultdict(list)
for r in c:
gt_types = np.array(cPickle.loads(zlib.decompress(r['gt_types'])))
gts = np.array(cPickle.loads(zlib.decompress(r['gts'])))
var_id = str(r['variant_id'])
chrom = str(r['chrom'])
start = str(r['start'])
end = str(r['end'])
gene = str(r['gene'])
impact = str(r['impact'])
biotype = str(r['biotype'])
in_dbsnp = str(r['in_dbsnp'])
clinvar_sig = str(r['clinvar_sig'])
clinvar_disease_name = str(r['clinvar_disease_name'])
aaf_1kg_all = str(r['aaf_1kg_all'])
aaf_esp_all = str(r['aaf_esp_all'])
for idx, gt_type in enumerate(gt_types):
if (gt_type == HET or gt_type == HOM_ALT):
if gene != "None":
(key, value) = (idx_to_sample[idx], \
(gene,var_id,chrom,start,end,impact, \
biotype,in_dbsnp,clinvar_sig, \
clinvar_disease_name,aaf_1kg_all, \
aaf_esp_all))
samples[idx_to_sample[idx]].append(value)
return samples
def get_lof_genes(c, args, idx_to_sample):
lof = defaultdict(list)
for r in c:
gt_types = np.array(cPickle.loads(zlib.decompress(r['gt_types'])))
gts = np.array(cPickle.loads(zlib.decompress(r['gts'])))
gene = str(r['gene'])
for idx, gt_type in enumerate(gt_types):
if (gt_type == HET or gt_type == HOM_ALT):
if gene != "None":
(key, value) = (idx_to_sample[idx], gene)
lof[idx_to_sample[idx]].append(gene)
return lof
def sample_gene_interactions(c, args, idx_to_sample):
out = open("file.dot", 'w')
#fetch variant gene dict for all samples
samples = get_variant_genes(c, args, idx_to_sample)
#file handle for fetching the hprd graph
config = read_gemini_config( args = args )
path_dirname = config["annotation_dir"]
file_graph = os.path.join(path_dirname, 'hprd_interaction_graph')
#load the graph using cPickle and close file handle
gr = graph()
f = open(file_graph, 'rb')
gr = cPickle.load(f)
f.close()
k = []
variants = []
#calculate nodes from the graph
hprd_genes = gr.nodes()
if args.gene == None or args.gene not in hprd_genes:
sys.stderr.write("Gene name not found or")
sys.stderr.write(" gene not in p-p interaction file\n")
elif args.gene in hprd_genes:
x, y = \
breadth_first_search(gr,root=args.gene,filter=radius(args.radius))
gst = digraph()
gst.add_spanning_tree(x)
dot = write(gst)
out.write(dot)
st, sd = shortest_path(gst, args.gene)
if args.var_mode:
for sample in samples.iterkeys():
var = samples[str(sample)]
#for each level return interacting genes if they are
# variants in the sample.
# 0th order would be returned if the user chosen
# gene is a variant in the sample
for x in range(0, (args.radius+1)):
for each in var:
for key, value in sd.iteritems():
if value == x and key == each[0]:
print "\t".join([str(sample),str(args.gene), \
str(x), \
str(key), \
str(each[1]), \
str(each[2]), \
str(each[3]), \
str(each[4]), \
str(each[5]), \
str(each[6]), \
str(each[7]), \
str(each[8]), \
str(each[9]), \
str(each[10]), \
str(each[11])])
elif (not args.var_mode):
for sample in samples.iterkeys():
for each in samples[str(sample)]:
variants.append(each[0])
for x in range(0, (args.radius+1)):
for key, value in sd.iteritems():
if value == x and key in set(variants):
k.append(key)
if k:
print "\t".join([str(sample), str(args.gene), \
str(x)+"_order:",
",".join(k)])
else:
print "\t".join([str(sample), str(args.gene), \
str(x)+"_order:", "none"])
#initialize keys for next iteration
k = []
#initialize variants list for next iteration
variants = []
def sample_lof_interactions(c, args, idx_to_sample, samples):
lof = get_lof_genes(c, args, idx_to_sample)
#file handle for fetching the hprd graph
config = read_gemini_config( args = args )
path_dirname = config["annotation_dir"]
file_graph = os.path.join(path_dirname, 'hprd_interaction_graph')
#load the graph using cPickle and close file handle
gr = graph()
f = open(file_graph, 'rb')
gr = cPickle.load(f)
f.close()
#calculate nodes from the graph
hprd_genes = gr.nodes()
#initialize keys
k = []
variants = []
if (not args.var_mode):
for sample in lof.iterkeys():
lofvariants = list(set(lof[str(sample)]))
for each in samples[str(sample)]:
variants.append(each[0])
for gene in lofvariants:
if gene in hprd_genes:
x, y = \
breadth_first_search(gr,root=gene,\
filter=radius(args.radius))
gst = digraph()
gst.add_spanning_tree(x)
st, sd = shortest_path(gst, gene)
# for each level return interacting genes
# if they are variants in the sample.
for rad in range(1, (args.radius+1)):
for key, value in sd.iteritems():
if (value == rad) and key in set(variants):
k.append(key)
if k:
print "\t".join([str(sample), \
str(gene), \
str(rad)+"_order:",
",".join(k)])
else:
print "\t".join([str(sample), \
str(gene), \
str(rad)+"_order:", \
"none"])
#initialize k
k = []
#initialize variants list for next iteration
variants = []
elif args.var_mode:
for sample in lof.iterkeys():
lofvariants = list(set(lof[str(sample)]))
var = samples[str(sample)]
for gene in lofvariants:
if gene in hprd_genes:
x, y = \
breadth_first_search(gr,root=gene, \
filter=radius(args.radius))
gst = digraph()
gst.add_spanning_tree(x)
st, sd = shortest_path(gst, gene)
for rad in range(1, (args.radius+1)):
for each in var:
for key, value in sd.iteritems():
if value == rad and key == each[0]:
print "\t".join([str(sample), \
str(gene), \
str(rad), \
str(key), \
str(each[1]), \
str(each[2]), \
str(each[3]), \
str(each[4]), \
str(each[5]), \
str(each[6]), \
str(each[7]), \
str(each[8]), \
str(each[9]), \
str(each[10]), \
str(each[11])])
def sample_variants(c, args):
idx_to_sample = util.map_indices_to_samples(c)
query = "SELECT variant_id, gt_types, gts, gene, impact, biotype, \
in_dbsnp, clinvar_sig, clinvar_disease_name, aaf_1kg_all, aaf_esp_all, chrom, \
start, end \
FROM variants"
c.execute(query)
if args.command == 'interactions':
#header
if args.var_mode:
print "\t".join(['sample','gene','order_of_interaction', \
'interacting_gene', 'var_id', 'chrom', 'start', \
'end', 'impact', 'biotype', 'in_dbsnp', \
'clinvar_sig', 'clinvar_disease_name', 'aaf_1kg_all', \
'aaf_esp_all'])
if (not args.var_mode):
print "\t".join(['sample','gene','order_of_interaction', \
'interacting_gene'])
sample_gene_interactions(c, args, idx_to_sample)
elif args.command == 'lof_interactions':
samples = get_variant_genes(c, args, idx_to_sample)
return samples
def sample_lof_variants(c, args, samples):
idx_to_sample = util.map_indices_to_samples(c)
query = "SELECT chrom, start, end, \
gt_types, gts, gene \
FROM variants \
WHERE is_lof='1'"
c.execute(query)
#header
if args.var_mode:
print "\t".join(['sample','lof_gene','order_of_interaction', \
'interacting_gene', 'var_id', 'chrom', 'start', \
'end', 'impact','biotype','in_dbsnp', 'clinvar_sig', \
'clinvar_disease_name', 'aaf_1kg_all','aaf_esp_all'])
elif (not args.var_mode):
print "\t".join(['sample','lof_gene','order_of_interaction', \
'interacting_gene'])
sample_lof_interactions(c, args, idx_to_sample, samples)
def genequery(parser, args):
if os.path.exists(args.db):
conn = sqlite3.connect(args.db)
conn.isolation_level = None
conn.row_factory = sqlite3.Row
c = conn.cursor()
sample_variants(c, args)
def lofgenequery(parser, args):
if os.path.exists(args.db):
conn = sqlite3.connect(args.db)
conn.isolation_level = None
conn.row_factory = sqlite3.Row
c = conn.cursor()
samples = sample_variants(c, args)
sample_lof_variants(c, args, samples)