-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathdartcsv2vcf.py
More file actions
277 lines (230 loc) · 8.27 KB
/
dartcsv2vcf.py
File metadata and controls
277 lines (230 loc) · 8.27 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
#!/usr/bin/env python
"""
dartcsv2vcf.py: Converts DArT-seq CSV file (Report_*_SNP_mapping_1.csv) to VCF
"""
import sys
import math
import argparse
CSV_COMMENT = "*"
CSV_HEADER_START = "AlleleID"
KNOWN_METADATA_COLS = {
"AlleleID",
"CloneID",
"AlleleSequence",
"AlleleSequenceRef",
"AlleleSequenceSnp",
"TrimmedSequence",
"TrimmedSequenceRef",
"TrimmedSequenceSnp",
"SNP",
"SnpPosition",
"CallRate",
"OneRatioRef",
"OneRatioSnp",
"FreqHomRef",
"FreqHomSnp",
"FreqHets",
"PICRef",
"PICSnp",
"AvgPIC",
"AvgCountRef",
"AvgCountSnp",
"RepAvg",
}
VCF_META_HEADER = """\
##fileformat=VCFv4.2
##source=dartcsv2vcf.py
##INFO=<ID=DP,Number=1,Type=Float,Description="Average total read depth (AvgCountRef + AvgCountSnp)">
##INFO=<ID=CR,Number=1,Type=Float,Description="Call rate (fraction of samples with a genotype call)">
##INFO=<ID=AF,Number=A,Type=Float,Description="Alternate allele frequency (FreqHomSnp + 0.5 * FreqHets)">
##INFO=<ID=HET,Number=1,Type=Float,Description="Observed heterozygote frequency">
##INFO=<ID=PIC,Number=1,Type=Float,Description="Average Polymorphism Information Content">
##INFO=<ID=REP,Number=1,Type=Float,Description="Reproducibility average">
##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype">
"""
VCF_COL_HEADER = "#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT"
__author__ = "Pim Bongaerts"
__copyright__ = "Copyright (C) 2021 Pim Bongaerts"
__license__ = "GPL"
def get_output_filename(csv_filename, extension):
return "{0}.{1}".format(csv_filename.replace(".csv", ""), extension)
def get_dict_of_name_changes(samplenames_filename):
"""Initialise dictionary with old (keys) and new (values)"""
rename_file = open(samplenames_filename, "r")
name_changes = {}
for line in rename_file:
cols = line.replace(",", "\t").split()
name_changes[cols[0].strip()] = cols[1].strip()
rename_file.close()
return name_changes
def get_new_sample_names(old_samples, name_changes):
"""Change samples to new sample names"""
sample_names = []
for sample in old_samples:
if sample.strip() in name_changes:
sample_names.append(name_changes[sample.strip()])
else:
print("Not renamed: {0}".format(sample))
sample_names.append(sample.strip())
return sample_names
def find_column_indices(header_cols):
"""Detect column positions from header names, returns dict"""
stripped = [c.strip() for c in header_cols]
col_map = {name: i for i, name in enumerate(stripped)}
idx = {}
idx["CloneID"] = col_map.get("CloneID")
idx["SNP"] = col_map.get("SNP")
idx["SnpPosition"] = col_map.get("SnpPosition")
idx["AlleleSeq"] = col_map.get("AlleleSequenceRef", col_map.get("AlleleSequence"))
missing = [k for k, v in idx.items() if v is None]
if missing:
sys.exit("Error: missing required columns: {}".format(", ".join(missing)))
for name in (
"CallRate",
"AvgCountRef",
"AvgCountSnp",
"RepAvg",
"FreqHomSnp",
"FreqHets",
"AvgPIC",
):
idx[name] = col_map.get(name)
first_sample_col = None
for i, name in enumerate(stripped):
if name not in KNOWN_METADATA_COLS:
first_sample_col = i
break
if first_sample_col is None:
sys.exit("Error: no sample columns found in header")
idx["first_sample"] = first_sample_col
return idx
def convert_to_vcf_genotype(genotype):
"""Convert DaRT to VCF genotype"""
dart_gt = genotype.strip()
if dart_gt == "-":
vcf_gt = "./."
elif dart_gt == "0":
vcf_gt = "0/0"
elif dart_gt == "1":
vcf_gt = "1/1"
elif dart_gt == "2":
vcf_gt = "0/1"
else:
print(dart_gt)
vcf_gt = "err"
return vcf_gt
def get_vcf_genotypes(dart_genotypes):
"""Get VCF genotypes"""
return "\t".join([convert_to_vcf_genotype(dart_gt) for dart_gt in dart_genotypes])
def repavg_to_phred(repavg):
"""Convert reproducibility (0-1) to Phred-scaled quality score"""
try:
r = float(repavg)
except (ValueError, TypeError):
return "."
if r >= 1.0:
return "100"
if r <= 0.0:
return "0"
return "{:.1f}".format(-10 * math.log10(1 - r))
def build_info_field(cols, idx):
"""Build VCF INFO field from DArT metadata columns"""
info_parts = []
if idx.get("AvgCountRef") is not None and idx.get("AvgCountSnp") is not None:
try:
dp = float(cols[idx["AvgCountRef"]]) + float(cols[idx["AvgCountSnp"]])
info_parts.append("DP={:.1f}".format(dp))
except (ValueError, IndexError):
pass
if idx.get("CallRate") is not None:
try:
info_parts.append("CR={}".format(cols[idx["CallRate"]].strip()))
except IndexError:
pass
if idx.get("FreqHomSnp") is not None and idx.get("FreqHets") is not None:
try:
af = float(cols[idx["FreqHomSnp"]]) + 0.5 * float(cols[idx["FreqHets"]])
info_parts.append("AF={:.4f}".format(af))
except (ValueError, IndexError):
pass
if idx.get("FreqHets") is not None:
try:
info_parts.append("HET={}".format(cols[idx["FreqHets"]].strip()))
except IndexError:
pass
if idx.get("AvgPIC") is not None:
try:
info_parts.append("PIC={}".format(cols[idx["AvgPIC"]].strip()))
except IndexError:
pass
if idx.get("RepAvg") is not None:
try:
info_parts.append("REP={}".format(cols[idx["RepAvg"]].strip()))
except IndexError:
pass
return ";".join(info_parts) if info_parts else "."
def output_vcf_line(vcf_file, chrom, pos, ref, alt, qual, info, vcf_genotypes_concat):
"""Output VCF line"""
vcf_file.write(
"{0}\t{1}\t.\t{2}\t{3}\t{4}\tPASS\t{5}\tGT\t{6}\n".format(
chrom, pos, ref, alt, qual, info, vcf_genotypes_concat
)
)
def main(dartcsv_filename, samplenames_filename=None):
name_changes = None
if samplenames_filename:
name_changes = get_dict_of_name_changes(samplenames_filename)
vcf_file = open(get_output_filename(dartcsv_filename, "vcf"), "w")
fasta_file = open(get_output_filename(dartcsv_filename, "fa"), "w")
vcf_file.write(VCF_META_HEADER)
idx = None
csv_file = open(dartcsv_filename, "r")
for line in csv_file:
cols = line.split(",")
if line.startswith(CSV_HEADER_START):
idx = find_column_indices(cols)
if name_changes:
sample_names = get_new_sample_names(
cols[idx["first_sample"] :], name_changes
)
else:
sample_names = [s.strip() for s in cols[idx["first_sample"] :]]
vcf_file.write("{0}\t{1}\n".format(VCF_COL_HEADER, "\t".join(sample_names)))
elif line[0] != CSV_COMMENT and idx is not None:
ref_allele, divider, alt_allele = cols[idx["SNP"]].split(":")[1]
vcf_genotypes_concat = get_vcf_genotypes(cols[idx["first_sample"] :])
qual = "."
if idx.get("RepAvg") is not None:
qual = repavg_to_phred(cols[idx["RepAvg"]])
info = build_info_field(cols, idx)
output_vcf_line(
vcf_file,
cols[idx["CloneID"]],
cols[idx["SnpPosition"]],
ref_allele,
alt_allele,
qual,
info,
vcf_genotypes_concat,
)
fasta_file.write(">{0}\n".format(cols[idx["CloneID"]]))
fasta_file.write("{0}\n".format(cols[idx["AlleleSeq"]]))
csv_file.close()
vcf_file.close()
fasta_file.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"dartcsv_filename", metavar="dartcsv_filename", help="DArT-seq csv filename"
)
parser.add_argument(
"samplenames_filename",
metavar="samplenames_file",
nargs="?",
default=None,
help="optional text file (tsv or csv) with old and "
"new name for each sample (not all samples have to "
"be listed)",
)
args = parser.parse_args()
main(args.dartcsv_filename, args.samplenames_filename)