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