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

plotRDA fix #101

Merged
merged 4 commits into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: miaViz
Title: Microbiome Analysis Plotting and Visualization
Version: 1.9.3
Version: 1.9.4
Authors@R:
c(person(given = "Tuomas", family = "Borman", role = c("aut", "cre"),
email = "[email protected]",
Expand Down
90 changes: 34 additions & 56 deletions R/plotCCA.R
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,9 @@ setMethod("plotRDA", signature = c(object = "matrix"),
#' @importFrom SingleCellExperiment reducedDim reducedDimNames
.incorporate_rda_vis <- function(
tse, dimred, ncomponents = 2, colour_by = color_by, color_by = NULL,
shape_by = NULL, size_by = NULL, order_by = NULL, text_by = NULL,
other_fields = list(), swap_rownames = NULL, point.padding = NA,
add.ellipse = TRUE, add.vectors = TRUE, add.significance = TRUE,
add.expl.var = TRUE, vec_lab = NULL, bins = NULL, sep.group = "\U2012",
repl.underscore = " ", ...){
add.significance = TRUE, add.expl.var = TRUE, add.ellipse = TRUE,
add.vectors = TRUE, vec.lab = NULL, expl_var = NULL,
sep.group = "\U2012", repl.underscore = " ", ...){

# Check dimred
if( !dimred %in% reducedDimNames(tse) ){
Expand All @@ -312,32 +310,30 @@ setMethod("plotRDA", signature = c(object = "matrix"),
if( ncol(reduced_dim) < 2 ){
stop("reducedDim specified by 'dimred' must have at least 2 columns.", call. = FALSE)
}
# Check that each by-argument matches the name of a column in colData
by_args <- c(
colour_by = colour_by,
shape_by = shape_by,
size_by = size_by,
order_by = order_by,
text_by = text_by
)
correct_args <- sapply(by_args, function(x) x %in% names(colData(tse)))
sapply(names(by_args[!correct_args]),
function(x) {
stop(paste0("'", x, "' must match the name of a column in colData."),
call. = FALSE)
})
# Only 2 dimensions are supported currently
ncomponents <- 2
# Make list of arguments for plotReducedDim
plotReducedDim_args <- c(by_args, list(
object = tse, dimred = dimred, ncomponents = ncomponents,
text_size = 5, text_colour = "black", text_color = "black",
label_format = c("%s %i", " (%i%%)"), other_fields = other_fields,
swap_rownames = swap_rownames, point.padding = point.padding, force = 1,
rasterise = FALSE, scattermore = FALSE, summary_fun = "sum", hex = FALSE
))

# If specified, get explained variance
if( add.expl.var ){
# Check if data is available
ind <- names(attributes(reduced_dim)) %in% c("rda", "cca")
# If it can be found
if( any(ind) ){
# Add explained variance
rda <- attributes(reduced_dim)[ind][[1]]
expl_var <- summary(rda)$concont$importance[2, ]*100
} else{
# If it cannot be found, give warning
warning(paste("RDA/CCA object was not found. Please compute",
"RDA/CCA by using runCCA or calculateCCA."),
call. = FALSE)
}
}

# Get scatter plot with plotReducedDim --> keep theme similar between ordination methods
plot <- do.call("plotReducedDim", plotReducedDim_args)
plot <- plotReducedDim(
tse, dimred = dimred, ncomponents = ncomponents, colour_by = colour_by,
percentVar = expl_var, ...)

# Get data for ellipse
ellipse_data <- NULL
Expand Down Expand Up @@ -384,7 +380,7 @@ setMethod("plotRDA", signature = c(object = "matrix"),
# Get those names that are present in data

# If user has not provided vector labels
if( is.null(vec_lab) ){
if( is.null(vec.lab) ){
vector_label <- rownames(vector_data)
# Make labels more tidy
if( all_var_found ){
Expand All @@ -398,12 +394,12 @@ setMethod("plotRDA", signature = c(object = "matrix"),
vector_data$vector_label <- vector_label
} else{
# Check that user-provided labels are correct length
if( length(vec_lab) != nrow(vector_data) ){
if( length(vec.lab) != nrow(vector_data) ){
stop("Number of labels in 'vec_lab' do not match with number of vectors.",
call. = FALSE)
}
# If they are, add labels to data
vector_data$vector_label <- vec_lab
vector_data$vector_label <- vec.lab
}
}

Expand All @@ -422,7 +418,8 @@ setMethod("plotRDA", signature = c(object = "matrix"),
# Get vector labels
vector_label <- vector_data[["vector_label"]]
# Add significance to vector labels
vector_label <- .add_signif_to_vector_labels(vector_label, variable_names, signif_data, ...)
vector_label <- .add_signif_to_vector_labels(
vector_label, variable_names, signif_data, repl.underscore)
vector_data[["vector_label"]] <- vector_label
} else{
# If it cannot be found, give warning
Expand All @@ -435,26 +432,6 @@ setMethod("plotRDA", signature = c(object = "matrix"),
# Create labels for axis
xlab <- paste0(dimred, " 1")
ylab <- paste0(dimred, " 2")
if( add.expl.var ){
# Check if data is available
ind <- names(attributes(reduced_dim)) %in% c("rda", "cca")
# If it can be found
if( any(ind) ){
# Add explained variance
rda <- attributes(reduced_dim)[ind][[1]]
xlab <- paste0(
xlab, " (",
format(round( summary(rda)$concont$importance[2, 1]*100, 1 ), nsmall = 1 ), "%)")
ylab <- paste0(
ylab, " (",
format(round( summary(rda)$concont$importance[2, 2]*100, 1 ), nsmall = 1 ), "%)")
} else{
# If it cannot be found, give warning
warning(paste("RDA/CCA object was not found. Please compute",
"RDA/CCA by using runCCA or calculateCCA."),
call. = FALSE)
}
}

# Create a list to return
result <- list(
Expand All @@ -470,7 +447,7 @@ setMethod("plotRDA", signature = c(object = "matrix"),
# Make vector labels more tidy, i.e, separate variable and group names.
# Replace also underscores with space
.tidy_vector_labels <- function(
vector_label, coldata, sep.group = "\U2012", repl.underscore = " "){
vector_label, coldata, sep.group = "\U2012", repl.underscore = " ", ...){
# Get variable names from sample metadata
var_names <- colnames(coldata)
# Loop through vector labels
Expand All @@ -495,7 +472,8 @@ setMethod("plotRDA", signature = c(object = "matrix"),
}

# This function adds significance info to vector labels
.add_signif_to_vector_labels <- function(vector_label, var_names, signif_data, repl.underscore = " ", ...){
.add_signif_to_vector_labels <- function(
vector_label, var_names, signif_data, repl.underscore = " ", ...){
# Replace underscores from significance data and variable names to match labels
rownames(signif_data) <- sapply(rownames(signif_data), function(x) gsub("_", repl.underscore, x))
var_names <- sapply(var_names, function(x) gsub("_", repl.underscore, x))
Expand All @@ -508,7 +486,7 @@ setMethod("plotRDA", signature = c(object = "matrix"),
paste(!!name, " (",
!!format(
round( signif_data[var_name, "Explained variance"]*100, 1),
nsmall = 1), "%, P = ",
nsmall = 1), "%, ", italic("P"), " = ",
!!gsub("0\\.","\\.", format(
round( signif_data[var_name, "Pr(>F)"], 3),
nsmall = 3)), ")"))
Expand Down Expand Up @@ -609,4 +587,4 @@ setMethod("plotRDA", signature = c(object = "matrix"),
# Add axis labels
plot <- plot + xlab(plot_data$xlab) + ylab(plot_data$ylab)
return(plot)
}
}
6 changes: 2 additions & 4 deletions tests/testthat/test-plotCCA.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ test_that("plot RDA/CCA", {
expect_no_error(plotRDA(tse, "RDA"))

# Wrong-entry scenarios
expect_error(plotRDA(tse, "RDA", colour_by = "wrong colname"),
"'colour_by' must match the name of a column in colData.")
expect_error(plotRDA(tse, "RDA", colour_by = "cohort", shape_by = "wrong colname"),
"'shape_by' must match the name of a column in colData.")
expect_error(plotRDA(tse, "RDA", colour_by = "wrong colname"))
expect_error(plotRDA(tse, "RDA", colour_by = "cohort", shape_by = "wrong colname"))
expect_error(plotRDA(tse, "RDA", add.ellipse = "invalid value"),
"'add.ellipse' must be one of c(TRUE, FALSE, 'fill', 'color', 'colour').",
fixed = TRUE)
Expand Down
Loading