from typing import Dict
from typing import Set


def gmt_to_dict(gmt_fp: str) -> Dict[str, Set[str]]:
    """Read GMT format, return dict: geneset name -> set of gene identifiers"""
    # Read GMT: get a list of geneset names, and a list of the genes in each geneset
    with open(gmt_fp) as fin:
        geneset_lines = fin.readlines()
    # GMT format: gene set name | optional description | gene1 | gene2 | gene 3
    # tab-separated
    # variable number of columns due variable gene set length
    # For each geneset, get the name and all contained genes as set
    geneset_names = [line.split("\t")[0] for line in geneset_lines]
    geneset_sets = [set(line.rstrip().split("\t")[2:]) for line in geneset_lines]
    genesets = dict(zip(geneset_names, geneset_sets))
    return genesets