Mercurial > repos > computational-metabolomics > mspurity_puritya
comparison purityX.R @ 0:e9f8d23f6923 draft default tip
"planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit 2579c8746819670348c378f86116f83703c493eb"
author | computational-metabolomics |
---|---|
date | Thu, 04 Mar 2021 12:12:45 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e9f8d23f6923 |
---|---|
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 print(xgroups) | |
41 | |
42 if (!is.null(opt$remove_nas)) { | |
43 df <- df[!is.na(df$mz), ] | |
44 } | |
45 | |
46 if (is.null(opt$isotope_matrix)) { | |
47 im <- NULL | |
48 }else{ | |
49 im <- read.table(opt$isotope_matrix, | |
50 header = TRUE, sep = "\t", stringsAsFactors = FALSE) | |
51 } | |
52 | |
53 if (is.null(opt$exclude_isotopes)) { | |
54 isotopes <- FALSE | |
55 }else{ | |
56 isotopes <- TRUE | |
57 } | |
58 | |
59 if (is.null(opt$rtraw_columns)) { | |
60 rtraw_columns <- FALSE | |
61 }else{ | |
62 rtraw_columns <- TRUE | |
63 } | |
64 | |
65 loadRData <- function(rdata_path, xset_name) { | |
66 #loads an RData file, and returns the named xset object if it is there | |
67 load(rdata_path) | |
68 return(get(ls()[ls() == xset_name])) | |
69 } | |
70 | |
71 target_obj <- loadRData(opt$xset_path, opt$rdata_name) | |
72 | |
73 if (opt$camera_xcms == "camera") { | |
74 xset <- target_obj@xcmsSet | |
75 }else{ | |
76 xset <- target_obj | |
77 } | |
78 | |
79 print(xset) | |
80 | |
81 minOffset <- as.numeric(opt$minOffset) | |
82 maxOffset <- as.numeric(opt$maxOffset) | |
83 | |
84 if (opt$iwNorm == "none") { | |
85 iwNorm <- FALSE | |
86 iwNormFun <- NULL | |
87 }else if (opt$iwNorm == "gauss") { | |
88 iwNorm <- TRUE | |
89 iwNormFun <- msPurity::iwNormGauss(minOff = -minOffset, maxOff = maxOffset) | |
90 }else if (opt$iwNorm == "rcosine") { | |
91 iwNorm <- TRUE | |
92 iwNormFun <- msPurity::iwNormRcosine(minOff = -minOffset, maxOff = maxOffset) | |
93 }else if (opt$iwNorm == "QE5") { | |
94 iwNorm <- TRUE | |
95 iwNormFun <- msPurity::iwNormQE.5() | |
96 } | |
97 | |
98 print(xset@filepaths) | |
99 | |
100 if (!is.null(opt$files)) { | |
101 updated_filepaths <- trimws(strsplit(opt$files, ",")[[1]]) | |
102 updated_filepaths <- updated_filepaths[updated_filepaths != ""] | |
103 print(updated_filepaths) | |
104 updated_filenames <- basename(updated_filepaths) | |
105 original_filenames <- basename(xset@filepaths) | |
106 update_idx <- match(updated_filenames, original_filenames) | |
107 | |
108 if (!is.null(opt$galaxy_files)) { | |
109 galaxy_files <- trimws(strsplit(opt$galaxy_files, ",")[[1]]) | |
110 galaxy_files <- galaxy_files[galaxy_files != ""] | |
111 xset@filepaths <- galaxy_files[update_idx] | |
112 }else{ | |
113 xset@filepaths <- updated_filepaths[update_idx] | |
114 } | |
115 } | |
116 | |
117 if (!is.null(opt$choose_class)) { | |
118 classes <- trimws(strsplit(opt$choose_class, ",")[[1]]) | |
119 | |
120 ignore_files_class <- which(!as.character(xset@phenoData$class) %in% classes) | |
121 | |
122 print("choose class") | |
123 print(ignore_files_class) | |
124 }else{ | |
125 ignore_files_class <- NA | |
126 } | |
127 | |
128 if (!is.null(opt$ignore_files)) { | |
129 ignore_files_string <- trimws(strsplit(opt$ignore_files, ",")[[1]]) | |
130 filenames <- rownames(xset@phenoData) | |
131 ignore_files <- which(filenames %in% ignore_files_string) | |
132 | |
133 ignore_files <- unique(c(ignore_files, ignore_files_class)) | |
134 ignore_files <- ignore_files[ignore_files != ""] | |
135 }else{ | |
136 if (anyNA(ignore_files_class)) { | |
137 ignore_files <- NULL | |
138 }else{ | |
139 ignore_files <- ignore_files_class | |
140 } | |
141 | |
142 } | |
143 | |
144 print("ignore_files") | |
145 print(ignore_files) | |
146 | |
147 | |
148 ppLCMS <- msPurity::purityX(xset = xset, | |
149 offsets = c(minOffset, maxOffset), | |
150 cores = opt$cores, | |
151 xgroups = xgroups, | |
152 purityType = opt$purityType, | |
153 ilim = opt$ilim, | |
154 isotopes = isotopes, | |
155 im = im, | |
156 iwNorm = iwNorm, | |
157 iwNormFun = iwNormFun, | |
158 singleFile = opt$singleFile, | |
159 fileignore = ignore_files, | |
160 rtrawColumns = rtraw_columns) | |
161 | |
162 dfp <- ppLCMS@predictions | |
163 | |
164 # to make compatable with deconrank | |
165 colnames(dfp)[colnames(dfp) == "grpid"] <- "peakID" | |
166 colnames(dfp)[colnames(dfp) == "median"] <- "medianPurity" | |
167 colnames(dfp)[colnames(dfp) == "mean"] <- "meanPurity" | |
168 colnames(dfp)[colnames(dfp) == "sd"] <- "sdPurity" | |
169 colnames(dfp)[colnames(dfp) == "stde"] <- "sdePurity" | |
170 colnames(dfp)[colnames(dfp) == "RSD"] <- "cvPurity" | |
171 colnames(dfp)[colnames(dfp) == "pknm"] <- "pknmPurity" | |
172 | |
173 if (sum(is.na(dfp$medianPurity)) > 0) { | |
174 dfp[is.na(dfp$medianPurity), ]$medianPurity <- 0 | |
175 } | |
176 | |
177 print(head(dfp)) | |
178 write.table(dfp, file.path(opt$out_dir, "purityX_output.tsv"), row.names = FALSE, sep = "\t") | |
179 | |
180 save.image(file.path(opt$out_dir, "purityX_output.RData")) |