1#ifndef SCRAN_SNNGRAPHCLUSTERING_HPP
2#define SCRAN_SNNGRAPHCLUSTERING_HPP
4#include "../utils/macros.hpp"
42 static constexpr int seed = 42;
113 return run(store.to_igraph(), store.
weights.data());
125 igraph::IntegerVector membership_holder;
126 igraph::RealVector modularity_holder;
127 igraph::IntegerMatrix memberships_holder;
128 igraph::RNGScope rngs(seed);
130 auto& modularity = modularity_holder.vector;
131 auto& membership = membership_holder.vector;
132 auto& memberships = memberships_holder.matrix;
135 igraph_vector_t weight_view;
136 igraph_vector_view(&weight_view, weights, igraph_ecount(graph.
get_graph()));
139 output.
status = igraph_community_multilevel(graph.
get_graph(), &weight_view, resolution, &membership, &memberships, &modularity);
142 output.
max = igraph_vector_which_max(&modularity);
144 size_t nmods = igraph_vector_size(&modularity);
146 for (
size_t i = 0; i < nmods; ++i) {
150 size_t ncells = igraph_vcount(graph.
get_graph());
151 size_t nlevels = igraph_matrix_int_nrow(&memberships);
154 for (
size_t i = 0; i < nlevels; ++i) {
156 current.resize(ncells);
157 for (
size_t j = 0; j < ncells; ++j) {
158 current[j] = MATRIX(memberships, i, j);
222 std::vector<std::pair<int, int> >
merges;
239 return run(store.to_igraph(), store.
weights.data());
251 igraph::IntegerMatrix merges_holder;
252 igraph::RealVector modularity_holder;
253 igraph::IntegerVector membership_holder;
255 auto& modularity = modularity_holder.vector;
256 auto& membership = membership_holder.vector;
257 auto& merges = merges_holder.matrix;
260 igraph_vector_t weight_view;
261 igraph_vector_view(&weight_view, weights, igraph_ecount(graph.
get_graph()));
264 output.
status = igraph_community_walktrap(graph.
get_graph(), &weight_view, steps, &merges, &modularity, &membership);
267 size_t nmods = igraph_vector_size(&modularity);
269 for (
size_t i = 0; i < nmods; ++i) {
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);
280 size_t ncells = igraph_vcount(graph.
get_graph());
282 for (
size_t i = 0; i < ncells; ++i) {
313 static constexpr double beta = 0.01;
330 static constexpr int seed = 42;
427 return run(store.to_igraph(), store.
weights.data());
439 igraph::IntegerVector membership_holder;
440 auto& membership = membership_holder.vector;
441 igraph_integer_t nb_clusters;
442 igraph_real_t quality;
444 igraph::RNGScope rngs(seed);
448 igraph_vector_t weight_view;
449 size_t nedges = igraph_ecount(graph.
get_graph());
450 igraph_vector_view(&weight_view, weights, nedges);
453 output.
status = igraph_community_leiden(graph.
get_graph(), &weight_view, NULL, resolution, beta,
false, iterations, &membership, &nb_clusters, &quality);
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);
461 double total_weights = std::accumulate(weights, weights + nedges, 0.0);
462 double mod_resolution = resolution / total_weights;
464 output.
status = igraph_community_leiden(graph.
get_graph(), &weight_view, &strength, mod_resolution, beta,
false, iterations, &membership, &nb_clusters, &quality);
468 size_t ncells = igraph_vcount(graph.
get_graph());
470 for (
size_t i = 0; i < ncells; ++i) {
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