annotate BC/batch_correction_3Lwrapper.R @ 4:23314e1192d4 draft default tip

Uploaded
author melpetera
date Thu, 14 Jan 2021 09:56:58 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
1 #!/usr/bin/env Rscript
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
2
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
3 ################################################################################################
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
4 # batch_correction_wrapper #
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
5 # #
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
6 # Authors: Marion LANDI / Jean-Francois MARTIN / Melanie Petera #
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
7 # User: Galaxy #
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
8 # Original data: -- #
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
9 # Starting date: 22-07-2014 #
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
10 # Version 1: 22-07-2014 #
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
11 # Version 2: 08-12-2014 #
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
12 # Version 2.1: 09-01-2015 modification in Error message of sample matching #
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
13 # Version 2.2: 16-03-2015 inclusion of miniTools' functions for special characters #
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
14 # Version 2.90: 18-08-2015 new parameter valnull #
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
15 # Version 2.91: 25-08-2016 error message improvment #
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
16 # Version 3: 02-10-2020 #
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
17 # - split of tool-linked code and script-linked one #
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
18 # - addition of args print and sessionInfo() #
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
19 # - adjustment of sample tags' parameters to 3L methods #
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
20 # - addition of the min.norm argument in meth3L() call #
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
21 # #
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
22 # Input files: dataMatrix.txt, sampleMetadata.txt, variableMetadata.txt (BC only) #
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
23 # Output files: graph.pdf, corrected table (BC only), diagnostic table (DBC only), #
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
24 # variableMetadata (BC only) #
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
25 # #
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
26 ################################################################################################
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
27
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
28
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
29 library(batch) #necessary for parseCommandArgs function
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
30
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
31 ##------------------------------
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
32 ## test help option
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
33 ##------------------------------
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
34
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
35 # Prog. constants
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
36 argv.help <- commandArgs(trailingOnly = FALSE)
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
37 script.path <- sub("--file=", "", argv.help[grep("--file=", argv.help)])
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
38 prog.name <- basename(script.path)
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
39
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
40 # Test Help
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
41 if (length(grep('-h', argv.help)) > 0) {
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
42 cat("Usage: Rscript ",
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
43 prog.name,
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
44 "{args} \n",
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
45 "parameters: \n",
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
46 "\tanalyse {val}: must be set to \"batch_correction\"",
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
47 "\tdataMatrix {file}: set the input data matrix file (mandatory) \n",
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
48 "\tsampleMetadata {file}: set the input sample metadata file (mandatory) \n",
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
49 "\tvariableMetadata {file}: set the input variable metadata file (mandatory) \n",
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
50 "\tmethod {opt}: set the method; can set to \"linear\", \"lowess\" or \"loess\" (mandatory) \n",
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
51 "\tspan {condition}: set the span condition; set to \"none\" if method is set to \"linear\" (mandatory) \n",
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
52 "\tref_factor {value}: set the ref_factor value; (if span value is set to NULL, optional) \n",
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
53 "\tdetail {value}: set the detail value; (if span value is set to NULL, optional) \n",
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
54 "\tdataMatrix_out {file}: set the output data matrix file (mandatory) \n",
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
55 "\tvariableMetadata_out {file}: set the output variable metadata file (mandatory) \n",
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
56 "\tgraph_output {file}: set the output graph file (mandatory) \n",
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
57 "\trdata_output {file}: set the output Rdata file (mandatory) \n",
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
58 "\tbatch_col_name {val}: the column name for batch. Default value is \"batch\".\n",
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
59 "\tinjection_order_col_name {val}: the column name for the injection order. Default value is \"injectionOrder\".\n",
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
60 "\tsample_type_col_name {val}: the column name for the sample types. Default value is \"sampleType\".\n",
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
61 "\tsample_type_tags {val}: the tags used inside the sample type column, defined as key/value pairs separated by commas (example: blank=blank,pool=pool,sample=sample).\n",
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
62 "\n")
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
63 quit(status = 0)
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
64 }
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
65
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
66 ##------------------------------
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
67 ## init. params
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
68 ##------------------------------
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
69
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
70 args = parseCommandArgs(evaluate=FALSE) #interpretation of arguments given in command line as an R list of objects
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
71
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
72
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
73 cat('\nJob starting time:\n',format(Sys.time(), "%a %d %b %Y %X"),
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
74 '\n\n--------------------------------------------------------------------',
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
75 '\nParameters used:\n\n')
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
76 print(args)
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
77 cat('--------------------------------------------------------------------\n\n')
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
78
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
79
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
80 # Set default col names
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
81 if ( ! 'batch_col_name' %in% names(args))
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
82 args[['batch_col_name']] <- 'batch'
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
83 if ( ! 'injection_order_col_name' %in% names(args))
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
84 args[['injection_order_col_name']] <- 'injectionOrder'
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
85 if ( ! 'sample_type_col_name' %in% names(args))
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
86 args[['sample_type_col_name']] <- 'sampleType'
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
87 if ( ! 'sample_type_tags' %in% names(args))
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
88 args[['sample_type_tags']] <- 'blank=blank,pool=pool,sample=sample'
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
89
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
90 # Parse sample type tags
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
91 sample.type.tags <- list()
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
92 for (kv in strsplit(strsplit(args$sample_type_tags, ',')[[1]], '='))
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
93 sample.type.tags[[kv[[1]]]] <- kv[-1]
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
94 if ( ! all(c('pool', 'blank', 'sample') %in% names(sample.type.tags)))
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
95 stop("All tags pool, blank and sample must be defined in option sampleTypeTags.")
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
96 args$sample_type_tags <- sample.type.tags
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
97
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
98 ##------------------------------
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
99 ## init. functions
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
100 ##------------------------------
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
101
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
102 source_local <- function(...){
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
103 argv <- commandArgs(trailingOnly = FALSE)
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
104 base_dir <- dirname(substring(argv[grep("--file=", argv)], 8))
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
105 for(i in 1:length(list(...))){source(paste(base_dir, list(...)[[i]], sep="/"))}
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
106 }
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
107 #Import the different functions
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
108 source_local("batch_correction_3Lfct.R","batch_correction_3Llauncher.R","easyrlibrary-lib/RcheckLibrary.R","easyrlibrary-lib/miniTools.R")
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
109
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
110 # Specificities of BC and DBC
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
111 if(args$analyse == "batch_correction") {
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
112 args$out_graph_pdf <- NULL
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
113 args$out_preNormSummary <- NULL
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
114 }else{
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
115 args$variableMetadata <- NULL
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
116 args$rdata_output <- NULL
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
117 args$dataMatrix_out <- NULL
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
118 args$variableMetadata_out <- NULL
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
119 args$graph_output <- NULL
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
120 args$method <- NULL
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
121 args$detail <- NULL
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
122 args$valnull <- NULL
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
123 }
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
124
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
125 # Launch tool
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
126 meth3L(idsample=args$sampleMetadata, iddata=args$dataMatrix, sample_type_col_name=args$sample_type_col_name, injection_order_col_name=args$injection_order_col_name,
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
127 batch_col_name=args$batch_col_name, sample_type_tags=args$sample_type_tags, factbio=args$ref_factor, analyse=args$analyse, metaion=args$variableMetadata,
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
128 detail=args$detail, method=args$method, outlog=args$graph_output, span=args$span, valnull=args$valnull, rdata_output=args$rdata_output,
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
129 dataMatrix_out=args$dataMatrix_out, variableMetadata_out=args$variableMetadata_out, out_graph_pdf=args$out_graph_pdf, out_preNormSummary=args$out_preNormSummary,
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
130 min.norm=1)
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
131
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
132
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
133 cat('\n\n--------------------------------------------------------------------',
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
134 '\nInformation about R (version, Operating System, attached or loaded packages):\n\n')
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
135 sessionInfo()
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
136 cat('--------------------------------------------------------------------\n',
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
137 '\nJob ending time:\n',format(Sys.time(), "%a %d %b %Y %X"))
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
138
23314e1192d4 Uploaded
melpetera
parents:
diff changeset
139 rm(args)