def get_gtcount(filepath, gpm):
    """
    Read 'gtcount' SLiM output into a nested Python dictionary of genotype counts at each generation.
    Each line of 'gtcount' file consists of:
        (generation)\t(' '-separated list of occupied nodes)\t(' '-separated list of respective counts)
    
    PARAMETERS:
    -----------
    filepath (str) : path to _gtcount.txt SLiM output
    gpm (GenotypePhenotypeMap object) : gpmap used to run the simulation

    RETURNS:
    --------
    gtcount (dict) : nested dictionary of genotype counts at each generation. 
        genotype integer ID corresponds to gpmap node. 
        format = {generation: {gt0: count, gt1: count}}
    """

    f = open(filepath, 'r')
    gtcount = {}

    for line in f.readlines():
        line = line.strip().split('\t')
        gen = int(line[0])                  # generation
        gts = line[1].strip().split(' ')    # IDs of present genotypes
        cts = line[2].strip().split(' ')    # count of each
        cdict = dict(zip([int(g) for g in gts], [int(c) for c in cts]))
        gendict = {}
        for ID in range(len(gpm.data)):
            if cdict.get(ID):
                gendict.update({ID: cdict[ID]})
            else:
                gendict.update({ID: 0})
        gtcount.update({gen: gendict})
    return gtcount