diff --git a/DESCRIPTION b/DESCRIPTION index a533bf8..83752fe 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: leiden Type: Package Title: R implementation of the Leiden package -Version: 0.2.0 +Version: 0.2.1 Date: 2018 Author: Tom Kelly Maintainer: Tom Kelly diff --git a/NAMESPACE b/NAMESPACE index 1156c91..ea8c261 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,4 +1,5 @@ # Generated by roxygen2: do not edit by hand export(leiden) -import(reticulate) +importFrom(reticulate,import) +importFrom(reticulate,r_to_py) diff --git a/R/leiden.R b/R/leiden.R index f048795..fde354b 100644 --- a/R/leiden.R +++ b/R/leiden.R @@ -7,23 +7,37 @@ ##' ##' @param adj_mat An adjacency matrix compatible with \code{\link[igraph]{igraph}} object. ##' @param partition_type Type of partition to use. Defaults to RBConfigurationVertexPartition. Options include: ModularityVertexPartition, RBERVertexPartition, CPMVertexPartition, MutableVertexPartition, SignificanceVertexPartition, SurpriseVertexPartition (see the Leiden python module documentation for more details) +##' @param initial_membership,weights,node_sizes Parameters to pass to the Python leidenalg function (defaults initial_membership=None, weights=None). ##' @param resolution_parameter A parameter controlling the coarseness of the clusters. Higher values lead to more clusters. (defaults to 1.0 for partition types that accept a resolution parameter) -##' @param ... Parameters to pass to the Python leidenalg function (defaults initial_membership=None, weights=None). ##' ##' @keywords graph network igraph mvtnorm simulation -##' @import reticulate +##' @importFrom reticulate import r_to_py ##' @export -leiden <- function(adj_mat, partition_type = c('RBConfigurationVertexPartition', 'ModularityVertexPartition', 'RBERVertexPartition', 'CPMVertexPartition', 'MutableVertexPartition', 'SignificanceVertexPartition', 'SurpriseVertexPartition'), resolution_parameter = 1, ...){ +leiden <- function(adj_mat, + partition_type = c( + 'RBConfigurationVertexPartition', + 'ModularityVertexPartition', + 'RBERVertexPartition', + 'CPMVertexPartition', + 'MutableVertexPartition', + 'SignificanceVertexPartition', + 'SurpriseVertexPartition' +), +initial_membership = NULL, +weights = NULL, +node_sizes = NULL, +resolution_parameter = 1 +) { #import python modules with reticulate - leidenalg <- reticulate::import("leidenalg", delay_load = TRUE) - ig <- reticulate::import("igraph", delay_load = TRUE) + leidenalg <- import("leidenalg", delay_load = TRUE) + ig <- import("igraph", delay_load = TRUE) #convert matrix input adj_mat <- as.matrix(ceiling(adj_mat)) ##convert to python numpy.ndarray, then a list - adj_mat_py <- reticulate::r_to_py(adj_mat) + adj_mat_py <- r_to_py(adj_mat) adj_mat_py <- adj_mat_py$tolist() #convert graph structure to a Python compatible object @@ -31,27 +45,52 @@ leiden <- function(adj_mat, partition_type = c('RBConfigurationVertexPartition', #compute partitions partition_type <- partition_type[1] - if(partition_type == "RBConfigurationVertexPartition"){ - part <- leidenalg$find_partition(snn_graph, leidenalg$RBConfigurationVertexPartition, resolution_parameter = resolution_parameter, ...) - } else if(partition_type == "ModularityVertexPartition"){ - part <- leidenalg$find_partition(snn_graph, leidenalg$ModularityVertexPartition, ...) - } else if(partition_type == "RBERVertexPartition"){ - part <- leidenalg$find_partition(snn_graph, leidenalg$RBERVertexPartition, resolution_parameter = resolution_parameter, ...) - } else if(partition_type == "CPMVertexPartition"){ - part <- leidenalg$find_partition(snn_graph, leidenalg$CPMVertexPartition, resolution_parameter = resolution_parameter, ...) - } else if(partition_type == "MutableVertexPartition"){ - part <- leidenalg$find_partition(snn_graph, leidenalg$MutableVertexPartition, ...) - } else if(partition_type == "SignificanceVertexPartition"){ - part <- leidenalg$find_partition(snn_graph, leidenalg$SignificanceVertexPartition, resolution_parameter = resolution_parameter, ...) - } else if(partition_type == "SurpriseVertexPartition"){ - part <- leidenalg$find_partition(snn_graph, leidenalg$SurpriseVertexPartition, resolution_parameter = resolution_parameter, ...) - } else { + part <- switch( + EXPR = partition_type, + 'RBConfigurationVertexPartition' = leidenalg$find_partition( + snn_graph, + leidenalg$RBConfigurationVertexPartition, + initial_membership = initial_membership, weights = weights, + resolution_parameter = resolution_parameter + ), + 'ModularityVertexPartition' = leidenalg$find_partition( + snn_graph, + leidenalg$ModularityVertexPartition, + initial_membership = initial_membership, weights = weights + ), + 'RBERVertexPartition' = leidenalg$find_partition( + snn_graph, + leidenalg$RBERVertexPartition, + initial_membership = initial_membership, weights = weights, node_sizes = node_sizes, + resolution_parameter = resolution_parameter + ), + 'CPMVertexPartition' = leidenalg$find_partition( + snn_graph, + leidenalg$CPMVertexPartition, + initial_membership = initial_membership, weights = weights, node_sizes = node_sizes, + resolution_parameter = resolution_parameter + ), + 'MutableVertexPartition' = leidenalg$find_partition( + snn_graph, + leidenalg$MutableVertexPartition, + initial_membership = initial_membership + ), + 'SignificanceVertexPartition' = leidenalg$find_partition( + snn_graph, + leidenalg$SignificanceVertexPartition, + initial_membership = initial_membership, node_sizes = node_sizes, + resolution_parameter = resolution_parameter + ), + 'SurpriseVertexPartition' = leidenalg$find_partition( + snn_graph, + leidenalg$SurpriseVertexPartition, + initial_membership = initial_membership, weights = weights, node_sizes = node_sizes + ), stop("please specify a partition type as a string out of those documented") - } + ) return(part$membership+1) } - # global reference to python modules (will be initialized in .onLoad) leidenalg <- NULL ig <- NULL diff --git a/README.md b/README.md index d258b52..a1c9344 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Leiden Algorithm -## leiden version 0.2.0 +## leiden version 0.2.1 [![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/leiden)](https://cran.r-project.org/package=leiden) [![Travis Build Status](https://travis-ci.org/TomKellyGenetics/leiden.svg?branch=master)](https://travis-ci.org/TomKellyGenetics/leiden) @@ -84,7 +84,7 @@ Please cite this implementation R in if you use it: To cite the leiden package in publications use: S. Thomas Kelly (2018). leiden: R implementation of the Leiden algorithm. R - package version 0.1.0 https://github.com/TomKellyGenetics/leiden + package version 0.2.1 https://github.com/TomKellyGenetics/leiden A BibTeX entry for LaTeX users is @@ -92,7 +92,7 @@ A BibTeX entry for LaTeX users is title = {leiden: R implementation of the Leiden algorithm}, author = {S. Thomas Kelly}, year = {2018}, - note = {R package version 0.2.0}, + note = {R package version 0.2.1}, url = {https://github.com/TomKellyGenetics/leiden}, } ``` diff --git a/man/leiden.Rd b/man/leiden.Rd index a2403ea..554c729 100644 --- a/man/leiden.Rd +++ b/man/leiden.Rd @@ -8,16 +8,17 @@ leiden(adj_mat, partition_type = c("RBConfigurationVertexPartition", "ModularityVertexPartition", "RBERVertexPartition", "CPMVertexPartition", "MutableVertexPartition", "SignificanceVertexPartition", - "SurpriseVertexPartition"), resolution_parameter = 1, ...) + "SurpriseVertexPartition"), initial_membership = NULL, + weights = NULL, node_sizes = NULL, resolution_parameter = 1) } \arguments{ \item{adj_mat}{An adjacency matrix compatible with \code{\link[igraph]{igraph}} object.} \item{partition_type}{Type of partition to use. Defaults to RBConfigurationVertexPartition. Options include: ModularityVertexPartition, RBERVertexPartition, CPMVertexPartition, MutableVertexPartition, SignificanceVertexPartition, SurpriseVertexPartition (see the Leiden python module documentation for more details)} -\item{resolution_parameter}{A parameter controlling the coarseness of the clusters. Higher values lead to more clusters. (defaults to 1.0 for partition types that accept a resolution parameter)} +\item{initial_membership, weights, node_sizes}{Parameters to pass to the Python leidenalg function (defaults initial_membership=None, weights=None).} -\item{...}{Parameters to pass to the Python leidenalg function (defaults initial_membership=None, weights=None).} +\item{resolution_parameter}{A parameter controlling the coarseness of the clusters. Higher values lead to more clusters. (defaults to 1.0 for partition types that accept a resolution parameter)} } \description{ Implements the Leiden clustering algorithm in R using reticulate to run the Python version. Requires the python "leidenalg" and "igraph" modules to be installed. Returns a vector of partition indices.