def draw_track_clone_no_matched2(clone, data, groups, outbase, opts=None):
    if opts is not None:
        axes, fig, pdf = drawcommon.get_axes(outfile=outbase,
                                         outfmt=opts.plotformat, dpi=opts.dpi)
    else:
        axes, fig, pdf = drawcommon.get_axes(outfile=outbase)
    xdata = range(1, len(groups) + 1)
    offset = 0.02
    numpoints = 25.0
    for i, x in enumerate(xdata):
        ydata = sorted(data[i])
        halfwidth = min(numpoints, len(ydata)) * offset / 2
        currxdata = []
        numrounds = int(math.ceil(len(ydata) / numpoints))
        for r in xrange(numrounds):
            numy = min(numpoints, len(ydata) - r * numpoints)
            for j in xrange(int(numy)):
                currxdata.append(x - halfwidth + j * offset)
        axes.plot(currxdata, ydata, ls='none', marker='o', markersize=5)

    #axes.boxplot(data)
    
    drawcommon.set_grid(axes)
    drawcommon.edit_spine(axes)
    drawcommon.set_xticks(axes, xdata, groups)
    #drawcommon.set_labels(axes, "Clone %s" % clone, "Groups", "Clone size")
    axes.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
    axes.set_xlim(0, len(groups) + 1)

    drawcommon.write_image(fig, pdf, outname=outbase)
def draw_clonesize_dist(name2obj, attr, outfile, outfmt='pdf', dpi=300):
    # name2stat: key = sample name, val = CloneSizeStat
    # attr = numclones/counts/numclones_cumul/counts_cumul/topfreqs/
    #         topfreqs_cumul
    assert len(name2obj) > 0
    axes, fig, pdf = drawcommon.get_axes(outfile=outfile, outfmt=outfmt,
                                                                      dpi=dpi)
    obj0 = name2obj.values()[0]
    xlabels = [str(x) for x in obj0.freqs]
    if attr == 'topfreqs' or attr == 'topfreqs_cumul':
        xlabels = [str(x + 1) for x in xrange(len(obj0.topfreqs))] 
    xdata = range(0, len(xlabels))
    numtop = len(xdata)
    if attr == 'numclones' or attr == 'numclones_attr':
        axes.set_yscale('log')
    linenames = []
    lines = []
    for name, obj in name2obj.iteritems():
        ydata = obj[attr]
        if len(xdata) != len(ydata):  # HACK
            continue
        line, = axes.plot(xdata, ydata, color=obj.color, marker=obj.marker,
                         markeredgecolor=obj.color, linestyle='-')
        lines.append(line)
        linenames.append(obj.name)

    drawcommon.set_grid(axes)
    drawcommon.set_legend(axes, lines, linenames) 
    drawcommon.edit_spine(axes)
    drawcommon.set_xticks(axes, xdata, xlabels)
    
    labels = cs_get_attr_plot_labels(attr, numtop)
    drawcommon.set_labels(axes, labels[0], labels[1], labels[2])

    drawcommon.write_image(fig, pdf, outfmt, outfile, dpi) 
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_track_clone_no_matched(clone, data, groups, outbase, opts):
    axes, fig, pdf = drawcommon.get_axes(outfile=outbase,
                                         outfmt=opts.plotformat, dpi=opts.dpi)
    xdata = range(1, len(groups) + 1)
    axes.boxplot(data)
    
    drawcommon.set_grid(axes)
    drawcommon.edit_spine(axes)
    drawcommon.set_xticks(axes, xdata, groups)
    #drawcommon.set_labels(axes, "Clone %s" % clone, "Groups", "Clone size")
    axes.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
    
    drawcommon.write_image(fig, pdf, opts.plotformat, outbase, opts.dpi)
def draw_track_clone_hack(clone, rows, groups, outbase, opts):
    axes, fig, pdf = drawcommon.get_axes(outfile=outbase,
                                         outfmt=opts.plotformat, dpi=opts.dpi)
    #sorted_groups = [
    #                 "cd4-naive-baseline", "cd4-naive-postchemo", "cd4-naive-week6", "cd4-naive-week14", "cd4-naive-week20",
    #                 "cd4-effector-baseline", "cd4-effector-postchemo", "cd4-effector-week6", "cd4-effector-week14", "cd4-effector-week20",
    #                 "cd4-central-memory-baseline", "cd4-central-memory-postchemo", "cd4-central-memory-week6", "cd4-central-memory-week14", "cd4-central-memory-week20",
    #                 "cd4-stemcell-memory-baseline", "cd4-stemcell-memory-postchemo", "cd4-stemcell-memory-week6", "cd4-stemcell-memory-week14", "cd4-stemcell-memory-week20",
    #                 "cd8-naive-baseline", "cd8-naive-postchemo", "cd8-naive-week6", "cd8-naive-week14", "cd8-naive-week20",
    #                 "cd8-effector-baseline", "cd8-effector-postchemo", "cd8-effector-week6", "cd8-effector-week14", "cd8-effector-week20",
    #                 "cd8-central-memory-baseline", "cd8-central-memory-postchemo", "cd8-central-memory-week6", "cd8-central-memory-week14", "cd8-central-memory-week20",
    #                 "cd8-stemcell-memory-baseline", "cd8-stemcell-memory-postchemo", "cd8-stemcell-memory-week6", "cd8-stemcell-memory-week14", "cd8-stemcell-memory-week20"
    #                 ]
    
    #sorted_groups = [
    #                 "cd4-N-BL", "cd4-N-PC", "cd4-N-W6", "cd4-N-W14", "cd4-N-W20",
    #                 "cd4-E-BL", "cd4-E-PC", "cd4-E-W6", "cd4-E-W14", "cd4-E-W20",
    #                 "cd4-cMem-BL", "cd4-cMem-PC", "cd4-cMem-W6", "cd4-cMem-W14", "cd4-cMem-W20",
    #                 "cd4-scMem-BL", "cd4-scMem-PC", "cd4-scMem-W6", "cd4-scMem-W14", "cd4-scMem-W20",
    #                 "cd8-N-BL", "cd8-N-PC", "cd8-N-W6", "cd8-N-W14", "cd8-N-W20",
    #                 "cd8-E-BL", "cd8-E-PC", "cd8-E-W6", "cd8-E-W14", "cd8-E-W20",
    #                 "cd8-cMem-BL", "cd8-cMem-PC", "cd8-cMem-W6", "cd8-cMem-W14", "cd8-cMem-W20",
    #                 "cd8-scMem-BL", "cd8-scMem-PC", "cd8-scMem-W6", "cd8-scMem-W14", "cd8-scMem-W20"
    #                 ]
    xdata = range(len(groups))
    #group2index = {}
    #for i, group in enumerate(groups):
    #    group2index[group] = i
    
    for row in rows:
        axes.plot(xdata, row, linestyle='-')
        #ydata = []
        #for group in sorted_groups:
        #    index = group2index[group]
        #    ydata.append(row[index])
        #axes.plot(xdata, ydata, linestyle='-')
    
    drawcommon.set_grid(axes)
    drawcommon.edit_spine(axes)
    #xlabels = []
    #for g in sorted_groups:
    #    xlabels.append(get_proper_hack(g))
    #drawcommon.set_xticks(axes, xdata, xlabels)
    #drawcommon.set_xticks(axes, xdata, sorted_groups)
    drawcommon.set_xticks(axes, xdata, groups)
    drawcommon.adjust_xticklabels(axes, size='xx-small', rotation=75)
    drawcommon.adjust_yticklabels(axes, size='xx-small')
    drawcommon.set_labels(axes, title="Clone %s" % clone, ylabel="Clone size")
    #drawcommon.set_labels(axes, "Clone %s" % clone, "Groups", "Clone size")
    
    drawcommon.write_image(fig, pdf, opts.plotformat, outbase, opts.dpi)
def draw_track_clone(clone, rows, groups, outbase, opts):
    axes, fig, pdf = drawcommon.get_axes(outfile=outbase,
                                         outfmt=opts.plotformat, dpi=opts.dpi)
    xdata = range(len(groups))
    for row in rows:
        axes.plot(xdata, row, linestyle='-')
    
    drawcommon.set_grid(axes)
    drawcommon.edit_spine(axes)
    drawcommon.set_xticks(axes, xdata, groups)
    drawcommon.adjust_xticklabels(axes, size='xx-small', rotation=75)
    drawcommon.set_labels(axes, "Clone %s" % clone, "Groups", "Clone size")
    
    drawcommon.write_image(fig, pdf, opts.plotformat, outbase, opts.dpi)
def draw_plot(sam2time2val, outfile, timepoints=None):
    axes, fig, pdf = drawcommon.get_axes(outfile=outfile)
    
    group2color = {'IL7_baseline': "#c6dbef", 'IL7_postchemo': "#9ecae1",
                   'IL7_week6': "#6baed6", 'IL7_week14': "#3182bd",
                   'IL7_week20': "#08519c", 'none_baseline': "#d9d9d9",
                   'none_postchemo': "#bdbdbd", 'none_week6': "#969696",
                   'none_week14': "#636363", 'none_week20': "#252525"}
    groups = ['IL7', 'none']
    group2sam = {'IL7': ['LaBo', 'FoCh', 'JaMa'], 'none': ['LiBr', 'BrBu', 'MeRi']}
    if timepoints is None:
        timepoints = ['baseline', 'postchemo', 'week6', 'week14', 'week20']
    
    barwidth = (1.0 - 0.25) / len(timepoints)
    
    sam_per_group = 3  #  number of sample per group
    xticks = []
    
    ymin = 20
    ymax = 0

    for i, group in enumerate(groups):
        xdata = [x + i * sam_per_group + 1 for x in xrange(sam_per_group)]
        samples = group2sam[group]
        num_times = len(timepoints)
        group_xticks = [x + float(num_times) / 2.0 * barwidth for x in xdata]
        xticks.extend(group_xticks)

        for j, timepoint in enumerate(timepoints):
            t_xdata = [x + j * barwidth for x in xdata]
            color = group2color["%s_%s" % (group, timepoint)]
            ydata = []
            for sam in samples:
                y = sam2time2val[sam][timepoint]
                ydata.append(y)
                ymin = min(ymin, y)
                ymax = max(ymax, y)
            line = axes.bar(t_xdata, ydata, barwidth, color=color, ecolor="#424242")
  
    drawcommon.set_grid(axes)
    drawcommon.edit_spine(axes)
    xlabels = ['LaBo_IL7', 'FoCh_IL7', 'JaMa_IL7', 'LiBr_none', 'BrBu_none', 'MeRi_none']
    drawcommon.set_xticks(axes, xticks, xlabels)
    #axes.set_xlim(xmin=8, xmax=min(maxx, 30))
    axes.set_ylim(ymin - 1, ymax + 0.5)

    drawcommon.set_labels(axes, xlabel='Samples', ylabel='Shannon Diversity Index')
    drawcommon.write_image(fig, pdf, outname=outfile)
Exemple #8
0
def draw_lendist(name2obj, attr, outfile, outfmt='pdf', dpi=300, bar=False):
    axes, fig, pdf = drawcommon.get_axes(outfile=outfile, outfmt=outfmt,
                                                                      dpi=dpi)
    group2line = {}
    lines = []
    linenames = []
    barwidth = (1.0 - 0.35) / len(name2obj)
    i = -1
    minx = 30 
    maxx = 0
    for name, obj in name2obj.iteritems():
        i += 1
        xdata, ydata = ld_get_sample_data(obj, attr)
        minx = min(minx, min(xdata))
        maxx = max(maxx, max(xdata))
        if not bar:
            line, = axes.plot(xdata, ydata, color=obj.color, marker=obj.marker,
                             markeredgecolor=obj.color, linestyle='-')
            lines.append(line)
        else:
            group_xdata = [x + barwidth * i for x in xdata]
            line = axes.bar(group_xdata, ydata, barwidth,
                            color=obj.color, ecolor="#424242") #,
                            #edgecolor=g2color[g], ecolor=g2color[g])
            lines.append(line[0])
        linenames.append(obj.name)
        if obj.group not in group2line:
            group2line[obj.group] = line

    if bar: 
        xticks = [x + 0.325 for x in xrange(minx, maxx + 1)]
        xlabels = [str(x) for x in xdata]
        drawcommon.set_xticks(axes, xticks, xlabels)
    axes.set_xlim(xmin=8, xmax=min(maxx, 30))
    axes.set_ylim(bottom=-0.005)
    
    if len(linenames) > 10:
        linenames = sorted(group2line.keys())
        lines = [group2line[group] for group in linenames]
    drawcommon.set_grid(axes)
    drawcommon.set_legend(axes, lines, linenames)
    drawcommon.edit_spine(axes)
    drawcommon.set_labels(axes, xlabel='Length', ylabel='%% of total %s' % attr)
    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) 
Exemple #10
0
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)
Exemple #11
0
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) 
Exemple #12
0
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) 
Exemple #13
0
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 draw_group_pairwise_similarity(group1, group2, vec11, vec12, vec22,
                                   outfile, outfmt='pdf', dpi=300,
                                   xmax=None, xmin=None, ymax=None, ymin=None):
    # xaxis: G1_G1, G1_G2, G2_G2; yaxis: similarity index
    axes, fig, pdf = drawcommon.get_axes(outfile=outfile, outfmt=outfmt,
                                                                      dpi=dpi)
    xdata = range(1, 4)
    xlabels = [group1, '%s_%s' % (group1, group2), group2]
    data = [vec11, vec12, vec22]
    axes.boxplot(data)
    
    #axes.yaxis.grid(b=True, color='#3F3F3F', linestyle='-', linewidth=0.05)
    drawcommon.set_grid(axes)
    drawcommon.edit_spine(axes)
    drawcommon.set_xticks(axes, xdata, xlabels)

    #axes.set_xlabel("Group", size='x-large', weight='bold')
    #axes.set_ylabel(attr, size='x-large', weight='bold')
    if not ymax or ymax > 1000:
        axes.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
    
    if xmin and xmax:
        axes.set_xlim(xmin=xmin, xmax=xmax)
    else:
        if xmin:
            axes.set_xlim(xmin=xmin)
        elif xmax:
            axes.set_xlim(xmax=xmax)
    if ymin and ymax:
        axes.set_ylim(ymin=ymin, ymax=ymax)
    else:
        if ymin:
            axes.set_ylim(ymin=ymin)
        elif ymax:
            axes.set_ylim(ymax=ymax)
    
    drawcommon.write_image(fig, pdf, outfmt, outfile, dpi)
Exemple #15
0
def draw_clonesize_dist_avr(name2obj, attr, outfile, outfmt='pdf', dpi=300):
    # name2stat: key = sample name, val = CloneSizeStat
    # attr = numclones/counts/numclones_cumul/counts_cumul/topfreqs/
    #         topfreqs_cumul
    assert len(name2obj) > 0
    axes, fig, pdf = drawcommon.get_axes(outfile=outfile, outfmt=outfmt,
                                                                      dpi=dpi)
    obj0 = name2obj.values()[0]
    xlabels = [str(x) for x in obj0.freqs]
    if attr == 'topfreqs' or attr == 'topfreqs_cumul':
        xlabels = [str(x + 1) for x in xrange(len(obj0.topfreqs))] 
    xdata = range(0, len(xlabels))
    numtop = len(xdata)
    #if attr == 'numclones':   # or attr == 'numclones_cumul':
    #    axes.set_yscale('log')
    
    g2ydata = {}
    g2color = {}
    for name, obj in name2obj.iteritems():
        ydata = obj[attr]
        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)

    linenames = []
    lines = []
    #boxwidth = 0.05
    #offset = boxwidth + 0.01
    offset = 0.05
    for i, g in enumerate(sorted(g2ydata.keys())):
        g_xdata = [x + 0.5 + offset * (i - 1) for x in xdata]
        g_xdata[0] += 0.35
        ydata = g2ydata[g]
        #axes.boxplot(ydata, positions=g_xdata, widths=boxwidth)
        mean_ydata = [np.mean(ylist) for ylist in ydata]
        std_ydata = [np.std(ylist) for ylist in ydata]
        if attr == "numclones_cumul" or attr == "numclones":
            mean_ydata = [log10(y) if y > 0 else 2.1 for y in mean_ydata]
            std_ydata = get_logstd(ydata)
        line, = axes.plot(g_xdata, mean_ydata, color=g2color[g], linestyle='-',
                          markeredgecolor=g2color[g], marker='o', lw=2)
        lines.append(line)
        linenames.append(g)
        axes.errorbar(g_xdata, mean_ydata, yerr=std_ydata, color=g2color[g],
                      linestyle="None", marker="None")

    drawcommon.set_grid(axes)
    drawcommon.set_legend(axes, lines, linenames) 
    drawcommon.edit_spine(axes)
    drawcommon.set_xticks(axes, xdata, xlabels)
    axes.set_xlim(-0.5, len(xlabels) + 0.5)
    if attr != "numclones_cumul" and attr != 'numclones':
        axes.set_ylim(bottom=-0.005)
    
    labels = cs_get_attr_plot_labels(attr, numtop)
    drawcommon.set_labels(axes, labels[0], labels[1], labels[2])

    drawcommon.write_image(fig, pdf, outfmt, outfile, dpi) 
Exemple #16
0
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) 
Exemple #17
0
def draw_lendist_avr(name2obj, attr, outfile, outfmt='pdf', dpi=300, bar=False):
    axes, fig, pdf = drawcommon.get_axes(outfile=outfile, outfmt=outfmt,
                                                                      dpi=dpi)
    g2x2y = {}
    g2numsam = {}
    g2color = {}
    minx = 30
    maxx = 0
    for name, obj in name2obj.iteritems():
        xdata, ydata = ld_get_sample_data(obj, attr)
        g = obj.group
        if g not in g2numsam:
            g2numsam[g] = 1
            g2color[g] = obj.color
        else:
            g2numsam[g] += 1
        minx = min(minx, min(xdata))
        maxx = max(maxx, max(xdata))
        for i, x in enumerate(xdata):
            y = ydata[i]
            if g not in g2x2y:
                g2x2y[g] = {x: [y]}
            elif x not in g2x2y[g]:
                g2x2y[g][x] = [y]
            else:
                g2x2y[g][x].append(y)

    lines = []
    linenames = []
    xdata = range(minx, maxx + 1)
    barwidth = (1.0 - 0.35) / len(g2x2y.keys())

    for i, g in enumerate(sorted(g2x2y.keys())):
        numsam = g2numsam[g]
        mean_ydata = []
        std_ydata = []
        for x in xdata:
            if x not in g2x2y[g]:
                mean_ydata.append(0)
                std_ydata.append(0)
            else:
                ylist = g2x2y[g][x] + [0] * (numsam - len(g2x2y[g][x]))
                mean_ydata.append(np.mean(ylist))
                std_ydata.append(np.std(ylist))
        if not bar:
            line, = axes.plot(xdata, mean_ydata, color=g2color[g], marker='o',
                              markeredgecolor=g2color[g], linestyle='-')
            axes.errorbar(xdata, mean_ydata, yerr=std_ydata, color=g2color[g],
                          linestyle='None', marker='None')
            lines.append(line)
        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], ecolor=g2color[g])
            lines.append(line[0])
        linenames.append(g)
    if bar: 
        xticks = [x + 0.325 for x in xdata]
        xlabels = [str(x) for x in xdata]
        drawcommon.set_xticks(axes, xticks, xlabels)
    axes.set_xlim(xmin=8, xmax=min(maxx, 30))
    axes.set_ylim(bottom=-0.005)
    drawcommon.set_legend(axes, lines, linenames)
    drawcommon.set_grid(axes)
    drawcommon.edit_spine(axes)
    drawcommon.set_labels(axes, xlabel='Length', ylabel='%% of total %s' % attr)
    drawcommon.write_image(fig, pdf, outfmt, outfile, dpi)