forked from huddlej/tab-to-vcf
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtab_to_vcf.py
201 lines (163 loc) · 7.69 KB
/
tab_to_vcf.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
#!/bin/env python
"""
Convert the given tab-delimited document into a VCF 4.1 document for annotation with Seattle Seq.
"""
import argparse
import csv
import os
import yaml
from fastahack import FastaHack
import vcf
from vcf.model import _Record
TEMPLATE_VCF_FILE = os.path.join(os.path.dirname(__file__), "template-4.1.vcf")
# each pair here represents a REF==>ALT mapping
# For example, IUPAC "R":
# REF ALT
# A --> G
# G --> A
IUPAC_CODES = {
"R": {"A":"G", "G":"A"},
"Y": {"C":"T", "T":"C"},
"S": {"G":"C", "C":"G"},
"W": {"A":"T", "T":"A"},
"K": {"G":"T", "T":"G"},
"M": {"A":"C", "C":"A"},
}
VCF_COLUMN_ORDER = ["CHROM", "POS", "ID", "REF", "ALT", "QUAL", "FILTER"]
CHROMOSOME_INDEX = 0
POSITION_INDEX = 1
REF_INDEX = 3
ALT_INDEX = 4
def get_sequence(reference_dict, chrom, position):
position = int(position)
return reference_dict["%s:%s-%s" % (chrom, position, position)].upper()
def gatk_indel_to_vcf(vcf_row, reference_dict):
"""
Convert the given indel from GATK format to VCF 4.1 standard. For example,
the following lines should be converted from:
2 60689253 1720 * +G . . . .
21 38877833 1721 * -C . . . .
21 47958429 1722 * +CTGGTCT . . . .
to:
2 60689253 1720 A AG . . . .
21 38877833 1721 GC G . . . .
21 47958429 1722 A ACTGGTCT . . . .
>>> reference_dict = FastaHack("human_g1k_v37.fasta")
>>> gatk_indel_to_vcf(['2', 60689253, '1720', '*', '+G', '.', '.', '.', '.'], reference_dict)
['2', 60689253, '1720', 'A', 'AG', '.', '.', '.', '.']
>>> gatk_indel_to_vcf(['21', 38877833, '1721', '*', '-C', '.', '.', '.', '.'], reference_dict)
['21', 38877833, '1721', 'GC', 'G', '.', '.', '.', '.']
>>> gatk_indel_to_vcf(['21', 47958429, '1722', '*', '+CTGGTCT', '.', '.', '.', '.'], reference_dict)
['21', 47958429, '1722', 'A', 'ACTGGTCT', '.', '.', '.', '.']
"""
# Load the base at the given position.
reference_base = get_sequence(reference_dict, vcf_row[CHROMOSOME_INDEX], vcf_row[POSITION_INDEX])
# Create a new reference allele based on the event type (the position's base
# for insertions, the position base plus the deleted base(s) for deletions).
# Create a new alternate allele based on the event type (the position's base
# plus the inserted base(s) for insertions, the position's base for
# deletions).
if vcf_row[ALT_INDEX].startswith("+"):
vcf_row[REF_INDEX] = reference_base
vcf_row[ALT_INDEX] = vcf_row[ALT_INDEX].replace("+", reference_base)
elif vcf_row[ALT_INDEX].startswith("-"):
vcf_row[REF_INDEX] = "%s%s" % (reference_base, vcf_row[ALT_INDEX].lstrip("-"))
vcf_row[ALT_INDEX] = reference_base
return vcf_row
def _convert_iupac(vcf_row):
"""
Convert a REF/ALT genotype, where the ALT is an IUPAC code
Does not support triallelic iupac codes. Errors print warning and return input
>>> convert_iupac(['21', 47958429, '1722', 'T', 'W', '.', '.', '.', '.'])
['21', 47958429, '1722', 'T', 'A', '.', '.', '.', '.']
"""
if vcf_row[ALT_INDEX] in IUPAC_CODES.keys():
try:
vcf_row[ALT_INDEX] = IUPAC_CODES[vcf_row[ALT_INDEX]][vcf_row[REF_INDEX]]
except KeyError:
print "WARNING: could not convert IUPAC code (triallelic, malformed or other?) for row:"
print "\t".join(vcf_row)
finally:
return vcf_row
else:
return vcf_row
def tab_to_vcf(input_file, output_file, reference_file, columns, info_fields, convert_iupac=False):
"""
Convert tab-delimited file to VCF.
Support for the fixed VCF fields: #CHROM, POS, ID, REF, ALT, QUAL, FILTER, INFO
PyVCF's _Record class requires the following arguments:
CHROM, POS, ID, REF, ALT, QUAL, FILTER, INFO, FORMAT, sample_indexes
convert_iupac (bool) : When present, convert IUPAC codes to the non-reference allele.
This is only possible for when the reference and IUPAC-determined alternates share
at least one allele. Tri-allelic conversion is not supported and will emit a warning.
IUPAC codes: http://www.bioinformatics.org/sms/iupac.html
"""
reference_dict = FastaHack(reference_file)
with open(input_file, "r") as input_fh:
reader = csv.DictReader(input_fh, delimiter="\t")
with open(TEMPLATE_VCF_FILE, "r") as template_fh:
vcf_reader = vcf.Reader(template_fh)
with open(output_file, "w") as output_fh:
vcf_writer = vcf.Writer(output_fh, vcf_reader, lineterminator='\n')
for row in reader:
args = [row.get(columns.get(f,None), ".") for f in VCF_COLUMN_ORDER]
# Convert position to an integer.
args[POSITION_INDEX] = int(args[POSITION_INDEX])
# Convert indels from GATK to VCF format.
if args[ALT_INDEX].startswith(("+", "-")) and not "/" in args[ALT_INDEX]:
args = gatk_indel_to_vcf(args, reference_dict)
# Optionally convert IUPAC code
if convert_iupac:
args = _convert_iupac(args)
# Convert alternate allele scalar to a list.
args[ALT_INDEX] = [args[ALT_INDEX]]
# Convert info fields
if info_fields:
INFO = {}
for vcf_field,tab_field in info_fields.items():
if tab_field in row:
INFO[vcf_field] = row[tab_field]
else:
INFO = {}
# Add empty entries for INFO, FORMAT, and sample_indexes.
args.extend([INFO, ".", []])
record = _Record(*args)
vcf_writer.write_record(record)
def load_config(config_file):
y = yaml.load(open(config_file))
return y
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("input_file", help="tab-delimited input")
parser.add_argument("output_file", help="VCF 4.1 output")
parser.add_argument("--config", help="config.yaml configuration file", required=False)
parser.add_argument("--reference_file", help="reference assembly for variants in a single FASTA file")
parser.add_argument("--convert-iupac", help="Convert IUPAC codes to alternate allele only",
required=False, default=False, action="store_true")
parser.add_argument("--info-fields", help="input:ouput (comma separated) mapping for INFO field",
required=False, default=None)
args = parser.parse_args()
if args.config:
config = load_config(args.config)
args.convert_iupac = config["format"]["convert_iupac"]
args.reference_file = config["format"]["reference"]
COLUMNS = config["columns"]
INFO = config["info"]
else:
COLUMNS = {
"CHROM": "Chrom",
"POS": "Pos(hg19)",
"ID": "Unique id",
"REF": "Ref",
"ALT": "Allele",
"QUAL": "QUAL",
"FILTER": "FILTER",
}
if args.info_fields:
INFO_FIELDS = {}
mapping = args.info_fields.split(",")
for m in mapping:
k,v = m.strip(" ").split(":")
INFO_FIELDS[v] = k
tab_to_vcf(args.input_file, args.output_file, args.reference_file, columns=COLUMNS, info_fields=INFO,
convert_iupac=args.convert_iupac)