def get_chrom_sizes(fai_file, exclude_scaffolds=True):
    """
    Get the names and sizes of all the chromosomes in a reference by iterating over the index.
    """
    chroms = {}

    with fai_file.open('r') as fin:
        for line in fin:
            # get the chrom name and size
            contig, size, _, _, _ = line.split()

            # strip any annoying `chr` prefix
            nochr = contig.replace('chr', '')

            # is this contig an actual chromosome
            is_chrom = nochr.isdigit() or nochr in ['X', 'Y', 'Z', 'W', 'MT']

            if exclude_scaffolds and not is_chrom:
                continue

            chroms[nochr] = int(size)

    return chroms