Debian Codegraph @

src/dr/evomodel/branchmodel/BranchSpecificBranchModel.java

raw symbols SCIP
1 /*
2 * BranchSpecificBranchModel.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.evomodel.branchmodel;
27
28 import dr.evolution.tree.TreeUtils;
29 import dr.evomodel.substmodel.FrequencyModel;
30 import dr.evomodel.substmodel.SubstitutionModel;
31 import dr.evolution.tree.NodeRef;
32 import dr.evolution.tree.Tree;
33 import dr.evolution.util.TaxonList;
34 import dr.evomodel.tree.TreeModel;
35 import dr.evomodelxml.branchratemodel.LocalClockModelParser;
36 import dr.inference.model.AbstractModel;
37 import dr.inference.model.Model;
38 import dr.inference.model.Parameter;
39 import dr.inference.model.Variable;
40
41 import java.util.*;
42
43 /**
44 * A branch model which allows different clades (defined by MRCAs of taxon lists) to have different
45 * substitution models.
46 *
47 * @author Andrew Rambaut
48 * @version $Id$
49 */
50 public class BranchSpecificBranchModel extends AbstractModel implements BranchModel {
51
52 private TreeModel treeModel;
53 protected Map<BitSet, Clade> clades = new HashMap<BitSet, Clade>();
54
55 private boolean updateNodeMaps = true;
56 private Map<NodeRef, Mapping> nodeMap = new HashMap<NodeRef, Mapping>();
57 private Map<NodeRef, Mapping> externalNodeMap = new HashMap<NodeRef, Mapping>();
58
59 private final SubstitutionModel rootSubstitutionModel;
60 private final List<SubstitutionModel> substitutionModels = new ArrayList<SubstitutionModel>();
61
62 private boolean requiresMatrixConvolution = false;
63
64 public BranchSpecificBranchModel(TreeModel treeModel, SubstitutionModel rootSubstitutionModel) {
65
66 super(LocalClockModelParser.LOCAL_CLOCK_MODEL);
67 this.treeModel = treeModel;
68
69 addModel(treeModel);
70
71 this.rootSubstitutionModel = rootSubstitutionModel;
72 addModel(rootSubstitutionModel);
73
74 substitutionModels.add(rootSubstitutionModel);
75 }
76
77 /**
78 * Adds a substitution model specific to a clade.
79 * @param taxonList a list of taxa who's MRCA define the clade
80 * @param substitutionModel the substitution model
81 * @param stemWeight the proportion of the stem branch to include in this model (0, 1)
82 * @throws TreeUtils.MissingTaxonException
83 */
84 public void addClade(TaxonList taxonList, SubstitutionModel substitutionModel, double stemWeight) throws TreeUtils.MissingTaxonException {
85 int index = substitutionModels.indexOf(substitutionModel);
86 if (index == -1) {
87 index = substitutionModels.size();
88 substitutionModels.add(substitutionModel);
89 addModel(substitutionModel);
90 }
91
92 BitSet tips = TreeUtils.getTipsBitSetForTaxa(treeModel, taxonList);
93 Clade clade = new Clade(index, tips, stemWeight);
94 clades.put(tips, clade);
95
96 if (stemWeight > 0.0 || stemWeight < 1.0) {
97 requiresMatrixConvolution = true;
98 }
99 }
100
101 public void addExternalBranches(TaxonList taxonList, SubstitutionModel substitutionModel) throws TreeUtils.MissingTaxonException {
102 int x = substitutionModels.indexOf(substitutionModel);
103 if (x == -1) {
104 x = substitutionModels.size();
105 substitutionModels.add(substitutionModel);
106 addModel(substitutionModel);
107 }
108 final int index = x;
109
110 for (int i = 0; i < treeModel.getExternalNodeCount(); i++) {
111 NodeRef node = treeModel.getExternalNode(i);
112
113 externalNodeMap.put(node, new Mapping() {
114 // @Override
115 public int[] getOrder() {
116 return new int[]{index};
117 }
118
119 // @Override
120 public double[] getWeights() {
121 return new double[]{1.0};
122 }
123 });
124 }
125 }
126
127 public void addBackbone(TaxonList taxonList, SubstitutionModel substitutionModel) throws TreeUtils.MissingTaxonException {
128 throw new UnsupportedOperationException("Not implemented yet");
129 }
130
131 @Override
132 public Mapping getBranchModelMapping(NodeRef branch) {
133 if (updateNodeMaps) {
134 setupNodeMaps();
135 }
136 Mapping mapping = externalNodeMap.get(branch);
137 if (mapping != null) {
138 return mapping;
139 }
140 mapping = nodeMap.get(branch);
141 if (mapping != null) {
142 return mapping;
143 }
144 return BranchModel.DEFAULT;
145 }
146
147 // @Override
148 public List<SubstitutionModel> getSubstitutionModels() {
149 return substitutionModels;
150 }
151
152 // @Override
153 public SubstitutionModel getRootSubstitutionModel() {
154 return rootSubstitutionModel;
155 }
156
157 public FrequencyModel getRootFrequencyModel() {
158 return getRootSubstitutionModel().getFrequencyModel();
159 }
160
161 // @Override
162 public boolean requiresMatrixConvolution() {
163 return requiresMatrixConvolution;
164 }
165
166 public void handleModelChangedEvent(Model model, Object object, int index) {
167 if (model == treeModel && clades.size() > 0) {
168 updateNodeMaps = true;
169 }
170 fireModelChanged();
171 }
172
173 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
174 }
175
176 protected void storeState() {
177 }
178
179 protected void restoreState() {
180 if (clades.size() > 0) {
181 updateNodeMaps = true;
182 }
183 }
184
185 protected void acceptState() {
186 }
187
188
189 private void setupNodeMaps() {
190 if (clades.size() > 0) {
191 setupNodeMaps(treeModel, treeModel.getRoot(), new BitSet());
192 }
193 updateNodeMaps = false;
194 }
195
196 private void setupNodeMaps(Tree tree, NodeRef node, BitSet tips) {
197 Clade clade;
198
199 if (tree.isExternal(node)) {
200 tips.set(node.getNumber());
201 clade = null;
202 } else {
203 for (int i = 0; i < tree.getChildCount(node); i++) {
204 NodeRef child = tree.getChild(node, i);
205 BitSet childTips = new BitSet();
206 setupNodeMaps(tree, child, childTips);
207
208 tips.or(childTips);
209 }
210 clade = clades.get(tips);
211 }
212
213 if (clade != null) {
214 for (int i = 0; i < tree.getChildCount(node); i++) {
215 NodeRef child = tree.getChild(node, i);
216 setNodeMap(tree, child, clade);
217 }
218
219 final double weight = clade.getStemWeight();
220
221 if (weight > 0.0) {
222 final int ancestralIndex;
223 final int index = clade.getIndex();
224
225 Mapping ancestoralMapping = nodeMap.get(node);
226
227 if (ancestoralMapping != null) {
228 ancestralIndex = ancestoralMapping.getOrder()[0];
229 } else {
230 ancestralIndex = 0;
231 }
232
233 nodeMap.put(node, new Mapping() {
234 // @Override
235 public int[] getOrder() {
236 return new int[] { index , ancestralIndex };
237 }
238
239 // @Override
240 public double[] getWeights() {
241 return new double[] { weight, 1.0 - weight };
242 }
243 });
244
245 }
246 }
247 }
248
249 private void setNodeMap(Tree tree, NodeRef node, final Clade clade) {
250
251 if (!tree.isExternal(node)) {
252 for (int i = 0; i < tree.getChildCount(node); i++) {
253 NodeRef child = tree.getChild(node, i);
254 setNodeMap(tree, child, clade);
255 }
256 }
257
258 nodeMap.put(node, new Mapping() {
259 // @Override
260 public int[] getOrder() {
261 return new int[] { clade.getIndex() };
262 }
263
264 // @Override
265 public double[] getWeights() {
266 return new double[] { 1.0 };
267 }
268 });
269 }
270
271 private class Clade {
272
273 Clade(int index, BitSet tips, double stemWeight) {
274 this.index = index;
275 this.tips = tips;
276 this.stemWeight = stemWeight;
277 }
278
279 public int getIndex() {
280 return index;
281 }
282
283 public BitSet getTips() {
284 return tips;
285 }
286
287 public double getStemWeight() {
288 return stemWeight;
289 }
290
291 private final int index;
292 private final BitSet tips;
293 private final double stemWeight;
294 }
295
296 }