1#ifndef SCRAN_MEDIAN_SIZE_FACTORS_HPP
2#define SCRAN_MEDIAN_SIZE_FACTORS_HPP
4#include "../utils/macros.hpp"
8#include "tatami/tatami.hpp"
120 template<
typename T,
typename IDX,
typename Ref,
typename Out>
121 void run(
const tatami::Matrix<T, IDX>* mat,
const Ref* ref, Out* output)
const {
122 auto NR = mat->nrow(), NC = mat->ncol();
124 std::vector<T> sums(NC);
125 tatami::parallelize([&](
size_t, IDX start, IDX length) ->
void {
126 auto ext = tatami::consecutive_extractor<false, false>(mat, start, length);
127 std::vector<T> buffer(NR);
129 for (IDX c = start, end = start + length; c < end; ++c) {
130 auto ptr = ext->fetch(c, buffer.data());
131 sums[c] = std::accumulate(ptr, ptr + NR,
static_cast<T
>(0));
134 for (IDX r = 0; r < NR; ++r) {
135 if (ref[r] == 0 && ptr[r] == 0) {
141 buffer[sofar] = std::numeric_limits<T>::infinity();
143 buffer[sofar] = ptr[r] / ref[r];
150 output[c] = tatami::stats::compute_median<Out>(buffer.data(), sofar);
208 if (prior_count && NR && NC) {
209 double mean = std::accumulate(sums.begin(), sums.end(),
static_cast<T
>(0));
212 double reftotal = std::accumulate(ref, ref + NR,
static_cast<double>(0));
214 if (mean && reftotal) {
215 double scaling = prior_count / mean;
216 for (
size_t i = 0; i < NC; ++i) {
217 output[i] += sums[i] * scaling / reftotal;
218 output[i] /= 1.0 + scaling;
226 centerer.
run(NC, output);
244 template<
typename T,
typename IDX,
typename Out>
246 auto ref = tatami::row_sums(mat, num_threads);
248 double NC = mat->ncol();
249 for (
auto& r : ref) {
253 run(mat, ref.data(), output);
263 template<
typename Out>
295 template<
typename Out =
double,
typename T,
typename IDX,
typename Ref>
314 template<
typename Out =
double,
typename T,
typename IDX>
Center size factors prior to normalization.
Center size factors prior to scaling normalization.
Definition CenterSizeFactors.hpp:27
SizeFactorValidity run(size_t n, T *size_factors) const
Definition CenterSizeFactors.hpp:103
Functions for single-cell RNA-seq analyses.
Definition AggregateAcrossCells.hpp:18