Ejemplo n.º 1
0
    def air_hist(self, id=None, channel_num=0, *args, **kwargs):
        from qtools.lib.nstats.peaks import gap_air
        from pyqlb.nstats.well import accepted_peaks
        from pyqlb.nstats.peaks import color_uncorrected_peaks, channel_amplitudes, peak_times
        from qtools.lib.mplot import air_hist, cleanup, render as plt_render

        qlwell = self.__qlwell_from_threshold_form(id)
        self.__set_threshold_context(qlwell)
        c.channel_num = int(channel_num)
        threshold = c.vic_threshold if c.channel_num == 1 else c.fam_threshold
        cutoff = request.params.get('cutoff', 500)

        # can detect air on either channel (especially if VICs super low)
        # but always report VIC amplitude
        air_drops = gap_air(qlwell, c.channel_num, threshold=threshold)
        uncorrected_air = color_uncorrected_peaks(air_drops, qlwell.color_compensation_matrix)

        # count number of accepted peak times
        air_drop_times = peak_times(air_drops)
        accepted_times = peak_times(accepted_peaks(qlwell))
        num_air_accepted = len([t for t in air_drop_times if t in accepted_times])

        # always gate on VIC
        air_amps = channel_amplitudes(uncorrected_air, 1)

        title = 'Air Droplet Histogram - %s, %s (%s)' % (c.well.plate.plate.name, c.well.well_name, 'VIC' if c.channel_num == 1 else 'FAM')
        fig = air_hist(title, air_amps, cutoff=cutoff, num_accepted=num_air_accepted)
        response.content_type = 'image/png'
        imgdata = plt_render(fig, dpi=72)
        cleanup(fig)
        return imgdata
Ejemplo n.º 2
0
def gap_rain(qlwell, channel_num=0, threshold=None, pct_boundary=0.3, gap_size=10000):
    """
    Return the rain in the gaps between non-rain droplets.
    """
    rain, nonrain = rain_split(qlwell,
                               channel_num=channel_num,
                               threshold=threshold,
                               pct_boundary=pct_boundary)
    
    # ok, now identify the gaps in the gates.
    times = peak_times(nonrain)
    if nonrain is None or len(nonrain) < 2:
        return np.ndarray([0],dtype=peak_dtype(2))
    
    intervals = np.ediff1d(times, to_begin=0, to_end=0)
    big_intervals = intervals > gap_size

    # find beginning of gaps with extract
    beginnings = np.extract(big_intervals[1:], times)
    ends = np.extract(big_intervals[:-1], times)

    gap_intervals = zip(beginnings, ends)
    gap_intervals.insert(0, (0, times[0]))
    gap_intervals.append((times[-1], times[-1]*100))
    
    # count the rain in the intervals
    gap_drops = np.extract(reduce(np.logical_or, [np.logical_and(peak_times(rain) > b,
                                                                 peak_times(rain) < e) for b, e in gap_intervals]),
                           rain)
    return gap_drops
Ejemplo n.º 3
0
    def cluster_csv(self, id=None, show_only_gated=True, *args, **kwargs):
        from pyqlb.nstats.well import accepted_peaks
        qlwell = self.__qlwell_from_threshold_form(id)

        if show_only_gated != 'False':
            peaks = accepted_peaks(qlwell)
        else:
            peaks = qlwell.peaks

        from pyqlb.nstats.peaks import fam_amplitudes, fam_widths, vic_amplitudes, vic_widths, peak_times
        from pyqlb.nstats.well import well_observed_cluster_assignments

        response.headers['Content-Type'] = 'text/csv'
        h.set_download_response_header(request, response, "%s_%s%s.csv" % \
            (str(c.well.plate.plate.name), str(c.well.well_name), '' if show_only_gated != 'False' else '_all'))
        out = StringIO.StringIO()
        csvwriter = csv_pkg.writer(out)
        csvwriter.writerow(['Plate',c.well.plate.plate.name])
        csvwriter.writerow(['Well',c.well.well_name])
        csvwriter.writerow([])
        csvwriter.writerow(['Time','FAMAmplitude','FAMWidth','VICAmplitude','VICWidth','Cluster'])
        csvwriter.writerow([])

        pts = peak_times(peaks)
        fas = fam_amplitudes(peaks)
        fws = fam_widths(peaks)
        vas = vic_amplitudes(peaks)
        vws = vic_widths(peaks)
        cls = well_observed_cluster_assignments(qlwell, peaks)

        for row in zip(pts, fas, fws, vas, vws, cls):
            csvwriter.writerow(row)
        csv = out.getvalue()
        out.close()
        return csv
Ejemplo n.º 4
0
    def svilen(self, id=None, *args, **kwargs):
        from pyqlb.nstats.well import accepted_peaks
        from pyqlb.nstats.peaks import cluster_2d, peak_times, fam_widths
        from pyqlb.factory import QLNumpyObjectFactory
        from qtools.lib.mplot import svilen, cleanup, render as plt_render

        qlwell = self.__qlwell_from_threshold_form(id)
        self.__set_threshold_context(qlwell)
        well_path = self.__well_path()
        # oh shit
        factory = QLNumpyObjectFactory()
        raw_well = factory.parse_well(well_path)

        crap, crap, gold, crap = cluster_2d(accepted_peaks(qlwell), c.fam_threshold,
                                            c.vic_threshold)
        
        times = peak_times(gold)
        widths = fam_widths(gold)

        title = "VIC+/FAM- droplet traces (accepted events)"
        ranges = [(int(t-(w*2)), int(t+(w*2))) for t, w in zip(times, widths)]
        if c.fam_threshold == 0 or c.vic_threshold == 0:
            ranges = []
            title = "%s (no events in quadrant)" % title
        elif len(ranges) > 100:
            ranges = ranges[:100]
            title = "%s (truncated at first 100)" % title

        
        fig = svilen(title, raw_well.samples, ranges, widths)
        response.content_type = 'image/png'
        imgdata = plt_render(fig, dpi=72)
        cleanup(fig)
        return imgdata
Ejemplo n.º 5
0
    def temporal2d(self, id=None, *args, **kwargs):
        from qtools.lib.nstats.peaks import accepted_peaks
        from pyqlb.nstats.peaks import peak_times, fam_amplitudes, vic_amplitudes
        qlwell = self.__qlwell_from_threshold_form(id)
        
        self.__set_threshold_context(qlwell)

        ok_peaks = accepted_peaks(qlwell)
        c.tvf = zip(peak_times(ok_peaks), vic_amplitudes(ok_peaks), fam_amplitudes(ok_peaks))

        return render('/well/temporal2d.html')
Ejemplo n.º 6
0
def gap_air(qlwell, channel_num=0, threshold=None, pct_boundary=0.3, gap_size=10000, gap_buffer=250, max_amp=1000):
    """
    Return the air (gap rain < max_amp) in the gaps between non-rain droplets.

    :param channel_num: The channel on which to detect air droplets.
    :param threshold: The threshold dividing positives and negatives (used to detect 'rain')
    :param pct_boundary: The percentage outside of which a droplet is classified as rain.
    :param gap_size: The minimum size (in samples) of a gap.  Default 0.1s.
    :param gap_buffer: The distance a droplet must be from the main population to be considered an
                       air droplet, in samples.  Default 0.0025s.
    :param max_amp: The maximum color-corrected amplitude of an air droplet.  Default 1000 RFU.
    """
    rain, nonrain = rain_split(qlwell,
                               channel_num=channel_num,
                               threshold=threshold,
                               pct_boundary=pct_boundary,
                               split_all_peaks=True)
    
    low_amp = np.extract(channel_amplitudes(rain, channel_num) < max_amp, rain)

    times = peak_times(nonrain)
    if nonrain is None or len(nonrain) < 2:
        return np.ndarray([0], dtype=peak_dtype(2))
    
    intervals = np.ediff1d(times, to_begin=0, to_end=0)
    big_intervals = intervals > gap_size

    # find beginning of gaps with extract
    beginnings = [b+gap_buffer for b in np.extract(big_intervals[1:], times)]
    ends = [e-gap_buffer for e in np.extract(big_intervals[:-1], times)]

    gap_intervals = zip(beginnings, ends)
    gap_intervals.insert(0, (0, times[0]-gap_buffer))
    gap_intervals.append((times[-1]+gap_buffer, times[-1]*100))
    
    # count the rain in the intervals
    gap_drops = np.extract(reduce(np.logical_or, [np.logical_and(peak_times(low_amp) > b,
                                                                 peak_times(low_amp) < e) for b, e in gap_intervals]),
                           low_amp)
    return gap_drops
Ejemplo n.º 7
0
    def temporal_galaxy(self, id=None, channel_num=0, *args, **kwargs):
        from qtools.lib.nstats.peaks import above_min_amplitude_peaks
        from pyqlb.nstats.peaks import peak_times, channel_amplitudes, channel_widths
        
        qlwell = self.__qlwell_from_threshold_form(id)
        self.__set_threshold_context(qlwell)
        c.channel_num = int(channel_num)

        ok_peaks = above_min_amplitude_peaks(qlwell)
        c.taw = zip(peak_times(ok_peaks), channel_amplitudes(ok_peaks, c.channel_num), channel_widths(ok_peaks, c.channel_num))
        if c.channel_num == 0:
            c.channel_name = 'FAM'
        else:
            c.channel_name = 'VIC'

        return render('/well/temporal_galaxy.html')
Ejemplo n.º 8
0
    def peak_csv(self, id=None, show_only_gated=True, *args, **kwargs):
        from qtools.lib.nstats.peaks import accepted_peaks
        qlwell = self.__qlwell_from_threshold_form(id)

        if show_only_gated != 'False':
            peaks = accepted_peaks(qlwell)
        else:
            peaks = qlwell.peaks

        from pyqlb.nstats.peaks import fam_amplitudes, fam_widths, fam_quality, vic_amplitudes, vic_widths, vic_quality, peak_times

        response.headers['Content-Type'] = 'text/csv'
        h.set_download_response_header(request, response, "%s_%s%s.csv" % \
            (str(c.well.plate.plate.name), str(c.well.well_name), '' if show_only_gated != 'False' else '_all'))
        out = StringIO.StringIO()
        csvwriter = csv_pkg.writer(out)
        csvwriter.writerow(['Plate',c.well.plate.plate.name])
        csvwriter.writerow(['Well',c.well.well_name])
        csvwriter.writerow([])
        csvwriter.writerow(['FAMThreshold',qlwell.channels[0].statistics.threshold])
        csvwriter.writerow(['VICThreshold',qlwell.channels[1].statistics.threshold])
        csvwriter.writerow(['WidthGate',qlwell.channels[0].statistics.min_width_gate,qlwell.channels[0].statistics.max_width_gate])
        csvwriter.writerow(['MinQualityGate',qlwell.channels[0].statistics.min_quality_gate])
        csvwriter.writerow([])
        csvwriter.writerow(['Time','FAMAmplitude','FAMWidth','FAMQuality','VICAmplitude','VICWidth','VICQuality'])
        csvwriter.writerow([])

        pts = peak_times(peaks)
        fas = fam_amplitudes(peaks)
        fws = fam_widths(peaks)
        fqs = fam_quality(peaks)
        vas = vic_amplitudes(peaks)
        vws = vic_widths(peaks)
        vqs = vic_quality(peaks)

        for row in zip(pts, fas, fws, fqs, vas, vws, vqs):
            csvwriter.writerow(row)
        csv = out.getvalue()
        out.close()
        return csv