...
|
...
|
@@ -313,21 +313,16 @@ setMethod("gsva", signature(expr="matrix", gset.idx.list="list"),
|
313
|
313
|
## we need to harmonize it with the contents of BPPARAM
|
314
|
314
|
if (parallel.sz > 1L && class(BPPARAM) == "SerialParam") {
|
315
|
315
|
BPPARAM=MulticoreParam(progressbar=verbose, workers=parallel.sz, tasks=100)
|
316
|
|
- if (verbose)
|
317
|
|
- message(sprintf("Setting parallel calculations through a multicore back-end with workers=%d and tasks=100.",
|
318
|
|
- parallel.sz))
|
319
|
316
|
} else if (parallel.sz == 1L && class(BPPARAM) != "SerialParam") {
|
320
|
317
|
parallel.sz <- bpnworkers(BPPARAM)
|
321
|
|
- if (verbose)
|
322
|
|
- message(sprintf("Setting parallel calculations through a %s back-end with %d workers.",
|
323
|
|
- class(BPPARAM), parallel.sz))
|
324
|
318
|
} else if (parallel.sz > 1L && class(BPPARAM) != "SerialParam") {
|
325
|
319
|
bpworkers(BPPARAM) <- parallel.sz
|
326
|
|
- if (verbose)
|
327
|
|
- message(sprintf("Setting parallel calculations through a %s back-end with %d workers.",
|
328
|
|
- class(BPPARAM), parallel.sz))
|
329
|
320
|
}
|
330
|
321
|
|
|
322
|
+ if (class(BPPARAM) != "SerialParam" && verbose)
|
|
323
|
+ cat(sprintf("Setting parallel calculations through a %s back-end\nwith workers=%d and tasks=100.\n",
|
|
324
|
+ class(BPPARAM), parallel.sz))
|
|
325
|
+
|
331
|
326
|
if (method == "ssgsea") {
|
332
|
327
|
if(verbose)
|
333
|
328
|
cat("Estimating ssGSEA scores for", length(gset.idx.list),"gene sets.\n")
|
...
|
...
|
@@ -342,7 +337,7 @@ setMethod("gsva", signature(expr="matrix", gset.idx.list="list"),
|
342
|
337
|
stop("rnaseq=TRUE does not work with method='zscore'.")
|
343
|
338
|
|
344
|
339
|
if(verbose)
|
345
|
|
- cat("Estimating combined z-scores for", length(gset.idx.list),"gene sets.\n")
|
|
340
|
+ cat("Estimating combined z-scores for", length(gset.idx.list), "gene sets.\n")
|
346
|
341
|
|
347
|
342
|
return(zscore(expr, gset.idx.list, parallel.sz, parallel.type,
|
348
|
343
|
verbose, BPPARAM=BPPARAM))
|
...
|
...
|
@@ -370,8 +365,6 @@ setMethod("gsva", signature(expr="matrix", gset.idx.list="list"),
|
370
|
365
|
colnames(es.obs) <- colnames(expr)
|
371
|
366
|
rownames(es.obs) <- names(gset.idx.list)
|
372
|
367
|
|
373
|
|
- if (verbose)
|
374
|
|
- cat("Computing GSVA enrichment scores\n")
|
375
|
368
|
es.obs <- compute.geneset.es(expr, gset.idx.list, 1:n.samples,
|
376
|
369
|
rnaseq=rnaseq, abs.ranking=abs.ranking,
|
377
|
370
|
parallel.sz=parallel.sz, parallel.type=parallel.type,
|
...
|
...
|
@@ -427,8 +420,12 @@ compute.geneset.es <- function(expr, gset.idx.list, sample.idxs, rnaseq=FALSE,
|
427
|
420
|
} else
|
428
|
421
|
cat("Estimating ECDFs directly\n")
|
429
|
422
|
}
|
430
|
|
- if (parallel.sz > 0 && length(sample.idxs > 100) && nrow(expr) > 100) {
|
431
|
|
- cat(sprintf("Using %d cores in parallel for ECDFs estimation\n", parallel.sz))
|
|
423
|
+
|
|
424
|
+ ## open parallelism only if ECDFs have to be estimated for
|
|
425
|
+ ## more than 100 genes on more than 100 samples
|
|
426
|
+ if (parallel.sz > 1 && length(sample.idxs > 100) && nrow(expr) > 100) {
|
|
427
|
+ if (verbose)
|
|
428
|
+ cat(sprintf("Estimating ECDFs in parallel\n", parallel.sz))
|
432
|
429
|
iter <- function(Y, n_chunks=BiocParallel::multicoreWorkers()) {
|
433
|
430
|
idx <- splitIndices(nrow(Y), min(nrow(Y), n_chunks))
|
434
|
431
|
i <- 0L
|
...
|
...
|
@@ -458,99 +455,16 @@ compute.geneset.es <- function(expr, gset.idx.list, sample.idxs, rnaseq=FALSE,
|
458
|
455
|
sort.sgn.idxs <- apply(gene.density, 2, order, decreasing=TRUE) # n.genes * n.samples
|
459
|
456
|
|
460
|
457
|
rank.scores <- apply(sort.sgn.idxs, 2, compute_rank_score)
|
461
|
|
-
|
462
|
|
- haveParallel <- .isPackageLoaded("parallel")
|
463
|
|
- haveSnow <- .isPackageLoaded("snow")
|
464
|
|
-
|
465
|
|
- if (parallel.sz > 1 || haveParallel) {
|
466
|
|
- if (!haveParallel && !haveSnow) {
|
467
|
|
- stop("In order to run calculations in parallel either the 'snow', or the 'parallel' library, should be loaded first")
|
468
|
|
- }
|
469
|
|
-
|
470
|
|
- if (haveSnow) { ## use snow
|
471
|
|
- ## copying ShortRead's strategy, the calls to the 'get()' are
|
472
|
|
- ## employed to quieten R CMD check, and for no other reason
|
473
|
|
- makeCl <- get("makeCluster", mode="function")
|
474
|
|
- parSapp <- get("parSapply", mode="function")
|
475
|
|
- clEvalQ <- get("clusterEvalQ", mode="function")
|
476
|
|
- stopCl <- get("stopCluster", mode="function")
|
477
|
|
-
|
478
|
|
- if (verbose)
|
479
|
|
- cat("Allocating cluster\n")
|
480
|
|
- cl <- makeCl(parallel.sz, type = parallel.type)
|
481
|
|
- clEvalQ(cl, library(GSVA))
|
482
|
|
- if (verbose) {
|
483
|
|
- cat("Estimating enrichment scores in parallel\n")
|
484
|
|
- if(mx.diff) {
|
485
|
|
- cat("Taking diff of max KS.\n")
|
486
|
|
- } else{
|
487
|
|
- cat("Evaluting max KS.\n")
|
488
|
|
- }
|
489
|
|
- }
|
490
|
|
-
|
491
|
|
- m <- t(parSapp(cl, gset.idx.list, ks_test_m,
|
492
|
|
- gene.density=rank.scores,
|
493
|
|
- sort.idxs=sort.sgn.idxs,
|
494
|
|
- mx.diff=mx.diff, abs.ranking=abs.ranking,
|
495
|
|
- tau=tau, verbose=FALSE))
|
496
|
|
- if(verbose)
|
497
|
|
- cat("Cleaning up\n")
|
498
|
|
- stopCl(cl)
|
499
|
|
-
|
500
|
|
- } else if (haveParallel) { ## use parallel
|
501
|
|
-
|
502
|
|
- mclapp <- get('mclapply', envir=getNamespace('parallel'))
|
503
|
|
- detCor <- get('detectCores', envir=getNamespace('parallel'))
|
504
|
|
- nCores <- detCor()
|
505
|
|
- setCores(nCores, parallel.sz)
|
506
|
|
-
|
507
|
|
- pb <- NULL
|
508
|
|
- if (verbose){
|
509
|
|
- cat("Using parallel with", getOption("mc.cores"), "cores\n")
|
510
|
|
- assign("progressBar", txtProgressBar(style=3), envir=globalenv()) ## show progress if verbose=TRUE
|
511
|
|
- assign("nGeneSets", ceiling(length(gset.idx.list) / getOption("mc.cores")), envir=globalenv())
|
512
|
|
- assign("iGeneSet", 0, envir=globalenv())
|
513
|
|
- }
|
514
|
458
|
|
515
|
|
- m <- mclapp(gset.idx.list, ks_test_m,
|
516
|
|
- gene.density=rank.scores,
|
517
|
|
- sort.idxs=sort.sgn.idxs,
|
518
|
|
- mx.diff=mx.diff, abs.ranking=abs.ranking,
|
519
|
|
- tau=tau, verbose=verbose)
|
520
|
|
- m <- do.call("rbind", m)
|
521
|
|
- colnames(m) <- colnames(expr)
|
|
459
|
+ m <- bplapply(gset.idx.list, ks_test_m,
|
|
460
|
+ gene.density=rank.scores,
|
|
461
|
+ sort.idxs=sort.sgn.idxs,
|
|
462
|
+ mx.diff=mx.diff, abs.ranking=abs.ranking,
|
|
463
|
+ tau=tau, verbose=verbose,
|
|
464
|
+ BPPARAM=BPPARAM)
|
|
465
|
+ m <- do.call("rbind", m)
|
|
466
|
+ colnames(m) <- colnames(expr)
|
522
|
467
|
|
523
|
|
- if (verbose) {
|
524
|
|
- close(get("progressBar", envir=globalenv()))
|
525
|
|
- }
|
526
|
|
- } else
|
527
|
|
- stop("In order to run calculations in parallel either the 'snow', or the 'parallel' library, should be loaded first")
|
528
|
|
-
|
529
|
|
- } else {
|
530
|
|
- if (verbose) {
|
531
|
|
- cat("Estimating enrichment scores\n")
|
532
|
|
- if (mx.diff) {
|
533
|
|
- cat("Taking diff of max KS.\n")
|
534
|
|
- } else{
|
535
|
|
- cat("Evaluting max KS.\n")
|
536
|
|
- }
|
537
|
|
- }
|
538
|
|
- pb <- NULL
|
539
|
|
- if (verbose){
|
540
|
|
- assign("progressBar", txtProgressBar(style=3), envir=globalenv()) ## show progress if verbose=TRUE
|
541
|
|
- assign("nGeneSets", length(gset.idx.list), envir=globalenv())
|
542
|
|
- assign("iGeneSet", 0, envir=globalenv())
|
543
|
|
- }
|
544
|
|
-
|
545
|
|
- m <- t(sapply(gset.idx.list, ks_test_m, rank.scores, sort.sgn.idxs,
|
546
|
|
- mx.diff=mx.diff, abs.ranking=abs.ranking,
|
547
|
|
- tau=tau, verbose=verbose))
|
548
|
|
-
|
549
|
|
- if (verbose) {
|
550
|
|
- setTxtProgressBar(get("progressBar", envir=globalenv()), 1)
|
551
|
|
- close(get("progressBar", envir=globalenv()))
|
552
|
|
- }
|
553
|
|
- }
|
554
|
468
|
return (m)
|
555
|
469
|
}
|
556
|
470
|
|
...
|
...
|
@@ -574,12 +488,6 @@ ks_test_m <- function(gset_idxs, gene.density, sort.idxs, mx.diff=TRUE,
|
574
|
488
|
as.integer(mx.diff),
|
575
|
489
|
as.integer(abs.ranking))$R
|
576
|
490
|
|
577
|
|
- if (verbose) {
|
578
|
|
- assign("iGeneSet", get("iGeneSet", envir=globalenv()) + 1, envir=globalenv())
|
579
|
|
- setTxtProgressBar(get("progressBar", envir=globalenv()),
|
580
|
|
- get("iGeneSet", envir=globalenv()) / get("nGeneSets", envir=globalenv()))
|
581
|
|
- }
|
582
|
|
-
|
583
|
491
|
return(geneset.sample.es)
|
584
|
492
|
}
|
585
|
493
|
|
...
|
...
|
@@ -656,63 +564,44 @@ ssgsea <- function(X, geneSets, alpha=0.25, parallel.sz,
|
656
|
564
|
p <- nrow(X)
|
657
|
565
|
n <- ncol(X)
|
658
|
566
|
|
659
|
|
- if (verbose) {
|
660
|
|
- assign("progressBar", txtProgressBar(style=3), envir=globalenv()) ## show progress if verbose=TRUE
|
661
|
|
- assign("nSamples", n, envir=globalenv())
|
662
|
|
- assign("iSample", 0, envir=globalenv())
|
663
|
|
- }
|
664
|
|
-
|
665
|
567
|
R <- apply(X, 2, function(x,p) as.integer(rank(x)), p)
|
666
|
568
|
|
667
|
|
- haveParallel <- .isPackageLoaded("parallel")
|
668
|
|
- haveSnow <- .isPackageLoaded("snow")
|
669
|
|
-
|
670
|
|
- cl <- makeCl <- parSapp <- stopCl <- mclapp <- detCor <- nCores <- NA
|
671
|
|
- if (parallel.sz > 1 || haveParallel) {
|
672
|
|
- if (!haveParallel && !haveSnow) {
|
673
|
|
- stop("In order to run calculations in parallel either the 'snow', or the 'parallel' library, should be loaded first")
|
674
|
|
- }
|
675
|
|
-
|
676
|
|
- if (!haveParallel) { ## use snow
|
677
|
|
- ## copying ShortRead's strategy, the calls to the 'get()' are
|
678
|
|
- ## employed to quieten R CMD check, and for no other reason
|
679
|
|
- makeCl <- get("makeCluster", mode="function")
|
680
|
|
- parSapp <- get("parSapply", mode="function")
|
681
|
|
- stopCl <- get("stopCluster", mode="function")
|
682
|
|
-
|
683
|
|
- if (verbose)
|
684
|
|
- cat("Allocating cluster\n")
|
685
|
|
- cl <- makeCl(parallel.sz, type = parallel.type)
|
686
|
|
- } else { ## use parallel
|
687
|
|
-
|
688
|
|
- mclapp <- get('mclapply', envir=getNamespace('parallel'))
|
689
|
|
- detCor <- get('detectCores', envir=getNamespace('parallel'))
|
690
|
|
- nCores <- detCor()
|
691
|
|
- setCores(nCores, parallel.sz)
|
692
|
|
- if (verbose)
|
693
|
|
- cat("Using parallel with", getOption("mc.cores"), "cores\n")
|
|
569
|
+ es <- matrix(NA, nrow=length(geneSets), ncol=ncol(X))
|
|
570
|
+
|
|
571
|
+ ## if there are more gene sets than samples, then
|
|
572
|
+ ## parallelization is done throughout gene sets
|
|
573
|
+ if (length(geneSets) > n) {
|
|
574
|
+ if (verbose) {
|
|
575
|
+ assign("progressBar", txtProgressBar(style=3), envir=globalenv())
|
|
576
|
+ assign("nSamples", n, envir=globalenv())
|
|
577
|
+ assign("iSample", 0, envir=globalenv())
|
694
|
578
|
}
|
695
|
|
- }
|
696
|
579
|
|
697
|
|
- es <- sapply(1:n, function(j, R, geneSets, alpha) {
|
698
|
|
- if (verbose) {
|
699
|
|
- assign("iSample", get("iSample", envir=globalenv()) + 1, envir=globalenv())
|
700
|
|
- setTxtProgressBar(get("progressBar", envir=globalenv()),
|
701
|
|
- get("iSample", envir=globalenv()) / get("nSamples", envir=globalenv()))
|
702
|
|
- }
|
703
|
|
- geneRanking <- order(R[, j], decreasing=TRUE)
|
704
|
|
- es_sample <- NA
|
705
|
|
- if (parallel.sz == 1 || (is.na(cl) && !haveParallel))
|
706
|
|
- es_sample <- sapply(geneSets, rndWalk, geneRanking, j, R, alpha)
|
707
|
|
- else {
|
708
|
|
- if (is.na(cl))
|
709
|
|
- es_sample <- mclapp(geneSets, rndWalk, geneRanking, j, R, alpha)
|
710
|
|
- else
|
711
|
|
- es_sample <- parSapp(cl, geneSets, rndWalk, geneRanking, j, R, alpha)
|
712
|
|
- }
|
713
|
|
-
|
714
|
|
- unlist(es_sample)
|
715
|
|
- }, R, geneSets, alpha)
|
|
580
|
+ es <- sapply(1:n, function(j, R, geneSets, alpha) {
|
|
581
|
+ if (verbose) {
|
|
582
|
+ assign("iSample", get("iSample", envir=globalenv()) + 1, envir=globalenv())
|
|
583
|
+ setTxtProgressBar(get("progressBar", envir=globalenv()),
|
|
584
|
+ get("iSample", envir=globalenv()) / get("nSamples",
|
|
585
|
+ envir=globalenv()))
|
|
586
|
+ }
|
|
587
|
+ geneRanking <- order(R[, j], decreasing=TRUE)
|
|
588
|
+ bpprogressbar(BPPARAM) <- FALSE ## since progress is reported by sample
|
|
589
|
+ es_sample <- bplapply(geneSets, rndWalk, geneRanking, j, R, alpha,
|
|
590
|
+ BPPARAM=BPPARAM)
|
|
591
|
+
|
|
592
|
+ unlist(es_sample)
|
|
593
|
+ }, R, geneSets, alpha)
|
|
594
|
+
|
|
595
|
+ } else { ## otherwise, parallelization is done throughout samples
|
|
596
|
+
|
|
597
|
+ es <- bplapply(as.list(1:n), function(j, R, geneSets, alpha) {
|
|
598
|
+ geneRanking <- order(R[, j], decreasing=TRUE)
|
|
599
|
+ es_sample <- lapply(geneSets, rndWalk, geneRanking, j, R, alpha)
|
|
600
|
+
|
|
601
|
+ unlist(es_sample)
|
|
602
|
+ }, R, geneSets, alpha, BPPARAM=BPPARAM)
|
|
603
|
+ es <- do.call("cbind", es)
|
|
604
|
+ }
|
716
|
605
|
|
717
|
606
|
if (length(geneSets) == 1)
|
718
|
607
|
es <- matrix(es, nrow=1)
|
...
|
...
|
@@ -729,14 +618,11 @@ ssgsea <- function(X, geneSets, alpha=0.25, parallel.sz,
|
729
|
618
|
rownames(es) <- names(geneSets)
|
730
|
619
|
colnames(es) <- colnames(X)
|
731
|
620
|
|
732
|
|
- if (verbose) {
|
|
621
|
+ if (verbose && length(geneSets) > n) {
|
733
|
622
|
setTxtProgressBar(get("progressBar", envir=globalenv()), 1)
|
734
|
623
|
close(get("progressBar", envir=globalenv()))
|
735
|
624
|
}
|
736
|
625
|
|
737
|
|
- if (!is.na(cl))
|
738
|
|
- stopCl(cl)
|
739
|
|
-
|
740
|
626
|
es
|
741
|
627
|
}
|
742
|
628
|
|
...
|
...
|
@@ -748,62 +634,43 @@ zscore <- function(X, geneSets, parallel.sz, parallel.type,
|
748
|
634
|
p <- nrow(X)
|
749
|
635
|
n <- ncol(X)
|
750
|
636
|
|
751
|
|
- if (verbose) {
|
752
|
|
- assign("progressBar", txtProgressBar(style=3), envir=globalenv()) ## show progress if verbose=TRUE
|
753
|
|
- assign("nSamples", n, envir=globalenv())
|
754
|
|
- assign("iSample", 0, envir=globalenv())
|
755
|
|
- }
|
|
637
|
+ Z <- t(scale(t(X)))
|
756
|
638
|
|
757
|
|
- Z <- t(apply(X, 1, function(x) (x-mean(x))/sd(x)))
|
|
639
|
+ es <- matrix(NA, nrow=length(geneSets), ncol=ncol(X))
|
758
|
640
|
|
759
|
|
- haveParallel <- .isPackageLoaded("parallel")
|
760
|
|
- haveSnow <- .isPackageLoaded("snow")
|
761
|
|
-
|
762
|
|
- cl <- makeCl <- parSapp <- stopCl <- mclapp <- detCor <- nCores <- NA
|
763
|
|
- if (parallel.sz > 1 || haveParallel) {
|
764
|
|
- if (!haveParallel && !haveSnow) {
|
765
|
|
- stop("In order to run calculations in parallel either the 'snow', or the 'parallel' library, should be loaded first")
|
766
|
|
- }
|
767
|
|
-
|
768
|
|
- if (!haveParallel) { ## use snow
|
769
|
|
- ## copying ShortRead's strategy, the calls to the 'get()' are
|
770
|
|
- ## employed to quieten R CMD check, and for no other reason
|
771
|
|
- makeCl <- get("makeCluster", mode="function")
|
772
|
|
- parSapp <- get("parSapply", mode="function")
|
773
|
|
- stopCl <- get("stopCluster", mode="function")
|
774
|
|
-
|
775
|
|
- if (verbose)
|
776
|
|
- cat("Allocating cluster\n")
|
777
|
|
- cl <- makeCl(parallel.sz, type = parallel.type)
|
778
|
|
- } else { ## use parallel
|
779
|
|
-
|
780
|
|
- mclapp <- get('mclapply', envir=getNamespace('parallel'))
|
781
|
|
- detCor <- get('detectCores', envir=getNamespace('parallel'))
|
782
|
|
- nCores <- detCor()
|
783
|
|
- setCores(nCores, parallel.sz)
|
784
|
|
- if (verbose)
|
785
|
|
- cat("Using parallel with", getOption("mc.cores"), "cores\n")
|
|
641
|
+ ## if there are more gene sets than samples, then
|
|
642
|
+ ## parallelization is done throughout gene sets
|
|
643
|
+ if (length(geneSets) > n) {
|
|
644
|
+ if (verbose) {
|
|
645
|
+ assign("progressBar", txtProgressBar(style=3), envir=globalenv())
|
|
646
|
+ assign("nSamples", n, envir=globalenv())
|
|
647
|
+ assign("iSample", 0, envir=globalenv())
|
786
|
648
|
}
|
|
649
|
+
|
|
650
|
+ es <- sapply(1:n, function(j, Z, geneSets) {
|
|
651
|
+ if (verbose) {
|
|
652
|
+ assign("iSample", get("iSample", envir=globalenv()) + 1, envir=globalenv())
|
|
653
|
+ setTxtProgressBar(get("progressBar", envir=globalenv()),
|
|
654
|
+ get("iSample", envir=globalenv()) / get("nSamples",
|
|
655
|
+ envir=globalenv())) }
|
|
656
|
+
|
|
657
|
+ bpprogressbar(BPPARAM) <- FALSE ## since progress is reported by sample
|
|
658
|
+ es_sample <- bplapply(geneSets, combinez, j, Z,
|
|
659
|
+ BPPARAM=BPPARAM)
|
|
660
|
+
|
|
661
|
+ unlist(es_sample)
|
|
662
|
+ }, Z, geneSets)
|
|
663
|
+
|
|
664
|
+ } else { ## otherwise, parallelization is done throughout samples
|
|
665
|
+
|
|
666
|
+ es <- bplapply(as.list(1:n), function(j, Z, geneSets) {
|
|
667
|
+ es_sample <- lapply(geneSets, combinez, j, Z)
|
|
668
|
+
|
|
669
|
+ unlist(es_sample)
|
|
670
|
+ }, Z, geneSets, BPPARAM=BPPARAM)
|
|
671
|
+ es <- do.call("cbind", es)
|
787
|
672
|
}
|
788
|
673
|
|
789
|
|
- es <- sapply(1:n, function(j, Z, geneSets) {
|
790
|
|
- if (verbose) {
|
791
|
|
- assign("iSample", get("iSample", envir=globalenv()) + 1, envir=globalenv())
|
792
|
|
- setTxtProgressBar(get("progressBar", envir=globalenv()),
|
793
|
|
- get("iSample", envir=globalenv()) / get("nSamples", envir=globalenv()))
|
794
|
|
- }
|
795
|
|
- es_sample <- NA
|
796
|
|
- if (parallel.sz == 1 || (is.na(cl) && !haveParallel))
|
797
|
|
- es_sample <- sapply(geneSets, combinez, j, Z)
|
798
|
|
- else {
|
799
|
|
- if (is.na(cl))
|
800
|
|
- es_sample <- mclapp(geneSets, combinez, j, Z)
|
801
|
|
- else
|
802
|
|
- es_sample <- parSapp(cl, geneSets, combinez, j, Z)
|
803
|
|
- }
|
804
|
|
-
|
805
|
|
- unlist(es_sample)
|
806
|
|
- }, Z, geneSets)
|
807
|
674
|
|
808
|
675
|
if (length(geneSets) == 1)
|
809
|
676
|
es <- matrix(es, nrow=1)
|
...
|
...
|
@@ -811,14 +678,11 @@ zscore <- function(X, geneSets, parallel.sz, parallel.type,
|
811
|
678
|
rownames(es) <- names(geneSets)
|
812
|
679
|
colnames(es) <- colnames(X)
|
813
|
680
|
|
814
|
|
- if (verbose) {
|
|
681
|
+ if (verbose && length(geneSets) > n) {
|
815
|
682
|
setTxtProgressBar(get("progressBar", envir=globalenv()), 1)
|
816
|
683
|
close(get("progressBar", envir=globalenv()))
|
817
|
684
|
}
|
818
|
685
|
|
819
|
|
- if (!is.na(cl))
|
820
|
|
- stopCl(cl)
|
821
|
|
-
|
822
|
686
|
es
|
823
|
687
|
}
|
824
|
688
|
|
...
|
...
|
@@ -833,86 +697,12 @@ plage <- function(X, geneSets, parallel.sz, parallel.type,
|
833
|
697
|
p <- nrow(X)
|
834
|
698
|
n <- ncol(X)
|
835
|
699
|
|
836
|
|
- if (verbose) {
|
837
|
|
- assign("progressBar", txtProgressBar(style=3), envir=globalenv()) ## show progress if verbose=TRUE
|
838
|
|
- assign("nGeneSets", length(geneSets), envir=globalenv())
|
839
|
|
- assign("iGeneSet", 0, envir=globalenv())
|
840
|
|
- }
|
|
700
|
+ Z <- t(scale(t(X)))
|
841
|
701
|
|
842
|
|
- Z <- t(apply(X, 1, function(x) (x-mean(x))/sd(x)))
|
|
702
|
+ es <- bplapply(geneSets, rightsingularsvdvectorgset, Z,
|
|
703
|
+ BPPARAM=BPPARAM)
|
843
|
704
|
|
844
|
|
- haveParallel <- .isPackageLoaded("parallel")
|
845
|
|
- haveSnow <- .isPackageLoaded("snow")
|
846
|
|
-
|
847
|
|
- ## the masterDescriptor() calls are disabled since they are not available in windows
|
848
|
|
- ## they would help to report progress by just one of the processors. now all processors
|
849
|
|
- ## will reporting progress. while this might not be the right way to report progress in
|
850
|
|
- ## parallel it should not affect a correct execution and progress should be more or less
|
851
|
|
- ## being reported to some extent.
|
852
|
|
- cl <- makeCl <- parSapp <- stopCl <- mclapp <- detCor <- nCores <- NA ## masterDesc <- NA
|
853
|
|
- if(parallel.sz > 1 || haveParallel) {
|
854
|
|
- if(!haveParallel && !haveSnow) {
|
855
|
|
- stop("In order to run calculations in parallel either the 'snow', or the 'parallel' library, should be loaded first")
|
856
|
|
- }
|
857
|
|
-
|
858
|
|
- if (!haveParallel) { ## use snow
|
859
|
|
- ## copying ShortRead's strategy, the calls to the 'get()' are
|
860
|
|
- ## employed to quieten R CMD check, and for no other reason
|
861
|
|
- makeCl <- get("makeCluster", mode="function")
|
862
|
|
- parSapp <- get("parSapply", mode="function")
|
863
|
|
- stopCl <- get("stopCluster", mode="function")
|
864
|
|
-
|
865
|
|
- if (verbose)
|
866
|
|
- cat("Allocating cluster\n")
|
867
|
|
- cl <- makeCl(parallel.sz, type = parallel.type)
|
868
|
|
- } else { ## use parallel
|
869
|
|
-
|
870
|
|
- mclapp <- get('mclapply', envir=getNamespace('parallel'))
|
871
|
|
- detCor <- get('detectCores', envir=getNamespace('parallel'))
|
872
|
|
- ## masterDesc <- get('masterDescriptor', envir=getNamespace('parallel'))
|
873
|
|
- nCores <- detCor()
|
874
|
|
- setCores(nCores, parallel.sz)
|
875
|
|
- if (verbose)
|
876
|
|
- cat("Using parallel with", getOption("mc.cores"), "cores\n")
|
877
|
|
- }
|
878
|
|
- }
|
879
|
|
-
|
880
|
|
- if (parallel.sz == 1 || (is.na(cl) && !haveParallel))
|
881
|
|
- es <- t(sapply(geneSets, function(gset, Z) {
|
882
|
|
- if (verbose) {
|
883
|
|
- assign("iGeneSet", get("iGeneSet", envir=globalenv()) + 1, envir=globalenv())
|
884
|
|
- setTxtProgressBar(get("progressBar", envir=globalenv()),
|
885
|
|
- get("iGeneSet", envir=globalenv()) / get("nGeneSets", envir=globalenv()))
|
886
|
|
- }
|
887
|
|
- rightsingularsvdvectorgset(gset, Z)
|
888
|
|
- }, Z))
|
889
|
|
- else {
|
890
|
|
- if (is.na(cl)) {
|
891
|
|
- ## firstproc <- mclapp(as.list(1:(options("mc.cores")$mc.cores)), function(x) masterDesc())[[1]]
|
892
|
|
- es <- mclapp(geneSets, function(gset, Z) { ##, firstproc) {
|
893
|
|
- if (verbose) { ## && masterDesc() == firstproc) {
|
894
|
|
- assign("iGeneSet", get("iGeneSet", envir=globalenv()) + 1, envir=globalenv())
|
895
|
|
- setTxtProgressBar(get("progressBar", envir=globalenv()),
|
896
|
|
- get("iGeneSet", envir=globalenv()) / get("nGeneSets", envir=globalenv()))
|
897
|
|
- }
|
898
|
|
- rightsingularsvdvectorgset(gset, Z)
|
899
|
|
- }, Z) ##, firstproc)
|
900
|
|
- es <- do.call(rbind, es)
|
901
|
|
- } else {
|
902
|
|
- if (verbose)
|
903
|
|
- message("Progress reporting for plage with a snow cluster not yet implemented")
|
904
|
|
-
|
905
|
|
- es <- parSapp(geneSets, function(gset, Z) {
|
906
|
|
- if (verbose) {
|
907
|
|
- assign("iGeneSet", get("iGeneSet", envir=globalenv()) + 1, envir=globalenv())
|
908
|
|
- setTxtProgressBar(get("progressBar", envir=globalenv()),
|
909
|
|
- get("iGeneSet", envir=globalenv()) / get("nGeneSets", envir=globalenv()))
|
910
|
|
- }
|
911
|
|
- rightsingularsvdvectorgset(gset, Z)
|
912
|
|
- }, Z)
|
913
|
|
- es <- do.call(rbind, es)
|
914
|
|
- }
|
915
|
|
- }
|
|
705
|
+ es <- do.call(rbind, es)
|
916
|
706
|
|
917
|
707
|
if (length(geneSets) == 1)
|
918
|
708
|
es <- matrix(es, nrow=1)
|
...
|
...
|
@@ -920,14 +710,6 @@ plage <- function(X, geneSets, parallel.sz, parallel.type,
|
920
|
710
|
rownames(es) <- names(geneSets)
|
921
|
711
|
colnames(es) <- colnames(X)
|
922
|
712
|
|
923
|
|
- if (verbose) {
|
924
|
|
- setTxtProgressBar(get("progressBar", envir=globalenv()), 1)
|
925
|
|
- close(get("progressBar", envir=globalenv()))
|
926
|
|
- }
|
927
|
|
-
|
928
|
|
- if (!is.na(cl))
|
929
|
|
- stopCl(cl)
|
930
|
|
-
|
931
|
713
|
es
|
932
|
714
|
}
|
933
|
715
|
|