Skip to content

Commit

Permalink
[FEAT] - scale and center option added to the PCA box
Browse files Browse the repository at this point in the history
  • Loading branch information
leopoldguyot committed May 5, 2024
1 parent e9f67c3 commit fc815a0
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
5 changes: 5 additions & 0 deletions R/interface_module_box_readscp.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ box_readscp_ui <- function(id) {
label = "Convert zeros to NA",
value = TRUE
),
checkboxInput(
inputId = NS(id, "singlecell"),
label = "Single cell data",
value = FALSE
),
actionButton(
inputId = NS(id, "convert"),
"Convert to a QFeatures object"
Expand Down
10 changes: 10 additions & 0 deletions R/interface_module_qc_metrics.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ interface_module_pca_box <- function(id, title) {
label = "Color by",
choices = NULL
),
checkboxInput(
inputId = NS(id, "scale"),
label = "Scale data",
value = TRUE
),
checkboxInput(
inputId = NS(id, "center"),
label = "Center data",
value = TRUE
),
checkboxInput(
inputId = NS(id, "show_legend"),
label = "Show Legend",
Expand Down
7 changes: 5 additions & 2 deletions R/server_module_qc_metrics.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ server_module_qc_metrics <- function(id, assays_to_process, type) {
#' @importFrom pcaMethods pca scores
#' @importFrom methods is
#'
server_module_pca_box <- function(id, single_assay, method, transpose) {
server_module_pca_box <- function(id, single_assay, method, transpose) {
moduleServer(id, function(input, output, session) {
annotation_names <- reactive({
req(single_assay())
Expand Down Expand Up @@ -104,12 +104,15 @@ server_module_pca_box <- function(id, single_assay, method, transpose) {
return(df)
})


pca_result <- reactive({
req(single_assay())
pcaMethods_wrapper(
single_assay(),
method = method,
transpose = transpose
transpose = transpose,
scale = input$scale,
center = input$center
)
})
dataframe <- reactive({
Expand Down
2 changes: 1 addition & 1 deletion R/server_module_summary_tab.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ server_module_summary_tab <- function(id) {

output$download_qfeatures <- downloadHandler(
filename = function() {
"scp_qfeature_object.rds"
"qfeatures_object.rds"
},
content = function(file) {
saveRDS(global_rv$qfeatures, file)
Expand Down
10 changes: 8 additions & 2 deletions R/utils_global.R
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ remove_scpGUI <- function(string) {
#'
#' @param sce A SingleCellExperiment object. The PCA is performed on the assay of this object.
#' @param method A character string specifying the PCA method to use. This should be one of the methods supported by the pcaMethods package.
#' @param center A logical indicating whether the variables should be centered before PCA.
#' @param scale A logical indicating whether the variables should be scaled before PCA.
#'
#' @return A pcaRes object resulting from the PCA.
#' @rdname INTERNAL_pcaMethods_wrapper
Expand All @@ -174,15 +176,19 @@ remove_scpGUI <- function(string) {
#' @importFrom pcaMethods pca
#' @importFrom SummarizedExperiment assay
#'
pcaMethods_wrapper <- function(sce, method, transpose = FALSE) {
pcaMethods_wrapper <- function(sce, method, center, scale, transpose = FALSE) {
mat <- assay(sce)
mat <- mat[rowSums(is.na(mat)) != ncol(mat), ]
mat <- mat[, colSums(is.na(mat)) < nrow(mat)]
if (scale) {
mat <- scale(mat)
}
if (transpose) {
mat <- t(mat)
}
pca <- pcaMethods::pca(mat,
method = method
method = method,
center = center
)
pca
}
Expand Down

0 comments on commit fc815a0

Please sign in to comment.