-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathasics_wrapper.R
198 lines (158 loc) · 6.45 KB
/
asics_wrapper.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/local/public/bin/Rscript --vanilla --slave --no-site-file
## 29122017_asics_wrapper.R
## Remi Servien, Patrick Tardivel, Marie Tremblay-Franco and Gaelle Lefort
runExampleL <- FALSE
##------------------------------
## Options
##------------------------------
strAsFacL <- options()$stringsAsFactors
options(stringsAsFactors=FALSE)
##------------------------------
## Libraries loading
##------------------------------
# ParseCommandArgs function
library(batch)
library(ASICS)
# R script call
source_local <- function(fname)
{
argv <- commandArgs(trailingOnly=FALSE)
base_dir <- dirname(substring(argv[grep("--file=", argv)], 8))
source(paste(base_dir, fname, sep="/"))
}
#Import the different functions
source_local("DrawSpec.R")
##------------------------------
## Errors ?????????????????????
##------------------------------
##------------------------------
## Constants
##------------------------------
topEnvC <- environment()
flagC <- "\n"
##------------------------------
## Script
##------------------------------
if(!runExampleL)
argLs <- parseCommandArgs(evaluate=FALSE)
# Standards loading
load(argLs[["standards"]])
## Parameters Loading
##-------------------
# Inputs
## Spectrum to annotate
zipfile= argLs[["zipfile"]]
directory=unzip(zipfile, list=F)
directory=paste(getwd(),strsplit(directory[1],"/")[[1]][2],sep="/")
##Exclusion zone(s)
exclusionZones <- argLs[["zone_exclusion_choices.choice"]]
exclusionZonesBorders <- NULL
if (!is.null(argLs$zone_exclusion_left))
{
for(i in which(names(argLs)=="zone_exclusion_left"))
{
# exclusionZonesBorders <- c(exclusionZonesBorders,list(c(argLs[[i]],argLs[[i+1]])))
exclusionZonesBorders <- c(exclusionZonesBorders,argLs[[i]],argLs[[i+1]])
}
exclusionZonesBorders <- matrix(exclusionZonesBorders, byrow=T, ncol=2)
}
if (is.null(argLs$zone_exclusion_left))
{
exclusionZonesBorders <- matrix(c(0,0), byrow=T, ncol=2)
}
## Maximal allowed shift
shift <- argLs[["shift"]]
## Graphical zone(s)
graphicalZones <- argLs[["zone_graphical_choices.choice"]]
graphicalZonesBorders <- NULL
if (!is.null(argLs$zone_exclusion_left))
{
for(i in which(names(argLs)=="zone_graphical_left"))
{
graphicalZonesBorders <- c(graphicalZonesBorders,list(c(argLs[[i]],argLs[[i+1]])))
}
}
# Outputs
logOut <- argLs[["logOut"]]
proportionEstimation <- argLs[["proportionEstimation"]]
graphOut <- argLs[["graphOut"]]
sink(logOut)
cat("\tPACKAGE INFO\n")
# pkgs=c("batch", "ASICS")
pkgs=c("batch", "ASICS")
for(pkg in pkgs) {
suppressPackageStartupMessages( stopifnot( library(pkg, quietly=TRUE, logical.return=TRUE, character.only=TRUE)))
cat(pkg,"\t",as.character(packageVersion(pkg)),"\n",sep="")
}
cat("\n")
## Checking arguments
##-------------------
error.stock <- "\n"
if(length(error.stock) > 1)
stop(error.stock)
## Computation
##------------
# annotation.Asics <- ASICS(directory, exclusion.areas=matrix(exclusionZonesBorders, byrow=T, ncol=2),
# max.shift=shift, which.spectra="last", library.metabolites=NULL,
# threshold.noise=0.02, seed=1234, nb.iter.signif=400)
annotation.Asics <- ASICS(directory, exclusion.areas=exclusionZonesBorders,
max.shift=shift, which.spectra="last", library.metabolites=NULL,
threshold.noise=0.02, seed=1234, nb.iter.signif=400)
## Saving
##-------
# Identified metabolites
metabolites.estimation <- present_metabolites(annotation.Asics)
colnames(metabolites.estimation) <- c("Metabolite",colnames(metabolites.estimation)[-1])
write.table(metabolites.estimation,file=argLs$proportionEstimation,row.names=FALSE,quote=FALSE,sep="\t")
## Graphical display
##------------------
# Raw and annotated spectra comparison
pdf(graphOut,onefile=TRUE)
## Graphical output: overlay of raw and estimated spectra
ppm.metabolites.estimation <- data.frame(round(ppm_grid(annotation.Asics),3),
original_mixture(annotation.Asics))
colnames(ppm.metabolites.estimation) <- c("PPM", "EstimatedProportion")
ppm.metabolites.estimation <- ppm.metabolites.estimation[order(ppm.metabolites.estimation[,1],decreasing=T), ]
mix <- data.frame(t(ppm.metabolites.estimation[,2]))
colnames(mix) <- ppm.metabolites.estimation[,1]
ppm <- ppm.metabolites.estimation[,1]
estimatedMix <- data.frame(round(ppm_grid(annotation.Asics),3), reconstituted_mixture(annotation.Asics))
colnames(estimatedMix) <- c("PPM","EstimatedProportion")
estimatedMix <- estimatedMix[order(estimatedMix[,1],decreasing=T), ]
estimatedMix <- estimatedMix[,2]
## Whole spectra
GraphRange <- 1:ncol(mix)
tempVal <- trunc(length(GraphRange)/10)
xPos <- c(10:0) * tempVal
plot(1:ncol(mix), mix, type='l', xlab="", main="", xaxt="n", ylab="")
axis(1, at=xPos, labels=colnames(mix)[xPos + 1])
lines(estimatedMix, col="red")
legend("topleft",legend=c("Real Mixture","Estimated Composition"),lty=c(1,1),col=c("black","red"))
## Zoomed spectral window depending on user-selected zone(s)
graphical.zone.length <- length(graphicalZonesBorders)
if (graphical.zone.length != 0)
# par(mfrow=c(2,1))
for (g in 1:graphical.zone.length)
{
print(g)
plot(1:length((which(round(as.numeric(colnames(mix)),2) == graphicalZonesBorders[[g]][1])[1]):(which(round(as.numeric(colnames(mix)),2) == max(graphicalZonesBorders[[g]][2],0.5))[1])),
mix[(which(round(as.numeric(colnames(mix)),2) == graphicalZonesBorders[[g]][1])[1]):(which(round(as.numeric(colnames(mix)),2) == max(graphicalZonesBorders[[g]][2],0.5))[1])], type='l', xlab="", ylab="Intensity", main="", xaxt="n")
lines(estimatedMix[(which(round(as.numeric(colnames(mix)),2) == graphicalZonesBorders[[g]][1])[1]):(which(round(as.numeric(colnames(mix)),2) == max(graphicalZonesBorders[[g]][2],0.5))[1])],col="red")
xPos <- 1
nAxisPos <- 4
startP <- length(nAxisPos)
endP <- length((which(round(as.numeric(colnames(mix)),2) == graphicalZonesBorders[[g]][1])[1]):(which(round(as.numeric(colnames(mix)),2) == max(graphicalZonesBorders[[g]][2],0.5))[1]))
GraphRange <- c(startP:endP)
tempVal <- trunc(length(GraphRange)/nAxisPos)
xPos <- c(0:nAxisPos) * tempVal
noms <- ppm.metabolites.estimation[xPos + which(ppm == round(graphicalZonesBorders[[g]][1],1))[1],1]
axis(1, at=xPos, labels=noms)
}
invisible(dev.off())
## Ending
##---------------------
cat("\nEnd of 'NMR annotation' Galaxy module call: ", as.character(Sys.time()), sep="")
options(stringsAsFactors=strAsFacL)
rm(list=ls())
sink()