scran_graph_cluster
Graph-based clustering of cells
Loading...
Searching...
No Matches
cluster_leiden.hpp
Go to the documentation of this file.
1#ifndef SCRAN_GRAPH_CLUSTER_CLUSTER_LEIDEN_HPP
2#define SCRAN_GRAPH_CLUSTER_CLUSTER_LEIDEN_HPP
3
4#include <vector>
5#include <algorithm>
6
7#include "raiigraph/raiigraph.hpp"
8#include "sanisizer/sanisizer.hpp"
9
10#include "igraph.h"
11
17namespace scran_graph_cluster {
18
28 double resolution = 1;
29
34 double beta = 0.01;
35
41 int iterations = 2;
42
49 bool modularity = false;
50
54 bool report_quality = true;
55
59 int seed = 42;
60};
61
70 int status = 0;
71
75 raiigraph::IntegerVector membership;
76
81 igraph_real_t quality = 0;
82};
83
97inline void cluster_leiden(const igraph_t* graph, const igraph_vector_t* weights, const ClusterLeidenOptions& options, ClusterLeidenResults& output) {
98 const auto membership = output.membership.get();
99 const auto quality = (options.report_quality ? &(output.quality) : static_cast<igraph_real_t*>(NULL));
100
101 const raiigraph::RNGScope rngs(options.seed);
102
103 if (!options.modularity) {
104 output.status = igraph_community_leiden(
105 graph,
106 weights,
107 NULL,
108 options.resolution,
109 options.beta,
110 false,
111 options.iterations,
112 membership,
113 NULL,
114 quality
115 );
116
117 } else {
118 // More-or-less translated from igraph::cluster_leiden in the R package.
119 auto strengths = sanisizer::create<raiigraph::RealVector>(igraph_vcount(graph));
120 igraph_strength(graph, strengths, igraph_vss_all(), IGRAPH_ALL, 1, weights);
121
122 const double total_weights = igraph_vector_sum(strengths);
123 const double mod_resolution = options.resolution / total_weights;
124
125 output.status = igraph_community_leiden(
126 graph,
127 weights,
128 strengths,
129 mod_resolution,
130 options.beta,
131 false,
132 options.iterations,
133 membership,
134 NULL,
135 quality
136 );
137 }
138}
139
151inline ClusterLeidenResults cluster_leiden(const raiigraph::Graph& graph, const std::vector<igraph_real_t>& weights, const ClusterLeidenOptions& options) {
152 // No need to free this, as it's just a view.
153 igraph_vector_t weight_view;
154 igraph_vector_view(&weight_view, weights.data(), sanisizer::cast<igraph_integer_t>(weights.size()));
155
157 cluster_leiden(graph.get(), &weight_view, options, output);
158 return output;
159}
160
161}
162
163#endif
Graph-based clustering of single-cell data.
Definition build_snn_graph.hpp:22
void cluster_leiden(const igraph_t *graph, const igraph_vector_t *weights, const ClusterLeidenOptions &options, ClusterLeidenResults &output)
Definition cluster_leiden.hpp:97
Options for cluster_leiden().
Definition cluster_leiden.hpp:22
int seed
Definition cluster_leiden.hpp:59
bool modularity
Definition cluster_leiden.hpp:49
double beta
Definition cluster_leiden.hpp:34
double resolution
Definition cluster_leiden.hpp:28
bool report_quality
Definition cluster_leiden.hpp:54
int iterations
Definition cluster_leiden.hpp:41
Result of cluster_leiden().
Definition cluster_leiden.hpp:65
int status
Definition cluster_leiden.hpp:70
igraph_real_t quality
Definition cluster_leiden.hpp:81
raiigraph::IntegerVector membership
Definition cluster_leiden.hpp:75