import collections


def process_types(mrsty_file):
    """Reads UMLS semantic types file MRSTY.2019.RRF.
    For details on each column, please check: https://www.ncbi.nlm.nih.gov/books/NBK9685/
    """
    cui_to_entity_types = collections.defaultdict(set)
    with open(mrsty_file) as rf:
        for line in rf:
            line = line.strip()
            if not line:
                continue

            # Each line is as such:
            # 'C0000005|T116|A1.4.1.2.1.7|Amino Acid, Peptide, or Protein|AT17648347|256|
            # CUI|TUI|STN|STY|ATUI|CVF
            # Unique identifier of concept|Unique identifier of Semantic Type|Semantic Type tree number|Semantic Type. The valid values are defined in the Semantic Network.|Unique identifier for attribute|Content View Flag
            line = line.split("|")

            e_id = line[0]
            e_type = line[3].strip()

            # considering entities with entity types only
            if not e_type:
                continue

            cui_to_entity_types[e_id].add(e_type)

    return cui_to_entity_types