diff --git a/R/api.R b/R/api.R index a0555e6bb6b6d2196b442dcc49b3759a37d56b1a..44d1ab1fee1e715f3cce9dbcd6c13833e1c2d46e 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 4d9876e7a224839c27f2ed56371817f0e2c50ca5..79f177d37354aaadf4b834443c7f5a114b174914 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 aa5c516fc1bf48b0dd4746901a7fe599a056d6a5..c49e9595285f27888b51c96fe5c8d174bdcc9b7c 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] + +}