-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathEpochBranchModel.java
More file actions
201 lines (160 loc) · 6.24 KB
/
EpochBranchModel.java
File metadata and controls
201 lines (160 loc) · 6.24 KB
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
/*
* EpochBranchModel.java
*
* Copyright (c) 2002-2022 Alexei Drummond, Andrew Rambaut and Marc Suchard
*
* This file is part of BEAST.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership and licensing.
*
* BEAST is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* BEAST is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with BEAST; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
package dr.evomodel.branchmodel;
import dr.evolution.tree.MutableTreeModel;
import dr.evomodel.substmodel.FrequencyModel;
import dr.evomodel.substmodel.SubstitutionModel;
import dr.evolution.tree.NodeRef;
import dr.inference.model.*;
import dr.util.Author;
import dr.util.Citable;
import dr.util.Citation;
import java.util.*;
/**
* @author Filip Bielejec
* @author Andrew Rambaut
* @author Marc A. Suchard
* @version $Id$
*/
public class EpochBranchModel extends AbstractModel implements BranchModel, Citable {
public static final String EPOCH_BRANCH_MODEL = "EpochBranchModel";
public EpochBranchModel(MutableTreeModel tree,
List<SubstitutionModel> substitutionModels,
Parameter epochTimes) {
super(EPOCH_BRANCH_MODEL);
this.substitutionModels = substitutionModels;
if (substitutionModels == null || substitutionModels.size() == 0) {
throw new IllegalArgumentException("EpochBranchModel must be provided with at least one substitution model");
}
this.epochTimes = epochTimes;
this.tree = tree;
for (SubstitutionModel model : substitutionModels) {
addModel(model);
}
addModel(tree);
addVariable(epochTimes);
}// END: Constructor
@Override
public Mapping getBranchModelMapping(NodeRef node) {
int nModels = substitutionModels.size();
int epochCount = nModels - 1;
double[] transitionTimes = epochTimes.getParameterValues();
double parentHeight = tree.getNodeHeight(tree.getParent(node));
double nodeHeight = tree.getNodeHeight(node);
List<Double> weightList = new ArrayList<>();
List<Integer> orderList = new ArrayList<>();
// find the epoch that the node height is in...
int epoch = 0;
while (epoch < epochCount && nodeHeight >= transitionTimes[epoch]) {
epoch ++;
}
double currentHeight = nodeHeight;
// find the epoch that the parent height is in...
while (epoch < epochCount && parentHeight > transitionTimes[epoch]) {
weightList.add( transitionTimes[epoch] - currentHeight );
orderList.add(epoch);
currentHeight = transitionTimes[epoch];
epoch ++;
}
weightList.add( parentHeight - currentHeight );
orderList.add(epoch);
final int[] order = new int[orderList.size()];
final double[] weights = new double[weightList.size()];
for (int i = 0; i < orderList.size(); i++) {
order[i] = orderList.get(i);
weights[i] = weightList.get(i);
}
return new Mapping() {
@Override
public int[] getOrder() {
return order;
}
@Override
public double[] getWeights() {
return weights;
}
};
}// END: getBranchModelMapping
@Override
public boolean requiresMatrixConvolution() {
return true;
}
@Override
public List<SubstitutionModel> getSubstitutionModels() {
return substitutionModels;
}
@Override
public SubstitutionModel getRootSubstitutionModel() {
return substitutionModels.get(substitutionModels.size() - 1);
}
public FrequencyModel getRootFrequencyModel() {
return rootFrequencyModel == null ?
getRootSubstitutionModel().getFrequencyModel() :
rootFrequencyModel;
}
protected void handleModelChangedEvent(Model model, Object object, int index) {
fireModelChanged();
}// END: handleModelChangedEvent
@SuppressWarnings("rawtypes")
protected void handleVariableChangedEvent(Variable variable, int index,
Parameter.ChangeType type) {
}// END: handleVariableChangedEvent
protected void storeState() {
}// END: storeState
protected void restoreState() {
}// END: restoreState
protected void acceptState() {
}// END: acceptState
@Override
public Citation.Category getCategory() {
return Citation.Category.SUBSTITUTION_MODELS;
}
@Override
public String getDescription() {
return "Epoch Branch model";
}
public List<Citation> getCitations() {
return Collections.singletonList(
new Citation(new Author[]{new Author("F", "Bielejec"),
new Author("P", "Lemey"),
new Author("G", "Baele"),
new Author("A", "Rambaut"),
new Author("MA", "Suchard")},
"Inferring heterogeneous evolutionary processes through time: from sequence substitution to phylogeography",
2014,
"Systematic Biology",
63,
493,
504,
Citation.Status.PUBLISHED));
}// END: getCitations
private final MutableTreeModel tree;
private final List<SubstitutionModel> substitutionModels;
private final Parameter epochTimes;
public void setRootFrequencyModel(FrequencyModel rootFreqModel) {
this.rootFrequencyModel = rootFreqModel;
}
private FrequencyModel rootFrequencyModel;
}// END: class