Mercurial > repos > tomnl > dma_filelist_generation
comparison flag-remove-peaks.R @ 0:6a2bb42acfe4 draft
planemo upload for repository https://github.com/computational-metabolomics/dma-tools-galaxy commit 6c48bd51987a28401de6cf5e49b1b30e5e73fe16-dirty
| author | tomnl |
|---|---|
| date | Tue, 27 Mar 2018 06:53:36 -0400 |
| parents | |
| children | 1aca78735588 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:6a2bb42acfe4 |
|---|---|
| 1 library(XCMSwrapper) | |
| 2 library(optparse) | |
| 3 | |
| 4 option_list <- list( | |
| 5 make_option(c("-o", "--out_dir"), type="character", default=getwd(), | |
| 6 help="Output folder for resulting files [default = %default]" | |
| 7 ), | |
| 8 make_option(c("-x", "--xset_path"), type="character", default=file.path(getwd(),"xset.rds"), | |
| 9 help="The path to the xcmsSet object [default = %default]" | |
| 10 ), | |
| 11 make_option("--polarity", default='NA', | |
| 12 help="polarity (just used for naming purpose for files being saved) [positive, negative, NA] [default %default]" | |
| 13 ), | |
| 14 make_option("--rsd_i_blank", default=NA, | |
| 15 help="RSD threshold for the blank [default = %default]" | |
| 16 ), | |
| 17 make_option("--minfrac_blank", default=0.5, | |
| 18 help="minimum fraction of files for features needed for the blank [default = %default]" | |
| 19 ), | |
| 20 make_option("--rsd_rt_blank", default=NA, | |
| 21 help="RSD threshold for the RT of the blank [default = %default]" | |
| 22 ), | |
| 23 | |
| 24 make_option("--ithres_blank", default=NA, | |
| 25 help="Intensity threshold for the blank [default = %default]" | |
| 26 ), | |
| 27 make_option("--s2b", default=10, | |
| 28 help="fold change (sample/blank) needed for sample peak to be allowed. e.g. | |
| 29 if s2b set to 10 and the recorded sample 'intensity' value was 100 and blank was 10. | |
| 30 1000/10 = 100, so sample has fold change higher than the threshold and the peak | |
| 31 is not considered a blank [default = %default]" | |
| 32 ), | |
| 33 make_option("--blank_class", default='blank', type="character", | |
| 34 help="A string representing the class that will be used for the blank.[default = %default]" | |
| 35 ), | |
| 36 make_option("--egauss_thr", default=NA, | |
| 37 help="Threshold for filtering out non gaussian shaped peaks. Note this only works | |
| 38 if the 'verbose columns' and 'fit gauss' was used with xcms | |
| 39 [default = %default]" | |
| 40 ), | |
| 41 make_option("--rsd_i_sample", default=NA, | |
| 42 help="RSD threshold for the samples [default = %default]" | |
| 43 ), | |
| 44 make_option("--minfrac_sample", default=0.8, | |
| 45 help="minimum fraction of files for features needed for the samples [default = %default]" | |
| 46 ), | |
| 47 make_option("--rsd_rt_sample", default=NA, | |
| 48 help="RSD threshold for the RT of the samples [default = %default]" | |
| 49 ), | |
| 50 make_option("--ithres_sample", default=5000, | |
| 51 help="Intensity threshold for the sample [default = %default]" | |
| 52 ), | |
| 53 make_option("--grp_rm_ids", default=NA, | |
| 54 help="vector of grouped_xcms peaks to remove (corresponds to the row from xcms::group output) | |
| 55 [default = %default]" | |
| 56 ), | |
| 57 make_option("--remove_spectra", action="store_true", | |
| 58 help=" TRUE if flagged spectra is to be removed [default = %default]" | |
| 59 ), | |
| 60 make_option("--minfrac_xcms", default=0.5, | |
| 61 help="minfrac for xcms grouping [default = %default]" | |
| 62 ), | |
| 63 make_option("--mzwid", default=0.001, | |
| 64 help="mzwid for xcms grouping [default = %default]" | |
| 65 ), | |
| 66 make_option("--bw", default=5, | |
| 67 help="bw for xcms grouping [default = %default]" | |
| 68 ), | |
| 69 | |
| 70 make_option("--temp_save", action="store_true", | |
| 71 help="Assign True if files for each step saved (for testing purposes) [default = %default]" | |
| 72 ), | |
| 73 | |
| 74 make_option("--xset_name", default="xset", | |
| 75 help="Name of the xcmsSet object within the RData file [default = %default]" | |
| 76 ) | |
| 77 | |
| 78 | |
| 79 ) | |
| 80 | |
| 81 # store options | |
| 82 opt<- parse_args(OptionParser(option_list=option_list)) | |
| 83 | |
| 84 if (is.null(opt$temp_save)){ | |
| 85 temp_save<-FALSE | |
| 86 }else{ | |
| 87 temp_save<-TRUE | |
| 88 } | |
| 89 | |
| 90 if (is.null(opt$remove_spectra)){ | |
| 91 remove_spectra<-FALSE | |
| 92 }else{ | |
| 93 remove_spectra<-TRUE | |
| 94 } | |
| 95 | |
| 96 | |
| 97 print(opt) | |
| 98 | |
| 99 loadRData <- function(rdata_path, xset_name){ | |
| 100 #loads an RData file, and returns the named xset object if it is there | |
| 101 load(rdata_path) | |
| 102 return(get(ls()[ls() == xset_name])) | |
| 103 } | |
| 104 | |
| 105 xset<-loadRData(opt$xset_path, opt$xset_name) | |
| 106 print(xset) | |
| 107 ffrm_out <- XCMSwrapper::flag_remove(xset, | |
| 108 pol=opt$polarity, | |
| 109 rsd_i_blank=opt$rsd_i_blank, | |
| 110 minfrac_blank=opt$minfrac_blank, | |
| 111 rsd_rt_blank=opt$rsd_rt_blank, | |
| 112 ithres_blank=opt$ithres_blank, | |
| 113 s2b=opt$s2b, | |
| 114 ref.class=opt$blank_class, | |
| 115 egauss_thr=opt$egauss_thr, | |
| 116 rsd_i_sample=opt$rsd_i_sample, | |
| 117 minfrac_sample=opt$minfrac_sample, | |
| 118 rsd_rt_sample=opt$rsd_rt_sample, | |
| 119 ithres_sample=opt$ithres_sample, | |
| 120 minfrac_xcms=opt$minfrac_xcms, | |
| 121 mzwid=opt$mzwid, | |
| 122 bw=opt$bw, | |
| 123 out_dir=opt$out_dir, | |
| 124 temp_save=temp_save, | |
| 125 remove_spectra=remove_spectra, | |
| 126 grp_rm_ids=unlist(strsplit(as.character(opt$grp_rm_ids), split=", "))[[1]]) | |
| 127 | |
| 128 xset <- ffrm_out[[1]] | |
| 129 grp_peaklist <- ffrm_out[[2]] | |
| 130 removed_peaks <- ffrm_out[[3]] | |
| 131 | |
| 132 save.image(file=file.path(opt$out_dir, 'xset_filtered.RData')) | |
| 133 | |
| 134 # grpid needed for mspurity ID needed for deconrank... (will clean up at some up) | |
| 135 write.table(data.frame('grpid'=rownames(grp_peaklist), 'ID'=rownames(grp_peaklist), grp_peaklist), | |
| 136 file.path(opt$out_dir, 'peaklist_filtered.txt'), row.names=FALSE, sep='\t') | |
| 137 | |
| 138 removed_peaks <- data.frame(removed_peaks) | |
| 139 write.table(data.frame('ID'=rownames(removed_peaks),removed_peaks), | |
| 140 file.path(opt$out_dir, 'removed_peaks.txt'), row.names=FALSE, sep='\t') |
