diff filterFragSpectra.R @ 6:52d48bcd3608 draft

"planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit 2579c8746819670348c378f86116f83703c493eb"
author computational-metabolomics
date Thu, 04 Mar 2021 12:29:17 +0000
parents 50eedf5ad217
children 94eb1fcfde88
line wrap: on
line diff
--- a/filterFragSpectra.R	Fri Nov 13 09:55:01 2020 +0000
+++ b/filterFragSpectra.R	Thu Mar 04 12:29:17 2021 +0000
@@ -5,96 +5,95 @@
 
 
 option_list <- list(
-  make_option("--out_rdata", type="character"),
-  make_option("--out_peaklist_prec", type="character"),
-  make_option("--out_peaklist_frag", type="character"),
-  make_option("--pa", type="character"),
+  make_option("--out_rdata", type = "character"),
+  make_option("--out_peaklist_prec", type = "character"),
+  make_option("--out_peaklist_frag", type = "character"),
+  make_option("--pa", type = "character"),
 
-  make_option("--ilim", default=0.0),
-  make_option("--plim", default=0.0),
+  make_option("--ilim", default = 0.0),
+  make_option("--plim", default = 0.0),
 
-  make_option("--ra", default=0.0),  
-  make_option("--snr", default=0.0),
+  make_option("--ra", default = 0.0),
+  make_option("--snr", default = 0.0),
 
-  make_option("--rmp", action="store_true"),
-  make_option("--snmeth", default="median", type="character")
+  make_option("--rmp", action = "store_true"),
+  make_option("--snmeth", default = "median", type = "character")
 )
 
-opt <- parse_args(OptionParser(option_list=option_list))
+opt <- parse_args(OptionParser(option_list = option_list))
 print(opt)
 
 
-loadRData <- function(rdata_path, name){
+loadRData <- function(rdata_path, name) {
     #loads an RData file, and returns the named xset object if it is there
     load(rdata_path)
     return(get(ls()[ls() %in% name]))
 }
 
 # Requires
-pa <- loadRData(opt$pa, 'pa')
+pa <- loadRData(opt$pa, "pa")
 
-if(is.null(opt$rmp)){
-  opt$rmp = FALSE
+if (is.null(opt$rmp)) {
+  opt$rmp <- FALSE
 }else{
-  opt$rmp = TRUE
+  opt$rmp <- TRUE
 }
 
-pa <- filterFragSpectra(pa, 
-                        ilim=opt$ilim, 
-                        plim=opt$plim,
-                        ra=opt$ra,
-                        snr=opt$snr,
-                        rmp=opt$rmp,
-                        snmeth=opt$snmeth)
+pa <- filterFragSpectra(pa,
+                        ilim = opt$ilim,
+                        plim = opt$plim,
+                        ra = opt$ra,
+                        snr = opt$snr,
+                        rmp = opt$rmp,
+                        snmeth = opt$snmeth)
 
 print(pa)
-save(pa, file=opt$out_rdata)
+save(pa, file = opt$out_rdata)
 
 # get the msms data for grpid from the purityA object
-msmsgrp <- function(grpid, pa){
+msmsgrp <- function(grpid, pa) {
   msms <- pa@grped_ms2[grpid]
-  
-  grpinfo <- pa@grped_df[pa@grped_df$grpid==grpid,]
-  
-  grpinfo$subsetid <- 1:nrow(grpinfo)
-  result <- plyr::ddply(grpinfo, ~subsetid, setid, msms=msms)
+
+  grpinfo <- pa@grped_df[pa@grped_df$grpid == grpid, ]
+
+  grpinfo$subsetid <- seq_len(nrow(grpinfo))
+  result <- plyr::ddply(grpinfo, ~subsetid, setid, msms = msms)
   return(result)
 }
 
-# Set the relevant details 
-setid <- function(grpinfo_i, msms){
+# Set the relevant details
+setid <- function(grpinfo_i, msms) {
   msms_i <- msms[[1]][[grpinfo_i$subsetid]]
   n <- nrow(msms_i)
   msms_i <- data.frame(msms_i)
-  colnames(msms_i)[1:2] <- c('mz', 'i')
-  m <- cbind('grpid'=rep(grpinfo_i$grpid,n), 'pid'=rep(grpinfo_i$pid,n), 'fileid'=rep(grpinfo_i$fileid,n), msms_i)
+  colnames(msms_i)[1:2] <- c("mz", "i")
+  m <- cbind("grpid" = rep(grpinfo_i$grpid, n), "pid" = rep(grpinfo_i$pid, n), "fileid" = rep(grpinfo_i$fileid, n), msms_i)
   return(m)
 }
 
 
 
-if (length(pa)>0){
+if (length(pa) > 0) {
 
-  if (length(pa@grped_ms2)==0){
-    message('No spectra available')
-  } else{
+  if (length(pa@grped_ms2) == 0) {
+    message("No spectra available")
+  } else {
 
     # get group ids
     grpids <- unique(as.character(pa@grped_df$grpid))
 
     # loop through all the group ids
-    df_fragments = plyr::adply(grpids, 1, msmsgrp, pa=pa)
-    df_fragments = merge(df_fragments, pa@puritydf[,c("pid", "acquisitionNum", "precursorScanNum")], by="pid")
-    df_fragments = df_fragments[order(df_fragments$grpid, df_fragments$pid, df_fragments$mz),]
+    df_fragments <- plyr::adply(grpids, 1, msmsgrp, pa = pa)
+    df_fragments <- merge(df_fragments, pa@puritydf[, c("pid", "acquisitionNum", "precursorScanNum")], by = "pid")
+    df_fragments <- df_fragments[order(df_fragments$grpid, df_fragments$pid, df_fragments$mz), ]
     #select and reorder columns
-    df_fragments = df_fragments[,c("grpid", "pid", "precursorScanNum", "acquisitionNum", "fileid", "mz", "i", "snr", "ra", "purity_pass_flag", "intensity_pass_flag", "ra_pass_flag", "snr_pass_flag", "pass_flag")]
+    df_fragments <- df_fragments[, c("grpid", "pid", "precursorScanNum", "acquisitionNum", "fileid", "mz", "i", "snr", "ra", "purity_pass_flag", "intensity_pass_flag", "ra_pass_flag", "snr_pass_flag", "pass_flag")]
 
-    pa@grped_df$filename = sapply(pa@grped_df$fileid, function(x) names(pa@fileList)[as.integer(x)])
+    pa@grped_df$filename <- sapply(pa@grped_df$fileid, function(x) names(pa@fileList)[as.integer(x)])
 
-    print(head(pa@grped_df))    
-    write.table(pa@grped_df, opt$out_peaklist_prec, row.names=FALSE, sep='\t')
+    print(head(pa@grped_df))
+    write.table(pa@grped_df, opt$out_peaklist_prec, row.names = FALSE, sep = "\t")
     print(head(df_fragments))
-    write.table(df_fragments, opt$out_peaklist_frag, row.names=FALSE, sep='\t')
+    write.table(df_fragments, opt$out_peaklist_frag, row.names = FALSE, sep = "\t")
   }
 }
-