scran_aggregate
Aggregate expression values across cells
Loading...
Searching...
No Matches
clean_factor.hpp
Go to the documentation of this file.
1#ifndef SCRAN_AGGREGATE_CLEAN_FACTORS_HPP
2#define SCRAN_AGGREGATE_CLEAN_FACTORS_HPP
3
4#include <unordered_map>
5#include <vector>
6#include <algorithm>
7
13namespace scran_aggregate {
14
32template<typename Factor_, typename Output_>
33std::vector<Factor_> clean_factor(size_t n, const Factor_* factor, Output_* cleaned) {
34 auto unique = [&]{ // scoping this in an IIFE to release map memory sooner.
35 std::unordered_map<Factor_, Output_> mapping;
36 for (size_t i = 0; i < n; ++i) {
37 auto current = factor[i];
38 auto mIt = mapping.find(current);
39 if (mIt != mapping.end()) {
40 cleaned[i] = mIt->second;
41 } else {
42 Output_ alt = mapping.size();
43 mapping[current] = alt;
44 cleaned[i] = alt;
45 }
46 }
47 return std::vector<std::pair<Factor_, Output_> >(mapping.begin(), mapping.end());
48 }();
49
50 // Remapping to a sorted set.
51 std::sort(unique.begin(), unique.end());
52 size_t nuniq = unique.size();
53 std::vector<Output_> remapping(nuniq);
54 std::vector<Factor_> output(nuniq);
55 for (size_t u = 0; u < nuniq; ++u) {
56 remapping[unique[u].second] = u;
57 output[u] = unique[u].first;
58 }
59
60 // Mapping each cell to its sorted factor.
61 for (size_t i = 0; i < n; ++i) {
62 cleaned[i] = remapping[cleaned[i]];
63 }
64
65 return output;
66}
67
68}
69
70#endif
Aggregate single-cell expression values.
Definition aggregate_across_cells.hpp:13
std::vector< Factor_ > clean_factor(size_t n, const Factor_ *factor, Output_ *cleaned)
Definition clean_factor.hpp:33