Repository navigation
Expand file tree
/
Copy pathtool_homozygosity_runs.py
More file actions
215 lines (174 loc) · 7 KB
/
Copy pathtool_homozygosity_runs.py
File metadata and controls
215 lines (174 loc) · 7 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
import os
import sys
from collections import defaultdict
from gemini_constants import *
import GeminiQuery
class Site(object):
def __init__(self, row):
self.chrom = row['chrom']
self.end = int(row['end'])
self.gt_type = None
def _prune_run(run):
"""
Prune the current run of genotypes.
Remove genotypes from the left of the first
non-homozygous genotype, since, owing to the
same logic behind run length encoding, those
genotypes cannot be part of a longer run than
we have seen before.
For example:
breaks = * * *
run = H H H h H H H U H H H h H H H H H H
prune_1 = H H H U H H H h H H H H H H
prune_2 = H H H h H H H H H H
prune_3 = H H H H H H
"""
try:
first_het_idx = run.index('H')
except:
first_het_idx = None
try:
first_unk_idx = run.index('U')
except:
first_unk_idx = None
if first_het_idx is not None and first_unk_idx is not None:
idx_of_first_disruption = min(run.index('H'), run.index('U'))
elif first_het_idx is not None:
idx_of_first_disruption = first_het_idx
elif first_unk_idx is not None:
idx_of_first_disruption = first_unk_idx
else:
# no interuptions, return an empty list
return 0, 0, len(run), []
hets_removed = run[0:idx_of_first_disruption+1].count('H')
unks_removed = run[0:idx_of_first_disruption+1].count('U')
homs_removed = idx_of_first_disruption - (hets_removed + unks_removed) + 1
return hets_removed, unks_removed, homs_removed, run[idx_of_first_disruption+1:]
def sweep_genotypes_for_rohs(args, chrom, samples):
"""
Sweep through the genotypes for each sample in search of ROHs.
Note: If the genotype was homozygous, the end position
of the variant is stored. Otherwise 'H' for het
and 'U' for unknown.
"""
hom_count = 0
het_count = 0
unk_count = 0
curr_run = []
for sample in samples:
sites = iter(samples[sample])
for site in sites:
# retain the last homozygote from previous
# run. See function docs for details
if len(curr_run):
hets_removed, unks_removed, homs_removed, curr_run = \
_prune_run(curr_run)
# reset for next run
hom_count -= homs_removed
het_count -= hets_removed
unk_count -= unks_removed
# sweep through the active sites until we encounter
# too many HETS or UNKNOWN genotypes.
while het_count <= args.max_hets and unk_count <= args.max_unknowns:
if site != 'H' and site != 'U':
hom_count +=1
curr_run.append(site)
elif site == 'H':
curr_run.append(site)
het_count += 1
elif site == 'U':
curr_run.append(site)
unk_count += 1
try:
site = sites.next()
except:
break
# skip the current run unless it contains enough sites.
if hom_count >= args.min_snps:
run_start = min(c for c in curr_run if c not in ['H', 'U'])
run_end = max(c for c in curr_run if c not in ['H', 'U'])
run_length = run_end - run_start
# report the run if it is long enough.
if run_length >= args.min_size:
density_per_kb = float(len(curr_run) * 1000) / float(run_length)
print "\t".join(str(s) for s in [chrom,
run_start, run_end, sample,
hom_count, round(density_per_kb, 4),
run_length])
else:
curr_run = []
hom_count = 0
het_count = 0
unk_count = 0
def get_homozygosity_runs(args):
gq = GeminiQuery.GeminiQuery(args.db)
# get a mapping of sample ids to sample indices
idx2smp = gq.index2sample
smp2idx = gq.sample2index
sm_index = []
# prepare a lookup of just the samples
# for which the user wishes to search for ROHs
if args.samples is not None:
sample_filter = args.samples.strip().split(",")
for sample in sample_filter:
try:
idx = smp2idx[sample]
except:
sys.exit("ERROR: Sample %s could not be found. Exiting.\n" \
% (sample))
sm_index.append(smp2idx[sample])
else:
for sample in smp2idx:
sm_index.append(smp2idx[sample])
###########################################################################
# Phase 1. Retrieve the variants for each chrom/sample
###########################################################################
query = "SELECT chrom, start, end, gt_types, gt_depths \
FROM variants \
WHERE type = 'snp' \
AND filter is NULL \
AND depth >= " + str(args.min_total_depth) + \
" ORDER BY chrom, end"
sys.stderr.write("LOG: Querying and ordering variants by chromosomal position.\n")
gq.run(query, needs_genotypes=True)
print "\t".join(['chrom',
'start', 'end', 'sample',
'num_of_snps','density_per_kb',
'run_length_in_bp'])
variants_seen = 0
samples = defaultdict(list)
prev_chrom = None
curr_chrom = None
for row in gq:
variants_seen += 1
if variants_seen % 10000 == 0:
sys.stderr.write("LOG: Loaded %d variants. Current variant on %s, position %d.\n" \
% (variants_seen, row['chrom'], row['end']))
gt_types = row['gt_types']
gt_depths = row['gt_depths']
curr_chrom = row['chrom']
# the chromosome has changed. search for ROHs in the previous chrom
if curr_chrom != prev_chrom and prev_chrom is not None:
sweep_genotypes_for_rohs(args, prev_chrom, samples)
samples = defaultdict(list)
# associate the genotype for the variant with each sample
for idx in sm_index:
sample = idx2smp[idx]
gt_type = gt_types[idx]
depth = gt_depths[idx]
# the genotype must have had sufficient depth to be considered
if depth < args.min_genotype_depth:
continue
if (gt_type == HOM_ALT or gt_type == HOM_REF):
samples[sample].append(row['end'])
elif gt_type == HET:
samples[sample].append('H')
elif gt_type == UNKNOWN:
samples[sample].append('U')
prev_chrom = curr_chrom
# search for ROHs in the final chromosome
sweep_genotypes_for_rohs(args, curr_chrom, samples)
def run(parser, args):
if os.path.exists(args.db):
# run the roh caller
get_homozygosity_runs(args)