-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathReadFids_script.R
404 lines (324 loc) · 13.3 KB
/
ReadFids_script.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
################################################################################################
#
# Read FIDs in Bruker format
#
#
################################################################################################
# vec2mat ==============================================================================
vec2mat <- function(vec) {
return(matrix(vec, nrow = 1, dimnames = list(c(1), names(vec))))
}
# ReadFid ==============================================================================
ReadFid <- function(path) {
# Read 1D FID using Bruker XWinNMR and TopSpin format. It is inspired of the
# matNMR matlab library which deals with 2D FID and also other formats
# Read also the parameters in the acqus file
paramFile <- file.path(path, "acqus")
# BYTEORDA: 0 -> Little Endian 1 -> Big Endian
params <- readParams(paramFile, c("TD", "BYTORDA", "DIGMOD", "DECIM", "DSPFVS",
"SW_h", "SW", "O1"))
if (params[["DSPFVS"]] >= 20) {
# The group delay first order phase correction is given directly from version 20
grpdly <- readParams(paramFile, c("GRPDLY"))
params[["GRPDLY"]] <- grpdly[["GRPDLY"]]
}
TD <- params[["TD"]]
endianness <- if (params$BYTORDA)
"big" else "little"
if (TD%%2 != 0) {
stop(paste("Only even numbers are allowed for size in TD because it is complex
data with the real and imaginary part for each element.",
"The TD value is in the", paramFile, "file"))
}
# Interpret params Dwell Time, time between 2 data points in the FID
params[["DT"]] <- 1/(2 * params[["SW_h"]])
# Read fid
fidFile <- file.path(path, "fid")
fidOnDisk <- readBin(fidFile, what = "int", n = TD, size = 4L, endian = endianness)
# Real size that is on disk (it should be equal to TD2, except for TopSpin/Bruker
# (which is our case) according to matNMR as just discussed
TDOnDisk <- length(fidOnDisk)
if (TDOnDisk < TD) {
warning("Size is smaller than expected, the rest is filled with zero so the size is the same for every fid")
fidGoodSize <- sapply(vector("list", length = TD), function(x) 0)
fidGoodSize[1:TDOnDisk] <- fidOnDisk
} else if (TDOnDisk > TD) {
warning("Size is bigger than expected, the rest ignored so the size is the same for every fid")
fidGoodSize <- fidOnDisk(1:TD)
} else {
fidGoodSize <- fidOnDisk
}
fidRePart <- fidGoodSize[seq(from = 1, to = TD, by = 2)]
fidImPart <- fidGoodSize[seq(from = 2, to = TD, by = 2)]
fid <- complex(real = fidRePart, imaginary = fidImPart)
return(list(fid = fid, params = params))
}
# getDirsContainingFid ==============================================================================
getDirsContainingFid <- function(path) {
subdirs <- dir(path, full.names = TRUE)
if (length(subdirs) > 0) {
cond <- sapply(subdirs, function(x) {
content <- dir(x)
# subdirs must contain fid, acqu and acqus files
return("fid" %in% content && "acqu" %in% content && "acqus" %in% content)
})
subdirs <- subdirs[cond]
}
return(subdirs)
}
# beginTreatment ==============================================================================
beginTreatment <- function(name, Signal_data = NULL, Signal_info = NULL,
force.real = FALSE) {
cat("Begin", name, "\n")
# Formatting the Signal_data and Signal_info -----------------------
vec <- is.vector(Signal_data)
if (vec) {
Signal_data <- vec2mat(Signal_data)
}
if (is.vector(Signal_info)) {
Signal_info <- vec2mat(Signal_info)
}
if (!is.null(Signal_data)) {
if (!is.matrix(Signal_data)) {
stop("Signal_data is not a matrix.")
}
if (!is.complex(Signal_data) && !is.numeric(Signal_data)) {
stop("Signal_data contains non-numerical values.")
}
}
if (!is.null(Signal_info) && !is.matrix(Signal_info)) {
stop("Signal_info is not a matrix.")
}
Original_data <- Signal_data
# Extract the real part of the spectrum ---------------------------
if (force.real) {
if (is.complex(Signal_data)) {
Signal_data <- Re(Signal_data)
} else {
# The signal is numeric Im(Signal_data) is zero anyway so let's avoid
# using complex(real=...,imaginary=0) which would give a complex signal
# in endTreatment()
force.real <- FALSE
}
}
# Return the formatted data and metadata entries --------------------
return(list(start = proc.time(), vec = vec, force.real = force.real,
Original_data = Original_data, Signal_data = Signal_data, Signal_info = Signal_info))
}
# endTreatment ==============================================================================
endTreatment <- function(name, begin_info, Signal_data) {
end_time = proc.time() # record it as soon as possible
start_time = begin_info[["start"]]
delta_time = end_time - start_time
delta = delta_time[]
cat("End", name, "\n")
cat("It lasted",
round(delta["user.self"], 3), "s user time,",
round(delta["sys.self"] , 3), "s system time and",
round(delta["elapsed"] , 3), "s elapsed time.\n")
if (begin_info[["force.real"]]) {
# The imaginary part is left untouched
i <- complex(real=0, imaginary=1)
Signal_data = Signal_data + i * Im(begin_info[["Original_data"]])
}
if (begin_info[["vec"]]) {
Signal_data = Signal_data[1,]
}
return(Signal_data)
}
# checkArg ==============================================================================
checkArg <- function(arg, checks, can.be.null=FALSE) {
check.list <- list(bool=c(is.logical, "a boolean"),
int =c(function(x){x%%1==0}, "an integer"),
num =c(is.numeric, "a numeric"),
str =c(is.character, "a string"),
pos =c(function(x){x>0}, "positive"),
pos0=c(function(x){x>=0}, "positive or zero"),
l1 =c(function(x){length(x)==1}, "of length 1")
)
if (is.null(arg)) {
if (!can.be.null) {
stop(deparse(substitute(arg)), " is null.")
}
} else {
if (is.matrix(arg)) {
stop(deparse(substitute(arg)), " is not scalar.")
}
for (c in checks) {
if (!check.list[[c]][[1]](arg)) {
stop(deparse(substitute(arg)), " is not ", check.list[[c]][[2]], ".")
}
}
}
}
# getArg ==============================================================================
getArg <- function(arg, info, argname, can.be.absent=FALSE) {
if (is.null(arg)) {
start <- paste("impossible to get argument", argname, "it was not given directly and");
if (!is.matrix(info)) {
stop(paste(start, "the info matrix was not given"))
}
if (!(argname %in% colnames(info))) {
if (can.be.absent) {
return(NULL)
} else {
stop(paste(start, "is not in the info matrix"))
}
}
if (nrow(info) < 1) {
stop(paste(start, "the info matrix has no row"))
}
arg <- info[1,argname]
if (is.na(arg)) {
stop(paste(start, "it is NA in the info matrix"))
}
}
return(arg)
}
# getTitle ==============================================================================
# Get the name of the signal from the title file or fromt the name of the subdirectory
# Get the name of the signal from the title file or fromt the name of the subdirectory
getTitle <- function(path, l, subdirs) {
title <- NULL
title_file <- file.path(file.path(file.path(path, "pdata"), "1"), "title")
if (file.exists(title_file)) {
lines <- readLines(title_file, warn = FALSE)
if (length(lines) >= 1) {
first_line <- gsub("^\\s+|\\s+$", "", lines[l])
if (nchar(first_line) >= 1) {
title <- first_line
} else {
warning(paste("The", l ,"line of the title file is blank for directory ",
path, "and the (sub)dirs names are used instead"))
}
} else {
warning(paste("The title file is empty for directory ", path, "and the (sub)dirs names are used instead"))
}
} else {
warning(paste("Title file doesn't exists for directory ", path, "\n the (sub)dirs names are used instead"))
}
if (is.null(title)) {
if(subdirs) {
separator <- .Platform$file.sep
path_elem <- strsplit(path,separator)[[1]]
title <- paste(path_elem[length(path_elem)-1], path_elem[length(path_elem)], sep = "_")
} else{title <- basename(path)}
}
return(title)
}
# readParams ==============================================================================
# Read parameter values for Fid_info in the ReadFids function
readParams <- function(file, paramsName) {
isDigit <- function(c) {
return(suppressWarnings(!is.na(as.numeric(c))))
}
lines <- readLines(file)
params <- sapply(paramsName, function(x) NULL)
for (paramName in paramsName) {
# Find the line with the parameter I add a '$' '=' in the pattern so that for
# example 'TD0' is not found where I look for 'TD' and LOCSW and WBSW when I look
# for 'SW'
pattern <- paste("\\$", paramName, "=", sep = "")
occurences <- grep(pattern, lines)
if (length(occurences) == 0L) {
stop(paste(file, "has no field", pattern))
}
if (length(occurences) > 1L) {
warning(paste(file, "has more that one field", pattern, " I take the first one"))
}
line <- lines[occurences[1]]
# Cut beginning and end of the line '##$TD= 65536' -> '65536'
igual = as.numeric(regexpr("=", line))
first <- igual
while (first <= nchar(line) & !isDigit(substr(line, first, first))) {
first <- first + 1
}
last <- nchar(line)
while (last > 0 & !isDigit(substr(line, last, last))) {
last <- last - 1
}
params[paramName] <- as.numeric(substr(line, first, last))
}
return(params)
}
# ReadFids ==============================================================================
ReadFids <- function(path, l = 1, subdirs = FALSE, dirs.names = FALSE) {
# Data initialisation and checks ----------------------------------------------
begin_info <- beginTreatment("ReadFids")
checkArg(path, c("str"))
checkArg(l, c("pos"))
if (file.exists(path) == FALSE) {
stop(paste("Invalid path:", path))
}
# Extract the FIDs and their info ----------------------------------------------
if (subdirs == FALSE) {
fidDirs <- getDirsContainingFid(path)
n <- length(fidDirs)
if (n == 0L) {
stop(paste("No valid fid in", path))
}
if (dirs.names) {
separator <- .Platform$file.sep
path_elem <- strsplit(fidDirs,separator)
fidNames <- sapply(path_elem, function(x) x[[length(path_elem[[1]])]])
}else {fidNames <- sapply(X = fidDirs, FUN = getTitle, l = l, subdirs = subdirs, USE.NAMES = F)}
for (i in 1:n) {
fidList <- ReadFid(fidDirs[i])
fid <- fidList[["fid"]]
info <- fidList[["params"]]
m <- length(fid)
if (i == 1) {
Fid_data <- matrix(nrow = n, ncol = m, dimnames = list(fidNames,
info[["DT"]] * (0:(m - 1))))
Fid_info <- matrix(nrow = n, ncol = length(info), dimnames = list(fidNames,
names(info)))
}
Fid_data[i, ] <- fid
Fid_info[i, ] <- unlist(info)
}
} else {
maindirs <- dir(path, full.names = TRUE) # subdirectories
Fid_data <- numeric()
Fid_info <- numeric()
fidDirs <- c()
for (j in maindirs) {
fd <- getDirsContainingFid(j) # recoved FIDs from subdirectories
n <- length(fd)
if (n > 0L) {
fidDirs <- c(fidDirs, fd)
} else {warning(paste("No valid fid in",j ))}
}
if (dirs.names==TRUE) {
if (length(fidDirs)!= length(dir(path))) { # at least one subdir contains more than 1 FID
separator <- .Platform$file.sep
path_elem <- strsplit(fidDirs,separator)
fidNames <- sapply(path_elem, function(x) paste(x[[length(path_elem[[1]])-1]],
x[[length(path_elem[[1]])]], sep = "_"))
}else {fidNames <- dir(path)}
} else {fidNames <- sapply(X = fidDirs, FUN = getTitle, l = l, subdirs = subdirs, USE.NAMES = F)}
for (i in 1:length(fidNames)) {
fidList <- ReadFid(fidDirs[i])
fid <- fidList[["fid"]]
info <- fidList[["params"]]
m <- length(fid)
if (i == 1) {
Fid_data <- matrix(nrow = length(fidNames), ncol = m, dimnames = list(fidNames,
info[["DT"]] * (0:(m - 1))))
Fid_info <- matrix(nrow = length(fidNames), ncol = length(info), dimnames = list(fidNames,
names(info)))
}
Fid_data[i, ] <- fid
Fid_info[i, ] <- unlist(info)
}
}
# Check for non-unique IDs ----------------------------------------------
NonnuniqueIds <- sum(duplicated(row.names(Fid_data)))
cat("dim Fid_data: ", dim(Fid_data), "\n")
cat("IDs: ", rownames(Fid_data), "\n")
cat("non-unique IDs?", NonnuniqueIds, "\n")
if (NonnuniqueIds > 0) {
warning("There are duplicated IDs: ", Fid_data[duplicated(Fid_data)])
}
# Return the results ----------------------------------------------
return(list(Fid_data = endTreatment("ReadFids", begin_info, Fid_data), Fid_info = Fid_info))
}