Mercurial > repos > computational-metabolomics > mspurity_createdatabase
comparison filterFragSpectra.R @ 0:f52287a06c02 draft
"planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit cb903cd93f9378cfb5eeb68512a54178dcea7bbc-dirty"
| author | computational-metabolomics |
|---|---|
| date | Wed, 27 Nov 2019 13:44:58 -0500 |
| parents | |
| children | 2f71b3495221 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:f52287a06c02 |
|---|---|
| 1 library(optparse) | |
| 2 library(msPurity) | |
| 3 library(xcms) | |
| 4 print(sessionInfo()) | |
| 5 | |
| 6 | |
| 7 option_list <- list( | |
| 8 make_option("--out_rdata", type="character"), | |
| 9 make_option("--out_peaklist_prec", type="character"), | |
| 10 make_option("--out_peaklist_frag", type="character"), | |
| 11 make_option("--pa", type="character"), | |
| 12 | |
| 13 make_option("--ilim", default=0.0), | |
| 14 make_option("--plim", default=0.0), | |
| 15 | |
| 16 make_option("--ra", default=0.0), | |
| 17 make_option("--snr", default=0.0), | |
| 18 | |
| 19 make_option("--rmp", action="store_true"), | |
| 20 make_option("--snmeth", default="median", type="character") | |
| 21 ) | |
| 22 | |
| 23 opt <- parse_args(OptionParser(option_list=option_list)) | |
| 24 print(opt) | |
| 25 | |
| 26 | |
| 27 loadRData <- function(rdata_path, name){ | |
| 28 #loads an RData file, and returns the named xset object if it is there | |
| 29 load(rdata_path) | |
| 30 return(get(ls()[ls() %in% name])) | |
| 31 } | |
| 32 | |
| 33 # Requires | |
| 34 pa <- loadRData(opt$pa, 'pa') | |
| 35 | |
| 36 if(is.null(opt$rmp)){ | |
| 37 opt$rmp = FALSE | |
| 38 }else{ | |
| 39 opt$rmp = TRUE | |
| 40 } | |
| 41 | |
| 42 pa <- filterFragSpectra(pa, | |
| 43 ilim=opt$ilim, | |
| 44 plim=opt$plim, | |
| 45 ra=opt$ra, | |
| 46 snr=opt$snr, | |
| 47 rmp=opt$rmp, | |
| 48 snmeth=opt$snmeth) | |
| 49 | |
| 50 print(pa) | |
| 51 save(pa, file=opt$out_rdata) | |
| 52 | |
| 53 # get the msms data for grpid from the purityA object | |
| 54 msmsgrp <- function(grpid, pa){ | |
| 55 msms <- pa@grped_ms2[grpid] | |
| 56 | |
| 57 grpinfo <- pa@grped_df[pa@grped_df$grpid==grpid,] | |
| 58 | |
| 59 grpinfo$subsetid <- 1:nrow(grpinfo) | |
| 60 result <- plyr::ddply(grpinfo, ~subsetid, setid, msms=msms) | |
| 61 return(result) | |
| 62 } | |
| 63 | |
| 64 # Set the relevant details | |
| 65 setid <- function(grpinfo_i, msms){ | |
| 66 msms_i <- msms[[1]][[grpinfo_i$subsetid]] | |
| 67 n <- nrow(msms_i) | |
| 68 msms_i <- data.frame(msms_i) | |
| 69 colnames(msms_i)[1:2] <- c('mz', 'i') | |
| 70 m <- cbind('grpid'=rep(grpinfo_i$grpid,n), 'pid'=rep(grpinfo_i$pid,n), 'fileid'=rep(grpinfo_i$fileid,n), msms_i) | |
| 71 return(m) | |
| 72 } | |
| 73 | |
| 74 | |
| 75 | |
| 76 if (length(pa)>0){ | |
| 77 | |
| 78 if (length(pa@grped_ms2)==0){ | |
| 79 message('No spectra available') | |
| 80 } else{ | |
| 81 | |
| 82 # get group ids | |
| 83 grpids <- unique(as.character(pa@grped_df$grpid)) | |
| 84 | |
| 85 # loop through all the group ids | |
| 86 df_fragments = plyr::adply(grpids, 1, msmsgrp, pa=pa) | |
| 87 df_fragments = merge(df_fragments, pa@puritydf[,c("pid", "acquisitionNum", "precursorScanNum")], by="pid") | |
| 88 df_fragments = df_fragments[order(df_fragments$grpid, df_fragments$pid, df_fragments$mz),] | |
| 89 #select and reorder columns | |
| 90 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")] | |
| 91 | |
| 92 pa@grped_df$filename = sapply(pa@grped_df$fileid, function(x) names(pa@fileList)[as.integer(x)]) | |
| 93 | |
| 94 print(head(pa@grped_df)) | |
| 95 write.table(pa@grped_df, opt$out_peaklist_prec, row.names=FALSE, sep='\t') | |
| 96 print(head(df_fragments)) | |
| 97 write.table(df_fragments, opt$out_peaklist_frag, row.names=FALSE, sep='\t') | |
| 98 } | |
| 99 } | |
| 100 |
