diff --git a/R/api.R b/R/api.R index 39e54934cd407095d6f06a94402c0dd94f6a7b32..3e7120935a61bc0d17381a865bdf78d7776de1dc 100644 --- a/R/api.R +++ b/R/api.R @@ -491,9 +491,57 @@ subset_summary <- function(m) { m } - #' @export create_plots <- function(m) { + ## Empty ms1_plot table. + + + ## Select the data nedeed for plotting. + x <- m$out$tab$flt_summ + ms1_plot_data <- gen_base_ms1_plot_tab(summ=x, + ms1_spec=m$out$tab$ms1_spec) + + group_data <- m$conf$figures$grouping + plot_group <- group_data$group + plot_plot <- group_data$plot + plot_label <- group_data$label + plot_index <- c(plot_group,plot_plot,plot_label) + + ## All the possible curve labels. + all_labels <- x[,unique(.SD),.SDcols=plot_label][[plot_label]] + + ## Plot styling. + style_eic_ms1 <- plot_decor(m,m$conf$logaxes$ms1_eic_int, + all_labels=all_labels, + legend_name=plot_label) + style_eic_ms2 <- plot_decor(m,m$conf$logaxes$ms2_eic_int, + all_labels=all_labels, + legend_name=plot_label) + style_spec_ms2 <- plot_decor(m,m$conf$logaxes$ms2_spec_int, + all_labels=all_labels, + legend_name = plot_label) + + ## Generate MS1 EIC plots. + ms1_plot <- ms1_plot_data[,.(fig={ + df <- .SD[,data.table::rbindlist(Map(function (a,b,c,d) { + s <- a[[1]] + s$plot_label <- b + s$rt_peak <- c + s$mz <- d + s}, + eicMS1, + .SD[[..plot_label]], + rt_peak, + mz))] + list(plot_eic_ms1(df,style_fun = style_eic_ms1, + plot_label = ..plot_label)) + + }),by=c(plot_group,plot_plot)] + m$out$tab$ms1_plot <- ms1_plot + m +} + +create_plots_old <- function(m) { ## Helpers textf <- ggplot2::element_text x <- m$out$tab$ms1_spec diff --git a/R/mix.R b/R/mix.R index 233fa46bb29eb62cb8deb69175a980b984e04e0e..c4e585836afe5761de6182dcb6f818bad478e83b 100644 --- a/R/mix.R +++ b/R/mix.R @@ -1045,3 +1045,70 @@ get_coord_lim <- function(new,def) { if (length(new[[2]])==0) res[[2]]<-def[[2]] res } + + + +gen_base_ms1_plot_tab <- function(summ,ms1_spec) { + + ident <- c("set", + "adduct", + "tag", + "ID", + "mz", + "Files") + + res <- summ[ms1_spec,c(.SD, + list(rt_peak=i.ms1_rt, + eicMS1=lapply(i.eicMS1,list))), + .SDcols=ident, + on=BASE_KEY, + nomatch=NULL] + setkeyv(res,cols=BASE_KEY) + res +} +plot_decor <- function(m,islog,all_labels,legend_name) { + textf <- ggplot2::element_text + + ## Logarithmic, or linear y axis? + scale_y <- if (shiny::isTruthy(islog)) + ggplot2::scale_y_log10 else ggplot2::scale_y_continuous + + rt_new_lim <- c(rt_in_min(m$conf$figures$rt_min), + rt_in_min(m$conf$figures$rt_max)) + rt_lim <- get_coord_lim(rt_new_lim,DEFAULT_RT_RANGE) + + my_theme <- function (...) ggplot2::theme() + + getpal <- colorRampPalette(RColorBrewer::brewer.pal(8,"Dark2")) + + col_all_vals <- getpal(length(all_labels)) + names(col_all_vals) <- all_labels + scale_colour <- function(breaks, labels, ...) ggplot2::scale_colour_manual(values = col_all_vals, + breaks = breaks, + labels = labels, + name = legend_name,...) + + my_coord <- ggplot2::coord_cartesian(xlim = rt_lim) + + function(plot,breaks,labels) plot + my_coord + scale_colour(breaks=breaks, + labels=labels) + scale_y() + my_theme() +} + + +plot_eic_ms1 <- function(df,style_fun,plot_label) { + mk_leg_lab<-function(tag,rt) {if (length(tag) > 0) paste(tag,"; rt= ",formatC(rt,format='f',digits=RT_DIGITS)," min",sep='') else character(0)} + + mz <- df[,unique(mz)] + tbl <- df[,.(verb_labs=mk_leg_lab(plot_label,rt_peak),plot_label), + by=c("plot_label","rt_peak")] + verb_labs <- tbl[,verb_labs] + labs <- tbl[,plot_label] + df[,plot_label:=factor(plot_label)] + style_fun(ggplot2::ggplot(df,ggplot2::aes(x=rt,y=intensity,colour=plot_label)), + breaks=labs, + labels=verb_labs) + + ggplot2::geom_line(key_glyph=KEY_GLYPH) + + ggplot2::labs(x=CHR_GRAM_X, + y=CHR_GRAM_Y) +} + diff --git a/R/resources.R b/R/resources.R index 05bfdcfefea487081369b93301f11f9883e18d9a..6b06bd7807442c3fab8dd133c1ed6b3e3612ab9c 100644 --- a/R/resources.R +++ b/R/resources.R @@ -174,7 +174,7 @@ QA_COLS <- c(QA_FLAGS,QA_NUM_REAL,QA_NUM_INT) MS2_SPEC_COLS <- c("adduct","ID","CE","rt","Files","spec","ms2_max_int") ## MS1 spectral table columns -MS1_SPEC_COLS <- c("adduct","Files","ID","eicMS1","ms1_int","ms1_rt","ms1_mean") +MS1_SPEC_COLS <- c("adduct","ID","eicMS1","ms1_int","ms1_rt","ms1_mean","Files") ## Default sorting keys of spectra in the summary table DEF_KEY_SUMM <- c("set","qa_pass","ms1_int","adduct","mz","tag") @@ -193,3 +193,14 @@ FIG_DEF_SUBSET <- c("set","adduct","ID") REPORT_AUTHOR <- "Anonymous" REPORT_TITLE <- "Plots of EICs and MS2 Spectra" + + +PLOT_FEATURES <- c("set", + "adduct", + "tag", + "ID", + "CE") + +## Select the most fundamental group of entries. Within this group, +## each ID is unique. +BASE_KEY <- c("adduct","Files","ID")