예제 #1
0
def plot_insert_len(insert_len_filename, settings_filename, output_dir):
    """
    Plot insert length distribution.
    """
    if not os.path.isfile(settings_filename):
        print "Error: settings filename %s not found." % (settings_filename)
        #sys.exit(1)

    plot_name = os.path.basename(insert_len_filename)
    sashimi_obj = Sashimi(plot_name,
                          output_dir,
                          settings_filename=settings_filename)
    settings = sashimi_obj.settings
    num_bins = settings["insert_len_bins"]
    output_filename = sashimi_obj.output_filename
    sashimi_obj.setup_figure()
    s = plt.subplot(1, 1, 1)

    print "Plotting insert length distribution..."
    print "  - Distribution file: %s" % (insert_len_filename)
    print "  - Output plot: %s" % (output_filename)

    insert_dist, params = pe_utils.load_insert_len(insert_len_filename)

    mean, sdev, dispersion, num_pairs \
          = pe_utils.compute_insert_len_stats(insert_dist)
    print "min insert: %.1f" % (min(insert_dist))
    print "max insert: %.1f" % (max(insert_dist))
    plt.title("%s (%d read-pairs)" \
              %(plot_name,
                num_pairs),
              fontsize=10)
    plt.hist(insert_dist,
             bins=num_bins,
             color='k',
             edgecolor="#ffffff",
             align='mid')
    axes_square(s)
    ymin, ymax = s.get_ylim()
    plt.text(0.05, 0.95, "$\mu$: %.1f\n$\sigma$: %.1f\n$d$: %.1f" \
             %(round(mean, 2),
               round(sdev, 2),
               round(dispersion, 2)),
             horizontalalignment='left',
             verticalalignment='top',
             bbox=dict(edgecolor='k', facecolor="#ffffff",
                       alpha=0.5),
             fontsize=10,
             transform=s.transAxes)
    plt.xlabel("Insert length (nt)")
    plt.ylabel("No. read pairs")
    sashimi_obj.save_plot()
예제 #2
0
def plot_insert_len(insert_len_filename,
                    settings_filename,
                    output_dir):
    """
    Plot insert length distribution.
    """
    if not os.path.isfile(settings_filename):
        print "Error: settings filename %s not found." %(settings_filename)
        #sys.exit(1)

    plot_name = os.path.basename(insert_len_filename)
    sashimi_obj = Sashimi(plot_name, output_dir,
                          settings_filename=settings_filename)
    settings = sashimi_obj.settings
    num_bins = settings["insert_len_bins"]
    output_filename = sashimi_obj.output_filename
    sashimi_obj.setup_figure()
    s = plt.subplot(1, 1, 1)
    
    print "Plotting insert length distribution..."
    print "  - Distribution file: %s" %(insert_len_filename)
    print "  - Output plot: %s" %(output_filename)
    
    insert_dist, params = pe_utils.load_insert_len(insert_len_filename)

    mean, sdev, dispersion, num_pairs \
          = pe_utils.compute_insert_len_stats(insert_dist)
    print "min insert: %.1f" %(min(insert_dist))
    print "max insert: %.1f" %(max(insert_dist))
    plt.title("%s (%d read-pairs)" \
              %(plot_name,
                num_pairs),
              fontsize=10)
    plt.hist(insert_dist, bins=num_bins, color='k',
             edgecolor="#ffffff", align='mid')
    axes_square(s)
    ymin, ymax = s.get_ylim()
    plt.text(0.05, 0.95, "$\mu$: %.1f\n$\sigma$: %.1f\n$d$: %.1f" \
             %(round(mean, 2),
               round(sdev, 2),
               round(dispersion, 2)),
             horizontalalignment='left',
             verticalalignment='top',
             bbox=dict(edgecolor='k', facecolor="#ffffff",
                       alpha=0.5),
             fontsize=10,
             transform=s.transAxes)
    plt.xlabel("Insert length (nt)")
    plt.ylabel("No. read pairs")
    sashimi_obj.save_plot()
def plot_density_from_file(settings_f,
                           pickle_filename,
                           event,
                           output_dir,
                           group_info=None,
                           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)

    # 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,
                 group_info=group_info,
                 plot_title=plot_title)

    # Save figure
    sashimi_obj.save_plot(plot_label=plot_label)
예제 #4
0
def plot_posterior(miso_filename, settings_filename, output_dir,
                   plot_mean=False, with_intervals=95):
    """
    Plot posterior distribution.
    """
    if not os.path.isfile(settings_filename):
        print "Error: settings filename %s not found." %(settings_filename)
        sys.exit(1)
#    samples, log_scores, params, gene = load_samples(miso_filename)
    samples, h, log_scores, sampled_map,\
             sampled_map_log_score, counts_info = load_samples(miso_filename)
    params = parse_sampler_params(miso_filename)

    plot_name = os.path.basename(miso_filename).replace(".miso", "")

    sashimi_obj = Sashimi(plot_name, output_dir,
                          settings_filename=settings_filename)
    settings = sashimi_obj.settings
    # Setup the figure
    sashimi_obj.setup_figure()

    dimensions = (settings["fig_width"],
                  settings["fig_height"])
    
    sp = SamplesPlotter(samples, params)
    
    if with_intervals != None:
        with_intervals = float(with_intervals)/100.
        print "Plotting with %d-percent confidence intervals" %(int(with_intervals * 100))
    else:
        with_intervals = False

    if plot_mean:
        print "Plotting mean of posterior."

    print "Plotting posterior distribution..."
    print "  - MISO event file: %s" %(miso_filename)
    print "  - Output dir: %s" %(output_dir)
    sashimi_obj.setup_figure()
    sp.plot(plot_intervals=with_intervals, fig_dims=dimensions,
            plot_mean=plot_mean)
    sashimi_obj.save_plot()
예제 #5
0
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)
예제 #6
0
def plot_bf_dist(bf_filename, settings_filename, output_dir, max_bf=1e12):
    """
    Plot a Bayes factor distribution from a .miso_bf file.
    """
    if not bf_filename.endswith(".miso_bf"):
        print "WARNING: %s does not end in .miso_bf, are you sure it is the " \
              "output of a MISO samples comparison?" %(bf_filename)

    # Load BF data
    data, h = csv2dictlist_raw(bf_filename)

    plot_name = os.path.basename(bf_filename)
    sashimi_obj = Sashimi(plot_name,
                          output_dir,
                          settings_filename=settings_filename)
    settings = sashimi_obj.settings

    # Setup the figure
    sashimi_obj.setup_figure()

    # Matrix of bayes factors and delta psi pairs
    bfs_and_deltas = []
    for event in data:
        bf = event['bayes_factor']
        delta_psi = event['diff']

        if type(bf) == str and "," in bf:
            print "WARNING: %s is a multi-isoform event, skipping..." \
                %(event)
            continue
        else:
            # Impose upper limit on Bayes factor
            bf = min(1e12, float(bf))
            delta_psi = float(delta_psi)

        bfs_and_deltas.append([bf, delta_psi])

    bfs_and_deltas = array(bfs_and_deltas)
    num_events = len(bfs_and_deltas)

    print "Loaded %d event comparisons." % (num_events)

    output_filename = sashimi_obj.output_filename

    print "Plotting Bayes factors distribution"
    print "  - Output filename: %s" % (output_filename)
    bf_thresholds = settings["bf_thresholds"]
    bar_color = settings["bar_color"]

    min_bf_thresh = min(bf_thresholds)
    num_events_used = sum(bfs_and_deltas[:, 0] >= min_bf_thresh)
    for thresh in bf_thresholds:
        if type(thresh) != int:
            print "Error: BF thresholds must be integers."
            #sys.exit(1)

    print "Using BF thresholds: "
    print bf_thresholds
    print "Using bar color: %s" % (bar_color)
    plot_cumulative_bars(bfs_and_deltas[:, 0],
                         bf_thresholds,
                         bar_color=bar_color,
                         logged=True)
    plt.xticks(bf_thresholds)
    c = 1
    plt.xlim([bf_thresholds[0] - c, bf_thresholds[-1] + c])
    plt.title("Bayes factor distributions\n(using %d/%d events)" \
              %(num_events_used, num_events))
    plt.xlabel("Bayes factor thresh.")
    plt.ylabel("No. events")
    sashimi_obj.save_plot()
예제 #7
0
파일: plot.py 프로젝트: fpadilla/Miso-Batch
def plot_bf_dist(bf_filename, settings_filename, output_dir,
                 max_bf=1e12):
    """
    Plot a Bayes factor distribution from a .miso_bf file.
    """
    if not bf_filename.endswith(".miso_bf"):
        print "WARNING: %s does not end in .miso_bf, are you sure it is the "
        "output of a MISO samples comparison?" %(bf_filename)

    # Load BF data
    data, h = csv2dictlist_raw(bf_filename)
    
    plot_name = os.path.basename(bf_filename)
    sashimi_obj = Sashimi(plot_name, output_dir,
                          settings_filename=settings_filename)
    settings = sashimi_obj.settings

    # Setup the figure
    sashimi_obj.setup_figure()

    # Matrix of bayes factors and delta psi pairs
    bfs_and_deltas = []
    for event in data:
        bf = event['bayes_factor']
        delta_psi = event['diff']
	print bf
        if type(bf) == str and  "," in bf:
            print "WARNING: %s is a multi-isoform event, skipping..."
            continue
        elif isinstance(bf,float) :
            # Impose upper limit on Bayes factor
            bf = min(1e12, float(bf))
            delta_psi = float(delta_psi)
        else:
            continue
        bfs_and_deltas.append([bf, delta_psi])

    bfs_and_deltas = array(bfs_and_deltas)
    num_events = len(bfs_and_deltas)

    print "Loaded %d event comparisons." %(num_events)

    output_filename = sashimi_obj.output_filename 

    print "Plotting Bayes factors distribution"
    print "  - Output filename: %s" %(output_filename)
    bf_thresholds = settings["bf_thresholds"]
    bar_color = settings["bar_color"]

    min_bf_thresh = min(bf_thresholds)
    num_events_used = sum(bfs_and_deltas[:, 0] >= min_bf_thresh)
    for thresh in bf_thresholds:
        if type(thresh) != int:
            print "Error: BF thresholds must be integers."
            sys.exit(1)
    print "Using BF thresholds: "
    print bf_thresholds
    print "Using bar color: %s" %(bar_color)
    plot_cumulative_bars(bfs_and_deltas[:, 0],
                         bf_thresholds,
                         bar_color=bar_color,
                         logged=True)
    plt.xticks(bf_thresholds)
    c = 1
    plt.xlim([bf_thresholds[0] - c, bf_thresholds[-1] + c])
    plt.title("Bayes factor distributions\n(using %d/%d events)" \
              %(num_events_used, num_events))
    plt.xlabel("Bayes factor thresh.")
    plt.ylabel("No. events")
    sashimi_obj.save_plot()