Debian Codegraph @

src/dr/evomodel/tree/EmpiricalTreeDistributionModel.java

raw symbols SCIP
1 /*
2 * EmpiricalTreeDistributionModel.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.tree;
27
28 import dr.evolution.io.Importer;
29 import dr.evolution.io.TreeImporter;
30 import dr.evolution.tree.NodeRef;
31 import dr.evolution.tree.Tree;
32 import dr.evolution.util.Taxon;
33 import dr.math.MathUtils;
34 import dr.inference.model.Statistic;
35
36 import java.io.IOException;
37 import java.util.Iterator;
38 import java.util.List;
39
40 /**
41 * @author Andrew Rambaut
42 * @version $Id$
43 */
44 public class EmpiricalTreeDistributionModel extends TreeModel {
45
46 /**
47 * This constructor takes an array of trees and jumps randomly amongst them.
48 * @param trees
49 * @param startingTree
50 */
51 public EmpiricalTreeDistributionModel(final Tree[] trees, int startingTree) {
52 this(trees, null, startingTree);
53 }
54
55 /**
56 * This constructor takes a TreeImporter and reads the trees one by one to the end (and
57 * then starts throwing exceptions).
58 * @param importer
59 * @param startingTree
60 */
61 public EmpiricalTreeDistributionModel(final TreeImporter importer, int startingTree) {
62 this(null, importer, startingTree);
63 }
64
65 private EmpiricalTreeDistributionModel(final Tree[] trees, final TreeImporter importer, int startingTree) {
66 super(EMPIRICAL_TREE_DISTRIBUTION_MODEL);
67
68 this.trees = trees;
69 this.importer = importer;
70 drawTreeIndex(startingTree);
71
72 addStatistic(new Statistic.Abstract("Current Tree") {
73
74 public int getDimension() {
75 return 1;
76 }
77
78 public double getStatisticValue(int dim) {
79 return currentTreeIndex;
80 }
81 });
82 }
83
84 protected void storeState() {
85 storedCurrentTree = currentTree;
86 }
87
88 protected void restoreState() {
89 currentTree = storedCurrentTree;
90 }
91
92 protected void acceptState() {
93 }
94
95 public void drawTreeIndex() {
96 drawTreeIndex(-1);
97 }
98
99 private void drawTreeIndex(int treeNumber) {
100 // System.err.print("Drawing new tree, (old tree = " + currentTreeIndex);
101
102 if (importer != null) {
103 try {
104 if (importer.hasTree() == false) {
105 throw new RuntimeException("EmpiricalTreeDistributionModel has run out of trees");
106 }
107 currentTree = importer.importNextTree();
108 } catch (IOException e) {
109 throw new RuntimeException("EmpiricalTreeDistributionModel unable to load next tree");
110 } catch (Importer.ImportException e) {
111 throw new RuntimeException("EmpiricalTreeDistributionModel unable to load next tree");
112 }
113 currentTreeIndex += 1;
114 } else {
115 if (treeNumber == -1) {
116 currentTreeIndex = MathUtils.nextInt(trees.length);
117 currentTree = trees[currentTreeIndex];
118 } else {
119 currentTreeIndex = treeNumber;
120 currentTree = trees[currentTreeIndex];
121 }
122 }
123
124 // Force computation of node heights now rather than later in the evaluation
125 // where multithreading may get conflicts.
126 currentTree.getNodeHeight(currentTree.getRoot());
127
128 // System.err.println(") new tree = " + currentTreeIndex);
129
130 fireModelChanged(new TreeChangedEvent());
131 }
132
133 public NodeRef getRoot() {
134 return currentTree.getRoot();
135 }
136
137 public int getNodeCount() {
138 return currentTree.getNodeCount();
139 }
140
141 public NodeRef getNode(final int i) {
142 return currentTree.getNode(i);
143 }
144
145 public NodeRef getInternalNode(final int i) {
146 return currentTree.getInternalNode(i);
147 }
148
149 public NodeRef getExternalNode(final int i) {
150 return currentTree.getExternalNode(i);
151 }
152
153 public int getExternalNodeCount() {
154 return currentTree.getExternalNodeCount();
155 }
156
157 public int getInternalNodeCount() {
158 return currentTree.getInternalNodeCount();
159 }
160
161 public Taxon getNodeTaxon(final NodeRef node) {
162 return trees[currentTreeIndex].getNodeTaxon(node);
163 }
164
165 public boolean hasNodeHeights() {
166 return currentTree.hasNodeHeights();
167 }
168
169 public double getNodeHeight(final NodeRef node) {
170 return currentTree.getNodeHeight(node);
171 }
172
173 public boolean hasBranchLengths() {
174 return currentTree.hasBranchLengths();
175 }
176
177 public double getBranchLength(final NodeRef node) {
178 return currentTree.getBranchLength(node);
179 }
180
181 public double getNodeRate(final NodeRef node) {
182 return currentTree.getNodeRate(node);
183 }
184
185 public Object getNodeAttribute(final NodeRef node, final String name) {
186 return currentTree.getNodeAttribute(node, name);
187 }
188
189 public Iterator getNodeAttributeNames(final NodeRef node) {
190 return currentTree.getNodeAttributeNames(node);
191 }
192
193 public boolean isExternal(final NodeRef node) {
194 return currentTree.isExternal(node);
195 }
196
197 public boolean isRoot(final NodeRef node) {
198 return currentTree.isRoot(node);
199 }
200
201 public int getChildCount(final NodeRef node) {
202 return currentTree.getChildCount(node);
203 }
204
205 public NodeRef getChild(final NodeRef node, final int j) {
206 return currentTree.getChild(node, j);
207 }
208
209 public NodeRef getParent(final NodeRef node) {
210 return currentTree.getParent(node);
211 }
212
213 public Tree getCopy() {
214 return currentTree.getCopy();
215 }
216
217 public int getTaxonCount() {
218 return currentTree.getTaxonCount();
219 }
220
221 public Taxon getTaxon(final int taxonIndex) {
222 return currentTree.getTaxon(taxonIndex);
223 }
224
225 public String getTaxonId(final int taxonIndex) {
226 return currentTree.getTaxonId(taxonIndex);
227 }
228
229 public int getTaxonIndex(final String id) {
230 return currentTree.getTaxonIndex(id);
231 }
232
233 public int getTaxonIndex(final Taxon taxon) {
234 return currentTree.getTaxonIndex(taxon);
235 }
236
237 public List<Taxon> asList() {
238 return currentTree.asList();
239 }
240
241 public Object getTaxonAttribute(final int taxonIndex, final String name) {
242 return currentTree.getTaxonAttribute(taxonIndex, name);
243 }
244
245 public Iterator<Taxon> iterator() {
246 return currentTree.iterator();
247 }
248
249 public Type getUnits() {
250 return currentTree.getUnits();
251 }
252
253 public void setUnits(final Type units) {
254 currentTree.setUnits(units);
255 }
256
257 public void setAttribute(final String name, final Object value) {
258 currentTree.setAttribute(name, value);
259 }
260
261 public Object getAttribute(final String name) {
262 return currentTree.getAttribute(name);
263 }
264
265 public Iterator<String> getAttributeNames() {
266 return currentTree.getAttributeNames();
267 }
268
269 public static final String EMPIRICAL_TREE_DISTRIBUTION_MODEL = "empiricalTreeDistributionModel";
270
271 private final Tree[] trees;
272 private final TreeImporter importer;
273 private Tree currentTree;
274 private Tree storedCurrentTree;
275
276 private int currentTreeIndex;
277 }