From f99bf3ec883079ec5f5bd8b6540d8c17b3ed1068 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Todor=20Kondi=C4=87?= <kontrapunkt@uclmail.net> Date: Tue, 28 Mar 2023 15:49:37 +0200 Subject: [PATCH] extract_spectra: Extract full MS2 spectra. --- R/api.R | 18 ++++++++---------- R/data-model.R | 10 ++++++++++ R/extraction.R | 29 +++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 10 deletions(-) diff --git a/R/api.R b/R/api.R index a0555e6..44d1ab1 100644 --- a/R/api.R +++ b/R/api.R @@ -374,24 +374,22 @@ extr_data <-function(m) { idx=integer(0), rt=numeric(0), intensity=numeric(0)) + + spectra = empty_spectra_table() + for (fn in names(lfdata)) { - ## x = lfdata[[fn]]$ms2[cgram_ms1, - ## .(an, - ## ce, - ## precid=i.precid, - ## rt, - ## intensity), - ## on=c(prec_idx="idx"), - ## nomatch=NULL, - ## allow.cartesian=T] rtab = relate_ms2_to_precid(coarse=coarse,ms2=lfdata[[fn]]$ms2,cgram_ms1=cgram_ms1) + sptab = extract_spectra(lms[[fn]],rtab) cgram_ms2 = rbind(cgram_ms2,rtab) + spectra = rbind(spectra,sptab) } setkey(cgram_ms1,precid,rt) setkey(cgram_ms2,precid,ce,rt) + setkey(spectra,precid,scan) m$db$extr$cgm$ms1 = cgram_ms1 m$db$extr$cgm$ms2 = cgram_ms2 - browser() + m$db$extr$spectra = spectra + m } diff --git a/R/data-model.R b/R/data-model.R index 4d9876e..79f177d 100644 --- a/R/data-model.R +++ b/R/data-model.R @@ -103,3 +103,13 @@ make_db_precursors <- function(m) { m$db$precursors = masses m } + + +empty_spectra_table <- function() { + r = data.table(precid=integer(0), + scan=character(0), + mz=numeric(0), + intensity=numeric(0)) + setkey(r,precid,scan) + r +} diff --git a/R/extraction.R b/R/extraction.R index aa5c516..c49e959 100644 --- a/R/extraction.R +++ b/R/extraction.R @@ -700,8 +700,19 @@ get_fdata <- function(ms) { relate_ms2_to_precid <- function(coarse,ms2,cgram_ms1) { + ## Take `coarse' table (the one with coarse mass limits), ms2 + ## fData and ms1 chromatogram, then relate precids from cgram_ms1 + ## to ms2 data. + + ## Select those MS2 entries the parents of which coarsely match + ## compound lists masses. res = ms2[coarse,on=.(prec_mz>iso_coarse_min,prec_mz<iso_coarse_max),.(prec_mz=x.prec_mz,precid,prec_idx,scan,idx,ce,rt,intensity),nomatch=NULL] setkey(res,precid,prec_idx) + + ## Now, make sure those coarsely matched MS2 actually have a + ## parent that finely matches something in the chromatogram (and + ## this is by ensuring that a `precid' with the correct scan (idx) + ## shows up in the chromatogram. cgram_ms1[res,on=.(precid,idx==prec_idx), .(precid,ce,scan=i.scan, idx=i.idx,rt=i.rt, @@ -709,3 +720,21 @@ relate_ms2_to_precid <- function(coarse,ms2,cgram_ms1) { } +extract_spectra <- function(ms,cgram_ms2) { + ## This will extract full MS2 spectra based on ms2 chromatogram entries. + indices = cgram_ms2[,.SD,.SDcol=c("precid","scan","idx")] + + res = empty_spectra_table() + selind = indices[,unique(.SD),.SDcol=c("scan","idx")] + sel = ms[selind$idx] + + masses = mz(sel) + intensities = intensity(sel) + res = selind + setkey(res,scan) + res = res[,data.table(mz=masses[[scan]], + intensity=intensities[[scan]]), + keyby=c("scan")] + res[indices,on=.(scan),precid:=i.precid] + +} -- GitLab