Example #1
0
def well_fragmentation_probability(well):
    """
    Returns the percentage likelihood per droplet that the molecules in
    the two channels are unlinked (TODO: refine definition)

    Returns the 3-tuple (probability, ci_low, ci_high).  If thresholds are
    not present, then the function will return the 3-tuple (None, None, None)
    """
    from qtools.lib.nstats.frag import prob_of_frag

    fam_threshold = well.channels[0].statistics.threshold
    vic_threshold = well.channels[1].statistics.threshold

    if not (fam_threshold and vic_threshold):
        return (None, None, None)
    
    ok_peaks = accepted_peaks(well)
    fampos_vicpos, fampos_vicneg, famneg_vicpos, famneg_vicneg = \
        cluster_2d(ok_peaks, fam_threshold, vic_threshold)
    
    retarr = prob_of_frag(len(fampos_vicneg), len(fampos_vicpos),
                          len(famneg_vicneg), len(famneg_vicpos),
                          len(ok_peaks))
    if not retarr:
        return (None, None, None)
    else:
        pct_interval, vic_interval, fam_interval, linked_interval = retarr
        return pct_interval
Example #2
0
    def view(self, id=None, *args, **kwargs):
        file_ok = False
        qlwell = None
        try:
            qlwell = self.__qlwell_from_threshold_form(id)
            file_ok = True
        except IOError:
            path = self.__plate_path()
            session['flash'] = "Could not read file: %s" % path
            session['flash_class'] = 'error'

        
        # this may be easier/more efficient to ascertain via direct SQL, but going ORM route
        well_ids = Session.query(QLBWell.id, QLBWell.well_name).filter(and_(QLBWell.plate_id == c.well.plate_id,
                                                      QLBWell.file_id != None)).\
                                          order_by(QLBWell.id).all()
        
        current_idx = -1
        for idx, (id, well_name) in enumerate(well_ids):
            if c.well.id == id:
                current_idx = idx
                break
        
        if current_idx == 0:
            c.prev_well_id = None
            c.prev_well_name = None
        else:
            c.prev_well_id, c.prev_well_name = well_ids[current_idx-1]
        
        if current_idx == len(well_ids)-1:
            c.next_well_id = None
            c.next_well_name = None
        else:
            c.next_well_id, c.next_well_name = well_ids[current_idx+1]
        
        thresholds = [self.form_result['fam_threshold'], self.form_result['vic_threshold']]
        c.fam_threshold, c.vic_threshold = thresholds
        for i, t in enumerate(thresholds):
            if t == 0:
                thresholds[i] = None
        
        max_amplitudes = [self.form_result['max_fam_amplitude'], self.form_result['max_vic_amplitude']]
        c.max_fam_amplitude, c.max_vic_amplitude = max_amplitudes

        if file_ok and qlwell:
            # get at well first
            statistics, cluster_data = stats_for_qlp_well(qlwell, compute_clusters=True, override_thresholds=thresholds)
            fam_cnv = get_cnv(statistics[0].positives, statistics[0].negatives,
                              statistics[1].positives, statistics[1].negatives,
                              reference_copies=statistics.ref_copy_num)
            vic_cnv = get_cnv(statistics[1].positives, statistics[1].negatives,
                              statistics[0].positives, statistics[0].negatives,
                              reference_copies=statistics.ref_copy_num)

            c.cluster_data = cluster_data
            c.statistics = statistics
            c.fam_cnv = fam_cnv
            c.vic_cnv = vic_cnv
            c.alg_version = statistics.alg_version

            if h.wowo('numpy.well_frag') and hasattr(c, 'cluster_data'):
                from qtools.lib.nstats import frag
                from pyqlb.nstats import chisquare_2d, zscore_2d, linkage_2d, balance_score_2d
                from pyqlb.nstats.well import BSCORE_MINIMUM_CLUSTER_SIZE
                n00 = len(c.cluster_data['negative_peaks']['negative_peaks'])
                n01 = len(c.cluster_data['negative_peaks']['positive_peaks'])
                n10 = len(c.cluster_data['positive_peaks']['negative_peaks'])
                n11 = len(c.cluster_data['positive_peaks']['positive_peaks'])

                frag_stats = frag.prob_of_frag(n10,n11,n00,n01,
                                               statistics[0].positives+statistics[0].negatives)
                c.frag_stats = frag_stats

                chi_stat, chi_p = chisquare_2d(n11,n10,n01,n00,
                                               yates_correction=True)
                c.chi_stat = chi_stat
                c.chi_p = chi_p

                z_fpvn, z_fpvp_vn, z_fpvp_fn, z_fnvp = \
                    zscore_2d(n11,n10,n01,n00)

                c.zscores = (z_fpvn, z_fpvp_vn, z_fpvp_fn, z_fnvp)
                c.linkage = linkage_2d(n11,n10,n01,n00)

                c.bscore =  balance_score_2d(n00,n01,n10,n11)[0]
                #if n00 >= BSCORE_MINIMUM_CLUSTER_SIZE \
                #   and n01 >= BSCORE_MINIMUM_CLUSTER_SIZE \
                #   and n10 >= BSCORE_MINIMUM_CLUSTER_SIZE \
                #   and n11 >= BSCORE_MINIMUM_CLUSTER_SIZE:
               
        return render('/well/view.html')