R/plot_lmx_qc.R

Defines functions plot_lmx_qc

Documented in plot_lmx_qc

#'plot_lmx_qc (Generate QC plots pre and post normalization)
#'@description normalization QC plots, generate
#'             1) analyt/pre vs post scatter plot, colored by plates
#'             2) optional to select a analyt of choice plot for
#'                pre vs post npx by plates scatter plots
#'             3) optional plot to track Randox behavior pre and post normalization
#'                if randox_info is given plot the targeted value
#'@param normed_se a summarizedexperiment, normaly generated by cmb_npx_sc() after normalization
#'@param bridge_pattern regex to identify bridging samples
#'@param fields colData name from which to identify bridging samples uing the bridge_pattern
#'@export
#'@md
#'
plot_lmx_qc <- function(normed_se, bridge_pattern= "HD Urine Pool", fields = "Sample"){
  
  bridge <- normed_se[, grep(bridge_pattern, normed_se[[fields]])] 
  
  assay_ls <- names(bridge@assays@data)
  bridge_data_ls <- lapply(assay_ls, function(x){
    bridge@assays@data[[x]] %>%
      data.frame()%>%
      set_colnames(value = bridge$File)%>%
      rownames_to_column(var = "Analyt") %>%
      gather(-Analyt, key = "f_name", value = "value")%>%
      mutate(Assay = x,
             f_name = gsub("^.*/", "", f_name),
             value = ifelse(!grepl("cv", Assay), log10(value), value))
  })
  bridge_data_ls <- do.call(rbind, bridge_data_ls)%>%
    mutate(f_name = gsub("Results.*$", "", f_name))
  
  normed_se_ls <- lapply(assay_ls, function(x){
    normed_se@assays@data[[x]] %>%
      data.frame()%>%
      set_colnames(value = normed_se$File)%>%
      rownames_to_column(var = "Analyt") %>%
      gather(-Analyt, key = "f_name", value = "value")%>%
      mutate(Assay = x,
             f_name = gsub("^.*/", "", f_name),
             value = ifelse(!grepl("cv", Assay), log10(value), value))
  })
  normed_se_ls <- do.call(rbind, normed_se_ls)%>%
    mutate(f_name = gsub("Results.*$", "", f_name))
  
  p1_1 <- bridge_data_ls %>%
    filter(Assay != "cv")%>%
    separate(col = "Assay", into = c("Assay", "Status"))%>%
    ggplot()+
    geom_point(aes(value, Analyt, color = f_name), shape = 16, size = 6, alpha = 0.7)+
    labs(title = bridge_pattern,
         x = "value (plotted in log10 scale)")+
    facet_wrap(Status ~ Assay)+
    theme_bw()+
    theme(legend.position = "none")
  
  p0_1 <- bridge_data_ls %>%
    filter(Assay == "cv")%>%
    ggplot()+
    geom_point(aes(value, Analyt, color = f_name), shape = 16, size = 6, alpha = 1)+
    labs(title = bridge_pattern)+
    theme_bw()+
    theme(legend.position = "bottom")
  
  p0_2 <- normed_se_ls %>%
    filter(Assay == "cv")%>%
    ggplot()+
    geom_point(aes(value, Analyt, color = f_name), shape = 21, position = "jitter")+
    labs(title = "Sample CV Reference")+
    theme_bw()+
    theme(legend.position = "none")
  
  p0_2+p0_1+plot_layout(widths = c(2, 2))+p1_1
  
}
ismms-himc/LuminexTools documentation built on July 2, 2024, 2:08 a.m.