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

Implement boxplot for MFI #69

Open
wants to merge 2 commits into
base: feat/refactor
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export(PlateBuilder)
export(create_standard_curve_model_analyte)
export(is_valid_data_type)
export(is_valid_sample_type)
export(plot_mfi_for_analyte)
export(plot_standard_curve_analyte)
export(plot_standard_curve_analyte_with_model)
export(predict_dilutions)
Expand Down
5 changes: 5 additions & 0 deletions R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,8 @@ color_codes <-
green_start = "\033[32m",
green_end = "\033[39m"
)


is_outlier <- function(x) {
return(x < quantile(x, 0.25) - 1.5 * IQR(x) | x > quantile(x, 0.75) + 1.5 * IQR(x))
}
57 changes: 57 additions & 0 deletions R/plots-mfi.R
nizwant marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#' Plot MFI value distribution for a given analyte
#'
#' @param plate A plate object
#' @param analyte The analyte to plot
#' @param data_type The type of data to plot. Default is "Median"
#' @param plot_type The type of plot to generate. Default is "boxplot".
#' Available options are "boxplot" and "violin".
#'
#' @return A ggplot object
#'
#' @import dplyr
#' @import ggplot2
#'
#' @export
plot_mfi_for_analyte <- function(plate, analyte,
data_type = "Median", plot_type = "boxplot") {
if (!(analyte %in% plate$analyte_names)) {
stop("Analyte not found in the plate")
}
if (!is_valid_data_type(data_type)) {
stop("Datatype not supported.")
}

main_geom <- switch(plot_type,
"boxplot" = ggplot2::geom_boxplot,
"violin" = ggplot2::geom_violin,
{
stop("Plot type not supported. Use either 'boxplot' or 'violin'")
}
)

df <- plate$data[[data_type]]
df %>%
dplyr::select(analyte) %>%
dplyr::rename("MFI" = analyte) %>%
dplyr::mutate(
SampleId = paste0("SampleId: ", seq_len(nrow(.))),
SampleTypes = plate$sample_types,
) %>%
dplyr::group_by(SampleTypes) %>%
dplyr::mutate(
outlier = ifelse(is_outlier(MFI), SampleId, as.character(NA))
) %>%
ggplot2::ggplot(aes(x = SampleTypes, y = MFI)) +
main_geom() +
ggplot2::geom_text(aes(label = outlier), na.rm = TRUE, hjust = -0.1) +
ggplot2::xlab("Sample Types") +
ggplot2::ylab("MFI (Median)") +
ggplot2::ggtitle(
paste0("Boxplot of MFI per sample type for analyte: ", analyte)
) +
ggplot2::theme_minimal() +
ggplot2::theme(
plot.title = element_text(hjust = 0.5), # Center title
axis.title.x = element_text(margin = margin(t = 10)) # Adjust x-axis title
)
}
2 changes: 0 additions & 2 deletions R/standard_curves.R
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,4 @@ plot_standard_curve_analyte_with_model <- function(plate, analyte_name, model, d
yintercept = bottom_asymptote, linetype = "dashed", color = "gray"
)
}

print(p)
}
29 changes: 29 additions & 0 deletions man/plot_mfi_for_analyte.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion tests/testthat/test-integration.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ library(testthat)
test_that("Fully Parse CovidOISExPONTENT.csv plate data", {
path <- system.file("extdata", "CovidOISExPONTENT.csv", package = "PvSTATEM", mustWork = TRUE)
expect_no_error(plate <- read_luminex_data(path, format = "xPONENT"))

expect_no_error(p <- plot_mfi_for_analyte(plate, "S2"))
expect_true(inherits(p, "ggplot"))

expect_warning(plate <- plate$blank_adjustment(in_place = FALSE))
expect_equal(plate$blank_adjusted, TRUE)
})

test_that("Fully Parse CovidOISExPONTENT_CO.csv plate data with layout", {
path <- system.file("extdata", "CovidOISExPONTENT_CO.csv", package = "PvSTATEM", mustWork = TRUE)
layout_path <- system.file("extdata", "CovidOISExPONTENT_CO_layout.xlsx", package = "PvSTATEM", mustWork = TRUE)
expect_no_error(read_luminex_data(path, format = "xPONENT", layout_filepath = layout_path))
expect_no_error(plate <- read_luminex_data(path, format = "xPONENT", layout_filepath = layout_path))
})