-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.R
302 lines (254 loc) · 10.3 KB
/
helpers.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
# Helper functions for tumor-microbiome project
# Load required libraries
library(ggsci)
library(RColorBrewer)
library(patchwork)
library(ggpubr)
library(ggtree)
library(ggthemes)
library(picante)
library(entropart)
library(vegan)
library(scater)
library(NetCoMi)
library(mia)
library(breakaway)
library(bluster)
library(microViz)
library(clusterProfiler)
library(variancePartition)
library(miaViz)
library(survminer)
library(survival)
library(phyloseq)
library(microbiome)
require(tidyverse)
# Various functions for processing
# Import pathseq counts files
import_pathseq = function (filepath, metadata.in, level = "genus", count_feature = "unambiguous", minimum = 1) {
message("Importing PathSeq files...")
pathseq.import = filepath %>%
map_dfr(.id = "sampleId", function(x) data.table::fread(x, colClasses = c("taxonomy" = "character",
"type" = "character",
"name" = "character",
"kingdom" = "character"))) %>%
filter(sampleId %in% row.names(metadata.in)) %>%
filter(type == level)
# Convert to phyloseq object
message("Get NCBI taxonomic annotations...")
ncbi.ids = unique(pathseq.import$tax_id)
names(ncbi.ids) = ncbi.ids
taxize.ids = suppressMessages(taxize::classification(ncbi.ids, db = 'ncbi'))
taxize.ids.long = map_dfr(taxize.ids, .f = bind_rows, .id = "old_id")
ncbi.ids.updated = taxize.ids.long %>%
filter(rank == level) %>%
rename(new_id = id) %>%
select(old_id, new_id) %>%
mutate(old_id = as.character(old_id))
message("Replacing outdated annotations...")
pathseq.import.fixed = pathseq.import %>%
mutate(tax_id = as.character(tax_id)) %>%
left_join(ncbi.ids.updated, by = c("tax_id" = "old_id"))
# Convert to phyloseq object
message("Converting counts to phyloseq...")
otu.table = pathseq.import.fixed %>%
filter(unambiguous >= minimum) %>%
dplyr::select(new_id, unambiguous, sampleId) %>%
filter(!is.na(new_id)) %>%
group_by(sampleId, new_id) %>%
summarise(sum = sum(unambiguous)) %>%
pivot_wider(names_from = sampleId, values_from = sum, values_fill = 0) %>%
column_to_rownames("new_id")
# Get taxonomy table
message("Converting taxonomy to phyloseq...")
tax.table = taxize.ids.long %>%
select(-id) %>%
filter(rank %in% c("superkingdom", "phylum", "class", "order", "family", "genus")) %>%
pivot_wider(id_cols = old_id, names_from = rank, values_from = name) %>%
left_join(ncbi.ids.updated) %>%
select(-old_id) %>%
distinct(superkingdom, phylum, class, order, family, genus, new_id, .keep_all = T) %>%
as.data.frame() %>%
filter(!is.na(new_id)) %>%
column_to_rownames("new_id") %>%
rename(Kingdom = superkingdom,
Phylum = phylum,
Class = class,
Order = order,
Family = family,
Genus = genus) %>%
as.matrix()
message(paste0("Detected ", nrow(otu.table), " taxa"))
message(paste0("Detected ", ncol(otu.table), " samples"))
SAMPLE = metadata.in %>% sample_data()
OTU = otu_table(otu.table, taxa_are_rows = TRUE)
TAX = tax_table(tax.table)
pseq = phyloseq(OTU, TAX, SAMPLE)
pseq.fil = prune_taxa(taxa_sums(pseq) > 0, pseq)
return(pseq.fil)
}
# Function to import Bracken counts into phyloseq object
import_bracken = function (fileinput, metadata.in) {
message("Importing Kraken2/Bracken files...")
kraken2.import = fileinput %>%
map_dfr(.id = "sampleId", data.table::fread) %>%
as_tibble() %>%
filter(sampleId %in% row.names(metadata.in))
# Convert to phyloseq object
message("Get NCBI taxonomic annotations...")
ncbi.ids = unique(kraken2.import$taxonomy_id)
names(ncbi.ids) = ncbi.ids
taxize.ids = suppressMessages(taxize::classification(ncbi.ids, db = 'ncbi'))
taxize.ids.long = map_dfr(taxize.ids, .f = bind_rows, .id = "old_id")
ncbi.ids.updated = taxize.ids.long %>%
filter(rank == "genus") %>%
rename(new_id = id) %>%
select(old_id, new_id) %>%
mutate(old_id = as.character(old_id))
message("Replacing outdated annotations...")
kraken2.import.fixed = kraken2.import %>%
mutate(taxonomy_id = as.character(taxonomy_id)) %>%
left_join(ncbi.ids.updated, by = c("taxonomy_id" = "old_id"))
# Convert to phyloseq object
message("Converting counts to phyloseq...")
otu.table = kraken2.import.fixed %>%
dplyr::select(new_id, new_est_reads, sampleId) %>%
filter(!is.na(new_id)) %>%
group_by(sampleId, new_id) %>%
summarise(sum = sum(new_est_reads)) %>%
pivot_wider(names_from = sampleId, values_from = sum, values_fill = 0) %>%
column_to_rownames("new_id")
# Get taxonomy table
message("Converting taxonomy to phyloseq...")
tax.table = taxize.ids.long %>%
select(-id) %>%
filter(rank %in% c("superkingdom", "phylum", "class", "order", "family", "genus")) %>%
pivot_wider(id_cols = old_id, names_from = rank, values_from = name) %>%
left_join(ncbi.ids.updated) %>%
select(-old_id) %>%
distinct(superkingdom, phylum, class, order, family, genus, new_id, .keep_all = T) %>%
as.data.frame() %>%
filter(!is.na(new_id)) %>%
column_to_rownames("new_id") %>%
rename(Kingdom = superkingdom,
Phylum = phylum,
Class = class,
Order = order,
Family = family,
Genus = genus) %>%
as.matrix()
message(paste0("Detected ", nrow(otu.table), " taxa"))
message(paste0("Detected ", ncol(otu.table), " samples"))
SAMPLE = metadata.in %>% sample_data()
OTU = otu_table(otu.table, taxa_are_rows = TRUE)
TAX = tax_table(tax.table)
pseq = phyloseq(OTU, TAX, SAMPLE)
pseq.fil = prune_taxa(taxa_sums(pseq) > 0, pseq)
return(pseq.fil)
}
get_intersection = function(kraken2, pathseq, pcutoff = 1, kcutoff = 10){
# For each sample, filter kraken2 based on the microbes found in pathseq
kraken2.abun = abundances(kraken2) %>% as.data.frame() %>% rownames_to_column("Id")
pathseq.abun = abundances(pathseq) %>% as.data.frame() %>% rownames_to_column("Id")
# Iterate over samples in Kraken2 table
pb <- progress_estimated(length(sample_names(kraken2)))
filtered = sample_names(kraken2) %>%
map(.f = function(x){
pb$tick()$print()
to.fil = select(pathseq.abun, "Id" , x) %>%
filter(.data[[x]] >= pcutoff) %>%
pull(Id)
select(kraken2.abun, "Id", x) %>%
filter(Id %in% to.fil) %>%
filter(.data[[x]] >= kcutoff)
})
# Merge all tables together
joined = filtered %>%
purrr::reduce(full_join, by = "Id") %>%
replace(is.na(.), 0)
# Convert to phyloseq object
mat = joined %>%
column_to_rownames("Id") %>%
as.matrix()
# Add back to phylsoeq object
results = kraken2
otu_table(results) = otu_table(mat, taxa_are_rows = T)
# Prune samples with no reads
results = prune_samples(sample_sums(results)>0, results)
results = prune_taxa(taxa_sums(results)>0, results)
return(results)
}
# Run maaslin2
run_maaslin = function(pseq.in,
transform = "LOG",
analysis_method = "LM",
normalization = "TSS",
fixed_effects = NULL,
random_effects = NULL,
max_significance = 0.25,
min_prevalence = 0.10,
min_variance = 0.0,
min_abundance = 0.0,
reference = c("primaryTumorLocation,CUP", "biopsySite,CNS")) {
# get temporary directory
tmp.dir = tempdir()
# Format counts table
pseq.in %>%
aggregate_taxa(level = "Genus") %>%
abundances() %>%
t() %>%
as.data.frame() %>%
rownames_to_column("ID") %>%
write_tsv(paste0(tmp.dir, "/maaslin2.counts.tsv"))
# Format metadata
meta(pseq.in) %>%
rownames_to_column("ID") %>%
write_tsv(paste0(tmp.dir, "/maaslin2.metadata.tsv"))
# Run command
library(Maaslin2)
fit_data <- Maaslin2( input_data = paste0(tmp.dir, "/maaslin2.counts.tsv"),
input_metadata = paste0(tmp.dir, "/maaslin2.metadata.tsv"),
output = paste0(tmp.dir, "/outdir"),
transform = transform,
normalization = normalization,
fixed_effects = fixed_effects,
random_effects = random_effects,
analysis_method = analysis_method,
standardize = T,
cores = 2,
min_abundance = min_abundance,
min_prevalence = min_prevalence,
max_significance = max_significance,
plot_heatmap = F,
plot_scatter = F,
reference = reference)
# Import results
results = read_tsv( paste0(tmp.dir, "/outdir/all_results.tsv"))
sig_results = read_tsv( paste0(tmp.dir, "/outdir/significant_results.tsv"))
# Return
return(list(results = results, sig_results = sig_results))
}
# Regress out covariates
get_residuals = function(input.table, formula = c("primaryTumorLocation", "biopsySite")) {
input.table.long = input.table %>%
pivot_longer(cols = -hmfSampleId, names_to = "signatures", values_to = "y") %>%
inner_join(meta(Hartwig)) %>%
left_join(select(immune.signatures, hmfSampleId, cd45)) %>%
#left_join(select(purple.data, hmfSampleId, tml, tmbPerMb, tmbStatus, tmlStatus)) %>%
as_tibble()
unique(input.table.long$signatures) %>%
map_dfr(.f = function(x) {
input.table.long %>%
filter(signatures == x) %>%
tibble::column_to_rownames("hmfSampleId") %>%
glm(as.formula(paste("y", paste(formula, collapse=" + "), sep=" ~ ")), data = ., family = "gaussian") %>%
broom::augment() %>%
rename(hmfSampleId = `.rownames`,
residual = `.resid`) %>%
mutate(diff = residual - mean(residual)) %>%
select(hmfSampleId, diff) %>%
mutate(signatures = x)
}) %>%
pivot_wider(id_cols = hmfSampleId, names_from = signatures, values_from = diff) %>%
as.data.frame()
}