Skip to main content
Topic: saving xcmsSET object to CDF or mzXML files (Read 6584 times) previous topic - next topic

saving xcmsSET object to CDF or mzXML files

Is it possible to save rt corrected xcmsSET objects to CDF or mzXML files without discarding rt correction data?

We aim to compare many sample classes, with different metabolites in each class. Thus, few features are shared across classes and therefore retention time correction methods (i.e obiwarp) might not be effective. In fact the only feature shared across all samples is a single internal standard we used to check RT shifts.

Is it possible to apply rt correction to each sample class individually then save and or merge xcmsSet objects in order to perform a final general comparison between classes?


Thank you all,

DF

Re: saving xcmsSET object to CDF or mzXML files

Reply #1
hi,

I had the same need, to save files back to .cdf after alignment, and so I used this code. Hope it helps you.

Best,
Agne

#########################################################
library(xcms)
library(data.table)

# raw data files
path = "path to the .cdf files"

## align the chromatograms
setwd(path)
xset <- xcmsSet()
xset2 = group(xset,method='density')
xset2 <- retcor(xset2, family="s", plottype="m")
xset2 <- group(xset2, bw =10, sleep = 0)
xset3 <- fillPeaks(xset2)

## get a list of all .cdf files
cdf_files = list.files("directory of .cdf files",full.names = TRUE)
cdf_files = cdf_files[cdf_files %like% ".CDF$" || ".cdf$"]

## match the names of the .cdf files with the sampnames of xset3
samples = sampnames(xset3)
i = sapply(samples,function(sample){
   which(grepl(pattern=sample,x=cdf_files))
})


for(j in 1:length(cdf_files)){
   x = xcmsRaw(cdf_files[j])
   rt = xset3@rt$corrected[[ i[j] ]]    #get the corrected retention times
   x@scantime = rt                #overwrite the uncorrected retention times
   
   ## save as .cdf file
   new_file = cdf_files[j]
   new_file = gsub('directory with .cdf files','directory for corrected .cdf files',new_file)
   write.cdf(x,new_file)
}