@@ -185,34 +185,6 @@ symmetrize.pmin.sparse <- function(i, j, x, height) {
185185
186186}
187187
188- # ' Normalise a matrix using L2 norm
189- # '
190- # ' @description
191- # ' Normalise the rows or columns of a matrix using L2 norm
192- # '
193- # ' @param mat a matrix (sparse or dense)
194- # ' @param MARGIN one of 1 or 2, corresponding to normalisation per rows and
195- # ' columns respectively
196- # '
197- # ' @return the matrix with normalised rows or columns
198- # '
199- # ' @importFrom rlang abort
200- # ' @importFrom Matrix rowSums colSums
201- # ' @note
202- # ' Adapted from \href{https://github.com/satijalab/seurat/blob/1549dcb3075eaeac01c925c4b4bb73c73450fc50/R/utilities.R#L1941-L1956}{Seurat:::L2Norm}
203- # ' @export
204-
205- NormaliseL2 <- function (mat , MARGIN = 1 ) {
206- marSums <- switch (MARGIN ,
207- `1` = rowSums ,
208- `2` = colSums ,
209- NULL
210- ) %|| % abort(paste(sQuote(MARGIN ), " is not a correct value for 'MARGIN'." ,
211- " Possible values are 1 (rows) or 2 (columns)" ))
212- mat.norm <- sweep(mat , MARGIN = MARGIN , STATS = sqrt(marSums(mat ^ 2 )), FUN = `/` )
213- mat.norm [! is.finite(mat.norm )] <- 0
214- return (mat.norm )
215- }
216188# ###############################################################################
217189# ############################### NN cut ################################
218190# ' Remove excessive number of neighbours in a knn graph
@@ -364,7 +336,7 @@ setMethod(".cut.knn", "Neighbor",
364336 return (object )
365337 })
366338
367- # ' Very similar to .cut.knn.Matrix, but doesn't contruct the trimmed knn network
339+ # ' Very similar to .cut.knn.Matrix, but doesn't construct the trimmed knn network
368340# ' @description
369341# ' Requires that all cells have at least k.max neighbors
370342# '
@@ -554,7 +526,7 @@ setMethod("GetNeighborsPerBatch", c("Seurat", "data.frame"),
554526# ' @usage NULL
555527setMethod ("GetNeighborsPerBatch ", "Graph",
556528 function (object , batch.var , count.self ) {
557- knnmat <- as.dgcmatrix(object > 0 )
529+ knnmat <- as.dgcmatrix(as( object > 0 , ' dsparseMatrix ' ) )
558530 GetNeighborsPerBatch(object = knnmat , batch.var = batch.var ,
559531 count.self = count.self )
560532})
@@ -753,17 +725,48 @@ n_zeros_mat <- function(mat) {
753725# ' @noRd
754726choose_matrix_format <- function (mat ) {
755727 max_int_32bit <- 2 ^ 31 - 1
756- if (all(c(ncol(mat ), nrow(mat )) < = max_int_32bit )) { # should always be TRUE
728+ if (all(c(ncol(mat ), nrow(mat )) < = max_int_32bit ) & length( mat ) > 0 ) { # should always be TRUE
757729 n_0s <- n_zeros_mat(mat = mat )
758- if ( (n_0s > max_int_32bit ) | (n_0s / length(mat ) < .35 ) ) {
730+ n_not0s <- length(mat ) - n_0s
731+ if ( (n_not0s > max_int_32bit ) | (n_0s / length(mat ) < .35 ) ) {
759732 mat <- as.matrix(mat )
760733 } else {
761- as.dgcmatrix(mat )
734+ mat <- as.dgcmatrix(mat )
762735 }
763736 }
764737 return (mat )
765738}
766739
740+ # ###############################################################################
741+ # ########################## Seurat extensions ############################
742+ # ' Normalise a matrix using L2 norm
743+ # '
744+ # ' @description
745+ # ' Normalise the rows or columns of a matrix using L2 norm
746+ # '
747+ # ' @param mat a matrix (sparse or dense)
748+ # ' @param MARGIN one of 1 or 2, corresponding to normalisation per rows and
749+ # ' columns respectively
750+ # '
751+ # ' @return the matrix with normalised rows or columns
752+ # '
753+ # ' @importFrom rlang abort
754+ # ' @importFrom Matrix rowSums colSums
755+ # ' @note
756+ # ' Adapted from \href{https://github.com/satijalab/seurat/blob/1549dcb3075eaeac01c925c4b4bb73c73450fc50/R/utilities.R#L1941-L1956}{Seurat:::L2Norm}
757+ # ' @export
758+
759+ NormaliseL2 <- function (mat , MARGIN = 1 ) {
760+ marSums <- switch (MARGIN ,
761+ `1` = rowSums ,
762+ `2` = colSums ,
763+ NULL
764+ ) %|| % abort(paste(sQuote(MARGIN ), " is not a correct value for 'MARGIN'." ,
765+ " Possible values are 1 (rows) or 2 (columns)" ))
766+ mat.norm <- sweep(mat , MARGIN = MARGIN , STATS = sqrt(marSums(mat ^ 2 )), FUN = `/` )
767+ mat.norm [! is.finite(mat.norm )] <- 0
768+ return (mat.norm )
769+ }
767770
768771# Creates data.frame with cell group assignments for integration
769772# uses SCT models if SCTAssay and layers otherwise
@@ -801,3 +804,8 @@ CreateIntegrationGroups <- function(object, layers, scale.layer) {
801804 return (groups )
802805}
803806# ###########
807+
808+ # ' @method as.Graph Graph
809+ as.Graph.Graph <- function (x , ... ) {
810+ return (x )
811+ }
0 commit comments