From 32e64b9847e2a6d9f4b2d0e8150733966692b34e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Todor=20Kondi=C4=87?= <kontrapunkt@uclmail.net> Date: Sun, 18 Dec 2022 15:01:10 +0100 Subject: [PATCH] api, mix, plotting: Fix unknown SMILES/Formulas plots. --- DESCRIPTION | 2 +- NAMESPACE | 1 - R/api.R | 29 ++++------------------------- R/mix.R | 6 +++++- R/plotting.R | 31 ++++++++++++++++++++++++------- R/shiny-ui-base.R | 11 ++--------- 6 files changed, 36 insertions(+), 44 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index b94ce0a..1953c5f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: shinyscreen Title: Pre-screening of Mass Spectrometry Data -Version: 1.2.1 +Version: 1.2.2 Author: Todor Kondić Maintainer: Todor Kondić <todor.kondic@uni.lu> Authors@R: diff --git a/NAMESPACE b/NAMESPACE index fe8b0e5..ec49d63 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -7,7 +7,6 @@ export(create_plots) export(create_stub_gui) export(extr_data) export(extract) -export(gen_struct_plots) export(get_fn_comp) export(get_fn_conf) export(get_fn_extr) diff --git a/R/api.R b/R/api.R index 65d7014..20a3fe3 100644 --- a/R/api.R +++ b/R/api.R @@ -192,6 +192,10 @@ mk_comp_tab <- function(m) { } smiforadd <- smiles[smiforadd,.(ID,SMILES,Formula,adduct),on=c("SMILES")] data.table::setkey(smiforadd,"adduct","ID") + + ## FIXME: Why is Formula a list when there are no SMILES, instead + ## of an empty string? + smiforadd[,Formula:=as.character(Formula)] ## Update the intermediate table with masses. message("Formulas have been calculated. Start calculating masses from formulas.") @@ -611,31 +615,6 @@ subset_summary <- function(m) { m } - -##' @export -gen_struct_plots <- function(m) { - ## Generate structure plots. - comp <- m$out$tab$comp - - res <- if (NROW(comp)>0) { - structtab <- m$out$tab$comp[known=="structure",unique(.SD),.SDcols=c("ID","SMILES")] - message("Start generating structures.") - if (NROW(structtab)>0) { - structtab[,img:=.({tmp <- lapply(SMILES,function (sm) smiles2img(sm,width = 500,height = 500, zoom = 4.5)) - tmp})] - message("Done generating structures.") - structtab - } else dtable(ID=character(0),SMILES=character(0),img=list()) - } else { - dtable(ID=character(0),SMILES=character(0),img=list()) - } - - m$out$tab$structfig <- res - - m -} - - #' @export create_plots <- function(m) { ## Produce plots of EICs and spectra and group them acording to diff --git a/R/mix.R b/R/mix.R index 69176c7..aa88970 100644 --- a/R/mix.R +++ b/R/mix.R @@ -132,6 +132,9 @@ calc_mz_from_formula <- function(chform,adduct,id) { smiles2form <- function(smiles) { + res <- character(length(smiles)) + res <- NA_character_ + isomething <- which(!is.na(smiles) | nchar(smiles) > 0L) one2form <- function (s) { mol <- try(RMassBank::getMolecule(s), silent = T) if (!is.atomic(mol)) { @@ -139,7 +142,8 @@ smiles2form <- function(smiles) { } else "" } - sapply(smiles,one2form,USE.NAMES = F) + res[isomething] <- sapply(smiles[isomething],one2form,USE.NAMES = F) + res } diff --git a/R/plotting.R b/R/plotting.R index 0f1004d..e98256f 100644 --- a/R/plotting.R +++ b/R/plotting.R @@ -101,18 +101,35 @@ pal_maker <- function(n,palname = NULL) { } ### PLOTTING: AESTHETIC FUNCTIONS -smiles2img <- function(smiles, kekulise=TRUE, width=300, height=300, + + +make_struct_plot <- function(smiles, kekulise=TRUE, width=300, height=300, zoom=1.3,style="cow", annotate="off", abbr="on",suppressh=TRUE, showTitle=FALSE, smaLimit=100, sma=NULL) { - dep <- rcdk::get.depictor(width = width, height = height, zoom = zoom, style = style, annotate = annotate, - abbr = abbr, suppressh = suppressh, showTitle = showTitle, smaLimit = smaLimit, - sma = NULL) - mol <- RMassBank::getMolecule(smiles) - z<-rcdk::view.image.2d(mol, depictor=dep) - grid::rasterGrob(z) + ## structure -> grob + smiles2img <- function() { + if (is.na(smiles) || nchar(smiles)==0) return(NULL) #Handle empty SMILES. + dep <- rcdk::get.depictor(width = width, height = height, zoom = zoom, style = style, annotate = annotate, + abbr = abbr, suppressh = suppressh, showTitle = showTitle, smaLimit = smaLimit, + sma = NULL) + + mol <- RMassBank::getMolecule(smiles) + z<-rcdk::view.image.2d(mol, depictor=dep) + grid::rasterGrob(z) + } + + grob <- smiles2img() + + if (!is.null(grob)) { + qplot(1:5, 2*(1:5), geom="blank") + + annotation_custom(grob, xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) + + theme_empty + } else NULL + } + guide_fun <- function() { ## ggplot2::guides(colour=ggplot2::guide_legend(nrow=2, ## byrow=T, diff --git a/R/shiny-ui-base.R b/R/shiny-ui-base.R index c730e22..05702e1 100644 --- a/R/shiny-ui-base.R +++ b/R/shiny-ui-base.R @@ -871,11 +871,7 @@ mk_shinyscreen_server <- function(projects,init) { row <- req(input$cindex_row_last_clicked) id <- rowtab <- cind[row][,..key][["ID"]][[1]] smi <- rvs$m$out$tab$comp[ID==(id),SMILES][[1]] - grb <- smiles2img(smi) - xx <- qplot(1:5, 2*(1:5), geom="blank") + - annotation_custom(grb, xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) + - theme_empty - xx + make_struct_plot(smi) }) rf_plot_spec_ms2 <- reactive({ @@ -1260,10 +1256,7 @@ mk_shinyscreen_server <- function(projects,init) { id <- rowtab <- cind[ri][,..key][["ID"]][[1]] smi <- rvs$m$out$tab$comp[ID==(id),SMILES][[1]] - grb <- smiles2img(smi) - p_struc <- qplot(1:5, 2*(1:5), geom="blank") + - annotation_custom(grb, xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) + - theme_empty + p_struc <- make_struct_plot(smi) p_spec <- make_spec_ms2_plot(ms2, summ, -- GitLab