R/summarizeMWresults.R

#' Summarizes \code{runMatch} annotation results
#' 
#' Summarizes the annotation data files generated by \code{runMatch}. Writes either one (single MS mode) or two
#' (coupled MS ionization modes) tab delimited text files containing a summary of the annotation hits and sorted 
#' by the annotation scores.
#' 
#' @param project The exact name as used for the relevant search run.
#' @param MSlib MS library object as generated by the \code{buildMSlibrary} function.
#' @param polarity The MS ionization mode ("negative", "positive" or "both") .
#' @param otherMSlib The MSlib MS library object corresponding with the complimentary ionization mode.
#' @return NULL. On succesful completion a delimited text file is written to the working directory
#'  containing details about the putative annotations, including: 
#'    compound data and scores of the individual annotation modules (columns 1-12)
#'    information about the corresponding library entery (columns 14-34) peak group 
#'    data (columns 36-42), sample intensity data and the annotation scores (last column).
#'  
#' @examples\donttest{
#' data (sampleLib)
#' # Run matching and get annotations in a single ionization mode
#' tomPL_neg <- system.file("extdata/Tom_all_negative_2ch_xan.tsv",package="matchWeiz")
#' rtFiles_neg <- system.file("extdata/rt_files_neg",package="matchWeiz")
#' runMatch (tomPL_neg,rtFiles_neg,"negative","tom_test",MSlib.neg)
#' summarizeMWresults ("tom_test",MSlib.neg,"negative")
#' # Run matching and get annotations using data from both ionization mode
#' tomPL_pos <- system.file("extdata/Tom_all_positive_2ch_xan.tsv",package="matchWeiz")
#' rtFiles_pos <- system.file("extdata/rt_files_pos",package="matchWeiz")
#' runMatch (tomPL_pos,rtFiles_pos,"positive","tom_test",MSlib.pos)
#' summarizeMWresults ("tom_test",MSlib.neg,"both",MSlib.pos)
#' }
#' # Running time: the example annotation run using the demo library (total 76 enteries) 
#' # took 65 seconds (on an eight core Intel i7-3820 CPU @ 3.60GHz, 16Gb RAM) 
#' # Annotation versus the complete WEIZMASS library (total 3309 enteries) 
#' # took 341 seconds using the same hardware.
#' @export
summarizeMWresults = function(  
  project,
  MSlib,
  polarity,
  otherMSlib=NULL  
) {
  
  if (!is.null(otherMSlib)) {
    # prepare cross ref tables mass-diffs reference matrix
    xref=t(sapply(ruleset.neg$massdiff, function(x) x-ruleset.pos$massdiff))
    rownames(xref) = ruleset.neg$name
    colnames(xref) = ruleset.pos$name   
    # run the analysis twice - once for each ionization mode
    modes = list("positive","negative")
    sapply (1:2, function(i) {
      # set ionization modes
      this_mode = modes[[i]]
      other_mode = modes[[(i%%2+1)]]             
      print (paste("Summarizing:",this_mode))
      # choose MS library 
      if (MSlib$polarity == this_mode) { currentLib=MSlib$peaks } else { currentLib=otherMSlib$peaks }
      # read metaboMatch annotation files 
      f = list.files(pattern=paste(project,".*",this_mode,".*idList.RData",sep=""),full.names=TRUE)  
      if (length(f)!=1) { print(f); stop(paste("Need exactly one annotation file in the",mode,"ion mode")) }
      load (f)
      mainList = idList
      f = list.files(pattern=paste(project,".*",other_mode,".*idList.RData",sep=""),full.names=TRUE)
      if (length(f)!=1) { print(f); stop(paste("Need exactly one results file in the",other_mode,"ion mode")) }
      load (f)      
      refList = idList
      rm (idList)          
      sampOutput = summarizeResults(mainList,currentLib,this_mode,refList,xref)                       
      fileName = paste("mmAnnotations",project,this_mode,"tsv",sep=".")
      write.table (sampOutput,file=fileName,sep="\t",row.names=FALSE)
    }) 
  } else {             
    print (paste("Summarizing:",polarity))
    f = list.files(pattern=paste(project,".*",polarity,".*idList.RData",sep=""),full.names=TRUE)
    if (length(f)!=1) { print(f); stop(paste("Need exactly one results file in the",polarity,"ion mode")) }
    load (f)
    mainList = idList
    rm (idList)
    sampOutput = summarizeResults(mainList,MSlib$peaks,polarity) 
    fileName = paste("mmAnnotations",project,polarity,"tsv",sep=".")
    write.table (sampOutput,file=fileName,sep="\t",row.names=FALSE)      
  } 
  print ("Done")
}
AsaphA/matchWeiz documentation built on May 5, 2019, 8:12 a.m.