Skip to content

Commit

Permalink
Add examples again: trying to make BiocCheck work
Browse files Browse the repository at this point in the history
  • Loading branch information
kollo97 committed Nov 21, 2024
1 parent 7a2ccc4 commit e81deaf
Show file tree
Hide file tree
Showing 29 changed files with 628 additions and 51 deletions.
Binary file added .DS_Store
Binary file not shown.
Binary file added ._.DS_Store
Binary file not shown.
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: anglemania
Title: Feature Extraction for scRNA-seq Dataset Integration
Version: 0.0.0.9000
Version: 0.99.0
Description: anglemania extracts genes from multi-batch scRNA-seq experiments for downstream dataset integration. It improves conventional usage of highly-variable genes for integration tasks.
Authors@R: c(
person("Aaron", "Kollotzek", email = "[email protected]", role = c("aut", "cre"),
Expand All @@ -16,6 +16,7 @@ Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
Imports:
Matrix,
stats,
checkmate,
Seurat,
SeuratObject,
Expand All @@ -31,6 +32,6 @@ Suggests:
covr,
DT
VignetteBuilder: knitr
biocViews: SingleCell, BatchEffect, MultipleComparison
biocViews: SingleCell, BatchEffect, MultipleComparison, FeatureExtraction
BiocType: Software
Config/testthat/edition: 3
5 changes: 1 addition & 4 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
# Generated by roxygen2: do not edit by hand

export("%>%")
export("angl_weights<-")
export("intersect_genes<-")
export("list_stats<-")
export("matrix_list<-")
export(add_unique_batch_key)
export(angl_weights)
export(anglemania)
Expand Down Expand Up @@ -58,4 +54,5 @@ importFrom(dplyr,select)
importFrom(magrittr,"%>%")
importFrom(pbapply,pblapply)
importFrom(pbapply,pboptions)
importFrom(stats,quantile)
importFrom(tidyr,unite)
15 changes: 11 additions & 4 deletions R/anglemania.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,16 @@
#' \url{https://arxiv.org/abs/1306.0256}
#'
#' @examples
#' \dontrun{
#' \donttest{
#' load(system.file(
#' "extdata",
#' "seurat_splatter_sim.RData",
#' package = "anglemania"))
#'
#' # Assuming you have an anglemania_object already created
#' angl <- create_anglemaniaObject(se,
#' batch_key = batch_key,
#' min_cells_per_gene = 1
#' )
#'
#' angl <- anglemania(
#' angl,
Expand All @@ -67,9 +74,9 @@
#' )
#'
#' # Access the selected genes
#' selected_genes <- extract_integration_genes(angl)
#' selected_genes <- get_anglemania_genes(angl)
#' }
#'
#' selected_genes[1:10]
#' @export
anglemania <- function(
anglemania_object,
Expand Down
63 changes: 60 additions & 3 deletions R/anglemanise_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
#' @return An \code{\link[bigstatsr]{FBM}} object from the \pkg{bigstatsr}
#' package.
#' @importFrom bigstatsr FBM
#' @examples
#' s_mat <- Matrix::rsparsematrix(nrow = 10, ncol = 5, density = 0.3)
#' # Convert the sparse matrix to an FBM using your function
#' fbm_mat <- sparse_to_fbm(s_mat)
#' fbm_mat
#' @export
sparse_to_fbm <- function(s_mat) {
n <- nrow(s_mat)
Expand Down Expand Up @@ -41,6 +46,13 @@ sparse_to_fbm <- function(s_mat) {
#' @return A list with statistical measures including \code{mean}, \code{sd},
#' \code{var}, \code{sn}, \code{min}, and \code{max}.
#' @importFrom bigstatsr big_apply
#' @examples
#' s_mat <- Matrix::rsparsematrix(nrow = 10, ncol = 5, density = 0.3)
#' # Convert the sparse matrix to an FBM using your function
#' fbm_mat <- sparse_to_fbm(s_mat)
#' result <- get_dstat(fbm_mat)
#' str(result)
#' result
#' @seealso \code{\link[bigstatsr]{big_apply}}, \code{\link[bigstatsr]{FBM}}
#' @export
get_dstat <- function(corr_matrix) {
Expand Down Expand Up @@ -129,6 +141,29 @@ get_dstat <- function(corr_matrix) {
#' In this case, the FBMs are the angle matrices computed in \code{factorise}.
#' @return A new \code{\link[bigstatsr]{FBM}} object containing the mean values.
#' @importFrom bigstatsr FBM
#' @examples
#' \donttest{
#' # Create FBMs
#' mat1 <- matrix(1:9, nrow = 3)
#' mat2 <- matrix(1:3, nrow = 3)
#'
#' fbm1 <- bigstatsr::FBM(nrow = nrow(mat1), ncol = ncol(mat1), init = mat1)
#' fbm2 <- bigstatsr::FBM(nrow = nrow(mat2), ncol = ncol(mat2), init = mat2)
#'
#' # Create weights
#' weights <- c(batch1 = 0.5, batch2 = 0.5)
#'
#' # Create the list of FBMs
#' fbm_list <- list(batch1 = fbm1, batch2 = fbm2)
#'
#' # Construct the anglemaniaObject
#' anglemania_object <- new(
#' "anglemaniaObject",
#' weights = weights,
#' matrix_list = fbm_list
#' )
#' big_mat_list_mean(anglemania_object)
#' }
#' @export
big_mat_list_mean <- function(anglemania_object) {
if (!inherits(anglemania_object, "anglemaniaObject")) {
Expand Down Expand Up @@ -186,6 +221,18 @@ big_mat_list_mean <- function(anglemania_object) {
#' @return A list containing three matrices: \code{mean_zscore},
#' \code{sds_zscore}, and \code{sn_zscore}.
#' @importFrom bigstatsr FBM big_apply
#' @examples
#' \donttest{
#' load(system.file(
#' "extdata",
#' "seurat_splatter_sim.RData",
#' package = "anglemania")
#' )
#' anglemania_object <- create_anglemaniaObject(se, batch_key = "Batch")
#' anglemania_object <- anglemania(anglemania_object)
#' list_stats(anglemania_object) <- get_list_stats(anglemania_object)
#' str(list_stats(anglemania_object))
#' }
#' @seealso \code{\link[bigstatsr]{big_apply}}, \code{\link[bigstatsr]{FBM}}
#' @export
get_list_stats <- function(anglemania_object) {
Expand Down Expand Up @@ -278,7 +325,7 @@ get_list_stats <- function(anglemania_object) {
#' \code{max_n_genes} exceeds the number of unique genes available, all unique
#' genes are returned.
#' @examples
#' \dontrun{
#' \donttest{
#' gene_pairs <- data.frame(
#' geneA = c("Gene1", "Gene2", "Gene3", "Gene4"),
#' geneB = c("Gene3", "Gene4", "Gene5", "Gene6")
Expand Down Expand Up @@ -323,6 +370,7 @@ extract_rows_for_unique_genes <- function(dt, max_n_genes) {
#' @return The input \code{anglemaniaObject} with the
#' \code{integration_genes} slot updated to include the selected genes and
#' their statistical information.
#' @importFrom stats quantile
#' @details
#' The function performs the following steps:
#' \enumerate{
Expand All @@ -339,6 +387,15 @@ extract_rows_for_unique_genes <- function(dt, max_n_genes) {
#' \code{anglemaniaObject}
#' with the selected genes and their statistics.
#' }
#' @examples
#' \donttest{
#' angl <- select_genes(angl,
#' zscore_mean_threshold = 2,
#' zscore_sn_threshold = 2,
#' max_n_genes = 2000)
#' anglemania_genes <- get_anglemania_genes(angl)
#' # View the selected genes and use for integration
#' }
#' @seealso \code{\link{extract_rows_for_unique_genes}},
#' \code{\link{intersect_genes}}, \code{\link{list_stats}}
#' @export
Expand Down Expand Up @@ -366,12 +423,12 @@ select_genes <- function(
# Adjust thresholds if no genes passed the cutoff
if (nrow(gene_ind) == 0) {
message("No genes passed the cutoff.")
quantile95mean <- quantile(
quantile95mean <- stats::quantile(
abs(list_stats(anglemania_object)$mean_zscore),
0.95,
na.rm = TRUE
)
quantile95sn <- quantile(
quantile95sn <- stats::quantile(
list_stats(anglemania_object)$sn_zscore,
0.95,
na.rm = TRUE
Expand Down
20 changes: 20 additions & 0 deletions R/extract_angles.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,27 @@
#' The diagonal elements are set to \code{NA}.
#'
#' @importFrom bigstatsr FBM big_apply big_transpose big_cor
#' @examples
#' \donttest{
#' mat <- matrix(
#' c(
#' 5, 3, 0, 0,
#' 0, 0, 0, 3,
#' 2, 1, 3, 4,
#' 0, 0, 1, 0,
#' 1, 2, 1, 2,
#' 3, 4, 3, 4
#' ),
#' nrow = 6, # 6 genes
#' ncol = 4, # 4 cells
#' byrow = TRUE
#' )
#'
#' mat <- bigstatsr::FBM(nrow = nrow(mat), ncol = ncol(mat), init = mat)
#'
#' angle_mat <- extract_angles(mat)
#' angle_mat[]
#' }
#' @seealso
#' \code{\link[bigstatsr]{big_apply}},
#' \code{\link[bigstatsr]{big_cor}},
Expand Down
22 changes: 22 additions & 0 deletions R/factorise.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,28 @@
#' @importFrom bigstatsr FBM big_apply
#' @importFrom checkmate assertClass assertString assertChoice
#'
#' @examples
#' \donttest{
#' mat <- matrix(
#' c(
#' 5, 3, 0, 0,
#' 0, 0, 0, 3,
#' 2, 1, 3, 4,
#' 0, 0, 1, 0,
#' 1, 2, 1, 2,
#' 3, 4, 3, 4
#' ),
#' nrow = 6, # 6 genes
#' ncol = 4, # 4 cells
#' byrow = TRUE
#' )
#'
#' mat <- bigstatsr::FBM(nrow = nrow(mat), ncol = ncol(mat), init = mat)
#'
#' # Run factorise with method "pearson" and a fixed seed
#' result_fbm <- factorise(mat, method = "pearson", seed = 1)
#' result_fbm[]
#' }
#' @seealso
#' \code{\link{extract_angles}},
#' \code{\link{get_dstat}},
Expand Down
25 changes: 20 additions & 5 deletions R/integrate_by_features.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#' `integrate_by_features` integrates samples or batches within a Seurat
#' object using canonical correlation analysis (CCA) based on a set of
#' selected features (genes). The function utilizes an `anglemaniaObject` to
#' extract integration genes and handles the integration process, including
#' extract anglemania genes and handles the integration process, including
#' optional downstream processing steps such as scaling, PCA, and UMAP
#' visualization.
#'
Expand All @@ -35,7 +35,7 @@
#'
#' @param seurat_object A \code{\link[Seurat]{Seurat}} object containing
#' all samples or batches to be integrated.
#' @param anglem_object An \code{\link{anglemaniaObject}} previously generated
#' @param anglemania_object An \code{\link{anglemaniaObject}} previously generated
#' using \code{\link{create_anglemaniaObject}} and \code{\link{anglemania}}.
#' It is important that the \code{dataset_key} and \code{batch_key} are
#' correctly set in the \code{anglemaniaObject}.
Expand All @@ -57,11 +57,19 @@
#' IntegrateData ScaleData RunPCA RunUMAP DefaultAssay
#' @importFrom pbapply pblapply
#' @importFrom checkmate assertClass assertLogical testFALSE
#'
#' @examples
#' \donttest{
#' # Integrate samples using anglemaniaObject
#' # Automatically reads the batch key from anglemaniaObject
#' # splits the seurat object into batches and integrates them
#' # using CCA integration and anglemania genes previously extracted
#' # with anglemania() or select_genes()
#' integrated_object <- integrate_by_features(seurat_object, anglemania_object)
#' }
#' @seealso
#' \code{\link{create_anglemaniaObject}},
#' \code{\link{anglemania}},
#' \code{\link{extract_integration_genes}},
#' \code{\link{get_anglemania_genes}},
#' \code{\link{integrate_seurat_list}},
#' \code{\link[Seurat]{IntegrateData}},
#' \code{\link[Seurat]{FindIntegrationAnchors}}
Expand Down Expand Up @@ -166,7 +174,14 @@ integrate_by_features <- function(
#' ScaleData RunPCA RunUMAP DefaultAssay
#' @importFrom pbapply pblapply
#' @importFrom checkmate assertClass assertCharacter assertLogical
#'
#' @examples
#' \donttest{
#' # Integrate a list of seurat object using selected
#' # features (e.g. anglemania genes or HVGs)
#' # and CCA integration method
#' seurat_list <- list(seurat_object1, seurat_object2)
#' integrated_seurat <- integrate_seurat_list(seurat_list, features)
#' }
#' @seealso
#' \code{\link{integrate_by_features}},
#' \code{\link[Seurat]{IntegrateData}},
Expand Down
Loading

0 comments on commit e81deaf

Please sign in to comment.