-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_hgnc.py
47 lines (44 loc) · 1.34 KB
/
search_hgnc.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
from http_get import (
http_get,
) # uses https://gist.github.com/dmyersturnbull/fade1a5901beeb1003680f8267454640
from typing import Mapping, Union, Iterable
import json
searchable_fields = {
"alias_name",
"alias_symbol",
"ccds_id",
"ena",
"ensemble_gene_id",
"entrez_id",
"hgnc_id",
"locus_group",
"locus_type",
"mgd_id",
"name",
"prev_name",
"prev_symbol",
"refseq_accession",
"rgd_id",
"status",
"symbol",
"ucsc_id",
"uniprot_ids",
"vega_id",
}
def search_hgnc(fields: Union[Mapping[str, str], str]) -> Iterable[str]:
"""Searches HGNC and returns a list of HGNC symbol IDs.
fields is either:
A) A map of field names (contained in searchable_fields) to their values
B) A string for the name field; i.e. search_hgnc('abc') === search_hgnc({'name': 'abc'})
"""
if isinstance(fields, str):
fields = {"name": fields}
for key in fields.keys():
if key not in searchable_fields:
raise ValueError("'{}' is not a searchable field".format(key))
query = "http://rest.genenames.org/search/" + "+AND+".join(
[key + ":" + value for key, value in fields.items()]
)
text = http_get(query, {"Accept": "application/json"})
results = json.loads(text)
return [x["symbol"] for x in results["response"]["docs"]]