Skip to content

Commit

Permalink
mimic the usual call, cover full=TRUE
Browse files Browse the repository at this point in the history
  • Loading branch information
dimalvovs committed Sep 26, 2024
1 parent d93559c commit 96e91ee
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 19 deletions.
53 changes: 36 additions & 17 deletions R/projectR.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,39 +78,58 @@ setMethod("projectR",signature(data="dgCMatrix",loadings="matrix"),function(
loadings, # a matrix of continous values to be projected with unique rownames
dataNames = NULL, # a vector with names of data rows
loadingsNames = NULL, # a vector with names of loadings rows
NP=NA, # vector of integers indicating which columns of loadings object to use. The default of NP=NA will use entire matrix.
NP=NULL, # vector of integers indicating which columns of loadings object to use. The default of NP=NA will use entire matrix.
full=FALSE # logical indicating whether to return the full model solution. By default only the new pattern object is returned.
){

#columns of dgcMatrix are LHS for stats::lm, and columns of loadings are the
#dense RHS (predictors). sometimes dgcMatrix is too big to fit RAM, so we
#just project in chunks lm models as supported by stats::lm/limma::lmFit
if(!is.null(NP)) {
loadings<-loadings[,NP]
}

#match genes in data sets
if(is.null(dataNames)){
dataNames <- rownames(data)
}
if(is.null(loadingsNames)){
loadingsNames <- rownames(loadings)
}

dataM<-geneMatchR(data1=data, data2=loadings, data1Names=dataNames, data2Names=loadingsNames, merge=FALSE)
print(paste(as.character(dim(dataM[[2]])[1]),'row names matched between data and loadings'))
print(paste('Updated dimension of data:',as.character(paste(dim(dataM[[2]]), collapse = ' '))))

print("dgCMatrix detected, projecting in chunks.")
#columns of dgcMatrix are LHS for stats::lm, and columns of loadings are the
#dense RHS (predictors). sometimes dgcMatrix is too big to fit RAM, so we
#just fit chunks of lm models as supported by stats::lm/limma::lmFit

chop <- function(sparsematrix) {
NCOL <- ncol(sparsematrix)
bins <- seq(1, NCOL, by = 1000)
coln <- ncol(sparsematrix)
bins <- seq(1, coln, by = 1000)
lapply(seq_along(bins), function(i) {
start <- bins[i]
end <- ifelse(i < length(bins), bins[i + 1] - 1, NCOL)
end <- ifelse(i < length(bins), bins[i + 1] - 1, coln)
return(start:end)
})
}
#capture print statement that projectR generated when geneMatchR is called
warns <- invisible(capture.output(
projectionList <- lapply(chop(data), function(i) {
projectR(as.matrix(data[,i]), loadings, dataNames, loadingsNames, NP, full)
#discard print statements projectR generates each time a chunk is called
invisible(capture.output(
projectionList <- lapply(chop(dataM[[2]]), function(i) {
projectR(as.matrix(dataM[[2]][,i]), dataM[[1]], full=full)
})
))

projectionPatterns <- do.call(cbind, projectionList)

if(full==TRUE){
projectionFit <- list('projection'=projectionPatterns, 'pval'=pval.matrix)
return(projectionFit)
if(full==TRUE) {
if(length(projectionList)==1) {#if only one chunk
res <- projectionList[[1]]
} else {
projectionFit <- lapply(projectionList, function(x) do.call(cbind, x))
res <- projectionFit
}
} else {
res <- do.call(cbind, projectionList)
}
else{return(projectionPatterns)}
return(res)
})


Expand Down
18 changes: 16 additions & 2 deletions tests/testthat/test_projectR.R
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,30 @@ test_that("results are correctly formatted for P value mode",{

})

test_that("projection works on sparse data matrix",{
test_that("projection works on sparse data matrix", {
dense <- as.matrix(p.RNAseq6l3c3t)
sparse <- as(p.RNAseq6l3c3t, "sparseMatrix")
loadings <- CR.RNAseq6l3c3t@featureLoadings

expect_true("dgCMatrix" %in% class(sparse))
expect_no_error(projectR(data, loadings))
expect_no_error(projectR(sparse, loadings))

pdense <- projectR(dense, loadings)
psparse <- projectR(sparse, loadings)
expect_identical(projectR(dense, loadings),
projectR(sparse, loadings))
})

test_that("projection works on sparse data matrix with full=TRUE", {
dense <- as.matrix(p.RNAseq6l3c3t)
sparse <- as(p.RNAseq6l3c3t, "sparseMatrix")
loadings <- CR.RNAseq6l3c3t@featureLoadings

expect_true("dgCMatrix" %in% class(sparse))
expect_no_error(projectR(sparse, loadings))

pdense <- projectR(dense, loadings, full=TRUE)
psparse <- projectR(sparse, loadings, full=TRUE)
expect_identical(projectR(dense, loadings),
projectR(sparse, loadings))
})

0 comments on commit 96e91ee

Please sign in to comment.