comparison Testtest/GCMS-test.R @ 0:40de28c7d3fb draft

Uploaded
author melpetera
date Thu, 23 Nov 2017 08:50:14 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:40de28c7d3fb
1 # author: Pauline Ribeyre
2
3
4 #####################
5 # required packages #
6 #####################
7
8 library(batch) # provides "parseCommandargv" function
9
10
11 ############
12 # init #
13 ############
14
15 # read the parameters
16 argv_list <- parseCommandArgs(evaluate = FALSE) #, trailingOnly = TRUE)
17 argv <- unlist(argv_list)
18
19 # common environment to share variables between files
20 env <- new.env()
21
22 # timestamp made unique by adding a random element
23 env$timestamp <- round(as.numeric(Sys.time()) + runif(1, 0, 100))
24
25 # set working directory
26 tmpwd <- paste0("/galaxy/data/", env$timestamp)
27 # system("chmod -R 0777 .")
28 # dir.create(tmpwd, mode = "0777")
29 dir.create(tmpwd)
30 setwd(tmpwd)
31
32 # readme file
33 readme_msg <- paste0("\"", env$timestamp, "\"",
34 " is a temporary folder storing data for an instance of Galaxy tool \"GCMS-test\" launched on ",
35 Sys.time(), ". ",
36 "This folder should be deleted automatically at the end of the tool's execution. ",
37 "If the instance ended or has been aborted and this folder still exists, it can be deleted manually.")
38 write(readme_msg, file = "README.txt")
39
40 ################
41 # source files #
42 ################
43
44 source_local <- function(fname){
45 # Gets a file's full name in Galaxy's directories.
46 #
47 # Args:
48 # fname: the name of the file.
49 #
50 # Returns:
51 # The full name of the file.
52
53 argv <- commandArgs(trailingOnly = FALSE)
54 base_dir <- dirname(substring(argv[grep("--file=", argv)], 8))
55 return (paste(base_dir, fname, sep="/"))
56
57 }
58
59 env$source_settings <- source_local("GCMS-test_settings.R")
60 env$source_analyze <- source_local("GCMS-test_analyze.R")
61 env$source_output <- source_local("GCMS-test_output.R")
62 env$source_spectrum <- source_local("GCMS-test_spectrum.R")
63
64 env$source_file_names <- "file_names.txt"
65
66
67 #############
68 # functions #
69 #############
70
71 args_to_vary_list <- function() {
72 # Creates the list of settings variations using the arguments.
73 #
74 # Returns:
75 # The list of settings variations.
76
77 param <- c("step", "steps", "mzdiff", "fwhm", "simthresh", "snthresh", "max",
78 "minclassfraction", "minclasssize", "rtdiff") #, "minfeat")
79
80 PeakPicking_param <- c("step", "steps", "mzdiff", "fwhm", "snthresh", "max")
81 betweenSamples_param <- c("min.class.fraction", "min.class.size", "rtdiff", "simthresh")
82
83 vary <- list()
84
85 for (p in param) {
86
87 min <- as.numeric(argv[[paste0(p, "_min")]])
88 max <- as.numeric(argv[[paste0(p, "_max")]])
89 step <- as.numeric(argv[[paste0(p, "_step")]])
90
91 if (p == "minclassfraction")
92 p <- "min.class.fraction"
93 else if (p == "minclasssize")
94 p <- "min.class.size"
95
96 if (p %in% PeakPicking_param)
97 name <- paste0("PeakPicking$", p)
98 else if (p %in% betweenSamples_param)
99 name <- paste0("betweenSamples.", p)
100 else
101 stop("\"", p, "\" is not a valid parameter to vary.")
102
103 # prevent seq error
104 if (min > max)
105 max <- min
106
107 range <- seq(min, max, step)
108 vary[[length(vary) + 1]] <- c(p, name, range)
109
110 }
111
112 return (vary)
113
114 }
115
116
117 read_ions_to_check <- function() {
118 # Reads the arguments concerning the ions to check.
119
120 env$ions_name <- list()
121 env$ions_rt <- list()
122 env$ions_mzs <- list()
123
124 env$nb_ions_to_check <- length(grep("ions_name_", names(argv)))
125
126 if (env$nb_ions_to_check > 0) {
127 for (i in 1:env$nb_ions_to_check) {
128 env$ions_name[length(env$ions_name) + 1] <- argv[[paste0("ions_name_", i)]]
129 env$ions_rt[length(env$ions_rt) + 1] <- as.numeric(argv[[paste0("ions_rt_", i)]])
130 mzs <- argv[[paste0("ions_mzs_", i)]]
131 mzs <- as.numeric(strsplit(mzs, ",")[[1]])
132 env$ions_mzs[length(env$ions_mzs) + 1] <- list(mzs)
133 }
134 }
135
136 }
137
138
139 ############
140 # inputs #
141 ############
142
143 env$nb_cores <- as.numeric(argv[["nb_cores"]])
144 cat("Nb cores:", env$nb_cores, "\n")
145
146 # get data from zip file
147 if (!is.null(argv[["zip_file"]])) {
148 env$cdf_files <- unzip(argv[["zip_file"]])
149 } else {
150 stop("No files have been provided.")
151 }
152
153 # vary parameters
154 env$vary_list <- args_to_vary_list()
155
156 # count duplicate ions
157 env$count_duplicates <- argv[["count_duplicates"]] == "true"
158 if (env$count_duplicates) {
159
160 # delta rt
161 env$duplicates_delta_rt <- as.numeric(argv[["duplicates_delta_rt"]])
162
163 # delta mz
164 env$duplicates_delta_mz <- as.numeric(argv[["duplicates_delta_mz"]])
165
166 }
167
168 # check the presence of ions
169 env$nb_ions_to_check <- 0
170 env$check_ions <- argv[["check_ions"]] == "true"
171 if (env$check_ions) {
172
173 read_ions_to_check()
174
175 # delta rt
176 env$ions_delta_rt <- as.numeric(argv[["ions_delta_rt"]])
177
178 # delta mz
179 env$ions_delta_mz <- as.numeric(argv[["ions_delta_mz"]])
180
181 }
182
183
184 #############
185 # outputs #
186 #############
187
188 env$summary_out <- argv[["summary_out"]]
189 env$peakspectra_out <- argv[["peakspectra_out"]]
190 env$intensity_graph_out <- argv[["intensity_graph_out"]]
191
192 if (env$count_duplicates)
193 env$count_duplicates_out <- argv[["duplicates_out"]]
194
195 env$compare_graph_out <- argv[["compare_graph_out"]]
196
197
198 ##########
199 # run #
200 ##########
201
202 cat("---------- Settings ----------\n")
203 sys.source(env$source_settings, env)
204
205
206 cat("---------- Analysis ----------\n")
207 sys.source(env$source_analyze, env)
208
209
210 cat("---------- Output ----------\n")
211 sys.source(env$source_output, env)
212
213
214 ##########
215 # end #
216 ##########
217
218 # delete temporary files
219 setwd("..")
220 unlink(tmpwd, recursive = TRUE)