def plot(args):
    """
    %prog plot tagged.new.bed chr1

    Plot gene identifiers along a particular chromosome, often to illustrate the
    gene id assignment procedure.
    """
    from jcvi.graphics.base import plt, savefig
    from jcvi.graphics.chromosome import ChromosomeMap

    p = OptionParser(plot.__doc__)
    p.add_option("--firstn", type="int", help="Only plot the first N genes")
    p.add_option("--ymax", type="int", help="Y-axis max value")
    p.add_option("--log", action="store_true",
                help="Write plotting data [default: %default]")
    opts, args, iopts = p.set_image_options(args, figsize="6x4")

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

    taggedbed, chr = args
    bed = Bed(taggedbed)
    beds = list(bed.sub_bed(chr))
    old, new = [], []
    i = 0
    for b in beds:
        accn = b.extra[0]
        if "te" in accn:
            continue

        accn, tag = accn.split("|")
        if tag == "OVERLAP":
            continue

        c, r = atg_name(accn)
        if tag == "NEW":
            new.append((i, r))
        else:
            old.append((i, r))
        i += 1

    ngenes = i
    assert ngenes == len(new) + len(old)

    logging.debug("Imported {0} ranks on {1}.".format(ngenes, chr))
    fig = plt.figure(1, (iopts.w, iopts.h))
    root = fig.add_axes([0, 0, 1, 1])

    xstart, xend = .2, .8
    ystart, yend = .2, .8
    pad = .02

    ngenes = opts.firstn or ngenes
    ymax = opts.ymax or 500000

    title = "Assignment of Medtr identifiers"
    if opts.ymax:
        subtitle = "{0}, first {1} genes".format(chr, ngenes)
    else:
        subtitle = "{0}, {1} genes ({2} new)".format(chr, ngenes, len(new))

    chr_map = ChromosomeMap(fig, root, xstart, xend, ystart, yend, pad, 0,
                        ymax, 5, title, subtitle)

    ax = chr_map.axes

    if opts.log:
        from jcvi.utils.table import write_csv
        header = ["x", "y"]
        write_csv(header, new, filename=chr + ".new")
        write_csv(header, old, filename=chr + ".old")

    x, y = zip(*new)
    ax.plot(x, y, "b,")
    x, y = zip(*old)
    ax.plot(x, y, "r,")

    # Legends
    ymid = (ystart + yend) / 2
    y = ymid + pad
    root.plot([.2], [y], "r.", lw=2)
    root.text(.2 + pad, y, "Existing Medtr ids", va="center", size=10)
    y = ymid - pad
    root.plot([.2], [y], "b.", lw=2)
    root.text(.2 + pad, y, "Newly instantiated ids", va="center", size=10)

    ax.set_xlim(0, ngenes)
    ax.set_ylim(0, ymax)
    ax.set_axis_off()

    root.set_xlim(0, 1)
    root.set_ylim(0, 1)
    root.set_axis_off()

    image_name = chr + ".identifiers." + iopts.format
    savefig(image_name, dpi=iopts.dpi, iopts=iopts)
def plot ( args ) : from jcvi . graphics . base import plt , savefig from jcvi . graphics . chromosome import ChromosomeMap p = OptionParser ( plot . __doc__ ) p . add_option ( "--firstn" , type = "int" , help = "Only plot the first N genes" ) p . add_option ( "--ymax" , type = "int" , help = "Y-axis max value" ) p . add_option ( "--log" , action = "store_true" , help = "Write plotting data [default: %default]" ) opts , args , iopts = p . set_image_options ( args , figsize = "6x4" ) if len ( args ) != 2 : sys . exit ( not p . print_help ( ) ) taggedbed , chr = args bed = Bed ( taggedbed ) beds = list ( bed . sub_bed ( chr ) ) old , new = [ ] , [ ] i = 0 for b in beds : accn = b . extra [ 0 ] if "te" in accn : continue accn , tag = accn . split ( "|" ) if tag == "OVERLAP" : continue c , r = atg_name ( accn ) if tag == "NEW" : new . append ( ( i , r ) ) else : old . append ( ( i , r ) ) i += 1 ngenes = i assert ngenes == len ( new ) + len ( old ) logging . debug ( "Imported {0} ranks on {1}." . format ( ngenes , chr ) ) fig = plt . figure ( 1 , ( iopts . w , iopts . h ) ) root = fig . add_axes ( [ 0 , 0 , 1 , 1 ] ) xstart , xend = .2 , .8 ystart , yend = .2 , .8 pad = .02 ngenes = opts . firstn or ngenes ymax = opts . ymax or 500000 title = "Assignment of Medtr identifiers" if opts . ymax : subtitle = "{0}, first {1} genes" . format ( chr , ngenes ) else : subtitle = "{0}, {1} genes ({2} new)" . format ( chr , ngenes , len ( new ) ) chr_map = ChromosomeMap ( fig , root , xstart , xend , ystart , yend , pad , 0 , ymax , 5 , title , subtitle ) ax = chr_map . axes if opts . log : from jcvi . utils . table import write_csv header = [ "x" , "y" ] write_csv ( header , new , filename = chr + ".new" ) write_csv ( header , old , filename = chr + ".old" ) x , y = zip ( * new ) ax . plot ( x , y , "b," ) x , y = zip ( * old ) ax . plot ( x , y , "r," ) ymid = ( ystart + yend ) / 2 y = ymid + pad root . plot ( [ .2 ] , [ y ] , "r." , lw = 2 ) root . text ( .2 + pad , y , "Existing Medtr ids" , va = "center" , size = 10 ) y = ymid - pad root . plot ( [ .2 ] , [ y ] , "b." , lw = 2 ) root . text ( .2 + pad , y , "Newly instantiated ids" , va = "center" , size = 10 ) ax . set_xlim ( 0 , ngenes ) ax . set_ylim ( 0 , ymax ) ax . set_axis_off ( ) root . set_xlim ( 0 , 1 ) root . set_ylim ( 0 , 1 ) root . set_axis_off ( ) image_name = chr + ".identifiers." + iopts . format savefig ( image_name , dpi = iopts . dpi , iopts = iopts )
%prog plot tagged.new.bed chr1

    Plot gene identifiers along a particular chromosome, often to illustrate the
    gene id assignment procedure.