Skip to content

Commit c00132c

Browse files
committed
cores can be specified
1 parent eac05df commit c00132c

File tree

4 files changed

+31
-17
lines changed

4 files changed

+31
-17
lines changed

R/compute_mutual_information.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,9 @@ compute_mutual_information <- function(dir_out, cores=NULL) {
8686
# register cores for parallel computation
8787
if (is.null(cores)) {
8888
cores <- max(1, parallel::detectCores() - 2)
89+
message(sprintf("Number of cores not specified, using %.0f.", cores))
8990
} else {
90-
cores <- max(1, cores)
91+
message(sprintf("Using %.0f specified cores.", cores))
9192
}
9293
doParallel::registerDoParallel(cores = cores)
9394

R/compute_non_self_talk.R

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ compute_non_self_talk_type <- function(ligands, type, letter, dir_in, dir_out) {
4343
index_valid <- index[valid,]
4444
ligands_valid <- ligands[valid,]
4545

46-
# detect and register cores
47-
cores <- max(1, parallel::detectCores() - 2)
48-
doParallel::registerDoParallel(cores = cores)
49-
5046
# parallel loop for MI distances
5147
i <- NULL
5248
score <- foreach::`%dopar%`(
@@ -72,9 +68,6 @@ compute_non_self_talk_type <- function(ligands, type, letter, dir_in, dir_out) {
7268

7369
# write out
7470
vroom::vroom_write(score, fpath_out, progress = FALSE)
75-
76-
# unregister cores
77-
doParallel::stopImplicitCluster()
7871
NULL
7972
}
8073

@@ -100,8 +93,22 @@ compute_non_self_talk_type <- function(ligands, type, letter, dir_in, dir_out) {
10093
#' @param dir_out Output directory
10194
#' @return None
10295
#' @export
103-
compute_non_self_talk <- function(ligands, type_a, type_b, dir_in, dir_out) {
96+
compute_non_self_talk <- function(
97+
ligands, type_a, type_b, dir_in, dir_out, cores=NULL) {
98+
99+
# detect and register cores
100+
if (is.null(cores)) {
101+
cores <- max(1, parallel::detectCores() - 2)
102+
message(sprintf("Number of cores not specified, using %.0f.", cores))
103+
} else {
104+
message(sprintf("Using %.0f specified cores.", cores))
105+
}
106+
doParallel::registerDoParallel(cores = cores)
107+
104108
compute_non_self_talk_type(ligands, type_a, "A", dir_in, dir_out)
105109
compute_non_self_talk_type(ligands, type_b, "B", dir_in, dir_out)
110+
111+
# unregister cores
112+
doParallel::stopImplicitCluster()
106113
NULL
107114
}

R/cytotalk.R

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ run_cytotalk <- function(
116116
ligands=CytoTalk::ligands_human,
117117
cutoff_a=0.1, cutoff_b=0.1,
118118
beta_max=100, omega_min=0.5, omega_max=0.5,
119-
depth=3, ntrial=10000) {
119+
depth=3, ntrial=10000, cores=NULL) {
120120

121121
# must have valid data directory
122122
type_names <- check_valid_names(dir_in)
@@ -143,7 +143,8 @@ run_cytotalk <- function(
143143
# compute preferential expression measure
144144
tick(1, "Preprocessing...")
145145
preprocess(proteins, type_a, type_b, cutoff_a, cutoff_b, dir_in, dir_out)
146-
compute_non_self_talk(ligands, type_a, type_b, dir_in, dir_out)
146+
compute_non_self_talk(
147+
ligands, type_a, type_b, dir_in, dir_out, cores)
147148
status <- compute_pem(dir_in, dir_out)
148149

149150
# exit if bad PEM status
@@ -153,7 +154,7 @@ run_cytotalk <- function(
153154

154155
# compute mutual information (within types)
155156
tick(2, "Mutual information matrix...")
156-
compute_mutual_information(dir_out)
157+
compute_mutual_information(dir_out, cores)
157158

158159
# use ARACNE.m to filter out indirect edges
159160
tick(3, "Indirect edge-filtered network...")
@@ -170,7 +171,7 @@ run_cytotalk <- function(
170171

171172
# run Kolmogorov-Smirnov tests
172173
tick(6, "Determine best signaling network...")
173-
generate_signaling_network(dir_out)
174+
generate_signaling_network(dir_out, cores)
174175

175176
# generate SIF and SVG files
176177
tick(7, "Generate network output...")

R/generate_signaling_network.R

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ generate_summary <- function(dir_out) {
3636
}
3737

3838
#' @noRd
39-
compute_kolmogorov_smirnov <- function(dir_out) {
39+
compute_kolmogorov_smirnov <- function(dir_out, cores=NULL) {
4040
# format filepaths
4141
fpath_edge <- file.path(dir_out, "PCSF_EdgeOccurance.txt")
4242
fpath_pval <- file.path(dir_out, "PCSF_EdgeTestValues.txt")
@@ -64,7 +64,12 @@ compute_kolmogorov_smirnov <- function(dir_out) {
6464
lst_counts <- tapply(vec_counts, vec_param, c)
6565

6666
# detect and register cores
67-
cores <- max(1, parallel::detectCores() - 2)
67+
if (is.null(cores)) {
68+
cores <- max(1, parallel::detectCores() - 2)
69+
message(sprintf("Number of cores not specified, using %.0f.", cores))
70+
} else {
71+
message(sprintf("Using %.0f specified cores.", cores))
72+
}
6873
doParallel::registerDoParallel(cores = cores)
6974

7075
# parallel loop for Kolmogorov-Smirnov test
@@ -105,8 +110,8 @@ compute_kolmogorov_smirnov <- function(dir_out) {
105110
#' @param dir_out Output directory
106111
#' @return None
107112
#' @export
108-
generate_signaling_network <- function(dir_out) {
113+
generate_signaling_network <- function(dir_out, cores=NULL) {
109114
generate_summary(dir_out)
110-
compute_kolmogorov_smirnov(dir_out)
115+
compute_kolmogorov_smirnov(dir_out, cores)
111116
NULL
112117
}

0 commit comments

Comments
 (0)