|
| 1 | +"""Get ICONCLASS as OBO.""" |
| 2 | + |
| 3 | +from collections.abc import Iterable |
| 4 | +from urllib.parse import quote |
| 5 | + |
| 6 | +import pandas as pd |
| 7 | + |
| 8 | +from pyobo.struct import Obo, Term |
| 9 | +from pyobo.utils.path import ensure_df |
| 10 | + |
| 11 | +__all__ = [ |
| 12 | + "IconclassGetter", |
| 13 | +] |
| 14 | + |
| 15 | +PREFIX = "iconclass" |
| 16 | +BASE_URL = "https://github.com/iconclass/data/raw/refs/heads/main/txt/en/txt_en_{}.txt" |
| 17 | +URLS = [ |
| 18 | + BASE_URL.format("0_1"), |
| 19 | + BASE_URL.format("2_3"), |
| 20 | + BASE_URL.format("4"), |
| 21 | + BASE_URL.format("5_6_7_8"), |
| 22 | + BASE_URL.format("9"), |
| 23 | + BASE_URL.format("keys"), |
| 24 | + # shakespeare |
| 25 | +] |
| 26 | + |
| 27 | + |
| 28 | +def get_df() -> pd.DataFrame: |
| 29 | + """Get an ICONCLASS terms dataframe.""" |
| 30 | + df = pd.concat( |
| 31 | + ensure_df(prefix=PREFIX, url=url, sep="|", names=["luid", "name"]) for url in URLS |
| 32 | + ) |
| 33 | + return df |
| 34 | + |
| 35 | + |
| 36 | +def iter_terms() -> Iterable[Term]: |
| 37 | + """Iterate over terms in ICONCLASS.""" |
| 38 | + for luid, name in get_df().values: |
| 39 | + yv = Term.from_triple(prefix=PREFIX, identifier=quote(luid), name=name) |
| 40 | + yield yv |
| 41 | + |
| 42 | + |
| 43 | +class IconclassGetter(Obo): |
| 44 | + """An ontology representation of ICONCLASS.""" |
| 45 | + |
| 46 | + ontology = PREFIX |
| 47 | + dynamic_version = True |
| 48 | + |
| 49 | + def iter_terms(self, force: bool = False) -> Iterable[Term]: |
| 50 | + """Iterate over terms in the ontology.""" |
| 51 | + return iter_terms() |
| 52 | + |
| 53 | + |
| 54 | +if __name__ == "__main__": |
| 55 | + IconclassGetter.cli() |
0 commit comments