Skip to content

Commit

Permalink
add rowRanks and colRanks
Browse files Browse the repository at this point in the history
  • Loading branch information
Yunuuuu committed Jan 27, 2024
1 parent 0acfb51 commit 7454cfa
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 53 deletions.
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ exportMethods(bindROWS)
exportMethods(cbind)
exportMethods(cbind2)
exportMethods(colMeans)
exportMethods(colRanks)
exportMethods(colSds)
exportMethods(colSums)
exportMethods(colVars)
Expand All @@ -83,6 +84,7 @@ exportMethods(rbind)
exportMethods(rbind2)
exportMethods(round)
exportMethods(rowMeans)
exportMethods(rowRanks)
exportMethods(rowSds)
exportMethods(rowSums)
exportMethods(rowVars)
Expand Down Expand Up @@ -118,8 +120,10 @@ importFrom(DelayedArray,rbind)
importFrom(DelayedArray,seed)
importFrom(DelayedArray,type)
importFrom(Matrix,crossprod)
importFrom(MatrixGenerics,colRanks)
importFrom(MatrixGenerics,colSds)
importFrom(MatrixGenerics,colVars)
importFrom(MatrixGenerics,rowRanks)
importFrom(MatrixGenerics,rowSds)
importFrom(MatrixGenerics,rowVars)
importFrom(S4Vectors,bindCOLS)
Expand Down
46 changes: 42 additions & 4 deletions R/Class-RankTransform.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ NULL

methods::setClass("BPCellsRankTransformSeed",
contains = c(
"BPCellsUnaryOpsSeed",
"BPCellsUnaryOpsSeed",
get_class("MatrixRankTransform")
)
)
Expand All @@ -43,7 +43,8 @@ methods::setMethod("BPCellsSeed", "MatrixRankTransform", function(x) {
##################### BPCellsRankTransformMatrix #######################
#' Rank-transform a BPCells IterableMatrix matrix
#'
#' @param object A `BPCellsSeed` or `BPCellsMatrix` object.
#' @param x,object A [BPCellsSeed][BPCellsSeed-class] or
#' [BPCellsMatrix][BPCellsMatrix-class] object.
#' @inheritDotParams BPCells::transpose_storage_order -matrix
#' @export
#' @name rank_transform
Expand All @@ -58,7 +59,8 @@ methods::setGeneric(
#' If `axis` value specified is different from the storage order of
#' `object`, [transpose_storage_order][BPCells::transpose_storage_order]
#' will be used to transpose the underlying storage order.
#' @return A [BPCellsSeed][BPCellsSeed-class] or
#' @return
#' - `rank_transform`: A [BPCellsSeed][BPCellsSeed-class] or
#' [BPCellsMatrix][BPCellsMatrix-class] object depends on the class of `object`.
#' @seealso [rank_transform][BPCells::rank_transform]
#' @importFrom DelayedArray DelayedArray
Expand All @@ -80,7 +82,7 @@ methods::setMethod(
object <- BPCells::transpose_storage_order(matrix = object, ...)
}
}
BPCells:::rank_transform(mat = object, axis = axis)
BPCellsSeed(BPCells:::rank_transform(mat = object, axis = axis))
}
)

Expand All @@ -101,3 +103,39 @@ methods::setMethod("rank_transform", "ANY", function(object, axis) {
"{.arg object} must be a {.cls BPCellsSeed} or {.cls BPCellsMatrix} object"
)
})

#######################################################################
# Rank
#' @importFrom MatrixGenerics rowRanks
#' @return
#' - `rowRanks()`: vector of row ranks.
#' @aliases rowRanks
#' @export
#' @rdname rank_transform
methods::setMethod("rowRanks", c(x = "BPCellsSeed"), function(x) {
rank_transform(x, axis = "row")
})

#' @importFrom MatrixGenerics colRanks
#' @return
#' - `colRanks()`: vector of column ranks.
#' @aliases colRanks
#' @export
#' @rdname rank_transform
methods::setMethod("colRanks", c(x = "BPCellsSeed"), function(x) {
rank_transform(x, axis = "col")
})

#' @importFrom MatrixGenerics rowRanks
#' @export
#' @rdname rank_transform
methods::setMethod("rowRanks", c(x = "BPCellsMatrix"), function(x) {
rank_transform(x, axis = "row")
})

#' @importFrom MatrixGenerics colRanks
#' @export
#' @rdname rank_transform
methods::setMethod("colRanks", c(x = "BPCellsMatrix"), function(x) {
rank_transform(x, axis = "col")
})
1 change: 1 addition & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ knitr::opts_chunk$set(
# BPCells backend for DelayedArray objects

<!-- badges: start -->
[![R-CMD-check](https://github.com/Yunuuuu/BPCellsArray/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/Yunuuuu/BPCellsArray/actions/workflows/R-CMD-check.yaml)
<!-- badges: end -->

```{r child = "vignettes/BPCellsArray.Rmd"}
Expand Down
51 changes: 26 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

<!-- badges: start -->

[![R-CMD-check](https://github.com/Yunuuuu/BPCellsArray/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/Yunuuuu/BPCellsArray/actions/workflows/R-CMD-check.yaml)
<!-- badges: end -->

BPCells is a package for high performance single cell analysis on
Expand All @@ -18,27 +19,27 @@ been optimized by c++ or c.

Here is a summarized delayed operations in BPCells:

| Operations | BPCells | BPCellsArray |
| ---------------------------------------- | --------------------------- | ---------------------------- |
| Combine by row | rbind2 | rbind2,rbind,arbind,bindROWS |
| Combine by column | cbind2 | cbind2,cbind,acbind,bindCOLS |
| transpose matrix | t | t |
| subset | `[` | `[` |
| Rename | `dimnames<-` | `dimnames<-` |
| Multiplication | `%*%` | `%*%` |
| Crossproduct | | crossprod |
| Arithmetic | `+`,`-`,`*`,`/` | `+`,`-`,`*`,`/` |
| Relational Operators | Binary (`<`,`>`,`<=`, `>=`) | Binary (`<`,`>`,`<=`, `>=`) |
| Storage mode | convert\_matrix\_type | convert\_type |
| Rank-transform | rank\_transform | rank\_transform |
| Mask matrix entries to zero | mask\_matrix | mask\_matrix |
| Take minumum with a global constant | min\_scalar | pmin\_scalar |
| Take the minimum with a per-col constant | min\_by\_col | pmin\_by\_col |
| Take the minimum with a per-row constant | min\_by\_row | pmin\_by\_row |
| Round number | round | round |
| `exp(x) - 1` | `expm1_slow`,`expm1` | `expm1_slow`,`expm1` |
| log(1+x) | `log1p`,`log1p_slow` | `log1p`,`log1p_slow` |
| Power | `pow_slow`,`^` | `pow_slow`,`^` |
| Operations | BPCells | BPCellsArray |
| ---------------------------------------- | --------------------------- | -------------------------------------- |
| Combine by row | rbind2 | rbind2,rbind,arbind,bindROWS |
| Combine by column | cbind2 | cbind2,cbind,acbind,bindCOLS |
| transpose matrix | t | t |
| subset | `[` | `[` |
| Rename | `dimnames<-` | `dimnames<-` |
| Multiplication | `%*%` | `%*%` |
| Crossproduct | | crossprod |
| Arithmetic | `+`,`-`,`*`,`/` | `+`,`-`,`*`,`/` |
| Relational Operators | Binary (`<`,`>`,`<=`, `>=`) | Binary (`<`,`>`,`<=`, `>=`) |
| Storage mode | convert\_matrix\_type | convert\_type |
| Rank-transform | rank\_transform | `rank_transform`,`rowRanks`,`colRanks` |
| Mask matrix entries to zero | mask\_matrix | mask\_matrix |
| Take minumum with a global constant | min\_scalar | pmin\_scalar |
| Take the minimum with a per-col constant | min\_by\_col | pmin\_by\_col |
| Take the minimum with a per-row constant | min\_by\_row | pmin\_by\_row |
| Round number | round | round |
| `exp(x) - 1` | `expm1_slow`,`expm1` | `expm1_slow`,`expm1` |
| `log(1+x)` | `log1p`,`log1p_slow` | `log1p`,`log1p_slow` |
| Power | `pow_slow`,`^` | `pow_slow`,`^` |

Other non-lazied operations:

Expand Down Expand Up @@ -86,7 +87,7 @@ library(SingleCellExperiment)
#> Attaching package: 'matrixStats'
#> The following objects are masked from 'package:BPCellsArray':
#>
#> colSds, colVars, rowSds, rowVars
#> colRanks, colSds, colVars, rowRanks, rowSds, rowVars
#>
#> Attaching package: 'MatrixGenerics'
#> The following objects are masked from 'package:matrixStats':
Expand Down Expand Up @@ -194,7 +195,7 @@ assay(sce, "counts")
#> Storage order: column major
#>
#> Queued Operations:
#> 1. Load compressed matrix from directory /tmp/RtmpmXAZgW/BPCells33ed53291e4b5a
#> 1. Load compressed matrix from directory /tmp/RtmpF5CN2k/BPCells37494a32e8ae23
```

If you do delayed operations with this assay, the class may be changed,
Expand All @@ -214,7 +215,7 @@ assay(sce, "counts")[1:10, 1:10]
#> Storage order: column major
#>
#> Queued Operations:
#> 1. Load compressed matrix from directory /tmp/RtmpmXAZgW/BPCells33ed53291e4b5a
#> 1. Load compressed matrix from directory /tmp/RtmpF5CN2k/BPCells37494a32e8ae23
#> 2. Select rows: 1, 2 ... 10 and cols: 1, 2 ... 10
as.matrix(assay(sce, "counts")[1:10, 1:10])
#> Cell_001 Cell_002 Cell_003 Cell_004 Cell_005 Cell_006 Cell_007
Expand Down Expand Up @@ -279,7 +280,7 @@ assay(sce, "logcounts")
#> Storage order: column major
#>
#> Queued Operations:
#> 1. Load compressed matrix from directory /tmp/RtmpmXAZgW/BPCells33ed53291e4b5a
#> 1. Load compressed matrix from directory /tmp/RtmpF5CN2k/BPCells37494a32e8ae23
#> 2. Scale columns by 0.984, 1.05 ... 1
#> 3. Transform log1p
#> 4. Scale by 1.44
Expand Down
31 changes: 28 additions & 3 deletions man/rank_transform.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 21 additions & 21 deletions vignettes/BPCellsArray.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,27 @@ object or written to disk. And most operations have been optimized by c++ or c.

Here is a summarized delayed operations in BPCells:

| Operations | BPCells | BPCellsArray |
| ---------------------------------------- | ---------------------------- | ---------------------------- |
| Combine by row | rbind2 | rbind2,rbind,arbind,bindROWS |
| Combine by column | cbind2 | cbind2,cbind,acbind,bindCOLS |
| transpose matrix | t | t |
| subset | `[` | `[` |
| Rename | `dimnames<-` | `dimnames<-` |
| Multiplication | `%*%` | `%*%` |
| Crossproduct | | crossprod |
| Arithmetic | `+`,`-`,`*`,`/` | `+`,`-`,`*`,`/` |
| Relational Operators | Binary (`<`,`>`,`<=`, `>=`) | Binary (`<`,`>`,`<=`, `>=`) |
| Storage mode | convert_matrix_type | convert_type |
| Rank-transform | rank_transform | rank_transform |
| Mask matrix entries to zero | mask_matrix | mask_matrix |
| Take minumum with a global constant | min_scalar | pmin_scalar |
| Take the minimum with a per-col constant | min_by_col | pmin_by_col |
| Take the minimum with a per-row constant | min_by_row | pmin_by_row |
| Round number | round | round |
| `exp(x) - 1` | `expm1_slow`,`expm1` | `expm1_slow`,`expm1` |
| log(1+x) | `log1p`,`log1p_slow` | `log1p`,`log1p_slow` |
| Power | `pow_slow`,`^` | `pow_slow`,`^` |
| Operations | BPCells | BPCellsArray |
| ---------------------------------------- | ---------------------------- | -------------------------------------- |
| Combine by row | rbind2 | rbind2,rbind,arbind,bindROWS |
| Combine by column | cbind2 | cbind2,cbind,acbind,bindCOLS |
| transpose matrix | t | t |
| subset | `[` | `[` |
| Rename | `dimnames<-` | `dimnames<-` |
| Multiplication | `%*%` | `%*%` |
| Crossproduct | | crossprod |
| Arithmetic | `+`,`-`,`*`,`/` | `+`,`-`,`*`,`/` |
| Relational Operators | Binary (`<`,`>`,`<=`, `>=`) | Binary (`<`,`>`,`<=`, `>=`) |
| Storage mode | convert_matrix_type | convert_type |
| Rank-transform | rank_transform | `rank_transform`,`rowRanks`,`colRanks` |
| Mask matrix entries to zero | mask_matrix | mask_matrix |
| Take minumum with a global constant | min_scalar | pmin_scalar |
| Take the minimum with a per-col constant | min_by_col | pmin_by_col |
| Take the minimum with a per-row constant | min_by_row | pmin_by_row |
| Round number | round | round |
| `exp(x) - 1` | `expm1_slow`,`expm1` | `expm1_slow`,`expm1` |
| `log(1+x)` | `log1p`,`log1p_slow` | `log1p`,`log1p_slow` |
| Power | `pow_slow`,`^` | `pow_slow`,`^` |


Other non-lazied operations:
Expand Down

0 comments on commit 7454cfa

Please sign in to comment.