Skip to content

Commit

Permalink
enable passing arguments and default to python leidenalg satijalab/se…
Browse files Browse the repository at this point in the history
  • Loading branch information
TomKellyGenetics committed Jan 30, 2019
1 parent e86f551 commit 53c529a
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 31 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
Maintainer: Tom Kelly <[email protected]>
Expand Down
3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Generated by roxygen2: do not edit by hand

export(leiden)
import(reticulate)
importFrom(reticulate,import)
importFrom(reticulate,r_to_py)
85 changes: 62 additions & 23 deletions R/leiden.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,90 @@
##'
##' @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
snn_graph <- ig$Graph$Adjacency(adj_mat_py)

#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
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -84,15 +84,15 @@ 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
@Manual{,
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},
}
```
Expand Down
7 changes: 4 additions & 3 deletions man/leiden.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 53c529a

Please sign in to comment.