From be2fe1a6bfd0f54b17d3e7fc37ddcdf3b881b0c8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Todor=20Kondi=C4=87?= <todor.kondic@uni.lu>
Date: Fri, 15 Jul 2022 16:23:29 +0200
Subject: [PATCH] ...

---
 R/plotting.R      | 47 ++++++++++++++++++++++++++---------------------
 R/resources.R     |  5 +++--
 R/shiny-ui-base.R |  8 ++++++--
 3 files changed, 35 insertions(+), 25 deletions(-)

diff --git a/R/plotting.R b/R/plotting.R
index 55a3c15..5d0eb90 100644
--- a/R/plotting.R
+++ b/R/plotting.R
@@ -397,17 +397,28 @@ get_data_from_key <- function(tab,key) {
     eval(bquote(tab[.(skey)]))
 }
 
+
+get_label_group <- function(key) {
+    message('CINDEX:',paste0(CINDEX_BY,coll=','))
+    message('key:', paste0(key,coll=','))
+    setdiff(CINDEX_BY,key)
+}
+
 make_line_label <- function(...) {
     paste(...,sep="; ")
 }
 
 ## Prepare MS1 eic data: rt and intensity + key made of splitby.
-get_data_4_eic_ms1 <- function(extr_ms1,adduct,id,splitby) {
-    key <- list(adduct=adduct,ID=id)
-    tab <-get_data_from_key(tab=extr_ms1,key=key)
-    pdata <- tab[,.(rt,intensity),by=c('ID',splitby)]
-    pdata <- eval(bquote(pdata[,label:=make_line_label(..(lapply(splitby,as.symbol))),by=.(splitby)],splice=T))
-    setkeyv(pdata,cols=c("ID",splitby,"rt"))
+get_data_4_eic_ms1 <- function(extr_ms1,key) {
+    actual_key <- key[intersect(names(key),names(extr_ms1))]
+    label_group <- intersect(get_label_group(names(key)),names(extr_ms1))
+    tab <-get_data_from_key(tab=extr_ms1,key=actual_key)
+    label_group <- as.list(label_group)
+    names(label_group) <- NULL
+    pdata <- tab[,.(rt,intensity),by=label_group]
+
+    pdata <- eval(bquote(pdata[,label:=make_line_label(..(lapply(label_group,as.symbol))),by=(label_group)],splice=T))
+    setkeyv(pdata,cols=unique(c("ID",as.character(label_group),"rt")))
     pdata
 }
 
@@ -423,16 +434,11 @@ get_data_4_eic_ms2 <- function(summ,adduct,id,splitby) {
 
 
 
-make_eic_ms1_plot <- function(extr_ms1,summ,set,adduct,id,splitby,axis="linear",rt_range=NULL) {
-    key <- list(set=set,
-                adduct=adduct,
-                ID=id)
+make_eic_ms1_plot <- function(extr_ms1,summ,key,axis="linear",rt_range=NULL) {
 
+   
     ## Get the table with ms1 data.
-    pdata <- get_data_4_eic_ms1(extr_ms1,
-                                adduct=adduct,
-                                id=id,
-                                splitby=splitby)
+    pdata <- get_data_4_eic_ms1(extr_ms1, key)
 
     ## Get metadata.
     summ_row  <- get_data_from_key(summ,key=key)
@@ -452,8 +458,8 @@ make_eic_ms1_plot <- function(extr_ms1,summ,set,adduct,id,splitby,axis="linear",
     title_txt = paste0("MS1 EIC for ion m/z = ",paste0(signif(unique(summ_row$mz),digits=7L),collapse=", "))
     nm <- paste(unique(summ_row$Name),collapse="; ")
     subt_txt = if (!length(nm)==0L && !is.na(nm) && nchar(nm)>0L) nm else NULL
-    p <- ggplot2::ggplot(pdata,aes(x=rt,y=intensity,colour=label))+ggplot2::labs(caption=tag_txt,title=title_txt,subtitle=subt_txt)+ggplot2::xlab("retention time")+ggplot2::geom_line()+ggplot2::coord_fixed(ratio=aspr)+scale_y(axis=axis,labels=sci10)+rt_lim
-
+    p <- ggplot2::ggplot(pdata,aes(x=rt,y=intensity,colour=label))+ggplot2::labs(caption=tag_txt,title=title_txt,subtitle=subt_txt)+ggplot2::xlab("retention time")+ggplot2::geom_line()+scale_y(axis=axis,labels=sci10)+rt_lim
+    ## +ggplot2::coord_fixed(ratio=aspr)
     annt_dx <- 5*dx/100.
     annt <- summ[summ_row,on=names(key),nomatch=NULL][,.(x=..annt_dx+ms1_rt,y=ms1_int,txt=signif(ms1_rt,5))]
 
@@ -465,7 +471,7 @@ make_eic_ms1_plot <- function(extr_ms1,summ,set,adduct,id,splitby,axis="linear",
 }
 
 
-make_eic_ms2_plot <- function(summ,set,adduct,id,splitby,axis="linear",rt_range=NULL) {
+make_eic_ms2_plot <- function(summ,key,splitby,axis="linear",rt_range=NULL) {
     key <- list(set=set,
                 adduct=adduct,
                 ID=id)
@@ -497,8 +503,8 @@ make_eic_ms2_plot <- function(summ,set,adduct,id,splitby,axis="linear",rt_range=
     p <- ggplot2::ggplot(pdata,aes(x=rt,ymin=0,ymax=intensity,colour=label)) +
         ggplot2::labs(caption=tag_txt,title=title_txt,subtitle=subt_txt) +
         ggplot2::xlab("retention time")+ggplot2::ylab("intensity")+ggplot2::geom_linerange()+
-        ggplot2::coord_fixed(ratio=aspr)+scale_y(axis=axis,labels=sci10)+rt_lim
-
+        scale_y(axis=axis,labels=sci10)+rt_lim
+    ## ggplot2::coord_fixed(ratio=aspr)+
     ans <- pdata[,unique(an)]
     annt_dx <- 5*dx/100.
     annt <- summ[an %in% (ans),.(an=an,x=ms2_rt+..annt_dx,y=1.1*ms2_int,txt=signif(ms2_rt,5))]
@@ -545,8 +551,7 @@ make_spec_ms2_plot <- function(extr_ms2,summ,set,adduct,id,splitby,axis="linear"
 }
 
 combine_plots <- function(p_eic_ms1,p_eic_ms2) {
-    pl <- list(p_eic_ms1,p_eic_ms2)
-    cowplot::plot_grid(pl,ncol=1,align='v')
+    cowplot::plot_grid(p_eic_ms1,p_eic_ms2,ncol=1,align='v',axis='b')
 }
 
     
diff --git a/R/resources.R b/R/resources.R
index a691fad..35f3481 100644
--- a/R/resources.R
+++ b/R/resources.R
@@ -283,11 +283,12 @@ CMPD_LIST_PATT <- "((*.csv)|(*.csv.gz))$"
 SET_LIST_PATT <- CMPD_LIST_PATT
 DFILES_LIST_PATT <- ".*\\.mz[Mm][Ll]$"
 
-CINDEX_BY <- c("set","ID","adduct","tag")
+CINDEX_BY <- c("set","ID","tag","adduct")
 CINDEX_COLS <- c("mz", "ms1_rt","Name","qa_ms1","qa_ms2")
 ARRANGE_CHOICES <- c(nothing="nothing",
                      quality="quality",
                      set="set",
                      adduct="adduct",
                      mz="mz",
-                     rt="rt")
+                     rt="rt",
+                     id="ID")
diff --git a/R/shiny-ui-base.R b/R/shiny-ui-base.R
index 985951a..f833950 100644
--- a/R/shiny-ui-base.R
+++ b/R/shiny-ui-base.R
@@ -704,9 +704,13 @@ mk_shinyscreen_server <- function(projects,init) {
             m$input$tab$setid
         })
 
+        rf_cindex_key <- reactive({
+            if (isTruthy(input$cindex_group)) setdiff(CINDEX_BY,input$cindex_group) else CINDEX_BY
+        })
+
         rf_get_cindex <- reactive({
             rvs$status$is_qa_stat
-            grp <- if (isTruthy(input$cindex_group)) setdiff(CINDEX_BY,input$cindex_group) else CINDEX_BY
+            grp <- rf_cindex_key()
             s1 <- input$sort1
             s2 <- input$sort2
             s3 <- input$sort3
@@ -1183,7 +1187,7 @@ mk_shinyscreen_server <- function(projects,init) {
             validate(need(NROW(tab)>0L,message="Need to prescreen, first."))
             DT::datatable(tab,
                           rownames=NULL,
-                          options=list(filter=T),
+                          options=list(filter=T,ordering=F,dom='t'),
                           selection="single")
         })
 
-- 
GitLab