From 8f2e7c8b167dd58fc6ae8068b886e98612cb9c4a Mon Sep 17 00:00:00 2001 From: I-Hsuan Lin <9032946+ycl6@users.noreply.github.com> Date: Wed, 26 Jun 2024 21:35:54 +0100 Subject: [PATCH] Add seed to 'netEmbedding' function --- R/analysis.R | 17 ++++++++++++++--- man/netEmbedding.Rd | 3 +++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/R/analysis.R b/R/analysis.R index a455f61..9d623ae 100644 --- a/R/analysis.R +++ b/R/analysis.R @@ -643,6 +643,7 @@ computeNetSimilarityPairwise <- function(object, slot.name = "netP", type = c("f #' @param min_dist This controls how tightly the embedding is allowed compress points together. #' Larger values ensure embedded points are moreevenly distributed, while smaller values allow the #' algorithm to optimise more accurately with regard to local structure. Sensible values are in the range 0.001 to 0.5. +#' @param seed An integer value indicating the random seed passed to \code{\link[base]{set.seed}}, use `NULL` to re-initializes seed. Defaults to 42. #' @param ... Parameters passing to umap #' @importFrom methods slot #' @return @@ -650,7 +651,7 @@ computeNetSimilarityPairwise <- function(object, slot.name = "netP", type = c("f #' #' @examples netEmbedding <- function(object, slot.name = "netP", type = c("functional","structural"), comparison = NULL, pathway.remove = NULL, - umap.method = c("umap-learn", "uwot"), n_neighbors = NULL,min_dist = 0.3,...) { + umap.method = c("umap-learn", "uwot"), n_neighbors = NULL, min_dist = 0.3, seed = 42,...) { umap.method <- match.arg(umap.method) if (object@options$mode == "single") { comparison <- "single" @@ -674,11 +675,21 @@ netEmbedding <- function(object, slot.name = "netP", type = c("functional","stru n_neighbors <- ceiling(sqrt(dim(Similarity)[1])) + 1 } options(warn = -1) + + if (is.numeric(seed)) { + seed <- as.integer(seed) + } + # dimension reduction if (umap.method == "umap-learn") { - Y <- runUMAP(Similarity, min_dist = min_dist, n_neighbors = n_neighbors,...) + Y <- runUMAP(Similarity, min_dist = min_dist, n_neighbors = n_neighbors, seed.use = seed,...) } else if (umap.method == "uwot") { - Y <- uwot::umap(Similarity, min_dist = min_dist, n_neighbors = n_neighbors,...) + if(packageVersion("uwot")<"0.1.15") { + set.seed(seed) + Y <- uwot::umap(Similarity, min_dist = min_dist, n_neighbors = n_neighbors,...) + } else { + Y <- uwot::umap(Similarity, min_dist = min_dist, n_neighbors = n_neighbors, seed = seed...) + } colnames(Y) <- paste0('UMAP', 1:ncol(Y)) rownames(Y) <- colnames(Similarity) } diff --git a/man/netEmbedding.Rd b/man/netEmbedding.Rd index ef18dce..310d1f0 100644 --- a/man/netEmbedding.Rd +++ b/man/netEmbedding.Rd @@ -13,6 +13,7 @@ netEmbedding( umap.method = c("umap-learn", "uwot"), n_neighbors = NULL, min_dist = 0.3, + seed = 42, ... ) } @@ -37,6 +38,8 @@ Can be umap-learn: Run the python umap-learn package; uwot: Runs umap via the uw Larger values ensure embedded points are moreevenly distributed, while smaller values allow the algorithm to optimise more accurately with regard to local structure. Sensible values are in the range 0.001 to 0.5.} +\item{seed}{An integer value indicating the random seed passed to \code{\link{set.seed}}, use `NULL` to re-initializes seed. Defaults to 42.} + \item{...}{Parameters passing to umap} } \description{