-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.R
167 lines (149 loc) · 4.36 KB
/
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
# load required libraries
library(imager)
library(colocr)
library(tidyverse)
library(xtable)
library(cowplot)
# get image path
fl <- system.file('extdata', 'Image0003_.jpg', package = 'colocr')
# load images and channels
img <- image_load(fl)
img1 <- channel(img, 1)
img2 <- channel(img, 2)
# generate figure of images and channels
par(mfrow = c(1,3), mar = c(0, 0, 1, 0))
plot(img, axes = FALSE, main = 'Merge')
plot(img1, axes = FALSE, main = 'Channel One')
plot(img2, axes = FALSE, main = 'Channel Two')
# generate manuscript figure1
figure1 <- list(
p_a = function() plot(img, axes = FALSE),
p_b = function() plot(img1, axes = FALSE),
p_c = function() plot(img2, axes = FALSE)
)
plot_grid(plotlist = map(figure1, ggdraw),
nrow = 1,
scale = 1.1,
labels = 'AUTO',
label_size = 10,
label_fontface = 'plain') %>%
ggsave(filename = 'figure1.png',
width = 18, height = 6, units = 'cm')
# select regions of interest
par(mfrow = c(2,2), mar = c(0, 0, 1, 0))
img %>%
roi_select(threshold = 90,
shrink = 10,
fill = 5,
clean = 10,
n = 3) %>%
roi_show()
# generate manuscript figure2
img_roi <- img %>%
roi_select(threshold = 90,
shrink = 10,
fill = 5,
clean = 10,
n = 3)
labels <- attr(img_roi, 'label')
dims <- dim(grayscale(img_roi))
a <- array(labels, dim = dims)
px <- cimg(a)
img1 <- channel(img_roi, 1)
img2 <- channel(img_roi, 2)
figure2 <- list(
p1 = function() plot(img, axes = FALSE),
p2 = function() plot(px, axes = FALSE),
p3 = function() {plot(img1, axes = FALSE); highlight(px)},
p4 = function() {plot(img2, axes = FALSE); highlight(px)}
)
plot_grid(plotlist = map(figure2, ggdraw),
nrow = 2,
scale = 1.1,
labels = 'AUTO',
label_size = 10,
label_fontface = 'plain') %>%
ggsave(filename = 'figure2.png',
width = 18, height = 18, units = 'cm')
# check pixel intensities
par(mfrow = c(1,2), mar = c(4, 4, 1, 1))
img %>%
roi_select(threshold = 90,
shrink = 10,
fill = 5,
clean = 10,
n = 3) %>%
roi_check()
# generate manuscript figure3
par(mar = c(10,10,10,10))
pix_int <- .intensity_get(img_roi)
d1 <- density(pix_int[[1]])
d2 <- density(pix_int[[2]])
xlim <- c(min(c(d1$x, d2$x)), max(c(d1$x, d2$x)))
ylim <- c(min(c(d1$y, d2$y)), max(c(d1$y, d2$y)))
figure3 <- list(
p1 = function() {
par(mar=c(9,9, 1, 1))
plot(pix_int[[1]], pix_int[[2]],
col = alpha(pix_int[[3]], 0.3),
pch = 16,
xlab = 'Channel One', ylab = 'Channel Two',
cex = .5,
cex.lab = 1.3)
},
p2 = function() {
par(mar=c(9,9, 1, 1))
plot(d1$x, d1$y,
xlim = xlim, ylim = ylim,
type = 'l', col = alpha('darkgreen', .5),
xlab = 'Pixel Value', ylab = 'Density',
cex.lab = 1.3,lwd = 1.5)
lines(d2$x, d2$y,
col = alpha('magenta', .5),
lwd = 1.5)
}
)
plot_grid(plotlist = figure3,
scale = .9,
nrow = 1,
labels = 'AUTO',
label_size = 10,
label_fontface = 'plain') %>%
ggsave(filename = 'figure3.png',
width = 20, height = 10, units = 'cm')
# calculate co-localization stats
img %>%
roi_select(threshold = 90,
shrink = 10,
fill = 5,
clean = 10,
n = 3) %>%
roi_test(type = 'both')
# generate table for co-colocalization stats
img %>%
roi_select(threshold = 90,
shrink = 10,
fill = 5,
clean = 10,
n = 3) %>%
roi_test(type = 'both') %>%
mutate(roi = row_number()) %>%
rbind(c(mean(.$pcc), mean(.$moc), 'Average')) %>%
select(roi, everything()) %>%
mutate_at(vars(pcc, moc), function(x) round(as.numeric(x),2)) %>%
setNames(c('ROI', 'PCC', 'MOC')) %>%
xtable(caption = '\\textbf{Co-localization statistics.}',
align = 'cccc',
label = 'tab:table2') %>%
print(include.rownames = FALSE,
booktabs = TRUE,
add.to.row = list(pos = list(3),
command = '\\midrule '),
caption.placement = 'top',
table.placement = 'H',
sanitize.text.function = identity,
comment = FALSE,
file = 'table2.tex')
# copy source code to manuscript dir
file.copy('script.R',
to = './script.R')