forked from arq5x/gemini
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtool_fusions.py
More file actions
188 lines (169 loc) · 6.92 KB
/
Copy pathtool_fusions.py
File metadata and controls
188 lines (169 loc) · 6.92 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
#!/usr/bin/env python
import sqlite3
import os
import sys
from collections import defaultdict
import GeminiQuery
import gemini_utils as util
from gemini_constants import *
import gemini_subjects as subjects
def report_fusion(event, subjects_dict, args):
"""
Report the fusion event.
"""
# filter single line events
if len(event) == 1:
sv = event.pop()
gene1 = sv['gene']
gene1_strand = sv['strand']
gene1_start = sv['transcript_min_start']
gene1_end = sv['transcript_max_end']
# query the table to test whether the END breakpoint lies in a gene
gq = GeminiQuery.GeminiQuery(args.db)
query = """SELECT gene,
strand,
in_cosmic_census
FROM gene_summary
WHERE gene_summary.chrom = '%s'
AND (gene_summary.transcript_min_start > %s
OR gene_summary.transcript_max_end < %s)
AND gene_summary.transcript_min_start < %s
AND gene_summary.transcript_max_end > %s
AND gene_summary.gene != 'None'
LIMIT 1
""" % (sv['chrom'],
sv['transcript_max_end'],
sv['transcript_min_start'],
sv['end'],
sv['end'])
gq.run(query)
gene2, gene2_strand, gene2_cosmic = (None, None, None)
for row in gq:
gene2 = row['gene']
gene2_strand = row['strand']
gene2_cosmic = row['in_cosmic_census']
break # just get the first gene interrupted by the breakend
# if SV is a deletion or duplication, genes must be same strand for fusion
if sv['sub_type'] == 'DEL' or sv['sub_type'] == 'DUP':
if gene1_strand != gene2_strand:
return
# if SV is an inversion, genes must be opposite strands for fusion
if sv['sub_type'] == 'INV':
if gene1_strand == gene2_strand:
return
# check COSMIC status, if required
if args.in_cosmic_census and not (sv['in_cosmic_census'] or gene2_cosmic):
return
# pass the variables for compatibility with multi-line variants
end1 = sv
end2_chrom = end1['chrom']
end2_start = sv['sv_cipos_start_right']
end2_end = sv['sv_cipos_end_right']
# filter multi-line events
elif len(event) == 2:
end1 = event.pop()
end2 = event.pop()
gene1_strand, gene2_strand = end1['strand'], end2['strand'] # this is gene_summary.strand
# require that the genes are non-overlapping
if (end1['chrom'] == end2['chrom'] \
and end1['transcript_max_end'] >= end2['transcript_min_start'] \
and end1['transcript_min_start'] <= end2['transcript_max_end']):
return
# if breakpoint joins same strand,
# then genes must be same strand for fusion
if (end1['sv_strand'][0] == end1['sv_strand'][1] \
and gene1_strand != gene2_strand):
return
# if breakpoint joins opposite strands,
# then genes must also be opposite strands for fusion
if (end1['sv_strand'][0] != end1['sv_strand'][1] \
and gene1_strand == gene2_strand):
return
# check COSMIC status, if required
if args.in_cosmic_census and not (end1['in_cosmic_census'] or end2['in_cosmic_census']):
return
# store the second end for compatibility with single-line SVs
gene2 = end2['gene']
end2_chrom = end2['chrom']
end2_start = end2['sv_cipos_start_right']
end2_end = end2['sv_cipos_end_right']
# fusion passes all filters, print
print '\t'.join(map(str,
[end1['chrom'],
end1['sv_cipos_start_left'] - 1,
end1['sv_cipos_end_left'],
end2_chrom,
end2_start - 1,
end2_end,
end1['sv_event_id'],
end1['qual'],
end1['sv_strand'][0],
end1['sv_strand'][1],
end1['sub_type'],
end1['gene'],
gene2,
end1['sv_tool'],
end1['sv_evidence_type'],
end1['sv_is_precise'],
','.join(end1['variant_samples'])
])
)
return
def get_fusions(args):
"""
Identify candidate rearrangments resulting in fusion genes.
"""
gq = GeminiQuery.GeminiQuery(args.db, include_gt_cols=True)
idx_to_sample = gq.idx_to_sample
subjects_dict = subjects.get_subjects(args)
# create strings for gemini query of command line args
qual_string, ev_type_string, cosmic_string = ("", "", "")
if args.min_qual:
qual_string = " AND qual >= %s" % args.min_qual
if args.evidence_type:
ev_type_string = " AND sv_evidence_type = '%s'" % args.evidence_type
query = """SELECT variants.chrom, start, end,
ref, alt,
qual,
is_somatic, somatic_score,
type, sub_type, variants.gene,
sv_strand, sv_length,
sv_cipos_start_left,
sv_cipos_start_right,
sv_cipos_end_left,
sv_cipos_end_right,
sv_event_id, sv_mate_id,
sv_tool, sv_evidence_type,
sv_is_precise,
gene_summary.strand,
gene_summary.transcript_min_start,
gene_summary.transcript_max_end,
gene_summary.in_cosmic_census
FROM variants, gene_summary
WHERE is_somatic = 1
AND type = 'sv'
AND variants.gene is not NULL
AND variants.chrom = gene_summary.chrom
AND variants.gene = gene_summary.gene
%s
%s
ORDER BY sv_event_id
""" % (qual_string, ev_type_string)
curr = None
prev = None
gq.run(query)
for row in gq:
# single-line variants (DEL, DUP, INV)
if row['sub_type'] != 'complex':
report_fusion([row], subjects_dict, args)
# multi-line variants (BND)
elif row['sv_mate_id']:
curr = row
# the SV event ids match, and prev is not None
if (prev and curr['sv_event_id'] == prev['sv_event_id']):
report_fusion([prev, curr], subjects_dict, args)
# shift the previous
prev = curr
def run(parser, args):
if os.path.exists(args.db):
get_fusions(args)