def model_makeplots(indir, outdir): objs = lcommon.load_pickledir(indir) if not objs: return rcommon.union_models(objs) for attr in objs[0].get_attrs_depth1(): outbase = os.path.join(outdir, attr) k2ydata = model_get_ydata(objs, attr) gene_attrs = ['v', 'd', 'j'] if attr in gene_attrs: xlabels = lcommon.sort_by_gene_number(k2ydata.keys()) rcmp.diff_plot(k2ydata, outbase, xlabels, attr.upper(), ylabel='Frequency') else: rcmp.diff_plot(k2ydata, outbase, label=attr.upper(), xmin=-0.5, xmax=30.5, ylabel='Frequency') for attr in objs[0].get_attrs_depth2(): k_k2ydata = model_get_ydata2(objs, attr) for k, k2ydata in k_k2ydata.iteritems(): if not isinstance(k, str): k = str(k) outbase = os.path.join(outdir, "%s_%s" % (attr, k)) if attr in ['v2del', 'j2del', 'd2del']: rcmp.diff_plot(k2ydata, outbase, label=attr.upper(), xmin=-0.5, xmax=16.5, ylabel='Frequency') else: rcmp.diff_plot(k2ydata, outbase, label=attr.upper(), ylabel='Frequency')
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 model_diff(indir1, indir2, outdir): # indir1 contains sam1_model.pickle, sam2_model.pickle ... # indir2: similarly to indir1 name1 = os.path.basename(indir1.rstrip("/")) name2 = os.path.basename(indir2.rstrip("/")) objs1 = lcommon.load_pickledir(indir1) objs2 = lcommon.load_pickledir(indir2) rcommon.union_models(objs1 + objs2) # statistic for each attr for differential usage: statsdir = os.path.join(outdir, "stat_tests") system("mkdir -p %s" % statsdir) model_diff_signi_test(objs1, objs2, statsdir) # if True: # HACK # return med1 = rcommon.model_get_median(objs1) # med2 = model_get_median(objs2) for attr in med1.get_attrs_depth1(): outbase1 = os.path.join(outdir, "%s_%s_cmp2_%s" % (attr, name2, name1)) k2diffs1 = diff_sample_model(med1, objs2, attr) gene_attrs = ["v", "d", "j", "dj"] if attr in gene_attrs: if attr == "dj": xlabels = sorted(k2diffs1.keys()) else: xlabels = lcommon.sort_by_gene_number(k2diffs1.keys()) diff_plot(k2diffs1, outbase1, xlabels, attr.upper(), ymin=-0.1, ymax=0.1) else: diff_plot(k2diffs1, outbase1, label=attr.upper(), xmin=-0.5, xmax=30.5, ymin=-0.02, ymax=0.05) # outbase2 = os.path.join(outdir, "%s_%s_cmp2_%s" % (attr, name1, name2)) # k2diffs2 = diff_sample_model(med2, objs1, attr) # diff_plot(k2diffs2, outbase2) for attr in med1.get_attrs_depth2(): k_k2diffs1 = diff_sample_model2(med1, objs2, attr) for k, k2diffs1 in k_k2diffs1.iteritems(): if not isinstance(k, str): k = str(k) outbase1 = os.path.join(outdir, "%s_%s_%s_cmp2_%s" % (attr, k, name2, name1)) if attr in ["v2del", "j2del", "d2del"]: diff_plot(k2diffs1, outbase1, label=attr.upper(), xmin=-0.5, xmax=16.5) else: diff_plot(k2diffs1, outbase1, label=attr.upper())
def trackclone_latex_tab(f, clone2groups): sortedclones = libcommon.sort_by_gene_number(clone2groups.keys()) #for clone in sorted(clone2groups.keys()): for clone in sortedclones: cloneitems = clone.split('_') v = cloneitems[0] seq = cloneitems[1] j = cloneitems[2] groupfreqs = clone2groups[clone] freqstr = '' for gfreqs in groupfreqs: if not gfreqs: freqstr += "& Absent" else: freqs = [float(freq) for freq in gfreqs.rstrip(',').split(',')] sortedfreqs = sorted(freqs, reverse=True) sortedfreqs = ["%.4f" % (freq * 100) for freq in sortedfreqs] freqstr += " & %s" % ",".join(sortedfreqs) f.write("%s & %s & %s %s\\\\\n" % (v, seq, j, freqstr)) f.write("\\hline\n")
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)