-
Notifications
You must be signed in to change notification settings - Fork 26
/
gnomad.py
212 lines (172 loc) · 6.19 KB
/
gnomad.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
from gnomad.resources.resource_utils import (
DataException,
TableResource,
VersionedTableResource,
)
DATA_TYPES = ["exomes", "genomes"]
CURRENT_EXOME_RELEASE = "2.1.1"
CURRENT_GENOME_RELEASE = "2.1.1"
EXOME_RELEASES = ["2.1", "2.1.1"]
GENOME_RELEASES = ["2.1", "2.1.1"]
SUBPOPS = {
"NFE": ["BGR", "EST", "NWE", "SEU", "SWE", "ONF"],
"EAS": ["KOR", "JPN", "OEA"],
}
GENOME_POPS = ["AFR", "AMR", "ASJ", "EAS", "FIN", "NFE", "OTH"]
EXOME_POPS = ["AFR", "AMR", "ASJ", "EAS", "FIN", "NFE", "OTH", "SAS"]
EXAC_POPS = ["AFR", "AMR", "EAS", "FIN", "NFE", "OTH", "SAS"]
POP_NAMES = {
"oth": "Other",
"afr": "African-American/African",
"ami": "Amish",
"amr": "Latino",
"eas": "East Asian",
"fin": "Finnish",
"eur": "European",
"nfe": "Non-Finnish European",
"sas": "South Asian",
"mde": "Middle Eastern",
"asj": "Ashkenazi Jewish",
"uniform": "Uniform",
"sas_non_consang": "South Asian (F < 0.05)",
"consanguineous": "South Asian (F > 0.05)",
"exac": "ExAC",
"bgr": "Bulgarian (Eastern European)",
"deu": "German",
"est": "Estonian",
"esp": "Spanish",
"gbr": "British",
"nwe": "North-Western European",
"seu": "Southern European",
"ita": "Italian",
"swe": "Swedish",
"chn": "Chinese",
"kor": "Korean",
"hkg": "Hong Kong",
"sgp": "Singaporean",
"twn": "Taiwanese",
"jpn": "Japanese",
"oea": "Other East Asian",
"oeu": "Other European",
"onf": "Other Non-Finnish European",
"unk": "Unknown",
}
def _public_release_ht_path(data_type: str, version: str) -> str:
"""
Get public release table path
:param data_type: One of "exomes" or "genomes"
:param version: One of the release versions of gnomAD on GRCh37
:return: Path to release Table
"""
return f"gs://gnomad-public-requester-pays/release/{version}/ht/{data_type}/gnomad.{data_type}.r{version}.sites.ht"
def _public_coverage_ht_path(data_type: str, version: str) -> str:
"""
Get public coverage hail table
:param data_type: One of "exomes" or "genomes"
:param version: One of the release versions of gnomAD on GRCh37
:return: path to coverage Table
"""
return f"gs://gnomad-public-requester-pays/release/{version}/coverage/{data_type}/gnomad.{data_type}.r{version}.coverage.ht"
def _public_pca_ht_path(subpop: str) -> str:
"""
Get public pca loadings path
:param subpop: Can be empty ("") -> global, "eas" or "nfe"
:return: Path to release Table
"""
subpop = f".{subpop}" if subpop else ""
return f"gs://gnomad-public/release/2.1/pca/gnomad.r2.1.pca_loadings{subpop}.ht"
def _liftover_data_path(data_type: str, version: str) -> str:
"""
Paths to liftover gnomAD Table
:param data_type: One of `exomes` or `genomes`
:param version: One of the release versions of gnomAD on GRCh37
:return: Path to chosen Table
"""
return f"gs://gnomad-public-requester-pays/release/{version}/liftover_grch38/ht/{data_type}/gnomad.{data_type}.r{version}.sites.liftover_grch38.ht"
def public_release(data_type: str) -> VersionedTableResource:
"""
Retrieves publicly released versioned table resource
:param data_type: One of "exomes" or "genomes"
:return: Release Table
"""
if data_type not in DATA_TYPES:
raise DataException(f"{data_type} not in {DATA_TYPES}")
if data_type == "exomes":
current_release = CURRENT_EXOME_RELEASE
releases = EXOME_RELEASES
else:
current_release = CURRENT_GENOME_RELEASE
releases = GENOME_RELEASES
return VersionedTableResource(
current_release,
{
release: TableResource(path=_public_release_ht_path(data_type, release))
for release in releases
},
)
def coverage(data_type: str) -> VersionedTableResource:
"""
Retrieves gnomAD's coverage table by data_type
:param data_type: One of "exomes" or "genomes"
:return: Coverage Table
"""
if data_type not in DATA_TYPES:
raise DataException(f"{data_type} not in {DATA_TYPES}")
if data_type == "exomes":
current_release = "2.1"
releases = EXOME_RELEASES
releases.remove("2.1.1")
else:
current_release = CURRENT_GENOME_RELEASE
releases = GENOME_RELEASES
return VersionedTableResource(
current_release,
{
release: TableResource(path=_public_coverage_ht_path(data_type, release))
for release in releases
},
)
def liftover(data_type: str) -> VersionedTableResource:
"""
Get the 38 liftover of gnomad v2.1.1
:param data_type: One of "exomes" or "genomes"
:return: Release Table
"""
if data_type not in DATA_TYPES:
raise DataException(f"{data_type} not in {DATA_TYPES}")
if data_type == "exomes":
current_release = CURRENT_EXOME_RELEASE
releases = EXOME_RELEASES
releases.remove("2.1")
else:
current_release = CURRENT_GENOME_RELEASE
releases = GENOME_RELEASES
return VersionedTableResource(
current_release,
{
release: TableResource(path=_liftover_data_path(data_type, release))
for release in releases
},
)
def public_pca_loadings(subpop: str = "") -> TableResource:
"""
Returns the TableResource containing sites and loadings from population PCA
:param subpop: Can be empty ("") -> global, "eas" or "nfe"
:return: gnomAD public PCA loadings TableResource
"""
if subpop not in ["", "eas", "nfe"]:
raise DataException(
'Available subpops are "eas" or "nfe", default value "" for global'
)
return TableResource(path=_public_pca_ht_path(subpop))
def release_vcf_path(data_type: str, version: str, contig: str) -> str:
"""
Publically released VCF. Provide specific contig, i.e. "20", to retrieve contig
specific VCF
:param data_type: One of "exomes" or "genomes"
:param version: One of the release versions of gnomAD on GRCh37
:param contig: Single contig "1" to "Y"
:return: Path to VCF
"""
contig = f".{contig}" if contig else ""
return f"gs://gnomad-public/release/{version}/vcf/{data_type}/gnomad.{data_type}.r{version}.sites{contig}.vcf.bgz"