forked from MurrellGroup/MolecularEvolution.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfelsenstein.jl
221 lines (206 loc) · 8.09 KB
/
felsenstein.jl
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
"""
felsenstein!(node::FelNode, models; partition_list = nothing)
Should usually be called on the root of the tree. Propagates Felsenstein pass up from the tips to the root.
models can either be a single model (if the messages on the tree contain just one Partition) or an array of models, if the messages have >1 Partition, or
a function that takes a node, and returns a Vector{<:BranchModel} if you need the models to vary from one branch to another.
partition_list (eg. 1:3 or [1,3,5]) lets you choose which partitions to run over.
"""
function felsenstein!(tree::FelNode, models; partition_list = 1:length(tree.message))
stack = [(tree, 1, true)]
#Note to future self: I tried replacing this with an actual stack, but it wasn't a big enough perf diff to justify the extra dependency
#stack = Stack{Tuple{FelNode, Int64, Bool}}()
#push!(stack, (tree,1, true))
node = nothing
while length(stack) > 0
node, ind, first = pop!(stack)
#println(node.nodeindex)
if !isleafnode(node)
if first
push!(stack, (node, ind, false))
for i = 1:length(node.children)
push!(stack, (node.children[i], i, true))
end
end
if !first
for part in partition_list
#Combine child messages into node message.
combine!(
node.message[part],
[mess[part] for mess in node.child_messages],
true,
)
end
if !isroot(node)
#Get the model list for the current branch.
model_list = models(node)
for part in partition_list
backward!(
node.parent.child_messages[ind][part],
node.message[part],
model_list[part],
node,
)
end
end
end
else
model_list = models(node)
for part in partition_list
backward!(
node.parent.child_messages[ind][part],
node.message[part],
model_list[part],
node,
)
end
end
end
end
function felsenstein!(
tree::FelNode,
models::Vector{<:BranchModel};
partition_list = 1:length(tree.message),
)
felsenstein!(tree, x -> models, partition_list = partition_list)
end
function felsenstein!(
tree::FelNode,
model::BranchModel;
partition_list = 1:length(tree.message),
)
felsenstein!(tree, x -> [model], partition_list = partition_list)
end
"""
felsenstein_down!(node::FelNode, models; partition_list = 1:length(tree.message), temp_message = copy_message(tree.message))
Should usually be called on the root of the tree. Propagates Felsenstein pass down from the root to the tips.
felsenstein!() should usually be called first.
models can either be a single model (if the messages on the tree contain just one Partition) or an array of models, if the messages have >1 Partition, or
a function that takes a node, and returns a Vector{<:BranchModel} if you need the models to vary from one branch to another.
partition_list (eg. 1:3 or [1,3,5]) lets you choose which partitions to run over.
"""
function felsenstein_down!(
tree::FelNode,
models;
partition_list = 1:length(tree.message),
temp_message = copy_message(tree.message),
)
stack = [tree]
#curr = nothing
while length(stack) > 0
node = pop!(stack)
model_list = models(node)
if !isleafnode(node)
for part in partition_list
forward!(
temp_message[part],
node.parent_message[part],
model_list[part],
node,
)
end
#Note that this uses a nested loop over children and siblings.
#Thats fine for binary trees, but will blow up when we have massive polytomies.
#Which is stupid, because those are equivalent to binary trees with zero branch lengths.
#Either make this cleverer, or always binarize the trees.
for i = 1:length(node.children)
sib_inds = sibling_inds(node.children[i])
for part in partition_list
if sum(sib_inds) > 0
combine!(
(node.children[i]).parent_message[part],
[mess[part] for mess in node.child_messages[sib_inds]],
true,
)
combine!(
(node.children[i]).parent_message[part],
[temp_message[part]],
false,
)
else
combine!(
(node.children[i]).parent_message[part],
[temp_message[part]],
true,
)
end
end
push!(stack, node.children[i])
end
end
end
end
function felsenstein_down!(
tree::FelNode,
models::Vector{<:BranchModel};
partition_list = 1:length(tree.message),
temp_message = copy_message(tree.message),
)
felsenstein_down!(
tree,
x -> models,
partition_list = partition_list,
temp_message = temp_message,
)
end
function felsenstein_down!(
tree::FelNode,
model::BranchModel;
partition_list = 1:length(tree.message),
temp_message = copy_message(tree.message),
)
felsenstein_down!(
tree,
x -> [model],
partition_list = partition_list,
temp_message = temp_message,
)
end
"""
felsenstein_roundtrip!(tree::FelNode, models; partition_list = 1:length(tree.message), temp_message = copy_message(tree.message[partition_list]))
Should usually be called on the root of the tree. First propagates Felsenstein pass up from the tips to the root,
then propagates Felsenstein pass down from the root to the tips, with the direction of time reversed (i.e. forward! = backward!).
**This is useful when searching for the optimal root** (see [`root_optim!`](@ref)).
models can either be a single model (if the messages on the tree contain just one Partition) or an array of models, if the messages have >1 Partition, or
a function that takes a node, and returns a Vector{<:BranchModel} if you need the models to vary from one branch to another.
partition_list (eg. 1:3 or [1,3,5]) lets you choose which partitions to run over.
"""
function felsenstein_roundtrip!(
tree::FelNode,
models;
partition_list = 1:length(tree.message),
temp_message = copy_message(tree.message[partition_list]),
)
parent_message = tree.parent_message[partition_list] #Store the parent message
tree.parent_message[partition_list] .= temp_message
identity!.(tree.parent_message[partition_list]) #Set the parent message to identity
always_up_models(n::FelNode) = AlwaysUpModel.(models(n))
felsenstein!(tree, always_up_models, partition_list = partition_list)
felsenstein_down!(tree, always_up_models, partition_list = partition_list)
tree.parent_message[partition_list] .= parent_message #Restore the parent message
end
function felsenstein_roundtrip!(
tree::FelNode,
models::Vector{<:BranchModel};
partition_list = 1:length(tree.message),
temp_message = copy_message(tree.message[partition_list]),
)
felsenstein_roundtrip!(
tree,
x -> models,
partition_list = partition_list,
temp_message = temp_message,
)
end
function felsenstein_roundtrip!(
tree::FelNode,
model::BranchModel;
partition_list = 1:length(tree.message),
temp_message = copy_message(tree.message[partition_list]),
)
felsenstein_roundtrip!(
tree,
x -> [model],
partition_list = partition_list,
temp_message = temp_message,
)
end