Skip to content

Commit 7b72c62

Browse files
committed
Performance improvements for evalSigGeneImportance and analyzeLocalCorrelation
1 parent b764190 commit 7b72c62

2 files changed

Lines changed: 51 additions & 24 deletions

File tree

R/AnalysisFunctions.R

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,21 @@ calcSignatureScores <- function(
285285
sigScores <- batchSigEvalNorm(object@sigData, normExpr)
286286

287287
if (sig_gene_importance) {
288-
sigGeneImportance <- evalSigGeneImportanceSparse(
289-
sigScores, object@sigData, normExpr
290-
)
288+
289+
if (is(object@exprData, "sparseMatrix")) {
290+
sigGeneImportance <- evalSigGeneImportanceSparse(
291+
sigScores, object@sigData, normExpr
292+
)
293+
} else {
294+
normExprDense <- getNormalizedCopy(
295+
object@exprData,
296+
object@params$signatures$sigNormMethod
297+
)
298+
sigGeneImportance <- evalSigGeneImportance(
299+
sigScores, object@sigData, normExprDense
300+
)
301+
}
302+
291303
} else {
292304
sigGeneImportance <- list()
293305
}

R/methods-Signature.R

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -333,22 +333,28 @@ sigsVsProjection_n <- function(sigScores, randomSigData,
333333
weights$weights <- weights$weights[cells, , drop = FALSE]
334334
}
335335

336-
gearyFG <- pbmclapply(seq_len(ncol(sigScoreMatrix)), function(ii) {
336+
# Thread setup time is around .36 seconds
337+
# Compute batches so that thread setup/teardown doesn't dominate runtime
338+
per_batch <- 3.6 * 58000 / length(weights$indices) / .0032
339+
per_batch <- max(min(per_batch, 2000), 10)
340+
341+
fgSigBatches <- batchify(seq_len(ncol(sigScoreMatrix)), per_batch, getOption("mc.cores", 2L))
342+
343+
gearyFG <- pbmclapply(fgSigBatches, function(ii) {
337344
sigScoreMatrixGroup <- sigScoreMatrix[, ii, drop = FALSE]
338345
sigScoreMatrixGroup <- matrixStats::colRanks(
339346
sigScoreMatrixGroup, preserveShape = TRUE, ties.method = "average"
340347
)
341348
geary_c <- geary_sig_v_proj(sigScoreMatrixGroup, weights$indices, weights$weights)
342349
return(geary_c)
343350
}, mc.preschedule = FALSE)
344-
gearyFG <- unlist(gearyFG)
345-
names(gearyFG) <- colnames(sigScoreMatrix)
351+
gearyFG <- setNames(unlist(gearyFG), colnames(sigScoreMatrix))
346352

347353
randomSigs <- randomSigData$randomSigs
348354
sigAssignments <- randomSigData$sigAssignments
349355
randomSigAssignments <- randomSigData$randomSigAssignments
350356

351-
randomSigBatches <- batchify(randomSigs, 10)
357+
randomSigBatches <- batchify(randomSigs, per_batch, getOption("mc.cores", 2L))
352358

353359
gearyBG <- pbmclapply(randomSigBatches, function(randomSigSubset){
354360
randomSigScores <- t(innerEvalSignatureBatchNorm(normExpr, randomSigSubset))
@@ -444,31 +450,40 @@ sigsVsProjection_pcn <- function(metaData, weights, cells = NULL, computePval =
444450
}
445451
jobs <- data.frame(type = type, col = col, stringsAsFactors = FALSE)
446452

447-
results <- pbmclapply(seq_len(nrow(jobs)), function(i) {
453+
# Thread setup time is around .36 seconds
454+
# Compute batches so that thread setup/teardown doesn't dominate runtime
455+
per_batch <- 3.6 * 58000 / length(weights$indices) / .0032
456+
per_batch <- max(min(per_batch, 2000), 10)
457+
jbatches <- batchify(seq_len(nrow(jobs)), per_batch, getOption("mc.cores", 2L))
448458

449-
type <- jobs[i, "type"]
450-
col <- jobs[i, "col"]
459+
results <- pbmclapply(jbatches, function(jbatch) {
451460

461+
lapply(jbatch, function(i) {
452462

453-
sigScores <- numericMetaRanks[, col, drop = FALSE]
463+
type <- jobs[i, "type"]
464+
col <- jobs[i, "col"]
454465

455-
if (any(is.na(sigScores))){
456-
return(0.0)
457-
}
458466

459-
if (all(sigScores == sigScores[1])) {
460-
return(0.0)
461-
}
467+
sigScores <- numericMetaRanks[, col, drop = FALSE]
462468

463-
if (type == "bg"){
464-
sigScores <- matrix(sample(sigScores), ncol = 1)
465-
colnames(sigScores) <- colnames(numericMetaRanks)[col]
466-
}
469+
if (any(is.na(sigScores))){
470+
return(0.0)
471+
}
472+
473+
if (all(sigScores == sigScores[1])) {
474+
return(0.0)
475+
}
467476

468-
geary_c <- geary_sig_v_proj(
469-
sigScores, weights$indices, weights$weights)
477+
if (type == "bg"){
478+
sigScores <- matrix(sample(sigScores), ncol = 1)
479+
colnames(sigScores) <- colnames(numericMetaRanks)[col]
480+
}
470481

471-
return(1 - geary_c)
482+
geary_c <- geary_sig_v_proj(
483+
sigScores, weights$indices, weights$weights)
484+
485+
return(1 - geary_c)
486+
})
472487

473488
}, mc.preschedule = FALSE)
474489

0 commit comments

Comments
 (0)