Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Main release 19 #11

Merged
merged 14 commits into from
May 3, 2024
6 changes: 6 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
^renv$
^renv\.lock$
^.*\.Rproj$
^\.Rproj\.user$
^LICENSE\.md$
Expand All @@ -11,3 +13,7 @@
^_pkgdown\.yml$
^pkgdown$
^\.github$
^.lintr
^renv
^renv.lock
^.editorconfig
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ APL.Rproj
/Meta/
inst/doc
docs

/renv/
renv.lock
.Rprofile
.editorconfig
4 changes: 4 additions & 0 deletions .lintr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
linters: linters_with_defaults(
indentation_linter(indent = 4L),
commented_code_linter = NULL
)
15 changes: 11 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
Package: APL
Type: Package
Title: Association Plots
Version: 1.7.1
Version: 1.9.0
Authors@R:
c(person(given = "Clemens",
family = "Kohl",
role = c("cre", "aut"),
email = "kohl.clemens@gmail.com"),
person(given = "Elzbieta",
family = "Gralinska",
role = c("aut"),
family = "Gralinska", role = c("aut"),
email = "gralinska@molgen.mpg.de"),
person(given = "Martin",
family = "Vingron",
Expand Down Expand Up @@ -50,7 +49,8 @@ Imports:
utils,
org.Hs.eg.db,
org.Mm.eg.db,
rlang
rlang,
Matrix
Depends: R (>= 4.2)
Suggests:
BiocStyle,
Expand All @@ -70,5 +70,12 @@ Collate:
'import_packages.R'
'plot.R'
'utils-pipe.R'
Config/reticulate:
list(
packages = list(
list(package = "numpy"),
list(package = "torch")
)
)
SystemRequirements: python, pytorch, numpy
URL: https://vingronlab.github.io/APL/
55 changes: 45 additions & 10 deletions R/CA.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ NULL
#' grand total of the original matrix "tot"
#' as well as row and column masses "rowm" and "colm" respectively.
#'
comp_std_residuals <- function(mat){
comp_std_residuals <- function(mat) {

if (!is(mat, "matrix")){
if (!is(mat, "matrix") & !is(mat, "dgCMatrix")) {
mat <- as.matrix(mat)
}
stopifnot(
Expand All @@ -31,8 +31,8 @@ comp_std_residuals <- function(mat){

tot <- sum(mat)
P <- mat/tot # proportions matrix
rowm <- rowSums(P) # row masses
colm <- colSums(P) # column masses
rowm <- Matrix::rowSums(P) # row masses
colm <- Matrix::colSums(P) # column masses

E <- rowm %o% colm # expected proportions
S <- (P - E) / sqrt(E) # standardized residuals
Expand All @@ -47,10 +47,10 @@ comp_std_residuals <- function(mat){
#' @param obj A matrix.
#' @return Input matrix with rows & columns consisting of only 0 removed.
rm_zeros <- function(obj){
stopifnot(is(obj, "matrix"))
stopifnot(is(obj, "matrix") | is(obj, "dgCMatrix"))

no_zeros_rows <- rowSums(obj) > 0
no_zeros_cols <- colSums(obj) > 0
no_zeros_rows <- Matrix::rowSums(obj) > 0
no_zeros_cols <- Matrix::colSums(obj) > 0
if (sum(!no_zeros_rows) != 0){
## Delete genes with only zero values across all columns
warning("Matrix contains rows with only 0s. ",
Expand Down Expand Up @@ -218,11 +218,18 @@ run_cacomp <- function(obj,
# S <- (diag(1/sqrt(r)))%*%(P-r%*%t(c))%*%(diag(1/sqrt(c)))
# message("Running singular value decomposition ...")

if (python == TRUE){
if (python == TRUE) {

# python implementation currently can only handle dense matrices of the base class.
if (!is(S, "matrix")) {
S <- as.matrix(S)
}

svd_torch <- NULL
# require(reticulate)
# source_python('./python_svd.py')
reticulate::source_python(system.file("python/python_svd.py", package = "APL"))

SVD <- svd_torch(S)
# SVD <- svd_linalg_torch(S)
names(SVD) <- c("U", "D", "V")
Expand All @@ -244,8 +251,8 @@ run_cacomp <- function(obj,
if(inertia == TRUE){
#calculate inertia
SVD$tot_inertia <- sum(SVD$D^2)
SVD$row_inertia <- rowSums(S^2)
SVD$col_inertia <- colSums(S^2)
SVD$row_inertia <- Matrix::rowSums(S^2)
SVD$col_inertia <- Matrix::colSums(S^2)
}

SVD$row_masses <- rowm
Expand Down Expand Up @@ -393,6 +400,34 @@ setMethod(f = "cacomp",

})

#' @rdname cacomp
#' @export
setMethod(f = "cacomp",
signature=(obj="dgCMatrix"),
function(obj,
coords = TRUE,
princ_coords = 3,
python = FALSE,
dims = NULL,
top = 5000,
inertia = TRUE,
rm_zeros = TRUE,
...){

caobj <- run_cacomp(obj = obj,
coords = coords,
princ_coords = princ_coords,
python = python,
dims = dims,
top = top,
inertia = inertia,
rm_zeros = rm_zeros,
...)

return(caobj)

})


#' Correspondance Analysis for Seurat objects
#'
Expand Down
Loading
Loading