From faad974bc7af3648ef2f895a9dbebe29573bc4d3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Todor=20Kondi=C4=87?= <todor.kondic@uni.lu>
Date: Tue, 12 Jul 2022 15:58:58 +0200
Subject: [PATCH] app: Sorting compound index works.

---
 R/mix.R           | 19 +++++++++++--------
 R/resources.R     | 10 ++++++++--
 R/shiny-state.R   |  2 +-
 R/shiny-ui-base.R | 30 ++++++++++++++++++------------
 inst/rmd/app.Rmd  |  2 +-
 5 files changed, 39 insertions(+), 24 deletions(-)

diff --git a/R/mix.R b/R/mix.R
index 377aa11..e1afbcd 100644
--- a/R/mix.R
+++ b/R/mix.R
@@ -196,14 +196,17 @@ gen_summ <- function(comp,qa_ms1,qa_ms2) {
     data.table::setcolorder(summ,SUMM_COLS)
 
     ## Quality scores for ms1 and ms2.
-    summ[,qa_ms1 := Map(function(m1,m2,m3) { m1*5L + m2*3L + m3*2L},
-                        as.integer(qa_ms1_exists),
-                        as.integer(qa_ms1_above_noise),
-                        as.integer(qa_ms1_good_int))]
-    summ[,qa_ms2 := Map(function(m1,m2,m3) { m1*5L + m2*3L + m3*2L},
-                        as.integer(qa_ms2_exists),
-                        as.integer(qa_ms2_near),
-                        as.integer(qa_ms2_good_int))]
+    summ[,qa_ms1 := as.integer(Map(function(m1,m2,m3) { m1*5L + m2*3L + m3*2L},
+                                   as.integer(qa_ms1_exists),
+                                   as.integer(qa_ms1_above_noise),
+                                   as.integer(qa_ms1_good_int)))]
+    summ[,qa_ms2 := as.integer(Map(function(m1,m2,m3) { m1*5L + m2*3L + m3*2L},
+                                   as.integer(qa_ms2_exists),
+                                   as.integer(qa_ms2_near),
+                                   as.integer(qa_ms2_good_int)))]
+
+    summ[is.na(qa_ms1),qa_ms1:=0L]
+    summ[is.na(qa_ms2),qa_ms2:=0L]
 
     summ
     
diff --git a/R/resources.R b/R/resources.R
index 2b131ab..0b52be1 100644
--- a/R/resources.R
+++ b/R/resources.R
@@ -284,5 +284,11 @@ SET_LIST_PATT <- CMPD_LIST_PATT
 DFILES_LIST_PATT <- ".*\\.mz[Mm][Ll]$"
 
 CINDEX_BY <- c("set","adduct","tag","ID")
-CINDEX_COLS <- c("mz", "ms1_rt", "Formula","Name")
-ARRANGE_CHOICES <- c(NA_character_,"quality","set", "adduct", "tag", "mz", "rt") 
+CINDEX_COLS <- c("mz", "ms1_rt", "Formula","Name","qa_ms1","qa_ms2")
+ARRANGE_CHOICES <- c(nothing="nothing",
+                     quality="quality",
+                     set="set",
+                     adduct="adduct",
+                     tag="tag",
+                     mz="mz",
+                     rt="ms1_rt")
diff --git a/R/shiny-state.R b/R/shiny-state.R
index d8e3ca3..3a2a252 100644
--- a/R/shiny-state.R
+++ b/R/shiny-state.R
@@ -335,7 +335,7 @@ pre_setup_val_block <- function(gui) {
     T
 }
 
-gen_cindex <- function(summ,cols = CINDEX_COLS,by. = CINDEX_BY, sort=character(0)) {
+gen_cindex <- function(summ,cols = CINDEX_COLS,by. = CINDEX_BY) {
     allc <- c(by.,cols)
     res <- summ[,first(.SD),by=by.][,..allc]
     
diff --git a/R/shiny-ui-base.R b/R/shiny-ui-base.R
index a9fef10..bc8653b 100644
--- a/R/shiny-ui-base.R
+++ b/R/shiny-ui-base.R
@@ -704,29 +704,35 @@ mk_shinyscreen_server <- function(projects,init) {
             m$input$tab$setid
         })
 
-        rf_get_cindex <- eventReactive(rvs$status$is_qa_stat,{
+        rf_get_cindex <- reactive({
+            rvs$status$is_qa_stat
+            s1 <- input$sort1
+            s2 <- input$sort2
+            s3 <- input$sort3
+            s4 <- input$sort4
             summ <- req(rvs$m$out$tab$summ)
+            isolate({
             if (NROW(summ)>0L) {
-                s1 <- input$sort1
-                s2 <- input$sort2
-                s3 <- input$sort3
-                s4 <- input$sort4
                 sortorder <- unique(c(s1,s2,s3,s4))
-                message("----")
-                message(paste0(sortorder,collapse=','))
-                wna <- which(is.na(sortorder)); if (length(wna)>0L) sortorder <- sortorder[-wna]
-                message(paste0(sortorder,collapse=','))
+                wna <- which(sortorder=="nothing"); if (length(wna)>0L) sortorder <- sortorder[-wna]
                 quality <- which("quality"==sortorder)
                 if (length(quality)>0L) {
                     pre <- head(sortorder,quality-1L)
-                    post <- tail(sortorder,quality+1L)
+                    post <- tail(sortorder,-quality)
                     sortorder <- c(pre,"qa_ms1","qa_ms2",post)
                 }
-                message(paste0(sortorder,collapse=','))
-                setorderv(gen_cindex(summ),cols=sortorder)
+
+                
+                ord <- rep(1L,length(sortorder))
+                if ("qa_ms1" %in% sortorder) {
+                    ind <- which(sortorder %in% c("qa_ms1","qa_ms2"))
+                    ord[ind] <- -1L
+                }
+                setorderv(gen_cindex(summ),cols=sortorder,order=ord)
             } else {
                 NULL
             }
+            })
         })
 
         
diff --git a/inst/rmd/app.Rmd b/inst/rmd/app.Rmd
index 007d9b9..c5330eb 100644
--- a/inst/rmd/app.Rmd
+++ b/inst/rmd/app.Rmd
@@ -642,7 +642,7 @@ DT::dataTableOutput("setid_table")
 
 <div style="display: flex; flex-flow: row wrap;">
 
-<div style="width: 25%; padding-right: 1.0em; background-color: var(--lt-gr-bg);">
+<div style="width: 25%; padding-right: 1.0em; background-color: var(--lt-gr-bg); overflow-y: scroll">
 
 <div>
 ### Compound Index
-- 
GitLab