Skip to content

Commit

Permalink
Use Bioregistry for URL generation
Browse files Browse the repository at this point in the history
Related to the discussion in dmis-lab#3, the Bioregistry has the logic for generating URLs given CURIEs
  • Loading branch information
cthoyt committed Jan 24, 2022
1 parent a16b9c7 commit df0e6d0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
44 changes: 25 additions & 19 deletions app/result_parser.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from typing import Optional

import bioregistry

COLOR_DICT = {
'disease': (228, 26, 28),
'mutation': (55, 126, 184),
Expand All @@ -10,23 +14,25 @@
'cell_type': (153, 153, 153)
}

def id2url(_id):
if "MESH" in _id:
t_id = _id.split(":")[1]
return "https://id.nlm.nih.gov/mesh/{}.html".format(t_id)
elif "OMIM" in _id:
t_id = _id.split(":")[1]
return "https://omim.org/entry/{}".format(t_id)
elif "EntrezGene" in _id:
t_id = _id.split(":")[1]
return "https://www.ncbi.nlm.nih.gov/gene/{}".format(t_id)
elif "CVCL" in _id:
return "https://web.expasy.org/cellosaurus/{}".format(_id)
elif "NCBI" in _id:
t_id = _id.split(":")[1]
return "https://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?id={}".format(t_id)
else:
return ""

def id2url(curie: str) -> Optional[str]:
"""Generate a URL for the given compact URI (CURIE), if possible.
:param curie: A compact URI (CURIE) in the form of `prefix:identifier`
:returns: A URL string if the Bioregistry can construct one, otherwise None.
>>> id2url("MESH:C063233")
'https://meshb.nlm.nih.gov/record/ui?ui=C063233'
>>> id2url("NCBI:txid10095")
'https://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?mode=Info&id=10095'
"""
if curie.startswith("NCBI:txid"):
# If the CURIE is using non-standard NCBI taxonomy identifiers,
# do a small amount of string pre-processing to fix the prefix
curie = "NCBITaxon:" + curie[len("NCBI:txid"):]
return bioregistry.get_iri(curie)


class Denotation:
def __init__(self, obj_id=None, point=None, offset=None, key=None, info=None, type=None, mention=None):
Expand Down Expand Up @@ -108,15 +114,15 @@ def id2anchor(self):
if self.key == "mutation":
_url = id2url(self.info['normalizedName'])

if _url == "":
if _url is None:
anchor_text = "{}{}".format(self.info['normalizedName'], self.mark)
else:
anchor_text = "<a href='{}' target='_blank'>{}</a>{}".format(_url, self.info['normalizedName'], self.mark)
else:
anchor_texts = []
for _id in self.ids:
_url = id2url(_id)
if _url == "":
if _url is None:
_a_text = _id
else:
_a_text = "<a href='{}' target='_blank'>{}</a>".format(_url, _id)
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ transformers==4.9.0
xmltodict==0.12.0
accelerate==0.3.0
pymongo
Flask
Flask
bioregistry

0 comments on commit df0e6d0

Please sign in to comment.