Skip to content

Instantly share code, notes, and snippets.

@pati-ni
Last active March 13, 2018 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pati-ni/bc43941a831c6a04f05f250b5c51d3f1 to your computer and use it in GitHub Desktop.
Save pati-ni/bc43941a831c6a04f05f250b5c51d3f1 to your computer and use it in GitHub Desktop.
Big Memory Package matrix reference wrap
library('bigmemory')
options(bigmemory.allow.dimnames = TRUE)
# Read SingleCellExperiment object
sce <- readRDS('lake.rds')
normcounts(sce) <- as.big.matrix(normcounts(sce))
process(normcounts(sce))
// [[Rcpp::depends(BH, bigmemory)]]
#include <Rcpp.h>
#include <RcppArmadillo.h>
#include <bigmemory/MatrixAccessor.hpp>
#include <bigmemory/BigMatrix.h>
arma::mat cast_big_matrix(const SEXP& mat)
{
Rcpp::XPtr<BigMatrix> xpMat(mat);
return arma::mat((double*)xpMat->matrix(), xpMat->nrow(), xpMat->ncol(), false);
}
arma::mat process(const SEXP& mat)
{
arma::mat matrix(cast_big_matrix(mat));
// Do processing
return matrix;
}
@pati-ni
Copy link
Author

pati-ni commented Mar 13, 2018

Pros

  • Works with large matrices that can not fit in memory

Cons

  • Have to cast matrix to big matrix at least once.
  • We loose regular R matrix functionality:
   > t<- sce@assays$data$normcounts 
   > t <- as.big.matrix(t) 
   > t == 0
   Error in t == 0 : 
   comparison (1) is possible only for atomic and list types

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment