-
Notifications
You must be signed in to change notification settings - Fork 1
/
SIMS_CBC_plot.R
343 lines (290 loc) · 11.3 KB
/
SIMS_CBC_plot.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# This script can make cycle-by-cycle plot from IMS 1280 asc file
# Absolute path for a directory containing SIMS .asc files
data.dir <- "/path/to/your/SIMS_data_directory"
# output.path should be end with '.pdf'
# If it's blank, path sets to "data.dir/[directory name].pdf"
# output.path <- file.path(data.dir, 'cycle_by_cycle.pdf')
output.path <- ''
# select element (oxygen, carbon)
# C and O are also acceptable
# TODO (Kouki): auto-detect
# element <- 'oxygen'
element <- 'carbon'
######################
# Plot configurations
######################
VSMOW <- 0.00200520
PDBO <- 0.0020672 # PDB oxygen
PDBC <- 0.0112372 # PDB carbon
data.headers <- c("delta",
"mass1",
"mass2",
"hydride",
"hydride.ratio")
if (element == 'carbon' | element == 'C') { # CARBON
mass1 <- "cps.V1" # 12C
mass2 <- "cps.V2" # 13C
hydride <- "cps.V3" # 13C1H
hydride.ratio <- "cps.V2" # 13C1H/13C
deltaScale <- PDBC
u.pri <- list('plot' = 'delta', # upper left
'unit' = '\u2030',
'label' = expression(paste(delta^{13}*C[raw], " [\u2030]")),
'color' = 'red2')
u.sec <- list('plot' = 'hydride.ratio', # upper right
'unit' = 'cps/cps',
'label' = expression(paste(''^{13}*C^{1}*H/''^{13}*C, " [cps/cps]")),
'color' = 'blue2')
l.pri <- list('plot' = 'mass2', # lower left
'unit' = 'cps',
'label' = expression(paste(''^{13}*C, " [cps]")),
'color' = 'seagreen')
l.sec <- list('plot' = 'hydride', # lower right
'unit' = 'cps',
'label' = expression(paste(''^{13}*C^{1}*H, " [cps]")),
'color' = 'darkorange')
} else if (element == 'oxygen' | element == 'O') { # OXYGEN
mass1 <- "cps.V1" # 16O
mass2 <- "cps.V3" # 18O
hydride <- "cps.V2" # 16O1H
hydride.ratio <- "cps.V1" # 16O1H/16O
deltaScale <- VSMOW
u.pri <- list('plot' = 'delta', # upper left
'unit' = '\u2030',
'label' = expression(paste(delta^{18}*O[raw], " [\u2030]")),
'color' = 'red2')
u.sec <- list('plot' = 'hydride.ratio', # upper right
'unit' = 'cps/cps',
'label' = expression(paste(''^{16}*O^{1}*H/''^{16}*O, " [cps/cps]")),
'color' = 'blue2')
l.pri <- list('plot' = 'mass1', # lower left
'unit' = 'cps',
'label' = expression(paste(''^{16}*O, " [cps]")),
'color' = 'seagreen')
l.sec <- list('plot' = 'hydride', # lower right
'unit' = 'cps',
'label' = expression(paste(''^{16}*O^{1}*H, " [cps]")),
'color' = 'darkorange')
}
# # # # # # # # # # # # # # # #
# Functions
# # # # # # # # # # # # # # # #
GetAscFileList <- function(dir.name, add.dir.name = TRUE) {
files <- list.files(dir.name, pattern="\\.asc$") # get .asc file list
file.num <- as.numeric(gsub('*@([0123456789]*)\\.asc$','\\1',files))
ascfiles <- files[order(file.num)] # sort file
if (add.dir.name) {
ascfiles <- file.path(dir.name, ascfiles)
}
return(ascfiles)
}
GetCommentList <- function(dir.name, spreadsheet=NULL) {
# import comment file
# comment file name: asc_comm.txt
# comment file format:
# Two columns [File, Comment] tab delimited
# If asc_comm.txt is not available, this function will try to find and read
# Excel spreadsheet
file.name <- basename(dir.name)
comment.file.name <- file.path(dir.name, "asc_comm.txt")
xls.file.name <- ifelse(!is.null(spreadsheet), spreadsheet,
file.path(dir.name, paste0(file.name, '.xls')))
comments <- NULL
if (file.exists(comment.file.name)) {
print("importing comments from asc_comm.txt")
comments <- read.table(comment.file.name,
header = T, sep = "\t", check.names = F,
comment.char = "", row.names = "File")
} else if (file.exists(xls.file.name)) {
library(xlsx) # reading xls file
xls <- read.xlsx(xls.file.name, sheetName = "Sum_table")
print(paste0("importing comments from excel file. [", basename(xls.file.name), "]"))
c <- subset(xls, !is.na(File) & !is.na(Comment), c(File, Comment))
comments <- data.frame(File = c$File, Comment = c$Comment)
write.table(comments, file = comment.file.name,
sep = "\t", quote = F, col.names = T, row.names = F)
comments <- transform(comments, row.names = 'File')
}
return(comments)
}
ParseAscFile <- function(ascfile) {
# Parse SIMS .asc file
#
tmpData <- read.csv(ascfile, header = F, blank.lines.skip = F,
as.is = F, fill = F, quote = "", sep = "`")
# get line numbers for cps data
limits <- try(grep('^#block', tmpData[,1], ignore.case = T))
limits[1] = as.numeric(limits[1]) + 3
limits[2] = as.numeric(limits[2]) - 3
# get cycle number
cycle <- limits[2] - limits[1] - 1
# get comment
comment <- gsub("\t", " ", gsub("\"", "", sub("\r", "", tmpData[8, 1])))
# parse cps data
d1 <- d2 <- d3 <- c()
for (i in seq(limits[1], limits[2])) {
tmpSepData <- as.numeric(
gsub('\\s+', '', strsplit(as.character(tmpData[i, 1]), "\t")[[1]])
)
if (length(tmpSepData) > 0 ) {
d1 <- c(d1, tmpSepData[3])
d2 <- c(d2, tmpSepData[4])
d3 <- c(d3, tmpSepData[5])
}
}
cps <- data.frame(V1 = d1, V2 = d2, V3 = d3)
return(c('cps' = cps, 'comment' = comment, 'cycle' = cycle))
}
pdf2 <- function(file, width = 0, height = 0, onefile, family, title, fonts,
version, paper = "us", encoding = "MacRoman", bg, fg,
pointsize, pagecentre, colormodel, useDingbats, useKerning,
fillOddEven, compress) {
grDevices::pdf(file=file, width=width, height=height, onefile, family, title,
fonts, version, paper=paper, encoding=encoding, bg, fg,
pointsize, pagecentre, colormodel, useDingbats, useKerning,
fillOddEven, compress)
}
# ##################
# Plotting functions
# ##################
PlotDataByCycle <- function(data, cycle, ylim, color, pch = 15, axes = TRUE) {
plot(data,
xlim = c(1, cycle), ylim = ylim, col = color, pch = pch, axes = axes,
main = '',
type = 'b', lty = 1, cex = 0.9,
xlab = '', ylab = '', yaxt = 'n', xaxt = 'n')
}
PlotPrimaryY <- function(data, target, stat, x.minor.ticks = FALSE) {
Y <- target$plot
cycle <- length(d[, 1])
limits <- stat$limits[[Y]]
average <- stat$ave[[Y]]
color <- target$color
PlotDataByCycle(data[[Y]], cycle, limits, color)
# Major ticks for Y axis
axis(side = 2, col.axis = color, las = 1, hadj = 0.75, tcl = 0.5)
# label for Y axis
mtext(target$label, side = 2, padj = -4, col = color)
AddYMinorTicks(x.minor.ticks)
# Major ticks for x axis
if (isTRUE(x.minor.ticks)) {
axis(side = 1, tcl = 0, at = axTicks(1), padj = -1.5)
axis(side = 3, tcl = 0.5, at = axTicks(1), padj = -1.5, labels=F)
} else {
axis(side = 1, labels=F, at = axTicks(1), tcl = 0.5)
}
# line for average value
abline(h = average, lwd = 0.5, col = color, lty = 3)
# text of average and 2SE
mtext(paste0(format(round(average, 2), nsmall = 2), " \u00B1 ",
format(round(stat$se[[Y]], 2), nsmall = 2), " [", target$unit, "] (2SE)"),
side = 3, padj = 1.7, adj = 0.98, col = color, cex = 0.9)
}
PlotSecondaryY <- function(data, target, stat) {
Y <- target$plot
cycle <- length(d[, 1])
limits <- stat$limits[[Y]]
average <- stat$ave[[Y]]
color <- target$color
par(new=T)
PlotDataByCycle(d[[Y]], cycle, limits, color, 1, FALSE)
axis(side = 4, at = axTicks(2),
format(axTicks(2), digits = 2, nsmall = 2, scientific = T),
las = 1, hadj = 0.3, tcl = 0.5, col.axis = color)
abline(h = average, lwd = 0.5, col = color, lty = 3)
# secondary label
mtext(target$label, side = 4, padj = 4.5, col = color)
# average and 2SE
abline(h = average, lwd = 0.5, col = color, lty = 3)
mtext(paste0(format(average, digits = 2, nsmall = 2, scientific = T),
" \u00B1 ",
format(stat$se[[Y]], digits = 2, nsmall = 2, scientific = T),
" [", target$unit, "] (2SE)"),
side = 3, padj = 3.1, adj = 0.98, col = color, cex = 0.9)
}
AddTitle <- function(title, subtitle) {
mtext(paste("(", basename(subtitle), ")", sep = ""), 3, padj = -0.5)
title(main=title)
}
AddYMinorTicks <- function(minorX=FALSE) {
library(Hmisc) # for minor ticks
minor.ticks.X <- axTicks(1)[2] - axTicks(1)[1]
minor.ticks.Y <- axTicks(2)[2] - axTicks(2)[1]
minor.ticks.Y <- minor.ticks.Y / 10 ^ floor(log10(minor.ticks.Y))
if (minor.ticks.Y == 1) {
minor.ticks.Y <- 5
} else if (minor.ticks.Y == 2) {
minor.ticks.Y <- 4
}
if (isTRUE(minorX)) {
minor.tick(minor.ticks.X, minor.ticks.Y, -0.5)
} else {
minor.tick(0, minor.ticks.Y, -0.5)
}
}
##################################################
# MAIN ROUTINE
##################################################
if (!file.exists(data.dir)) {
stop(paste("Directory [", data.dir, "] doesn't exist"))
}
if (output.path == '') {
output.path = file.path(data.dir, paste0(basename(data.dir), '.pdf'))
}
ascfiles <- GetAscFileList(data.dir) # get list of .asc files
pdf2(output.path, paper="us") # set output device
par(mfrow=c(2, 1), oma=c(0,0,0,0)) # set print margins
comments <- GetCommentList(data.dir) # get comments from Spreadsheet
# ============
# MAIN LOOP
# ============
for (filename in ascfiles) {
asc.data <- ParseAscFile(filename)
comment <- ifelse(!is.null(comments),
as.character(comments[basename(filename),]),
asc.data$comment)
print(paste(basename(filename), ":", comment))
d <- data.frame(delta = ((asc.data[[mass2]] / asc.data[[mass1]]) / deltaScale - 1) * 1000,
mass1 = asc.data[[mass1]],
mass2 = asc.data[[mass2]],
hydride = asc.data[[hydride]],
hydride.ratio = asc.data[[hydride]] / asc.data[[hydride.ratio]])
margin <- 0.15 * c(-1, 1) # set 15% margin on Y-axes for legends
stat <- list()
for (i in data.headers) {
tmp <- d[[i]]
tmp.range <- range(tmp) + margin * abs(diff(range(tmp, na.rm = T)))
stat$ave[[i]] <- mean(tmp, na.rm = T)
stat$se[[i]] <- sd(tmp, na.rm = T) / sqrt(length(tmp)) * 2
stat$limits[[i]] <- tmp.range # c(min, max)
}
# ===================
# Graph drawing
# ===================
# ==================
# upper plot
# ------------------
par(mar=c(0, 5.5, 4, 5.5))
PlotPrimaryY(d, u.pri, stat)
PlotSecondaryY(d, u.sec, stat)
AddTitle(comment, filename)
# legend for upper plot
legend("bottomright", legend = c(u.pri$label, u.sec$label),
col = c(u.pri$color, u.sec$color), pch = c(15, 1), ncol = 2,
bg = "white")
# ==================
# lower plot
# ------------------
par(mar=c(5, 5.5, 0, 5.5))
PlotPrimaryY(d, l.pri, stat, x.minor.ticks = TRUE)
PlotSecondaryY(d, l.sec, stat)
# legend for lower plot
legend("bottomleft", legend = c(l.pri$label, l.sec$label),
col = c(l.pri$color, l.sec$color),
pch = c(15, 1), ncol = 2, bg = "white")
# x label
mtext("Cycle #", side = 1, line = 1, padj = 1.5)
} # end of main loop
dev.off() # shuts down pdf writer
# open pdf file
system(paste(options('pdfviewer'), output.path))