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
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)
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)
def plot_ref_genome(ref_placements, cycle, total_length, segSeqD, imputed_status, label_segs, onco_set=None): if onco_set is None: 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]] start_angle, end_angle = start_end_angle(refObj.abs_end_pos, refObj.abs_start_pos, total_length) # 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 = int((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 = vu.pol2cart(outer_bar, (text_angle / 360 * 2 * np.pi)) x_t, y_t = vu.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)) all_relGenes.extend(relGenes) # plot the gene track # print(ind, refObj.to_string(), len(relGenes)) plot_gene_track(refObj.abs_start_pos, refObj.abs_end_pos, relGenes, seg_coord_tup, total_length, cycle[ind][1], ind) # 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 = vu.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')