def main(): p = OptionParser(__doc__) p.add_option( "--customfont", default="Airswing.ttf", choices=available_fonts, help="Custom font name", ) p.add_option("--color", default="limegreen", help="Font color") p.add_option("--size", default=36, type="int", help="Font size") opts, args, iopts = p.set_image_options(figsize="2x1", dpi=60, format="png") if len(args) != 1: sys.exit(not p.print_help()) (text,) = args plt.rcdefaults() fig = plt.figure(1, (iopts.w, iopts.h)) ax = fig.add_axes([0, 0, 1, 1]) ax.text(0.5, 0.5, text, color=opts.color, ha="center", va="center") fontprop(ax, opts.customfont, size=opts.size) ax.set_xlim(0, 1) ax.set_ylim(0, 1) ax.set_axis_off() image_name = text + "." + iopts.format savefig(image_name, dpi=iopts.dpi, iopts=iopts)
def main(tx=None): """ %prog newicktree Plot Newick formatted tree. The gene structure can be plotted along if --gffdir is given. The gff file needs to be `genename.gff`. If --sizes is on, also show the number of amino acids. """ p = OptionParser(main.__doc__) p.add_option("--outgroup", help="Root the tree using the outgroup. " + \ "Use comma to separate multiple taxa.") p.add_option("--rmargin", default=.3, type="float", help="Set blank rmargin to the right [default: %default]") p.add_option( "--gffdir", default=None, help="The directory that contain GFF files [default: %default]") p.add_option("--sizes", default=None, help="The FASTA file or the sizes file [default: %default]") opts, args, iopts = set_image_options(p, figsize="8x6") if len(args) != 1: sys.exit(not p.print_help()) datafile, = args outgroup = None if opts.outgroup: outgroup = opts.outgroup.split(",") pf = datafile.rsplit(".", 1)[0] if tx: pf = "demo" else: tx = open(datafile).read() logging.debug("Load tree file `{0}`.".format(datafile)) fig = plt.figure(1, (iopts.w, iopts.h)) root = fig.add_axes([0, 0, 1, 1]) draw_tree(root, tx, rmargin=opts.rmargin, outgroup=outgroup, gffdir=opts.gffdir, sizes=opts.sizes) root.set_xlim(0, 1) root.set_ylim(0, 1) root.set_axis_off() image_name = pf + "." + iopts.format logging.debug("Print image to `{0}` {1}".format(image_name, iopts)) plt.savefig(image_name, dpi=iopts.dpi) plt.rcdefaults()
def main(tx=None): """ %prog newicktree Plot Newick formatted tree. The gene structure can be plotted along if --gffdir is given. The gff file needs to be `genename.gff`. If --sizes is on, also show the number of amino acids. """ p = OptionParser(main.__doc__) p.add_option("--outgroup", help="Root the tree using the outgroup. " + \ "Use comma to separate multiple taxa.") p.add_option("--rmargin", default=.3, type="float", help="Set blank rmargin to the right [default: %default]") p.add_option("--gffdir", default=None, help="The directory that contain GFF files [default: %default]") p.add_option("--sizes", default=None, help="The FASTA file or the sizes file [default: %default]") opts, args, iopts = set_image_options(p, figsize="8x6") if len(args) != 1: sys.exit(not p.print_help()) datafile, = args outgroup = None if opts.outgroup: outgroup = opts.outgroup.split(",") pf = datafile.rsplit(".", 1)[0] if tx: pf = "demo" else: tx = open(datafile).read() logging.debug("Load tree file `{0}`.".format(datafile)) fig = plt.figure(1, (iopts.w, iopts.h)) root = fig.add_axes([0, 0, 1, 1]) draw_tree(root, tx, rmargin=opts.rmargin, outgroup=outgroup, gffdir=opts.gffdir, sizes=opts.sizes) root.set_xlim(0, 1) root.set_ylim(0, 1) root.set_axis_off() image_name = pf + "." + iopts.format logging.debug("Print image to `{0}` {1}".format(image_name, iopts)) plt.savefig(image_name, dpi=iopts.dpi) plt.rcdefaults()
width = iopts.w height = iopts.h * ratio fig = plt.figure(1, (width, height)) root = fig.add_axes([0, 0, 1, 1]) # the whole canvas ax = fig.add_axes([.1, .1, .8, .8]) # the dot plot blastplot(ax, blastfile, qsizes, ssizes, qbed, sbed, style=opts.style, proportional=proportional, sampleN=opts.sample, baseticks=True, stripNames=opts.stripNames) # add genome names to_ax_label = lambda fname: _(op.basename(fname).split(".")[0]) gx, gy = [to_ax_label(x.filename) for x in (qsizes, ssizes)] ax.set_xlabel(gx, size=16) ax.set_ylabel(gy, size=16) root.set_xlim(0, 1) root.set_ylim(0, 1) root.set_axis_off() logging.debug("Print image to `{0}` {1}".format(image_name, iopts)) plt.savefig(image_name, dpi=iopts.dpi) plt.rcdefaults()
def heatmap(args): """ %prog heatmap fastafile chr1 Combine stack plot with heatmap to show abundance of various tracks along given chromosome. Need to give multiple beds to --stacks and --heatmaps """ p = OptionParser(heatmap.__doc__) p.add_option("--stacks", default="Exons,Introns,DNA_transposons,Retrotransposons", help="Features to plot in stackplot [default: %default]") p.add_option("--heatmaps", default="Copia,Gypsy,hAT,Helitron,Introns,Exons", help="Features to plot in heatmaps [default: %default]") p.add_option("--meres", default=None, help="Extra centromere / telomere features [default: %default]") add_window_options(p) opts, args, iopts = set_image_options(p, args, figsize="8x5") if len(args) != 2: sys.exit(not p.print_help()) fastafile, chr = args window, shift = check_window_options(opts) stacks = opts.stacks.split(",") heatmaps = opts.heatmaps.split(",") stackbeds = [x + ".bed" for x in stacks] heatmapbeds = [x + ".bed" for x in heatmaps] stackbins = get_binfiles(stackbeds, fastafile, shift) heatmapbins = get_binfiles(heatmapbeds, fastafile, shift) window, shift = check_window_options(opts) margin = .06 inner = .015 clen = Sizes(fastafile).mapping[chr] fig = plt.figure(1, (iopts.w, iopts.h)) root = fig.add_axes([0, 0, 1, 1]) # Gauge ratio = draw_gauge(root, margin, clen, rightmargin=4 * margin) yinterval = .3 xx = margin yy = 1 - margin yy -= yinterval xlen = clen / ratio if "_" in chr: ca, cb = chr.split("_") cc = ca[0].upper() + cb root.add_patch(Rectangle((xx, yy), xlen, yinterval - inner, color=gray)) ax = fig.add_axes([xx, yy, xlen, yinterval - inner]) nbins = clen / shift if clen % shift: nbins += 1 owindow = clen / 100 if owindow > window: window = owindow / shift * shift stackplot(ax, stackbins, nbins, palette, chr, window, shift) root.text(xx + inner, yy + yinterval - 2 * inner, cc, va="top") # Legends xx += xlen + .01 yspace = (yinterval - inner) / (len(stackbins) + 1) yy = 1 - margin - yinterval for s, p in zip(stacks, palette): s = s.replace("_", " ") s = Registration.get(s, s) yy += yspace root.add_patch(Rectangle((xx, yy), inner, inner, color=p, lw=0)) root.text(xx + 1.5 * inner, yy, s, size=10) yh = .05 # Heatmap height # Heatmaps xx = margin yy = 1 - margin - yinterval - inner for s, p in zip(heatmaps, heatmapbins): s = s.replace("_", " ") s = Registration.get(s, s) yy -= yh m = stackarray(p, chr, window, shift) Y = np.array([m, m]) root.imshow(Y, extent=(xx, xx + xlen, yy, yy + yh - inner), interpolation="nearest", aspect="auto") root.text(xx + xlen + .01, yy, s, size=10) yy -= yh meres = opts.meres if meres: bed = Bed(meres) for b in bed: if b.seqid != chr: continue pos = (b.start + b.end) / 2 cpos = pos / ratio xx = margin + cpos accn = b.accn.capitalize() root.add_patch(CirclePolygon((xx, yy), radius=.01, fc="m", ec="m")) root.text(xx + .014, yy, _(accn), va="center", color="m") root.set_xlim(0, 1) root.set_ylim(0, 1) root.set_axis_off() image_name = chr + "." + iopts.format logging.debug("Print image to `{0}` {1}".format(image_name, iopts)) plt.savefig(image_name, dpi=iopts.dpi) plt.rcdefaults()
def stack(args): """ %prog stack fastafile Create landscape plots that show the amounts of genic sequences, and repetitive sequences along the chromosomes. """ p = OptionParser(stack.__doc__) p.add_option("--top", default=10, type="int", help="Draw the first N chromosomes [default: %default]") p.add_option("--stacks", default="Exons,Introns,DNA_transposons,Retrotransposons", help="Features to plot in stackplot [default: %default]") p.add_option("--switch", help="Change chr names based on two-column file [default: %default]") add_window_options(p) opts, args, iopts = set_image_options(p, args, figsize="8x8") if len(args) != 1: sys.exit(not p.print_help()) fastafile, = args top = opts.top window, shift = check_window_options(opts) switch = opts.switch if switch: switch = DictFile(opts.switch) bedfiles = [x + ".bed" for x in opts.stacks.split(",")] binfiles = get_binfiles(bedfiles, fastafile, shift) sizes = Sizes(fastafile) s = list(sizes.iter_sizes())[:top] maxl = max(x[1] for x in s) margin = .08 inner = .02 # y distance between tracks pf = fastafile.rsplit(".", 1)[0] fig = plt.figure(1, (iopts.w, iopts.h)) root = fig.add_axes([0, 0, 1, 1]) max_len = s # Gauge ratio = draw_gauge(root, margin, maxl) # Per chromosome yinterval = (1 - 2 * margin) / (top + 1) xx = margin yy = 1 - margin for chr, clen in s: yy -= yinterval xlen = clen / ratio if "_" in chr: ca, cb = chr.split("_") cc = ca[0].upper() + cb if switch and cc in switch: cc = "\n".join((cc, "({0})".format(switch[cc]))) root.add_patch(Rectangle((xx, yy), xlen, yinterval - inner, color=gray)) ax = fig.add_axes([xx, yy, xlen, yinterval - inner]) nbins = clen / shift if clen % shift: nbins += 1 stackplot(ax, binfiles, nbins, palette, chr, window, shift) root.text(xx - .04, yy + .5 * (yinterval - inner), cc, ha="center", va="center") ax.set_xlim(0, nbins) ax.set_ylim(0, 1) ax.set_axis_off() # Legends yy -= yinterval xx = margin for b, p in zip(bedfiles, palette): b = b.rsplit(".", 1)[0].replace("_", " ") b = Registration.get(b, b) root.add_patch(Rectangle((xx, yy), inner, inner, color=p, lw=0)) xx += 2 * inner root.text(xx, yy, _(b), size=13) xx += len(b) * .012 + inner root.set_xlim(0, 1) root.set_ylim(0, 1) root.set_axis_off() image_name = pf + "." + iopts.format logging.debug("Print image to `{0}` {1}".format(image_name, iopts)) plt.savefig(image_name, dpi=iopts.dpi) plt.rcdefaults()
def main(): p = OptionParser(__doc__) p.add_option("--groups", default=False, action="store_true", help="The first row contains group info [default: %default]") p.add_option("--rowgroups", help="Row groupings [default: %default]") p.add_option("--horizontalbar", default=False, action="store_true", help="Horizontal color bar [default: vertical]") p.add_option("--cmap", default="jet", help="Use this color map [default: %default]") opts, args, iopts = set_image_options(p, figsize="8x8") if len(args) != 1: sys.exit(not p.print_help()) datafile, = args pf = datafile.rsplit(".", 1)[0] rowgroups = opts.rowgroups groups, rows, cols, data = parse_csv(datafile, vmin=1, groups=opts.groups) cols = [x.replace("ay ", "") for x in cols] if rowgroups: fp = open(rowgroups) rgroups = [] for row in fp: a, b = row.split() irows = [rows.index(x) for x in b.split(",")] rgroups.append((a, min(irows), max(irows))) plt.rcParams["axes.linewidth"] = 0 xstart = .18 fig = plt.figure(1, (iopts.w, iopts.h)) root = fig.add_axes([0, 0, 1, 1]) ax = fig.add_axes([xstart, .15, .7, .7]) default_cm = cm.get_cmap(opts.cmap) im = ax.matshow(data, cmap=default_cm, norm=LogNorm(vmin=1, vmax=10000)) nrows, ncols = len(rows), len(cols) xinterval = .7 / ncols yinterval = .7 / max(nrows, ncols) plt.xticks(range(ncols), cols, rotation=45, size=10, ha="center") plt.yticks(range(nrows), rows, size=10) for x in ax.get_xticklines() + ax.get_yticklines(): x.set_visible(False) ax.set_xlim(-.5, ncols - .5) t = [1, 10, 100, 1000, 10000] pad = .06 if opts.horizontalbar: ypos = .5 * (1 - nrows * yinterval) - pad axcolor = fig.add_axes([.3, ypos, .4, .02]) orientation = "horizontal" else: axcolor = fig.add_axes([.9, .3, .02, .4]) orientation = "vertical" fig.colorbar(im, cax=axcolor, ticks=t, format=_("%d"), orientation=orientation) if groups: groups = [(key, len(list(nn))) for key, nn in groupby(groups)] yy = .5 + .5 * nrows / ncols * .7 + .06 e = .005 sep = -.5 for k, kl in groups: # Separator in the array area sep += kl ax.plot([sep, sep], [-.5, nrows - .5], "w-", lw=2) # Group labels on the top kl *= xinterval root.plot([xstart + e, xstart + kl - e], [yy, yy], "-", color="gray", lw=2) root.text(xstart + .5 * kl, yy + e, k, ha="center", color="gray") xstart += kl if rowgroups: from jcvi.graphics.glyph import TextCircle xpos = .04 tip = .015 assert rgroups ystart = 1 - .5 * (1 - nrows * yinterval) for gname, start, end in rgroups: start = ystart - start * yinterval end = ystart - (end + 1) * yinterval start -= tip / 3 end += tip / 3 # Bracket the groups root.plot((xpos, xpos + tip), (start, start), "k-", lw=2) root.plot((xpos, xpos), (start, end), "k-", lw=2) root.plot((xpos, xpos + tip), (end, end), "k-", lw=2) TextCircle(root, xpos, .5 * (start + end), gname) root.set_xlim(0, 1) root.set_ylim(0, 1) root.set_axis_off() image_name = pf + "." + opts.cmap + "." + iopts.format logging.debug("Print image to `{0}` {1}".format(image_name, iopts)) plt.savefig(image_name, dpi=iopts.dpi) plt.rcdefaults()
image_name = op.splitext(blastfile)[0] + "." + opts.format plt.rcParams["xtick.major.pad"] = 16 plt.rcParams["ytick.major.pad"] = 16 # Fix the width xsize, ysize = qsizes.totalsize, ssizes.totalsize ratio = ysize * 1. / xsize if proportional else 1 width = iopts.w height = iopts.h * ratio fig = plt.figure(1, (width, height)) root = fig.add_axes([0, 0, 1, 1]) # the whole canvas ax = fig.add_axes([.1, .1, .8, .8]) # the dot plot blastplot(ax, blastfile, qsizes, ssizes, qbed, sbed, style=opts.style, proportional=proportional, sampleN=opts.sample, baseticks=True, stripNames=opts.stripNames) # add genome names to_ax_label = lambda fname: _(op.basename(fname).split(".")[0]) gx, gy = [to_ax_label(x.filename) for x in (qsizes, ssizes)] ax.set_xlabel(gx, size=16) ax.set_ylabel(gy, size=16) root.set_xlim(0, 1) root.set_ylim(0, 1) root.set_axis_off() logging.debug("Print image to `{0}` {1}".format(image_name, iopts)) plt.savefig(image_name, dpi=iopts.dpi) plt.rcdefaults()
def stack(args): """ %prog stack fastafile Create landscape plots that show the amounts of genic sequences, and repetitive sequences along the chromosomes. """ p = OptionParser(stack.__doc__) p.add_option("--top", default=10, type="int", help="Draw the first N chromosomes [default: %default]") p.add_option("--stacks", default="Exons,Introns,DNA_transposons,Retrotransposons", help="Features to plot in stackplot [default: %default]") p.add_option( "--switch", help="Change chr names based on two-column file [default: %default]") add_window_options(p) opts, args, iopts = set_image_options(p, args, figsize="8x8") if len(args) != 1: sys.exit(not p.print_help()) fastafile, = args top = opts.top window, shift, subtract = check_window_options(opts) switch = opts.switch if switch: switch = DictFile(opts.switch) bedfiles = [x + ".bed" for x in opts.stacks.split(",")] binfiles = get_binfiles(bedfiles, fastafile, shift, subtract) sizes = Sizes(fastafile) s = list(sizes.iter_sizes())[:top] maxl = max(x[1] for x in s) margin = .08 inner = .02 # y distance between tracks pf = fastafile.rsplit(".", 1)[0] fig = plt.figure(1, (iopts.w, iopts.h)) root = fig.add_axes([0, 0, 1, 1]) max_len = s # Gauge ratio = draw_gauge(root, margin, maxl) # Per chromosome yinterval = (1 - 2 * margin) / (top + 1) xx = margin yy = 1 - margin for chr, clen in s: yy -= yinterval xlen = clen / ratio if "_" in chr: ca, cb = chr.split("_") cc = ca[0].upper() + cb if switch and cc in switch: cc = "\n".join((cc, "({0})".format(switch[cc]))) root.add_patch(Rectangle((xx, yy), xlen, yinterval - inner, color=gray)) ax = fig.add_axes([xx, yy, xlen, yinterval - inner]) nbins = clen / shift if clen % shift: nbins += 1 stackplot(ax, binfiles, nbins, palette, chr, window, shift) root.text(xx - .04, yy + .5 * (yinterval - inner), cc, ha="center", va="center") ax.set_xlim(0, nbins) ax.set_ylim(0, 1) ax.set_axis_off() # Legends yy -= yinterval xx = margin for b, p in zip(bedfiles, palette): b = b.rsplit(".", 1)[0].replace("_", " ") b = Registration.get(b, b) root.add_patch(Rectangle((xx, yy), inner, inner, color=p, lw=0)) xx += 2 * inner root.text(xx, yy, _(b), size=13) xx += len(b) * .012 + inner root.set_xlim(0, 1) root.set_ylim(0, 1) root.set_axis_off() image_name = pf + "." + iopts.format logging.debug("Print image to `{0}` {1}".format(image_name, iopts)) plt.savefig(image_name, dpi=iopts.dpi) plt.rcdefaults()
def heatmap(args): """ %prog heatmap fastafile chr1 Combine stack plot with heatmap to show abundance of various tracks along given chromosome. Need to give multiple beds to --stacks and --heatmaps """ p = OptionParser(heatmap.__doc__) p.add_option("--stacks", default="Exons,Introns,DNA_transposons,Retrotransposons", help="Features to plot in stackplot [default: %default]") p.add_option("--heatmaps", default="Copia,Gypsy,hAT,Helitron,Introns,Exons", help="Features to plot in heatmaps [default: %default]") p.add_option( "--meres", default=None, help="Extra centromere / telomere features [default: %default]") add_window_options(p) opts, args, iopts = set_image_options(p, args, figsize="8x5") if len(args) != 2: sys.exit(not p.print_help()) fastafile, chr = args window, shift, subtract = check_window_options(opts) stacks = opts.stacks.split(",") heatmaps = opts.heatmaps.split(",") stackbeds = [x + ".bed" for x in stacks] heatmapbeds = [x + ".bed" for x in heatmaps] stackbins = get_binfiles(stackbeds, fastafile, shift, subtract) heatmapbins = get_binfiles(heatmapbeds, fastafile, shift, subtract) margin = .06 inner = .015 clen = Sizes(fastafile).mapping[chr] fig = plt.figure(1, (iopts.w, iopts.h)) root = fig.add_axes([0, 0, 1, 1]) # Gauge ratio = draw_gauge(root, margin, clen, rightmargin=4 * margin) yinterval = .3 xx = margin yy = 1 - margin yy -= yinterval xlen = clen / ratio if "_" in chr: ca, cb = chr.split("_") cc = ca[0].upper() + cb root.add_patch(Rectangle((xx, yy), xlen, yinterval - inner, color=gray)) ax = fig.add_axes([xx, yy, xlen, yinterval - inner]) nbins = clen / shift if clen % shift: nbins += 1 owindow = clen / 100 if owindow > window: window = owindow / shift * shift stackplot(ax, stackbins, nbins, palette, chr, window, shift) root.text(xx + inner, yy + yinterval - 2 * inner, cc, va="top") # Legends xx += xlen + .01 yspace = (yinterval - inner) / (len(stackbins) + 1) yy = 1 - margin - yinterval for s, p in zip(stacks, palette): s = s.replace("_", " ") s = Registration.get(s, s) yy += yspace root.add_patch(Rectangle((xx, yy), inner, inner, color=p, lw=0)) root.text(xx + 1.5 * inner, yy, s, size=10) yh = .05 # Heatmap height # Heatmaps xx = margin yy = 1 - margin - yinterval - inner for s, p in zip(heatmaps, heatmapbins): s = s.replace("_", " ") s = Registration.get(s, s) yy -= yh m = stackarray(p, chr, window, shift) Y = np.array([m, m]) root.imshow(Y, extent=(xx, xx + xlen, yy, yy + yh - inner), interpolation="nearest", aspect="auto") root.text(xx + xlen + .01, yy, s, size=10) yy -= yh meres = opts.meres if meres: bed = Bed(meres) for b in bed: if b.seqid != chr: continue pos = (b.start + b.end) / 2 cpos = pos / ratio xx = margin + cpos accn = b.accn.capitalize() root.add_patch(CirclePolygon((xx, yy), radius=.01, fc="m", ec="m")) root.text(xx + .014, yy, _(accn), va="center", color="m") root.set_xlim(0, 1) root.set_ylim(0, 1) root.set_axis_off() image_name = chr + "." + iopts.format logging.debug("Print image to `{0}` {1}".format(image_name, iopts)) plt.savefig(image_name, dpi=iopts.dpi) plt.rcdefaults()