1#ifndef SCRAN_QC_CRISPR_QUALITY_CONTROL_HPP
2#define SCRAN_QC_CRISPR_QUALITY_CONTROL_HPP
42template<
typename Sum_ =
double,
typename Detected_ =
int,
typename Value_ =
double,
typename Index_ =
int>
98template<
typename Value_,
typename Index_,
typename Sum_,
typename Detected_>
122template<
typename Sum_ =
double,
typename Detected_ =
int,
typename Value_ =
double,
typename Index_ =
int>
160template<
typename Sum_ =
double,
typename Detected_ =
int,
typename Value_ =
double,
typename Index_ =
int>
165 auto NC = mat.
ncol();
170#ifdef SCRAN_QC_TEST_INIT
174 x.
sum = output.
sum.data();
177#ifdef SCRAN_QC_TEST_INIT
184#ifdef SCRAN_QC_TEST_INIT
191#ifdef SCRAN_QC_TEST_INIT
217template<
typename Float_,
class Host_,
typename Sum_,
typename Detected_,
typename Value_,
typename Index_,
typename BlockSource_>
219 constexpr bool unblocked = std::is_same<BlockSource_, bool>::value;
221 if constexpr(unblocked) {
222 return std::vector<Float_>(n);
229 static_assert(std::is_floating_point<Float_>::value);
230 std::vector<Float_> maxprop;
232 for (
size_t i = 0; i < n; ++i) {
233 maxprop.push_back(
static_cast<Float_
>(res.
max_value[i]) /
static_cast<Float_
>(res.
sum[i]));
239 if constexpr(unblocked) {
246 for (
size_t i = 0; i < n; ++i) {
248 if constexpr(unblocked){
249 return prop_res.median;
251 return prop_res[block[i]].median;
254 if (maxprop[i] >= limit) {
257 maxprop[i] = std::numeric_limits<Float_>::quiet_NaN();
266 host.get_max_value() = [&]{
267 if constexpr(unblocked) {
275template<
class Host_,
typename Sum_,
typename Detected_,
typename Value_,
typename Index_,
typename BlockSource_,
typename Output_>
276void crispr_filter(
const Host_& host,
size_t n,
const ComputeCrisprQcMetricsBuffers<Sum_, Detected_, Value_, Index_>& metrics, BlockSource_ block, Output_* output) {
277 constexpr bool unblocked = std::is_same<BlockSource_, bool>::value;
278 std::fill_n(output, n, 1);
280 const auto& mv = host.get_max_value();
281 for (
size_t i = 0; i < n; ++i) {
283 if constexpr(unblocked) {
289 output[i] = output[i] && (metrics.max_value[i] >= thresh);
293template<
typename Sum_,
typename Detected_,
typename Value_,
typename Index_>
294ComputeCrisprQcMetricsBuffers<const Sum_, const Detected_, const Value_, const Index_> to_buffer(
const ComputeCrisprQcMetricsResults<Sum_, Detected_, Value_, Index_>& metrics) {
295 ComputeCrisprQcMetricsBuffers<const Sum_, const Detected_, const Value_, const Index_> buffer;
296 buffer.sum = metrics.sum.data();
297 buffer.detected = metrics.detected.data();
298 buffer.max_value = metrics.max_value.data();
299 buffer.max_index = metrics.max_index.data();
314template<
typename Float_ =
double>
332 Float_ my_max_value = 0;
347 template<
typename Sum_,
typename Detected_,
typename Value_,
typename Index_,
typename Output_>
349 internal::crispr_filter(*
this, num, metrics,
false, output);
363 template<
typename Sum_,
typename Detected_,
typename Value_,
typename Index_,
typename Output_>
365 return filter(metrics.
max_value.size(), internal::to_buffer(metrics), output);
378 template<
typename Output_ = u
int8_t,
typename Sum_ =
double,
typename Detected_ =
int,
typename Value_ =
double,
typename Index_ =
int>
380 std::vector<Output_> output(metrics.
max_value.size()
381#ifdef SCRAN_QC_TEST_INIT
385 filter(metrics, output.data());
423template<
typename Float_ =
double,
typename Sum_,
typename Detected_,
typename Value_,
typename Index_>
430 internal::crispr_populate<Float_>(output, num, metrics,
false, options);
446template<
typename Float_ =
double,
typename Sum_ =
double,
typename Detected_ =
int,
typename Value_ =
double,
typename Index_ =
int>
459template<
typename Float_ =
double>
479 std::vector<Float_> my_sum;
480 std::vector<Float_> my_max_value;
498 template<
typename Sum_,
typename Detected_,
typename Value_,
typename Index_,
typename Block_,
typename Output_>
500 internal::crispr_filter(*
this, num, metrics, block, output);
517 template<
typename Sum_,
typename Detected_,
typename Value_,
typename Index_,
typename Block_,
typename Output_>
519 filter(metrics.
max_value.size(), internal::to_buffer(metrics), block, output);
536 template<
typename Output_ = u
int8_t,
typename Sum_ =
double,
typename Detected_ =
int,
typename Value_ =
double,
typename Index_ =
int,
typename Block_ =
int>
538 std::vector<Output_> output(metrics.
max_value.size()
539#ifdef SCRAN_QC_TEST_INIT
543 filter(metrics, block, output.data());
567template<
typename Float_ =
double,
typename Sum_,
typename Detected_,
typename Value_,
typename Index_,
typename Block_>
575 internal::crispr_populate<Float_>(output, num, metrics, block, options);
593template<
typename Float_ =
double,
typename Sum_,
typename Detected_,
typename Value_,
typename Index_,
typename Block_>
Define QC filter thresholds using a MAD-based approach.
Filter on using CRISPR-based QC metrics with blocking.
Definition crispr_quality_control.hpp:460
std::vector< Output_ > filter(const ComputeCrisprQcMetricsResults< Sum_, Detected_, Value_, Index_ > &metrics, const Block_ *block) const
Definition crispr_quality_control.hpp:537
void filter(size_t num, const ComputeCrisprQcMetricsBuffers< Sum_, Detected_, Value_, Index_ > &metrics, const Block_ *block, Output_ *output) const
Definition crispr_quality_control.hpp:499
std::vector< Float_ > & get_max_value()
Definition crispr_quality_control.hpp:474
void filter(const ComputeCrisprQcMetricsResults< Sum_, Detected_, Value_, Index_ > &metrics, const Block_ *block, Output_ *output) const
Definition crispr_quality_control.hpp:518
const std::vector< Float_ > & get_max_value() const
Definition crispr_quality_control.hpp:466
Filter for high-quality cells using CRISPR-based metrics.
Definition crispr_quality_control.hpp:315
std::vector< Output_ > filter(const ComputeCrisprQcMetricsResults< Sum_, Detected_, Value_, Index_ > &metrics) const
Definition crispr_quality_control.hpp:379
void filter(const ComputeCrisprQcMetricsResults< Sum_, Detected_, Value_, Index_ > &metrics, Output_ *output) const
Definition crispr_quality_control.hpp:364
Float_ get_max_value() const
Definition crispr_quality_control.hpp:320
Float_ & get_max_value()
Definition crispr_quality_control.hpp:327
void filter(size_t num, const ComputeCrisprQcMetricsBuffers< Sum_, Detected_, Value_, Index_ > &metrics, Output_ *output) const
Definition crispr_quality_control.hpp:348
virtual Index_ ncol() const=0
Simple quality control for single-cell data.
Definition adt_quality_control.hpp:20
CrisprQcBlockedFilters< Float_ > compute_crispr_qc_filters_blocked(size_t num, const ComputeCrisprQcMetricsBuffers< Sum_, Detected_, Value_, Index_ > &metrics, const Block_ *block, const ComputeCrisprQcFiltersOptions &options)
Definition crispr_quality_control.hpp:568
std::vector< ChooseFilterThresholdsResults< Float_ > > choose_filter_thresholds_blocked(const std::vector< FindMedianMadResults< Float_ > > mms, const ChooseFilterThresholdsOptions &options)
Definition choose_filter_thresholds.hpp:217
void compute_crispr_qc_metrics(const tatami::Matrix< Value_, Index_ > &mat, const ComputeCrisprQcMetricsBuffers< Sum_, Detected_, Value_, Index_ > &output, const ComputeCrisprQcMetricsOptions &options)
Definition crispr_quality_control.hpp:99
FindMedianMadResults< Float_ > find_median_mad(Index_ num, Float_ *metrics, const FindMedianMadOptions &options)
Definition find_median_mad.hpp:79
void per_cell_qc_metrics(const tatami::Matrix< Value_, Index_ > &mat, const std::vector< Subset_ > &subsets, const PerCellQcMetricsBuffers< Sum_, Detected_, Value_, Index_ > &output, const PerCellQcMetricsOptions &options)
Definition per_cell_qc_metrics.hpp:810
CrisprQcFilters< Float_ > compute_crispr_qc_filters(size_t num, const ComputeCrisprQcMetricsBuffers< Sum_, Detected_, Value_, Index_ > &metrics, const ComputeCrisprQcFiltersOptions &options)
Definition crispr_quality_control.hpp:424
ChooseFilterThresholdsResults< Float_ > choose_filter_thresholds(const FindMedianMadResults< Float_ > &mm, const ChooseFilterThresholdsOptions &options)
Definition choose_filter_thresholds.hpp:133
std::vector< FindMedianMadResults< Output_ > > find_median_mad_blocked(Index_ num, const Value_ *metrics, const Block_ *block, FindMedianMadWorkspace< Output_, Index_ > *workspace, const FindMedianMadOptions &options)
Definition find_median_mad.hpp:269
Compute per-cell quality control metrics.
Options for choose_filter_thresholds().
Definition choose_filter_thresholds.hpp:20
bool upper
Definition choose_filter_thresholds.hpp:31
bool log
Definition choose_filter_thresholds.hpp:56
double num_mads
Definition choose_filter_thresholds.hpp:38
Options for compute_crispr_qc_filters().
Definition crispr_quality_control.hpp:204
double max_value_num_mads
Definition crispr_quality_control.hpp:209
Buffers for compute_crispr_qc_metrics().
Definition crispr_quality_control.hpp:43
Sum_ * sum
Definition crispr_quality_control.hpp:48
Detected_ * detected
Definition crispr_quality_control.hpp:54
Index_ * max_index
Definition crispr_quality_control.hpp:66
Value_ * max_value
Definition crispr_quality_control.hpp:60
Options for compute_crispr_qc_metrics().
Definition crispr_quality_control.hpp:25
int num_threads
Definition crispr_quality_control.hpp:30
Results of compute_crispr_qc_metrics().
Definition crispr_quality_control.hpp:123
std::vector< Index_ > max_index
Definition crispr_quality_control.hpp:142
std::vector< Value_ > max_value
Definition crispr_quality_control.hpp:137
std::vector< Detected_ > detected
Definition crispr_quality_control.hpp:132
std::vector< Sum_ > sum
Definition crispr_quality_control.hpp:127
Buffers for per_cell_qc_metrics().
Definition per_cell_qc_metrics.hpp:75
Value_ * max_value
Definition per_cell_qc_metrics.hpp:108
Index_ * max_index
Definition per_cell_qc_metrics.hpp:102
Sum_ * sum
Definition per_cell_qc_metrics.hpp:90
Detected_ * detected
Definition per_cell_qc_metrics.hpp:96
Options for per_cell_qc_metrics().
Definition per_cell_qc_metrics.hpp:22
int num_threads
Definition per_cell_qc_metrics.hpp:63