xcms icon indicating copy to clipboard operation
xcms copied to clipboard

Skip peak calling for centroided DI-MS data

Open joannawolthuis opened this issue 4 years ago • 2 comments

Hi crew! I have a (hopefully easy ;) ) question for you. I am playing with the centroiding that Thermo has built in as an alternative to MSW peak calling, but I would still like to do the peak clustering as I'd usually do. Unfortunately I cannot assign through chromPeaks(item) <- centroided_peaks as the right classes have not been created yet in that case. Is there some way I can assign chromPeaks in a sample that has not yet been through peak finding in xcms?

Kind regards, Joanna

joannawolthuis avatar Feb 14 '20 17:02 joannawolthuis

Hi, please find below a snippet.

     ## Perform the peak detection using centWave on some of the files from the
     ## faahKO package. Files are read using the readMSData from the MSnbase
     ## package
     library(faahKO)
     library(xcms)

     ##  Get some peaks for demonstration:
     cwp <- CentWaveParam(ppm = 20, noise = 10000)
     ## Change snthresh parameter
     snthresh(cwp) <- 25
     cwp
     
     fls <- dir(system.file("cdf/KO", package = "faahKO"), recursive = TRUE,
                full.names = TRUE)
     raw_data <- readMSData(fls[1:2], mode = "onDisk")
     
     ## Perform the peak detection using the settings defined above.
     res <- findChromPeaks(raw_data, param = cwp)

centroided_peaks <- chromPeaks(res)

## Now what you actually asked for:
item <- new("XCMSnExp")
chromPeaks(item) <- centroided_peaks ## Fails with "invalid class “XCMSnExp” object: The number of available samples does not match with the sample assignment of peaks in the 'chromPeaks' element of the msFeatureData slot! "

## This works:
chromPeaks(item) <- centroided_peaks[1:11,]

Problem is that there is a check about the length(fileNames(item), but I couldn't find a way to set fileNames(item) <- c("A","B") . At least it's a start ;-) Yours, Steffen

sneumann avatar Feb 15 '20 14:02 sneumann

Thanks so much!!! This gave me enough courage to give it another proper go (as my current progress bar was at 2 days loading all 3000+ files in just for pos mode.. ; _; Testing the following now, it worked on a smaller dataset:

if(centroided){
    summed_peaks <- readMSData(files = peakfiles,
                               mode = "onDisk")   
     pklists = pbapply::pblapply(1:length(summed_peaks), function(i){
      pks = as.data.frame(Spectra(summed_peaks[[i]]))[,2:4]
      colnames(pks) <- c("sample", "mz", "into")
      pks <- pks[pks$into > 0,]
      pks$mzmin <- pks$mz
      pks$mzmax <- pks$mz
      pks$rt <- c(NA)
      pks$rtmin <- c(NA)
      pks$rtmax <- c(NA)
      pks$sample <- c(i)
      pks <- pks[pks$into > 0,]  
    })
    allPeaks <- as.matrix(data.table::rbindlist(pklists))
    res <- new("XCMSnExp")
    res@processingData@files <- fileNames(summed_peaks)
    chromPeaks(res) <- allPeaks
    res@experimentData <- summed_peaks@experimentData
    featureData(res) <- summed_peaks@featureData
    }

Fingers crossed! Also, forgive me for dissecting your carefully created class system in here ;)

joannawolthuis avatar Feb 15 '20 17:02 joannawolthuis