def iterator_transcripts2genes(gtf_iterator, min_overlap=0):
    """cluster transcripts by exon overlap.

    The gene id is set to the first transcript encountered of a gene.
    If a gene stretches over several contigs, subsequent copies are
    appended a number.
    """

    map_transcript2gene = {}
    gene_ids = collections.defaultdict(list)

    for chunk in iterator_overlaps(gtf_iterator):
        transcript_ids = list(set([x.transcript_id for x in chunk]))
        contig = chunk[0].contig

        # have any of these already encountered?
        for x in transcript_ids:
            if x in map_transcript2gene:
                gene_id = map_transcript2gene[x]
                break
        else:
            # arbitrarily pick one
            gene_id = transcript_ids[0]

        if gene_id not in gene_ids:
            gene_ids[gene_id].append(contig)
            index = 0
        else:
            try:
                index = gene_ids[gene_id].index(contig)
            except __HOLE__:
                index = len(gene_ids[gene_id])
                gene_ids[gene_id].append(contig)

        for x in transcript_ids:
            map_transcript2gene[x] = gene_id

        if index:
            gene_id += ".%i" % index
        for x in chunk:
            x.gene_id = gene_id
        yield chunk
dataset/ETHPy150Open CGATOxford/cgat/CGAT/GTF.py/iterator_transcripts2genes