Skip to content

Commit

Permalink
Dev 200402 (GreenleafLab#72)
Browse files Browse the repository at this point in the history
* bug fix mat column order

* force names for getting matrices

* force matching cell names

* fix

* bug fix name order
  • Loading branch information
jgranja24 authored Apr 3, 2020
1 parent 4b50263 commit e1b8cb6
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 32 deletions.
Binary file modified .DS_Store
Binary file not shown.
113 changes: 83 additions & 30 deletions R/ArrowRead.R
Original file line number Diff line number Diff line change
Expand Up @@ -171,23 +171,37 @@ getMatrixFromProject <- function(

ArrowFiles <- getArrowFiles(ArchRProj)

cellNames <- ArchRProj$cellNames


seL <- ArchR:::.safelapply(seq_along(ArrowFiles), function(x){

.messageDiffTime(paste0("Reading ", useMatrix," : ", names(ArrowFiles)[x], "(",x," of ",length(ArrowFiles),")"), tstart, verbose = verbose)

o <- getMatrixFromArrow(
ArrowFile = ArrowFiles[x],
useMatrix = useMatrix,
useSeqnames = useSeqnames,
cellNames = ArchRProj$cellNames,
ArchRProj = ArchRProj,
verbose = verbose,
binarize = binarize
)
allCells <- .availableCells(ArrowFile = ArrowFiles[x], subGroup = useMatrix)
allCells <- allCells[allCells %in% cellNames]

if(length(allCells) != 0){

o <- getMatrixFromArrow(
ArrowFile = ArrowFiles[x],
useMatrix = useMatrix,
useSeqnames = useSeqnames,
cellNames = allCells,
ArchRProj = ArchRProj,
verbose = verbose,
binarize = binarize
)

.messageDiffTime(paste0("Completed ", useMatrix," : ", names(ArrowFiles)[x], "(",x," of ",length(ArrowFiles),")"), tstart, verbose = verbose)

.messageDiffTime(paste0("Completed ", useMatrix," : ", names(ArrowFiles)[x], "(",x," of ",length(ArrowFiles),")"), tstart, verbose = verbose)
o

}else{

o
NULL

}

}, threads = threads)

Expand Down Expand Up @@ -281,6 +295,14 @@ getMatrixFromArrow <- function(
featureDF <- featureDF[BiocGenerics::which(featureDF$seqnames %bcin% seqnames), ]

.messageDiffTime(paste0("Getting ",useMatrix," from ArrowFile : ", basename(ArrowFile)), tstart)

if(!is.null(cellNames)){
allCells <- .availableCells(ArrowFile = ArrowFile, subGroup = useMatrix)
if(!all(cellNames %in% allCells)){
stop("cellNames must all be within the ArrowFile!!!!")
}
}

mat <- .getMatFromArrow(
ArrowFile = ArrowFile,
featureDF = featureDF,
Expand Down Expand Up @@ -437,11 +459,14 @@ getMatrixFromArrow <- function(
mat <- mat[rownames(featureDF), , drop = FALSE]
rownames(mat) <- NULL

if(!is.null(cellNames)){
mat <- mat[,cellNames,drop=FALSE]
}

return(mat)

}


####################################################################
# Helper read functioning
####################################################################
Expand Down Expand Up @@ -471,6 +496,16 @@ getMatrixFromArrow <- function(
rownames(featureDF) <- paste0("f", seq_len(nrow(featureDF)))
cellNames <- unlist(groupList, use.names = FALSE) ### UNIQUE here? doublet check JJJ

allCellsList <- lapply(seq_along(ArrowFiles), function(x){
allCells <- .availableCells(ArrowFile = ArrowFiles[x], subGroup = useMatrix)
allCells <- allCells[allCells %in% cellNames]
if(length(allCells) != 0){
allCells
}else{
NULL
}
})

mat <- .safelapply(seq_along(seqnames), function(x){

.messageDiffTime(sprintf("Constructing Group Matrix %s of %s", x, length(seqnames)), tstart, verbose = verbose)
Expand All @@ -484,29 +519,36 @@ getMatrixFromArrow <- function(
rownames(matChr) <- rownames(featureDFx)

for(y in seq_along(ArrowFiles)){

maty <- .getMatFromArrow(
ArrowFile = ArrowFiles[y],
useMatrix = useMatrix,
featureDF = featureDFx,
cellNames = cellNames,
useIndex = useIndex
)

for(z in seq_along(groupList)){
allCells <- allCellsList[[y]]

if(!is.null(allCells)){

maty <- .getMatFromArrow(
ArrowFile = ArrowFiles[y],
useMatrix = useMatrix,
featureDF = featureDFx,
cellNames = allCells,
useIndex = useIndex
)

#Check Cells In Group
cellsGroupz <- groupList[[z]]
idx <- BiocGenerics::which(colnames(maty) %in% cellsGroupz)
for(z in seq_along(groupList)){

#Check Cells In Group
cellsGroupz <- groupList[[z]]
idx <- BiocGenerics::which(colnames(maty) %in% cellsGroupz)

#If In Group RowSums
if(length(idx) > 0){
matChr[,z] <- matChr[,z] + Matrix::rowSums(maty[,idx,drop=FALSE])
}

#If In Group RowSums
if(length(idx) > 0){
matChr[,z] <- matChr[,z] + Matrix::rowSums(maty[,idx,drop=FALSE])
}

}
rm(maty)

rm(maty)
}


if(y %% 20 == 0 | y %% length(ArrowFiles) == 0){
gc()
Expand Down Expand Up @@ -564,11 +606,22 @@ getMatrixFromArrow <- function(

.messageDiffTime(sprintf("Getting Partial Matrix %s of %s", x, length(ArrowFiles)), tstart, verbose = verbose)

allCells <- .availableCells(ArrowFile = ArrowFiles[x], subGroup = useMatrix)
allCells <- allCells[allCells %in% cellNames]

if(length(allCells) == 0){
if(doSampleCells){
return(list(mat = NULL, out = NULL))
}else{
return(NULL)
}
}

o <- h5closeAll()
matx <- .getMatFromArrow(
ArrowFile = ArrowFiles[x],
featureDF = featureDF,
cellNames = cellNames,
cellNames = allCells,
useMatrix = useMatrix,
useIndex = useIndex
)
Expand Down
3 changes: 1 addition & 2 deletions R/VisualizeData.R
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ plotGroups <- function(

if(!inherits(values, "matrix")){
values <- matrix(as.matrix(values), ncol = nCells(ArchRProj))
colnames(values) <- unlist(cellNamesList)
colnames(values) <- ArchRProj$cellNames
}

#Values Summary
Expand All @@ -570,7 +570,6 @@ plotGroups <- function(

}


.fixPlotSize <- function(
p = NULL,
plotWidth = unit(6, "in"),
Expand Down

0 comments on commit e1b8cb6

Please sign in to comment.