Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions R/cluster_gene.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 <whou10@jhu.edu>
#' @param testobj object returned from lamian.test().
Expand All @@ -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
Expand All @@ -44,7 +45,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
}
Expand Down
6 changes: 4 additions & 2 deletions R/evaluate_uncertainty.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -18,7 +19,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
) {
Expand All @@ -44,7 +46,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
Expand Down
7 changes: 4 additions & 3 deletions R/infer_tree_structure.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#' @author Wenpin Hou <whou10@jhu.edu>
#' @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.
Expand Down Expand Up @@ -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)
Expand All @@ -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],
Expand Down
5 changes: 3 additions & 2 deletions R/mykmeans.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down