Metabolomics Society Forum

Software => XCMS => R => XCMS - FAQ => Topic started by: hpbenton on August 19, 2011, 10:08:29 AM

Title: m/z sort assumption violated! Argh!!
Post by: hpbenton on August 19, 2011, 10:08:29 AM
Don't panic!
You have probably received a message like the one below
Quote
Detecting mass traces at 5 ppm ...
 % finished: 0 Error in .local(object, ...) :
  m/z sort assumption violated !

This error means that one of the scans in your data has the m/z (vector) out of order. CentWave performs a lot of checks on the data to make sure everything is ok. Don't worry though this can be fixed. You can either use openMS FileFilter (-sort_peaks option) or xcms. If it's a single file try this code
Code: [Select]
library(xcms)
xr<-xcmsRaw("YourFile", profstep=0)
write.mzdata(xr, file="NewFixedFile.mzData")

However, you will probably find that you have a lot of files that are like this so you want to automate the code. The code below will test every file in the current directory and directories below.

If you could help the developer by telling us what instrument this happened on by filling out the poll (http://http://metabolomics-forum.com/viewtopic.php?f=8&t=149&sid=a101b2408c1c49915f915eb4b2486476). Thank you.

Code: [Select]
## Notes: This program will check the m/z vector for a single file for sort violation
## If a sort violation is found the m/z vector is reorganised along with the Intensity
## vector. Finally a new CDF file is made which is fixed.
## !!!NB!!! : Parallel version does not report progress

require(xcms)
checkAllcdfs<-function(Ftype="mzXML", nSlaves=1){
AllCDFs<-list.files(recursive=TRUE, pattern=Ftype, ignore.case=TRUE, full.names=TRUE)
if(nSlaves >1){
if(require(snow)){
cl <- makeCluster(nSlaves, type = "SOCK")
}
clusterEvalQ(cl, library(xcms))
unlist(clusterApply(cl, AllCDFs, checkCDFfile))
stopCluster(cl)
} else{
sapply(AllCDFs, checkCDFfile)
cat("n")
}
}

checkCDFfile<-function(file, type=".mzXML"){
cat("n")
cat(paste("Loading File:", file, sep=""))
xr<-xcmsRaw(file, profstep=0)
for(i in 1:length(xr@scanindex)){
scan<-getScan(xr, scan=i)
if(is.unsorted(scan[,"mz"]) == TRUE){
cat(" x ")
newfile<-sub(type, "-Fixed.mzdata", file, ignore.case=TRUE)
write.mzdata(xr, newfile)
file.copy(file, sub(type, ".OLD", file, ignore.case=TRUE))
unlink(file)
rm(list=ls())
gc()
return(1)
}
if(i == length(xr@scanindex)){
cat(" O ")
rm(list=ls())
gc()
return(0)
}
}
}