def contamination_carryover_peaks(self, qlplate, exclude_min_amplitude_peaks=True): """ Returns (aggregate contamination peaks, aggregate gated contamination peaks, aggregate carryover peaks, number of carryover wells, well name->#carryover well peaks per well) """ well_pairs = [] eventful_well = None stealth_well = None # FIXFIX relax when QuantaSoft bug is resolved #for well in qlplate.in_run_order: wells = sorted(qlplate.analyzed_wells.values(), cmp=QLWell.row_order_comparator) for well in sorted(qlplate.analyzed_wells.values(), cmp=QLWell.row_order_comparator): if well.original_sample_name not in self.empty_sample_names: stealth_well = None eventful_well = well elif well.original_sample_name in self.empty_sample_names: stealth_well = well if eventful_well: well_pairs.append((eventful_well, stealth_well)) eventful_well = None stealth_well = None contamination_peaks = np.ndarray([0], dtype=peak_dtype(2)) gated_contamination_peaks = np.ndarray([0], dtype=peak_dtype(2)) carryover_peaks = np.ndarray([0], dtype=peak_dtype(2)) num_wells = 0 carryover_well_peak_dict = dict() for e, s in well_pairs: num_wells = num_wells + 1 stats = e.channels[self.channel_num].statistics threshold = stats.threshold min_width_gate, max_width_gate = well_static_width_gates(e) # TODO: what to do about quality gating? # get all contamination first above 750 RFU (assume threshold above 750?) if exclude_min_amplitude_peaks: peaks = above_min_amplitude_peaks(s) else: peaks = s.peaks well_contamination_peaks, too_low = cluster_1d(peaks, self.channel_num, 750) well_gated_contamination_peaks = width_gated(well_contamination_peaks, min_width_gate=min_width_gate, max_width_gate=max_width_gate, on_channel_num=self.channel_num, ignore_gating_flags=True) if threshold: well_carryover_peaks, under_threshold = cluster_1d(well_gated_contamination_peaks, self.channel_num, threshold) else: well_carryover_peaks = np.ndarray([0], dtype=peak_dtype(2)) contamination_peaks = np.hstack([contamination_peaks, well_contamination_peaks]) gated_contamination_peaks = np.hstack([gated_contamination_peaks, well_gated_contamination_peaks]) carryover_peaks = np.hstack([carryover_peaks, well_carryover_peaks]) carryover_well_peak_dict[s.name] = len(well_carryover_peaks) return contamination_peaks, gated_contamination_peaks, carryover_peaks, num_wells, carryover_well_peak_dict
def extracluster_peaks(well, channel_num, threshold=None, pct_boundary=0.3, exclude_min_amplitude_peaks=True): """ Return the peaks that are outside the clusters. A superset of polydispersity peaks, meant primarily for dye wells, where there should be no biological basis for rain. Returns a 3-tuple: peaks, rain gates, width gates """ if not threshold: threshold = well.channels[channel_num].statistics.threshold if not threshold: threshold = None if exclude_min_amplitude_peaks: peaks = above_min_amplitude_peaks(well) else: peaks = well.peaks # get rain_pvalues p_plus, p, p_minus, pos, middle_high, middle_low, neg = \ rain_pvalues_thresholds(peaks, channel_num=channel_num, threshold=threshold, pct_boundary=pct_boundary) min_gate, max_gate = well_static_width_gates(well) if middle_high and middle_low: extracluster_peaks = np.extract(np.logical_not( np.logical_or( reduce(np.logical_and, (channel_widths(peaks, channel_num) > min_gate, channel_widths(peaks, channel_num) < max_gate, channel_amplitudes(peaks, channel_num) > middle_high, channel_amplitudes(peaks, channel_num) < pos)), reduce(np.logical_and, (channel_widths(peaks, channel_num) > min_gate, channel_widths(peaks, channel_num) < max_gate, channel_amplitudes(peaks, channel_num) > neg, channel_amplitudes(peaks, channel_num) < middle_low)) ) ), peaks) else: extracluster_peaks = np.extract(np.logical_not( reduce(np.logical_and, (channel_widths(peaks, channel_num) > min_gate, channel_widths(peaks, channel_num) < max_gate, channel_amplitudes(peaks, channel_num) > neg, channel_amplitudes(peaks, channel_num) < pos) ) ), peaks) return (extracluster_peaks, (pos, middle_high, middle_low, neg), (min_gate, max_gate))
def galaxy(self, id=None, channel_num=0, *args, **kwargs): from qtools.lib.nstats.peaks import above_min_amplitude_peaks from pyqlb.nstats.well import well_static_width_gates qlwell = self.__qlwell_from_threshold_form(id) self.__set_threshold_context(qlwell) channel_idx = int(request.params.get("channel", 0)) from qtools.lib.mplot import galaxy, cleanup, render as plt_render peaks = above_min_amplitude_peaks(qlwell) title = 'Galaxy Lite - %s - %s, %s' % (c.well.plate.plate.name, c.well.well_name, 'VIC' if channel_idx == 1 else 'FAM') threshold = c.vic_threshold if channel_idx == 1 else c.fam_threshold min_width_gate, max_width_gate = well_static_width_gates(qlwell) fig = galaxy(title, peaks, threshold, min_width_gate, max_width_gate, channel_idx) response.content_type = 'image/png' imgdata = plt_render(fig, dpi=72) cleanup(fig) return imgdata
def contamination_carryover_peaks(self, qlplate, exclude_min_amplitude_peaks=True): """ For now, just count FAM HI carryover """ carryover_upper_bound = None carryover_lower_bound = None min_width_gate = None max_width_gate = None contamination_peaks = np.ndarray([0], dtype=peak_dtype(2)) gated_contamination_peaks = np.ndarray([0], dtype=peak_dtype(2)) carryover_peaks = np.ndarray([0], dtype=peak_dtype(2)) num_wells = 0 carryover_well_peak_dict = dict() fam_bounds = [] vic_bounds = [] # TODO fixfix when QS bug resolved #for idx, well in enumerate(qlplate.in_run_order): for idx, well in enumerate(sorted(qlplate.analyzed_wells.values(), cmp=QLWell.row_order_comparator)): if exclude_min_amplitude_peaks: peaks = above_min_amplitude_peaks(well) else: peaks = well.peaks if well.original_sample_name not in ('FAM HI', 'FAM 350nM'): num_wells = num_wells+1 for bounds, channel in zip((fam_bounds, vic_bounds),(0,1)): for lower_bound, upper_bound, min_width_gate, max_width_gate in bounds: # TODO: this will double-count contamination if the bounds overlap. But if the bounds # overlap, you have much bigger problems than carryover. OK for now. argument_bounds = [(None, None),(None, None)] argument_bounds[channel] = (lower_bound, upper_bound) well_contamination_peaks = filter_amplitude_range(peaks, argument_bounds) well_carryover_peaks = width_gated(well_contamination_peaks, min_width_gate=min_width_gate, max_width_gate=max_width_gate, on_channel_num=channel, ignore_gating_flags=True) if well.name not in carryover_well_peak_dict: carryover_well_peak_dict[well.name] = len(well_carryover_peaks) else: carryover_well_peak_dict[well.name] = carryover_well_peak_dict[well.name] + len(well_carryover_peaks) contamination_peaks = np.hstack([contamination_peaks, well_contamination_peaks]) carryover_peaks = np.hstack([carryover_peaks, well_carryover_peaks]) if well.original_sample_name in ('FAM HI', 'FAM 350nM', 'FAM LO', 'FAM 40nM', 'VIC HI', 'VIC 350nM'): if well.original_sample_name.startswith('VIC'): add_to = vic_bounds amps = vic_amplitudes(peaks) else: add_to = fam_bounds amps = fam_amplitudes(peaks) min_width_gate, max_width_gate = well_static_width_gates(well) mean = np.mean(amps) std = np.std(amps) lower_bound = mean - 3*std upper_bound = mean + 3*std add_to.append((lower_bound, upper_bound, min_width_gate, max_width_gate)) return contamination_peaks, gated_contamination_peaks, carryover_peaks, num_wells, carryover_well_peak_dict
def polydisperse_peaks(well, channel_num, threshold=None, pct_boundary=0.3, exclude_min_amplitude_peaks=True): """ Returns a 3-tuple (4-tuple, 4-tuple, 2-tuple). The first 4-tuple is: * positive rain above the width gates. * middle rain above the width gates. * middle rain below the width gates. * negative rain below the width gates. The second 4-tuple is: * positive rain boundary * middle rain upper boundary (can be None) * middle rain lower boundary (can be None) * negative rain boundary The last 2-tuple is: * computed min width gate * computed max width gate Positives & negatives are computed on the specified channel number. """ if not threshold: threshold = well.channels[channel_num].statistics.threshold if not threshold: threshold = None # filter out min_amplitude_peaks if exclude_min_amplitude_peaks: peaks = above_min_amplitude_peaks(well) else: peaks = well.peaks p_plus, p, p_minus, pos, middle_high, middle_low, neg = \ rain_pvalues_thresholds(peaks, channel_num=channel_num, threshold=threshold, pct_boundary=pct_boundary) min_gate, max_gate = well_static_width_gates(well) pos_peaks = np.extract( reduce(np.logical_and, (channel_widths(peaks, channel_num) > max_gate, channel_amplitudes(peaks, channel_num) > pos)), peaks) if middle_high and middle_low: midhigh_peaks = np.extract( reduce(np.logical_and, (channel_widths(peaks, channel_num) > max_gate, reduce(np.logical_and, (channel_amplitudes(peaks, channel_num) < middle_high, channel_amplitudes(peaks, channel_num) > middle_low)))), peaks) midlow_peaks = np.extract( reduce(np.logical_and, (channel_widths(peaks, channel_num) < min_gate, reduce(np.logical_and, (channel_amplitudes(peaks, channel_num) < middle_high, channel_amplitudes(peaks, channel_num) > middle_low)))), peaks) else: midhigh_peaks = np.ndarray([0],dtype=peak_dtype(2)) midlow_peaks = np.ndarray([0],dtype=peak_dtype(2)) neg_peaks = np.extract( reduce(np.logical_and, (channel_widths(peaks, channel_num) < min_gate, channel_amplitudes(peaks, channel_num) < neg)), peaks) return ((pos_peaks, midhigh_peaks, midlow_peaks, neg_peaks), (pos, middle_high, middle_low, neg), (min_gate, max_gate))