From e0ddd5125ae55a9af7df9a01a26ed7cbae5bc4ea Mon Sep 17 00:00:00 2001 From: Arshammik <79arsham@gmail.com> Date: Fri, 9 May 2025 19:33:03 -0400 Subject: [PATCH 1/3] Adding the linux Makevars for linux --- src/Makevars.linux | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Makevars.linux b/src/Makevars.linux index e69de29..4952079 100644 --- a/src/Makevars.linux +++ b/src/Makevars.linux @@ -0,0 +1,4 @@ +## src/Makevars (Linux) +CXX_STD = CXX14 +PKG_CXXFLAGS += -O3 -march=native -pipe -fopenmp +PKG_LIBS += -fopenmp $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS) From a6031985eaad89078f265acf6861e662105da375 Mon Sep 17 00:00:00 2001 From: Arshammik <79arsham@gmail.com> Date: Fri, 9 May 2025 19:34:05 -0400 Subject: [PATCH 2/3] Avoiding for loops in the gene/events finding fucntions --- DESCRIPTION | 26 ++++++------- R/feature_selection.R | 76 ++++++++++++++++++------------------- R/zzz.R | 4 +- man/find_variable_events.Rd | 6 +-- 4 files changed, 54 insertions(+), 58 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 97e5171..9b65f38 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: splikit Title: A toolkit for analysing RNA splicing in scRNA-seq data -Version: 1.0.3 +Version: 1.0.4 Authors@R: person("Arsham", "Mikaeili Namini", , "arsham.mikaeilinamini@mail.mcgill.ca", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-9453-6951")) @@ -14,18 +14,14 @@ License: MIT + file LICENSE Encoding: UTF-8 Roxygen: list(markdown = TRUE) RoxygenNote: 7.3.2 -Imports: - Matrix, - data.table, - methods, - stats, - Rcpp, - RcppArmadillo, - RcppEigen -LinkingTo: - Rcpp, - RcppArmadillo, - RcppEigen -Suggests: - testthat (>= 3.0.0) +Imports: Matrix, data.table, methods, stats, Rcpp, RcppArmadillo, + RcppEigen +LinkingTo: Rcpp, RcppArmadillo, RcppEigen +Suggests: testthat (>= 3.0.0) Config/testthat/edition: 3 +NeedsCompilation: yes +Packaged: 2025-05-09 22:41:00 UTC; arsham79 +Author: Arsham Mikaeili Namini [aut, cre] + () +Maintainer: Arsham Mikaeili Namini +Depends: R (>= 3.5.0) diff --git a/R/feature_selection.R b/R/feature_selection.R index 805a7e6..b140fe2 100644 --- a/R/feature_selection.R +++ b/R/feature_selection.R @@ -2,9 +2,9 @@ #' #' @param m1_matrix A matrix representing the inclusion matrix. Rows are events, columns are barcodes. #' @param m2_matrix A matrix representing the exclusion matrix. Rows are events, columns are barcodes. +#' @param n_threads If the module OpenPM is available for your device, the function suggests using multi-thread processing for even faster computation. #' @param min_row_sum A numeric value specifying the minimum row sum threshold for filtering events. Defaults to 50. #' @param verbose Logical. If \code{TRUE} (default), prints progress and informational messages. -#' @param n_threads If the module OpenPM is available for your device, the function suggests using multi-thread processing for even faster computation. #' @param ... Additional arguments to be passed. #' #' @return A \code{data.table} containing the events and their corresponding sum deviance values. @@ -19,31 +19,16 @@ #' # printing the results #' print(HVE[order(-sum_deviance)]) #' @export -find_variable_events <- function(m1_matrix, m2_matrix, min_row_sum = 50, verbose=TRUE, n_threads=1, ...) { - - # Load necessary libraries - if (!requireNamespace("data.table", quietly = TRUE)) { - stop("The 'data.table' package is required but not installed.") - } - - if (!requireNamespace("Rcpp", quietly = TRUE)) { - stop("The 'Rcpp' package is required but not installed.") - } - - if (!requireNamespace("Matrix", quietly = TRUE)) { - stop("The 'Matrix' package is required but not installed.") - } +find_variable_events <- function(m1_matrix, m2_matrix, min_row_sum = 50, n_threads=1, verbose=TRUE, ...) { # Check if matrices are sparse if (!(inherits(m1_matrix, "Matrix") && inherits(m2_matrix, "Matrix"))) { stop("Both 'm1_matrix' and 'm2_matrix' must be sparse matrices of class 'Matrix'.") } - # Check matrix compatibility if (!identical(colnames(m1_matrix), colnames(m2_matrix))) { stop("The colnames (barcodes) of inclusion and exclusion matrices are not identical.") } - if (!identical(rownames(m1_matrix), rownames(m2_matrix))) { stop("The rownames (junction events) of inclusion and exclusion matrices are not identical.") } @@ -65,24 +50,26 @@ find_variable_events <- function(m1_matrix, m2_matrix, min_row_sum = 50, verbose sum_deviances <- numeric(nrow(m1_matrix)) names(sum_deviances) <- rownames(m1_matrix) - for (lib in libraries) { + deviance_results <- lapply(libraries, function(lib) { filter <- which(meta[, ID] == lib) M1_sub <- m1_matrix[, filter, drop = FALSE] M2_sub <- m2_matrix[, filter, drop = FALSE] - # Calculate deviances using the C++ function deviance_values <- tryCatch({ calcDeviances_ratio(M1_sub, M2_sub, n_threads) }, error = function(e) { stop("Error in calcDeviances_ratio function: ", e$message) }) - deviance_values <- c(deviance_values) names(deviance_values) <- rownames(M1_sub) - sum_deviances <- sum_deviances + deviance_values - if(verbose){cat("Calculating the deviances for sample", lib, "has been completed!\n")} - } + if(verbose) { + cat("Calculating the deviances for sample", lib, "has been completed!\n") + } + return(deviance_values) + }) + # Combine all results + sum_deviances <- Reduce(`+`, deviance_results) rez <- data.table::data.table(events = names(sum_deviances), sum_deviance = as.numeric(sum_deviances)) return(rez) cat("All Done!\n") @@ -166,25 +153,38 @@ find_variable_genes <- function(gene_expression_matrix, method = "vst", ...) { sum_deviances <- numeric(nrow(gene_expression_matrix)) names(sum_deviances) <- rownames(gene_expression_matrix) - # Loop over each library to compute deviances - for (lib in libraries) { - filter <- which(meta[, ID] == lib) - gene_expression_matrix_sub <- gene_expression_matrix[, filter, drop = FALSE] - - # Calculate deviances using the C++ function - deviance_values <- tryCatch({ - calcNBDeviancesWithThetaEstimation(as(gene_expression_matrix_sub, "dgCMatrix")) - - }, error = function(e) { - stop("Error in calcNBDeviancesWithThetaEstimation function: ", e$message) + calculate_all_deviances <- function(gene_expression_matrix, meta, ID) { + # Get unique libraries + libraries <- unique(meta[, ID]) + + # Initialize results list + deviances_list <- vector("list", length(libraries)) + names(deviances_list) <- libraries + + # Getting the sim devinaces fir NB model + deviances_list <- lapply(libraries, function(lib) { + filter <- which(meta[, ID] == lib) + gene_expression_matrix_sub <- gene_expression_matrix[, filter, drop = FALSE] + deviance_values <- tryCatch({ + calcNBDeviancesWithThetaEstimation(as(gene_expression_matrix_sub, "dgCMatrix")) + }, error = function(e) { + stop("Error in calcNBDeviancesWithThetaEstimation function: ", e$message) + }) + deviance_values <- c(deviance_values) + names(deviance_values) <- rownames(gene_expression_matrix_sub) + cat("Calculating the deviances for sample", lib, "has been completed!\n") + return(deviance_values) }) - deviance_values <- c(deviance_values) - names(deviance_values) <- rownames(gene_expression_matrix_sub) - sum_deviances <- sum_deviances + deviance_values - cat("Calculating the deviances for sample", lib, "has been completed!\n") + # Sum deviances across all libraries + sum_deviances <- Reduce(`+`, deviances_list) + + return(sum_deviances) } + # Then use it: + sum_deviances <- calculate_all_deviances(gene_expression_matrix, meta, ID) + # Compute row variance using the previously defined function row_var <- tryCatch({ splikit::get_rowVar(M = gene_expression_matrix) diff --git a/R/zzz.R b/R/zzz.R index 70f0eaf..4b44563 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -6,8 +6,8 @@ " at McGill University, Montreal. Distributed under the MIT License.\n", " For documentation and examples, see the package vignette.\n", "-------------------------------------------------------------------------------------------------------\n", - " Bienvenue dans Splikit — Développé par le Laboratoire de génomique computationnelle et statistique\n", - " de l’Université McGill, à Montréal. Distribué sous licence MIT.\n", + " Bienvenue dans Splikit — Developpe par le Laboratoire de genomique computationnelle et statistique\n", + " de l’Universite McGill, a Montreal. Distribue sous licence MIT.\n", " Pour la documentation et des exemples, consultez la vignette du package.\n", "=======================================================================================================\n" ) diff --git a/man/find_variable_events.Rd b/man/find_variable_events.Rd index f92bafe..188aa07 100644 --- a/man/find_variable_events.Rd +++ b/man/find_variable_events.Rd @@ -8,8 +8,8 @@ find_variable_events( m1_matrix, m2_matrix, min_row_sum = 50, - verbose = TRUE, n_threads = 1, + verbose = TRUE, ... ) } @@ -20,10 +20,10 @@ find_variable_events( \item{min_row_sum}{A numeric value specifying the minimum row sum threshold for filtering events. Defaults to 50.} -\item{verbose}{Logical. If \code{TRUE} (default), prints progress and informational messages.} - \item{n_threads}{If the module OpenPM is available for your device, the function suggests using multi-thread processing for even faster computation.} +\item{verbose}{Logical. If \code{TRUE} (default), prints progress and informational messages.} + \item{...}{Additional arguments to be passed.} } \value{ From 6f56f517d408c351639b122e274cab221a4d3f72 Mon Sep 17 00:00:00 2001 From: Arshammik <79arsham@gmail.com> Date: Fri, 9 May 2025 19:34:36 -0400 Subject: [PATCH 3/3] Updating the test to best-practice style --- .github/workflows/R-CMD-check.yml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/R-CMD-check.yml b/.github/workflows/R-CMD-check.yml index ecd6a9f..0e051c5 100644 --- a/.github/workflows/R-CMD-check.yml +++ b/.github/workflows/R-CMD-check.yml @@ -12,14 +12,14 @@ jobs: strategy: matrix: config: - - { os: ubuntu-latest, r: '4.5' } - - { os: ubuntu-latest, r: '4.4' } - - { os: windows-latest, r: '4.5' } - - { os: windows-latest, r: '4.4' } - - { os: macos-latest, r: '4.5' } - - { os: macos-latest, r: '4.4' } + - { os: ubuntu-latest, r: 'release' } + - { os: ubuntu-latest, r: 'oldrel-1' } + - { os: windows-latest, r: 'release' } + - { os: windows-latest, r: 'oldrel-1' } + - { os: macos-latest, r: 'release' } + - { os: macos-latest, r: 'oldrel-1' } name: R ${{ matrix.config.r }} - ${{ matrix.config.os }} - + steps: - uses: actions/checkout@v3