-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvcf_write.py
302 lines (251 loc) · 9.51 KB
/
vcf_write.py
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
295
296
297
298
299
300
301
302
# file from github.com/cggh/scikit-allel with modifications to support writing sample data
import csv
from datetime import date
import itertools
from operator import itemgetter
import numpy as np
import allel
VCF_FIXED_FIELDS = 'CHROM', 'POS', 'ID', 'REF', 'ALT', 'QUAL', 'FILTER', 'INFO', 'FORMAT'
VCF_FIXED_FORMAT = 'GT'
def normalize_callset(callset):
if hasattr(callset, 'keys'):
names = list()
new_callset = dict()
if 'samples' in callset.keys():
new_callset['samples'] = callset['samples']
else:
raise ValueError('Callset must contain samples')
for k in list(callset.keys()):
a = callset[k]
if k.startswith('samples'):
continue
if k.startswith('calldata/'):
if not k.lower().startswith('calldata/gt'):
continue
k = k[9:]
if k.startswith('variants/'):
k = k[9:]
names.append(k)
new_callset[k] = a
callset = new_callset
elif hasattr(callset, 'dtype') and callset.dtype.names:
names = list(callset.dtype.names)
else:
raise ValueError('callset should be dict or recarray, found %r' % callset)
return names, callset
def write_vcf(path, callset, rename=None, number=None, description=None,
fill=None, write_header=True):
"""Preliminary support for writing a VCF file. Currently does not support sample data.
Needs further work."""
names, callset = normalize_callset(callset)
with open(path, 'w') as vcf_file:
if write_header:
write_vcf_header(vcf_file, names, callset=callset, rename=rename,
number=number, description=description)
write_vcf_data(vcf_file, names, callset=callset, rename=rename, fill=fill)
def write_vcf_header(vcf_file, names, callset, rename, number, description):
if rename is None:
rename = dict()
if number is None:
number = dict()
if description is None:
description = dict()
# write file format version
print('##fileformat=VCFv4.1', file=vcf_file)
# write today's date
today = date.today().strftime('%Y%m%d')
print('##fileDate=%s' % today, file=vcf_file)
# write source
print('##source=scikit-allel-%s' % allel.__version__, file=vcf_file)
info_names = [n for n in names
if not n.upper().startswith('FILTER_') and not n.upper() in VCF_FIXED_FIELDS and n.upper() != 'SAMPLES' and n.upper() != 'GT']
info_ids = [rename[n] if n in rename else n
for n in info_names]
# write INFO headers, sorted by ID
for name, vcf_id in sorted(zip(info_names, info_ids), key=itemgetter(1)):
col = callset[name]
# determine VCF Number
if name in number:
vcf_number = number[name]
else:
if col.ndim == 1 and col.dtype.kind == 'b':
# Flag
vcf_number = 0
elif col.ndim == 1:
vcf_number = 1
elif col.ndim == 2:
vcf_number = col.shape[1]
else:
raise NotImplementedError('only columns with 1 or two '
'dimensions are supported')
# determine VCF Type
kind = col.dtype.kind
if kind == 'b':
vcf_type = 'Flag'
elif kind in 'ui':
vcf_type = 'Integer'
elif kind == 'f':
vcf_type = 'Float'
else:
vcf_type = 'String'
# determine VCF Description
if name in description:
vcf_description = description[name]
else:
vcf_description = ''
# construct INFO header line
header_line = '##INFO=<ID=%s,Number=%s,Type=%s,Description="%s">'\
% (vcf_id, vcf_number, vcf_type, vcf_description)
print(header_line, file=vcf_file)
filter_names = [n for n in names if n.upper().startswith('FILTER_')]
filter_ids = [rename[n] if n in rename else n[7:]
for n in filter_names]
# write FILTER headers, sorted by ID
for name, vcf_id in sorted(zip(filter_names, filter_ids),
key=itemgetter(1)):
# determine VCF Description
if name in description:
vcf_description = description[name]
else:
vcf_description = ''
# construct FILTER header line
header_line = '##FILTER=<ID=%s,Description="%s">'\
% (vcf_id, vcf_description)
print(header_line, file=vcf_file)
# write FORMAT header
header_line = '##FORMAT=<ID=GT,Number=1,Type=String,Description="Phased Genotype">'
print(header_line, file=vcf_file)
# write column names
line = '#' + '\t'.join(list(VCF_FIXED_FIELDS) + callset['samples'])
print(line, file=vcf_file)
# noinspection PyShadowingBuiltins
def write_vcf_data(vcf_file, names, callset, rename, fill):
if rename is None:
rename = dict()
if fill is None:
fill = dict()
# find the fixed columns, allowing for case insensitive naming in the
# input array
col_chrom = None
col_pos = None
col_id = None
col_ref = None
col_alt = None
col_qual = None
for n in names:
if n.upper() == 'CHROM':
col_chrom = callset[n]
elif n.upper() == 'POS':
col_pos = callset[n]
elif n.upper() == 'ID':
col_id = callset[n]
elif n.upper() == 'REF':
col_ref = callset[n]
elif n.upper() == 'ALT':
col_alt = callset[n]
elif n.upper() == 'QUAL':
col_qual = callset[n]
elif n.upper() == 'GT':
GT = callset[n]
# check for required columns
if col_chrom is None:
raise ValueError('CHROM column not found')
if col_pos is None:
raise ValueError('POS column not found')
# pad optional columns
dot = itertools.repeat('.')
if col_id is None:
col_id = dot
if col_ref is None:
col_ref = dot
if col_alt is None:
col_alt = dot
if col_qual is None:
col_qual = dot
# find FILTER columns
filter_names = [n for n in names
if n.upper().startswith('FILTER_')]
filter_ids = [rename[n] if n in rename else n[7:]
for n in filter_names]
filter_cols = [callset[n] for n in filter_names]
# sort by ID
if filter_names:
filters = sorted(zip(filter_names, filter_ids, filter_cols),
key=itemgetter(1))
filter_names, filter_ids, filter_cols = zip(*filters)
# find INFO columns
info_names = [n for n in names
if not n.upper().startswith('FILTER_') and not n.upper() in VCF_FIXED_FIELDS and n.upper() != 'SAMPLES' and n.upper() != 'GT']
info_ids = [rename[n] if n in rename else n
for n in info_names]
info_cols = [callset[n] for n in info_names]
# sort by ID
if info_names:
infos = sorted(zip(info_names, info_ids, info_cols),
key=itemgetter(1))
info_names, info_ids, info_cols = zip(*infos)
# setup writer
writer = csv.writer(vcf_file, delimiter='\t', lineterminator='\n')
# zip up data as rows
rows = zip(col_chrom, col_pos, col_id, col_ref, col_alt, col_qual)
filter_rows = zip(*filter_cols)
info_rows = zip(*info_cols)
for i, (row, filter_row, info_row) in enumerate(itertools.zip_longest(rows, filter_rows, info_rows)):
# unpack main row
chrom, pos, id, ref, alt, qual = row
chrom = _vcf_value_str(chrom)
pos = _vcf_value_str(pos)
id = _vcf_value_str(id)
ref = _vcf_value_str(ref)
alt = _vcf_value_str(alt, fill=fill.get('ALT', None))
qual = _vcf_value_str(qual)
# construct FILTER value
if filter_row is not None:
flt = [i for i, v in zip(filter_ids, filter_row) if v]
if flt:
flt = ';'.join(flt)
else:
flt = 'PASS'
else:
flt = '.'
# construct INFO value
if info_row is not None:
info_vals = [_vcf_info_str(n, i, v, fill)
for n, i, v in zip(info_names, info_ids, info_row)]
info_vals = [x for x in info_vals if x is not None]
info = ';'.join(info_vals)
else:
info = '.'
# construct FORMAT value
form = VCF_FIXED_FORMAT
# construct sample values
sample_data = tuple(['{}|{}'.format(genotype_to_str(diploid_genotype[0]), genotype_to_str(diploid_genotype[1])) for diploid_genotype in GT[i]])
# repack
row = chrom, pos, id, ref, alt, qual, flt, info, form
row += sample_data
writer.writerow(row)
def _vcf_value_str(o, fill=None):
if isinstance(o, bytes):
return str(o, encoding='ascii')
elif isinstance(o, (tuple, list, np.ndarray)):
if fill is None:
t = [_vcf_value_str(x) for x in o]
else:
t = [_vcf_value_str(x) for x in o if x != fill]
return ','.join(t)
else:
return str(o)
# noinspection PyShadowingBuiltins
def _vcf_info_str(name, id, value, fill):
if isinstance(value, (bool, np.bool_)):
if bool(value):
return id
else:
return None
else:
return '%s=%s' % (id, _vcf_value_str(value, fill=fill.get(name, None)))
def genotype_to_str(genotype):
if isinstance(genotype, np.int8) and genotype >= 0:
return str(genotype)
elif genotype == -1:
return '.'