Skip to content

Commit

Permalink
use stop() instead
Browse files Browse the repository at this point in the history
  • Loading branch information
stemangiola committed Oct 17, 2024
1 parent c521054 commit 54efc9f
Show file tree
Hide file tree
Showing 12 changed files with 162 additions and 524 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ Biarch: true
biocViews: AssayDomain, Infrastructure, RNASeq, DifferentialExpression, GeneExpression, Normalization, Clustering, QualityControl, Sequencing, Transcription, Transcriptomics
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
LazyDataCompression: xz
URL: https://github.com/stemangiola/tidybulk
BugReports: https://github.com/stemangiola/tidybulk/issues
9 changes: 3 additions & 6 deletions R/cibersort.R
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,9 @@ my_CIBERSORT <- function(Y, X, perm=0, QN=TRUE, cores = 3, exp_transform = FALSE
))
Y=Y[,colSums(Y)>0, drop=FALSE]

# Check if package is installed, otherwise install
if (find.package("matrixStats", quiet = TRUE) %>% length %>% equals(0)) {
message("tidybulk says: Installing matrixStats needed for cibersort")
install.packages("matrixStats", repos = "https://cloud.r-project.org")
}

# Check if 'matrixStats' package is installed, otherwise stop with instructions
check_and_install_packages("matrixStats")

# Eliminate sd == 0
if(length(which(matrixStats::colSds(Y)==0))>0)
warning(sprintf(
Expand Down
223 changes: 43 additions & 180 deletions R/functions.R

Large diffs are not rendered by default.

99 changes: 21 additions & 78 deletions R/functions_SE.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ get_clusters_kmeans_bulk_SE <-
#'
#' @import tibble
#' @importFrom rlang :=
#' @importFrom utils install.packages
#'
#' @param .data A tibble
#' @param .abundance A column symbol with the value the clustering is based on (e.g., `count`)
Expand All @@ -79,18 +78,7 @@ get_clusters_SNN_bulk_SE <-


# Check if package is installed, otherwise install
if (find.package("cluster", quiet = TRUE) %>% length %>% equals(0)) {
message("Installing cluster")
install.packages("cluster", repos = "https://cloud.r-project.org")
}
if (find.package("Seurat", quiet = TRUE) %>% length %>% equals(0)) {
message("Installing Seurat")
install.packages("Seurat", repos = "https://cloud.r-project.org")
}
if (find.package("KernSmooth", quiet = TRUE) %>% length %>% equals(0)) {
message("Installing KernSmooth")
install.packages("KernSmooth", repos = "https://cloud.r-project.org")
}
check_and_install_packages(c("cluster", "Seurat", "KernSmooth"))

ndims = min(c(nrow(.data), ncol(.data), 30))-1

Expand Down Expand Up @@ -314,7 +302,6 @@ we suggest to partition the dataset for sample clusters.
#' @import tibble
#' @importFrom rlang :=
#' @importFrom stats setNames
#' @importFrom utils install.packages
#'
#' @param .data A tibble
#' @param .abundance A column symbol with the value the clustering is based on (e.g., `count`)
Expand Down Expand Up @@ -354,10 +341,8 @@ get_reduced_dimensions_TSNE_bulk_SE <-


# Check if package is installed, otherwise install
if (find.package("Rtsne", quiet = TRUE) %>% length %>% equals(0)) {
message("Installing Rtsne")
install.packages("Rtsne", repos = "https://cloud.r-project.org")
}
check_and_install_packages("Rtsne")


# Set perprexity to not be too high
if (!"perplexity" %in% names(arguments))
Expand Down Expand Up @@ -397,7 +382,6 @@ get_reduced_dimensions_TSNE_bulk_SE <-
#' @import tibble
#' @importFrom rlang :=
#' @importFrom stats setNames
#' @importFrom utils install.packages
#'
#' @param .data A tibble
#' @param .abundance A column symbol with the value the clustering is based on (e.g., `count`)
Expand Down Expand Up @@ -437,10 +421,7 @@ get_reduced_dimensions_UMAP_bulk_SE <-


# Check if package is installed, otherwise install
if (find.package("uwot", quiet = TRUE) %>% length %>% equals(0)) {
message("tidybulk says: Installing uwot")
install.packages("uwot", repos = "https://cloud.r-project.org")
}
check_and_install_packages("uwot")


# Calculate based on PCA
Expand Down Expand Up @@ -584,10 +565,8 @@ remove_redundancy_elements_through_correlation_SE <- function(.data,
. = NULL

# Check if package is installed, otherwise install
if (find.package("widyr", quiet = TRUE) %>% length %>% equals(0)) {
message("Installing widyr needed for correlation analyses")
install.packages("widyr", repos = "https://cloud.r-project.org")
}
check_and_install_packages("widyr")


# Get the redundant data frame
.data %>%
Expand Down Expand Up @@ -699,7 +678,6 @@ remove_redundancy_elements_though_reduced_dimensions_SE <-
#' @import tibble
#' @importFrom magrittr set_colnames
#' @importFrom stats model.matrix
#' @importFrom utils install.packages
#' @importFrom purrr when
#' @importFrom magrittr extract2
#'
Expand Down Expand Up @@ -762,12 +740,7 @@ get_differential_transcript_abundance_bulk_SE <- function(.data,
~ NULL)

# Check if package is installed, otherwise install
if (find.package("edgeR", quiet = TRUE) %>% length %>% equals(0)) {
message("Installing edgeR needed for differential transcript abundance analyses")
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager", repos = "https://cloud.r-project.org")
BiocManager::install("edgeR", ask = FALSE)
}
check_and_install_packages("edgeR")

# If no assay is specified take first
my_assay = ifelse(
Expand Down Expand Up @@ -880,7 +853,6 @@ get_differential_transcript_abundance_bulk_SE <- function(.data,
#' @import tibble
#' @importFrom magrittr set_colnames
#' @importFrom stats model.matrix
#' @importFrom utils install.packages
#' @importFrom purrr when
#'
#'
Expand Down Expand Up @@ -928,19 +900,17 @@ get_differential_transcript_abundance_bulk_voom_SE <- function(.data,
)
)

# Check if package is installed, otherwise install
check_and_install_packages("limma")


my_contrasts =
.contrasts %>%
ifelse_pipe(length(.) > 0,
~ limma::makeContrasts(contrasts = .x, levels = design),
~ NULL)

# Check if package is installed, otherwise install
if (find.package("limma", quiet = TRUE) %>% length %>% equals(0)) {
message("Installing limma needed for differential transcript abundance analyses")
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager", repos = "https://cloud.r-project.org")
BiocManager::install("limma", ask = FALSE)
}


# If no assay is specified take first
my_assay = ifelse(
Expand Down Expand Up @@ -1061,7 +1031,6 @@ get_differential_transcript_abundance_bulk_voom_SE <- function(.data,
#' @import tibble
#' @importFrom magrittr set_colnames
#' @importFrom stats model.matrix
#' @importFrom utils install.packages
#' @importFrom purrr when
#'
#'
Expand Down Expand Up @@ -1118,12 +1087,7 @@ get_differential_transcript_abundance_glmmSeq_SE <- function(.data,
# }

# Check if package is installed, otherwise install
if (find.package("glmmSeq", quiet = TRUE) %>% length %>% equals(0)) {
message("tidybulk says: Installing glmmSeq needed for differential transcript abundance analyses")
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager", repos = "https://cloud.r-project.org")
BiocManager::install("glmmSeq", ask = FALSE)
}
check_and_install_packages("glmmSeq")

# If no assay is specified take first
my_assay = ifelse(
Expand Down Expand Up @@ -1225,7 +1189,6 @@ get_differential_transcript_abundance_glmmSeq_SE <- function(.data,
#' @import tibble
#' @importFrom magrittr set_colnames
#' @importFrom stats model.matrix
#' @importFrom utils install.packages
#' @importFrom purrr when
#'
#'
Expand Down Expand Up @@ -1268,12 +1231,8 @@ get_differential_transcript_abundance_deseq2_SE <- function(.data,
}

# Check if package is installed, otherwise install
if (find.package("DESeq2", quiet = TRUE) %>% length %>% equals(0)) {
message("Installing DESeq2 needed for differential transcript abundance analyses")
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager", repos = "https://cloud.r-project.org")
BiocManager::install("DESeq2", ask = FALSE)
}
check_and_install_packages("DESeq2")


if (is.null(test_above_log2_fold_change)) {
test_above_log2_fold_change <- 0
Expand Down Expand Up @@ -1396,15 +1355,8 @@ multivariable_differential_tissue_composition_SE = function(
when(
grepl("Surv", .my_formula) %>% any ~ {
# Check if package is installed, otherwise install
if (find.package("survival", quiet = TRUE) %>% length %>% equals(0)) {
message("Installing betareg needed for analyses")
install.packages("survival", repos = "https://cloud.r-project.org")
}

if (find.package("boot", quiet = TRUE) %>% length %>% equals(0)) {
message("Installing boot needed for analyses")
install.packages("boot", repos = "https://cloud.r-project.org")
}
check_and_install_packages(c("survival", "boot"))


(.) %>%
survival::coxph(.my_formula, .) %>%
Expand Down Expand Up @@ -1466,15 +1418,8 @@ univariable_differential_tissue_composition_SE = function(
when(
grepl("Surv", .my_formula) %>% any ~ {
# Check if package is installed, otherwise install
if (find.package("survival", quiet = TRUE) %>% length %>% equals(0)) {
message("Installing betareg needed for analyses")
install.packages("survival", repos = "https://cloud.r-project.org")
}

if (find.package("boot", quiet = TRUE) %>% length %>% equals(0)) {
message("Installing boot needed for analyses")
install.packages("boot", repos = "https://cloud.r-project.org")
}
check_and_install_packages(c("survival", "boot"))


(.) %>%
mutate(.proportion_0_corrected = .proportion_0_corrected %>% boot::logit()) %>%
Expand All @@ -1484,10 +1429,8 @@ univariable_differential_tissue_composition_SE = function(
} ,
~ {
# Check if package is installed, otherwise install
if (find.package("betareg", quiet = TRUE) %>% length %>% equals(0)) {
message("Installing betareg needed for analyses")
install.packages("betareg", repos = "https://cloud.r-project.org")
}
check_and_install_packages("betareg")

(.) %>%
betareg::betareg(.my_formula, .) %>%
broom::tidy() %>%
Expand Down
6 changes: 1 addition & 5 deletions R/ggplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,7 @@ log10_reverse_trans <- function() {
#' @export
logit_trans <- function(){


if (find.package("functional", quiet = TRUE) %>% length %>% equals(0)) {
message("Installing functional needed for analyses")
install.packages("functional", repos = "https://cloud.r-project.org")
}
check_and_install_packages("functional")

trans <- qlogis
inv <- plogis
Expand Down
33 changes: 7 additions & 26 deletions R/glmmSeq.R
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ glmmTMB_to_confidence_intervals_random_effects = function(fit){
pivot_longer(-group_id, names_to = "parameter", values_to = "CI")
)

mod = glmmTMB::ranef(fit, condVar=T)$cond
mod = glmmTMB::ranef(fit, condVar=TRUE)$cond
mod = map2_dfr(
mod, names(mod),
~ .x |>
Expand Down Expand Up @@ -230,7 +230,7 @@ lmer_to_confidence_intervals_random_effects = function(fit){
pivot_longer(-group_id, names_to = "parameter", values_to = "CI")
)

mod = lme4::ranef(fit, condVar=T)
mod = lme4::ranef(fit, condVar=TRUE)
mod = map2_dfr(
mod, names(mod),
~ .x |>
Expand Down Expand Up @@ -581,12 +581,7 @@ glmmSeq = function (modelFormula, countdata, metadata, id = NULL, dispersion = N
if (progress) {

# Check if package is installed, otherwise install
if (find.package("pblapply", quiet = TRUE) %>% length %>% equals(0)) {
message("tidybulk says: Installing pblapply needed for differential transcript abundance analyses")
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager", repos = "https://cloud.r-project.org")
BiocManager::install("pblapply", ask = FALSE)
}
check_and_install_packages("pbapply")

resultList <- pbapply::pblapply(fullList, function(geneList) {
args <- c(list(geneList = geneList, fullFormula = fullFormula,
Expand Down Expand Up @@ -635,12 +630,7 @@ glmmSeq = function (modelFormula, countdata, metadata, id = NULL, dispersion = N
else if (progress) {

# Check if package is installed, otherwise install
if (find.package("pbmcapply", quiet = TRUE) %>% length %>% equals(0)) {
message("tidybulk says: Installing pbmcapply needed for differential transcript abundance analyses")
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager", repos = "https://cloud.r-project.org")
BiocManager::install("pbmcapply", ask = FALSE)
}
check_and_install_packages("pbmcapply")

resultList <- pbmcapply::pbmclapply(fullList, function(geneList) {
glmerCore(geneList, fullFormula, reduced,
Expand Down Expand Up @@ -688,12 +678,8 @@ glmmSeq = function (modelFormula, countdata, metadata, id = NULL, dispersion = N
if (progress) {

# Check if package is installed, otherwise install
if (find.package("pblapply", quiet = TRUE) %>% length %>% equals(0)) {
message("tidybulk says: Installing pblapply needed for differential transcript abundance analyses")
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager", repos = "https://cloud.r-project.org")
BiocManager::install("pblapply", ask = FALSE)
}
check_and_install_packages("pbapply")


resultList <- pbapply::pblapply(fullList, function(geneList) {
args <- c(list(geneList = geneList, fullFormula = fullFormula,
Expand Down Expand Up @@ -721,12 +707,7 @@ glmmSeq = function (modelFormula, countdata, metadata, id = NULL, dispersion = N
if (progress) {

# Check if package is installed, otherwise install
if (find.package("pbmcapply", quiet = TRUE) %>% length %>% equals(0)) {
message("tidybulk says: Installing pbmcapply needed for differential transcript abundance analyses")
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager", repos = "https://cloud.r-project.org")
BiocManager::install("pbmcapply", ask = FALSE)
}
check_and_install_packages("pbmcapply")

resultList <- pbmcapply::pbmclapply(fullList, function(geneList) {
glmmTMBcore(geneList, fullFormula, reduced,
Expand Down
Loading

0 comments on commit 54efc9f

Please sign in to comment.