Skip to main content

Show Posts

This section allows you to view all Show Posts made by this member. Note that you can only see Show Posts made in areas you currently have access to.

Messages - u0008355

1
XCMS / Re: Add spectra of xcmsRaw objects together & write to cdf
Dear all,
OK, figured out how to do it, maybe not the most elegant solution, but it seems to work. Currently, I assume that your different files are already reasonably well aligned.

Code: [Select]
######### function to sum the spectra of two xcmsRaw objects together and return it as a new xcmsRaw object 

addspectra = function(object1,object2,profst=1) {
mzminobj1=min(object1@mzrange)
mzmaxobj1=max(object1@mzrange)
mzminobj2=min(object2@mzrange)
mzmaxobj2=max(object2@mzrange)
mzmin=min(mzminobj1,mzminobj2)
mzmax=max(mzmaxobj1,mzmaxobj2)
if (mzmin<mzminobj1) {object1@env$profile=rbind(matrix(0,nrow=round((mzminobj1-mzmin)/profst),ncol=ncol(object1@env$profile)),object1@env$profile)}
if (mzmin<mzminobj2) {object2@env$profile=rbind(matrix(0,nrow=round((mzminobj2-mzmin)/profst),ncol=ncol(object2@env$profile)),object2@env$profile)}
if (mzmax>mzmaxobj1) {object1@env$profile=rbind(object1@env$profile,matrix(0,nrow=round((mzmax-mzmaxobj1)/profst),ncol=ncol(object1@env$profile)))}
if (mzmax>mzmaxobj2) {object2@env$profile=rbind(object2@env$profile,matrix(0,nrow=round((mzmax-mzmaxobj2)/profst),ncol=ncol(object2@env$profile)))}

object1@mzrange=c(mzmin,mzmax)
object2@mzrange=c(mzmin,mzmax)

object3=object1
object3@env$profile=object1@env$profile+object2@env$profile  # matrix with scans in columns and rows representing equally spaced m/z values
object3@tic=object1@tic+object2@tic
nscans=ncol(object1@env$profile)

mznew=c()
intensitynew=c()
scanindexnew=c()
mzall=seq(mzmin,mzmax,by=profst)

x=object3@env$profile 
intensitynew=lapply(seq_len(ncol(x)), function(i) {res=x[,i];res[res!=0]})
mznew=lapply(seq_len(ncol(x)), function(i) {res=x[,i];mzall[res!=0]})
scanindexnew=sapply(seq_len(length(mznew)), function(i) {length(mznew[[i]])})
scanindexnew=c(0,scanindexnew)[1:length(scanindexnew)]
scanindexnew=sapply(seq_len(length(scanindexnew)), function(i) {sum(scanindexnew[1:i])})

object3@env$mz=unlist(mznew)
object3@env$intensity=unlist(intensitynew)
object3@scanindex=as.integer(scanindexnew)

return(object3)
}


######### function to sum the spectra of several cdf files and write it back as a cdf file

addspectracdffiles = function(files,profst=1,outfile="summedspectra.cdf") {
  obj=xcmsRaw(files[1],profstep=profst,profmethod="bin")
  nfiles=length(files)
  if (nfiles>1) {
  for (i in 2:length(files)) {
    obj2=xcmsRaw(files[i],profstep=profst,profmethod="bin")
    obj=addspectra(obj,obj2,profst=profst)
    print(paste("Added spectrum of file",files[i]))
  }}
  write.cdf(obj,file=outfile)
  return(obj)
}
2
XCMS / Add spectra of xcmsRaw objects together & write to cdf
Dear all,
In order to examine the average spectra of a series of samples I would like to be able to add the spectra of several xcmsRaw objects together and then write this object to a new cdf file. Does anyone perhaps happen to know how one could achieve this? (I am dealing with GC/EI-MS data) (a function to add the m/z values and intensities of just two xcmsRaw objects together would be fine, as I could take it from there)

cheers,
Tom