Source code for cardinal_pythonlib.tools.convert_athena_ohdsi_codes

#!/usr/bin/env python
# cardinal_pythonlib/tools/convert_athena_ohdsi_codes.py

r"""
===============================================================================

    Original code copyright (C) 2009-2022 Rudolf Cardinal (rudolf@pobox.com).

    This file is part of cardinal_pythonlib.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        https://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

===============================================================================

Convert SNOMED-CT codes, and their children, to OPCS4 procedure codes, given
an appropriate SNOMED-and-OPCS download from Athena OHDSI; see See
https://athena.ohdsi.org/. More generally, does this for any pair of
vocabularies.

Example:

.. code-block:: bash

    cardinalpythonlib_convert_athena_ohdsi_codes 175898006 118677009 265764009 --src_vocabulary SNOMED --descendants --dest_vocabulary OPCS4 > renal_procedures_opcs4.txt
    # ... kidney operation, procedure on urinary system, renal dialysis

"""  # noqa

import argparse
import logging
import os
from typing import Iterable, List, Set

from cardinal_pythonlib.athena_ohdsi import (
    AthenaConceptRow,
    AthenaRelationshipId,
    AthenaVocabularyId,
    get_athena_concepts,
    get_athena_concept_relationships,
)
from cardinal_pythonlib.logs import main_only_quicksetup_rootlogger

log = logging.getLogger()

DEFAULT_CONCEPT = os.path.join(os.getcwd(), "CONCEPT.csv")
DEFAULT_CONCEPT_RELATIONSHIP = os.path.join(
    os.getcwd(), "CONCEPT_RELATIONSHIP.csv"
)


def report(concepts: Iterable[AthenaConceptRow]) -> str:
    descriptions = [str(c) for c in concepts]
    return "\n".join(sorted(descriptions))





[docs]def main() -> None: """ Command-line entry point. """ parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter ) parser.add_argument( "source_codes", nargs="+", type=int, help="Source codes to look up " "(along with their descendants if --descendants is specified)", ) parser.add_argument( "--descendants", action="store_true", help="Include descendants of the codes specified", ) parser.add_argument( "--concept", type=str, default=DEFAULT_CONCEPT, help="Athena OHDSI CONCEPT.csv TSV file including the source and " "destination vocabularies", ) parser.add_argument( "--concept_relationship", type=str, default=DEFAULT_CONCEPT_RELATIONSHIP, help="Athena OHDSI CONCEPT_RELATIONSHIP.csv TSV file " "including the source and destination vocabularies", ) parser.add_argument( "--src_vocabulary", type=str, default=AthenaVocabularyId.SNOMED, help="Source vocabulary", ) parser.add_argument( "--dest_vocabulary", type=str, default=AthenaVocabularyId.OPCS4, help="Destination vocabulary", ) args = parser.parse_args() main_only_quicksetup_rootlogger() print_equivalent_opcs_codes( source_codes=args.source_codes, source_vocabulary=args.src_vocabulary, destination_vocabulary=args.dest_vocabulary, concept_file=args.concept, concept_relationship_file=args.concept_relationship, with_descendants=args.descendants, )
if __name__ == "__main__": main()