-
Notifications
You must be signed in to change notification settings - Fork 1
/
.Rhistory
512 lines (512 loc) · 15.4 KB
/
.Rhistory
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
binMatrix = binMat,
coord = coord,
method = "tpoint")
View(pp)
dim(ionImages)
ii2 <- array(c(ionImages, ionImages))
dim(ii2)
ii2 <- array(c(ionImages, ionImages), dim = c(dim(ionImages)[c(1,2)], dim(ionImages)[3]*2))
dim(ii2)
image(ii2[,,3])
image(ii2[,,1])
image(ii2[,,4])
image(ii2[,,6])
#' \code{threshold} numeric, the threshold estimated by \code{tpoint}-function.
#' \code{spectraIdx} list of integer vectors containing the indecies of the spectra in the list of \code{MALDIquant::massObject}.
#' Also, there is another entry called \code{unified}: The result of all binarized ion images combined.
#' In addition to \code{conComp}, \code{binMat}, and \code{spectraIdx} that contain the equivalent matrices of the unified data as described above this entry contains the following:
#' \code{intensities} list of lists containing the intensities of the corresponding pixels for the provided ion images
#'
#' @export
#'
#' @examples
#' pp <- plaquePicker(NLGF67w_mouse1_rep1, coord = NLGF67w_mouse1_rep1_coord)
plaquePicker <- function(ionImages, coord, method = c("tpoint", "geometric", "peak"), binMatrix = NULL, addIonImages = NULL, ...) {
if(!is.null(binMatrix)) {
# check if dimof ionImages and binMatrix match. Otherwise stop.
if(!dim(ionImages[,,1])[1] == dim(binMatrix)[1] & !dim(ionImages[,,1])[2] == dim(binMatrix)[2]) {
stop("Dimensions of binMatrix has to be the same as ionImages! \n
!dim(ionImages[,,1]) == dim(binMatrix)\n")
}
method <- "binMat"
} else {
method <- match.arg(method)
}
if(!is.null(addIonImages)) {
# check if dim of ionImages and addIonImages match. Otherwise stop.
if(!dim(ionImages[,,1])[1] == dim(addIonImages)[1] & !dim(ionImages[,,1])[2] == dim(addIonImages)[2]) {
stop("Dimensions of addIonImages has to be the same as ionImages! \n
!dim(ionImages[,,1]) == dim(addIonImages)\n")
}
}
mzValues <- attr(ionImages, "center")
resultList <- vector("list", length = length(mzValues)+1)
# empty matrix for storage of unified binary image
uni <- matrix(data = 0, nrow = dim(ionImages[,,1])[1],
ncol = dim(ionImages[,,1])[2])
# name cols and rows of uni
# according to the original coordinate system in MALDIQuant object
rownames(uni) <- sort(unique(coord[,1])) # x
colnames(uni) <- sort(unique(coord[,2])) # y
for(i in 1:length(mzValues)) {
cat("processing", mzValues[i], i , "/", length(mzValues) ,"\n")
# get threshold and perform binarization
ints <- as.vector(ionImages[,,i])
switch(method,
"tpoint" = {
threshold <- tpoint(ints, ...)
cat("\n threshold of mz", mzValues[i],"based on t-point thresholding = ", threshold, "\n")
bin <- ifelse(test = threshold < ints,
yes = 1,
no = ifelse(is.na(ints),
yes = NA,
no = 0))
},
"geometric" = {
threshold <- geometricThreshold(ints, ...)
cat("\n threshold of mz", mzValues[i],"based on geometric thresholding = ", threshold, "\n")
bin <- ifelse(test = threshold < ints,
yes = 1,
no = ifelse(is.na(ints),
yes = NA,
no = 0))
},
"peak" = {
cat("\n no threshold needed for mz", mzValues[i],"data considered as peaks\n")
bin <- ifelse(test = ints > 0,
yes = 1,
no = ifelse(is.na(ints),
yes = NA,
no = 0))
threshold <- -Inf
},
"binMat" = {
cat("\n no threshold needed for mz", mzValues[i],"binMatrix used for segmentation\n")
bin <- binMatrix
threshold <- -Inf
}
)
# rebuild the matrix
binMat <- matrix(bin,
nrow = dim(ionImages[,,i])[1],
ncol = dim(ionImages[,,i])[2])
# perfrom connceted component labeling
conComp <- raster::as.matrix(raster::clump(raster::raster(binMat),
direction = 8))
# name cols and rows of conComp
# according to the original coordinate system in MALDIQuant object
rownames(conComp) <- sort(unique(coord[,1])) # x
colnames(conComp) <- sort(unique(coord[,2])) # y
resultList[[i]][["conComp"]] <- conComp
resultList[[i]][["binMat"]] <- binMat
resultList[[i]][["threshold"]] <- threshold
cat("extracting clump information...\n")
resultList[[i]][["spectraIdx"]] <- get_specIdx(comp = conComp, coord = coord)
# add binMat to unified binary image
uni <- uni + binMat
uni[uni > 0] <- 1
}
resultList[[length(resultList)]][["uniBin"]] <- uni
uniComp <- raster::as.matrix(raster::clump(raster::raster(uni),
direction = 8))
# name cols and rows of uniComp
rownames(uniComp) <- rownames(uni)
colnames(uniComp) <- colnames(uni)
resultList[[length(resultList)]][["uniComp"]] <- uniComp
cat("extracting unified clump information...\n")
resultList[[length(resultList)]][["spectraIdx"]] <- get_specIdx(comp = uniComp, coord = coord)
names(resultList) <- c(mzValues, "unified")
if(!is.null(addIonImages)) {
} else {
d <- dim(ionImages)
allIonImages <- array(data = c(ionImages, addIonImages), dim = c(d[1:2], d[3]*2))
resultList[[length(resultList)]][["intensities"]] <- get_intensities(comp = uniComp, ii = allIonImages)
}
return(resultList)
}
pp <- plaquePicker(ionImages = ionImages,
addIonImages = ionImages[,,1],
coord = coord,
method = "tpoint")
source('W:/1-DrArbeitPhD/1 - phd/32-Thomas Enzlein/Paper contributions/Enzlein 2020/mousemodels_series/R/PlaquePicker0.1/PlaquePicker/R/plaquePicker.R', echo=TRUE)
pp <- plaquePicker(ionImages = ionImages,
addIonImages = ionImages[,,1],
coord = coord,
method = "tpoint")
View(pp)
#' \code{threshold} numeric, the threshold estimated by \code{tpoint}-function.
#' \code{spectraIdx} list of integer vectors containing the indecies of the spectra in the list of \code{MALDIquant::massObject}.
#' Also, there is another entry called \code{unified}: The result of all binarized ion images combined.
#' In addition to \code{conComp}, \code{binMat}, and \code{spectraIdx} that contain the equivalent matrices of the unified data as described above this entry contains the following:
#' \code{intensities} list of lists containing the intensities of the corresponding pixels for the provided ion images
#'
#' @export
#'
#' @examples
#' pp <- plaquePicker(NLGF67w_mouse1_rep1, coord = NLGF67w_mouse1_rep1_coord)
plaquePicker <- function(ionImages, coord, method = c("tpoint", "geometric", "peak"), binMatrix = NULL, addIonImages = NULL, ...) {
if(!is.null(binMatrix)) {
# check if dimof ionImages and binMatrix match. Otherwise stop.
if(!dim(ionImages[,,1])[1] == dim(binMatrix)[1] & !dim(ionImages[,,1])[2] == dim(binMatrix)[2]) {
stop("Dimensions of binMatrix has to be the same as ionImages! \n
!dim(ionImages[,,1]) == dim(binMatrix)\n")
}
method <- "binMat"
} else {
method <- match.arg(method)
}
if(!is.null(addIonImages)) {
# check if dim of ionImages and addIonImages match. Otherwise stop.
if(!dim(ionImages[,,1])[1] == dim(addIonImages)[1] & !dim(ionImages[,,1])[2] == dim(addIonImages)[2]) {
stop("Dimensions of addIonImages has to be the same as ionImages! \n
!dim(ionImages[,,1]) == dim(addIonImages)\n")
}
}
mzValues <- attr(ionImages, "center")
resultList <- vector("list", length = length(mzValues)+1)
# empty matrix for storage of unified binary image
uni <- matrix(data = 0, nrow = dim(ionImages[,,1])[1],
ncol = dim(ionImages[,,1])[2])
# name cols and rows of uni
# according to the original coordinate system in MALDIQuant object
rownames(uni) <- sort(unique(coord[,1])) # x
colnames(uni) <- sort(unique(coord[,2])) # y
for(i in 1:length(mzValues)) {
cat("processing", mzValues[i], i , "/", length(mzValues) ,"\n")
# get threshold and perform binarization
ints <- as.vector(ionImages[,,i])
switch(method,
"tpoint" = {
threshold <- tpoint(ints, ...)
cat("\n threshold of mz", mzValues[i],"based on t-point thresholding = ", threshold, "\n")
bin <- ifelse(test = threshold < ints,
yes = 1,
no = ifelse(is.na(ints),
yes = NA,
no = 0))
},
"geometric" = {
threshold <- geometricThreshold(ints, ...)
cat("\n threshold of mz", mzValues[i],"based on geometric thresholding = ", threshold, "\n")
bin <- ifelse(test = threshold < ints,
yes = 1,
no = ifelse(is.na(ints),
yes = NA,
no = 0))
},
"peak" = {
cat("\n no threshold needed for mz", mzValues[i],"data considered as peaks\n")
bin <- ifelse(test = ints > 0,
yes = 1,
no = ifelse(is.na(ints),
yes = NA,
no = 0))
threshold <- -Inf
},
"binMat" = {
cat("\n no threshold needed for mz", mzValues[i],"binMatrix used for segmentation\n")
bin <- binMatrix
threshold <- -Inf
}
)
# rebuild the matrix
binMat <- matrix(bin,
nrow = dim(ionImages[,,i])[1],
ncol = dim(ionImages[,,i])[2])
# perfrom connceted component labeling
conComp <- raster::as.matrix(raster::clump(raster::raster(binMat),
direction = 8))
# name cols and rows of conComp
# according to the original coordinate system in MALDIQuant object
rownames(conComp) <- sort(unique(coord[,1])) # x
colnames(conComp) <- sort(unique(coord[,2])) # y
resultList[[i]][["conComp"]] <- conComp
resultList[[i]][["binMat"]] <- binMat
resultList[[i]][["threshold"]] <- threshold
cat("extracting clump information...\n")
resultList[[i]][["spectraIdx"]] <- get_specIdx(comp = conComp, coord = coord)
# add binMat to unified binary image
uni <- uni + binMat
uni[uni > 0] <- 1
}
resultList[[length(resultList)]][["uniBin"]] <- uni
uniComp <- raster::as.matrix(raster::clump(raster::raster(uni),
direction = 8))
# name cols and rows of uniComp
rownames(uniComp) <- rownames(uni)
colnames(uniComp) <- colnames(uni)
resultList[[length(resultList)]][["uniComp"]] <- uniComp
cat("extracting unified clump information...\n")
resultList[[length(resultList)]][["spectraIdx"]] <- get_specIdx(comp = uniComp, coord = coord)
names(resultList) <- c(mzValues, "unified")
if(!is.null(addIonImages)) {
d <- dim(ionImages)
allIonImages <- array(data = c(ionImages, addIonImages), dim = c(d[1:2], d[3]*2))
resultList[[length(resultList)]][["intensities"]] <- get_intensities(comp = uniComp, ii = allIonImages)
} else {
resultList[[length(resultList)]][["intensities"]] <- get_intensities(comp = uniComp, ii = ionImages)
}
return(resultList)
}
pp <- plaquePicker(ionImages = ionImages,
addIonImages = ionImages[,,1],
coord = coord,
method = "tpoint")
debugonce(get_intensities)
pp <- plaquePicker(ionImages = ionImages,
addIonImages = ionImages[,,1],
coord = coord,
method = "tpoint")
source('W:/1-DrArbeitPhD/1 - phd/32-Thomas Enzlein/Paper contributions/Enzlein 2020/mousemodels_series/R/PlaquePicker0.1/PlaquePicker/R/plaquePicker.R', echo=TRUE)
pp <- plaquePicker(ionImages = ionImages,
addIonImages = ionImages[,,1],
coord = coord,
method = "tpoint")
View(pp)
debugonce(get_intensities)
pp <- plaquePicker(ionImages = ionImages,
addIonImages = ionImages[,,1],
coord = coord,
method = "tpoint")
d <- dim(ionImages)
allIonImages <- array(data = c(ionImages, ionImages[,,1]), dim = c(d[1:2], d[3]*2))
attr(allIonImages, "center")
attr(ionImages, "center")
attr(ionImages[,,1], "center")
add <- ionImages[,,1:2]
attr(add, "center") <- attr(ionImages, "center")[1:2]
attr(add, "center")
pp <- plaquePicker(ionImages = ionImages,
addIonImages = add,
coord = coord,
method = "tpoint")
View(pp)
View(pp)
file.remove("NLGF_Prot_NLGF1.imzML")
file.remove("NLGF_Prot_NLGF1.ibd")
library(PlaquePicker)
library(PlaquePicker)
library(PlaquePicker)
library(PlaquePicker)
library(PlaquePicker)
devtools::document()
library(PlaquePicker)
debugonce(plaquePicker)
plaquePicker()
devtools::document()
library(PlaquePicker)
library(PlaquePicker)
devtools::document()
library(PlaquePicker)
devtools::document()
library(PlaquePicker)
data("NLGF67w_mouse1_rep1")
binarizeAndCombine <- function(im, method = c("tpoint", "geometric", "peak"), binMatrix) {
f(!is.null(binMatrix)) {
# check if dimof ionImages and binMatrix match. Otherwise stop.
if(!dim(im)[1] == dim(binMatrix)[1] & !dim(im)[2] == dim(im)[2]) {
stop("Dimensions of binMatrix has to be the same as ionImages! \n
!dim(ionImages[,,1]) == dim(binMatrix)\n")
}
method <- "binMat"
} else {
method <- match.arg(method)
}
if(is.matrix(im)) {
z <- 1
} else {
z <- dim(im)[3]
}
uni <- matrix(data = 0, nrow = dim(im)[1],
ncol = dim(im)[2])
for(i in 1:z) {
if(is.matrix(im)) {
ints <- as.vector(im)
} else {
ints <- as.vector(im[,,i])
}
switch(method,
"tpoint" = {
threshold <- tpoint(ints, ...)
bin <- ifelse(test = threshold < ints,
yes = 1,
no = ifelse(is.na(ints),
yes = NA,
no = 0))
},
"geometric" = {
threshold <- geometricThreshold(ints, ...)
bin <- ifelse(test = threshold < ints,
yes = 1,
no = ifelse(is.na(ints),
yes = NA,
no = 0))
},
"peak" = {
bin <- ifelse(test = ints > 0,
yes = 1,
no = ifelse(is.na(ints),
yes = NA,
no = 0))
threshold <- -Inf
},
"binMat" = {
bin <- binMatrix
threshold <- -Inf
}
)
# rebuild the matrix
binMat <- matrix(bin,
nrow = dim(ionImages[,,i])[1],
ncol = dim(ionImages[,,i])[2])
uni <- uni + binMat
uni[uni > 0] <- 1
}
return(uni)
}
binarizeAndCombine <- function(im, method = c("tpoint", "geometric", "peak"), binMatrix) {
if(!is.null(binMatrix)) {
# check if dimof ionImages and binMatrix match. Otherwise stop.
if(!dim(im)[1] == dim(binMatrix)[1] & !dim(im)[2] == dim(im)[2]) {
stop("Dimensions of binMatrix has to be the same as ionImages! \n
!dim(ionImages[,,1]) == dim(binMatrix)\n")
}
method <- "binMat"
} else {
method <- match.arg(method)
}
if(is.matrix(im)) {
z <- 1
} else {
z <- dim(im)[3]
}
uni <- matrix(data = 0, nrow = dim(im)[1],
ncol = dim(im)[2])
for(i in 1:z) {
if(is.matrix(im)) {
ints <- as.vector(im)
} else {
ints <- as.vector(im[,,i])
}
switch(method,
"tpoint" = {
threshold <- tpoint(ints, ...)
bin <- ifelse(test = threshold < ints,
yes = 1,
no = ifelse(is.na(ints),
yes = NA,
no = 0))
},
"geometric" = {
threshold <- geometricThreshold(ints, ...)
bin <- ifelse(test = threshold < ints,
yes = 1,
no = ifelse(is.na(ints),
yes = NA,
no = 0))
},
"peak" = {
bin <- ifelse(test = ints > 0,
yes = 1,
no = ifelse(is.na(ints),
yes = NA,
no = 0))
threshold <- -Inf
},
"binMat" = {
bin <- binMatrix
threshold <- -Inf
}
)
# rebuild the matrix
binMat <- matrix(bin,
nrow = dim(ionImages[,,i])[1],
ncol = dim(ionImages[,,i])[2])
uni <- uni + binMat
uni[uni > 0] <- 1
}
return(uni)
}
image(binarizeAndCombine(NLGF67w_mouse1_rep1))
binarizeAndCombine <- function(im, method = c("tpoint", "geometric", "peak"), binMatrix = NULL) {
if(!is.null(binMatrix)) {
# check if dimof ionImages and binMatrix match. Otherwise stop.
if(!dim(im)[1] == dim(binMatrix)[1] & !dim(im)[2] == dim(im)[2]) {
stop("Dimensions of binMatrix has to be the same as ionImages! \n
!dim(ionImages[,,1]) == dim(binMatrix)\n")
}
method <- "binMat"
} else {
method <- match.arg(method)
}
if(is.matrix(im)) {
z <- 1
} else {
z <- dim(im)[3]
}
uni <- matrix(data = 0, nrow = dim(im)[1],
ncol = dim(im)[2])
for(i in 1:z) {
if(is.matrix(im)) {
ints <- as.vector(im)
} else {
ints <- as.vector(im[,,i])
}
switch(method,
"tpoint" = {
threshold <- tpoint(ints, ...)
bin <- ifelse(test = threshold < ints,
yes = 1,
no = ifelse(is.na(ints),
yes = NA,
no = 0))
},
"geometric" = {
threshold <- geometricThreshold(ints, ...)
bin <- ifelse(test = threshold < ints,
yes = 1,
no = ifelse(is.na(ints),
yes = NA,
no = 0))
},
"peak" = {
bin <- ifelse(test = ints > 0,
yes = 1,
no = ifelse(is.na(ints),
yes = NA,
no = 0))
threshold <- -Inf
},
"binMat" = {
bin <- binMatrix
threshold <- -Inf
}
)
# rebuild the matrix
binMat <- matrix(bin,
nrow = dim(ionImages[,,i])[1],
ncol = dim(ionImages[,,i])[2])
uni <- uni + binMat
uni[uni > 0] <- 1
}
return(uni)
}
image(binarizeAndCombine(NLGF67w_mouse1_rep1))
library(PlaquePicker)
image(binarizeAndCombine(NLGF67w_mouse1_rep1))
devtools::document()
library(PlaquePicker)
library(PlaquePicker)
image(binarizeAndCombine(NLGF67w_mouse1_rep1))
library(PlaquePicker)
image(binarizeAndCombine(NLGF67w_mouse1_rep1))
devtools::document()
rm(binarizeAndCombine())
rm(binarizeAndCombine
)
image(binarizeAndCombine(NLGF67w_mouse1_rep1))
image(binarizeAndCombine(NLGF67w_mouse1_rep1), method ="geometric")
image(binarizeAndCombine(NLGF67w_mouse1_rep1, method ="geometric"))
image(binarizeAndCombine(NLGF67w_mouse1_rep1, method ="tpoint"))
a <- binarizeAndCombine(NLGF67w_mouse1_rep1, method ="tpoint")
image(binarizeAndCombine(NLGF67w_mouse1_rep1, binMatrix = a))