Browse code

Speed up sparse matrix assignmnet

Yuan authored on 01/10/2021 04:19:10
Showing 2 changed files

... ...
@@ -495,7 +495,22 @@ 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
+    
501
+    allCol <- paste0("col_", 1:ncol(estRmat))
502
+    colnames(estRmat) <- allCol
503
+    
504
+    subCol <- paste0("col_", which(batch == bat))
505
+    colnames(estRmat.temp) <- subCol
506
+    
507
+    estRmat <- estRmat[, !(allCol %in% subCol)]
508
+    estRmat <- cbind(estRmat, estRmat.temp)
509
+    
510
+    # Recover order
511
+    estRmat <- estRmat[, allCol]
512
+    
513
+    #(Old method) estRmat[seq(nrow(counts)), which(batch == bat)] <- estRmat.temp
499 514
     dimnames(estRmat) <- list(geneNames, allCellNames)
500 515
 
501 516
     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) {