comparison NmrNormalization_wrapper.R @ 5:3d00a98974b7 draft

planemo upload for repository https://github.com/workflow4metabolomics/normalization commit 1aedb0fe77d56139bc8f5ded1fee1e455e8e9495
author lecorguille
date Tue, 02 Oct 2018 12:14:47 -0400
parents
children 221cbd549c40
comparison
equal deleted inserted replaced
4:8178d9a118e3 5:3d00a98974b7
1 #!/usr/bin/env Rscript
2
3 ## 070115_NmrBucketing2galaxy_v1.R
4 ## Marie Tremblay-Franco
5 ## MetaboHUB: The French Infrastructure for Metabolomics and Fluxomics
6 ## www.metabohub.fr/en
7 ## marie.tremblay-franco@toulouse.inra.fr
8
9 runExampleL <- FALSE
10
11
12 ##------------------------------
13 ## Options
14 ##------------------------------
15 strAsFacL <- options()$stringsAsFactors
16 options(stringsAsFactors = FALSE)
17
18
19 ##------------------------------
20 ## Libraries laoding
21 ##------------------------------
22 # For parseCommandArgs function
23 library(batch)
24
25 # Constants
26 argv <- commandArgs(trailingOnly = FALSE)
27 script.path <- sub("--file=","",argv[grep("--file=",argv)])
28 prog.name <- basename(script.path)
29
30 # Print help
31 if (length(grep('-h', argv)) >0) {
32 cat("Usage:", prog.name,
33 "dataMatrix myDataMatrix.tsv",
34 "scalingMethod PQN|QuantitativeVariable",
35 "graphType None|Overlay|One_per_individual",
36 "logOut myLog.txt",
37 "dataMatrixOut myDataMatrixOutput.tsv",
38 "graphOut myGraph.pdf",
39 "\n")
40 quit(status = 0)
41 }
42
43 # R script call
44 source_local <- function(fname)
45 {
46 argv <- commandArgs(trailingOnly = FALSE)
47 base_dir <- dirname(substring(argv[grep("--file=", argv)], 8))
48 source(paste(base_dir, fname, sep="/"))
49 }
50 #Import the different functions
51 source_local("NmrNormalization_script.R")
52 source_local("DrawSpec.R")
53
54
55 ##------------------------------
56 ## Errors ?????????????????????
57 ##------------------------------
58
59
60 ##------------------------------
61 ## Constants
62 ##------------------------------
63 topEnvC <- environment()
64 flagC <- "\n"
65
66
67 ##------------------------------
68 ## Script
69 ##------------------------------
70 if(!runExampleL)
71 argLs <- parseCommandArgs(evaluate=FALSE)
72
73
74 ## Parameters Loading
75 ##-------------------
76 # Inputs
77 data <- read.table(argLs[["dataMatrix"]], check.names=FALSE, header=TRUE, sep="\t", row.names=1)
78 names <- rownames(data)
79 ## Add a test to check if all values are numercical
80 if (!all(vapply(data, is.numeric, FUN.VALUE = FALSE)))
81 stop("Data are not numeric")
82 ## Integer conversion to avoid stack overflow when computin the sum
83 data <- as.data.frame(lapply(data, as.numeric))
84 rownames(data) <- names
85
86 scaling <- argLs[["scalingMethod"]]
87 graphique <- argLs[["graphType"]]
88
89 if (scaling=='PQN')
90 {
91 metadataSample <- read.table(argLs[["sampleMetadata"]],check.names=FALSE,header=TRUE,sep="\t")
92 factor<- argLs[["factor"]]
93 ControlGroup <- argLs[["controlGroup"]]
94 }
95 if (scaling=='QuantitativeVariable')
96 {
97 metadataSample <- read.table(argLs[["sampleMetadata"]],check.names=FALSE,header=TRUE,sep="\t")
98 factor <- argLs[["factor"]]
99 }
100
101 # Outputs
102 nomGraphe <- argLs[["graphOut"]]
103 dataMatrixOut <- argLs[["dataMatrixOut"]]
104 log <- argLs[["logOut"]]
105
106
107 ## Checking R packages
108 ##--------------------
109 sink(log)
110 cat("\tPACKAGE INFO\n")
111 pkgs=c("batch")
112 for(pkg in pkgs) {
113 suppressPackageStartupMessages( stopifnot( library(pkg, quietly=TRUE, logical.return=TRUE, character.only=TRUE)))
114 cat(pkg,"\t",as.character(packageVersion(pkg)),"\n",sep="")
115 }
116 cat("\n")
117
118
119 ## Checking arguments
120 ##-------------------
121 error.stock <- "\n"
122 if(length(error.stock) > 1)
123 stop(error.stock)
124
125
126 ## Computation
127 ##------------
128 NormalizationResults <- NmrNormalization(dataMatrix=data,scalingMethod=scaling,sampleMetadata=metadataSample,
129 bioFactor=factor,ControlGroup=ControlGroup,
130 graph=graphique,nomFichier=nomGraphe,savLog.txtC=log)
131
132 data_normalized <- NormalizationResults[[1]]
133
134
135 ## Graphical outputs
136 ##------------------
137 if (graphique != "None")
138 {
139 # Graphic Device opening
140 pdf(nomGraphe,onefile=TRUE)
141
142 if (graphique == "Overlay")
143 {
144 # Global spectral window
145 spectra <- data.frame(t(data_normalized))
146 drawSpec(spectra,xlab="", ylab="Intensity", main="")
147 }
148 else
149 {
150 for (i in 1:ncol(data_normalized))
151 {
152 spectra <- t(data_normalized[,i])
153 drawSpec(spectra,xlab="", ylab="Intensity", main=colnames(data_normalized)[i])
154 }
155 }
156 dev.off()
157 }
158
159
160 ## Saving
161 ##-------
162 # Data
163 data_normalized <- cbind(rownames(data_normalized),data_normalized)
164 colnames(data_normalized) <- c("Variable",colnames(data_normalized)[-1])
165 write.table(data_normalized,file=argLs$dataMatrixOut,quote=FALSE,row.names=FALSE,sep="\t")
166
167
168 ## Ending
169 ##---------------------
170 cat("\nEnd of 'Normalization' Galaxy module call: ", as.character(Sys.time()), sep = "")
171 sink()
172 options(stringsAsFactors = strAsFacL)
173 rm(list = ls())