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

Fix error, notes, and warning based on BiocCheck() #469

Merged
merged 26 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
70e3e7e
fix warning BiocCheck
erawijantari Oct 18, 2023
787cb87
fix warning BiocCheck
erawijantari Oct 18, 2023
81af557
fix warning BiocCheck
erawijantari Oct 18, 2023
0ae1602
fix note and warning BiocCheck
erawijantari Oct 18, 2023
27c15c0
fix note and warning BiocCheck
erawijantari Oct 18, 2023
4933280
fix note and warning BiocCheck
erawijantari Oct 18, 2023
213682a
fix note and warning BiocCheck
erawijantari Oct 18, 2023
64c97df
fix note and warning BiocCheck
erawijantari Oct 18, 2023
e441369
fix note and warning BiocCheck
erawijantari Oct 18, 2023
1571cc3
fix note and warning BiocCheck
erawijantari Oct 18, 2023
7b367a7
fix note and warning BiocCheck
erawijantari Oct 18, 2023
1af277b
fix note and warning BiocCheck
erawijantari Oct 19, 2023
911cb53
fix note and warning BiocCheck
erawijantari Oct 19, 2023
d97aef8
Merge branch 'master' into master
antagomir Oct 19, 2023
c628baa
fix note and warning BiocCheck
erawijantari Oct 19, 2023
9880846
Merge branch 'master' of https://github.com/erawijantari/mia
erawijantari Oct 19, 2023
181691e
fix note and warning BiocCheck
erawijantari Oct 19, 2023
6bdd6fc
remove set.seed() inside function
erawijantari Nov 28, 2023
0db2e52
adjust the test code by setting the seed outside function
erawijantari Nov 28, 2023
ed4162c
Merge branch 'master' into master
TuomasBorman Nov 28, 2023
e87d76c
Merge branch 'master' into master
antagomir Feb 19, 2024
c23ca90
Merge branch 'master' into master
antagomir Feb 22, 2024
d04d36e
resolve conflict
erawijantari Mar 11, 2024
a56c07f
Merge branch 'erawijantari-master'
erawijantari Mar 11, 2024
6c64ee0
Merge branch 'master' into master
ake123 Mar 12, 2024
2103c98
Merge branch 'master' into master
antagomir Mar 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions R/estimateDivergence.R
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,13 @@ setMethod("estimateDivergence", signature = c(x="SummarizedExperiment"),
if( "median" %in% reference || "mean" %in% reference ){
reference <- apply(mat, 1, reference)
} else if( !reference %in% colnames(mat) ) {
stop(paste("Reference", reference, "not recognized."))
stop("Reference ", reference, " not recognized.", call. = FALSE)
}
}

# Distance between all samples against one reference sample
# FIXME: could be be optimzed with sweep / parallelization
v <- seq_len(ncol(mat))
sapply(v, function (i) {FUN(rbind(mat[,i], reference), method=method, ...)})
vapply(v, function (i) {FUN(rbind(mat[,i], reference), method=method, ...)},FUN.VALUE = numeric(1))
}

2 changes: 1 addition & 1 deletion R/estimateDiversity.R
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@
#' plotColData(tse, "Shannon")
#' # ... by sample type
#' plotColData(tse, "Shannon", "SampleType")
#' \dontrun{
#' \donttest{
#' # combining different plots
#' library(patchwork)
#' plot_index <- c("Shannon","GiniSimpson")
Expand Down
2 changes: 1 addition & 1 deletion R/estimateDominance.R
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@
#'
#' # Indices must be written correctly (e.g. dbp, not dbp), otherwise an error
#' # gets thrown
#' \dontrun{esophagus <- estimateDominance(esophagus, index="dbp")}
#' \donttest{esophagus <- estimateDominance(esophagus, index="dbp")}
#' # Calculates dbp and Core Abundance indices
#' esophagus <- estimateDominance(esophagus, index=c("dbp", "core_abundance"))
#' # Shows all indices
Expand Down
2 changes: 1 addition & 1 deletion R/estimateRichness.R
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@
#'
#' # Indices must be written correctly (all lowercase), otherwise an error
#' # gets thrown
#' \dontrun{esophagus <- estimateRichness(esophagus, index="ace")}
#' \donttest{esophagus <- estimateRichness(esophagus, index="ace")}
#'
#' # Calculates Chao1 and ACE indices only
#' esophagus <- estimateRichness(esophagus, index=c("chao1", "ace"),
Expand Down
22 changes: 11 additions & 11 deletions R/getExperimentCrossAssociation.R
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,7 @@ setMethod("getExperimentCrossCorrelation", signature = c(x = "ANY"),
# If assays were identical, and duplicate variable pairs were dropped
if( assays_identical ){
# Change names so that they are not equal to colnames of variable_pairs
colnames(variable_pairs)[1:2] <- c("Var1_", "Var2_")
colnames(variable_pairs)[c(1,2)] <- c("Var1_", "Var2_")
# Combine feature-pair names with correlation values and p-values
correlations_and_p_values <- cbind(variable_pairs, correlations_and_p_values)

Expand Down Expand Up @@ -1170,27 +1170,27 @@ setMethod("getExperimentCrossCorrelation", signature = c(x = "ANY"),
do.call(association_FUN, args = c(list(feature_mat), list(...)))
},
error = function(cond) {
stop(paste0("Error occurred during calculation. Check, e.g., that ",
stop("Error occurred during calculation. Check, e.g., that ",
"'association_FUN' fulfills requirements. 'association_FUN' ",
"threw a following error:\n", cond),
"threw a following error:\n", cond,
call. = FALSE)
})
} else {
temp <- tryCatch({
suppressWarnings( do.call(association_FUN, args = c(list(feature_mat), list(...))) )
},
error = function(cond) {
stop(paste0("Error occurred during calculation. Check, e.g., that ",
stop("Error occurred during calculation. Check, e.g., that ",
"'association_FUN' fulfills requirements. 'association_FUN' ",
"threw a following error:\n", cond),
"threw a following error:\n", cond,
call. = FALSE)
})
}

# If temp's length is not 1, then function does not return single numeric value for each pair
if( length(temp) != 1 ){
stop(paste0("Error occurred during calculation. Check that ",
"'association_FUN' fulfills requirements."),
stop("Error occurred during calculation. Check that ",
"'association_FUN' fulfills requirements.",
call. = FALSE)
}
return(temp)
Expand Down Expand Up @@ -1298,8 +1298,8 @@ setMethod("getExperimentCrossCorrelation", signature = c(x = "ANY"),
use="pairwise.complete.obs")))$order
},
error = function(cond) {
stop(paste0("Error occurred during sorting. Possible reason is that ",
"correlation matrix includes NAs. Try with 'sort = FALSE'."),
stop("Error occurred during sorting. Possible reason is that ",
"correlation matrix includes NAs. Try with 'sort = FALSE'.",
call. = FALSE)
}
)
Expand All @@ -1308,8 +1308,8 @@ setMethod("getExperimentCrossCorrelation", signature = c(x = "ANY"),
use="pairwise.complete.obs")))$order
},
error = function(cond) {
stop(paste0("Error occurred during sorting. Possible reason is that ",
"correlation matrix includes NAs. Try with 'sort = FALSE'."),
stop("Error occurred during sorting. Possible reason is that ",
"correlation matrix includes NAs. Try with 'sort = FALSE'.",
call. = FALSE)
}
)
Expand Down
4 changes: 2 additions & 2 deletions R/loadFromMetaphlan.R
Original file line number Diff line number Diff line change
Expand Up @@ -307,13 +307,13 @@ loadFromMetaphlan <- function(
sample_names <- rownames(coldata)
names(sample_names) <- sample_names
} else{
sample_names <- sapply(rownames(coldata), function(x){
sample_names <- vapply(rownames(coldata), function(x){
x <- colnames(tse)[grep(x, colnames(tse))]
if( length(x) != 1 ){
x <- NULL
}
return(x)
})
},FUN.VALUE = character(1))
sample_names <- unlist(sample_names)
}

Expand Down
4 changes: 2 additions & 2 deletions R/merge.R
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,14 @@ setGeneric("mergeSamples",
.check_assays_for_merge <- function(assay.type, assay){
# Check if assays include binary or negative values
if( all(assay == 0 | assay == 1) ){
warning(paste0("'",assay.type,"'", " includes binary values."),
warning("'",assay.type,"'", " includes binary values.",
"\nAgglomeration of it might lead to meaningless values.",
"\nCheck the assay, and consider doing transformation again manually",
" with agglomerated data.",
call. = FALSE)
}
if( !all( assay >= 0 | is.na(assay) ) ){
warning(paste0("'",assay.type,"'", " includes negative values."),
warning("'",assay.type,"'", " includes negative values.",
"\nAgglomeration of it might lead to meaningless values.",
"\nCheck the assay, and consider doing transformation again manually",
" with agglomerated data.",
Expand Down
4 changes: 2 additions & 2 deletions R/mergeSEs.R
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ setMethod("right_join", signature = c(x = "ANY"),

# Loop through individual TreeSEs and add them to tse
if( length(x) > 0 ){
for( i in 1:length(x) ){
for( i in seq_len(length(x)) ){
# Give message if TRUE
if( verbose ){
message("\r", i+1, "/", length(x)+1, appendLF = FALSE)
Expand Down Expand Up @@ -765,7 +765,7 @@ setMethod("right_join", signature = c(x = "ANY"),
# Get the shared class that is highest in hierarchy
if( all( classes %in% allowed_classes[1] ) ){
class <- allowed_classes[1]
} else if( all( classes %in% allowed_classes[1:2] ) ){
} else if( all( classes %in% allowed_classes[c(1,2)] ) ){
class <- allowed_classes[2]
} else {
class <- allowed_classes[3]
Expand Down
2 changes: 1 addition & 1 deletion R/runCCA.R
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ setMethod("runCCA", "SingleCellExperiment",
# Get the dissimilarity matrix based on original dissimilarity index
# provided by user. If the analysis is CCA, disable method; calculate
# always euclidean distances because CCA is based on Euclidean distances.
if( length(class(rda)) == 1 && class(rda) == "cca" ){
if( length(class(rda)) == 1 && is(rda, 'cca') ){
dist_mat <- vegdist(mat, method = "euclidean")
} else{
dist_mat <- vegdist(mat, method = method, ...)
Expand Down
1 change: 1 addition & 0 deletions R/splitOn.R
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ setMethod("splitOn", signature = c(x = "TreeSummarizedExperiment"),
# If the returned value is a list, go through all of them
if( is(x, 'SimpleList') ){
x <- SimpleList(lapply(x, .agglomerate_trees))

} else {
# Otherwise, the returned value is TreeSE
x <- .agglomerate_trees(x)
Expand Down
163 changes: 79 additions & 84 deletions R/subsampleCounts.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#' instances where it can be useful.
#' Note that the output of \code{subsampleCounts} is not the equivalent as the
#' input and any result have to be verified with the original dataset.
#' To maintain the reproducibility, please define the seed using set.seed()
#' before implement this function.
#'
#' @param x A
#' \code{SummarizedExperiment} object.
Expand All @@ -28,7 +30,7 @@
#' simulated this can equal to lowest number of total counts
#' found in a sample or a user specified number.
#'
#' @param seed A random number seed for reproducibility of sampling.
#'
#'
#' @param replace Logical Default is \code{TRUE}. The default is with
#' replacement (\code{replace=TRUE}).
Expand Down Expand Up @@ -68,10 +70,11 @@
#' # they will be removed.
#' data(GlobalPatterns)
#' tse <- GlobalPatterns
#' set.seed(123)
#' tse.subsampled <- subsampleCounts(tse,
#' min_size = 60000,
#' name = "subsampled",
#' seed = 123)
#' name = "subsampled"
#' )
#' tse.subsampled
#' dim(tse)
#' dim(tse.subsampled)
Expand All @@ -84,7 +87,7 @@ NULL
setGeneric("subsampleCounts", signature = c("x"),
function(x, assay.type = assay_name, assay_name = "counts",
min_size = min(colSums2(assay(x))),
seed = runif(1, 0, .Machine$integer.max), replace = TRUE,
replace = TRUE,
name = "subsampled", verbose = TRUE, ...)
standardGeneric("subsampleCounts"))

Expand All @@ -94,86 +97,78 @@ setGeneric("subsampleCounts", signature = c("x"),
#' @aliases rarifyCounts
#' @export
setMethod("subsampleCounts", signature = c(x = "SummarizedExperiment"),
function(x, assay.type = assay_name, assay_name = "counts",
min_size = min(colSums2(assay(x))),
seed = runif(1, 0, .Machine$integer.max), replace = TRUE,
name = "subsampled", verbose = TRUE, ...){

warning("Subsampling/Rarefying may undermine downstream analyses ",
"and have unintended consequences. Therefore, make sure ",
"this normalization is appropriate for your data.",
call. = FALSE)
.check_assay_present(assay.type, x)
if(any(assay(x, assay.type) %% 1 != 0)){
warning("assay contains non-integer values. Only counts table ",
"is applicable...")
}
if(!is.logical(verbose)){
stop("`verbose` has to be logical i.e. TRUE or FALSE")
}
if(verbose){
# Print to screen this value
message("`set.seed(", seed, ")` was used to initialize repeatable ",
"random subsampling.","\nPlease record this for your ",
"records so others can reproduce.")
}
if(!.is_numeric_string(seed)){
stop("`seed` has to be an numeric value See `?set.seed`")
}
if(!is.logical(replace)){
stop("`replace` has to be logical i.e. TRUE or FALSE")
}
# Check name
if(!.is_non_empty_string(name) ||
name == assay.type){
stop("'name' must be a non-empty single character value and be ",
"different from `assay.type`.",
call. = FALSE)
}
set.seed(seed)
# Make sure min_size is of length 1.
if(length(min_size) > 1){
stop("`min_size` had more than one value. ",
"Specifiy a single integer value.")
min_size <- min_size[1]
}
if(!is.numeric(min_size) ||
as.integer(min_size) != min_size && min_size <= 0){
stop("min_size needs to be a positive integer value.")
}
# get samples with less than min number of reads
if(min(colSums2(assay(x, assay.type))) < min_size){
rmsams <- colnames(x)[colSums2(assay(x, assay.type)) < min_size]
# Return NULL, if no samples were found after subsampling
if( !any(!colnames(x) %in% rmsams) ){
stop("No samples were found after subsampling.",
call. = FALSE)
}
if(verbose){
message(length(rmsams), " samples removed ",
"because they contained fewer reads than `min_size`.")
}
# remove sample(s)
newtse <- x[, !colnames(x) %in% rmsams]
} else {
newtse <- x
}
newassay <- apply(assay(newtse, assay.type), 2,
.subsample_assay,
min_size=min_size, replace=replace)
rownames(newassay) <- rownames(newtse)
# remove features not present in any samples after subsampling
message(paste(length(which(rowSums2(newassay) == 0)), "features",
"removed because they are not present in all samples",
"after subsampling."))
newassay <- newassay[rowSums2(newassay)>0,]
newtse <- newtse[rownames(newassay),]
assay(newtse, name, withDimnames=FALSE) <- newassay
newtse <- .add_values_to_metadata(newtse,
"subsampleCounts_min_size",
min_size)
return(newtse)
}
function(x, assay.type = assay_name, assay_name = "counts",
min_size = min(colSums2(assay(x))),
replace = TRUE,
name = "subsampled", verbose = TRUE, ...){

warning("Subsampling/Rarefying may undermine downstream analyses ",
"and have unintended consequences. Therefore, make sure ",
"this normalization is appropriate for your data.",
call. = FALSE)
.check_assay_present(assay.type, x)
if(any(assay(x, assay.type) %% 1 != 0)){
warning("assay contains non-integer values. Only counts table ",
"is applicable...")
}
if(!is.logical(verbose)){
stop("`verbose` has to be logical i.e. TRUE or FALSE")
}

if(!is.logical(replace)){
stop("`replace` has to be logical i.e. TRUE or FALSE")
}
# Check name
if(!.is_non_empty_string(name) ||
name == assay.type){
stop("'name' must be a non-empty single character value and be ",
"different from `assay.type`.",
call. = FALSE)
}
#set.seed(seed)
# Make sure min_size is of length 1.
if(length(min_size) > 1){
stop("`min_size` had more than one value. ",
"Specifiy a single integer value.")
min_size <- min_size[1]
}
if(!is.numeric(min_size) ||
as.integer(min_size) != min_size && min_size <= 0){
stop("min_size needs to be a positive integer value.")
}
# get samples with less than min number of reads
if(min(colSums2(assay(x, assay.type))) < min_size){
rmsams <- colnames(x)[colSums2(assay(x, assay.type)) < min_size]
# Return NULL, if no samples were found after subsampling
if( !any(!colnames(x) %in% rmsams) ){
stop("No samples were found after subsampling.",
call. = FALSE)
}
if(verbose){
message(length(rmsams), " samples removed ",
"because they contained fewer reads than `min_size`.")
}
# remove sample(s)
newtse <- x[, !colnames(x) %in% rmsams]
} else {
newtse <- x
}
newassay <- apply(assay(newtse, assay.type), 2,
.subsample_assay,
min_size=min_size, replace=replace)
rownames(newassay) <- rownames(newtse)
# remove features not present in any samples after subsampling
message(paste(length(which(rowSums2(newassay) == 0)), "features",
"removed because they are not present in all samples",
"after subsampling."))
newassay <- newassay[rowSums2(newassay)>0,]
newtse <- newtse[rownames(newassay),]
assay(newtse, name, withDimnames=FALSE) <- newassay
newtse <- .add_values_to_metadata(newtse,
"subsampleCounts_min_size",
min_size)
return(newtse)
}
)


Expand Down
2 changes: 1 addition & 1 deletion R/summaries.R
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ setMethod("summary", signature = c(object = "SummarizedExperiment"),
.check_NAs_assay_counts <- function(x, assay.type){
assay.x <- .get_assay(x, assay.type)
if(any(is.na(assay.x))) {
stop(paste0("There are samples with NAs in 'assay': ", assay.type),
stop("There are samples with NAs in 'assay': ", assay.type,
" . This function is limited to sequencing data only. ",
"Where raw counts do not usually have NAs. ",
"Try to supply raw counts",
Expand Down
2 changes: 1 addition & 1 deletion R/transformCounts.R
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ setMethod("relAbundanceCounts", signature = c(x = "SummarizedExperiment"),
pseudocount <- ifelse(pseudocount, min(mat[mat>0]), 0)
# Report pseudocount if positive value
if ( pseudocount > 0 ){
message(paste("A pseudocount of", pseudocount, "was applied."))
message("A pseudocount of ", pseudocount, " was applied.")
}
}
# Give warning if pseudocount should not be added
Expand Down
Loading
Loading