comparison purityX.R @ 0:35898942bfbb draft

"planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit cb903cd93f9378cfb5eeb68512a54178dcea7bbc-dirty"
author computational-metabolomics
date Wed, 27 Nov 2019 14:20:07 -0500
parents
children d25273689e04
comparison
equal deleted inserted replaced
-1:000000000000 0:35898942bfbb
1 library(msPurity)
2 library(optparse)
3 print(sessionInfo())
4
5 option_list <- list(
6 make_option(c("--xset_path"), type="character"),
7 make_option(c("-o", "--out_dir"), type="character"),
8 make_option(c("--mzML_path"), type="character"),
9 make_option("--minOffset", default=0.5),
10 make_option("--maxOffset", default=0.5),
11 make_option("--ilim", default=0.05),
12 make_option("--iwNorm", default="none", type="character"),
13 make_option("--exclude_isotopes", action="store_true"),
14 make_option("--isotope_matrix", type="character"),
15 make_option("--purityType", default="purityFWHMmedian"),
16 make_option("--singleFile", default=0),
17 make_option("--cores", default=4),
18 make_option("--xgroups", type="character"),
19 make_option("--rdata_name", default='xset'),
20 make_option("--camera_xcms", default='xset'),
21 make_option("--files", type="character"),
22 make_option("--galaxy_files", type="character"),
23 make_option("--choose_class", type="character"),
24 make_option("--ignore_files", type="character"),
25 make_option("--rtraw_columns", action="store_true")
26 )
27
28
29 opt<- parse_args(OptionParser(option_list=option_list))
30 print(opt)
31
32
33 if (!is.null(opt$xgroups)){
34 xgroups = as.numeric(strsplit(opt$xgroups, ',')[[1]])
35 }else{
36 xgroups = NULL
37 }
38
39
40
41 print(xgroups)
42
43 if (!is.null(opt$remove_nas)){
44 df <- df[!is.na(df$mz),]
45 }
46
47 if (is.null(opt$isotope_matrix)){
48 im <- NULL
49 }else{
50 im <- read.table(opt$isotope_matrix,
51 header = TRUE, sep='\t', stringsAsFactors = FALSE)
52 }
53
54 if (is.null(opt$exclude_isotopes)){
55 isotopes <- FALSE
56 }else{
57 isotopes <- TRUE
58 }
59
60 if (is.null(opt$rtraw_columns)){
61 rtraw_columns <- FALSE
62 }else{
63 rtraw_columns <- TRUE
64 }
65
66 loadRData <- function(rdata_path, xset_name){
67 #loads an RData file, and returns the named xset object if it is there
68 load(rdata_path)
69 return(get(ls()[ls() == xset_name]))
70 }
71
72 target_obj <- loadRData(opt$xset_path, opt$rdata_name)
73
74 if (opt$camera_xcms=='camera'){
75 xset <- target_obj@xcmsSet
76 }else{
77 xset <- target_obj
78 }
79
80 print(xset)
81
82 minOffset = as.numeric(opt$minOffset)
83 maxOffset = as.numeric(opt$maxOffset)
84
85
86 if (opt$iwNorm=='none'){
87 iwNorm = FALSE
88 iwNormFun = NULL
89 }else if (opt$iwNorm=='gauss'){
90 iwNorm = TRUE
91 iwNormFun = msPurity::iwNormGauss(minOff=-minOffset, maxOff=maxOffset)
92 }else if (opt$iwNorm=='rcosine'){
93 iwNorm = TRUE
94 iwNormFun = msPurity::iwNormRcosine(minOff=-minOffset, maxOff=maxOffset)
95 }else if (opt$iwNorm=='QE5'){
96 iwNorm = TRUE
97 iwNormFun = msPurity::iwNormQE.5()
98 }
99
100 print(xset@filepaths)
101
102 if (!is.null(opt$files)){
103 updated_filepaths <- trimws(strsplit(opt$files, ',')[[1]])
104 updated_filepaths <- updated_filepaths[updated_filepaths != ""]
105 print(updated_filepaths)
106 updated_filenames = basename(updated_filepaths)
107 original_filenames = basename(xset@filepaths)
108 update_idx = match(updated_filenames, original_filenames)
109
110 if (!is.null(opt$galaxy_files)){
111 galaxy_files <- trimws(strsplit(opt$galaxy_files, ',')[[1]])
112 galaxy_files <- galaxy_files[galaxy_files != ""]
113 xset@filepaths <- galaxy_files[update_idx]
114 }else{
115 xset@filepaths <- updated_filepaths[update_idx]
116 }
117 }
118
119 if (!is.null(opt$choose_class)){
120 classes <- trimws(strsplit(opt$choose_class, ',')[[1]])
121
122
123 ignore_files_class <- which(!as.character(xset@phenoData$class) %in% classes)
124
125 print('choose class')
126 print(ignore_files_class)
127 }else{
128 ignore_files_class <- NA
129 }
130
131 if (!is.null(opt$ignore_files)){
132 ignore_files_string <- trimws(strsplit(opt$ignore_files, ',')[[1]])
133 filenames <- rownames(xset@phenoData)
134 ignore_files <- which(filenames %in% ignore_files_string)
135
136 ignore_files <- unique(c(ignore_files, ignore_files_class))
137 ignore_files <- ignore_files[ignore_files != ""]
138 }else{
139 if (anyNA(ignore_files_class)){
140 ignore_files <- NULL
141 }else{
142 ignore_files <- ignore_files_class
143 }
144
145 }
146
147 print('ignore_files')
148 print(ignore_files)
149
150
151 ppLCMS <- msPurity::purityX(xset=xset,
152 offsets=c(minOffset, maxOffset),
153 cores=opt$cores,
154 xgroups=xgroups,
155 purityType=opt$purityType,
156 ilim = opt$ilim,
157 isotopes = isotopes,
158 im = im,
159 iwNorm = iwNorm,
160 iwNormFun = iwNormFun,
161 singleFile = opt$singleFile,
162 fileignore = ignore_files,
163 rtrawColumns=rtraw_columns)
164
165
166 dfp <- ppLCMS@predictions
167
168 # to make compatable with deconrank
169 colnames(dfp)[colnames(dfp)=='grpid'] = 'peakID'
170 colnames(dfp)[colnames(dfp)=='median'] = 'medianPurity'
171 colnames(dfp)[colnames(dfp)=='mean'] = 'meanPurity'
172 colnames(dfp)[colnames(dfp)=='sd'] = 'sdPurity'
173 colnames(dfp)[colnames(dfp)=='stde'] = 'sdePurity'
174 colnames(dfp)[colnames(dfp)=='RSD'] = 'cvPurity'
175 colnames(dfp)[colnames(dfp)=='pknm'] = 'pknmPurity'
176 if(sum(is.na(dfp$medianPurity))>0){
177 dfp[is.na(dfp$medianPurity),]$medianPurity = 0
178 }
179
180
181 print(head(dfp))
182 write.table(dfp, file.path(opt$out_dir, 'purityX_output.tsv'), row.names=FALSE, sep='\t')
183
184 save.image(file.path(opt$out_dir, 'purityX_output.RData'))