From bcc626a7f3524db85feb1320c30ea1b4597d8757 Mon Sep 17 00:00:00 2001 From: Alexis-Varin Date: Fri, 5 Apr 2024 12:56:52 +0200 Subject: [PATCH 1/2] Fixing mc.cores > 1 error on Windows computers. --- R/cluster_gene.R | 2 +- R/evaluate_uncertainty.R | 5 +++-- R/infer_tree_structure.R | 5 +++-- R/mykmeans.R | 5 +++-- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/R/cluster_gene.R b/R/cluster_gene.R index d50ca7f..6a3f69b 100644 --- a/R/cluster_gene.R +++ b/R/cluster_gene.R @@ -44,7 +44,7 @@ cluster_gene <- function(testobj, set.seed(seed) # if (k.auto){ - clu <- mykmeans(mat.scale, maxclunum = 20)$cluster + clu <- mykmeans(mat.scale, maxclunum = 20, ncores = ncores)$cluster } else { clu <- kmeans(mat.scale, k, iter.max = 1000)$cluster } diff --git a/R/evaluate_uncertainty.R b/R/evaluate_uncertainty.R index 1bba75c..055b13a 100644 --- a/R/evaluate_uncertainty.R +++ b/R/evaluate_uncertainty.R @@ -18,7 +18,8 @@ evaluate_uncertainty <- n.permute, subset.cell = NULL, design = NULL, - return.ctcomp = FALSE + return.ctcomp = FALSE, + ncores = detectCores() # branchPropTest.method = 'ttest', ## this is a quick way to call the t-test in branchPropTest(); however, if want to use the multinom test, call branchPropTest() separately. # branchPropTest.value.log = FALSE ) { @@ -44,7 +45,7 @@ evaluate_uncertainty <- ## cluster cells invisible(capture.output(clu <- - mykmeans(pr.pm, number.cluster = max(inferobj$clusterid))$cluster)) + mykmeans(pr.pm, number.cluster = max(inferobj$clusterid))$cluster), ncores = ncores) ## build pseudotime diff --git a/R/infer_tree_structure.R b/R/infer_tree_structure.R index dd06e93..920c1f4 100644 --- a/R/infer_tree_structure.R +++ b/R/infer_tree_structure.R @@ -35,7 +35,8 @@ infer_tree_structure <- xlab = 'PC1', ylab = 'PC2', max.clunum = 50, - kmeans.seed = 12345) { + kmeans.seed = 12345, + ncores = detectCores()) { alls <- cellanno[, 2] names(alls) <- cellanno[, 1] ## set.seed(12345) @@ -50,7 +51,7 @@ infer_tree_structure <- ## clustering clu <- - mykmeans(pr, maxclunum = 50, number.cluster = number.cluster, seed = kmeans.seed)$cluster + mykmeans(pr, maxclunum = 50, number.cluster = number.cluster, seed = kmeans.seed, ncores = ncores)$cluster table(clu) pd = data.frame(x = pr[, 1], y = pr[, 2], diff --git a/R/mykmeans.R b/R/mykmeans.R index 09a7063..5b088eb 100644 --- a/R/mykmeans.R +++ b/R/mykmeans.R @@ -16,14 +16,15 @@ mykmeans <- function(matrix, number.cluster = NA, maxclunum = 30, - seed = 12345) { + seed = 12345, + ncores = detectCores()) { library(parallel) if (is.na(number.cluster)) { rss <- mclapply(seq_len(maxclunum), function(clunum) { ## set.seed(seed) tmp <- kmeans(matrix, clunum, iter.max = 1000) tmp$betweenss / tmp$totss - }, mc.cores = 30) + }, mc.cores = ncores) rss <- unlist(rss) # number.cluster <- which(diff(rss) < 1e-2)[1] x <- 2:maxclunum From 9fbd520e566abe7ac7a2f2d0ed13eb372263ab81 Mon Sep 17 00:00:00 2001 From: Alexis-Varin Date: Fri, 5 Apr 2024 13:06:04 +0200 Subject: [PATCH 2/2] Adding parallel to import --- R/cluster_gene.R | 5 +++-- R/evaluate_uncertainty.R | 1 + R/infer_tree_structure.R | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/R/cluster_gene.R b/R/cluster_gene.R index 6a3f69b..2a88920 100644 --- a/R/cluster_gene.R +++ b/R/cluster_gene.R @@ -2,7 +2,7 @@ #' #' This function is used to support the external function clusterGene(). cluster genes based on their temporal patterns of gene expression (constant time test) or the temporal patterns of gene expression group difference (sample covariate test). #' -#' @import ggplot2 RColorBrewer splines gridExtra viridis +#' @import ggplot2 RColorBrewer splines gridExtra viridis parallel #' @return a plot #' @author Wenpin Hou #' @param testobj object returned from lamian.test(). @@ -19,7 +19,8 @@ cluster_gene <- function(testobj, type = 'Time', method = 'kmeans', scale.difference = F, - seed = 12345){ + seed = 12345, + ncores = detectCores()){ if (toupper(type) == 'TIME'){ if ('populationFit' %in% names(testobj)) { fit <- testobj$populationFit diff --git a/R/evaluate_uncertainty.R b/R/evaluate_uncertainty.R index 055b13a..a031f6a 100644 --- a/R/evaluate_uncertainty.R +++ b/R/evaluate_uncertainty.R @@ -2,6 +2,7 @@ #' #' This function is designed to evaluate the pseudotime tree uncertainty, as one of the main functions in lamain module 1. #' +#' @import parallel #' @param inferobj the output object from function infer_tree_structure(). #' @param n.permute: a numeric number of permutation in the permutation test. #' @param subset.cell a character vector of the names of the selected cells where boostrap will happen on. If NULL, then boostrap from all the cells. diff --git a/R/infer_tree_structure.R b/R/infer_tree_structure.R index 920c1f4..3f3e307 100644 --- a/R/infer_tree_structure.R +++ b/R/infer_tree_structure.R @@ -5,7 +5,7 @@ #' @author Wenpin Hou #' @return a list #' @export -#' @import TSCAN scattermore RColorBrewer grDevices +#' @import TSCAN scattermore RColorBrewer grDevices parallel #' @importFrom grDevices pdf #' @importFrom grDevices dev.off #' @param pca cell by principal component (pc) matrix. Principal components reduction of the cells.