leiden
Implementation of the Leiden algorithm called by reticulate in R. (CRAN)
Science Score: 57.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
✓CITATION.cff file
Found CITATION.cff file -
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
✓DOI references
Found 2 DOI reference(s) in README -
○Academic publication links
-
○Committers with academic emails
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (15.5%) to scientific vocabulary
Repository
Implementation of the Leiden algorithm called by reticulate in R. (CRAN)
Basic Info
- Host: GitHub
- Owner: TomKellyGenetics
- License: gpl-3.0
- Language: R
- Default Branch: master
- Homepage: https://CRAN.R-project.org/package=leiden
- Size: 17.1 MB
Statistics
- Stars: 40
- Watchers: 2
- Forks: 8
- Open Issues: 9
- Releases: 21
Metadata Files
README.md
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 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:
pip uninstall igraph
pip install leidenalg python-igraph
The python version can be installed with pip or conda:
pip uninstall -y igraph
pip install -U -q leidenalg python-igraph
conda install -c vtraag leidenalg
It is also possible to install the python dependencies with reticulate in R.
{r, eval = FALSE}
library("reticulate")
py_install("python-igraph")
py_install("leidenalg", forge = TRUE)
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:
R
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:
R
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.
R
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:
R
partition <- leiden(adjacency_matrix)
Use with iGraph
For an igraph object 'graph' in R:
R
adjacency_matrix <- igraph::as_adjacency_matrix(graph)
partition <- leiden(adjacency_matrix)
Calling leiden directly on a graph object is also available:
R
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.
```R library(RANN) snn <- RANN::nn2(t(datamat), k=30)$nn.idx adjacencymatrix <- matrix(0L, ncol(datamat), ncol(datamat)) rownames(adjacencymatrix) <- colnames(adjacencymatrix) <- colnames(datamat) for(ii in 1:ncol(datamat)) { adjacencymatrix[i,colnames(datamat)[snn[ii,]]] <- 1L }
check that rows add to k
sum(adjacencymatrix[1,]) == 30 table(apply(adjacencymatrix, 1, sum)) ```
For a dimension reduction embedding of m samples (rows) by n dimensions (columns):
```R library(RANN) snn <- RANN::nn2(embedding, k=30)$nn.idx adjacencymatrix <- matrix(0L, nrow(embedding), nrow(embedding)) rownames(adjacencymatrix) <- colnames(adjacencymatrix) <- colnames(datamat) for(ii in 1:nrow(embedding)) { adjacencymatrix[ii,rownames(datamat)[snn[ii,]]] <- 1L }
check that rows add to k
sum(adjacencymatrix[1,]) == 30 table(apply(adjacencymatrix, 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.
R
library("Seurat")
FindClusters(pbmc_small)
adjacency_matrix <- as.matrix(pbmc_small@snn)
partition <- leiden(adjacency_matrix)
pbmc_small@ident <- as.factor(partition)
names(test@ident) <- rownames(test@meta.data)
pbmc_small@meta.data$ident <- as.factor(partition)
Seurat objects contain an SNN graph that can be passed directly to the igraph method. For example
R
library("Seurat")
FindClusters(pbmc_small)
membership <- leiden(pbmc_small@snn)
table(membership)
pbmc_small@ident <- as.factor(membership)
names(pbmc_small@ident) <- rownames(pbmc_small@meta.data)
pbmc_small@meta.data$ident <- as.factor(membership)
```R library("RColorBrewer") colourPal <- function(groups) colorRampPalette(brewer.pal(min(length(names(table(groups))), 11), "Set3"))(length(names(table(groups))))
pbmcsmall <- RunPCA(object = pbmcsmall, do.print = TRUE, pcs.print = 1:5, genes.print = 5) PCAPlot(object = pbmcsmall, colors.use = colourPal(pbmcsmall@ident), group.by = "ident")
pbmcsmall <- RunTSNE(object = pbmcsmall, dims.use = 1:20, do.fast = TRUE, dim.embed = 2) TSNEPlot(object = pbmcsmall, colors.use = colourPal(pbmcsmall@ident), group.by = "ident")
pbmcsmall <- RunUMAP(object = pbmcsmall, dims.use = 1:20, metric = "correlation", max.dim = 2) DimPlot(pbmcsmall, reduction.use = "umap", colors.use = colourPal(pbmcsmall@ident), group.by = "ident") ```
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::FindClusters and algorithm = "leiden").
R
library("Seurat")
FindClusters(pbmc_small, algorithm = 4)
These clusters can then be plotted with:
```R library("RColorBrewer") colourPal <- function(groups) colorRampPalette(brewer.pal(min(length(names(table(groups))), 11), "Set3"))(length(names(table(groups))))
PCAPlot(object = pbmcsmall, colors.use = colourPal(pbmcsmall@active.ident), group.by = "ident")
TSNEPlot(object = pbmcsmall, colors.use = colourPal(pbmcsmall@active.ident), group.by = "ident")
pbmcsmall <- RunUMAP(object = pbmcsmall, reduction.use = "pca", dims.use = 1:20, metric = "correlation", max.dim = 2) DimPlot(pbmcsmall, reduction.use = "umap", colors.use = colourPal(pbmcsmall@active.ident), group.by = "ident") ```
Example
```
generate example data
adjacency_matrix <- rbind(cbind(matrix(round(rbinom(4000, 1, 0.8)), 20, 20), matrix(round(rbinom(4000, 1, 0.3)), 20, 20), matrix(round(rbinom(400, 1, 0.1)), 20, 20)),
' cbind(matrix(round(rbinom(400, 1, 0.3)), 20, 20), matrix(round(rbinom(400, 1, 0.8)), 20, 20), matrix(round(rbinom(4000, 1, 0.2)), 20, 20)),
' cbind(matrix(round(rbinom(400, 1, 0.3)), 20, 20), matrix(round(rbinom(4000, 1, 0.1)), 20, 20), matrix(round(rbinom(4000, 1, 0.9)), 20, 20)))
library("igraph") rownames(adjacencymatrix) <- 1:60 colnames(adjacencymatrix) <- 1:60 graphobject <- graphfromadjacencymatrix(adjacency_matrix, mode = "directed")
plot graph structure
library("devtools") installgithub("TomKellyGenetics/igraph.extensions") library("plot.igraph") plotdirected(graph_object, cex.arrow = 0.3, col.arrow = "grey50")
generate partitions
partition <- leiden(adjacency_matrix) table(partition)
plot results
library("RColorBrewer") node.cols <- brewer.pal(max(partition),"Pastel1")[partition] plotdirected(graphobject, cex.arrow = 0.3, col.arrow = "grey50", fill.node = node.cols) ```
Vignette
For more details see the follow vignettes:
- running leiden on an adjacency matrix
https://github.com/TomKellyGenetics/leiden/blob/master/vignettes/run_leiden.html
- running leiden on an igraph object
https://github.com/TomKellyGenetics/leiden/blob/master/vignettes/run_igraph.html
- comparing running leiden in Python to various methods in R
https://github.com/TomKellyGenetics/leiden/blob/master/vignettes/benchmarking.html
Citation
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>
Owner
- Name: Tom Kelly
- Login: TomKellyGenetics
- Kind: user
- Location: Akiruno, Tokyo, Japan
- Company: RIKEN IMS; H.U. Group @hugp-ri
- Website: https://orcid.org/0000-0003-3904-6690
- Twitter: tomkxy
- Repositories: 138
- Profile: https://github.com/TomKellyGenetics
Bioinformatician. PhD 2014-2017: @mikblacklab Otago Uni. Postdoc 2018-2021: @minoda-lab RIKEN. The @carpentries. Genomics | Health | Math | Stats | AWS Dev
Citation (CITATION.cff)
cff-version: 1.2.0 message: "If you use this software, please cite it as below." title = "leiden: R implementation of the Leiden algorithm" authors: - family-names: "Kelly" given-names: "S. Thomas" orcid: "https://orcid.org/0000-0003-3904-66900" title: "leiden" version: 0.4.3.1 date-released: 2023-11-08 url: "https://github.com/TomKellyGenetics/leiden"
GitHub Events
Total
- Issues event: 1
- Watch event: 4
- Issue comment event: 2
- Fork event: 1
Last Year
- Issues event: 1
- Watch event: 4
- Issue comment event: 2
- Fork event: 1
Committers
Last synced: over 2 years ago
Top Committers
| Name | Commits | |
|---|---|---|
| TomKellyGenetics | t****s@g****m | 685 |
| Yihui Xie | x****e@y****e | 2 |
Committer Domains (Top 20 + Academic)
Issues and Pull Requests
Last synced: 6 months ago
All Time
- Total issues: 31
- Total pull requests: 1
- Average time to close issues: 4 months
- Average time to close pull requests: 21 days
- Total issue authors: 21
- Total pull request authors: 1
- Average comments per issue: 4.35
- Average comments per pull request: 8.0
- Merged pull requests: 1
- Bot issues: 0
- Bot pull requests: 0
Past Year
- Issues: 3
- Pull requests: 0
- Average time to close issues: N/A
- Average time to close pull requests: N/A
- Issue authors: 3
- Pull request authors: 0
- Average comments per issue: 0.33
- Average comments per pull request: 0
- Merged pull requests: 0
- Bot issues: 0
- Bot pull requests: 0
Top Authors
Issue Authors
- TomKellyGenetics (7)
- igordot (2)
- PCK1992 (2)
- akhst7 (2)
- EEmanetci (2)
- sklarz-bgu (1)
- cakirb (1)
- pangwopi (1)
- krovi137 (1)
- AAA-3 (1)
- ayushnoori (1)
- ccnawrocki (1)
- ollieeknight (1)
- wt12318 (1)
- anncfs (1)
Pull Request Authors
- yihui (1)
Top Labels
Issue Labels
Pull Request Labels
Packages
- Total packages: 2
-
Total downloads:
- cran 4,088 last-month
- Total docker downloads: 151,652
-
Total dependent packages: 5
(may contain duplicates) -
Total dependent repositories: 35
(may contain duplicates) - Total versions: 27
- Total maintainers: 1
cran.r-project.org: leiden
R Implementation of Leiden Clustering Algorithm
- Homepage: https://github.com/TomKellyGenetics/leiden
- Documentation: http://cran.r-project.org/web/packages/leiden/leiden.pdf
- License: GPL-3 | file LICENSE
-
Latest release: 0.4.3
published over 3 years ago
Rankings
Maintainers (1)
conda-forge.org: r-leiden
- Homepage: https://github.com/TomKellyGenetics/leiden
- License: GPL-3.0-only
-
Latest release: 0.4.3
published over 3 years ago
Rankings
Dependencies
- Matrix * imports
- igraph >= 1.2.7 imports
- methods * imports
- reticulate * imports
- RColorBrewer * suggests
- bipartite * suggests
- covr * suggests
- data.table * suggests
- devtools * suggests
- graphsim * suggests
- knitr * suggests
- markdown * suggests
- multinet * suggests
- multiplex * suggests
- network * suggests
- qpdf * suggests
- remotes * suggests
- rmarkdown * suggests
- spelling * suggests
- testthat * suggests
- tibble * suggests