From 0a3fe7aeea76a8424da3a310fa8a8449cd4f28aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Todor=20Kondi=C4=87?= <kontrapunkt@uclmail.net> Date: Sat, 15 Apr 2023 12:35:24 +0200 Subject: [PATCH] Adapted `mf_narrow_summ' and `pack_ms2_w_summ' to new data model. Also adapted metfrag subsystem to new data model. --- R/api.R | 2 +- R/metfrag.R | 36 ++++++++++++++++++------------------ R/resources.R | 2 +- R/shiny-ui-base.R | 5 +++-- R/state.R | 8 +++++--- 5 files changed, 28 insertions(+), 25 deletions(-) diff --git a/R/api.R b/R/api.R index b29adef..239060c 100644 --- a/R/api.R +++ b/R/api.R @@ -783,7 +783,7 @@ metfrag <- function(m) { path = m$run$metfrag$path, subpaths = m$run$metfrag$subpaths, db_file = m$run$metfrag$db_file, - stag_tab = stagtab, ms2 = m$db$extr$cgm$ms2, + stag_tab = stagtab, ms2 = m$db$extr$spectra, runtime=m$run$metfrag$runtime, java_bin=m$run$metfrag$java_bin, nproc = m$conf$metfrag$nproc) diff --git a/R/metfrag.R b/R/metfrag.R index cfab5a1..50d5cfc 100644 --- a/R/metfrag.R +++ b/R/metfrag.R @@ -36,7 +36,7 @@ metfrag_get_stag_tab <- function(summ) { ## Argument summ can be a subset of actual `summ' table. x = gen_1d_keytab(summ) data.table::setnames(x,old="key1d",new="stag") - res = x[summ,`:=`(CE=i.CE,ion_mz=mz)] + res = x[summ,`:=`(ce=i.ce,ion_mz=mz)] res } @@ -54,18 +54,21 @@ get_mf_res_ext <- function(fn) { metfrag_run <- function(param,path,subpaths,db_file,stag_tab,ms2,runtime,java_bin,nproc = 1L) { keys = intersect(colnames(stag_tab),colnames(ms2)) + rms2 = ms2[stag_tab,on=keys,nomatch=NULL] message("Generating MetFrag configs.") - file_tab = ms2[stag_tab,{ + + + file_tab = rms2[,{ r = write_metfrag_config(param = ..param, path = ..path, subpaths = ..subpaths, db_file = ..db_file, - stag = stag, - adduct = adduct, - ion_mz = ion_mz, + stag = first(stag), + adduct = first(adduct), + ion_mz = first(ion_mz), spec = data.table(mz=mz,intensity=intensity)) - c(r,stag = stag) - },by=.EACHI,on=keys] + c(r,stag = first(stag)) + },keyby=keys] message("Done generating MetFrag configs.") withr::with_dir(path,{ @@ -89,21 +92,18 @@ metfrag_run <- function(param,path,subpaths,db_file,stag_tab,ms2,runtime,java_bi mf_narrow_summ <- function(summ,kv,ms2_rt_i=NA_integer_,ms2_rt_f=NA_integer_) { skey = data.table::key(summ) - cols = c("adduct","tag","ID","CE","scan","mz","qa_pass","ms2_rt") - nsumm = get_rows_from_summ(summ,kv,cols) + cols = union(names(skey),c("adduct","tag","ID","ce","precid","scan","mz","qa_pass","ms2_rt")) + dtkv = as.data.table(kv) + nsumm = summ[dtkv,on=names(kv),.SD,.SDcols=cols] nsumm = nsumm[qa_pass==T] # Those that make sense. - nsumm_key = union(SUMM_KEY,"ms2_rt") + nsumm_key = intersect(union(SUMM_KEY,"ms2_rt"),colnames(nsumm)) data.table::setkeyv(nsumm,nsumm_key) - if (!is.na(ms2_rt_i)) { - nsumm = nsumm[ms2_rt>(ms2_rt_i)] - } + ms2_rt_i = if (!is.na(ms2_rt_i)) ms2_rt_i else 0. + ms2_rt_f = if (!is.na(ms2_rt_f)) ms2_rt_f else Inf - if (!is.na(ms2_rt_f)) { - nsumm = nsumm[ms2_rt<(ms2_rt_f)] - } + nsumm[ms2_rt > (ms2_rt_i) & ms2_rt < (ms2_rt_f)] - nsumm } get_metfrag_targets <- function(stag_tab,ms2) { @@ -220,7 +220,7 @@ summarise_metfrag_results <- function(param,path,subpaths,cand_parameters,db_sco } .adapt_col_types <- function(x) { - x[,(names(db_scores)):=lapply(.SD, as.numeric),.SDcol=names(db_scores)] + if (length(db_scores)>0) x[,(names(db_scores)):=lapply(.SD, as.numeric),.SDcol=names(db_scores)] else x } .calc_basic_scores <- function(x) { diff --git a/R/resources.R b/R/resources.R index 4cd7360..13688b4 100644 --- a/R/resources.R +++ b/R/resources.R @@ -217,7 +217,7 @@ FIG_DEF_CONF =list(grouping=list(group="adduct", SUMM_COLS=c("set",BASE_KEY_MS2,"mz","ms1_rt", "ms1_int", "ms2_rt", "ms2_int", "ms1_mean","ms2_sel",QA_FLAGS,"Name", "SMILES", "Formula", "known","Comments","file") -SUMM_KEY = c("set","ID","adduct","tag","scan") +SUMM_KEY = c("set","ID","adduct","tag","precid","catid","scan") PLOT_FEATURES = c("adduct", "tag", diff --git a/R/shiny-ui-base.R b/R/shiny-ui-base.R index a186eb4..03f5613 100644 --- a/R/shiny-ui-base.R +++ b/R/shiny-ui-base.R @@ -1372,7 +1372,7 @@ mk_shinyscreen_server <- function(projects,init) { fn = file.path(projdir,input$ms2_spectra_tab_name) shinymsg(paste0("Saving MS2 spectra table to: ",basename(fn))) tab2file(pack_ms2_w_summ(rvs$m$out$tab$summ, - rvs$m$db$extr$cgm$ms2), + rvs$m$db$extr$spectra), fn) shinymsg("Done saving MS2 spectra table.") }) @@ -1634,7 +1634,8 @@ mk_shinyscreen_server <- function(projects,init) { path = rvs$m$run$metfrag$path, subpaths = rvs$m$run$metfrag$subpaths, db_file = rvs$m$run$metfrag$db_file, - stag_tab = stagtab, ms2 = rvs$m$db$extr$cgm$ms2, + stag_tab = stagtab, + ms2 = rvs$m$db$extr$spectra, #rvs$m$db$extr$cgm$ms2, runtime=rvs$m$run$metfrag$runtime, java_bin=rvs$m$run$metfrag$java_bin, nproc = rvs$m$conf$metfrag$nproc) diff --git a/R/state.R b/R/state.R index 157bbd9..309f83c 100644 --- a/R/state.R +++ b/R/state.R @@ -408,9 +408,11 @@ pack_ms2_w_summ <- function(summ,ms2) { ## Takes summ, finds entries with high quality spectra and subsets ms2 based on that. ## Take the columns we need from summ. - x = summ[ms2_sel==T,.SD,.SDcols=c(key(summ),"mz","SMILES","Formula","Name")] - mrg_keys = c(intersect(key(ms2),key(summ)),"scan") - ms2[x,.(mz=i.mz,ms2_spectrum=encode_ms2_to_line(.SD[,c("mz","intensity")])),on=mrg_keys,by=.EACHI] + x = summ[ms2_sel==T,.SD,.SDcols=c(key(summ),"mz","SMILES","Name")] + mrg_keys = intersect(key(ms2),key(summ)) + mrg_keys = c(mrg_keys,"scan") + ## ms2[x,.(mz=i.mz,ms2_spectrum=encode_ms2_to_line(.SD[,c("mz","intensity")])),on=mrg_keys,by=.EACHI] + ms2[x,.(ion_mz=i.mz,mz,intensity),on=mrg_keys,by=.EACHI] } -- GitLab