Ejemplo n.º 1
0
def plot_cmap_track(seg_placements, total_length, unadj_bar_height, color, seg_id_labels=False):
    cycle_label_locs = defaultdict(list)
    for ind, segObj in seg_placements.items():
        bar_height = unadj_bar_height + segObj.track_height_shift
        print("cmap_plot", segObj.id)
        start_angle, end_angle = start_end_angle(segObj.abs_end_pos, segObj.abs_start_pos, total_length)
        patches.append(mpatches.Wedge((0, 0), bar_height + bar_width, end_angle, start_angle, width=bar_width))
        f_color_v.append(color)
        e_color_v.append('k')
        lw_v.append(0)

        linewidth = min(0.25 * 2000000 / total_length, 0.25)
        for i in segObj.label_posns:
            if i > segObj.abs_end_pos or i < segObj.abs_start_pos:
                continue

            label_rads = i / total_length * 2 * np.pi
            x, y = vu.pol2cart(bar_height, label_rads)
            x_t, y_t = vu.pol2cart(bar_height + bar_width, label_rads)
            # linewidth = min(0.2*2000000/total_length,0.2)
            ax.plot([x, x_t], [y, y_t], color='k', alpha=0.9, linewidth=linewidth)

        if seg_id_labels:
            mid_sp = (segObj.abs_end_pos + segObj.abs_start_pos) / 2
            text_angle = mid_sp / total_length * 360.
            x, y = vu.pol2cart(bar_height - 1.2, (text_angle / 360. * 2. * np.pi))
            text_angle, ha = vu.correct_text_angle(text_angle)
            text = segObj.id + segObj.direction
            ax.text(x, y, text, color='grey', rotation=text_angle,
                    ha=ha, fontsize=5, rotation_mode='anchor')

    return cycle_label_locs
Ejemplo n.º 2
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')
Ejemplo n.º 3
0
def plot_bpg_connection(ref_placements, prev_seg_index_is_adj, bpg_dict,
                        seg_end_pos_d):
    connect_width = bar_width / 2.
    for ind, refObj in ref_placements.items():
        next_ind = (ind + 1) % len(ref_placements)
        next_refObj = ref_placements[next_ind]
        if not prev_seg_index_is_adj[
                next_ind]:  # or next_ind == 0 to try and close
            bpg_adjacency = vu.pair_is_edge(refObj.id, next_refObj.id,
                                            refObj.direction,
                                            next_refObj.direction, bpg_dict,
                                            seg_end_pos_d)

            if not bpg_adjacency or ind == len(ref_placements) - 1:
                continue

            bpg_connector_len = next_refObj.abs_start_pos - refObj.abs_end_pos
            # makes the reference genome wedges
            patches.append(
                mpatches.Rectangle(
                    (refObj.abs_end_pos, ref_bar_height + bar_width / 4.),
                    bpg_connector_len, connect_width))
            f_color_v.append('grey')
            e_color_v.append('grey')
            lw_v.append(0.2)
Ejemplo n.º 4
0
def construct_path_ref_placements(path,
                                  segSeqD,
                                  raw_path_length,
                                  prev_seg_index_is_adj,
                                  next_seg_index_is_adj,
                                  cycle_seg_counts,
                                  aln_vect=None):
    if aln_vect is None:
        aln_vect = []

    spacing_bp = seg_spacing * raw_path_length
    path_ref_placements = {}
    curr_start = 0.0
    for ind, i in enumerate(path):
        seg_id_count = cycle_seg_counts[i[0]]
        seg_len = segSeqD[i[0]][2] - segSeqD[i[0]][1]
        seg_end = curr_start + seg_len
        padj, nadj = prev_seg_index_is_adj[ind], next_seg_index_is_adj[ind]
        curr_obj = vu.CycleVizElemObj(i[0], segSeqD[i[0]][0], segSeqD[i[0]][1],
                                      segSeqD[i[0]][2], i[1], curr_start,
                                      seg_end, seg_id_count, padj, nadj)

        path_ref_placements[ind] = curr_obj
        next_start = seg_end
        mod_ind = (ind + 1) % (len(prev_seg_index_is_adj))
        if not prev_seg_index_is_adj[mod_ind]:
            next_start += spacing_bp

        curr_start = next_start

    total_length = next_start
    return path_ref_placements, total_length
Ejemplo n.º 5
0
def plot_bpg_connection(ref_placements, cycle, total_length,
                        prev_seg_index_is_adj, bpg_dict, seg_end_pos_d):
    connect_width = bar_width / 2.
    for ind, refObj in ref_placements.items():
        next_ind = (ind + 1) % len(ref_placements)
        next_refObj = ref_placements[next_ind]
        if not prev_seg_index_is_adj[
                next_ind]:  # or next_ind == 0 to try and close
            bpg_adjacency = vu.pair_is_edge(refObj.id, next_refObj.id,
                                            refObj.direction,
                                            next_refObj.direction, bpg_dict,
                                            seg_end_pos_d)

            if not bpg_adjacency:
                continue

            start_angle, end_angle = start_end_angle(next_refObj.abs_start_pos,
                                                     refObj.abs_end_pos,
                                                     total_length)
            # makes the reference genome wedges
            patches.append(
                mpatches.Wedge((0, 0),
                               outer_bar - bar_width / 4,
                               end_angle,
                               start_angle,
                               width=connect_width))
            f_color_v.append('grey')
            e_color_v.append('grey')
            lw_v.append(0.2)
Ejemplo n.º 6
0
def plot_alignment(contig_locs, segment_locs, total_length):
    segs_base = outer_bar + segment_bar_height
    linewidth = min(0.25 * 2000000 / total_length, 0.25)
    for a_d in aln_vect:
        c_id = a_d["contig_id"]
        c_num_dir = int(a_d["contig_dir"] + "1")

        contig_label_vect = contig_locs[c_id].label_posns
        seg_label_vect = segment_locs[a_d["seg_aln_number"]].label_posns
        c_l_pos = contig_label_vect[a_d["contig_label"] - 1]
        c_l_loc = c_l_pos / total_length * 2. * np.pi
        # s_l_pos = seg_label_vect[s_num_dir*a_d["seg_label"]-(s_num_dir+1)/2]
        s_l_pos = seg_label_vect[a_d["seg_label"] - 1]
        s_l_loc = s_l_pos / total_length * 2. * np.pi
        contig_top = outer_bar + contig_bar_height + contig_locs[c_id].track_height_shift + bar_width
        x_c, y_c = vu.pol2cart(contig_top, c_l_loc)
        x_s, y_s = vu.pol2cart(segs_base, s_l_loc)
        ax.plot([x_c, x_s], [y_c, y_s], color="grey", linewidth=linewidth)
Ejemplo n.º 7
0
def plot_gene_track(currStart, relGenes, pTup, total_length, strand):
    for ind,i in enumerate(relGenes):
        truncStart = False
        truncEnd = False
        #e_posns is a list of tuples of exon (start,end)
        #these can be plotted similarly to how the coding region is marked
        tstart,tend,e_posns = relGenes[i]
        seg_len = pTup[2] - pTup[1]
        if strand == "+":
            normStart = currStart + max(0,tstart-pTup[1])
            normEnd = currStart + min(seg_len,tend-pTup[1])
        else:
            normEnd = currStart + min(seg_len,pTup[2]-tstart)
            normStart = currStart + max(0,pTup[2] - tend)

        # print max(0,tstart-pTup[1]),min(seg_len,tend-pTup[1]),seg_len
        # print i,normStart,normEnd, currStart, currStart+seg_len,tstart,tend,strand

        start_angle = normStart/total_length*360
        end_angle = normEnd/total_length*360
        text_angle = (start_angle + end_angle)/2.0
        gene_to_locations[i].append((start_angle/360.,end_angle/360.))
        if end_angle < 0 and start_angle > 0:
            end_angle+=360
        
        patches.append(mpatches.Wedge((0,0), outer_bar, start_angle, end_angle, width=bar_width/2.0))
        f_color_v.append('k')
        e_color_v.append('k')
        lw_v.append(0)

        # x,y = pol2cart(outer_bar+(bar_width/2.0),(text_angle/360*2*np.pi))
        x_t,y_t = pol2cart(outer_bar + bar_width + 2,(text_angle/360*2*np.pi))
        #ax.plot([x,x_t],[y,y_t],color='grey',linewidth=0.4)
        
        text_angle,ha = vu.correct_text_angle(text_angle)
        ax.text(x_t,y_t,i,color='k',rotation=text_angle,ha=ha,va="center",fontsize=9,rotation_mode='anchor')

        for exon in e_posns:
            if exon[1] > pTup[1] and exon[0] < pTup[2]:
                if strand == "+":
                    normStart = currStart + max(1,exon[0]-pTup[1])
                    normEnd = currStart + min(pTup[2]-pTup[1],exon[1]-pTup[1])

                else:
                    normEnd = currStart + min(pTup[2]-pTup[1],pTup[2]-exon[0])
                    normStart = currStart + max(1,pTup[2] - exon[1])

                start_angle, end_angle = start_end_angle(normStart,normEnd,total_length)
                patches.append(mpatches.Wedge((0,0), outer_bar-bar_width/2.0, start_angle, end_angle, width=bar_width/2.0))
                f_color_v.append('k')
                e_color_v.append('k')
                lw_v.append(0)
Ejemplo n.º 8
0
def construct_cycle_ref_placements(cycle,segSeqD,raw_cycle_length,prev_seg_index_is_adj,isCycle,aln_vect = []):
    spacing_bp = seg_spacing*raw_cycle_length
    cycle_ref_placements = {}
    curr_start = 0.0 if isCycle else spacing_bp
    for ind,i in enumerate(cycle):
        seg_len = segSeqD[i[0]][2] - segSeqD[i[0]][1]
        seg_end = curr_start+seg_len
        curr_obj = vu.CycleVizElemObj(i[0],i[1],curr_start,seg_end)
        cycle_ref_placements[ind] = curr_obj
        next_start = seg_end
        mod_ind = (ind+1) % (len(prev_seg_index_is_adj))
        if not prev_seg_index_is_adj[mod_ind]:
            next_start+=spacing_bp

        curr_start = next_start

    total_length = next_start
    return cycle_ref_placements,total_length
Ejemplo n.º 9
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')
Ejemplo n.º 10
0
    args.ref = "hg38"

if not args.outname:
    args.outname = os.path.split(args.cycles_file)[1].split(".")[0]

outdir = os.path.dirname(args.outname)
if outdir and not os.path.exists(outdir):
    os.makedirs(outdir)

fname = args.outname + "_path_" + str(args.path) + "_trim_" + str(
    args.reduce_path[0]) + "_" + str(args.reduce_path[1])

print(args.reduce_path, "path reduction (L, R)")

print("Reading genes")
gene_tree = vu.parse_genes(args.ref, [])

print("Unaligned fraction cutoff set to " + str(vu.unaligned_cutoff_frac))

chromosome_colors = vu.get_chr_colors()
plt.clf()
fig, ax = plt.subplots(figsize=(10, 6))
patches = []
f_color_v = []
e_color_v = []
lw_v = []

paths, segSeqD, circular_D = vu.parse_cycles_file(args.cycles_file)
path_num = args.path
path = paths[path_num]
Ejemplo n.º 11
0
def plot_gene_track(currStart, currEnd, relGenes, pTup, total_length, seg_dir):
    overlap_genes.append({})
    for gObj in relGenes:
        # e_posns is a list of tuples of exon (start,end)
        # these can be plotted similarly to how the coding region is marked
        gname, gstart, gend, e_posns = gObj.gname, gObj.gstart, gObj.gend, gObj.eposns
        seg_len = pTup[2] - pTup[1]
        if seg_dir == "+":
            normStart = currStart + max(0, gstart - pTup[1])
            normEnd = currStart + min(seg_len, gend - pTup[1])
        else:
            normEnd = currStart + min(seg_len, pTup[2] - gstart)
            normStart = currStart + max(0, pTup[2] - gend)

        start_angle = normStart / total_length * 360
        end_angle = normEnd / total_length * 360
        text_angle = (start_angle + end_angle) / 2.0
        gene_to_locations[gname].append((start_angle / 360., end_angle / 360.))
        if end_angle < 0 and start_angle > 0:
            end_angle += 360

        patches.append(
            mpatches.Wedge((0, 0),
                           outer_bar,
                           start_angle,
                           end_angle,
                           width=bar_width / 2.0))
        f_color_v.append('k')
        e_color_v.append('k')
        lw_v.append(0)

        x_t, y_t = pol2cart(outer_bar + bar_width + 2,
                            (text_angle / 360 * 2 * np.pi))
        # Use filtering from LinearViz to control which names are printed:
        '''
        if i not in plotted_gene_names or args.print_dup_genes:
            print i
        '''
        text_angle, ha = vu.correct_text_angle(text_angle)
        if gname not in overlap_genes[len(overlap_genes) -
                                      2] or gstart > overlap_genes[
                                          len(overlap_genes) - 2].get(gname):
            ax.text(x_t,
                    y_t,
                    gname,
                    style='italic',
                    color='k',
                    rotation=text_angle,
                    ha=ha,
                    va="center",
                    fontsize=gene_fontsize,
                    rotation_mode='anchor')

        if currEnd < gend:
            overlap_genes[len(overlap_genes) - 1][gname] = gend

        for exon in e_posns:
            if exon[1] > pTup[1] and exon[0] < pTup[2]:
                if seg_dir == "+":
                    normStart = currStart + max(1, exon[0] - pTup[1])
                    normEnd = currStart + min(pTup[2] - pTup[1],
                                              exon[1] - pTup[1])

                else:
                    normEnd = currStart + min(pTup[2] - pTup[1],
                                              pTup[2] - exon[0])
                    normStart = currStart + max(1, pTup[2] - exon[1])

                start_angle, end_angle = start_end_angle(
                    normStart, normEnd, total_length)
                patches.append(
                    mpatches.Wedge((0, 0),
                                   outer_bar - bar_width / 2.0,
                                   start_angle,
                                   end_angle,
                                   width=bar_width / 2.0))
                f_color_v.append('k')
                e_color_v.append('k')
                lw_v.append(0)
Ejemplo n.º 12
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')
Ejemplo n.º 13
0
def plot_gene_track(currStart, currEnd, relGenes, pTup, total_length, seg_dir, ind, plot_gene_direction=True):
    overlap_genes.append({})
    for gObj in relGenes:
        # e_posns is a list of tuples of exon (start,end)
        # these can be plotted similarly to how the coding region is marked
        gname, gstart, gend, e_posns = gObj.gname, gObj.gstart, gObj.gend, gObj.eposns
        # print(gname, gstart, gend, pTup, len(gObj.gdrops))
        seg_len = pTup[2] - pTup[1]
        hasStart = False
        hasEnd = False
        if seg_dir == "+":
            ts = max(0, gstart - pTup[1])
            te = min(seg_len, gend - pTup[1])
            if gObj.strand == "+":
                drop = 1.4 * bar_width
                if ts > 0: hasStart = True
                if te < seg_len: hasEnd = True
            else:
                drop = 2 * bar_width
                if ts > 0: hasEnd = True
                if te < seg_len: hasStart = True

            normStart = currStart + max(0, gstart - pTup[1])
            normEnd = currStart + min(seg_len, gend - pTup[1])

        else:
            te = min(seg_len, pTup[2] - gstart)
            ts = max(0, pTup[2] - gend)
            if gObj.strand == "+":
                drop = 2 * bar_width
                if te < seg_len: hasStart = True
                if ts > 0: hasEnd = True
            else:
                drop = 1.4 * bar_width
                if te < seg_len: hasEnd = True
                if ts > 0: hasStart = True

            normEnd = currStart + min(seg_len, pTup[2] - gstart)
            normStart = currStart + max(0, pTup[2] - gend)

        start_angle = normStart / total_length * 360
        end_angle = normEnd / total_length * 360
        text_angle = (start_angle + end_angle) / 2.0
        gene_to_locations[gname].append((start_angle / 360., end_angle / 360.))
        if end_angle < 0 and start_angle > 0:
            end_angle += 360

        patches.append(mpatches.Wedge((0, 0), outer_bar, start_angle, end_angle, width=bar_width / 2.0))
        f_color_v.append('k')
        e_color_v.append('k')
        lw_v.append(0)

        if gname not in overlap_genes[len(overlap_genes)-2] or gstart > overlap_genes[len(overlap_genes)-2].get(gname)[0]\
                or seg_dir != overlap_genes[len(overlap_genes)-2].get(gname)[1]:
            x_t, y_t = vu.pol2cart(outer_bar + bar_width + 1.7, (text_angle / 360 * 2 * np.pi))
            text_angle, ha = vu.correct_text_angle(text_angle)

            if gObj.highlight_name:
                ax.text(x_t, y_t, gname, style='italic', color='r', rotation=text_angle, ha=ha, va="center",
                        fontsize=gene_fontsize, rotation_mode='anchor')
            else:
                ax.text(x_t, y_t, gname, style='italic', color='k', rotation=text_angle, ha=ha, va="center",
                        fontsize=gene_fontsize, rotation_mode='anchor')

        # draw something to show direction and truncation status
        if plot_gene_direction:
            plot_gene_direction_indicator(normStart, normEnd, drop)
            gObj.gdrops.append((normStart, normEnd, total_length, seg_dir, currStart, currEnd, hasStart, hasEnd, ind, drop, pTup))
            # gObj.gdrops = [(normStart, normEnd, total_length, seg_dir, currStart, currEnd, pTup), ]

        if currEnd < gend:
            overlap_genes[len(overlap_genes)-1][gname] = (gend, seg_dir)

        for exon in e_posns:
            #fix exon orientation
            if exon[1] > pTup[1] and exon[0] < pTup[2]:
                if seg_dir == "+":
                    normStart = currStart + max(1, exon[0] - pTup[1])
                    normEnd = currStart + min(pTup[2] - pTup[1], exon[1] - pTup[1])

                else:
                    normEnd = currStart + min(pTup[2] - pTup[1], pTup[2] - exon[0])
                    normStart = currStart + max(1, pTup[2] - exon[1])

                start_angle, end_angle = start_end_angle(normStart, normEnd, total_length)
                patches.append(
                    mpatches.Wedge((0, 0), outer_bar - bar_width / 2.0, start_angle, end_angle, width=bar_width / 2.0))
                f_color_v.append('k')
                e_color_v.append('k')
                lw_v.append(0)
Ejemplo n.º 14
0
parser.add_argument("--sname", help="output prefix")
parser.add_argument("--rot", help="number of segments to rotate counterclockwise", type=int, default=0)
parser.add_argument("--label_segs", help="label segs with graph IDs", action='store_true')
parser.add_argument("--gene_subset_file", help="file containing subset of genes to plot (e.g. oncogene genelist file)",
                    default="")
parser.add_argument("--gene_subset_list", help="list of genes to plot (e.g. MYC PVT1)", nargs="+", type=str)
parser.add_argument("--print_dup_genes", help="if a gene appears multiple times print name every time.",
                    action='store_true', default=False)
parser.add_argument("--gene_highlight_list", help="list of gene names to highlight", nargs="+", type=str, default=[])
parser.add_argument("--gene_fontsize", help="font size for gene names", type=float, default=7)
parser.add_argument("--tick_fontsize", help="font size for genomic position ticks", type=float, default=7)
parser.add_argument("--bedgraph", help="bedgraph file specifying additional data")

args = parser.parse_args()
if args.yaml_file:
    args = vu.parse_yaml(args)
if args.ref == "GRCh38":
    args.ref = "hg38"

print(args.ref)

if not args.sname:
    args.sname = os.path.split(args.cycles_file)[1].split(".")[0] + "_"

outdir = os.path.dirname(args.sname)
if outdir and not os.path.exists(outdir):
    os.makedirs(outdir)

fname = args.sname + "cycle_" + args.cycle

print("Reading genes")