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

Merged
merged 2 commits into from
Aug 14, 2024
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
3 changes: 3 additions & 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 All @@ -18,3 +19,5 @@ import(readxl)
import(stringr)
import(utils)
importFrom(R6,R6Class)
importFrom(stats,IQR)
importFrom(stats,quantile)
12 changes: 12 additions & 0 deletions R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,15 @@ color_codes <-
green_start = "\033[32m",
green_end = "\033[39m"
)


#' Check if a value is an outlier
#'
#' @param x Vector of numeric values from which the outliers are to be detected.
#'
#' @return A logical vector indicating whether each value is an outlier.
#'
#' @importFrom stats IQR quantile
is_outlier <- function(x) {
return(x < quantile(x, 0.25) - 1.5 * IQR(x) | x > quantile(x, 0.75) + 1.5 * IQR(x))
}
71 changes: 71 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,71 @@
#' 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[["Median"]] %>%
dplyr::select(analyte) %>%
dplyr::rename("MFI" = analyte) %>%
dplyr::mutate(
SampleId = paste0("SampleId: ", seq_len(nrow(.))),
SampleType = plate$sample_types,
)

blanks_df <- df %>% dplyr::filter(SampleType == "BLANK")
blank_mean <- mean(blanks_df$MFI)
sc_df <- df %>% dplyr::filter(SampleType == "STANDARD CURVE")
test_df <- df %>% dplyr::filter(SampleType == "TEST")

p <- test_df %>%
dplyr::mutate(
outlier = ifelse(is_outlier(MFI), SampleId, as.character(NA))
) %>%
ggplot2::ggplot(aes(x = SampleType, y = MFI)) +
main_geom(color = "blue") +
ggplot2::geom_text(aes(label = outlier), na.rm = TRUE, hjust = -0.1) +
ggplot2::geom_hline(
aes(yintercept = blank_mean, linetype = "BLANK MEAN"),
color = "dark grey", linewidth = 1
) +
ggplot2::geom_point(data = sc_df, size = 3, color = "red") +
ggplot2::scale_linetype_manual(
name = "Boundries", values = c("BLANK MEAN" = "dashed")
) +
ggplot2::guides(color = ggplot2::guide_legend(title = "Sample Type")) +
ggplot2::ggtitle(
paste0("MFI Boxplot of test sample coverage\n for analyte: ", analyte)
) +
ggplot2::xlab("Sample Type") +
ggplot2::ylab("MFI (Median)") +
ggplot2::theme_minimal() +
ggplot2::theme(
plot.title = element_text(hjust = 0.5) # Center title
)
p
}
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)
}
17 changes: 17 additions & 0 deletions man/is_outlier.Rd

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

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))
})