-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path.Rhistory
512 lines (512 loc) · 24.9 KB
/
.Rhistory
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
y = y+1
}
}
}
duplicates<-unique(node_data$cluster[duplicated(node_data$cluster)])
problems<-duplicates[which(stringr::str_count(duplicates, pattern = "\\.") == 0)]
duplicates<-duplicates[which(stringr::str_count(duplicates, pattern = "\\.") != 0)]
for (i in 1:length(duplicates)) {
test<-which(node_data$cluster == duplicates[i])
test<-test[-c(1)]
x<-1
for (j in 1:length(test)) {
name<-unlist(stringr::str_split(node_data$cluster[test[j]], "\\."))
name[length(name)]<-x+as.integer(name[length(name)])
x<-(x+1)
node_data$cluster[test[j]]<-paste(c(name), collapse='.' )
}
}
}
unclassified<-which(!grepl("_", node_data$cluster))
unclassified<-unclassified[c(-1)]
#
for (i in 1:(length(node_data$Node))) {
test<-which(node_data$Node %in% ips::descendants(tree, node_data$Node[i], type = "all", ignore.tip = T))
node_data$test[c(test)] <- paste(node_data$cluster[i], ".1", sep = "")
node_data$test<-stringr::str_replace(node_data$test, "A1\\..\\..\\..", "B1")
node_data$test<-stringr::str_replace(node_data$test, "B1\\..\\..\\..", "C1")
node_data$test<-stringr::str_replace(node_data$test, "C1\\..\\..\\..", "D1")
node_data$test<-stringr::str_replace(node_data$test, "D1\\..\\..\\..", "E1")
node_data$test<-stringr::str_replace(node_data$test, "E1\\..\\..\\..", "F1")
node_data$test<-stringr::str_replace(node_data$test, "F1\\..\\..\\..", "G1")
node_data$test<-stringr::str_replace(node_data$test, "G1\\..\\..\\..", "H1")
node_data$test<-stringr::str_replace(node_data$test, "H1\\..\\..\\..", "I1")
node_data$test<-stringr::str_replace(node_data$test, "I1\\..\\..\\..", "J1")
node_data$test<-stringr::str_replace(node_data$test, "J1\\..\\..\\..", "K1")
node_data$test<-stringr::str_replace(node_data$test, "K1\\..\\..\\..", "L1")
node_data$test<-stringr::str_replace(node_data$test, "L1\\..\\..\\..", "M1")
node_data$test<-stringr::str_replace(node_data$test, "M1\\..\\..\\..", "N1")
node_data$test<-stringr::str_replace(node_data$test, "N1\\..\\..\\..", "O1")
node_data$test<-stringr::str_replace(node_data$test, "O1\\..\\..\\..", "P1")
node_data$test<-stringr::str_replace(node_data$test, "P1\\..\\..\\..", "Q1")
node_data$test<-stringr::str_replace(node_data$test, "Q1\\..\\..\\..", "R1")
node_data$test<-stringr::str_replace(node_data$test, "R1\\..\\..\\..", "S1")
node_data$test<-stringr::str_replace(node_data$test, "S1\\..\\..\\..", "T1")
node_data$cluster[unclassified]<-node_data$test[unclassified]
for (v in 1:length(problem_names$letters)) {
if (length(which(node_data$cluster == problem_names$letters[v]))>1) {
problems<-which(node_data$cluster == problem_names$letters[v])
problems<-problems[-c(1)]
y=1
for (f in 1:length(problems)) {
letter<-which(problem_names$letters == (node_data$cluster[problems[f]]))
node_data$cluster[problems[f]]<-problem_names$letters[(letter+y)]
y = y+1
}
}
}
duplicates<-unique(node_data$cluster[duplicated(node_data$cluster)])
problems<-duplicates[which(stringr::str_count(duplicates, pattern = "\\.") == 0)]
duplicates<-duplicates[which(stringr::str_count(duplicates, pattern = "\\.") != 0)]
for (i in 1:length(duplicates)) {
test<-which(node_data$cluster == duplicates[i])
test<-test[-c(1)]
x<-1
for (j in 1:length(test)) {
name<-unlist(stringr::str_split(node_data$cluster[test[j]], "\\."))
name[length(name)]<-x+as.integer(name[length(name)])
x<-(x+1)
node_data$cluster[test[j]]<-paste(c(name), collapse='.' )
}
}
fix<-which(node_data$cluster %in% 1:1000)
while (length(fix) != 0) {
letter<-problem_names$letters[(length(which(problem_names$letters %in% node_data$cluster))+1)]
node_data$cluster<-gsub(fix, letter, node_data$cluster)
fix<-which(node_data$cluster %in% 1:1000)
}
fix<-grep("NA", node_data$cluster)
fix<-c(fix, which(is.na(node_data$cluster)))
while (length(fix) != 0) {
letter<-problem_names$letters[(length(which(problem_names$letters %in% node_data$cluster))+1)]
node_data$cluster<-gsub("NA", letter, node_data$cluster)
node_data$cluster[which(is.na(node_data$cluster))]<-letter
fix<-grep("NA", node_data$cluster)
fix<-c(fix, which(is.na(node_data$cluster)))
}
duplicates<-unique(node_data$cluster[duplicated(node_data$cluster)])
x<-2
while (length(duplicates) != 0 && all(!is.na(duplicates))) {
for (i in 1:length(duplicates)) {
test<-which(node_data$cluster == duplicates[i])
test<-test[-c(1)]
for (j in 1:length(test)) {
name<-unlist(stringr::str_split(node_data$cluster[test[j]], "_"))
node_data$cluster[test[j]]<-paste(name[1], problem_names$letters[x], sep = "_")
x<-(x+1)
}
}
duplicates<-unique(node_data$cluster[duplicated(node_data$cluster)])
}
}
node_data<-node_data[, -c((grep("test", names(node_data))), grep("previous", names(node_data)))]
node_data<-node_data[,-c(4)]
names(node_data)<-c("node", "n_tips", "n_diff_ancestor", "lineage")
if(length(grep("-", node_data$lineage)) != 0){
clade<-strsplit(node_data$lineage[grep("-", node_data$lineage)][1], "-")[[1]][1]
node_data$lineage[-c(grep("-", node_data$lineage))]<-
paste(clade, node_data$lineage[-c(grep("-", node_data$lineage))])
}else{
if(length(grep("_", node_data$lineage)) != 0){
clade<-strsplit(node_data$lineage[grep("_", node_data$lineage)][1], " ")[[1]][1]
node_data$lineage[-c(grep("_", node_data$lineage))]<-
paste(clade, node_data$lineage[-c(grep("_", node_data$lineage))])
}}
if(length(grep("NA", node_data$lineage) != 0)){
node_data<-node_data[-c(grep("NA", node_data$lineage)),]
}
rm(list=ls())
args<-"Cosmo"
tree<-ape::read.tree(paste(args, "/Trees/", args, "_combined_aligned.fasta.contree", sep = ""))
ancestral<-seqinr::read.alignment(paste(args, "/Timetree/ancestral_sequences.fasta", sep = ""), format = "fasta")
metadata<-read.csv(paste(args, "/", args, "_combined_metadata.csv", sep = ""))
metadata$year[which(is.na(metadata$year))]<-"-"
alignment<-seqinr::read.alignment(paste(args, "/Alignment/", args, "_combined_aligned.fasta", sep = ""), format = "fasta")
all_lineage<-read.csv("inst/extdata/References/RABV/lineage_info.csv")
min.support<-90
tree$node.comment<- gsub(".*=", "", tree$node.label, perl = T)
alignment_matrix <- seqinr::as.matrix.alignment(alignment)
ancestral_matrix <- seqinr::as.matrix.alignment(ancestral)
sequences <- 10
max.support<-100
# Need it as a matrix for later analyses
`%notin%` <- Negate(`%in%`)
#############################################
# BOOTSTRAP SUPPORT #
#############################################
# Identify nodes with a bootstrap of over 70 (why would the first ~570 nodes be NA?)
nodes_70 <- which(tree$node.comment > min.support | tree$node.comment == max.support)
nodes_70 <- nodes_70 + length(tree$tip.label)
node_data <- data.frame(Node = nodes_70, n_tips = NA)
# Make a dataframe ready for values to be put in
# Fill the first column with the numbers of the nodes identified in the previous steps
for(i in 1:length(nodes_70)) {
node_data[i,2] <- length(phangorn::Descendants(tree, nodes_70[i], type = "tips")[[1]])
}
# For each node identified in the previous step, count the number of tips descended from that node
nodes_5 <- node_data[(which(node_data[,2]>= sequences)),]
# Only carry forwards nodes which have more than 5 tips descended from it
# This has been identified as the definition for a cluster in previous studies
#############################################
# 95% COVERAGE WGS #
#############################################
# Make a dataframe ready to fill with info about number of gaps and N bases, and length of the alignment and sequence
seq_data <- data.frame(ID = alignment$nam, N = NA, "gap" = NA,
Length_before = nchar(alignment$seq[[1]]), Length_after = NA)
for (i in 1:length(alignment$seq)) {
seq_data$N[i] <- stringr::str_count(alignment$seq[[i]], pattern = 'n')
seq_data$gap[i] <- stringr::str_count(alignment$seq[[i]], pattern = '-')
seq_data$Length_after[i] <- (seq_data$Length_before[i] - seq_data$N[i] - seq_data$gap[i])
}
# For each sequence, count the number of n bases and gaps
# Calculate the length after removing these
# Identify seqs with less than 95% coverage and corresponding to tip numbers
# List the ancestor nodes for each of these tip numbers
#############################################
# DIFFERENCE FROM ANCESTOR #
#############################################
seq_data$Year <- NA # Add another column to the seq data ready to fill in dates
# Add collection year of each sequence to the table (Use latest, as exact collection not always filled in)
for (i in 1:length(alignment$seq)) {
seq_data$Year[i] <- metadata$year[which(metadata$ID == seq_data$ID[i])]
}
nodes_5$diff <- NA # Add a column in nodes_5 to count the number of nucleotide differences each cluster has from the old seq
# For each node of interest, find all the tips
# Make a note of the differences between the oldest seq in the each cluster/lineage and one of the seqs in the lineage
# Which differences between the old seq and each seq are shared between all the seqs in the lineage
# E.g. which lineages show one or more shared nucleotides differences from the ancestor
# Count these differences and add them to the table to be analysed further (may just be n's)
nodes_reduced <- data.frame(Nodes = (nodes_5$Node - (1+length(tree$tip.label))))
for (i in 1:length(nodes_5$Node)) {
cm <- caper::clade.members(nodes_5$Node[i], tree, include.nodes = F, tip.labels = T)
seq_cm <- which(seq_data$ID %in% cm)
old <- which(row.names(ancestral_matrix) == paste("NODE_", (sprintf("%07d", nodes_reduced$Nodes[i])), sep=""))
tips <- which(row.names(ancestral_matrix) %in% cm)
x <- which(ancestral_matrix[old,] != ancestral_matrix[(tips[1]),])
for (j in tips[-c(1)]) {
x <- x[which(x %in% (which(ancestral_matrix[old,] != ancestral_matrix[j,])))]
print(x)
nodes_5$diff[i] <- length(x)
}
}
nodes_diff <- nodes_5[(which(nodes_5[,3]!=0)),] # Get rid of the ones with no differences straight away
#############################################
# OVERLAPPING TIPS REMOVAL #
#############################################
# Add a column to nodes_diff and for each node, count how many of the other nodes of interest are descended from it
nodes_diff$overlaps <- NA
for (i in 1:length(nodes_diff$Node)) {
nodes_diff$overlaps[i] <- length(which((phangorn::allDescendants(tree)[[(nodes_diff[i,1])]]) %in% nodes_diff[,1]))
}
# Create a data frame for lineage assignments. Add the tip labels, and a column ready to add the lineage they're assigned to
lineage_assignments <- data.frame(tip = tree$tip.label, cluster = NA)
# Order the nodes of interest by the number of times they overlap the other nodes of interest (descending)
nodes_diff <- nodes_diff[order(-nodes_diff$overlaps),]
# Add a column called cluster and label the clusters
nodes_diff$cluster <- c(1:(length(nodes_diff$Node)))
for (i in 1:(length(nodes_diff$Node))) {
lineage_assignments[which(lineage_assignments[,1] %in% caper::clade.members(nodes_diff[i,1], tree, include.nodes = F, tip.labels = T)), 2] <- nodes_diff[i,5]
}
# For each sequence, see if it's a member of a lineage. If yes, put the number of the cluster in it's lineage assignment
# Do this in order of the node with the most overlaps to the least, to ensure the assignment is at the lowest possible level
# E.g. if a sequence is in clusters 1-7, it will appear as 7
summary <- data.frame(cluster = nodes_diff$cluster, count = NA)
for (i in 1:(length(summary$cluster))) {
summary$count[i] <- length(which(lineage_assignments$cluster == summary$cluster[i]))
}
# Count the number of sequences assigned to each lineage
if(length(which(summary$count < 2))<0){
nodes_diff <- nodes_diff[-c(which(nodes_diff$cluster %in% summary$cluster[(which(summary$count < 2))])),]
}
# If any lineages have no sequences in them, remove them as an option from the nodes_diff table
min <- min(summary$count)
while (min < 2){
nodes_diff <- nodes_diff[order(-nodes_diff$overlaps),]
nodes_diff$cluster <-c(1:(length(nodes_diff$Node)))
lineage_assignments$cluster <- NA
for (i in c(1:(length(nodes_diff$Node)))) {
lineage_assignments[which(lineage_assignments[,1] %in% caper::clade.members((nodes_diff[i,1]), tree, include.nodes = F, tip.labels = T)),2]<-nodes_diff[i,5]
}
summary <- data.frame(cluster = nodes_diff$cluster, count = NA)
for (i in 1:(length(summary$cluster))) {
summary$count[i] <- length(which(lineage_assignments$cluster == summary$cluster[i]))
}
min <- min(summary$count)
if (min >= 2) {
print("done")
} else {
nodes_diff<-nodes_diff[-c(which(nodes_diff$cluster %in% summary$cluster[(which(summary$count < 2))])), ]
}
}
# Repeat the above steps until there are no clusters with 0 sequences left
issues<-data.frame(node = nodes_diff$Node, n_tips = nodes_diff$n_tips, cluster = nodes_diff$cluster)
issues<-issues[order(issues$cluster),]
issues$parent<-NA
issues$parent[1]<-""
for (i in 2:length(issues$node)) {
if (length(which(issues$node %in% treeio::ancestor(tree, issues$node[i]))) == 0) {
issues$parent[i]<-""
} else {
parent<-issues$cluster[which(issues$node %in% treeio::ancestor(tree, issues$node[i]))]
issues$parent[i]<-parent[length(parent)]
}
}
issues<-issues[rev(order(issues$parent)),]
issues$number<-NA
for (i in 1:length(which(issues$parent %in% 1:1000))) {
issues$number[i]<-
issues$n_tips[which(issues$cluster == issues$parent[i])] - issues$n_tips[i]
}
if(length(which(issues$number <5)) != 0){
nodes_diff<-nodes_diff[-c(which(nodes_diff$Node %in% issues$node[which(issues$number < 5)])),]
}
for (i in 1:(length(nodes_diff$Node))) {
lineage_assignments[which(lineage_assignments[,1] %in% caper::clade.members(nodes_diff[i,1], tree, include.nodes = F, tip.labels = T)), 2] <- nodes_diff[i,5]
}
nodes_diff$numbers<-1:length(nodes_diff$Node)
for (i in 1:length(nodes_diff$Node)) {
lineage_assignments$cluster[which(lineage_assignments$cluster == nodes_diff$cluster[i])]<-
nodes_diff$numbers[i]
}
nodes_diff$cluster<-nodes_diff$numbers
for(i in 1:length(seq_data$ID)){
seq_data$cluster[i]<-lineage_assignments$cluster[which(lineage_assignments$tip == seq_data$ID[i])]
}
sequence_data<-seq_data
node_data<-nodes_diff
node_data<-node_data[order(node_data$overlaps, decreasing = T),]
sequence_data$previous <- NA
for (i in 1:length(sequence_data$ID)) {
sequence_data$previous[i]<-
metadata$assignment[which(metadata$ID == sequence_data$ID[i])]
}
previous_assignments<-data.frame(assignment = unique(sequence_data$previous), node = NA)
node_data$previous<-NA
for (i in 1:length(node_data$Node)) {
clades<-unique(sequence_data$previous[
which(sequence_data$ID %in% tree$tip.label[c(unlist(
phangorn::Descendants(tree, node_data$Node[i], type = "tips")))])])
node_data$previous[i]<-
paste(c(clades), collapse = ", ")
}
for (i in 1:length(previous_assignments$assignment)) {
previous_assignments$node[i]<-which(node_data$previous == previous_assignments$assignment[i])[1]
previous_assignments$assignment[i]<-previous_assignments$assignment[i]
}
possible_names<-data.frame(names = rep(previous_assignments$assignment, 26))
previous_assignments$assignment<-paste(previous_assignments$assignment, "_A1", sep = "")
for (i in 1:length(previous_assignments$assignment)) {
node_data$cluster[previous_assignments$node[i]]<-previous_assignments$assignment[i]
}
if ((length(which(previous_assignments$node == 1))) == 0) {
node_data$cluster[1]<-"A1"
}
node_data<-node_data[order(node_data$overlaps, decreasing = T), ]
node_data$test <- NA
problem_names<-data.frame(letters = c("A1", "B1", "C1", "D1", "E1", "F1", "G1", "H1", "I1", "J1", "K1", "L1", "M1", "N1",
"O1", "P1", "Q1", "R1", "S1", "T1", "U1", "V1", "W1", "X1", "Y1", "Z1", "AA1", "AB1",
"AC1", "AD1", "AE1", "AF1", "AG1", "AH1", "AI1", "AJ1", "AK1",
"AL1", "AM1", "AN1", "AO1", "AP1", "AQ1", "AR1", "AS1", "AT1",
"AU1", "AV1", "AW1", "AX1", "AY1", "AZ1", "BA1", "BB1", "BC1",
"BD1", "BE1", "BF1", "BG1", "BH1", "BI1", "BJ1", "BK1", "BL1",
"BM1", "BN1", "BO1", "BP1", "BQ1", "BR1", "BS1", "BT1", "BU1",
"BV1", "BW1", "BX1", "BY1", "BZ1"))
possible_names<-possible_names[order(possible_names$names),]
possible_names<-paste(possible_names, problem_names$letters, sep = "_")
issues<-which(node_data$Node %notin% ips::descendants(tree, node_data$Node[1], type = "all", ignore.tip = T))
x<-1
y<-1
numbers<-1
while (length(issues)>y) {
issues<-issues[-c(1)]
node_data$cluster[issues[1]]<-paste(node_data$previous[issues[1]], "_", problem_names$letters[x], sep = "")
numbers<-c(numbers, issues[1])
nodes<-ips::descendants(tree, node_data$Node[1], type = "all", ignore.tip = T)
for (i in 2:length(numbers)){
nodes<-c(nodes, ips::descendants(tree, node_data$Node[i], type = "all", ignore.tip = T))
}
issues<-which(node_data$Node %notin% nodes)
y<-y+1
if (length(grep(problem_names$letters[2], node_data$cluster)) == 0) {
x<-1
} else {
x<-x+1
}
}
fix<-grep(",", node_data$cluster)
while (length(fix) != 0) {
letter<-problem_names$letters[(length(which(problem_names$letters %in% node_data$cluster))+1)]
node_data$cluster<-gsub(node_data$cluster[fix], letter, node_data$cluster)
fix<-grep(",", node_data$cluster)
}
for (i in 1:length(node_data$Node)) {
test<-which(node_data$Node %in% ips::descendants(tree, node_data$Node[i], type = "all", ignore.tip = T))
node_data$test[c(test)] <- paste(node_data$cluster[i], ".1", sep = "")
node_data$test<-stringr::str_replace(node_data$test, "A1\\..\\..\\..", "B1")
node_data$test<-stringr::str_replace(node_data$test, "B1\\..\\..\\..", "C1")
node_data$test<-stringr::str_replace(node_data$test, "C1\\..\\..\\..", "D1")
node_data$test<-stringr::str_replace(node_data$test, "D1\\..\\..\\..", "E1")
node_data$test<-stringr::str_replace(node_data$test, "E1\\..\\..\\..", "F1")
node_data$test<-stringr::str_replace(node_data$test, "F1\\..\\..\\..", "G1")
node_data$test<-stringr::str_replace(node_data$test, "G1\\..\\..\\..", "H1")
node_data$test<-stringr::str_replace(node_data$test, "H1\\..\\..\\..", "I1")
node_data$test<-stringr::str_replace(node_data$test, "I1\\..\\..\\..", "J1")
node_data$test<-stringr::str_replace(node_data$test, "J1\\..\\..\\..", "K1")
node_data$test<-stringr::str_replace(node_data$test, "K1\\..\\..\\..", "L1")
node_data$test<-stringr::str_replace(node_data$test, "L1\\..\\..\\..", "M1")
node_data$test<-stringr::str_replace(node_data$test, "M1\\..\\..\\..", "N1")
node_data$test<-stringr::str_replace(node_data$test, "N1\\..\\..\\..", "O1")
node_data$test<-stringr::str_replace(node_data$test, "O1\\..\\..\\..", "P1")
node_data$test<-stringr::str_replace(node_data$test, "P1\\..\\..\\..", "Q1")
node_data$test<-stringr::str_replace(node_data$test, "Q1\\..\\..\\..", "R1")
node_data$test<-stringr::str_replace(node_data$test, "R1\\..\\..\\..", "S1")
node_data$test<-stringr::str_replace(node_data$test, "S1\\..\\..\\..", "T1")
majors<-which(grepl("_", node_data$test))
node_data$cluster[c(majors)] <- node_data$test[c(majors)]
for (k in 1:length(possible_names)) {
if (length(which(node_data$cluster == possible_names[k]))>1) {
problems<-which(node_data$cluster == possible_names[k])
problems<-problems[-c(1)]
y=1
for (a in 1:length(problems)) {
letter<-which(problem_names$letters == (stringr::str_split(node_data$cluster[problems[a]], "_")[[1]][2]))
node_data$cluster[problems[a]]<-paste((stringr::str_split(node_data$cluster[problems[a]], "_")[[1]][1]), problem_names$letters[(letter+y)], sep = "_")
y = y+1
}
}
}
duplicates<-unique(node_data$cluster[duplicated(node_data$cluster)])
problems<-duplicates[which(stringr::str_count(duplicates, pattern = "\\.") == 0)]
duplicates<-duplicates[which(stringr::str_count(duplicates, pattern = "\\.") != 0)]
for (i in 1:length(duplicates)) {
test<-which(node_data$cluster == duplicates[i])
test<-test[-c(1)]
x<-1
for (j in 1:length(test)) {
name<-unlist(stringr::str_split(node_data$cluster[test[j]], "\\."))
name[length(name)]<-x+as.integer(name[length(name)])
x<-(x+1)
node_data$cluster[test[j]]<-paste(c(name), collapse='.' )
}
}
}
unclassified<-which(!grepl("_", node_data$cluster))
unclassified<-unclassified[c(-1)]
for (i in 1:length(node_data$Node)) {
test<-which(node_data$Node %in% ips::descendants(tree, node_data$Node[i], type = "all", ignore.tip = T))
node_data$test[c(test)] <- paste(node_data$cluster[i], ".1", sep = "")
node_data$test<-stringr::str_replace(node_data$test, "A1\\..\\..\\..", "B1")
node_data$test<-stringr::str_replace(node_data$test, "B1\\..\\..\\..", "C1")
node_data$test<-stringr::str_replace(node_data$test, "C1\\..\\..\\..", "D1")
node_data$test<-stringr::str_replace(node_data$test, "D1\\..\\..\\..", "E1")
node_data$test<-stringr::str_replace(node_data$test, "E1\\..\\..\\..", "F1")
node_data$test<-stringr::str_replace(node_data$test, "F1\\..\\..\\..", "G1")
node_data$test<-stringr::str_replace(node_data$test, "G1\\..\\..\\..", "H1")
node_data$test<-stringr::str_replace(node_data$test, "H1\\..\\..\\..", "I1")
node_data$test<-stringr::str_replace(node_data$test, "I1\\..\\..\\..", "J1")
node_data$test<-stringr::str_replace(node_data$test, "J1\\..\\..\\..", "K1")
node_data$test<-stringr::str_replace(node_data$test, "K1\\..\\..\\..", "L1")
node_data$test<-stringr::str_replace(node_data$test, "L1\\..\\..\\..", "M1")
node_data$test<-stringr::str_replace(node_data$test, "M1\\..\\..\\..", "N1")
node_data$test<-stringr::str_replace(node_data$test, "N1\\..\\..\\..", "O1")
node_data$test<-stringr::str_replace(node_data$test, "O1\\..\\..\\..", "P1")
node_data$test<-stringr::str_replace(node_data$test, "P1\\..\\..\\..", "Q1")
node_data$test<-stringr::str_replace(node_data$test, "Q1\\..\\..\\..", "R1")
node_data$test<-stringr::str_replace(node_data$test, "R1\\..\\..\\..", "S1")
node_data$test<-stringr::str_replace(node_data$test, "S1\\..\\..\\..", "T1")
node_data$cluster[unclassified]<-node_data$test[unclassified]
for (v in 1:length(problem_names$letters)) {
if (length(which(node_data$cluster == problem_names$letters[v]))>1) {
problems<-which(node_data$cluster == problem_names$letters[v])
problems<-problems[-c(1)]
y=1
for (f in 1:length(problems)) {
letter<-which(problem_names$letters == (node_data$cluster[problems[f]]))
node_data$cluster[problems[f]]<-problem_names$letters[(letter+y)]
y = y+1
}
}
}
duplicates<-unique(node_data$cluster[duplicated(node_data$cluster)])
problems<-duplicates[which(stringr::str_count(duplicates, pattern = "\\.") == 0)]
duplicates<-duplicates[which(stringr::str_count(duplicates, pattern = "\\.") != 0)]
for (i in 1:length(duplicates)) {
test<-which(node_data$cluster == duplicates[i])
test<-test[-c(1)]
x<-1
for (j in 1:length(test)) {
name<-unlist(stringr::str_split(node_data$cluster[test[j]], "\\."))
name[length(name)]<-x+as.integer(name[length(name)])
x<-(x+1)
node_data$cluster[test[j]]<-paste(c(name), collapse='.' )
}
}
fix<-which(node_data$cluster %in% 1:1000)
while (length(fix) != 0) {
letter<-problem_names$letters[(length(which(problem_names$letters %in% node_data$cluster))+1)]
node_data$cluster<-gsub(fix, letter, node_data$cluster)
fix<-which(node_data$cluster %in% 1:1000)
}
fix<-grep("NA", node_data$cluster)
fix<-c(fix, which(is.na(node_data$cluster)))
while (length(fix) != 0) {
letter<-problem_names$letters[(length(which(problem_names$letters %in% node_data$cluster))+1)]
node_data$cluster<-gsub("NA", letter, node_data$cluster)
node_data$cluster[which(is.na(node_data$cluster))]<-letter
fix<-grep("NA", node_data$cluster)
fix<-c(fix, which(is.na(node_data$cluster)))
}
duplicates<-unique(node_data$cluster[duplicated(node_data$cluster)])
x<-2
while (length(duplicates) != 0 && all(!is.na(duplicates))) {
for (i in 1:length(duplicates)) {
test<-which(node_data$cluster == duplicates[i])
test<-test[-c(1)]
for (j in 1:length(test)) {
name<-unlist(stringr::str_split(node_data$cluster[test[j]], "_"))
node_data$cluster[test[j]]<-paste(name[1], problem_names$letters[x], sep = "_")
x<-(x+1)
}
}
duplicates<-unique(node_data$cluster[duplicated(node_data$cluster)])
}
}
node_data<-node_data[, -c((grep("test", names(node_data))), grep("previous", names(node_data)))]
for (i in 1:length(node_data$cluster)) {
sequence_data$cluster[which(sequence_data$cluster == i)] <- node_data$cluster[i]
}
sequence_data<-sequence_data[,-c(4)]
names(sequence_data)<-c("ID", "n_N", "n_gap", "length", "year", "lineage", "previous")
if(length(grep("-", node_data$cluster)) != 0){
clade<-strsplit(node_data$cluster[grep("-", node_data$cluster)][1], "-")[[1]][1]
sequence_data$lineage[-c(grep("-", sequence_data$lineage))]<-
paste(clade, sequence_data$lineage[-c(grep("-", sequence_data$lineage))])
}else{
if(length(grep("_", node_data$cluster)) != 0){
clade<-strsplit(node_data$cluster[grep("_", node_data$cluster)][1], " ")[[1]][1]
sequence_data$lineage[-c(grep("_", sequence_data$lineage))]<-
paste(clade, sequence_data$lineage[-c(grep("_", sequence_data$lineage))])
}}
if(length(grep("NA", node_data$lineage) != 0)){
node_data<-node_data[-c(grep("NA", node_data$lineage)),]
}
sequence_data$lineage[grep("NA", sequence_data$lineage)]<-NA
rm(list=ls())
args<-"Cosmo"
source("Run/seq_designation_nolength.R")
source("Run/node_info_nolength.R")
source("R/lineage_info.R")
library(dplyr)
library(ggtree)
library(ape)
library(phytools)
library(phangorn)
library(castor)
library(ips)
tree<-ape::read.tree(paste(args, "/Trees/", args, "_combined_aligned.fasta.contree", sep = ""))
ancestral<-seqinr::read.alignment(paste(args, "/Timetree/ancestral_sequences.fasta", sep = ""), format = "fasta")
metadata<-read.csv(paste(args, "/", args, "_combined_metadata.csv", sep = ""))
metadata$year[which(is.na(metadata$year))]<-"-"
alignment<-seqinr::read.alignment(paste(args, "/Alignment/", args, "_combined_aligned.fasta", sep = ""), format = "fasta")
all_lineage<-read.csv("inst/extdata/References/RABV/lineage_info.csv")
node_data<-node_info(tree, 90, alignment, metadata, ancestral)