/
plotting.R
277 lines (244 loc) · 9.27 KB
/
plotting.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
#' Plot ion chromatogram
#' @rdname plotChromatogram
#' @description Plot ion chromatogram from MetaboProfile.
#' @param processed_data S4 object of class MetaboProfile
#' @param cls sample information column to use for plot colours.
#' @param group average samples within groups. If cls is NULL, plot average chromatogram across all samples.
#' @param alpha plot line transparancy alpha
#' @param aggregationFun value to pass to \code{xcms::chromatogram()} for chromatogram type. Defaults to base peak ("max").
#' @param ... arguments to pass to \code{erah::plotChr()}
#' @importFrom ggthemes scale_colour_ptol
#' @importFrom xcms chromatogram
#' @importFrom magrittr set_names
#' @importFrom ggplot2 ggplot geom_line theme_bw aes labs theme element_text
#' @importFrom dplyr bind_rows group_by summarise all_of
#' @importFrom erah plotChr
#' @importFrom MSnbase as.MSnExp.OnDiskMSnExp
setMethod('plotChromatogram',signature = 'MetaboProfile',
function(processed_data,
cls = NULL,
group = FALSE,
alpha = 1,
aggregationFun = 'max',
...){
info <- processed_data %>%
sampleInfo()
if (str_detect(technique(processed_data),'eRah')) {
processed_data %>%
extractProcObject() %>%
plotChr(...)
} else {
x <- processed_data %>%
extractProcObject()
if (!is.list(x)) {
x <- list(x)
}
chrom <- x %>%
map(~{
.x %>%
as.MSnExp.OnDiskMSnExp() %>%
chromatogram(aggregationFun = aggregationFun) %>%
.@.Data %>%
map(~{
tibble(rtime = .@rtime,
intensity = .@intensity)
}) %>%
set_names(info$name) %>%
bind_rows(.id = 'Sample') %>%
mutate(rtime = rtime/60)
}) %>%
set_names(names(x)) %>%
bind_rows(.id = 'mode')
if (group == TRUE) {
chrom <- chrom %>%
mutate(rtime = round(rtime,1))
}
if (!is.null(cls)) {
chrom <- chrom %>%
left_join(info %>%
dplyr::mutate(name = as.character(name)) %>%
select(name,Class = all_of(cls)),
by = c('Sample' = 'name'))
if (group == TRUE) {
chrom <- chrom %>%
group_by(mode,Class,rtime) %>%
summarise(intensity = mean(intensity))
}
} else {
if (group == TRUE) {
chrom <- chrom %>%
group_by(mode,rtime) %>%
summarise(intensity = mean(intensity)) %>%
mutate(Sample = 1)
}
}
pls <- chromPlot(chrom,
cls = cls,
group = group,
alpha = alpha,
mode = mode)
return(pls)
}
}
)
#' @importFrom ggplot2 element_blank element_line scale_x_continuous scale_y_continuous
#' @importFrom stringr str_replace_all
chromPlot <- function(chrom,
cls,
group,
alpha,
mode = NA){
chrom <- chrom %>%
mutate(mode = str_replace_all(mode,
'n',
'Negative Mode') %>%
str_replace_all('p',
'Positive Mode') %>%
str_replace_all('1',
''))
title <- ifelse(length(unique(chrom$mode)) > 1,
'Ion Chromatograms',
'Ion Chromatogram')
if (!is.null(cls) & group == TRUE) {
pl <- ggplot(chrom,aes(x = rtime,
y = intensity,
group = Class))
} else {
pl <- ggplot(chrom,aes(x = rtime,
y = intensity,
group = Sample))
}
pl <- pl +
theme_bw() +
labs(title = title,
x = 'Retention Time (minutes)',
y = 'Intensity') +
theme(plot.title = element_text(face = 'bold',
hjust = 0.5),
panel.border = element_blank(),
panel.grid = element_blank(),
axis.line = element_line(),
axis.title = element_text(face = 'bold'),
legend.title = element_text(face = 'bold'),
legend.position = 'bottom',
strip.background = element_blank(),
strip.text = element_text(face = 'bold',
size = 10)) +
scale_x_continuous(expand = c(0,0)) +
scale_y_continuous(expand = c(0,0)) +
facet_wrap(~mode,
ncol = 1,
scales = 'free')
if (!is.null(cls)) {
pl <- pl +
geom_line(aes(colour = Class),alpha = alpha) +
guides(colour = guide_legend(title = cls))
if (length(unique(chrom$Class)) < 12) {
pl <- pl +
scale_colour_ptol()
}
} else {
pl <- pl +
geom_line(alpha = alpha)
}
return(pl)
}
TICplot <- function(d,TICmedian,colour,by){
pl <- ggplot(d,aes(x = Index,
y = TIC,
fill = Colour)) +
geom_hline(data = TICmedian,
aes(yintercept = Median)) +
geom_hline(data = TICmedian,
aes(yintercept = Q1),
linetype = 2) +
geom_hline(data = TICmedian,
aes(yintercept = Q3),
linetype = 2) +
geom_hline(data = TICmedian,
aes(yintercept = LowerOut),
linetype = 3) +
geom_hline(data = TICmedian,
aes(yintercept = UpperOut),
linetype = 3) +
geom_point(shape = 21) +
theme_bw() +
theme(plot.title = element_text(face = 'bold',
hjust = 0.5),
panel.border = element_blank(),
panel.grid = element_blank(),
axis.line = element_line(),
axis.title = element_text(face = 'bold'),
legend.title = element_text(face = 'bold'),
strip.background = element_blank(),
strip.text = element_text(face = 'bold',
size = 10)) +
labs(title = 'Sample TICs',
caption = 'The solid line shows the median TIC across the sample set.
The dashed line shows the inter-quartile range (IQR) and
the dotted line shows the outlier boundary (1.5 X IQR).',
y = 'Total Ion Count',
x = by,
fill = colour) +
facet_wrap(~Mode)
if (length(unique(d$Colour)) <= 12) {
pl <- pl +
scale_fill_ptol()
}
return(pl)
}
#' Plot total ion counts
#' @rdname plotTIC
#' @description Plot sample total ion counts.
#' @param processed S4 object of class MetaboProfile
#' @param by info column to plot against
#' @param colour info column to provide colour labels
#' @importFrom ggplot2 geom_hline geom_point facet_wrap guides guide_legend
#' @importFrom stats median IQR
#' @importFrom ggthemes scale_fill_ptol
#' @importFrom stringr str_detect
#' @importFrom dplyr across
setMethod('plotTIC',signature = 'MetaboProfile',
function(processed,by = 'injOrder', colour = 'block'){
info <- processed %>%
sampleInfo()
d <- processed %>%
processedData()
if (class(d)[1] != 'list'){
d <- list(d)
}
d <- d %>%
map(~{
rowSums(.) %>%
tibble(TIC = .) %>%
mutate(Colour = info[,colour] %>%
unlist() %>%
factor(),
Index = info[,by] %>%
unlist())
}) %>%
bind_rows(.id = 'Mode')
d <- d %>%
mutate(Mode = str_replace_all(Mode,
'n',
'Negative Mode') %>%
str_replace_all('p',
'Positive Mode') %>%
str_replace_all('1',
''))
TICmedian <- d %>%
group_by(Mode) %>%
summarise(Median = median(TIC),
Q1 = Median - IQR(TIC),
Q3 = Median + IQR(TIC),
LowerOut = Q1 - IQR(TIC) * 1.5,
UpperOut = Q3 + IQR(TIC) * 1.5)
TICmedian <- TICmedian %>%
mutate(across(where(is.numeric),~ replace(.x,.x < 0,0)))
pl <- TICplot(d,
TICmedian,
colour = colour,
by = by)
return(pl)
}
)