Mercurial > repos > ppericard > viscorvar
comparison mixomics_blocksplsda_script.R @ 0:d0b77b926863 draft
"planemo upload for repository https://gitlab.com/bilille/galaxy-viscorvar commit 85dac6b13a9adce48b47b2b8cb28d2319ae9c1ca-dirty"
author | ppericard |
---|---|
date | Tue, 23 Jun 2020 19:57:35 -0400 |
parents | |
children | e93350dc99f1 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:d0b77b926863 |
---|---|
1 #!/usr/bin/env Rscript | |
2 | |
3 # Setup R error handling to go to stderr | |
4 options( show.error.messages=F, error = function () { cat( geterrmessage(), file=stderr() ); q( "no", 1, F ) } ) | |
5 | |
6 # we need that to not crash galaxy with an UTF8 error on German LC settings. | |
7 loc <- Sys.setlocale("LC_MESSAGES", "en_US.UTF-8") | |
8 | |
9 ## Get parameters ## | |
10 suppressPackageStartupMessages(require(argparse)) | |
11 | |
12 parser <- ArgumentParser(description='Run the mixOmics block.splsda function') | |
13 | |
14 parser$add_argument('--block', dest='blocks_list', nargs=4, action="append", required=TRUE, | |
15 help="Block name + nb variables to select + data matrix file + variables metadata file") | |
16 parser$add_argument('--sample_metadata_in', dest='sample_metadata_in', required=TRUE, | |
17 help="Samples metadata file") | |
18 parser$add_argument('--sample_description_col', dest='sample_description_col', type='integer', | |
19 default=0, help="Sample description column number") | |
20 parser$add_argument('--ncomp', dest='ncomp', type='integer', default=2, | |
21 help="Number of components to include in the model") | |
22 parser$add_argument('--correlation', dest='correlation', action="store_true", | |
23 help="Add correlation between all blocks") | |
24 parser$add_argument('--scheme', dest='scheme', default="horst", help="Scheme") | |
25 parser$add_argument('--mode', dest='mode', default="regression", help="Mode") | |
26 parser$add_argument('--maxiter', dest='maxiter', type='integer', default=100, | |
27 help="Maximum number of iterations") | |
28 parser$add_argument('--scale', dest='scale', action="store_true", | |
29 help="Each block is standardized to zero means and unit variances") | |
30 parser$add_argument('--check_missing_values', dest='check_missing_values', action="store_true", | |
31 help="Check for missing values and raise an error") | |
32 parser$add_argument('--init', dest='init', default="svd", | |
33 help="Init (svd or svd.single)") | |
34 parser$add_argument('--tol', dest='tol', type='double', default=1e-06, | |
35 help="Convergence stopping value") | |
36 parser$add_argument('--nearzerovar', dest='nearzerovar', action="store_true", | |
37 help="Should be set in particular for data with many zero values") | |
38 parser$add_argument('--rdata_out', dest='rdata_out', required=TRUE, help="Output Rdata file") | |
39 parser$add_argument('--sample_metadata_out', dest='sample_metadata_out', required=TRUE, help="Output sample metadata file") | |
40 parser$add_argument('--variable_metadata_outdir', dest='variable_metadata_outdir', required=TRUE, help="Output variable metadata directory") | |
41 | |
42 args <- parser$parse_args() | |
43 | |
44 ## Print parameters | |
45 print("Blocks:") | |
46 print(args$blocks_list) | |
47 print("Sample metadata file:") | |
48 print(args$sample_metadata_in) | |
49 print("Sample description column number:") | |
50 print(args$sample_description_col) | |
51 print("Number of components:") | |
52 print(args$ncomp) | |
53 print("Compute correlation between all blocks:") | |
54 print(args$correlation) | |
55 print("Scheme:") | |
56 print(args$scheme) | |
57 print("Mode:") | |
58 print(args$mode) | |
59 print("Max nb of iterations:") | |
60 print(args$maxiter) | |
61 print("Scale:") | |
62 print(args$scale) | |
63 print("Check for missing values:") | |
64 print(args$check_missing_values) | |
65 print("Tol:") | |
66 print(args$tol) | |
67 print("near.zero.var:") | |
68 print(args$nearzerovar) | |
69 print("Output Rdata file:") | |
70 print(args$rdata_out) | |
71 print("Output sample metadata file:") | |
72 print(args$sample_metadata_out) | |
73 print("Output variable metadata directory:") | |
74 print(args$variable_metadata_outdir) | |
75 | |
76 ## Loading libraries | |
77 suppressPackageStartupMessages(require(mixOmics)) | |
78 | |
79 ## Read sample metadata file and set description factor matrix | |
80 sample_metadata <- read.table(args$sample_metadata_in, sep='\t', header=TRUE, row.names=1) | |
81 sample_metadata_names <- row.names(sample_metadata) | |
82 # print(sample_metadata_names) | |
83 | |
84 # print("Sample metadata matrix:") | |
85 # print(head(sample_metadata)) | |
86 | |
87 description_column <- ncol(sample_metadata) | |
88 if(args$sample_description_col > 0) | |
89 { | |
90 description_column <- args$sample_description_col | |
91 } | |
92 | |
93 Y <- factor(sample_metadata[[description_column]]) | |
94 | |
95 print("Y factor matrix:") | |
96 print(Y) | |
97 | |
98 ## Read and prepare block datasets | |
99 list_X <- c() | |
100 keepX <- c() | |
101 | |
102 for(i in 1:nrow(args$blocks_list)) | |
103 { | |
104 # Read block input parameters | |
105 block_name <- args$blocks_list[i,1] | |
106 block_keep <- strtoi(args$blocks_list[i,2]) | |
107 block_data_matrix_filename <- args$blocks_list[i,3] | |
108 # block_meta_var <- args$blocks_list[i,4] | |
109 | |
110 print(sprintf("Processing block %s", block_name)) | |
111 | |
112 # Store block data matrices | |
113 block_data_matrix <- t(read.table(block_data_matrix_filename, sep='\t', header=TRUE, row.names=1)) # transpose the matrix so that the samples become rows and the variables become columns | |
114 block_data_matrix_names <- row.names(block_data_matrix) | |
115 # print(block_data_matrix_names) | |
116 | |
117 if(!identical(sample_metadata_names, block_data_matrix_names)) | |
118 { | |
119 stop("Sample names must be the same and in the same order in the sample metadata matrix and the block data matrix") | |
120 } | |
121 | |
122 list_X[[block_name]] <- block_data_matrix | |
123 | |
124 # Set the nb of variables to keep | |
125 nb_variables = ncol(list_X[[block_name]]) | |
126 if(block_keep > 0) | |
127 { | |
128 keepX[[block_name]] <- rep(block_keep, args$ncomp) | |
129 } | |
130 else | |
131 { | |
132 keepX[[block_name]] <- rep(nb_variables, args$ncomp) | |
133 } | |
134 print(sprintf("Block %s contains %d variables and %d will be selected", block_name, nb_variables, block_keep)) | |
135 } | |
136 | |
137 # print(list_X) | |
138 | |
139 ## Generate design matrix | |
140 block_nb <- nrow(args$blocks_list) | |
141 | |
142 design <- matrix(0, nrow = block_nb, ncol = block_nb) | |
143 | |
144 if(args$correlation) | |
145 { | |
146 design <- matrix(1, nrow = block_nb, ncol = block_nb) | |
147 diag(design) <- 0 | |
148 } | |
149 | |
150 # print("Design matrix:") | |
151 # print(design) | |
152 | |
153 ################### | |
154 ## Main function ## | |
155 ################### | |
156 | |
157 mixomics_result <- block.splsda(X = list_X, | |
158 Y = Y, | |
159 ncomp = args$ncomp, | |
160 keepX = keepX, | |
161 design = design, | |
162 scheme = args$scheme, | |
163 mode = args$mode, | |
164 scale = args$scale, | |
165 init = args$init, | |
166 tol = args$tol, | |
167 max.iter = args$maxiter, | |
168 near.zero.var = args$nearzerovar, | |
169 all.outputs = TRUE) | |
170 | |
171 print("Block.splsda object:") | |
172 print(mixomics_result) | |
173 print(attributes(mixomics_result)) | |
174 | |
175 ## Save output Rdata file | |
176 save(mixomics_result, file=args$rdata_out) | |
177 | |
178 ## Save output sample metadata file | |
179 # print("Block.splsda variates:") | |
180 # print(mixomics_result$variates) | |
181 | |
182 for(block_name in names(mixomics_result$variates)) | |
183 { | |
184 # print(block_name) | |
185 # print(mixomics_result$variates[[block_name]]) | |
186 | |
187 # Format the column names to add the block name and replace spaces | |
188 colnames(mixomics_result$variates[[block_name]]) <- paste("block.splsda_variates", block_name, gsub(" ", "_", colnames(mixomics_result$variates[[block_name]])), sep = "_") | |
189 # print(mixomics_result$variates[[block_name]]) | |
190 | |
191 # Append the new columns to the sample metadata matrix | |
192 sample_metadata <- cbind2(sample_metadata, mixomics_result$variates[[block_name]]) | |
193 } | |
194 | |
195 # print(sample_metadata) | |
196 | |
197 write.table(sample_metadata, file = args$sample_metadata_out, quote = TRUE, sep = "\t", row.names = TRUE, col.names = NA) | |
198 | |
199 ## Save output variable metadata files in output directory | |
200 # print("Block.splsda loadings:") | |
201 # print(mixomics_result$loadings) | |
202 | |
203 for(i in 1:nrow(args$blocks_list)) | |
204 { | |
205 # Read again block input parameters | |
206 block_name <- args$blocks_list[i,1] | |
207 # block_keep <- strtoi(args$blocks_list[i,2]) | |
208 # block_data_matrix_filename <- args$blocks_list[i,3] | |
209 block_meta_var <- args$blocks_list[i,4] | |
210 | |
211 print(sprintf("Saving block %s output metavar", block_name)) | |
212 | |
213 meta_variable <- mixomics_result$loadings[[block_name]] | |
214 # print(head(meta_variable)) | |
215 | |
216 # Format the column names to add the block name and replace spaces | |
217 colnames(meta_variable) <- paste("block.splsda_loadings", gsub(" ", "_", colnames(meta_variable)), sep = "_") | |
218 | |
219 # Read input block variable metadata files if provided (optional) | |
220 if(block_meta_var != "None") | |
221 { | |
222 input_meta_variable <- read.table(block_meta_var, sep='\t', header=TRUE, row.names=1) | |
223 # print(head(input_meta_variable)) | |
224 | |
225 # Append the new columns to the variable metadata matrix | |
226 meta_variable <- cbind2(input_meta_variable, meta_variable) | |
227 } | |
228 | |
229 # print(head(meta_variable)) | |
230 | |
231 block_meta_var_output_filename <- paste("mixomics_blocksplsda_block_", block_name, "_variable_metadata.tsv", sep="") | |
232 write.table(meta_variable, file = paste(args$variable_metadata_outdir,block_meta_var_output_filename, sep='/'), quote = TRUE, sep = "\t", row.names = TRUE, col.names = NA) | |
233 } |