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')
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]
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") gene_tree = vu.parse_genes(args.ref, args.gene_highlight_list) print("Unaligned fraction cutoff set to " + str(vu.unaligned_cutoff_frac)) chromosome_colors = vu.get_chr_colors() plt.clf() fig, ax = plt.subplots() patches = [] f_color_v = [] e_color_v = [] lw_v = [] cycles, segSeqD, circular_D = vu.parse_cycles_file(args.cycles_file) cycle_num = args.cycle isCycle = circular_D[cycle_num] cycle = cycles[cycle_num] prev_seg_index_is_adj = vu.adjacent_segs(cycle, segSeqD, isCycle)
def plot_ref_genome(ref_placements, path, total_length, segSeqD, imputed_status, label_segs, onco_set=set()): font0 = FontProperties() p_end = 0 for ind, refObj in ref_placements.iteritems(): 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_base, 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] 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 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 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 # TODO: IMPLEMENT plot_gene_track(refObj.abs_start_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_base-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') ax.text(mid_sp, ref_bar_height + 0.25 * bar_width, path[ind][0] + path[ind][1], fontsize=8, fontproperties=font)