1#ifndef SCRAN_QC_RNA_QUALITY_CONTROL_HPP
2#define SCRAN_QC_RNA_QUALITY_CONTROL_HPP
41template<
typename Sum_ =
double,
typename Detected_ =
int,
typename Proportion_ =
double>
91template<
typename Value_,
typename Index_,
typename Subset_,
typename Sum_,
typename Detected_,
typename Proportion_>
94 const std::vector<Subset_>& subsets,
99 size_t nsubsets = subsets.size();
105 constexpr bool same_type = std::is_same<Sum_, Proportion_>::value;
106 typename std::conditional<same_type, bool, std::vector<std::vector<Sum_> > >::type placeholder_subset;
110 if constexpr(same_type) {
113 placeholder_subset.resize(nsubsets);
115 for (
size_t s = 0; s < nsubsets; ++s) {
116 auto& b = placeholder_subset[s];
127 for (
size_t s = 0 ; s < nsubsets; ++s) {
131 for (Index_ c = 0; c < NC; ++c) {
132 dest[c] =
static_cast<Proportion_
>(src[c]) /
static_cast<Proportion_
>(tmp.
sum[c]);
144template<
typename Sum_ =
double,
typename Detected_ =
int,
typename Proportion_ =
double>
182template<
typename Sum_ =
double,
typename Detected_ =
int,
typename Proportion_ =
double,
typename Value_ =
double,
typename Index_ =
int,
typename Subset_ = const u
int8_t*>
184 auto NC = mat.
ncol();
189#ifdef SCRAN_QC_TEST_INIT
193 x.
sum = output.
sum.data();
196#ifdef SCRAN_QC_TEST_INIT
202 size_t nsubsets = subsets.size();
205 for (
size_t s = 0; s < nsubsets; ++s) {
207#ifdef SCRAN_QC_TEST_INIT
246template<
typename Float_,
class Host_,
typename Sum_,
typename Detected_,
typename Proportion_,
typename BlockSource_>
248 constexpr bool unblocked = std::is_same<BlockSource_, bool>::value;
250 if constexpr(unblocked) {
251 return std::vector<Float_>(n);
262 host.get_sum() = [&]{
263 if constexpr(unblocked) {
276 host.get_detected() = [&]{
277 if constexpr(unblocked) {
291 host.get_subset_proportion().resize(nsubsets);
292 for (
size_t s = 0; s < nsubsets; ++s) {
294 host.get_subset_proportion()[s] = [&]{
295 if constexpr(unblocked) {
305template<
class Host_,
typename Sum_,
typename Detected_,
typename Proportion_,
typename BlockSource_,
typename Output_>
306void rna_filter(
const Host_& host,
size_t n,
const ComputeRnaQcMetricsBuffers<Sum_, Detected_, Proportion_>& metrics, BlockSource_ block, Output_* output) {
307 constexpr bool unblocked = std::is_same<BlockSource_, bool>::value;
308 std::fill_n(output, n, 1);
310 const auto& sum = host.get_sum();
311 for (
size_t i = 0; i < n; ++i) {
313 if constexpr(unblocked) {
316 return sum[block[i]];
319 output[i] = output[i] && (metrics.sum[i] >= thresh);
322 const auto& detected = host.get_detected();
323 for (
size_t i = 0; i < n; ++i) {
325 if constexpr(unblocked) {
328 return detected[block[i]];
331 output[i] = output[i] && (metrics.detected[i] >= thresh);
334 size_t nsubsets = metrics.subset_proportion.size();
335 for (
size_t s = 0; s < nsubsets; ++s) {
336 auto sub = metrics.subset_proportion[s];
337 const auto& sthresh = host.get_subset_proportion()[s];
338 for (
size_t i = 0; i < n; ++i) {
340 if constexpr(unblocked) {
343 return sthresh[block[i]];
346 output[i] = output[i] && (sub[i] <= thresh);
351template<
typename Sum_,
typename Detected_,
typename Proportion_>
352ComputeRnaQcMetricsBuffers<const Sum_, const Detected_, const Proportion_> to_buffer(
const ComputeRnaQcMetricsResults<Sum_, Detected_, Proportion_>& metrics) {
353 ComputeRnaQcMetricsBuffers<const Sum_, const Detected_, const Proportion_> buffer;
354 buffer.sum = metrics.sum.data();
355 buffer.detected = metrics.detected.data();
356 buffer.subset_proportion.reserve(metrics.subset_proportion.size());
357 for (
const auto& s : metrics.subset_proportion) {
358 buffer.subset_proportion.push_back(s.data());
372template<
typename Float_ =
double>
394 return my_subset_proportion;
416 return my_subset_proportion;
421 Float_ my_detected = 0;
422 std::vector<Float_> my_subset_proportion;
436 template<
typename Sum_,
typename Detected_,
typename Proportion_,
typename Output_>
438 internal::rna_filter(*
this, num, metrics,
false, output);
451 template<
typename Sum_,
typename Detected_,
typename Proportion_,
typename Output_>
453 return filter(metrics.
sum.size(), internal::to_buffer(metrics), output);
465 template<
typename Output_ = u
int8_t,
typename Sum_ =
double,
typename Detected_ =
int,
typename Proportion_ =
double>
467 std::vector<Output_> output(metrics.
sum.size()
468#ifdef SCRAN_QC_TEST_INIT
472 filter(metrics, output.data());
494template<
typename Float_ =
double,
typename Sum_ =
double,
typename Detected_ =
int,
typename Proportion_ =
double>
497 internal::rna_populate<Float_>(output, num, metrics,
false, options);
516template<
typename Float_ =
double,
typename Sum_ =
double,
typename Detected_ =
int,
typename Proportion_ =
double>
525template<
typename Float_ =
double>
550 return my_subset_proportion;
575 return my_subset_proportion;
579 std::vector<Float_> my_sum;
580 std::vector<Float_> my_detected;
581 std::vector<std::vector<Float_> > my_subset_proportion;
599 template<
typename Index_,
typename Sum_,
typename Detected_,
typename Proportion_,
typename Block_,
typename Output_>
601 internal::rna_filter(*
this, num, metrics, block, output);
618 template<
typename Sum_,
typename Detected_,
typename Proportion_,
typename Block_,
typename Output_>
620 return filter(metrics.
sum.size(), internal::to_buffer(metrics), block, output);
637 template<
typename Output_ = u
int8_t,
typename Sum_ =
double,
typename Detected_ =
int,
typename Proportion_ =
double,
typename Block_ =
int>
639 std::vector<Output_> output(metrics.
sum.size()
640#ifdef SCRAN_QC_TEST_INIT
644 filter(metrics, block, output.data());
663template<
typename Float_ =
double,
typename Sum_,
typename Detected_,
typename Proportion_,
typename Block_>
671 internal::rna_populate<Float_>(output, num, metrics, block, options);
688template<
typename Float_ =
double,
typename Sum_,
typename Detected_,
typename Proportion_,
typename Block_>
Define QC filter thresholds using a MAD-based approach.
Filter for high-quality cells using RNA-based metrics with blocking.
Definition rna_quality_control.hpp:526
void filter(Index_ num, const ComputeRnaQcMetricsBuffers< Sum_, Detected_, Proportion_ > &metrics, const Block_ *block, Output_ *output) const
Definition rna_quality_control.hpp:600
std::vector< Float_ > & get_detected()
Definition rna_quality_control.hpp:565
void filter(const ComputeRnaQcMetricsResults< Sum_, Detected_, Proportion_ > &metrics, const Block_ *block, Output_ *output) const
Definition rna_quality_control.hpp:619
std::vector< Output_ > filter(const ComputeRnaQcMetricsResults< Sum_, Detected_, Proportion_ > &metrics, const Block_ *block) const
Definition rna_quality_control.hpp:638
std::vector< std::vector< Float_ > > & get_subset_proportion()
Definition rna_quality_control.hpp:574
std::vector< Float_ > & get_sum()
Definition rna_quality_control.hpp:557
const std::vector< Float_ > & get_detected() const
Definition rna_quality_control.hpp:540
const std::vector< Float_ > & get_sum() const
Definition rna_quality_control.hpp:532
const std::vector< std::vector< Float_ > > & get_subset_proportion() const
Definition rna_quality_control.hpp:549
Filter for high-quality cells using RNA-based metrics.
Definition rna_quality_control.hpp:373
Float_ & get_detected()
Definition rna_quality_control.hpp:407
Float_ get_detected() const
Definition rna_quality_control.hpp:385
const std::vector< Float_ > & get_subset_proportion() const
Definition rna_quality_control.hpp:393
std::vector< Float_ > & get_subset_proportion()
Definition rna_quality_control.hpp:415
void filter(const ComputeRnaQcMetricsResults< Sum_, Detected_, Proportion_ > &metrics, Output_ *output) const
Definition rna_quality_control.hpp:452
Float_ get_sum() const
Definition rna_quality_control.hpp:378
void filter(size_t num, const ComputeRnaQcMetricsBuffers< Sum_, Detected_, Proportion_ > &metrics, Output_ *output) const
Definition rna_quality_control.hpp:437
Float_ & get_sum()
Definition rna_quality_control.hpp:400
std::vector< Output_ > filter(const ComputeRnaQcMetricsResults< Sum_, Detected_, Proportion_ > &metrics) const
Definition rna_quality_control.hpp:466
virtual Index_ ncol() const=0
Simple quality control for single-cell data.
Definition adt_quality_control.hpp:20
void compute_rna_qc_metrics(const tatami::Matrix< Value_, Index_ > &mat, const std::vector< Subset_ > &subsets, const ComputeRnaQcMetricsBuffers< Sum_, Detected_, Proportion_ > &output, const ComputeRnaQcMetricsOptions &options)
Definition rna_quality_control.hpp:92
std::vector< ChooseFilterThresholdsResults< Float_ > > choose_filter_thresholds_blocked(const std::vector< FindMedianMadResults< Float_ > > mms, const ChooseFilterThresholdsOptions &options)
Definition choose_filter_thresholds.hpp:217
RnaQcBlockedFilters< Float_ > compute_rna_qc_filters_blocked(size_t num, const ComputeRnaQcMetricsBuffers< Sum_, Detected_, Proportion_ > &metrics, const Block_ *block, const ComputeRnaQcFiltersOptions &options)
Definition rna_quality_control.hpp:664
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
ChooseFilterThresholdsResults< Float_ > choose_filter_thresholds(const FindMedianMadResults< Float_ > &mm, const ChooseFilterThresholdsOptions &options)
Definition choose_filter_thresholds.hpp:133
RnaQcFilters< Float_ > compute_rna_qc_filters(size_t num, const ComputeRnaQcMetricsBuffers< Sum_, Detected_, Proportion_ > &metrics, const ComputeRnaQcFiltersOptions &options)
Definition rna_quality_control.hpp:495
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
bool lower
Definition choose_filter_thresholds.hpp:25
Options for compute_rna_qc_filters().
Definition rna_quality_control.hpp:221
double detected_num_mads
Definition rna_quality_control.hpp:226
double subset_proportion_num_mads
Definition rna_quality_control.hpp:238
double sum_num_mads
Definition rna_quality_control.hpp:232
Buffers for compute_rna_qc_metrics().
Definition rna_quality_control.hpp:42
Detected_ * detected
Definition rna_quality_control.hpp:53
Sum_ * sum
Definition rna_quality_control.hpp:47
std::vector< Proportion_ * > subset_proportion
Definition rna_quality_control.hpp:60
Options for compute_rna_qc_metrics().
Definition rna_quality_control.hpp:25
int num_threads
Definition rna_quality_control.hpp:30
Results of compute_rna_qc_metrics().
Definition rna_quality_control.hpp:145
std::vector< Sum_ > sum
Definition rna_quality_control.hpp:149
std::vector< Detected_ > detected
Definition rna_quality_control.hpp:154
std::vector< std::vector< Proportion_ > > subset_proportion
Definition rna_quality_control.hpp:160
Buffers for per_cell_qc_metrics().
Definition per_cell_qc_metrics.hpp:75
Sum_ * sum
Definition per_cell_qc_metrics.hpp:90
Detected_ * detected
Definition per_cell_qc_metrics.hpp:96
std::vector< Sum_ * > subset_sum
Definition per_cell_qc_metrics.hpp:116
Options for per_cell_qc_metrics().
Definition per_cell_qc_metrics.hpp:22
int num_threads
Definition per_cell_qc_metrics.hpp:63