Repository navigation
Expand file tree
/
Copy pathgemini_set_somatic.py
More file actions
executable file
·135 lines (103 loc) · 4.63 KB
/
Copy pathgemini_set_somatic.py
File metadata and controls
executable file
·135 lines (103 loc) · 4.63 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
#!/usr/bin/env python
import sqlite3
from collections import Counter
from gemini_constants import *
import gemini_subjects
import GeminiQuery
def tag_somatic_mutations(args):
t_n_pairs = gemini_subjects.get_families(args.db)
gq = GeminiQuery.GeminiQuery(args.db)
depth_string, qual_string, chrom_string = ("", "", "")
if args.min_depth:
depth_string = " AND depth >= %s" % args.min_depth
if args.min_qual:
qual_string = " AND qual >= %s" % args.min_qual
if args.chrom:
chrom_string = " AND chrom = '%s'" % args.chrom
if args.chrom is None:
query = "SELECT variant_id, chrom, start, end, \
ref, alt, gene, impact, gts, gt_types, \
gt_ref_depths, gt_alt_depths \
FROM variants \
WHERE 1 \
%s \
%s \
%s" % (depth_string, qual_string, chrom_string)
gq.run(query)
smp2idx = gq.sample_to_idx
somatic_counter = 0
somatic_v_ids = []
if args.dry_run:
print'\t'.join(['tum_name', 'tum_gt', 'tum_alt_freq', 'tum_alt_depth', 'tum_depth', \
'nrm_name', 'nrm_gt', 'nrm_alt_freq', 'nrm_alt_depth', 'nrm_depth',
'chrom', 'start', 'end', 'ref', 'alt', 'gene'])
for row in gq:
# we can skip variants where all genotypes are identical
if len(set(row['gt_types'])) == 1:
continue
for pair in t_n_pairs:
samples = pair.subjects
if len(samples) != 2:
continue
tumor = pair.subjects[0]
normal = pair.subjects[1]
# swap if we guessed the tumor incorrectly
if tumor.affected is False:
tumor, normal = normal, tumor
tum_idx = smp2idx[tumor.name]
nrm_idx = smp2idx[normal.name]
tum_gt = row['gts'][tum_idx]
nrm_gt = row['gts'][nrm_idx]
tum_gt_type = row['gt_types'][tum_idx]
nrm_gt_type = row['gt_types'][nrm_idx]
if nrm_gt_type == tum_gt_type:
continue
if nrm_gt_type == UNKNOWN or tum_gt_type == UNKNOWN:
continue
# the genotypes pass the smell test for somatic
# mutations if in this block.
if (nrm_gt_type == HOM_REF and tum_gt_type != HOM_REF):
tum_ref_depth = row['gt_ref_depths'][tum_idx]
nrm_ref_depth = row['gt_ref_depths'][nrm_idx]
tum_alt_depth = row['gt_alt_depths'][tum_idx]
nrm_alt_depth = row['gt_alt_depths'][nrm_idx]
# total observed depth
nrm_depth = nrm_alt_depth + nrm_ref_depth
tum_depth = tum_alt_depth + tum_ref_depth
if (nrm_depth < args.min_norm_depth \
or \
tum_depth < args.min_tumor_depth):
continue
try:
tum_alt_freq = float(tum_alt_depth) / \
(float(tum_alt_depth) + float(tum_ref_depth))
except ZeroDivisionError:
tum_alt_freq = 'NA'
try:
nrm_alt_freq = float(nrm_alt_depth) / \
(float(nrm_alt_depth) + float(nrm_ref_depth))
except ZeroDivisionError:
nrm_alt_freq = 'NA'
# apply evidence thresholds.
if (args.max_norm_alt_freq and nrm_alt_freq > args.max_norm_alt_freq) \
or \
(args.max_norm_alt_count and nrm_alt_depth > args.max_norm_alt_count):
continue
somatic_counter += 1
somatic_v_ids.append((1, row['variant_id']))
print'\t'.join(str(s) for s in [tumor.name, tum_gt, tum_alt_freq, tum_alt_depth, tum_depth, \
normal.name, nrm_gt, nrm_alt_freq, nrm_alt_depth, nrm_depth, \
row['chrom'], row['start'], row['end'], row['ref'], row['alt'], row['gene']])
if not args.dry_run:
conn = sqlite3.connect(args.db)
conn.isolation_level = None
c = conn.cursor()
# now set the identified mutations to True.
update_qry = "UPDATE variants SET is_somatic = ? "
update_qry += " WHERE variant_id = ?"
c.executemany(update_qry, somatic_v_ids)
print "Identified and set", somatic_counter, "somatic mutations"
else:
print "Would have identified and set", somatic_counter, "somatic mutations"
def set_somatic(parser, args):
tag_somatic_mutations(args)