Note: this is the development version of the leiden R package. This version
has remote dependencies on the development version of the R
igraph package. This must be
cloned and compiled from source. It depends on functions not on CRAN (yet).
This development version is for testing an upcoming release. It is not
recommended to use this unless you require features not supported in previous
releases.
Dependancies
This package requires the ‘leidenalg’ and ‘igraph’ modules for python (2) to be installed on your system. For example:
pip install leidenalg numpy python-igraph
Note you may need to uninstall the igraph 0.1.11 (now deprecated to jgraph) and install python-igraph or igraph-0.7.0:
If you do not have root access, you can use pip install --user or pip install --prefix to install these in your user directory (which you have write permissions for) and ensure that this directory is in your PATH so that Python can find it.
Dependancies can also be installed from a conda repository. This is recommended for Windows users:
conda -c vtraag python-igraph leidenalg
Stable release
The stable ‘leiden’ package and the dependancies can be installed from CRAN:
install.packages("leiden")
Development version
The ‘devtools’ package can also be used to install development version of ‘leiden’ and the dependancies (igraph and reticulate) from GitHub:
if (!requireNamespace("devtools"))
install.packages("devtools")
devtools::install_github("TomKellyGenetics/leiden", ref = "master")
Development version
To use or test the development version, install the “dev” branch from GitHub.
if (!requireNamespace("devtools"))
install.packages("devtools")
devtools::install_github("TomKellyGenetics/leiden", ref = "dev")
Please submit pull requests to the “dev” branch. This can be downloaded to your system with:
git clone --branch dev git@github.com:TomKellyGenetics/leiden.git
Usage
This package provides a function to perform clustering with the Leiden algorithm:
Calling leiden directly on a graph object is also available:
partition <- leiden(graph_object)
See the benchmarking vignette on details of performance.
Computing partitions on data matrices or dimension reductions
To generate an adjacency matrix from a dataset, we can compute the shared nearest neighbours (SNN) from the data. For example, for a dataset data_mat with n features (rows) by m samples or cells (columns), we generate an adjacency matrix of nearest neighbours between samples.
library(RANN)
snn <- RANN::nn2(t(data_mat), k=30)$nn.idx
adjacency_matrix <- matrix(0L, ncol(data_mat), ncol(data_mat))
rownames(adjacency_matrix) <- colnames(adjacency_matrix) <- colnames(data_mat)
for(ii in 1:ncol(data_mat)) {
adjacency_matrix[i,colnames(data_mat)[snn[ii,]]] <- 1L
}
#check that rows add to k
sum(adjacency_matrix[1,]) == 30
table(apply(adjacency_matrix, 1, sum))
For a dimension reduction embedding of m samples (rows) by n dimensions (columns):
library(RANN)
snn <- RANN::nn2(embedding, k=30)$nn.idx
adjacency_matrix <- matrix(0L, nrow(embedding), nrow(embedding))
rownames(adjacency_matrix) <- colnames(adjacency_matrix) <- colnames(data_mat)
for(ii in 1:nrow(embedding)) {
adjacency_matrix[ii,rownames(data_mat)[snn[ii,]]] <- 1L
}
#check that rows add to k
sum(adjacency_matrix[1,]) == 30
table(apply(adjacency_matrix, 1, sum))
This is compatible with PCA, tSNE, or UMAP results.
Use with Seurat
Seurat version 2
To use Leiden with the Seurat pipeline for a Seurat Object object that has an SNN computed (for example with Seurat::FindClusters with save.SNN = TRUE). This will compute the Leiden clusters and add them to the Seurat Object Class.
Note that this code is designed for Seurat version 2 releases. For Seurat version 3 objects, the Leiden algorithm will be implemented in the Seurat version 3 package with Seurat::FindClusters and algorithm = "leiden").
Please cite this implementation R in if you use it:
To cite the leiden package in publications use:
S. Thomas Kelly (2023). leiden: R implementation of the Leiden algorithm. R
package version 0.4.3.1 https://github.com/TomKellyGenetics/leiden
A BibTeX entry for LaTeX users is
@Manual{,
title = {leiden: R implementation of the Leiden algorithm},
author = {S. Thomas Kelly},
year = {2023},
note = {R package version 0.4.3.1},
url = {https://github.com/TomKellyGenetics/leiden},
}
Please also cite the original publication of this algorithm.
Traag, V.A., Waltman. L., Van Eck, N.-J. (2019). From Louvain to
Leiden: guaranteeing well-connected communities.
Sci Rep 9, 5233 <https://doi.org/10.1038/s41598-019-41695-z>
Leiden Algorithm
leiden version 0.4.3.1
Clustering with the Leiden Algorithm in R
This package allows calling the Leiden algorithm for clustering on an igraph object from R. See the Python and Java implementations for more details:
https://github.com/CWTSLeiden/networkanalysis
https://github.com/vtraag/leidenalg
Install
Note: this is the development version of the
leidenR package. This version has remote dependencies on the development version of the R igraph package. This must be cloned and compiled from source. It depends on functions not on CRAN (yet).This development version is for testing an upcoming release. It is not recommended to use this unless you require features not supported in previous releases.
Dependancies
This package requires the ‘leidenalg’ and ‘igraph’ modules for python (2) to be installed on your system. For example:
Note you may need to uninstall the igraph 0.1.11 (now deprecated to jgraph) and install python-igraph or igraph-0.7.0:
The python version can be installed with pip or conda:
It is also possible to install the python dependencies with reticulate in R.
If you do not have root access, you can use
pip install --userorpip install --prefixto install these in your user directory (which you have write permissions for) and ensure that this directory is in your PATH so that Python can find it.Dependancies can also be installed from a conda repository. This is recommended for Windows users:
Stable release
The stable ‘leiden’ package and the dependancies can be installed from CRAN:
Development version
The ‘devtools’ package can also be used to install development version of ‘leiden’ and the dependancies (igraph and reticulate) from GitHub:
Development version
To use or test the development version, install the “dev” branch from GitHub.
Please submit pull requests to the “dev” branch. This can be downloaded to your system with:
Usage
This package provides a function to perform clustering with the Leiden algorithm:
Use with iGraph
For an igraph object ‘graph’ in R:
Calling leiden directly on a graph object is also available:
See the benchmarking vignette on details of performance.
Computing partitions on data matrices or dimension reductions
To generate an adjacency matrix from a dataset, we can compute the shared nearest neighbours (SNN) from the data. For example, for a dataset
data_matwithnfeatures (rows) bymsamples or cells (columns), we generate an adjacency matrix of nearest neighbours between samples.For a dimension reduction
embeddingofmsamples (rows) byndimensions (columns):This is compatible with PCA, tSNE, or UMAP results.
Use with Seurat
Seurat version 2
To use Leiden with the Seurat pipeline for a Seurat Object
objectthat has an SNN computed (for example withSeurat::FindClusterswithsave.SNN = TRUE). This will compute the Leiden clusters and add them to the Seurat Object Class.Seurat objects contain an SNN graph that can be passed directly to the igraph method. For example
Seurat version 3 (or higher)
Note that this code is designed for Seurat version 2 releases. For Seurat version 3 objects, the Leiden algorithm will be implemented in the Seurat version 3 package with
Seurat::FindClustersandalgorithm = "leiden").These clusters can then be plotted with:
Example
Vignette
For more details see the follow vignettes:
https://github.com/TomKellyGenetics/leiden/blob/master/vignettes/run_leiden.html
https://github.com/TomKellyGenetics/leiden/blob/master/vignettes/run_igraph.html
https://github.com/TomKellyGenetics/leiden/blob/master/vignettes/benchmarking.html
Citation
Please cite this implementation R in if you use it:
Please also cite the original publication of this algorithm.