Skip to content
36 changes: 31 additions & 5 deletions R/dimension_reduction.R
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ plot_pca <- function(data, design = NULL, plot_colors = NULL, plot_title = TRUE,
plot_size = 5, plot_alpha = NULL, plot_labels = FALSE, size_column = NULL,
pc_method = "fast_svd", x_pc = 1, y_pc = 2, max_overlaps = 20,
num_pc = NULL, expt_names = NULL, label_chars = 10,
cond_column = "condition", batch_column = "batch",
cond_column = "condition", batch_column = "batch", hex = TRUE,
...) {
arglist <- list(...)
## Set default columns in the experimental design for condition and batch
Expand Down Expand Up @@ -1036,6 +1036,9 @@ plot_pca <- function(data, design = NULL, plot_colors = NULL, plot_title = TRUE,
} else {
## Leave the title blank.
}

## If want a hexagonal heatmap
#if(hexagonal) comp_plot + geom_hex()

## Finally, return a list of the interesting bits of what happened.
pca_return <- list(
Expand Down Expand Up @@ -1748,11 +1751,34 @@ plot_pcs <- function(pca_data, first = "PC1", second = "PC2", variances = NULL,
guide = ggplot2::guide_legend(override.aes = list(size = plot_size, fill = "grey")),
values = 21:25)
} else if (is.null(size_column) && num_batches > 5) {
### cannot mix integer with characters even using list
### 19 removed because it looks like 16, which might not be needed since shapes are reused for batches more than 25
#symbols = c(0:18,20:25)
symbols = c(0:25)
if(length(symbols) < num_batches){
idx = c(rep(1:length(symbols),
num_batches %/% length(symbols)),
1:(num_batches %% length(symbols)))
values = symbols[idx]
}else{
values = symbols[1:num_batches]
}

## change outline color to black for batches 21+
#if(num_batches > 20){
# cond20 = unique(pca_data[['condition']])[1:20]
# idx = which(pca_data[['condition']] %in% cond20)
# othern = nrow(pca_data) - length(idx)
## outline_col = c(pca_data[['condition']][idx],rep('black',othern))
# }else{
# outline_col = pca_data[['condition']]
# }

pca_plot <- pca_plot +
ggplot2::geom_point(size = plot_size, alpha = plot_alpha,
aes(shape = .data[["batch"]],
fill = .data[["condition"]],
colour = .data[["condition"]])) +
colour = outline_col)) +
ggplot2::scale_color_manual(name = "Condition",
##guide = ggplot2::guide_legend(override.aes = list(size = plot_size)),
guide = "legend",
Expand All @@ -1764,7 +1790,7 @@ plot_pcs <- function(pca_data, first = "PC1", second = "PC2", variances = NULL,
ggplot2::scale_shape_manual(name = "Batch",
labels = levels(as.factor(pca_data[["batch"]])),
guide = ggplot2::guide_legend(overwrite.aes = list(size = plot_size)),
values = 1:num_batches)
values = values)
} else if (!is.null(size_column) && num_batches <= 5) {
## This will require the 6 steps above and one more
pca_plot <- ggplot(data = as.data.frame(pca_data),
Expand Down Expand Up @@ -1804,14 +1830,14 @@ plot_pcs <- function(pca_data, first = "PC1", second = "PC2", variances = NULL,
values = as.numeric(levels(pca_data[["size"]])))
} else if (!is.null(size_column) && num_batches > 5) {
pca_plot <- pca_plot +
ggplot2::geom_point(alpha = plot_alpha,
ggplot2::geom_point(alpha = plot_alpha,
aes(shape = .data[["batch"]],
colour = .data[["condition"]],
size = .data[["size"]])) +
ggplot2::scale_shape_manual(name = "Batch",
labels = levels(as.factor(pca_data[["batch"]])),
guide = ggplot2::guide_legend(overwrite.aes = list(size = plot_size)),
values = 1:num_batches) +
values = values) +
ggplot2::scale_color_manual(name = "Condition",
guide = "legend",
values = color_list) +
Expand Down
8 changes: 4 additions & 4 deletions R/normalize_batch.R
Original file line number Diff line number Diff line change
Expand Up @@ -310,11 +310,11 @@ all_adjusters <- function(input, design = NULL, estimate_type = "sva", batch1 =
mesg("Attempting fsva surrogate estimation with ",
chosen_surrogates, " ", sword, ".")
type_color <- "darkred"
sva_object <- sm(sva::sva(log2_mtrx, conditional_model,
null_model, n.sv = chosen_surrogates))
surrogate_result <- sva::fsva(log2_mtrx, conditional_model,
sva_object <- sink(sm(sva::sva(log2_mtrx, conditional_model,
null_model, n.sv = chosen_surrogates)))
surrogate_result <- sink(sva::fsva(log2_mtrx, conditional_model,
sva_object, newdat = as.matrix(log2_mtrx),
method = "exact")
method = "exact"))
model_adjust <- as.matrix(surrogate_result[["newsv"]])
source_counts <- surrogate_result[["new"]]
},
Expand Down
Loading