-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGTDB_phylo.R
392 lines (333 loc) · 11.8 KB
/
GTDB_phylo.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
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
# Scripts adapted from https://bitbucket.org/doxeylabcrew/annotree-manuscript-scripts/src
library(tidyverse)
library(data.table)
library(maditr)
library(ape)
library(phangorn)
library(pbmcapply)
library(unixtools)
# Script setup used to run the analyses in the de.NBI cloud
#setwd("/vol/cloud/gtdb/phylo_analyses/data")
#set.tempdir("/vol/cloud/gtdb/phylo_analyses/tmp")
# Set some variable -------------------------------------------------------
setDTthreads(28)
# Get dedup files
lo_env <- new.env()
# Gather data -------------------------------------------------------------
# Data stored in data/GTDB
# All GTDB ORFs, included those in MG clusters (3,270,101 clusters, and 75,297,319 ORFs)
lo_env$cl_data <-
fread(
"/vol/scratch/gtdb/mg_gtdb_kept_cluster_genome_orf_categ.tsv.gz",
header = FALSE,
col.names = c(
"genome",
"domain",
"orf",
"cl_name"
)
)
# GTDB contextual data (the example is for bacterial genomes, same data are available also for the archaea)
gtdb_cdata <-
fread("gtdb_data/arc_metadata_r86.tsv", #"gtdb_data/bac_metadata_r86.tsv"
header = TRUE,
sep = "\t")
# GTDB tree
tree <- read.tree("gtdb_data/gtdb_r86_ar.tree") #"gtdb_data/gtdb_r86_bac.tree"
node_depths <- node.depth.edgelength(tree)
# GTDB taxonomy
gtdb_tax <-
read.delim(
"gtdb_data/arc_taxonomy_r86.tsv", #"gtdb_data/bac_taxonomy_r86.tsv"
row.names = NULL,
header = FALSE,
col.names = c("genome", "taxonomy_string")
)
# Prepare taxonomy data
gtdb_tax <- gtdb_tax[match(tree$tip.label,
gtdb_tax$genome), ]
parsed_ranks <- data.frame(t(sapply(gtdb_tax$taxonomy_string,
function(s) {
ranks <- strsplit(as.character(s), split = ";")[[1]]
return(sapply(ranks, function(rank) {
strsplit(rank, split = "__")[[1]][2]
}))
})))
colnames(parsed_ranks) <-
c("domain",
"phylum",
"class",
"order",
"family",
"genus",
"species")
gtdb_tax <- cbind(gtdb_tax, parsed_ranks)
tax_levels <- colnames(gtdb_tax)[4:ncol(gtdb_tax)]
# order: "phylum" "class" "order" "family" "genus" "species"
get_level_index <- function(level) {
return(which(level == tax_levels))
}
# Get number of taxa below each node
leaf_counts <-
setNames(sapply(prop.part(tree), length), 1:tree$Nnode)
node_tax_levels <- read_tsv("gtdb_data/node_tax_levels_arc_r86.tsv", col_names = TRUE) #"gtdb_data/node_tax_levels_bac_r86.tsv"
tax_levels <-
c(
d = "Domain",
p = "Phylum",
c = "Class",
o = "Order",
f = "Family",
g = "Genus",
s = "Species"
)
########################
## Optional Prep
########################
# Get all tip ancestors to speed up F1 score computation
tip_ancestors <-
sapply(1:length(tree$tip.label), function(tip_index) {
return(phangorn::Ancestors(tree, tip_index, type = "all"))
})
# Prepare cluster data ----------------------------------------------------
# Get number of ORFs per genome
orf_genome <- lo_env$cl_data %>%
dt_select(genome) %>%
dt_mutate(n_orfs = .N, by = genome) %>%
unique()
orf_genome$n_orfs %>% summary()
lo_env$cl_data <- lo_env$cl_data %>%
dt_mutate(categ=tstrsplit(cl_name, "_", fixed=TRUE)[1]) %>%
dt_select(genome, cl_name, categ) %>%
dt_mutate(categ = ifelse((categ != "K" &
categ != "KWP" &
categ != "GU" & categ != "EU"),
"NO_HIT",
categ
)) %>%
dt_filter(genome %in% tree$tip.label)
# check we have all tips in our data set
all.equal((lo_env$cl_data$genome %>% unique() %>% sort),
(tree$tip.label %>% sort()))
cl_to_keep <- lo_env$cl_data %>%
dt_filter(categ != "NO_HIT") %>%
dt_summarise(nobs = .N, by = cl_name) %>%
dt_filter(nobs >= 1)
cl_data_kept <-
lo_env$cl_data %>%
dt_filter(cl_name %in% cl_to_keep$cl_name) %>%
dt_summarise(n = .N, by = list(genome, cl_name))
#################################################
## Prepare tree and taxonomy
#################################################
cl_data_kept <-
cl_data_kept %>% setnames(old = "genome",
new = "tip",
skip_absent = TRUE)
cls <- cl_data_kept$cl_name %>% unique()
# Let's create a table with the genomes and the clusters
# As the number of columns are very high we will do in
# multiple steps
# Split cluster in 10K lists
list_cls <- split(cls, (seq(length(cls)) - 1) %/% 1e2)
# Get lineage specificity score (F1-score) function:
# Source functions contained in the file phylo_functions.R
get_f1 <- function(x) {
require(maditr)
require(ape)
require(tidyverse)
library(pbmcapply)
library(castor)
library(furrr)
source("phylo_functions.R")
message("Creating incidence matrix...")
tip_labels <- tree$tip.label
X <- list_cls[[x]]
tips <-
expand.grid(tip_labels, X) %>%
as.data.table() %>%
setnames(old = c("Var1", "Var2"), new = c("tip", "cl_name"))
tips$tip <- as.character(tips$tip)
tips$cl_name <- as.character(tips$cl_name)
tmp <- cl_data_kept %>%
dt_filter(cl_name %in% X) %>%
dt_right_join(tips) %>%
dt_mutate(n = case_when(n >= 1 ~ 1,
is.na(n) ~ 0,
TRUE ~ 0)) %>%
data.table::dcast(tip ~ cl_name, fill = 0, value.var = "n") %>%
as.data.frame()
tmp <- tmp[match(tree$tip.label, tmp$tip),]
rownames(tmp) <- tmp$tip
tmp$tip <- NULL
cols <- (intersect(X, colnames(tmp)))
tmp <- subset(tmp, cols %in% colnames(tmp))
message("Calculating F1...")
f1scores.max.cl <-
pbmclapply(colnames(tmp), function(trait_profile) {
return(
f1score.trait(
trait_profile,
tree,
reduced = TRUE,
tip_ancestors = tip_ancestors,
states = tmp
)
)
},
mc.cores = 6,
mc.preschedule = TRUE)
names(f1scores.max.cl) <- colnames(tmp)
message("Gathering F1 results...")
# Combine scores
f1scores.max.cl <-
do.call(rbind,
pbmclapply(
f1scores.max.cl,
data.frame,
mc.cores = 6,
mc.preschedule = TRUE
))
message("Calculating TauD...")
Y <- tmp[,1]
tauD <-
pbmclapply(colnames(tmp), function(trait_profile) {
get_trait_depth(tip_states = tmp[, trait_profile], tree = tree, Npermutations = 1000)
},
mc.cores = 6,
mc.preschedule = TRUE)
names(tauD) <- colnames(tmp)
tauD <- map_dfr(tauD, `[`, c("mean_depth", "var_depth", "min_depth", "max_depth", "P", "mean_random_depth"), .id = "cl_name")
results <- f1scores.max.cl %>%
as_tibble(rownames = "cl_name") %>%
mutate(f1_score = round(f1_score, 6)) %>%
inner_join(tauD) %>%
rename(trait = cl_name)
message("done...")
return(results)
}
# Distribute jobs in the cluster nodes (de.NBI cloud) using batchtools
library(batchtools)
# Create the job registry
reg_dir_f1 <- file.path(getwd(), paste(format(Sys.Date()), format(Sys.time(), "%H%M%S"), sep = "-"))
reg_data_f1 <- makeRegistry(reg_dir_f1, seed=123, conf.file = ".batchtools.conf.R")
# Define number of jobs (here 4)
Njobs <- 1:length(list_cls)
ids <- batchMap(fun=get_f1, x=Njobs)
# two chunks of 5 jobs each
ids[, chunk := chunk(job.id, chunk.size = 100)]
batchExport(export = list(tree = tree,
cl_data_kept = cl_data_kept,
list_cls = list_cls,
tip_ancestors = tip_ancestors),
reg = reg_data_f1)
done <- submitJobs(ids,
reg=reg_data_f1,
resources=list(partition = "debug",
ncpus=5,
walltime = "100:00:00",
memory = "36G",
ntasks = 1,
chunks.as.arrayjobs = TRUE,
omp.threads = 6),
)
waitForJobs(reg = reg_data_f1) # Wait until jobs are completed
getStatus(reg = reg_data_f1) # Summarize job status
f1scores.max.cl <- map_dfr(Njobs, loadResult, reg = reg_data_f1) %>% as_tibble()
write_tsv(
f1scores.max.cl,
"new_results/max_f1_scores_per_node.gtdb_arc_r86.tsv"
)
saveRegistry(reg = reg_data_f1)
# Make a descriptive table with F1 scores and taxonomic info
get_lineages <- function(x) {
library(tidyverse)
source("phylo_functions.R")
map_dfr(list_nodes[[x]], function(X){
node_index <- X
node_lineage_vec <- node_tax_levels[match(node_index,
node_tax_levels$node_index),
2:ncol(node_tax_levels)]
node_lineage_vec <- as.vector(unlist(node_lineage_vec))
names(node_lineage_vec) <-
colnames(node_tax_levels)[2:ncol(node_tax_levels)]
lineage_string <- vector2lineage_string(node_lineage_vec)
return(tibble(node_index = node_index, lineage = lineage_string))
})
}
reg_dir_lin <- file.path(getwd(), paste(format(Sys.Date()), format(Sys.time(), "%H%M%S"), sep = "-"))
reg_data_lin <- makeRegistry(reg_dir_lin, seed=123, conf.file = ".batchtools.conf.R")
node_indices <- f1scores.max.cl$node_index %>% unique()
list_nodes <- split(node_indices, (seq(length(node_indices)) - 1) %/% 1e4)
# Define number of jobs (here 4)
Njobs <- 1:length(list_nodes)
ids <- batchMap(fun=get_lineages, x=Njobs, reg = reg_data_lin)
# two chunks of 5 jobs each
ids[, chunk := chunk(job.id, chunk.size = 50)]
batchExport(export = list(node_tax_levels = node_tax_levels,
list_nodes = list_nodes),
reg = reg_data_lin)
done <- submitJobs(ids,
reg=reg_data_lin,
resources=list(partition = "debug",
ncpus=1,
walltime = "100:00:00",
memory = "36G",
ntasks = 1,
chunks.as.arrayjobs = TRUE),
)
waitForJobs(reg = reg_data_lin) # Wait until jobs are completed
getStatus(reg = reg_data_lin) # Summarize job status
cl_lineages <- map_dfr(Njobs, loadResult, reg = reg_data_lin)
f1score.out_table.cl <- f1scores.max.cl %>%
inner_join(cl_lineages) %>%
arrange(desc(f1_score))
write_tsv(
f1score.out_table.cl,
"new_results/f1_scores.gtdb_arc_r86.tsv"
)
f1score.out_table.cl_filt <- f1score.out_table.cl %>%
as_tibble() %>%
filter(
(n_present_tips.phylo < (length(tree$tip.label) / 2)),
(n_present_tips.phylo > 1),
(f1_score > 0.95)
)
get_rank_level <- function(lineage) {
lineage_vector <- strsplit(lineage, split = "__|;")[[1]]
lowest_rank <- lineage_vector[length(lineage_vector)]
lowest_level <-
tax_levels[lineage_vector[length(lineage_vector) - 1]]
return(tibble(lineage = lineage, lowest_rank = lowest_rank, lowest_level = lowest_level))
}
f1score.out_table.cl_filt_rank_level <- pmap_dfr(f1score.out_table.cl_filt %>%
select(lineage) %>%
distinct(), get_rank_level)
f1score.out_table.cl_filt_lowest_rank_level <- f1score.out_table.cl_filt %>%
inner_join(f1score.out_table.cl_filt_rank_level) %>%
separate(
trait,
into = "categ",
sep = "_",
remove = F,
extra = "drop"
)
write_tsv(
f1score.out_table.cl_filt_lowest_rank_level,
"new_results/filtered_f1_scores_lowest_levels_ranks.gtdb_arc_r86-1.tsv"
)
nrow(f1score.out_table.cl_filt_lowest_rank_level)
read_tsv("new_results/filtered_f1_scores_lowest_levels_ranks.gtdb_arc_r86-1.tsv") %>% nrow()
write_tsv(
f1score.out_table.cl_filt,
"new_results/filtered_f1_scores.gtdb_arc_r86-1.tsv"
)
# save(
# f1score.out_table.cl,
# tree,
# gtdb_tax,
# f1,
# cl_annotation_plot.df,
# tax_levels,
# file = "new_results/gtdb_arc_r86_plot.Rda" #"gtdb_bac_r86_plot.Rda"
# )