scran
C++ library for basic single-cell RNA-seq analyses
Loading...
Searching...
No Matches
FilterCells.hpp
Go to the documentation of this file.
1#ifndef SCRAN_FILTER_CELLS_H
2#define SCRAN_FILTER_CELLS_H
3
4#include "../utils/macros.hpp"
5
6#include "tatami/tatami.hpp"
7#include <vector>
8#include <numeric>
9#include <algorithm>
10
17namespace scran {
18
27public:
31 struct Defaults {
35 static constexpr bool discard = true;
36
40 static constexpr bool intersect = false;
41 };
42
43private:
44 bool discard = Defaults::discard;
45 bool intersect = Defaults::intersect;
46
47public:
54 discard = false;
55 return *this;
56 }
57
64 discard = d;
65 return *this;
66 }
67
75 intersect = i;
76 return *this;
77 }
78
79public:
94 template<class MAT, typename IDX = int, typename X = uint8_t>
95 std::shared_ptr<MAT> run(std::shared_ptr<MAT> mat, const X* filter) {
96 size_t NC = mat->ncol();
97
98 // Don't use accumulate: we want to defend against non-0/1 values in 'filter'.
99 IDX num = 0;
100 for (size_t i = 0; i < NC; ++i) {
101 num += (filter[i] != 0);
102 }
103
104 std::vector<IDX> retained;
105 retained.reserve(discard ? (NC - num) : num);
106
107 for (size_t i = 0; i < NC; ++i) {
108 if (discard == (filter[i] == 0)) {
109 retained.push_back(i);
110 }
111 }
112
113 return tatami::make_DelayedSubset<1>(mat, retained);
114 }
115
116public:
134 template<class MAT, typename IDX = int, typename X = uint8_t>
135 std::shared_ptr<MAT> run(std::shared_ptr<MAT> mat, const std::vector<X*>& filters) {
136 size_t NC = mat->ncol();
137
138 std::vector<uint8_t> finalvec(NC, intersect);
139 if (intersect) {
140 for (auto filter : filters) {
141 for (size_t i = 0; i < NC; ++i) {
142 finalvec[i] &= static_cast<uint8_t>(filter[i] != 0);
143 }
144 }
145 } else {
146 for (auto filter : filters) {
147 for (size_t i = 0; i < NC; ++i) {
148 finalvec[i] |= static_cast<uint8_t>(filter[i] != 0);
149 }
150 }
151 }
152
153 IDX num = std::accumulate(finalvec.begin(), finalvec.end(), static_cast<IDX>(0));
154 std::vector<IDX> retained;
155 retained.reserve(discard ? (NC - num) : num);
156
157 for (size_t i = 0; i < NC; ++i) {
158 if (discard == (finalvec[i] == 0)) {
159 retained.push_back(i);
160 }
161 }
162
163 return tatami::make_DelayedSubset<1>(mat, retained);
164 }
165};
166
167}
168
169#endif
Filter out low-quality cells.
Definition FilterCells.hpp:26
std::shared_ptr< MAT > run(std::shared_ptr< MAT > mat, const std::vector< X * > &filters)
Definition FilterCells.hpp:135
FilterCells & set_intersect(bool i=Defaults::intersect)
Definition FilterCells.hpp:74
std::shared_ptr< MAT > run(std::shared_ptr< MAT > mat, const X *filter)
Definition FilterCells.hpp:95
FilterCells & set_retain()
Definition FilterCells.hpp:53
FilterCells & set_discard(bool d=Defaults::discard)
Definition FilterCells.hpp:63
Functions for single-cell RNA-seq analyses.
Definition AggregateAcrossCells.hpp:18
Default choices for all parameters.
Definition FilterCells.hpp:31
static constexpr bool discard
Definition FilterCells.hpp:35
static constexpr bool intersect
Definition FilterCells.hpp:40