Example #1
0
    def test_get_beta_plate_metrics(self):
        cnv_metrics = get_beta_plate_metrics(self.cnv_dbplate, self.cnv_qlplate)
        # assert some stuff
        assert len([wm for wm in cnv_metrics.well_metrics if wm.expected_cnv is not None]) == 21
        assert len([wm for wm in cnv_metrics.well_metrics if wm.expected_cnv == 6]) == 3
        assert len([wm for wm in cnv_metrics.well_metrics if wm.expected_cnv == 2]) == 6
        assert (
            len([wm for wm in cnv_metrics.well_metrics if wm.well_channel_metrics[0].auto_threshold_expected is False])
            == 3
        )
        assert (
            len([wm for wm in cnv_metrics.well_metrics if wm.well_channel_metrics[1].auto_threshold_expected is False])
            == 3
        )

        # copied verbatim from TestMetricsMethods
        wd = cnv_metrics.well_metric_name_dict
        assert wd["A01"].expected_cnv == 1
        assert wd["A02"].expected_cnv is None
        assert wd["B01"].expected_cnv == 2
        assert wd["B02"].expected_cnv == 2
        assert wd["F02"].expected_cnv == 3
        assert wd["F01"].expected_cnv == 6
        assert wd["E02"].expected_cnv == 4
        assert wd["E03"].expected_cnv == 5

        assert wd["A01"].cnv > 0.9
        assert wd["A01"].cnv < 1.1
        assert wd["A02"].cnv is None
        assert wd["B01"].cnv > 1.8
        assert wd["B01"].cnv < 2.2
        assert wd["B02"].cnv > 1.8
        assert wd["B02"].cnv < 2.2
        assert wd["F02"].cnv > 2.8
        assert wd["F02"].cnv < 3.2
        assert wd["F01"].cnv > 5.7
        assert wd["F01"].cnv < 6.3
        assert wd["E02"].cnv > 3.8
        assert wd["E02"].cnv < 4.2
        assert wd["E03"].cnv > 4.8
        assert wd["E03"].cnv < 5.2

        assert len([wm for wm in cnv_metrics.well_metrics if wm.well_channel_metrics[0].threshold == 0]) == 3
        assert len([wm for wm in cnv_metrics.well_metrics if wm.well_channel_metrics[1].threshold == 0]) == 3

        duplex_metrics = get_beta_plate_metrics(self.duplex_dbplate, self.duplex_qlplate)
        assert (
            len([wm for wm in duplex_metrics.well_metrics if wm.well_channel_metrics[0].expected_concentration == 0])
            == 4
        )
        assert (
            len([wm for wm in duplex_metrics.well_metrics if wm.well_channel_metrics[1].expected_concentration == 1000])
            == 24
        )
        assert (
            len(
                [
                    wm
                    for wm in duplex_metrics.well_metrics
                    if wm.well_channel_metrics[0].auto_threshold_expected is False
                ]
            )
            == 4
        )
        assert (
            len(
                [
                    wm
                    for wm in duplex_metrics.well_metrics
                    if wm.well_channel_metrics[1].auto_threshold_expected is False
                ]
            )
            == 0
        )

        # copied verbatim from test_compute_conc_metrics
        wd = duplex_metrics.well_metric_name_dict
        assert wd["A01"].well_channel_metrics[0].expected_concentration == 0
        assert wd["A01"].well_channel_metrics[1].expected_concentration == 1000
        assert wd["B02"].well_channel_metrics[0].expected_concentration == 125
        assert wd["B02"].well_channel_metrics[1].expected_concentration == 1000
        assert wd["C03"].well_channel_metrics[0].expected_concentration == 250
        assert wd["C03"].well_channel_metrics[1].expected_concentration == 1000
        assert wd["D04"].well_channel_metrics[0].expected_concentration == 500
        assert wd["D04"].well_channel_metrics[1].expected_concentration == 1000
        assert wd["A05"].well_channel_metrics[0].expected_concentration == 1020
        assert wd["A05"].well_channel_metrics[1].expected_concentration == 1000
        assert wd["B06"].well_channel_metrics[0].expected_concentration == 2000
        assert wd["B06"].well_channel_metrics[1].expected_concentration == 1000

        assert wd["B02"].well_channel_metrics[0].concentration > 118
        assert wd["B02"].well_channel_metrics[0].concentration < 119
        assert wd["B02"].well_channel_metrics[1].concentration > 978
        assert wd["B02"].well_channel_metrics[1].concentration < 979

        fpfn_metrics = get_beta_plate_metrics(self.fpfn_dbplate, self.fpfn_qlplate)
        assert (
            len([wm for wm in fpfn_metrics.well_metrics if wm.well_channel_metrics[1].false_positive_peaks is not None])
            == 2
        )
        assert (
            len([wm for wm in fpfn_metrics.well_metrics if wm.well_channel_metrics[1].false_negative_peaks is not None])
            == 1
        )
        assert (
            len([wm for wm in fpfn_metrics.well_metrics if wm.well_channel_metrics[0].auto_threshold_expected is False])
            == 24
        )
        assert (
            len([wm for wm in fpfn_metrics.well_metrics if wm.well_channel_metrics[1].auto_threshold_expected is False])
            == 19
        )

        # copied verbatim from test_compute_false_positives
        wd = fpfn_metrics.well_metric_name_dict
        assert wd["F03"].well_channel_metrics[1].false_positive_peaks == 0
        assert wd["F06"].well_channel_metrics[1].false_positive_peaks == 6
        assert wd["E03"].well_channel_metrics[1].false_negative_peaks == 61

        red_metrics = get_beta_plate_metrics(self.red_dbplate, self.red_qlplate)
        # copied verbatim from test_compute_lod_false_positives
        wd = red_metrics.well_metric_name_dict
        assert (
            len([well for name, well in wd.items() if well.well_channel_metrics[0].false_positive_peaks is None]) == 21
        )
        assert (
            len([well for name, well in wd.items() if well.well_channel_metrics[0].false_positive_peaks is not None])
            == 3
        )
        assert (
            len([wm for wm in red_metrics.well_metrics if wm.well_channel_metrics[0].auto_threshold_expected is False])
            == 12
        )
        assert (
            len([wm for wm in red_metrics.well_metrics if wm.well_channel_metrics[1].auto_threshold_expected is False])
            == 21
        )

        assert wd["E10"].well_channel_metrics[0].false_positive_peaks == 5
        assert wd["E11"].well_channel_metrics[0].false_positive_peaks == 5
        assert wd["E12"].well_channel_metrics[0].false_positive_peaks == 5

        # since RED has both expected conc and false positives, just test expected conc
        assert wd["E07"].well_channel_metrics[0].expected_concentration == 0
        assert wd["E07"].well_channel_metrics[1].expected_concentration == 0
        assert wd["E10"].well_channel_metrics[0].expected_concentration == 0
        assert wd["E10"].well_channel_metrics[1].expected_concentration is None
        assert wd["F08"].well_channel_metrics[0].expected_concentration == 0.1
        assert wd["F08"].well_channel_metrics[1].expected_concentration is None
        assert wd["F12"].well_channel_metrics[0].expected_concentration == 0.5
        assert wd["F12"].well_channel_metrics[1].expected_concentration is None
        assert wd["G09"].well_channel_metrics[0].expected_concentration == 1
        assert wd["G09"].well_channel_metrics[1].expected_concentration is None
        assert wd["G11"].well_channel_metrics[0].expected_concentration == 5
        assert wd["G11"].well_channel_metrics[1].expected_concentration is None
        assert wd["H07"].well_channel_metrics[0].expected_concentration == 10
        assert wd["H07"].well_channel_metrics[1].expected_concentration is None
        assert wd["H10"].well_channel_metrics[0].expected_concentration == 10
        assert wd["H10"].well_channel_metrics[1].expected_concentration is None
Example #2
0
def write_images_stats_for_plate(dbplate, qlplate, image_source, overwrite=False, override_plate_type=None):
    """
    Write plate metrics to the database and thumbnails to local storage,
    as dictated by image_source.

    Metrics will be related to the supplied dbplate (Plate model)
    qlplate is a QLPlate object derived from reading the QLP file.
    """
    if image_source.subdir_exists(str(dbplate.id)):
        if not overwrite:
            return
    else:
        image_source.make_subdir(str(dbplate.id))
    
    max_amplitudes = (24000, 12000)
    show_only_gated = False # keep default behavior
    if qlplate:
        for well_name, qlwell in sorted(qlplate.analyzed_wells.items()):
            # TODO: common lib?
            if well_channel_automatic_classification(qlwell, 0):
                fig = plot_fam_peaks(qlwell.peaks,
                                     threshold=qlwell.channels[0].statistics.threshold,
                                     max_amplitude=max_amplitudes[0])
            else:
                fig = plot_fam_peaks(qlwell.peaks,
                                     threshold=qlwell.channels[0].statistics.threshold,
                                     threshold_color='red',
                                     max_amplitude=max_amplitudes[0],
                                     background_rgb=MANUAL_THRESHOLD_FAM_BGCOLOR)
            fig.savefig(image_source.get_path('%s/%s_%s.png' % (dbplate.id, well_name, 0)), format='png', dpi=72)
            plt_cleanup(fig)

            if well_channel_automatic_classification(qlwell, 1):
                fig = plot_vic_peaks(qlwell.peaks,
                                     threshold=qlwell.channels[1].statistics.threshold,
                                     max_amplitude=max_amplitudes[1])
            else:
                fig = plot_vic_peaks(qlwell.peaks,
                                     threshold=qlwell.channels[1].statistics.threshold,
                                     threshold_color='red',
                                     max_amplitude=max_amplitudes[1],
                                     background_rgb=MANUAL_THRESHOLD_VIC_BGCOLOR)
                
            fig.savefig(image_source.get_path('%s/%s_%s.png' % (dbplate.id, well_name, 1)), format='png', dpi=72)
            plt_cleanup(fig)

            if qlwell.clusters_defined:
                threshold_fallback = qlwell.clustering_method == QLWell.CLUSTERING_TYPE_THRESHOLD
                fig = plot_cluster_2d(qlwell.peaks,
                                      width=60,
                                      height=60,
                                      thresholds=[qlwell.channels[0].statistics.threshold,
                                                  qlwell.channels[1].statistics.threshold],
                                      boundaries=[0,0,12000,24000],
                                      show_axes=False,
                                      antialiased=True,
                                      unclassified_alpha=0.5,
                                      use_manual_clusters=not well_channel_automatic_classification(qlwell),
                                      highlight_thresholds=threshold_fallback)
                fig.savefig(image_source.get_path('%s/%s_2d.png' % (dbplate.id, well_name)), format='png', dpi=72)
                plt_cleanup(fig)
        
        pm = [pm for pm in dbplate.plate.metrics if pm.reprocess_config_id is None]
        for p in pm:
            Session.delete(p)

        plate = dbplate_tree(dbplate.plate.id)
        
        # override plate_type if supplied (another artifact of bad abstraction)
        if override_plate_type:
            plate.plate_type = override_plate_type

        # this relies on apply_template/apply_setup working correctly on plate addition
        # verify on DR 10005 plate that this works
        if plate.plate_type and plate.plate_type.code in beta_plate_types:
            plate_metrics = get_beta_plate_metrics(plate, qlplate)
        else:
            plate_metrics = process_plate(plate, qlplate)
        Session.add(plate_metrics)