scran
C++ library for basic single-cell RNA-seq analyses
Loading...
Searching...
No Matches
ClusterSnnGraph.hpp
Go to the documentation of this file.
1#ifndef SCRAN_SNNGRAPHCLUSTERING_HPP
2#define SCRAN_SNNGRAPHCLUSTERING_HPP
3
4#include "../utils/macros.hpp"
5
6#include "BuildSnnGraph.hpp"
7
8#include <vector>
9#include <algorithm>
10
11#include "igraph.h"
12#include "igraph_utils.hpp"
13
20namespace scran {
21
29public:
33 struct Defaults {
37 static constexpr double resolution = 1;
38
42 static constexpr int seed = 42;
43 };
44
45private:
46 double resolution = Defaults::resolution;
47 int seed = Defaults::seed;
48
49public:
56 seed = s;
57 return *this;
58 }
59
67 resolution = r;
68 return *this;
69 }
70
71public:
79 struct Results {
84 int status = 0;
85
90 size_t max = 0;
91
96 std::vector<std::vector<int> > membership;
97
102 std::vector<double> modularity;
103 };
104
112 Results run(const BuildSnnGraph::Results& store) const {
113 return run(store.to_igraph(), store.weights.data());
114 }
115
124 Results run(const igraph::Graph& graph, const igraph_real_t* weights) const {
125 igraph::IntegerVector membership_holder;
126 igraph::RealVector modularity_holder;
127 igraph::IntegerMatrix memberships_holder;
128 igraph::RNGScope rngs(seed);
129
130 auto& modularity = modularity_holder.vector;
131 auto& membership = membership_holder.vector;
132 auto& memberships = memberships_holder.matrix;
133
134 // No need to free this, as it's just a view.
135 igraph_vector_t weight_view;
136 igraph_vector_view(&weight_view, weights, igraph_ecount(graph.get_graph()));
137
138 Results output;
139 output.status = igraph_community_multilevel(graph.get_graph(), &weight_view, resolution, &membership, &memberships, &modularity);
140
141 if (!output.status) {
142 output.max = igraph_vector_which_max(&modularity);
143
144 size_t nmods = igraph_vector_size(&modularity);
145 output.modularity.resize(nmods);
146 for (size_t i = 0; i < nmods; ++i) {
147 output.modularity[i] = VECTOR(modularity)[i];
148 }
149
150 size_t ncells = igraph_vcount(graph.get_graph());
151 size_t nlevels = igraph_matrix_int_nrow(&memberships);
152 output.membership.resize(nlevels);
153
154 for (size_t i = 0; i < nlevels; ++i) {
155 auto& current = output.membership[i];
156 current.resize(ncells);
157 for (size_t j = 0; j < ncells; ++j) {
158 current[j] = MATRIX(memberships, i, j);
159 }
160 }
161 }
162
163 return output;
164 }
165};
166
174public:
178 struct Defaults {
183 static constexpr int steps = 4;
184 };
185
186private:
187 int steps = Defaults::steps;
188
189public:
196 steps = s;
197 return *this;
198 }
199
200public:
206 struct Results {
211 int status = 0;
212
216 std::vector<int> membership;
217
222 std::vector<std::pair<int, int> > merges;
223
228 std::vector<double> modularity;
229 };
230
238 Results run(const BuildSnnGraph::Results& store) const {
239 return run(store.to_igraph(), store.weights.data());
240 }
241
250 Results run(const igraph::Graph& graph, const igraph_real_t* weights) const {
251 igraph::IntegerMatrix merges_holder;
252 igraph::RealVector modularity_holder;
253 igraph::IntegerVector membership_holder;
254
255 auto& modularity = modularity_holder.vector;
256 auto& membership = membership_holder.vector;
257 auto& merges = merges_holder.matrix;
258
259 // No need to free this, as it's just a view.
260 igraph_vector_t weight_view;
261 igraph_vector_view(&weight_view, weights, igraph_ecount(graph.get_graph()));
262
263 Results output;
264 output.status = igraph_community_walktrap(graph.get_graph(), &weight_view, steps, &merges, &modularity, &membership);
265
266 if (!output.status) {
267 size_t nmods = igraph_vector_size(&modularity);
268 output.modularity.resize(nmods);
269 for (size_t i = 0; i < nmods; ++i) {
270 output.modularity[i] = VECTOR(modularity)[i];
271 }
272
273 size_t nmerges = igraph_matrix_int_nrow(&merges);
274 output.merges.resize(nmerges);
275 for (size_t i = 0; i < nmerges; ++i) {
276 output.merges[i].first = MATRIX(merges, i, 0);
277 output.merges[i].second = MATRIX(merges, i, 1);
278 }
279
280 size_t ncells = igraph_vcount(graph.get_graph());
281 output.membership.resize(ncells);
282 for (size_t i = 0; i < ncells; ++i) {
283 output.membership[i] = VECTOR(membership)[i];
284 }
285 }
286
287 return output;
288 }
289};
290
298public:
302 struct Defaults {
307 static constexpr double resolution = 1;
308
313 static constexpr double beta = 0.01;
314
319 static constexpr int iterations = 2;
320
325 static constexpr bool modularity = false;
326
330 static constexpr int seed = 42;
331 };
332
333private:
334 double resolution = Defaults::resolution;
335 double beta = Defaults::beta;
336 int iterations = Defaults::iterations;
337 bool modularity = Defaults::modularity;
338 int seed = Defaults::seed;
339
340public:
347 seed = s;
348 return *this;
349 }
350
358 resolution = r;
359 return *this;
360 }
361
368 beta = b;
369 return *this;
370 }
371
379 iterations = i;
380 return *this;
381 }
382
391 modularity = m;
392 return *this;
393 }
394
395public:
401 struct Results {
406 int status = 0;
407
411 std::vector<int> membership;
412
416 double quality = 0;
417 };
418
426 Results run(const BuildSnnGraph::Results& store) const {
427 return run(store.to_igraph(), store.weights.data());
428 }
429
438 Results run(const igraph::Graph& graph, const igraph_real_t* weights) const {
439 igraph::IntegerVector membership_holder;
440 auto& membership = membership_holder.vector;
441 igraph_integer_t nb_clusters;
442 igraph_real_t quality;
443
444 igraph::RNGScope rngs(seed);
445 Results output;
446
447 // No need to free this, as it's just a view.
448 igraph_vector_t weight_view;
449 size_t nedges = igraph_ecount(graph.get_graph());
450 igraph_vector_view(&weight_view, weights, nedges);
451
452 if (!modularity) {
453 output.status = igraph_community_leiden(graph.get_graph(), &weight_view, NULL, resolution, beta, false, iterations, &membership, &nb_clusters, &quality);
454 } else {
455 // More-or-less translated from igraph::cluster_leiden in the R package,
456 // but with the iterations moved into igraph_community_leiden itself.
457 igraph::RealVector strength_holder(igraph_vcount(graph.get_graph()));
458 auto& strength = strength_holder.vector;
459 igraph_strength(graph.get_graph(), &strength, igraph_vss_all(), IGRAPH_ALL, 1, &weight_view);
460
461 double total_weights = std::accumulate(weights, weights + nedges, 0.0);
462 double mod_resolution = resolution / total_weights;
463
464 output.status = igraph_community_leiden(graph.get_graph(), &weight_view, &strength, mod_resolution, beta, false, iterations, &membership, &nb_clusters, &quality);
465 }
466
467 if (!output.status) {
468 size_t ncells = igraph_vcount(graph.get_graph());
469 output.membership.resize(ncells);
470 for (size_t i = 0; i < ncells; ++i) {
471 output.membership[i] = VECTOR(membership)[i];
472 }
473 output.quality = quality;
474 }
475
476 return output;
477 }
478};
479
480}
481
482#endif
483
Build a shared nearest-neighbor graph on the cells.
Leiden clustering on a shared nearest-neighbor graph.
Definition ClusterSnnGraph.hpp:297
Results run(const BuildSnnGraph::Results &store) const
Definition ClusterSnnGraph.hpp:426
Results run(const igraph::Graph &graph, const igraph_real_t *weights) const
Definition ClusterSnnGraph.hpp:438
ClusterSnnGraphLeiden & set_resolution(double r=Defaults::resolution)
Definition ClusterSnnGraph.hpp:357
ClusterSnnGraphLeiden & set_seed(int s=Defaults::seed)
Definition ClusterSnnGraph.hpp:346
ClusterSnnGraphLeiden & set_modularity(bool m=Defaults::modularity)
Definition ClusterSnnGraph.hpp:390
ClusterSnnGraphLeiden & set_beta(double b=Defaults::beta)
Definition ClusterSnnGraph.hpp:367
ClusterSnnGraphLeiden & set_iterations(int i=Defaults::iterations)
Definition ClusterSnnGraph.hpp:378
Multi-level clustering on a shared nearest-neighbor graph.
Definition ClusterSnnGraph.hpp:28
ClusterSnnGraphMultiLevel & set_seed(int s=Defaults::seed)
Definition ClusterSnnGraph.hpp:55
Results run(const BuildSnnGraph::Results &store) const
Definition ClusterSnnGraph.hpp:112
ClusterSnnGraphMultiLevel & set_resolution(double r=Defaults::resolution)
Definition ClusterSnnGraph.hpp:66
Results run(const igraph::Graph &graph, const igraph_real_t *weights) const
Definition ClusterSnnGraph.hpp:124
Walktrap clustering on a shared nearest-neighbor graph.
Definition ClusterSnnGraph.hpp:173
Results run(const BuildSnnGraph::Results &store) const
Definition ClusterSnnGraph.hpp:238
ClusterSnnGraphWalktrap & set_steps(int s=Defaults::steps)
Definition ClusterSnnGraph.hpp:195
Results run(const igraph::Graph &graph, const igraph_real_t *weights) const
Definition ClusterSnnGraph.hpp:250
Utilities for manipulating igraph data structures.
Functions for single-cell RNA-seq analyses.
Definition AggregateAcrossCells.hpp:18
Results of SNN graph construction.
Definition BuildSnnGraph.hpp:139
std::vector< double > weights
Definition BuildSnnGraph.hpp:162
Default parameter settings.
Definition ClusterSnnGraph.hpp:302
static constexpr int iterations
Definition ClusterSnnGraph.hpp:319
static constexpr int seed
Definition ClusterSnnGraph.hpp:330
static constexpr bool modularity
Definition ClusterSnnGraph.hpp:325
static constexpr double resolution
Definition ClusterSnnGraph.hpp:307
static constexpr double beta
Definition ClusterSnnGraph.hpp:313
Result of the igraph leiden community detection algorithm.
Definition ClusterSnnGraph.hpp:401
double quality
Definition ClusterSnnGraph.hpp:416
std::vector< int > membership
Definition ClusterSnnGraph.hpp:411
int status
Definition ClusterSnnGraph.hpp:406
Default parameter settings.
Definition ClusterSnnGraph.hpp:33
static constexpr int seed
Definition ClusterSnnGraph.hpp:42
static constexpr double resolution
Definition ClusterSnnGraph.hpp:37
Result of the igraph multi-level community detection algorithm.
Definition ClusterSnnGraph.hpp:79
int status
Definition ClusterSnnGraph.hpp:84
size_t max
Definition ClusterSnnGraph.hpp:90
std::vector< double > modularity
Definition ClusterSnnGraph.hpp:102
std::vector< std::vector< int > > membership
Definition ClusterSnnGraph.hpp:96
Default parameter settings.
Definition ClusterSnnGraph.hpp:178
static constexpr int steps
Definition ClusterSnnGraph.hpp:183
Result of the igraph Walktrap community detection algorithm.
Definition ClusterSnnGraph.hpp:206
std::vector< double > modularity
Definition ClusterSnnGraph.hpp:228
std::vector< std::pair< int, int > > merges
Definition ClusterSnnGraph.hpp:222
std::vector< int > membership
Definition ClusterSnnGraph.hpp:216
int status
Definition ClusterSnnGraph.hpp:211
Wrapper around the igraph_t class from igraph.
Definition igraph_utils.hpp:180
const igraph_t * get_graph() const
Definition igraph_utils.hpp:256