def annotateGenome(iterator, fasta, options):

    ninput, noutput, nadded, nambiguous, nframeshifts, nunknown = 0, 0, 0, 0, 0, 0
    last = None
    is_ambiguous = False

    for this in iterator:
        ninput += 1

        E.debug("last=%s" % str(last))
        E.debug("this=%s" % str(this))
        E.debug("is_ambiguous=%s" % str(is_ambiguous))

        if last and last.contig == this.contig:
            # check if file is sorted correctly
            assert last.start <= this.start, "input file needs to be sorted by contig, start"
            if last.end <= this.start:
                if not is_ambiguous:
                    if last.gene_id != this.gene_id:
                        nadded += addIntergenicSegment(last,
                                                       this, fasta, options)
                    else:
                        d = this.start - last.end
                        if d >= options.min_intron_length:
                            nadded += addSegment("intronic",
                                                 last.end,
                                                 this.start,
                                                 last,
                                                 options)
                        elif d <= options.max_frameshift_length:
                            nframeshifts += addSegment("frameshift",
                                                       last.end,
                                                       this.start,
                                                       last,
                                                       options)
                        else:
                            nunknown += addSegment("unknown",
                                                   last.end,
                                                   this.start,
                                                   last,
                                                   options)
                else:
                    if last.feature == this.feature and \
                       last.gene_id == this.gene_id:
                        nambiguous += addSegment(
                            last.feature,
                            last.end, this.start,
                            last, options)
                    else:
                        nambiguous += addSegment(
                            "ambiguous",
                            last.end, this.start,
                            last, options)
                    is_ambiguous = False
                last = this
            elif last.end > this.start:
                if last.gene_id != this.gene_id:
                    # flag next region as ambiguous
                    is_ambiguous = True
                last.end = this.end
        else:
            nadded += addIntergenicSegment(last, None, fasta, options)
            nadded += addIntergenicSegment(None, this, fasta, options)
            last = this

        options.stdout.write("%s\n" % str(this))
        noutput += 1

    E.info(
        "ninput=%i, noutput=%i, nadded=%i, nambiguous=%i, nframeshifts=%i, nunknown=%i" %
        (ninput, noutput, nadded, nambiguous, nframeshifts, nunknown))
perform a full segmentation of the genome (UTR, exon, intron ...)
dataset/ETHPy150Open CGATOxford/cgat/scripts/gtf2gff.py/annotateGenome