1#ifndef SCRAN_CHOOSE_PSEUDO_COUNT_HPP
2#define SCRAN_CHOOSE_PSEUDO_COUNT_HPP
100 static double find_quantile(
double quantile,
size_t n,
double* ptr) {
101 double raw =
static_cast<double>(n - 1) * quantile;
102 size_t index = std::ceil(raw);
103 std::nth_element(ptr, ptr + index, ptr + n);
104 double upper = *(ptr + index);
105 std::nth_element(ptr, ptr + index - 1, ptr + index);
106 double lower = *(ptr + index - 1);
107 return lower * (index - raw) + upper * (raw - (index - 1));
122 double run(
size_t n,
const double* size_factors,
double* buffer)
const {
129 for (
size_t i = 0; i < n; ++i) {
130 if (size_factors[i] > 0) {
131 buffer[counter] = size_factors[i];
141 double lower_sf, upper_sf;
143 lower_sf = *std::min_element(buffer, buffer + n);
144 upper_sf = *std::max_element(buffer, buffer + n);
146 lower_sf = find_quantile(quantile, n, buffer);
147 upper_sf = find_quantile(1 - quantile, n, buffer);
151 double pseudo_count = (1 / lower_sf - 1 / upper_sf) / (8 * max_bias);
153 return std::max(min_value, pseudo_count);
163 double run(
size_t n,
const double* size_factors)
const {
164 std::vector<double> buffer(n);
165 return run(n, size_factors, buffer.data());
Choose a pseudo-count for log-transformation.
Definition ChoosePseudoCount.hpp:35
ChoosePseudoCount & set_max_bias(double b=Defaults::max_bias)
Definition ChoosePseudoCount.hpp:80
ChoosePseudoCount & set_min_value(double v=Defaults::min_value)
Definition ChoosePseudoCount.hpp:91
double run(size_t n, const double *size_factors) const
Definition ChoosePseudoCount.hpp:163
ChoosePseudoCount & set_quantile(double q=Defaults::quantile)
Definition ChoosePseudoCount.hpp:70
double run(size_t n, const double *size_factors, double *buffer) const
Definition ChoosePseudoCount.hpp:122
Functions for single-cell RNA-seq analyses.
Definition AggregateAcrossCells.hpp:18
Default parameters.
Definition ChoosePseudoCount.hpp:40
static constexpr double quantile
Definition ChoosePseudoCount.hpp:44
static constexpr double max_bias
Definition ChoosePseudoCount.hpp:49
static constexpr double min_value
Definition ChoosePseudoCount.hpp:54