-
Notifications
You must be signed in to change notification settings - Fork 19
/
writer.py
419 lines (356 loc) · 12.4 KB
/
writer.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
"""Class for writing SNPs."""
import logging
import warnings
import numpy as np
import pandas as pd
import snps
from snps.io import get_empty_snps_dataframe
from snps.utils import clean_str, get_utc_now, save_df_as_csv
logger = logging.getLogger(__name__)
class Writer:
"""Class for writing SNPs to files."""
def __init__(
self,
snps=None,
filename="",
vcf=False,
atomic=True,
vcf_alt_unavailable=".",
vcf_chrom_prefix="",
vcf_qc_only=False,
vcf_qc_filter=False,
**kwargs,
):
"""Initialize a `Writer`.
Parameters
----------
snps : SNPs
SNPs to save to file or write to buffer
filename : str or buffer
filename for file to save or buffer to write to
vcf : bool
flag to save file as VCF
atomic : bool
atomically write output to a file on local filesystem
vcf_alt_unavailable : str
representation of VCF ALT allele when ALT is not able to be determined
vcf_chrom_prefix : str
prefix for chromosomes in VCF CHROM column
vcf_qc_only : bool
for VCF, output only SNPs that pass quality control
vcf_qc_filter : bool
for VCF, populate VCF FILTER column based on quality control results
**kwargs
additional parameters to `pandas.DataFrame.to_csv`
"""
self._snps = snps
self._filename = filename
self._vcf = vcf
self._atomic = atomic
self._vcf_alt_unavailable = vcf_alt_unavailable
self._vcf_chrom_prefix = vcf_chrom_prefix
self._vcf_qc_only = vcf_qc_only
self._vcf_qc_filter = vcf_qc_filter
self._kwargs = kwargs
def write(self):
"""Write SNPs to file or buffer.
Returns
-------
str
path to file in output directory if SNPs were saved, else empty str
discrepant_vcf_position : pd.DataFrame
SNPs with discrepant positions discovered while saving VCF
"""
if self._vcf:
return self._write_vcf()
else:
return (self._write_csv(),)
@classmethod
def write_file(
cls,
snps=None,
filename="",
vcf=False,
atomic=True,
vcf_alt_unavailable=".",
vcf_qc_only=False,
vcf_qc_filter=False,
**kwargs,
):
warnings.warn(
"This method will be removed in a future release.", DeprecationWarning
)
w = cls(
snps=snps,
filename=filename,
vcf=vcf,
atomic=atomic,
vcf_alt_unavailable=vcf_alt_unavailable,
vcf_qc_only=vcf_qc_only,
vcf_qc_filter=vcf_qc_filter,
**kwargs,
)
return w.write()
def _write_csv(self):
"""Write SNPs to a CSV file.
Returns
-------
str
path to file in output directory if SNPs were saved, else empty str
"""
filename = self._filename
if not filename:
ext = ".txt"
if "sep" in self._kwargs and self._kwargs["sep"] == ",":
ext = ".csv"
filename = f"{clean_str(self._snps.source)}_{self._snps.assembly}{ext}"
comment = (
f"# Source(s): {self._snps.source}\n"
f"# Build: {self._snps.build}\n"
f"# Build Detected: { self._snps.build_detected}\n"
f"# Phased: {self._snps.phased}\n"
f"# SNPs: {self._snps.count}\n"
f"# Chromosomes: {self._snps.chromosomes_summary}\n"
)
if "header" in self._kwargs:
if isinstance(self._kwargs["header"], bool):
if self._kwargs["header"]:
self._kwargs["header"] = ["chromosome", "position", "genotype"]
else:
self._kwargs["header"] = ["chromosome", "position", "genotype"]
return save_df_as_csv(
self._snps._snps,
self._snps._output_dir,
filename,
comment=comment,
atomic=self._atomic,
**self._kwargs,
)
def _write_vcf(self):
"""Write SNPs to a VCF file.
References
----------
1. The Variant Call Format (VCF) Version 4.2 Specification, 8 Mar 2019,
https://samtools.github.io/hts-specs/VCFv4.2.pdf
Returns
-------
str
path to file in output directory if SNPs were saved, else empty str
discrepant_vcf_position : pd.DataFrame
SNPs with discrepant positions discovered while saving VCF
"""
filename = self._filename
if not filename:
filename = f"{clean_str(self._snps.source)}_{self._snps.assembly}{'.vcf'}"
comment = (
f"##fileformat=VCFv4.2\n"
f'##fileDate={get_utc_now().strftime("%Y%m%d")}\n'
f'##source="{self._snps.source}; snps v{snps.__version__}; https://pypi.org/project/snps/"\n'
)
reference_sequence_chroms = (
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"11",
"12",
"13",
"14",
"15",
"16",
"17",
"18",
"19",
"20",
"21",
"22",
"X",
"Y",
"MT",
)
df = self._snps.snps
p = self._snps._parallelizer
tasks = []
# skip insertions and deletions
df = df.drop(
df.loc[
df["genotype"].notnull()
& (
(df["genotype"].str[0] == "I")
| (df["genotype"].str[0] == "D")
| (df["genotype"].str[1] == "I")
| (df["genotype"].str[1] == "D")
)
].index
)
chroms_to_drop = []
for chrom in df["chrom"].unique():
if chrom not in reference_sequence_chroms:
chroms_to_drop.append(chrom)
continue
tasks.append(
{
"resources": self._snps._resources,
"assembly": self._snps.assembly,
"chrom": chrom,
"snps": pd.DataFrame(df.loc[(df["chrom"] == chrom)]),
"cluster": (
self._snps.cluster
if self._vcf_qc_only or self._vcf_qc_filter
else ""
),
"low_quality_snps": (
self._snps.low_quality
if self._vcf_qc_only or self._vcf_qc_filter
else get_empty_snps_dataframe()
),
}
)
# drop chromosomes without reference sequence data (e.g., unassigned PAR)
for chrom in chroms_to_drop:
df = df.drop(df.loc[df["chrom"] == chrom].index)
# create the VCF representation for SNPs
results = p(self._create_vcf_representation, tasks)
contigs = []
vcf = [pd.DataFrame()]
discrepant_vcf_position = [pd.DataFrame()]
for result in list(results):
contigs.append(result["contig"])
vcf.append(result["vcf"])
discrepant_vcf_position.append(result["discrepant_vcf_position"])
vcf = pd.concat(vcf)
discrepant_vcf_position = pd.concat(discrepant_vcf_position)
comment += "".join(contigs)
if self._vcf_qc_filter and self._snps.cluster:
comment += '##FILTER=<ID=lq,Description="Low quality SNP per Lu et al.: https://doi.org/10.1016/j.csbj.2021.06.040">\n'
comment += '##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype">\n'
comment += "#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT\tSAMPLE\n"
return (
save_df_as_csv(
vcf,
self._snps._output_dir,
filename,
comment=comment,
prepend_info=False,
header=False,
index=False,
na_rep=".",
sep="\t",
),
discrepant_vcf_position,
)
def _create_vcf_representation(self, task):
resources = task["resources"]
assembly = task["assembly"]
chrom = task["chrom"]
snps = task["snps"]
cluster = task["cluster"]
low_quality_snps = task["low_quality_snps"]
if len(snps.loc[snps["genotype"].notnull()]) == 0:
return {
"contig": "",
"vcf": pd.DataFrame(),
"discrepant_vcf_position": pd.DataFrame(),
}
seqs = resources.get_reference_sequences(assembly, [chrom])
seq = seqs[chrom]
contig = f'##contig=<ID={seq.ID},URL={seq.url},length={seq.length},assembly={seq.build},md5={seq.md5},species="{seq.species}">\n'
if self._vcf_qc_only and cluster:
# drop low quality SNPs if SNPs object maps to a cluster
snps = snps.drop(snps.index.intersection(low_quality_snps.index))
if self._vcf_qc_filter and cluster:
# initialize filter for all SNPs if SNPs object maps to a cluster,
snps["filter"] = "PASS"
# then indicate SNPs that were identified as low quality
snps.loc[snps.index.intersection(low_quality_snps.index), "filter"] = "lq"
snps = snps.reset_index()
df = pd.DataFrame(
columns=[
"CHROM",
"POS",
"ID",
"REF",
"ALT",
"QUAL",
"FILTER",
"INFO",
"FORMAT",
"SAMPLE",
]
)
df = df.astype(
{
"CHROM": object,
"POS": np.uint32,
"ID": object,
"REF": object,
"ALT": object,
"QUAL": np.float32,
"FILTER": object,
"INFO": object,
"FORMAT": object,
"SAMPLE": object,
}
)
df["CHROM"] = self._vcf_chrom_prefix + snps["chrom"]
df["POS"] = snps["pos"]
df["ID"] = snps["rsid"]
if self._vcf_qc_filter and cluster:
df["FILTER"] = snps["filter"]
# drop SNPs with discrepant positions (outside reference sequence)
discrepant_vcf_position = snps.loc[
(snps.pos - seq.start < 0) | (snps.pos - seq.start > seq.length - 1)
]
df.drop(discrepant_vcf_position.index, inplace=True)
# https://stackoverflow.com/a/24838429
df["REF"] = list(map(chr, seq.sequence[df.POS - seq.start]))
df["FORMAT"] = "GT"
seq.clear()
df["genotype"] = snps["genotype"]
temp = df.loc[df["genotype"].notnull()]
# https://stackoverflow.com/a/19976286
df.loc[df["genotype"].notnull(), "ALT"] = np.vectorize(self._compute_alt)(
temp["REF"], temp["genotype"]
)
temp = df.loc[df["genotype"].notnull()]
df.loc[df["genotype"].notnull(), "SAMPLE"] = np.vectorize(
self._compute_genotype
)(temp["REF"], temp["ALT"], temp["genotype"])
df.loc[df["SAMPLE"].isnull(), "SAMPLE"] = "./."
del df["genotype"]
return {
"contig": contig,
"vcf": df,
"discrepant_vcf_position": discrepant_vcf_position,
}
def _compute_alt(self, ref, genotype):
genotype_alleles = list(set(genotype))
if ref in genotype_alleles:
if len(genotype_alleles) == 1:
return self._vcf_alt_unavailable
else:
genotype_alleles.remove(ref)
return genotype_alleles.pop(0)
else:
genotype_alleles.sort()
return ",".join(genotype_alleles)
def _compute_genotype(self, ref, alt, genotype):
alleles = [ref]
if self._snps.phased:
separator = "|"
else:
separator = "/"
if pd.notna(alt):
alleles.extend(alt.split(","))
if len(genotype) == 2:
return (
f"{alleles.index(genotype[0])}{separator}{alleles.index(genotype[1])}"
)
else:
return f"{alleles.index(genotype[0])}"