diff --git a/src/pyobo/sources/mesh.py b/src/pyobo/sources/mesh.py index 169a05dc..ba10125c 100644 --- a/src/pyobo/sources/mesh.py +++ b/src/pyobo/sources/mesh.py @@ -6,7 +6,7 @@ import itertools as itt import logging import re -from typing import Any, Dict, Iterable, List, Mapping, Optional, Set, Tuple +from typing import Any, Collection, Dict, Iterable, List, Mapping, Optional, Set, Tuple from xml.etree.ElementTree import Element from tqdm.auto import tqdm @@ -317,5 +317,32 @@ def _get_descriptor_qualifiers(descriptor: Element) -> List[Mapping[str, str]]: ] +def get_mesh_category_curies(letter: str, skip: Optional[Collection[str]] = None) -> List[str]: + """Get the MeSH LUIDs for a category, by letter (e.g., "A"). + + :param letter: The MeSH tree, A for anatomy, C for disease, etc. + :param skip: An optional collection of MeSH tree codes to skip, such as "A03" + :returns: A list of MeSH CURIE strings for the top level of each MeSH tree. + + .. seealso:: https://meshb.nlm.nih.gov/treeView + """ + import bioversions + + mesh_version = bioversions.get_version("mesh") + if mesh_version is None: + raise ValueError + tree_to_mesh = get_tree_to_mesh_id(mesh_version) + rv = [] + for i in range(1, 100): + key = f"{letter}{i:02}" + if skip and key in skip: + continue + mesh_id = tree_to_mesh.get(key) + if mesh_id is None: + break + rv.append(f"mesh:{mesh_id}") + return rv + + if __name__ == "__main__": get_obo(force=True).write_default(force=True, write_obo=True)