Skip to content

Commit

Permalink
Add UMLS semantic types
Browse files Browse the repository at this point in the history
  • Loading branch information
cthoyt committed Apr 2, 2024
1 parent 3f9cc0b commit 1ff911a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ install_requires =
# Resource Downloaders
drugbank_downloader
chembl_downloader
umls_downloader>=0.1.2
umls_downloader>=0.1.3
typing_extensions

# Random options
Expand Down
20 changes: 18 additions & 2 deletions src/pyobo/sources/umls/umls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@

import itertools as itt
import operator
from typing import Iterable
from collections import defaultdict
from typing import Iterable, Mapping, Set

import bioregistry
import pandas as pd
from tqdm import tqdm
from tqdm.auto import tqdm
from umls_downloader import open_umls
from umls_downloader import open_umls, open_umls_semantic_types

from pyobo import Obo, Reference, Synonym, SynonymTypeDef, Term

Expand Down Expand Up @@ -66,8 +68,20 @@ def get_obo() -> Obo:
return UMLSGetter()


def get_semantic_types() -> Mapping[str, Set[str]]:
"""Get UMLS"""
dd = defaultdict(set)
with open_umls_semantic_types() as file:
for line in tqdm(file, unit_scale=True):
cui, sty, _ = line.decode("utf8").split("|", 2)
dd[cui].add(sty)
return dict(dd)


def iter_terms(version: str) -> Iterable[Term]:
"""Iterate over UMLS terms."""
semantic_types = get_semantic_types()

with open_umls(version=version) as file:
it = tqdm(file, unit_scale=True, desc="[umls] parsing")
lines = (line.decode("utf-8").strip().split("|") for line in it)
Expand Down Expand Up @@ -118,6 +132,8 @@ def iter_terms(version: str) -> Iterable[Term]:
synonyms=synonyms,
xrefs=xrefs,
)
for sty_id in semantic_types.get(cui, set()):
term.append_parent(Reference(prefix="sty", identifier=sty_id))
yield term


Expand Down

0 comments on commit 1ff911a

Please sign in to comment.