Skip to content
Snippets Groups Projects
Commit f99bf3ec authored by Todor Kondić's avatar Todor Kondić
Browse files

extract_spectra: Extract full MS2 spectra.

parent 8d829d08
No related branches found
No related tags found
No related merge requests found
......@@ -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
}
......
......@@ -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
}
......@@ -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]
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment