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#include <cstddef>
8
9#include "sanisizer/sanisizer.hpp"
10
16namespace scran_aggregate {
17
35template<typename Factor_, typename Output_>
36std::vector<Factor_> clean_factor(std::size_t n, const Factor_* factor, Output_* cleaned) {
37 auto unique = [&]{ // scoping this in an IIFE to release map memory sooner.
38 std::unordered_map<Factor_, Output_> mapping;
39 for (decltype(n) i = 0; i < n; ++i) {
40 auto current = factor[i];
41 auto mIt = mapping.find(current);
42 if (mIt != mapping.end()) {
43 cleaned[i] = mIt->second;
44 } else {
45 Output_ alt = mapping.size();
46 mapping[current] = alt;
47 cleaned[i] = alt;
48 }
49 }
50 return std::vector<std::pair<Factor_, Output_> >(mapping.begin(), mapping.end());
51 }();
52
53 // Remapping to a sorted set.
54 std::sort(unique.begin(), unique.end());
55 auto nuniq = unique.size();
56 auto remapping = sanisizer::create<std::vector<Output_> >(nuniq);
57 auto output = sanisizer::create<std::vector<Factor_> >(nuniq);
58 for (decltype(nuniq) u = 0; u < nuniq; ++u) {
59 remapping[unique[u].second] = u;
60 output[u] = unique[u].first;
61 }
62
63 // Mapping each cell to its sorted factor.
64 for (decltype(n) i = 0; i < n; ++i) {
65 cleaned[i] = remapping[cleaned[i]];
66 }
67
68 return output;
69}
70
71}
72
73#endif
Aggregate single-cell expression values.
Definition aggregate_across_cells.hpp:18
std::vector< Factor_ > clean_factor(std::size_t n, const Factor_ *factor, Output_ *cleaned)
Definition clean_factor.hpp:36