import collections


def process_rels(mrrel_file, ro_only=True):
    """Reads UMLS relation triples file MRREL.2019.RRF.

    Use ``ro_only`` to consider relations of "RO" semantic type only.
    50813206 lines in UMLS2019.

    RO = has relationship other than synonymous, narrower, or broader

    For details on each column, please check:
    https://www.ncbi.nlm.nih.gov/books/NBK9685/table/ch03.T.related_concepts_file_mrrel_rrf/?report=objectonly

    """
    relation_text_to_groups = collections.defaultdict(set)
    with open(mrrel_file) as rf:
        for line in rf:
            line = line.strip()
            if not line:
                continue

            # Each line is as such: C0012792|A24166664|SCUI|RO|C0026827|A0088733|SCUI|induced_by|R176819430||MED-RT|MED-RT||N|N||
            line = line.split("|")

            # Consider relations of 'RO' type only
            if line[3] != "RO" and ro_only:
                continue

            e1_id = line[0]
            e2_id = line[4]
            rel_text = line[7].strip()

            # considering relations with textual descriptions only
            if not rel_text:
                continue

            relation_text_to_groups[rel_text].add((e1_id, e2_id))
    return relation_text_to_groups