def translate(args):
    """
    %prog translate cdsfasta

    Translate CDS to proteins. The tricky thing is that sometimes the CDS
    represents a partial gene, therefore disrupting the frame of the protein.
    Check all three frames to get a valid translation.
    """
    transl_tables = [str(x) for x in xrange(1,25)]
    p = OptionParser(translate.__doc__)
    p.add_option("--ids", default=False, action="store_true",
                 help="Create .ids file with the complete/partial/gaps "
                      "label [default: %default]")
    p.add_option("--longest", default=False, action="store_true",
                 help="Find the longest ORF from each input CDS [default: %default]")
    p.add_option("--table", default=1, choices=transl_tables,
            help="Specify translation table to use [default: %default]")
    p.set_outfile()

    opts, args = p.parse_args(args)

    if len(args) != 1:
        sys.exit(not p.print_help())

    cdsfasta, = args
    if opts.longest:
        cdsfasta = longestorf([cdsfasta])

    f = Fasta(cdsfasta, lazy=True)
    outfile = opts.outfile
    fw = must_open(outfile, "w")

    if opts.ids:
        idsfile = cdsfasta.rsplit(".", 1)[0] + ".ids"
        ids = open(idsfile, "w")
    else:
        ids = None

    five_prime_missing = three_prime_missing = 0
    contain_ns = complete = cannot_translate = total = 0

    for name, rec in f.iteritems_ordered():
        cds = rec.seq
        cdslen = len(cds)
        peplen = cdslen / 3
        total += 1

        # Try all three frames
        pep = ""
        for i in xrange(3):
            newcds = cds[i: i + peplen * 3]
            newpep = newcds.translate(table=opts.table)
            if len(newpep.split("*")[0]) > len(pep.split("*")[0]):
                pep = newpep

        labels = []
        if pep.rstrip("*") in "*":
            logging.error("{0} cannot translate".format(name))
            cannot_translate += 1
            labels.append("cannot_translate")

        contains_start = pep.startswith("M")
        contains_stop = pep.endswith("*")
        contains_ns = "X" in pep
        start_ns = pep.startswith("X")
        end_ns = pep.endswith("X")

        if not contains_start:
            five_prime_missing += 1
            labels.append("five_prime_missing")
        if not contains_stop:
            three_prime_missing += 1
            labels.append("three_prime_missing")
        if contains_ns:
            contain_ns += 1
            labels.append("contain_ns")
        if contains_start and contains_stop:
            complete += 1
            labels.append("complete")
        if start_ns:
            labels.append("start_ns")
        if end_ns:
            labels.append("end_ns")

        if ids:
            print >> ids, "\t".join((name, ",".join(labels)))

        peprec = SeqRecord(pep, id=name, description=rec.description)
        SeqIO.write([peprec], fw, "fasta")
        fw.flush()

    print >> sys.stderr, "Complete gene models: {0}".\
                        format(percentage(complete, total))
    print >> sys.stderr, "Missing 5`-end: {0}".\
                        format(percentage(five_prime_missing, total))
    print >> sys.stderr, "Missing 3`-end: {0}".\
                        format(percentage(three_prime_missing, total))
    print >> sys.stderr, "Contain Ns: {0}".\
                        format(percentage(contain_ns, total))

    if cannot_translate:
        print >> sys.stderr, "Cannot translate: {0}".\
                        format(percentage(cannot_translate, total))

    fw.close()

    return cdsfasta, outfile
dataset/ETHPy150Open tanghaibao/jcvi/formats/fasta.py/translate/swapped operands of `in`