forked from DaehwanKimLab/hisat2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_snps.py
More file actions
executable file
·278 lines (237 loc) · 8.9 KB
/
Copy pathextract_snps.py
File metadata and controls
executable file
·278 lines (237 loc) · 8.9 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
#!/usr/bin/env python
#
# Copyright 2015, Daehwan Kim <infphilo@gmail.com>
#
# This file is part of HISAT 2.
#
# HISAT 2 is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# HISAT 2 is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with HISAT 2. If not, see <http://www.gnu.org/licenses/>.
#
import sys, re
from collections import defaultdict as dd, Counter
from argparse import ArgumentParser, FileType
"""
"""
def reverse_complement(seq):
result = ""
for nt in seq:
base = nt
if nt == 'A':
base = 'T'
elif nt == 'a':
base = 't'
elif nt == 'C':
base = 'G'
elif nt == 'c':
base = 'g'
elif nt == 'G':
base = 'C'
elif nt == 'g':
base = 'c'
elif nt == 'T':
base = 'A'
elif nt == 't':
base = 'a'
result = base + result
return result
"""
"""
def read_genome(genome_file):
chr_dic = {}
chr_name, sequence = "", ""
for line in genome_file:
if line[0] == ">":
if chr_name and sequence:
chr_dic[chr_name] = sequence
chr_name = line[1:-1]
sequence = ""
else:
sequence += line[:-1]
if chr_name and sequence:
chr_dic[chr_name] = sequence
return chr_dic
"""
"""
def extract_snps(genome_file, snp_file, verbose = False, testset = False):
# load genomic sequences
chr_dic = read_genome(genome_file)
if testset:
testset_bname = genome_file.name.split(".")[0]
ref_testset_file = open(testset_bname + ".ref.testset.fa", "w")
alt_testset_file = open(testset_bname + ".alt.testset.fa", "w")
# load SNPs
snp_list = []
for line in snp_file:
if not line or line.startswith('#'):
continue
line = line.strip()
try:
"""
id, chr, start, end, rs_id, score, strand, refNCBI, refUCSC, observed, molType, classType, valid, \
avHet, avHetSE, func, locType, weight, exceptions, submitterCount, submitters, \
alleleFreqCount, alleles, alleleNs, alleleFreqs, bitfields = line.split("\t")
"""
id, chr, start, end, rs_id, score, strand, refNCBI, refUCSC, observed, molType, classType = line.split('\t')[:12]
except ValueError:
continue
start, end = int(start), int(end)
score = int(score)
if molType != "genomic":
continue
if classType not in ["single", "deletion", "insertion"]:
continue
if classType == "single":
if start + 1 != end:
continue
elif classType == "deletion":
assert start < end
else:
assert classType == "insertion"
assert start == end
if chr not in chr_dic:
continue
chr_seq = chr_dic[chr]
chr_len = len(chr_seq)
if start >= len(chr_seq):
continue
# daehwan - for debugging purposes
# """
if len(snp_list) > 0:
_, _, last_chr, last_start, _, _ = snp_list[-1]
if chr == last_chr and abs(start - last_start) <= 20:
continue
# """
observed = observed.upper()
allele_list = observed.split("/")
# Reverse complement alleles if strand is negative
if strand == "-":
tmp_allele_list = []
for allele in allele_list:
tmp_allele_list.append(reverse_complement(allele))
allele_list = tmp_allele_list
if classType == "single":
ref_base = chr_seq[start].upper()
if ref_base not in allele_list:
continue
for allele in allele_list:
if allele not in "ACGT" or len(allele) != 1:
continue
if allele == ref_base:
continue
snp_list.append([rs_id, classType, chr, start, end, allele])
if testset:
ref_seq = chr_seq[start-50:start+50]
alt_seq = chr_seq[start-50:start] + allele + chr_seq[start+1:start+50]
print >> ref_testset_file, ">%s_single_%d" % (rs_id, start - 50)
print >> ref_testset_file, ref_seq
print >> alt_testset_file, ">%s_single_%d_%s" % (rs_id, start - 50, ref_seq)
print >> alt_testset_file, alt_seq
elif classType == "deletion":
snp_list.append([rs_id, classType, chr, start, end, ""])
delLen = end -start
if testset and delLen > 0 and delLen <= 10:
ref_seq = chr_seq[start-50:start+50]
alt_seq = chr_seq[start-50:start] + chr_seq[start+delLen:start+50+delLen]
print >> ref_testset_file, ">%s_deletion_%d" % (rs_id, start - 50)
print >> ref_testset_file, ref_seq
print >> alt_testset_file, ">%s_deletion_%d_%s" % (rs_id, start - 50, ref_seq)
print >> alt_testset_file, alt_seq
else:
assert classType == "insertion"
for allele in allele_list:
if allele == "-" or len(allele) <= 0:
continue
if re.match('[ACGT]+', allele):
snp_list.append([rs_id, classType, chr, start, end, allele])
insLen = len(allele)
if testset and insLen > 0 and insLen <= 10:
ref_seq = chr_seq[start-50:start+50]
alt_seq = chr_seq[start-50:start] + allele + chr_seq[start:start+50-insLen]
print >> ref_testset_file, ">%s_insertion_%d" % (rs_id, start - 50)
print >> ref_testset_file, ref_seq
print >> alt_testset_file, ">%s_insertion_%d_%s" % (rs_id, start - 50, ref_seq)
print >> alt_testset_file, alt_seq
if testset:
ref_testset_file.close()
alt_testset_file.close()
# Sort SNPs (snp_list) according to chromosomes, genomic coordinates, types, and alleles
def snp_cmp(a, b):
_, classType1, chr1, start1, end1, allele1 = a
_, classType2, chr2, start2, end2, allele2 = b
if chr1 < chr2:
return -1
elif chr2 < chr1:
return 1
if start1 < start2:
return -1
elif start2 < start1:
return 1
if end1 < end2:
return -1
elif end2 < end1:
return 1
if classType1 != classType2:
if classType1 == "single":
return -1
elif classType2 == "single":
return 1
elif classType1 == "deletion":
return -1
else:
assert classType1 == "insertion" and classType2 == "deletion"
return 1
if allele1 < allele2:
return -1
elif allele2 < allele1:
return 1
else:
return 0
snp_list = sorted(snp_list, cmp=snp_cmp)
assert len(snp_list) > 0
tmp_snp_list = snp_list[:1]
for snp in snp_list[1:]:
if cmp(tmp_snp_list[-1], snp) != 0:
tmp_snp_list.append(snp)
snp_list = tmp_snp_list
print >> sys.stderr, "Number of SNPs: %d" % (len(snp_list))
for snp in snp_list:
id, classType, chr, start, end, allele = snp
if classType in ["single", "insertion"]:
out = "%s\t%s\t%s\t%d\t%s" % (id, classType, chr, start, allele)
elif classType in ["deletion"]:
out = "%s\t%s\t%s\t%d\t%d" % (id, classType, chr, start, end - start)
print out
if __name__ == '__main__':
parser = ArgumentParser(
description='Extract SNPs from a SNP file')
parser.add_argument('genome_file',
nargs='?',
type=FileType('r'),
help='input genome file')
parser.add_argument('snp_file',
nargs='?',
type=FileType('r'),
help='input snp file')
parser.add_argument('-v', '--verbose',
dest='verbose',
action='store_true',
help='also print some statistics to stderr')
parser.add_argument('--testset',
dest='testset',
action='store_true',
help='print test reads')
args = parser.parse_args()
if not args.snp_file:
parser.print_help()
exit(1)
extract_snps(args.genome_file, args.snp_file, args.verbose, args.testset)