Exemplo n.º 1
0
def plot_ref_genome(ref_placements,cycle,total_length,segSeqD,imputed_status,label_segs,onco_set=set()):
    font0 = FontProperties()
    rot_sp = global_rot/360.*total_length
    for ind,refObj in ref_placements.iteritems():
        seg_coord_tup = segSeqD[cycle[ind][0]]
        # print(refObj.to_string())
        start_angle, end_angle = start_end_angle(refObj.abs_end_pos,refObj.abs_start_pos,total_length)
        # print start_angle,end_angle
        
        #makes the reference genome wedges    
        patches.append(mpatches.Wedge((0,0), outer_bar, end_angle, start_angle, width=bar_width))
        chrom = segSeqD[cycle[ind][0]][0]
        f_color_v.append(chromosome_colors[chrom])
        e_color_v.append('grey')
        lw_v.append(0.2)
        
        #makes the ticks on the reference genome wedges
        if cycle[ind][1] == "+":
            # posns = zip(range(seg_coord_tup[1],seg_coord_tup[2]+1),np.arange(refObj.abs_end_pos,refObj.abs_start_pos,-1))
            posns = zip(range(seg_coord_tup[1],seg_coord_tup[2]+1),np.arange(refObj.abs_start_pos,refObj.abs_end_pos))
        else:
            # posns = zip(np.arange(seg_coord_tup[2],seg_coord_tup[1]-1,-1),np.arange(refObj.abs_end_pos,refObj.abs_start_pos,-1))
            posns = zip(np.arange(seg_coord_tup[2],seg_coord_tup[1]-1,-1),np.arange(refObj.abs_start_pos,refObj.abs_end_pos))

        tick_freq = max(30000,30000*int(np.floor(total_length/1000000)))
        if refObj.abs_end_pos-refObj.abs_start_pos < 30000:
            tick_freq = 10000
        for j in posns:
            if j[0] % tick_freq == 0:
                text_angle = j[1]/total_length*360
                x,y = pol2cart(outer_bar,(text_angle/360*2*np.pi))
                x_t,y_t = pol2cart(outer_bar + 0.2,(text_angle/360*2*np.pi))
                ax.plot([x,x_t],[y,y_t],color='grey',linewidth=1)
                
                text_angle,ha = vu.correct_text_angle(text_angle)
                txt = " " + str(int(round((j[0])/10000))) if ha == "left" else str(int(round((j[0])/10000))) + " "

                ax.text(x_t,y_t,txt,color='grey',rotation=text_angle,
                ha=ha,va="center",fontsize=9,rotation_mode='anchor')
    
        gene_tree = vu.parse_genes(seg_coord_tup[0],args.ref)
        relGenes = vu.rel_genes(gene_tree,seg_coord_tup,copy.copy(onco_set))
        #plot the gene track
        plot_gene_track(refObj.abs_start_pos,relGenes,seg_coord_tup,total_length,cycle[ind][1])

        #label the segments by number in cycle
        mid_sp = (refObj.abs_end_pos + refObj.abs_start_pos)/2
        text_angle = mid_sp/total_length*360.
        x,y = pol2cart((outer_bar-2*bar_width),(text_angle/360.*2.*np.pi))
        font = font0.copy()
        if imputed_status[ind]:
            font.set_style('italic')
            # font.set_weight('bold')

        text_angle,ha = vu.correct_text_angle(text_angle)

        if label_segs:
            ax.text(x,y,cycle[ind][0]+cycle[ind][1],color='grey',rotation=text_angle,
                ha=ha,fontsize=5,fontproperties=font,rotation_mode='anchor')
Exemplo n.º 2
0
def plot_ref_genome(ref_placements,
                    path,
                    total_length,
                    segSeqD,
                    imputed_status,
                    label_segs,
                    color_map,
                    onco_set=None):
    if onco_set is None:
        onco_set = set()
    font0 = FontProperties()
    p_end = 0
    for ind, refObj in ref_placements.items():
        print(ind, refObj.to_string(), ref_bar_height)
        seg_coord_tup = segSeqD[path[ind][0]]
        # print(refObj.to_string())
        # start_angle, end_angle = start_end_angle(refObj.abs_end_pos,refObj.abs_start_pos,total_length)
        box_len = refObj.abs_end_pos - refObj.abs_start_pos
        # print start_angle,end_angle

        # makes the reference genome wedges
        # patches.append(mpatches.Wedge((0,0), seg_bar_height, end_angle, start_angle, width=bar_width))
        patches.append(
            mpatches.Rectangle((refObj.abs_start_pos, ref_bar_height), box_len,
                               bar_width))
        chrom = segSeqD[path[ind][0]][0]
        if color_map == "standard":
            try:
                c_col = chromosome_colors[chrom]
            except KeyError:
                print("Color not found for " + chrom + ". Using red.")
                chromosome_colors[chrom] = "red"
                c_col = chromosome_colors[chrom]

        else:
            c_col = color_map(float(refObj.id) / len(segSeqD))

        f_color_v.append(c_col)
        e_color_v.append(c_col)
        lw_v.append(0.2)

        # makes the ticks on the reference genome wedges
        if path[ind][1] == "+":
            # posns = zip(range(seg_coord_tup[1],seg_coord_tup[2]+1),np.arange(refObj.abs_end_pos,refObj.abs_start_pos,-1))
            posns = zip(range(seg_coord_tup[1], seg_coord_tup[2] + 1),
                        np.arange(refObj.abs_start_pos, refObj.abs_end_pos))
        else:
            # posns = zip(np.arange(seg_coord_tup[2],seg_coord_tup[1]-1,-1),np.arange(refObj.abs_end_pos,refObj.abs_start_pos,-1))
            posns = zip(np.arange(seg_coord_tup[2], seg_coord_tup[1] - 1, -1),
                        np.arange(refObj.abs_start_pos, refObj.abs_end_pos))

        tick_freq = max(40000, 80000 * int(np.floor(total_length / 1000000)))
        # segment too small, nothing gets ticked
        if (not any(j[0] % tick_freq == 0
                    for j in posns)) and abs(refObj.abs_start_pos - p_end) > 1:
            tens = [j[0] for j in posns if j[0] % 10000 == 0]
            middleIndex = (len(tens) - 1) // 2  # do integer division
            if tens:
                tick_freq = tens[middleIndex]
            else:
                tick_freq = 10000

        for j in posns:
            if j[0] % tick_freq == 0:
                x_i, y_i = j[1], ref_bar_height
                x_f, y_f = j[1], ref_bar_height - bar_width * 0.3
                ax.plot([x_i, x_f], [y_i, y_f], color='grey', linewidth=1)
                txt = " " + str(int(round(
                    (j[0]) / 10000
                )))  # if ha == "left" else str(int(round((j[0])/10000))) + " "
                # txt = str(j[0])
                x_t, y_t = j[1], ref_bar_height - bar_width * 0.4
                ax.text(x_t,
                        y_t,
                        txt,
                        color='grey',
                        rotation=-90,
                        rotation_mode="anchor",
                        ha="left",
                        va="center",
                        fontsize=12)

        p_end = refObj.abs_end_pos
        # gene_tree = vu.parse_genes(seg_coord_tup[0], args.ref)
        relGenes = vu.rel_genes(gene_tree, seg_coord_tup, copy.copy(onco_set))

        # plot the gene track
        plot_gene_track(refObj.abs_start_pos, refObj.abs_end_pos, relGenes,
                        seg_coord_tup, total_length, path[ind][1])

        # label the segments by number in path
        mid_sp = (refObj.abs_end_pos + refObj.abs_start_pos) / 2
        # text_angle = mid_sp/total_length*360.
        # x,y = pol2cart((seg_bar_height-2*bar_width),(text_angle/360.*2.*np.pi))
        font = font0.copy()
        if imputed_status[ind]:
            font.set_style('italic')
        #     # font.set_weight('bold')

        # text_angle,ha = vu.correct_text_angle(text_angle)

        if label_segs:
            #     ax.text(x,y,cycle[ind][0]+cycle[ind][1],color='grey',rotation=text_angle,
            #         ha=ha,fontsize=5,fontproperties=font,rotation_mode='anchor')
            label_text = path[ind][1]
            if label_segs == "id":
                label_text = path[ind][0] + label_text

            ax.text(mid_sp,
                    ref_bar_height + 0.25 * bar_width,
                    label_text,
                    fontsize=8,
                    fontproperties=font,
                    ha='center')
Exemplo n.º 3
0
def plot_ref_genome(ref_placements,
                    cycle,
                    total_length,
                    segSeqD,
                    imputed_status,
                    label_segs,
                    onco_set=set()):
    font0 = FontProperties()
    p_end = 0
    # rot_sp = global_rot / 360. * total_length
    for ind, refObj in ref_placements.items():
        seg_coord_tup = segSeqD[cycle[ind][0]]
        # print(refObj.to_string())
        start_angle, end_angle = start_end_angle(refObj.abs_end_pos,
                                                 refObj.abs_start_pos,
                                                 total_length)
        # print start_angle,end_angle

        # makes the reference genome wedges
        patches.append(
            mpatches.Wedge((0, 0),
                           outer_bar,
                           end_angle,
                           start_angle,
                           width=bar_width))
        chrom = segSeqD[cycle[ind][0]][0]
        try:
            f_color_v.append(chromosome_colors[chrom])
        except KeyError:
            print("Color not found for " + chrom + ". Using red.")
            chromosome_colors[chrom] = "red"
            f_color_v.append("red")

        e_color_v.append('grey')
        lw_v.append(0.2)

        # makes the ticks on the reference genome wedges
        if cycle[ind][1] == "+":
            posns = zip(range(seg_coord_tup[1], seg_coord_tup[2] + 1),
                        np.arange(refObj.abs_start_pos, refObj.abs_end_pos))
        else:
            posns = zip(np.arange(seg_coord_tup[2], seg_coord_tup[1] - 1, -1),
                        np.arange(refObj.abs_start_pos, refObj.abs_end_pos))

        tick_freq = max(10000, 30000 * int(np.floor(total_length / 800000)))
        #if refObj.abs_end_pos - refObj.abs_start_pos < 30000:
        #tick_freq = 25000

        # if there are no labels present on the segment given the current frequency, AND this refobject is not adjacent
        # to the previous, get positions in this segment divisible by 10kbp, set the middle one as the labelling site
        # else just set it to 10000
        if (not any(j[0] % tick_freq == 0
                    for j in posns)) and abs(refObj.abs_start_pos - p_end) > 1:
            tens = [j[0] for j in posns if j[0] % 10000 == 0]
            middleIndex = (len(tens) - 1) / 2
            if tens:
                tick_freq = tens[middleIndex]
            else:
                tick_freq = 10000

        p_end = refObj.abs_end_pos

        print("tick freq", tick_freq)
        for j in posns:
            if j[0] % tick_freq == 0:
                text_angle = j[1] / total_length * 360
                x, y = pol2cart(outer_bar, (text_angle / 360 * 2 * np.pi))
                x_t, y_t = pol2cart(outer_bar + 0.2,
                                    (text_angle / 360 * 2 * np.pi))
                ax.plot([x, x_t], [y, y_t], color='grey', linewidth=1)

                text_angle, ha = vu.correct_text_angle(text_angle)
                txt = " " + str(int(round(
                    (j[0]) / 10000))) if ha == "left" else str(
                        int(round((j[0]) / 10000))) + " "

                ax.text(x_t,
                        y_t,
                        txt,
                        color='grey',
                        rotation=text_angle,
                        ha=ha,
                        va="center",
                        fontsize=tick_fontsize,
                        rotation_mode='anchor')

        # gene_tree = vu.parse_genes(seg_coord_tup[0], args.ref)
        relGenes = vu.rel_genes(gene_tree, seg_coord_tup, copy.copy(onco_set))
        # plot the gene track
        plot_gene_track(refObj.abs_start_pos, refObj.abs_end_pos, relGenes,
                        seg_coord_tup, total_length, cycle[ind][1])

        # label the segments by number in cycle
        mid_sp = (refObj.abs_end_pos + refObj.abs_start_pos) / 2
        text_angle = mid_sp / total_length * 360.
        x, y = pol2cart((outer_bar - 2 * bar_width),
                        (text_angle / 360. * 2. * np.pi))
        font = font0.copy()
        if imputed_status[ind]:
            font.set_style('italic')
            # font.set_weight('bold')

        text_angle, ha = vu.correct_text_angle(text_angle)

        if label_segs:
            ax.text(x,
                    y,
                    cycle[ind][0] + cycle[ind][1],
                    color='grey',
                    rotation=text_angle,
                    ha=ha,
                    fontsize=5,
                    fontproperties=font,
                    rotation_mode='anchor')