generated from NCBI-Codeathons/codeathon-team-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimplevcf.py
273 lines (227 loc) · 7.89 KB
/
simplevcf.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
from __future__ import annotations
from typing import Any
import numpy as np
import oxbow as ox
import pandas as pd
import polars as pl
import pyarrow as pa
import pysam
import bioframe
# Maps the VCF-derived input types to numpy/pandas dtypes
TYPE_MAP = {
"Integer": "int64",
"Float": "float64",
"String": "object",
"Flag": "bool",
}
def _read_info_schema(f: pysam.VariantFile) -> pd.DataFrame:
return pd.DataFrame(
[
(obj.name, obj.number, obj.type, obj.description)
for obj in f.header.info.values()
],
columns=["name", "number", "type", "description"],
)
def _read_sample_schema(f: pysam.VariantFile) -> pd.DataFrame:
return pd.DataFrame(
[
(obj.name, obj.number, obj.type, obj.description)
for obj in f.header.formats.values()
],
columns=["name", "number", "type", "description"],
)
def _read_vcf_as_records(
f: pysam.VariantFile,
query: str | None,
info_fields: set[str],
sample_fields: set[str],
samples: list[str],
include_unspecified: bool,
) -> list[dict[str, Any]]:
if query is not None:
record_iter = f.fetch(*bioframe.parse_region(query))
else:
record_iter = f
records = []
for record in record_iter:
# Main fields
dct = {
"chrom": record.chrom,
"pos": record.pos,
"id": record.id,
"ref": record.ref,
"alts": list(record.alts),
"qual": record.qual,
"filters": list(record.filter.keys()),
}
# Info
for key, value in record.info.items():
if key in info_fields or include_unspecified:
if isinstance(value, tuple):
dct[key] = list(value)
else:
dct[key] = value
# Samples
for sample, genotype in record.samples.items():
if sample in samples or include_unspecified:
for key, value in genotype.items():
if key in sample_fields or include_unspecified:
if key == "GT":
dct[f"{sample}.GT"] = list(genotype[key])
dct[f"{sample}.phased"] = genotype.phased
elif isinstance(value, tuple):
dct[f"{sample}.{key}"] = list(value)
else:
dct[f"{sample}.{key}"] = value
records.append(dct)
return records
def read_info_schema(path: str):
"""
Read the schema of the INFO column of a VCF.
This currently uses pysam.
Parameters
----------
path : str
Path to VCF file.
Returns
-------
pd.DataFrame
Dataframe with [name, number, type] columns
Notes
-----
Possible values for `type` are: "Integer", "Float", "String", "Flag".
Possible values for `number` are:
- An integer (e.g. 0, 1, 2, 3, 4, etc.) - for fields where the number of
values per VCF record is fixed. 0 means the field is a "Flag".
- A string ("A", "G", "R") - for fields where the number of values per VCF
record is determined by the number of alts, the total number of alleles,
or the number of genotypes, respectively.
- A dot (".") - for fields where the number of values per VCF record
varies, is unknown, or is unbounded.
"""
with pysam.VariantFile(path) as f:
return _read_info_schema(f)
def read_sample_schema(path: str):
"""
Read the schema of the genotype sample columns of a VCF.
This currently uses pysam.
Parameters
----------
f : pysam.VariantFile
Returns
-------
pd.DataFrame
Dataframe with [name, number, type] columns
Notes
-----
Possible values for `type` are: "Integer", "Float", and "String".
"""
with pysam.VariantFile(path) as f:
return _read_sample_schema(f)
def read_vcf_as_pandas(
path: str,
query: str | None = None,
info_fields: list[str] | None = None,
sample_fields: list[str] | None = None,
samples: list[str] | None = None,
include_unspecified: bool = False,
) -> pd.DataFrame:
"""
Read a VCF into a pandas dataframe, extracting INFO and sample genotype fields.
Parameters
----------
path : str
Path to VCF file.
query : str, optional
Genomic range query string. If None, all records will be read.
info_fields : list[str], optional
List of fields to extract from the INFO column. If None, all fields
will be extracted.
sample_fields: list[str], optional
List of fields to extract from the sample genotype columns. If None,
all fields will be extracted.
samples : list[str], optional
List of samples to extract. If None, all samples will be extracted.
Returns
-------
pd.DataFrame
Pandas DataFrame with columns corresponding to the requested fields.
"""
with pysam.VariantFile(path) as f:
info_schema = _read_info_schema(f)
sample_schema = _read_sample_schema(f)
if info_fields is None:
info_fields = list(info_schema["name"])
if sample_fields is None:
sample_fields = list(sample_schema["name"])
if samples is None:
samples = list(f.header.samples)
records = _read_vcf_as_records(
f, query, set(info_fields), set(sample_fields), samples, include_unspecified
)
df = pd.DataFrame.from_records(records)
columns = (
["chrom", "pos", "id", "ref", "alts", "qual", "filters"]
+ info_fields
+ [f"{sample}.{field}" for sample in samples
for field in [f"phased", *sample_fields]]
)
columns = columns + list(set(df.columns) - set(columns))
for col in columns:
if col not in df.columns:
df[col] = pd.NA
return df[columns].fillna(np.nan)
def read_vcf_as_polars(
path: str,
query: str | None = None,
info_fields: list[str] | None = None,
sample_fields: list[str] | None = None,
samples: list[str] | None = None,
include_unspecified: bool = False,
) -> pd.DataFrame:
"""
Read a VCF into a polars dataframe, extracting INFO and sample genotype fields.
Parameters
----------
path : str
Path to VCF file.
query : str, optional
Genomic range query string. If None, all records will be read.
info_fields : list[str], optional
List of fields to extract from the INFO column. If None, all fields
will be extracted.
sample_fields: list[str], optional
List of fields to extract from the sample genotype columns. If None,
all fields will be extracted.
samples : list[str], optional
List of samples to extract. If None, all samples will be extracted.
Returns
-------
pl.DataFrame
Polars DataFrame with columns corresponding to the requested fields.
"""
with pysam.VariantFile(path) as f:
info_schema = _read_info_schema(f)
sample_schema = _read_sample_schema(f)
if info_fields is None:
info_fields = list(info_schema["name"])
if sample_fields is None:
sample_fields = list(sample_schema["name"])
if samples is None:
samples = list(f.header.samples)
records = _read_vcf_as_records(
f, query, set(info_fields), set(sample_fields), samples, include_unspecified
)
df = pl.from_records(records)
columns = (
["chrom", "pos", "id", "ref", "alts", "qual", "filters"]
+ info_fields
+ [f"{sample}.{field}" for sample in samples
for field in [f"phased", *sample_fields]]
)
columns = columns + list(set(df.columns) - set(columns))
for col in columns:
if col not in df.columns:
df = df.with_columns(pl.lit(None).alias(col))
return df.select(columns)
# return df[columns].fillna(np.nan)