1#ifndef SCRAN_QC_PER_CELL_QC_METRICS_HPP
2#define SCRAN_QC_PER_CELL_QC_METRICS_HPP
10#include "tatami_stats/tatami_stats.hpp"
74template<
typename Sum_,
typename Detected_,
typename Value_,
typename Index_>
132template<
typename Value_,
typename Index_,
typename Subset_,
typename Sum_,
typename Detected_>
135 const std::vector<Subset_>&
subsets,
140 if (!
output.subset_sum.empty() || !
output.subset_detected.empty()) {
141 if constexpr(std::is_pointer<Subset_>::value) {
159 auto NR =
mat.nrow();
189 if (max_value <
ptr[
r]) {
197 output.max_index[
c] = max_index;
200 output.max_value[
c] = max_value;
204 if (!
output.subset_sum.empty() || !
output.subset_detected.empty()) {
206 const auto&
sub = [&]() {
207 if constexpr(std::is_pointer<Subset_>::value) {
222 if (!
output.subset_detected.empty() &&
output.subset_detected[
s]) {
232 },
mat.ncol(), num_threads);
235template<
typename Index_,
typename Subset_,
typename Sum_,
typename Detected_,
typename Value_>
239 if (!
output.subset_sum.empty() || !
output.subset_detected.empty()) {
240 if constexpr(!std::is_pointer<Subset_>::value) {
255template<
typename Value_,
typename Index_,
typename Subset_,
typename Sum_,
typename Detected_>
258 const std::vector<Subset_>&
subsets,
265 auto NR =
mat.nrow();
294 max_value =
range.value[0];
295 max_index =
range.index[0];
297 if (max_value <
range.value[
i]) {
298 max_value =
range.value[
i];
299 max_index =
range.index[
i];
303 if (max_value <= 0 &&
range.number <
NR) {
310 }
else if (
range.value[
i] == 0) {
324 output.max_index[
c] = max_index;
327 output.max_value[
c] = max_value;
331 if (!
output.subset_sum.empty() || !
output.subset_detected.empty()) {
333 const auto&
sub = [&]() {
334 if constexpr(std::is_pointer<Subset_>::value) {
349 if (!
output.subset_detected.empty() &&
output.subset_detected[
s]) {
359 },
mat.ncol(), num_threads);
362template<
typename Sum_,
typename Detected_,
typename Value_,
typename Index_>
365 PerCellQcMetricsRunningBuffers(
const PerCellQcMetricsBuffers<Sum_, Detected_, Value_, Index_>& output,
int thread, Index_ start, Index_ len) {
367 my_sum = tatami_stats::LocalOutputBuffer<Sum_>(thread, start, len, output.sum);
370 if (output.detected) {
371 my_detected = tatami_stats::LocalOutputBuffer<Detected_>(thread, start, len, output.detected);
374 if (output.max_value) {
375 my_max_value = tatami_stats::LocalOutputBuffer<Value_>(thread, start, len, output.max_value);
376 }
else if (output.max_index) {
377 my_holding_max_value.resize(len);
380 if (output.max_index) {
381 my_max_index = tatami_stats::LocalOutputBuffer<Index_>(thread, start, len, output.max_index);
384 my_subset_sum.resize(output.subset_sum.size());
385 for (
size_t s = 0, send = output.subset_sum.size(); s < send; ++s) {
386 if (output.subset_sum[s]) {
387 my_subset_sum[s] = tatami_stats::LocalOutputBuffer<Sum_>(thread, start, len, output.subset_sum[s]);
391 my_subset_detected.resize(output.subset_detected.size());
392 for (
size_t s = 0, send = output.subset_detected.size(); s < send; ++s) {
393 if (output.subset_detected[s]) {
394 my_subset_detected[s] = tatami_stats::LocalOutputBuffer<Detected_>(thread, start, len, output.subset_detected[s]);
400 tatami_stats::LocalOutputBuffer<Sum_> my_sum;
401 tatami_stats::LocalOutputBuffer<Detected_> my_detected;
403 tatami_stats::LocalOutputBuffer<Value_> my_max_value;
404 std::vector<Value_> my_holding_max_value;
405 tatami_stats::LocalOutputBuffer<Index_> my_max_index;
407 std::vector<tatami_stats::LocalOutputBuffer<Sum_> > my_subset_sum;
408 std::vector<tatami_stats::LocalOutputBuffer<Detected_> > my_subset_detected;
412 return my_sum.data();
415 Detected_* detected_data() {
416 return my_detected.data();
419 Value_* max_value_data() {
420 auto dptr = my_max_value.data();
421 return (dptr ? dptr : my_holding_max_value.data());
424 Index_* max_index_data() {
425 return my_max_index.data();
428 std::vector<Sum_*> subset_sum_data() {
429 std::vector<Sum_*> output;
430 output.reserve(my_subset_sum.size());
431 for (
auto& s : my_subset_sum) {
432 output.push_back(s.data());
437 std::vector<Detected_*> subset_detected_data() {
438 std::vector<Detected_*> output;
439 output.reserve(my_subset_detected.size());
440 for (
auto& s : my_subset_detected) {
441 output.push_back(s.data());
450 if (my_detected.data()) {
451 my_detected.transfer();
454 if (my_max_value.data()) {
455 my_max_value.transfer();
457 if (my_max_index.data()) {
458 my_max_index.transfer();
461 for (
auto& s : my_subset_sum) {
467 for (
auto& s : my_subset_detected) {
475template<
typename Value_,
typename Index_,
typename Subset_,
typename Sum_,
typename Detected_>
478 const std::vector<Subset_>&
subsets,
485 auto NR =
mat.nrow();
536 const auto&
sub = [&]() {
537 if constexpr(std::is_pointer<Subset_>::value) {
565 },
mat.ncol(), num_threads);
568template<
typename Value_,
typename Index_,
typename Subset_,
typename Sum_,
typename Detected_>
571 const std::vector<Subset_>&
subsets,
580 auto NR =
mat.nrow();
651 const auto&
sub = [&]() {
652 if constexpr(std::is_pointer<Subset_>::value) {
680 auto NR =
mat.nrow();
703 },
mat.ncol(), num_threads);
722template<
typename Sum_,
typename Detected_,
typename Value_,
typename Index_>
809template<
typename Value_,
typename Index_,
typename Subset_,
typename Sum_,
typename Detected_>
812 const std::vector<Subset_>&
subsets,
817 if (
mat.prefer_rows()) {
823 if (
mat.prefer_rows()) {
850template<
typename Sum_ =
double,
typename Detected_ =
int,
typename Value_,
typename Index_,
typename Subset_>
853 const std::vector<Subset_>&
subsets,
864 if (
options.compute_detected) {
868 if (
options.compute_max_index) {
872 if (
options.compute_max_value) {
879 if (
options.compute_subset_sum) {
888 if (
options.compute_subset_detected) {
Simple quality control for single-cell data.
Definition adt_quality_control.hpp:20
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
void parallelize(Function_ fun, Index_ tasks, int threads)
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
std::vector< Detected_ * > subset_detected
Definition per_cell_qc_metrics.hpp:124
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
bool compute_sum
Definition per_cell_qc_metrics.hpp:27
bool compute_subset_sum
Definition per_cell_qc_metrics.hpp:51
bool compute_max_value
Definition per_cell_qc_metrics.hpp:39
bool compute_subset_detected
Definition per_cell_qc_metrics.hpp:57
bool compute_detected
Definition per_cell_qc_metrics.hpp:33
bool compute_max_index
Definition per_cell_qc_metrics.hpp:45
int num_threads
Definition per_cell_qc_metrics.hpp:63
Result store for QC metric calculations.
Definition per_cell_qc_metrics.hpp:723
std::vector< Index_ > max_index
Definition per_cell_qc_metrics.hpp:751
std::vector< Value_ > max_value
Definition per_cell_qc_metrics.hpp:757
std::vector< std::vector< Detected_ > > subset_detected
Definition per_cell_qc_metrics.hpp:771
std::vector< Detected_ > detected
Definition per_cell_qc_metrics.hpp:744
std::vector< Sum_ > sum
Definition per_cell_qc_metrics.hpp:738
std::vector< std::vector< Sum_ > > subset_sum
Definition per_cell_qc_metrics.hpp:764
bool sparse_ordered_index