-
Notifications
You must be signed in to change notification settings - Fork 1
/
proteomics_functions.R
358 lines (319 loc) · 13.5 KB
/
proteomics_functions.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
###########################################################################
#
# proteomics_functions
#
###########################################################################
# Author: Matthew Muller
# Date: 2023-12-28
# Script Name: proteomics_functions
#======================== LIBRARIES ========================#
suppressMessages(library(tidyverse))
suppressMessages(library(glue))
suppressMessages(library(OlinkAnalyze))
suppressMessages(library(clusterProfiler))
suppressMessages(library(AnnotationDbi))
# LOAD FUNCTIONS
# space reserved for sourcing in functions
source('https://raw.githubusercontent.com/mattmuller0/Rtools/main/general_functions.R')
message("
| \ .-' | \ .- |__
| -,-.-. '-. _ \ .\ . . | || | `. |
| | | | || `\ | \ | | `._/`-'_.' |
' '-' `._/ . | | `-'
| `-' _ .
_ _ . \ |__ | _ .-. _ _ .' | |
/ | `||,-. | |--. /-' | __| `\ /' || | |
._ `-'`| | | | |\ , | |`._/ \ .' \ | '
` '- |`-'__ `-' o
,-'`` .'
/`. / .'` .-; .-.
/ \ | \ _,' /-,_ \ `.
/ _ .-' _.---:. `' _.-'_.' )
'` \\ ,;' . \ ` ,'
') `7_, `'-; .-'-.._
// ,;-'` .-''. ,-. _.f`
.'/ .-'`\ _ .-'`\ \ _ `'- >
/ / / /` \ ,' ; / `'. ,-''-.
| | | \ \ / |; \ ,' `.
\ '.; `. \ -. || _/ |
_mx__`.___\\ / L\ \ \ _O_;|O__ .'` /________
`. | `'-.( | _..--''/ _.-`
`-.`.__.p \ -' \ / , ;` /.
'.----'L__p\ '. v| | \ `'-._ / \
`. '.__b\ \ _~ v-'`v-' ,\' `.' |
`. `<~| .-' ,' _ _.'` _.' /
`. `' '_,-'' ,.-''`- _.' .'
`. \ _,,=' ,-' _.-'
`. _ __ _.-' _.-'
`. `. ` ` _.-'
`. \ _.-'
`. |_.-'
`-._.'
")
#======================== CODE ========================#
# Function to get olink sample info
olink_info <- function(data) {
# get the sample IDs
sampleIDs <- unique(data$SampleID)
# get the plates
plates <- unique(data$PlateID)
# get the proteins
proteins <- unique(data$Assay)
# get the protein groups
panels <- unique(data$Panel)
# write out the sample IDs, plates, proteins, and protein groups to a file
out <- list(
sampleIDs = sampleIDs,
plates = plates,
proteins = proteins,
panels = panels
)
return(out)
}
# Function to get olink PCA outliers and plot
# Arguments:
# data: data frame of olink data
# outdir: output directory
# outlierDefX: outlier definition for x axis
# outlierDefY: outlier definition for y axis
# byPanel: boolean to plot by panel
# ...: additional arguments to pass to olink_pca_plot
# Outputs:
# plot of pca outliers
olink_pca_outliers <- function(data, outdir, outlierDefX = 2.5, outlierDefY = 4, byPanel = TRUE, ...) {
dir.create(outdir, showWarnings = F)
# get the pca
pca <- olink_pca_plot(data, outlierDefX = outlierDefX, outlierDefY = outlierDefY, byPanel = byPanel, ...)
lapply(names(pca), function(x) {ggsave(glue('{outdir}/{x}_pca.pdf'), pca[[x]])})
# outliers
outliers <- lapply(pca, function(x) {x$data}) %>%
bind_rows() %>%
filter(Outlier == 1) %>%
dplyr::select(SampleID, Outlier, Panel)
write.csv(outliers, glue('{outdir}/pca_outliers.csv'), row.names = F)
out <- list(pca = pca, outliers = outliers)
return(out)
}
# Function to get olink UMAP outliers and plot
# Arguments:
# data: data frame of olink data
# outdir: output directory
# outlierDefX: outlier definition for x axis
# outlierDefY: outlier definition for y axis
# byPanel: boolean to plot by panel
# ...: additional arguments to pass to olink_pca_plot
# Outputs:
# plot of pca outliers
olink_umap_outliers <- function(data, outdir, outlierDefX = 2.5, outlierDefY = 4, byPanel = TRUE, ...) {
dir.create(outdir, showWarnings = F)
# get the pca
umap <- olink_umap_plot(data, outlierDefX = outlierDefX, outlierDefY = outlierDefY, byPanel = byPanel, ...)
lapply(names(umap), function(x) {ggsave(glue('{outdir}/{x}_umap.pdf'), umap[[x]])})
# outliers
outliers <- lapply(umap, function(x) {x$data}) %>%
bind_rows() %>%
filter(Outlier == 1) %>%
dplyr::select(SampleID, Outlier, Panel)
write.csv(outliers, glue('{outdir}/umap_outliers.csv'), row.names = F)
out <- list(umap = umap, outliers = outliers)
return(out)
}
# Function test level of detection (LOD) for each protein
olink_lod_qc <- function(data, outdir, plot = TRUE) {
dir.create(outdir, showWarnings = F)
# get the lod
lod <- data %>% dplyr::mutate(LOD_QC = NPX > LOD)
write.csv(lod, glue('{outdir}/lod_qc.csv'), row.names = F)
# plot the NPX facet wrappped by Assay and colored by LOD_QC
if (plot) {
p <- ggplot(lod, aes(x = NPX, fill = LOD_QC)) +
geom_histogram(bins = 100) +
facet_wrap(~Assay, scales = 'free') +
theme_bw() +
theme(legend.position = 'bottom')
ggsave(glue('{outdir}/npx_hist.pdf'), p, width = 45, height = 25)
}
# proteins that fail the LOD in more than 75% of samples
# outliers <- lod %>%
# dplyr::group_by(Assay) %>%
# dplyr::summarise(n_pass = sum(LOD_QC) / n()) %>%
# dplyr::filter(n_pass < 0.75)
outliers <- lod %>% dplyr::filter(MissingFreq > 0.25)
# return the proteins that fail the LOD
out <- list(lod = lod, outliers = outliers)
return(out)
}
# Function to convert olink data to count table
# Arguments:
# data: data frame of olink data
# sampleID: sample ID column
# assay: assay column
# value: value column
# Outputs:
# count table
olink_count_table <- function(data, sampleID = 'SampleID', assay = 'Assay', value = 'NPX') {
# get the count table
count_table <- data %>%
# get the sample ID, assay, and value
dplyr::select(sampleID, assay, value) %>%
# pivot to wide
tidyr::pivot_wider(names_from = assay, values_from = value) %>%
# remove the sample ID
column_to_rownames(sampleID)
return(count_table)
}
# Function to do olink filtering
# Arguments:
# data: data frame of olink data
olink_filtering <- function(data, outdir, pca_args = list(), umap_args = list(), lod_args = list()) {
dir.create(outdir, showWarnings = F)
# get the sample info
message('Getting sample info')
sample_info <- do.call(olink_info, list(data))
writeLines(glue("{names(sample_info)}: {sample_info}\n"), file.path(outdir, "sampleIDs.txt"))
# get the pca outliers
message('Running PCA')
pca_outliers <- do.call(olink_pca_outliers, c(list(data = data, outdir = file.path(outdir, 'pca')), pca_args))
saveRDS(pca_outliers, glue('{outdir}/pca/pca_outliers.rds'))
# get the umap outliers
message('Running UMAP')
umap_outliers <- do.call(olink_umap_outliers, c(list(data = data, outdir = file.path(outdir, 'umap')), umap_args))
saveRDS(umap_outliers, glue('{outdir}/umap/umap_outliers.rds'))
# get the lod qc
message('Running LOD')
lod_qc <- do.call(olink_lod_qc, c(list(data = data, outdir = file.path(outdir, 'lod')), lod_args))
saveRDS(lod_qc, glue('{outdir}/lod/lod_qc.rds'))
# get the outliers
message('Getting outliers')
qc_outliers <- data %>% filter(QC_Warning == "WARN") %>% pull(SampleID) %>% unique()
pca_outliers <- pca_outliers$outliers %>% pull(SampleID)
umap_outliers <- umap_outliers$outliers %>% filter(Outlier == 1) %>% pull(SampleID)
outliers <- unique(c(qc_outliers, pca_outliers, umap_outliers))
write.csv(outliers, glue('{outdir}/sample_outliers.csv'), row.names = F)
# proteins that are below the LOD
message('Getting proteins below the LOD')
lod_outliers <- lod_qc$outliers %>% pull(Assay) %>% unique()
write.csv(lod_outliers, glue('{outdir}/protein_outliers.csv'), row.names = F)
# remove the outliers
data %<>% filter(!grepl(paste(outliers, collapse = "|"), SampleID))
# remove the proteins that are below the LOD
data %<>% filter(!grepl(paste(lod_outliers, collapse = "|"), Assay))
write.csv(data, glue('{outdir}/npx_data.csv'), row.names = F)
# get the count table
count_table <- olink_count_table(data)
write.csv(count_table, glue('{outdir}/count_table.csv'))
# return the data
message('Done filtering')
out <- list(data = data, count_table = count_table, outliers = outliers, lod_outliers = lod_outliers)
return(out)
}
# Function to run differential expression and pathway analysis
# Arguments:
# data: data frame of olink data
# condition: condition column
# outdir: output directory
# de_fxn: differential expression function
# gsea_fxn: pathway analysis function
# de_args: arguments to pass to differential expression function
# gsea_args: arguments to pass to pathway analysis function
# Outputs:
# list of differential expression and pathway analysis results
olink_analysis <- function(
data,
conditions,
outdir,
de_fxn = olink_wilcox,
gsea_fxn = olink_pathway_enrichment,
de_args = list(),
volcano_args = list(),
gsea_args = list()
) {
dir.create(outdir, showWarnings = F)
# make sure the conditions are in the data
if (!all(conditions %in% colnames(data))) {
stop('Conditions not in data')
}
de_res <- list()
gsea_res <- list()
res <- list()
message("Looping over: ", paste0(conditions, collapse = ", "))
for (condition in conditions) {
dir.create(glue('{outdir}/{condition}'), showWarnings = F)
# get the differential expression
message('Running differential expression')
de <- do.call(de_fxn, c(list(data, condition), de_args))
p <- do.call(olink_volcano_plot, list(de))
ggsave(glue('{outdir}/{condition}/volcano.pdf'), p, width = 6, height = 6)
write.csv(de, glue('{outdir}/{condition}/de_results.csv'), row.names = F)
de_res[[condition]] <- de
r <- summarize_experiment(de, logFC_column = "estimate", pvalue_column = "p.value", padj_column = "Adjusted_pval")
r$condition <- condition
res[[condition]] <- r
# get the pathway analysis
message('Running pathway analysis')
gsea <- do.call(olink_pathway_enrichment, c(list(data, de), gsea_args))
write.csv(gsea, glue('{outdir}/{condition}/gsea_results.csv'), row.names = F)
p <- do.call(olink_pathway_visualization, list(gsea))
ggsave(glue('{outdir}/{condition}/gsea.pdf'), p, width = 12, height = 8)
p <- do.call(olink_pathway_heatmap, list(gsea, de))
ggsave(glue('{outdir}/{condition}/gsea_heatmap.pdf'), p, width = 12, height = 8)
gsea_res[[condition]] <- gsea
}
res <- do.call(rbind, res)
write.csv(res, glue('{outdir}/experiment_summary.csv'), row.names = F)
# return the results
message('Done with analysis')
out <- list(de = de_res, gsea = gsea_res)
return(out)
}
# Function to calculate odds ratios for olink data
# Arguments:
# data: data frame of olink data
# proteins: vector of proteins to calculate odds ratios for
# events: vector of events to calculate odds ratios for
# adjustments: vector of variables to adjust for
# Outputs:
# list of odds ratios for each event
olink_odds_ratios <- function(data, proteins, events, adjustments = NULL) {
# map over the events and proteins to get the odds ratios
or.df <- purrr::map(
.x = events,
.f = function(event) {
out <- purrr::map(
.x = proteins,
.f = function(protein) {
# get the counts for the event and protein
dat <- data %>%
dplyr::select(protein, event, any_of(adjustments)) %>%
drop_na()
# make a formula to adjust for
if (is.null(adjustments)) {
form <- paste0(event, ' ~ ', protein)
} else {
form <- paste0(event, ' ~ ', protein, ' + ', paste(adjustments, collapse = ' + '))
}
# get the odds ratio
odds_ratio <- glm(
formula = form,
data = dat,
family = binomial(link = 'logit')
) %>%
broom::tidy() %>%
filter(term == protein) %>%
mutate(
odds.ratio = exp(estimate),
or.ci.lower = exp(estimate - 1.96 * std.error),
or.ci.upper = exp(estimate + 1.96 * std.error),
formula = form
) %>%
dplyr::select(term, odds.ratio, or.ci.lower, or.ci.upper, estimate, std.error, p.value, formula)
}
)
out <- do.call(rbind, out)
out$p.adj <- p.adjust(out$p.value, method = 'BH')
out
}
)
names(or.df) <- events
return(or.df)
}