def plot_density_from_file(settings_f, pickle_filename, event, output_dir, no_posteriors=False, plot_title=None, plot_label=None): """ Read MISO estimates given an event name. """ ## ## Read information about gene ## tx_start, tx_end, exon_starts, exon_ends, gene_obj, mRNAs, strand, chrom = \ parseGene(pickle_filename, event) # check the output format is_png = False if plot_label is not None: if plot_label[-4:] == ".png": is_png = True plot_label = plot_label[:-4] elif plot_label[-4:] == ".pdf": plot_label = plot_label[:-4] # Override settings flag on whether to show posterior plots # if --no-posteriors was given to plot.py sashimi_obj = Sashimi(event, output_dir, event=event, chrom=chrom, settings_filename=settings_f, no_posteriors=no_posteriors, png=is_png) print "Plotting read densities and MISO estimates along event..." print " - Event: %s" % (event) settings = sashimi_obj.settings if no_posteriors: settings["show_posteriors"] = False bam_files = settings['bam_files'] miso_files = settings['miso_files'] # Setup the figure sashimi_obj.setup_figure() plot_density(sashimi_obj, pickle_filename, event, plot_title=plot_title) # Save figure sashimi_obj.save_plot(plot_label=plot_label)
def plot_density_from_file(settings_f, pickle_filename, event, output_dir, no_posteriors=False, plot_title=None, plot_label=None): """ Read MISO estimates given an event name. """ ## ## Read information about gene ## tx_start, tx_end, exon_starts, exon_ends, gene_obj, mRNAs, strand, chrom = \ parseGene(pickle_filename, event) #chrom = str(chrom)[3:] # Override settings flag on whether to show posterior plots # if --no-posteriors was given to plot.py sashimi_obj = Sashimi(event, output_dir, event=event, chrom=chrom, settings_filename=settings_f, no_posteriors=no_posteriors) #print "Plotting read densities and MISO estimates along event..." #print " - Event: %s" %(event) settings = sashimi_obj.settings if no_posteriors: settings["show_posteriors"] = False bam_files = settings['bam_files'] miso_files = settings['miso_files'] # Setup the figure sashimi_obj.setup_figure() plot_density(sashimi_obj, pickle_filename, event, plot_title=plot_title) # Save figure sashimi_obj.save_plot(plot_label=plot_label)
def plot_density(sashimi_obj, pickle_filename, event, plot_title=None, group_info=None): # intron_scale=30, exon_scale=1, gene_posterior_ratio=5, posterior_bins=40, # colors=None, ymax=None, logged=False, show_posteriors=True, coverages=None, # number_junctions=True, resolution=.5, fig_width=8.5, fig_height=11, # font_size=6, junction_log_base=10, reverse_minus=False, # bar_posterior=False): # Get the settings we need settings = sashimi_obj.settings bam_files = settings["bam_files"] miso_files = settings["miso_files"] intron_scale = settings["intron_scale"] exon_scale = settings["exon_scale"] gene_posterior_ratio = settings["gene_posterior_ratio"] posterior_bins = settings["posterior_bins"] colors = settings["colors"] ymax = settings["ymax"] logged = settings["logged"] show_posteriors = settings["show_posteriors"] coverages = settings["coverages"] number_junctions = settings["number_junctions"] resolution = settings["resolution"] junction_log_base = settings["junction_log_base"] reverse_minus = settings["reverse_minus"] bar_posterior = settings["bar_posteriors"] font_size = settings["font_size"] nyticks = settings["nyticks"] nxticks = settings["nxticks"] show_ylabel = settings["show_ylabel"] show_xlabel = settings["show_xlabel"] if plot_title is None: plot_title = event print "Using intron scale ", intron_scale print "Using exon scale ", exon_scale # Always show y-axis for read densities for now showYaxis = True # Parse gene pickle file to get information about gene tx_start, tx_end, exon_starts, exon_ends, gene_obj, mRNAs, strand, chrom = \ parseGene(pickle_filename, event) # Get the right scalings graphcoords, graphToGene = getScaling(tx_start, tx_end, strand, exon_starts, exon_ends, intron_scale, exon_scale, reverse_minus) if group_info is not None: group_files, group_labels, group_colors = analyze_group_info(group_info, bam_files, settings["sample_labels"]) settings["sample_labels"] = group_labels # if the group color is customized by the user if len(colors) != len(group_colors): print('\033[0;31;m') # change the print color as red print("The number of custom colors is {0} which doesn't match the group number {1}. The program uses the " "rainbow color as default.".format(len(colors), len(group_colors))) print('\033[0m') # set the color as default value colors = settings["colors"] = group_colors nfiles = len(group_files) else: nfiles = len(bam_files) group_files = [] # if the group_info is not provided, also produce a group_file = [['bam1'],['bam2'],...] for i in range(nfiles): group_files.append([bam_files[i]]) if plot_title is not None: # Use custom title if given suptitle(plot_title, fontsize=10) else: suptitle(event, fontsize=10) plotted_axes = [] maxys = np.zeros(nfiles) for i in range(nfiles): if colors is not None: color = colors[i] else: color = None if coverages is not None: coverage = coverages[i] else: coverage = 1 if i < nfiles - 1: showXaxis = False else: showXaxis = True bam_group = group_files[i] # ['./testData/S1.R1.test.bam','./testData/S1.R2.test.bam'] # bam_file = os.path.expanduser(bam_files[i]) ax1 = subplot2grid((nfiles + 3, gene_posterior_ratio), (i, 0), colspan=gene_posterior_ratio - 1) # Read sample label sample_label = settings["sample_labels"][i] print "Reading sample label: %s" %(sample_label) # print "Processing BAM: %s" %(bam_file) plotted_ax, maxy = plot_density_single(settings, sample_label, tx_start, tx_end, gene_obj, mRNAs, strand, graphcoords, graphToGene, bam_group, ax1, chrom, paired_end=False, intron_scale=intron_scale, exon_scale=exon_scale, color=color, ymax=ymax, logged=logged, coverage=coverage, number_junctions=number_junctions, resolution=resolution, showXaxis=showXaxis, nyticks=nyticks, nxticks=nxticks, show_ylabel=show_ylabel, show_xlabel=show_xlabel, font_size=font_size, junction_log_base=junction_log_base) plotted_axes.append(plotted_ax) maxys[i] = maxy if show_posteriors: miso_file = os.path.expanduser(miso_files[i]) try: ax2 = subplot2grid((nfiles + 3, gene_posterior_ratio),\ (i, gene_posterior_ratio - 1)) if not os.path.isfile(miso_file): print "Warning: MISO file %s not found" %(miso_file) print "Loading MISO file: %s" %(miso_file) plot_posterior_single(miso_file, ax2, posterior_bins, showXaxis=showXaxis, show_ylabel=False, font_size=font_size, bar_posterior=bar_posterior) except: box(on=False) xticks([]) yticks([]) print "Posterior plot failed." ## ## Figure out correct y-axis values ## ymax_vals = [] if ymax != None: # Use user-given ymax values if provided max_used_yval = ymax else: # Compute best ymax value for all samples: take # maximum y across all. used_yvals = [curr_ax.get_ylim()[1] for curr_ax in plotted_axes] # Round up max_used_yval = math.ceil(max(used_yvals)) # Reset axes based on this. # Set fake ymin bound to allow lower junctions to be visible fake_ymin = -0.6 * max_used_yval universal_yticks = linspace(0, max_used_yval, nyticks + 1) # Round up yticks universal_ticks = map(math.ceil, universal_yticks) for sample_num, curr_ax in enumerate(plotted_axes): if showYaxis: curr_ax.set_ybound(lower=fake_ymin, upper=max_used_yval) curr_yticklabels = [] for label in universal_yticks: if label <= 0: # Exclude label for 0 curr_yticklabels.append("") else: if label % 1 != 0: curr_yticklabels.append("%.1f" %(label)) else: curr_yticklabels.append("%d" %(label)) curr_ax.set_yticklabels(curr_yticklabels, fontsize=font_size) curr_ax.spines["left"].set_bounds(0, max_used_yval) curr_ax.set_yticks(universal_yticks) curr_ax.yaxis.set_ticks_position('left') curr_ax.spines["right"].set_color('none') if show_ylabel: y_horz_alignment = 'left' if logged: curr_ax.set_ylabel('RPKM $(\mathregular{\log}_{\mathregular{10}})$', fontsize=font_size, ha=y_horz_alignment) else: curr_ax.set_ylabel('RPKM', fontsize=font_size, va="bottom", ha=y_horz_alignment) else: curr_ax.spines["left"].set_color('none') curr_ax.spines["right"].set_color('none') curr.ax.set_yticks([]) ## ## Plot sample labels ## sample_color = colors[sample_num] # Make sample label y position be halfway between highest # and next to highest ytick if len(universal_yticks) >= 2: halfway_ypos = (universal_yticks[-1] - universal_yticks[-2]) / 2. label_ypos = universal_yticks[-2] + halfway_ypos else: label_ypos = universal_yticks[-1] curr_label = settings["sample_labels"][sample_num] curr_ax.text(max(graphcoords), max(label_ypos, maxys[sample_num]), curr_label, fontsize=font_size, va='bottom', ha='right', color=sample_color) # Draw gene structure ax = subplot2grid((nfiles + 3, gene_posterior_ratio), (nfiles + 1, 0), colspan=gene_posterior_ratio - 1, rowspan=2) plot_mRNAs(tx_start, mRNAs, strand, graphcoords, reverse_minus) subplots_adjust(hspace=.10, wspace=.7)
def plot_density(sashimi_obj, pickle_filename, event, plot_title=None): # intron_scale=30, exon_scale=1, gene_posterior_ratio=5, posterior_bins=40, # colors=None, ymax=None, logged=False, show_posteriors=True, coverages=None, # number_junctions=True, resolution=.5, fig_width=8.5, fig_height=11, # font_size=6, junction_log_base=10, reverse_minus=False, # bar_posterior=False): # Get the settings we need settings = sashimi_obj.settings bam_files = settings["bam_files"] miso_files = settings["miso_files"] intron_scale = settings["intron_scale"] exon_scale = settings["exon_scale"] gene_posterior_ratio = settings["gene_posterior_ratio"] posterior_bins = settings["posterior_bins"] colors = settings["colors"] ymax = settings["ymax"] logged = settings["logged"] show_posteriors = settings["show_posteriors"] coverages = settings["coverages"] number_junctions = settings["number_junctions"] resolution = settings["resolution"] junction_log_base = settings["junction_log_base"] reverse_minus = settings["reverse_minus"] bar_posterior = settings["bar_posteriors"] font_size = settings["font_size"] nyticks = settings["nyticks"] nxticks = settings["nxticks"] show_ylabel = settings["show_ylabel"] show_xlabel = settings["show_xlabel"] if plot_title is None: plot_title = event print "Using intron scale ", intron_scale print "Using exon scale ", exon_scale # Always show y-axis for read densities for now showYaxis = True # Parse gene pickle file to get information about gene tx_start, tx_end, exon_starts, exon_ends, gene_obj, mRNAs, strand, chrom = \ parseGene(pickle_filename, event) # Get the right scalings graphcoords, graphToGene = getScaling(tx_start, tx_end, strand, exon_starts, exon_ends, intron_scale, exon_scale, reverse_minus) nfiles = len(bam_files) if plot_title is not None: # Use custom title if given suptitle(plot_title, fontsize=10) else: suptitle(event, fontsize=10) plotted_axes = [] for i in range(nfiles): if colors is not None: color = colors[i] else: color = None if coverages is not None: coverage = coverages[i] else: coverage = 1 if i < nfiles - 1: showXaxis = False else: showXaxis = True bam_file = os.path.expanduser(bam_files[i]) ax1 = subplot2grid((nfiles + 3, gene_posterior_ratio), (i, 0), colspan=gene_posterior_ratio - 1) # Read sample label sample_label = settings["sample_labels"][i] print "Reading sample label: %s" %(sample_label) print "Processing BAM: %s" %(bam_file) plotted_ax = plot_density_single(settings, sample_label, tx_start, tx_end, gene_obj, mRNAs, strand, graphcoords, graphToGene, bam_file, ax1, chrom, paired_end=False, intron_scale=intron_scale, exon_scale=exon_scale, color=color, ymax=ymax, logged=logged, coverage=coverage, number_junctions=number_junctions, resolution=resolution, showXaxis=showXaxis, nyticks=nyticks, nxticks=nxticks, show_ylabel=show_ylabel, show_xlabel=show_xlabel, font_size=font_size, junction_log_base=junction_log_base) plotted_axes.append(plotted_ax) if show_posteriors: miso_file = os.path.expanduser(miso_files[i]) try: ax2 = subplot2grid((nfiles + 3, gene_posterior_ratio),\ (i, gene_posterior_ratio - 1)) if not os.path.isfile(miso_file): print "Warning: MISO file %s not found" %(miso_file) print "Loading MISO file: %s" %(miso_file) plot_posterior_single(miso_file, ax2, posterior_bins, showXaxis=showXaxis, show_ylabel=False, font_size=font_size, bar_posterior=bar_posterior) except: box(on=False) xticks([]) yticks([]) print "Posterior plot failed." ## ## Figure out correct y-axis values ## ymax_vals = [] if ymax != None: # Use user-given ymax values if provided max_used_yval = ymax else: # Compute best ymax value for all samples: take # maximum y across all. used_yvals = [curr_ax.get_ylim()[1] for curr_ax in plotted_axes] # Round up max_used_yval = math.ceil(max(used_yvals)) # Reset axes based on this. # Set fake ymin bound to allow lower junctions to be visible fake_ymin = -0.6 * max_used_yval universal_yticks = linspace(0, max_used_yval, nyticks + 1) # Round up yticks universal_ticks = map(math.ceil, universal_yticks) for sample_num, curr_ax in enumerate(plotted_axes): if showYaxis: curr_ax.set_ybound(lower=fake_ymin, upper=max_used_yval) curr_yticklabels = [] for label in universal_yticks: if label <= 0: # Exclude label for 0 curr_yticklabels.append("") else: if label % 1 != 0: curr_yticklabels.append("%.1f" %(label)) else: curr_yticklabels.append("%d" %(label)) curr_ax.set_yticklabels(curr_yticklabels, fontsize=font_size) curr_ax.spines["left"].set_bounds(0, max_used_yval) curr_ax.set_yticks(universal_yticks) curr_ax.yaxis.set_ticks_position('left') curr_ax.spines["right"].set_color('none') if show_ylabel: y_horz_alignment = 'left' if logged: curr_ax.set_ylabel('RPKM $(\mathregular{\log}_{\mathregular{10}})$', fontsize=font_size, ha=y_horz_alignment) else: curr_ax.set_ylabel('RPKM', fontsize=font_size, va="bottom", ha=y_horz_alignment) else: curr_ax.spines["left"].set_color('none') curr_ax.spines["right"].set_color('none') curr.ax.set_yticks([]) ## ## Plot sample labels ## sample_color = colors[sample_num] # Make sample label y position be halfway between highest # and next to highest ytick if len(universal_yticks) >= 2: halfway_ypos = (universal_yticks[-1] - universal_yticks[-2]) / 2. label_ypos = universal_yticks[-2] + halfway_ypos else: label_ypos = universal_yticks[-1] curr_label = settings["sample_labels"][sample_num] curr_ax.text(max(graphcoords), label_ypos, curr_label, fontsize=font_size, va='bottom', ha='right', color=sample_color) # Draw gene structure ax = subplot2grid((nfiles + 3, gene_posterior_ratio), (nfiles + 1, 0), colspan=gene_posterior_ratio - 1, rowspan=2) plot_mRNAs(tx_start, mRNAs, strand, graphcoords, reverse_minus) subplots_adjust(hspace=.10, wspace=.7)
def plot_density(sashimi_obj, pickle_filename, event, plot_title=None): # intron_scale=30, exon_scale=1, gene_posterior_ratio=5, posterior_bins=40, # colors=None, ymax=None, logged=False, show_posteriors=True, coverages=None, # number_junctions=True, resolution=.5, fig_width=8.5, fig_height=11, # font_size=6, junction_log_base=10, reverse_minus=False, # bar_posterior=False): # Get the settings we need settings = sashimi_obj.settings bam_files = settings["bam_files"] miso_files = settings["miso_files"] intron_scale = settings["intron_scale"] exon_scale = settings["exon_scale"] gene_posterior_ratio = settings["gene_posterior_ratio"] posterior_bins = settings["posterior_bins"] colors = settings["colors"] ymax = settings["ymax"] logged = settings["logged"] show_posteriors = settings["show_posteriors"] coverages = settings["coverages"] number_junctions = settings["number_junctions"] resolution = settings["resolution"] junction_log_base = settings["junction_log_base"] reverse_minus = settings["reverse_minus"] bar_posterior = settings["bar_posteriors"] font_size = settings["font_size"] nyticks = settings["nyticks"] nxticks = settings["nxticks"] show_ylabel = settings["show_ylabel"] show_xlabel = settings["show_xlabel"] if plot_title is None: plot_title = event print "Using intron scale ", intron_scale print "Using exon scale ", exon_scale # Always show y-axis for read densities for now showYaxis = True # Parse gene pickle file to get information about gene tx_start, tx_end, exon_starts, exon_ends, gene_obj, mRNAs, strand, chrom = \ parseGene(pickle_filename, event) # Get the right scalings graphcoords, graphToGene = getScaling(tx_start, tx_end, strand, exon_starts, exon_ends, intron_scale, exon_scale, reverse_minus) nfiles = len(bam_files) if plot_title is not None: # Use custom title if given suptitle(plot_title, fontsize=1.4 * font_size) else: suptitle(event, fontsize=1.4 * font_size) plotted_axes = [] h_ratio = [1] * nfiles + [font_size / 10.0, len(mRNAs) * 0.25] if show_posteriors: w_ratio = [gene_posterior_ratio, 1] gs = gridspec.GridSpec(nfiles + 2, 2, height_ratios=h_ratio, width_ratios=w_ratio) else: gs = gridspec.GridSpec(nfiles + 2, 1, height_ratios=h_ratio) for i in range(nfiles): if colors is not None: color = colors[i] else: color = None if coverages is not None: coverage = coverages[i] else: coverage = 1 if i < nfiles - 1: showXaxis = False else: showXaxis = True bam_file = os.path.expanduser(bam_files[i]) # Read sample label sample_label = settings["sample_labels"][i] print "Reading sample label: %s" % (sample_label) print "Processing BAM: %s" % (bam_file) ax1 = subplot(gs[i, 0]) plotted_ax = plot_density_single(settings, sample_label, tx_start, tx_end, gene_obj, mRNAs, strand, graphcoords, graphToGene, bam_file, ax1, chrom, paired_end=False, intron_scale=intron_scale, exon_scale=exon_scale, color=color, ymax=ymax, logged=logged, coverage=coverage, number_junctions=number_junctions, resolution=resolution, showXaxis=showXaxis, nyticks=nyticks, nxticks=nxticks, show_ylabel=show_ylabel, show_xlabel=show_xlabel, font_size=font_size, junction_log_base=junction_log_base) plotted_axes.append(plotted_ax) if show_posteriors: miso_file = os.path.expanduser(miso_files[i]) try: ax2 = subplot(gs[i, 1]) if not os.path.isfile(miso_file): print "Warning: MISO file %s not found" % (miso_file) print "Loading MISO file: %s" % (miso_file) plot_posterior_single(miso_file, ax2, posterior_bins, event, showXaxis=showXaxis, show_ylabel=False, font_size=font_size, bar_posterior=bar_posterior) except: box(on=False) xticks([]) yticks([]) print "Posterior plot failed." ## ## Figure out correct y-axis values ## ymax_vals = [] if ymax != None: # Use user-given ymax values if provided max_used_yval = ymax else: # Compute best ymax value for all samples: take # maximum y across all. used_yvals = [curr_ax.get_ylim()[1] for curr_ax in plotted_axes] # Round up max_used_yval = max(used_yvals) * 1.05 // nyticks * nyticks # Reset axes based on this. # Set fake ymin bound to allow lower junctions to be visible fake_ymin = -0.6 * max_used_yval universal_yticks = linspace(0, max_used_yval, nyticks + 1) # Round up yticks universal_ticks = map(math.ceil, universal_yticks) for sample_num, curr_ax in enumerate(plotted_axes): if showYaxis: curr_ax.set_ybound(lower=fake_ymin, upper=max_used_yval) curr_yticklabels = [] for label in universal_yticks: if label <= 0: # Exclude label for 0 curr_yticklabels.append("") else: if label % 1 != 0: curr_yticklabels.append("%.1f" % (label)) else: curr_yticklabels.append("%d" % (label)) curr_ax.set_yticklabels(curr_yticklabels, fontsize=font_size) curr_ax.spines["left"].set_bounds(0, max_used_yval) curr_ax.set_yticks(universal_yticks) curr_ax.yaxis.set_ticks_position('left') curr_ax.spines["right"].set_color('none') if show_ylabel and sample_num == nfiles // 2: y_horz_alignment = 'left' if logged: curr_ax.set_ylabel( 'RPKM $(\mathregular{\log}_{\mathregular{10}})$', fontsize=font_size, ha=y_horz_alignment) else: curr_ax.set_ylabel('RPKM', fontsize=font_size, va="bottom", ha=y_horz_alignment) else: curr_ax.spines["left"].set_color('none') curr_ax.spines["right"].set_color('none') curr.ax.set_yticks([]) ## ## Plot sample labels ## sample_color = colors[sample_num] # Make sample label y position be halfway between highest # and next to highest ytick if len(universal_yticks) >= 2: halfway_ypos = (universal_yticks[-1] - universal_yticks[-2]) / 2. label_ypos = universal_yticks[-2] + halfway_ypos else: label_ypos = universal_yticks[-1] curr_label = settings["sample_labels"][sample_num] curr_ax.text(max(graphcoords), label_ypos, curr_label, fontsize=font_size * 1.2, va='bottom', ha='right', color=sample_color) # Draw gene structure ax = subplot(gs[nfiles + 1, 0]) plot_mRNAs(tx_start, mRNAs, strand, graphcoords, reverse_minus) # subplots_adjust(hspace=.10, wspace=.7) subplots_adjust(hspace=.0, wspace=0.1)