-
Notifications
You must be signed in to change notification settings - Fork 0
/
01.phylogenetic_tree_calculation.R
173 lines (140 loc) · 4.97 KB
/
01.phylogenetic_tree_calculation.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
#### recommended machine: 02#####
#### packages ----
library("tidyverse")
library("plyr")
library("tidyr")
library("V.PhyloMaker")
library("ape")
library("phytools")
library("tidytree")
#### load sPlot data ----
load("02.data/Backbone3.0.RData")
load("02.data/DT_sPlot3.0.RData")
# select species, genus and family from backbone data
bb.species <- Backbone %>%
filter(
!is.na(Name_correct),
Rank_correct == "species"
) %>%
dplyr::select(
species = Name_short,
genus = Genus_correct,
family = Family_correct,
Rank_correct
) %>%
distinct(species, .keep_all = T) %>%
filter(
!(genus %in% lichen.genera),
!(family %in% mushroom.families)
)
sPlot.species <- DT2 %>%
distinct(Species)
data <- left_join(sPlot.species,
bb.species,
by = c("Species" = "species")
) # to add the genus and family information
rm(DT2, Backbone) # reduce needed RAM
#### calculate tree with V.PhyloMaker package ----
tree3 <- phylo.maker(
sp.list = data,
tree = GBOTB.extended,
nodes = nodes.info.1,
scenarios = "S3"
) # tip for a new genus binds to the 1/2 point of the family branch
#### explore the tree ----
sum(tree3$species.list[[5]] == "bind") # 39247
sum(tree3$species.list[[5]] == "fail to bind") # 8860
sum(tree3$species.list[[5]] == "prune") # 28805
bind <- tree3$species.list %>%
as.data.frame() %>%
filter(status == "bind")
fail <- tree3$species.list %>%
as.data.frame() %>%
filter(status == "fail to bind") %>%
mutate(species = gsub(" ", "_", species)) %>%
mutate(genus = ifelse(is.na(genus), species, genus)) %>%
mutate(
genus = sub("\\_.*", "", genus),
forcebind = NA
)
#### resolve the failed species ----
for (i in 1:nrow(fail)) {
# get the indices in tip.label for every entry in the matching genus
x <- grep(paste0(fail[i, "genus"], "_"), tree3$scenario.3$tip.label)
# for x length 1 the most recent common ancestor (MRCA) is not defined,
# adding the new tip at 1/2 way along the terminal edge leading to the one species of the genus
if (length(x) == 1) {
tree3$scenario.3 <- bind.tip(tree3$scenario.3, paste0(fail[i, "species"]),
where = x,
position = 0.5 * tree3$scenario.3$edge.length[which(tree3$scenario.3$edge[, 2] == x)]
)
fail[i, "status"] <- "1/2 terminal level"
} else if (length(x) > 1) {
# for x > 1 new species is bind to most recent common ancestor
y <- findMRCA(tree3$scenario.3, tree3$scenario.3$tip.label[x])
tree3$scenario.3 <- bind.tip(tree3$scenario.3, paste0(fail[i, "species"]), where = y)
fail[i, "status"] <- "MRCA"
} else {
fail[i, "forcebind"] <- "failed"
}
}
# check which species remain unresolved
missing <- fail %>% filter(forcebind == "failed")
tree.species <- sp.tree %>%
filter(status != "fail to bind") %>%
rbind(fail %>% select(-forcebind))
#### save ----
sPlot.phylo.tree <- tree3
save(tree.species, sPlot.phylo.tree, file = "02.data/PhyloTree_sPlot3.0.Rdata")
### calculate tree again----
#### calculate tree with V.PhyloMaker package ----
tree3.1 <- phylo.maker(
sp.list = data,
tree = GBOTB.extended,
nodes = nodes.info.1,
scenarios = "S3"
) # tip for a new genus binds to the 1/2 point of the family branch
#### explore the tree ----
sum(tree3.1$species.list[[5]] == "bind") # 39247
sum(tree3.1$species.list[[5]] == "fail to bind") # 8860
sum(tree3.1$species.list[[5]] == "prune") # 28805
bind1 <- tree3.1$species.list %>%
as.data.frame() %>%
filter(status == "bind")
fail1 <- tree3.1$species.list %>%
as.data.frame() %>%
filter(status == "fail to bind") %>%
mutate(species = gsub(" ", "_", species)) %>%
mutate(genus = ifelse(is.na(genus), species, genus)) %>%
mutate(
genus = sub("\\_.*", "", genus),
forcebind = NA
)
#### resolve the failed species ----
for (i in 1:nrow(fail1)) {
# get the indices in tip.label for every entry in the matching genus
x <- grep(paste0(fail1[i, "genus"], "_"), tree3.1$scenario.3$tip.label)
# for x length 1 the most recent common ancestor (MRCA) is not defined,
# adding the new tip at 1/2 way along the terminal edge leading to the one species of the genus
if (length(x) == 1) {
tree3.1$scenario.3 <- bind.tip(tree3.1$scenario.3, paste0(fail1[i, "species"]),
where = x,
position = 0.5 * tree3.1$scenario.3$edge.length[which(tree3.1$scenario.3$edge[, 2] == x)]
)
fail1[i, "status"] <- "1/2 terminal level"
} else if (length(x) > 1) {
# for x > 1 new species is bind to most recent common ancestor
y <- findMRCA(tree3.1$scenario.3, tree3.1$scenario.3$tip.label[x])
tree3.1$scenario.3 <- bind.tip(tree3.1$scenario.3, paste0(fail1[i, "species"]), where = y)
fail1[i, "status"] <- "MRCA"
} else {
fail1[i, "forcebind"] <- "failed"
}
}
# check which species remain unresolved
missing1 <- fail1 %>% filter(forcebind == "failed")
#### save ----
sPlot.phylo.tree1 <- tree3.1
save(sPlot.phylo.tree1, file = "02.data/PhyloTree-1_sPlot3.0.Rdata")
load("02.data/PhyloTree_sPlot3.0.Rdata")
comparePhylo(sPlot.phylo.tree$scenario.3, sPlot.phylo.tree1$scenario.3)