Browse code

Merge pull request #348 from yuan-yin-truly/devel

Speed up sparse matrix value assignment

Joshua D. Campbell authored on 01/10/2021 21:02:16 • GitHub committed on 01/10/2021 21:02:16
Showing 3 changed files

... ...
@@ -30,7 +30,7 @@ LinkingTo: Rcpp, RcppEigen
30 30
 License: MIT + file LICENSE
31 31
 Encoding: UTF-8
32 32
 LazyData: true
33
-RoxygenNote: 7.1.1
33
+RoxygenNote: 7.1.2
34 34
 BugReports: https://github.com/campbio/celda/issues
35 35
 biocViews: SingleCell, GeneExpression, Clustering, Sequencing, Bayesian
36 36
 NeedsCompilation: yes
... ...
@@ -495,7 +495,21 @@ setMethod(
495 495
       z = as.integer(res$z),
496 496
       pseudocount = 1e-20
497 497
     )
498
-    estRmat[seq(nrow(counts)), which(batch == bat)] <- estRmat.temp
498
+
499
+    # Speed up sparse matrix value assignment by cbind -> order recovery
500
+    allCol <- paste0("col_", seq_len(ncol(estRmat)))
501
+    colnames(estRmat) <- allCol
502
+
503
+    subCol <- paste0("col_", which(batch == bat))
504
+    colnames(estRmat.temp) <- subCol
505
+
506
+    estRmat <- estRmat[, !(allCol %in% subCol)]
507
+    estRmat <- cbind(estRmat, estRmat.temp)
508
+
509
+    # Recover order
510
+    estRmat <- estRmat[, allCol]
511
+
512
+    ##estRmat[seq(nrow(counts)), which(batch == bat)] <- estRmat.temp
499 513
     dimnames(estRmat) <- list(geneNames, allCellNames)
500 514
 
501 515
     resBatch[[bat]] <- list(
... ...
@@ -6,6 +6,11 @@
6 6
 
7 7
 using namespace Rcpp;
8 8
 
9
+#ifdef RCPP_USE_GLOBAL_ROSTREAM
10
+Rcpp::Rostream<true>&  Rcpp::Rcout = Rcpp::Rcpp_cout_get();
11
+Rcpp::Rostream<false>& Rcpp::Rcerr = Rcpp::Rcpp_cerr_get();
12
+#endif
13
+
9 14
 // decontXEM
10 15
 Rcpp::List decontXEM(const Eigen::MappedSparseMatrix<double>& counts, const NumericVector& counts_colsums, const NumericVector& theta, const bool& estimate_eta, const NumericMatrix& eta, const NumericMatrix& phi, const IntegerVector& z, const bool& estimate_delta, const NumericVector& delta, const double& pseudocount);
11 16
 RcppExport SEXP _celda_decontXEM(SEXP countsSEXP, SEXP counts_colsumsSEXP, SEXP thetaSEXP, SEXP estimate_etaSEXP, SEXP etaSEXP, SEXP phiSEXP, SEXP zSEXP, SEXP estimate_deltaSEXP, SEXP deltaSEXP, SEXP pseudocountSEXP) {