def draw_diversity_plot_hacktimeseries(group2names, name2obj, attr, outfile, outfmt='pdf', dpi=300): '''Instead of box plot for each group, keep each sample separately ''' axes, fig, pdf = drawcommon.get_axes(outfile=outfile, outfmt=outfmt, dpi=dpi) xlabels = ['BL', 'PC', 'W6', 'W14', 'W20'] xdata = range(1, len(xlabels) + 1) cat2group2names = {'None': {}, 'IL7': {}} for catgroup, names in group2names.iteritems(): items = catgroup.split('-') cat = items[0] group = items[1] cat2group2names[cat][group] = names #HACK #sam2color = {'LiBr': "#6baed6", 'BrBu': "#3182bd", 'MeRi': "#08519c", # blue # 'LaBo': "#74c476", 'FoCh': "#31a354", 'JaMa': "#006d2c"} sam2color = {'LiBr': "#525252", 'BrBu': "#969696", 'MeRi': "#cccccc", # gray 'LaBo': "#2171b5", 'FoCh': "#6baed6", 'JaMa': "#bdd7e7"} # blue sam2data = {} sam2marker = {} name2cat = {} for cat, g2n in cat2group2names.iteritems(): for i, group in enumerate(xlabels): # each timepoint names = g2n[group] if i == 0: name2cat[names[0].split('-')[0]] = cat for name in names: sam = name.split('-')[0] y = name2obj[name][attr] if sam not in sam2data: sam2data[sam] = [y] #sam2color[sam] = name2obj[name].color sam2marker[sam] = name2obj[name].marker else: sam2data[sam].append(y) lines = [] linenames = [] for sam, ydata in sam2data.iteritems(): l, = axes.plot(xdata, ydata, color=sam2color[sam], marker=sam2marker[sam], linestyle='-', markeredgecolor=sam2color[sam]) if sam in name2cat: #legend lines.append(l) linenames.append(name2cat[sam]) drawcommon.set_grid(axes) drawcommon.set_legend(axes, lines, linenames) drawcommon.edit_spine(axes) drawcommon.set_xticks(axes, xdata, xlabels) drawcommon.adjust_ticklabels(axes, xrotation=30) axes.set_xlim(0.5, len(xdata) + 0.5) drawcommon.set_labels(axes, xlabel="Group", ylabel=attr.title()) axes.ticklabel_format(style='sci', axis='y', scilimits=(0,0)) drawcommon.write_image(fig, pdf, outfmt, outfile, dpi)
def draw_gene_usage(name2obj, attr, type, outbase, genes, opts=None): '''xaxis: genes yaxis: relative usage one line/ sample ''' if opts: axes, fig, pdf = drawcommon.get_axes(outfile=outbase, outfmt=opts.plotformat, dpi=opts.dpi) else: axes, fig, pdf = drawcommon.get_axes(outfile=outbase) #genes = sorted(genes) genes = libcommon.sort_by_gene_number(genes) xdata = range(len(genes)) group2line = {} lines = [] linenames = [] for name, obj in name2obj.iteritems(): ydata = gu_get_sample_data(obj, attr, type, genes) line, = axes.plot(xdata, ydata, color=obj.color, marker=obj.marker, markeredgecolor=obj.color, linestyle='-') lines.append(line) linenames.append(obj.name) if obj.group not in group2line: group2line[obj.group] = line if len(linenames) > 10: linenames = sorted(group2line.keys()) lines = [group2line[group] for group in linenames] drawcommon.set_legend(axes, lines, linenames) drawcommon.adjust_ticklabels(axes, xrotation=75) drawcommon.set_grid(axes) drawcommon.edit_spine(axes) xlabels = [g.lstrip("TRB") for g in genes] drawcommon.set_xticks(axes, xdata, xlabels) axes.set_xlim(-0.5, len(xlabels) + 0.5) axes.set_ylim(bottom=-0.005) drawcommon.adjust_ticklabels(axes, xrotation=75) drawcommon.set_labels(axes, xlabel="Gene", ylabel="%% of total %s" % attr) if opts: drawcommon.write_image(fig, pdf, opts.plotformat, outbase, opts.dpi) else: drawcommon.write_image(fig, pdf, outname=outbase)
def diff_plot(x2yvec, outbase, xlabels=[], label="", xmin=None, xmax=None, ymin=None, ymax=None, ylabel=""): if not x2yvec: return axes, fig, pdf = drawcommon.get_axes(outfile=outbase) if not xlabels: xlabels = sorted(x2yvec.keys()) xdata = range(len(xlabels)) ydata = [x2yvec[x] for x in xlabels] axes.boxplot(ydata) drawcommon.set_grid(axes) drawcommon.edit_spine(axes) # Set limits if xmin: if isinstance(xlabels[0], numbers.Number): xmin += xdata[0] - xlabels[0] + 1 else: xmin = -0.5 if xmax: if isinstance(xlabels[0], numbers.Number): xmax += xdata[0] - xlabels[0] + 1 else: xmax = len(xlabels) + 0.5 if isinstance(xlabels[0], numbers.Number) or isinstance(xlabels[0], tuple): xlabels = [str(x) for x in xlabels] xlabels = [x.replace("TRB", "") for x in xlabels] drawcommon.set_xticks(axes, [x + 1 for x in xdata], xlabels) axes.plot([xmin, xmax], [0, 0], ls="-", color="#252525") axes.set_xlim(xmin, xmax) if ymin and ymax: axes.set_ylim(ymin, ymax) elif ymin: # axes.set_ylim(bottom=-0.005) axes.set_ylim(bottom=ymin) drawcommon.adjust_ticklabels(axes, xrotation=75) if not ylabel: ylabel = "Difference in %s" % label drawcommon.set_labels(axes, xlabel=label, ylabel=ylabel) drawcommon.write_image(fig, pdf, "pdf", outbase, 300)
def diff_plot(n2diff, genes, outbase): axes, fig, pdf = drawcommon.get_axes(outfile=outbase) xdata = range(len(genes)) data = [] #for name, ydata in n2diff.iteritems(): #axes.plot(xdata, ydata, linestyle='None', marker='.') for x in xdata: ydata = [n2diff[name][x] for name in n2diff] data.append(ydata) axes.boxplot(data) drawcommon.set_grid(axes) drawcommon.edit_spine(axes) xlabels = [g.lstrip("TRB") for g in genes] drawcommon.set_xticks(axes, [x + 1 for x in xdata], xlabels) axes.set_xlim(-0.5, len(xlabels) + 0.5) #axes.set_ylim(bottom=-0.005) drawcommon.adjust_ticklabels(axes, xrotation=75) drawcommon.set_labels(axes, xlabel="Gene", ylabel="Difference in gene usage") drawcommon.write_image(fig, pdf, 'pdf', outbase, 300)
def draw_diversity_plot(group2names, name2obj, attr, outfile, outfmt='pdf', dpi=300): axes, fig, pdf = drawcommon.get_axes(outfile=outfile, outfmt=outfmt, dpi=dpi) xdata = range(1, len(group2names) + 1) xlabels = sorted(group2names.keys()) data = [] for group in xlabels: names = group2names[group] vec = [name2obj[name][attr] for name in names] data.append(vec) axes.boxplot(data) drawcommon.set_grid(axes) drawcommon.edit_spine(axes) drawcommon.set_xticks(axes, xdata, xlabels) drawcommon.adjust_ticklabels(axes, xrotation=30) drawcommon.set_labels(axes, xlabel="Group", ylabel=attr.title()) axes.ticklabel_format(style='sci', axis='y', scilimits=(0,0)) drawcommon.write_image(fig, pdf, outfmt, outfile, dpi)
def pair_group_cmp(medvec, vecs, genes, outfile): axes, fig, pdf = drawcommon.get_axes(outfile=outfile) xdata = range(len(genes)) data = [] #for vec in vecs: #ydata = [v - medvec[i] for i, v in enumerate(vec)] #axes.plot(xdata, ydata, linestyle='None', marker='.') for x in xdata: ydata = [vec[x] - medvec[x] for vec in vecs] data.append(ydata) axes.boxplot(data) drawcommon.set_grid(axes) drawcommon.edit_spine(axes) xlabels = [g.lstrip("TRB") for g in genes] #drawcommon.set_xticks(axes, xdata, xlabels) drawcommon.set_xticks(axes, [x + 1 for x in xdata], xlabels) axes.set_xlim(-0.5, len(xlabels) + 0.5) #axes.set_ylim(bottom=-0.005) drawcommon.adjust_ticklabels(axes, xrotation=75) drawcommon.set_labels(axes, xlabel="Gene", ylabel="Difference in gene usage") drawcommon.write_image(fig, pdf, 'pdf', outfile, 300)
def draw_gene_usage_avr(name2obj, attr, type, outbase, genes, opts, bar=False): '''xaxis: genes yaxis: relative usage one line/ sample ''' axes, fig, pdf = drawcommon.get_axes(outfile=outbase, outfmt=opts.plotformat, dpi=opts.dpi) #genes = sorted(genes) #genes = sorted(genes, key=lambda g: libcommon.get_gene_number(g)) genes = libcommon.sort_by_gene_number(genes) xdata = range(len(genes)) g2ydata = {} g2color = {} for name, obj in name2obj.iteritems(): ydata = gu_get_sample_data(obj, attr, type, genes) g = obj.group if g not in g2ydata: g2ydata[g] = [[y] for y in ydata] g2color[g] = obj.color else: assert len(g2ydata[g]) == len(ydata) for i, y in enumerate(ydata): g2ydata[g][i].append(y) barwidth = (1.0 - 0.35) / len(g2ydata.keys()) lines = [] linenames = [] #for g, ydata in g2ydata.iteritems(): for i, g in enumerate(sorted(g2ydata.keys())): ydata = g2ydata[g] ydata = [ylist if ylist else [0.0] for ylist in ydata] mean_ydata = [np.mean(ylist) for ylist in ydata] std_ydata = [np.std(ylist) for ylist in ydata] if not bar: line, = axes.plot(xdata, mean_ydata, color=g2color[g], linestyle='-', markeredgecolor=g2color[g], marker='o') lines.append(line) axes.errorbar(xdata, mean_ydata, yerr=std_ydata, color=g2color[g], linestyle="None", marker="None") else: group_xdata = [x + barwidth * i for x in xdata] line = axes.bar(group_xdata, mean_ydata, barwidth, yerr=std_ydata, color=g2color[g], ecolor="#424242", edgecolor=g2color[g]) lines.append(line[0]) linenames.append(g) drawcommon.set_legend(axes, lines, linenames) drawcommon.set_grid(axes) drawcommon.edit_spine(axes) xlabels = [g.lstrip("TRB") for g in genes] if not bar: drawcommon.set_xticks(axes, xdata, xlabels) else: xticks = [x + 0.325 for x in xdata] drawcommon.set_xticks(axes, xticks, xlabels) axes.set_xlim(-0.5, len(xlabels) + 0.5) axes.set_ylim(bottom=-0.005) drawcommon.adjust_ticklabels(axes, xrotation=75) drawcommon.set_labels(axes, xlabel="Gene", ylabel="%% of total %s" % attr) drawcommon.write_image(fig, pdf, opts.plotformat, outbase, opts.dpi)