Debian Codegraph @

src/dr/evomodel/arg/ARGTree.java

raw symbols SCIP
1 /*
2 * ARGTree.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.arg;
27
28 import dr.evolution.tree.MutableTreeListener;
29 import dr.evolution.tree.NodeRef;
30 import dr.evolution.tree.Tree;
31 import dr.evolution.tree.TreeUtils;
32 import dr.evolution.util.MutableTaxonListListener;
33 import dr.evolution.util.Taxon;
34 import dr.evomodel.arg.ARGModel.Node;
35 import dr.util.Attributable;
36
37 import java.util.*;
38
39 public class ARGTree implements Tree {
40
41 //NodeRef root;
42
43 protected Taxon[] taxaList;
44 protected int taxaCount;
45
46 private final Node initialRoot;
47
48 public ARGModel argModel;
49 // private Map<NodeRef,Integer> mapARGNodesToInts;
50
51 private final Map<NodeRef,NodeRef> mapARGNodesToTreeNodes;
52
53 // public Map<NodeRef,Integer> getMappingInts() { return mapARGNodesToInts; }
54
55 // public Map<ARGModel.Node, NodeRef> getMappingNodes() { return mapARGNodesToTreeNodes; }
56
57 private int partition = -9;
58
59 // /**
60 // * Constructor to represent complete ARG as a tree
61 // *
62 // * @param arg
63 // */
64 // public ARGTree(ARGModel arg) {
65 // this.argModel = arg;
66 // mapNodesARGToTree = new HashMap<Node,Node>(arg.getNodeCount());
67 // root = arg.new Node((Node) arg.getRoot());
68 // }
69
70
71 // /**
72 // * Constructor for specific partition tree
73 // *
74 // * @param arg
75 // * @param partition
76 // */
77
78 // ArrayList<Node> nodeList;
79
80 public boolean wasRootTrimmed() {
81 return (root != initialRoot);
82 }
83
84
85 public String toGraphString() {
86 StringBuffer sb = new StringBuffer();
87 for (Node node : nodes) {
88 sb.append(node.number);
89 if (node.leftParent != null)
90 sb.append(" " + node.leftParent.number);
91 else
92 sb.append(" 0");
93 if (node.rightParent != null)
94 sb.append(" " + node.rightParent.number);
95 else
96 sb.append(" 0");
97 if (node.leftChild != null)
98 sb.append(" " + node.leftChild.number);
99 else
100 sb.append(" 0");
101 if (node.rightChild != null)
102 sb.append(" " + node.rightChild.number);
103 else
104 sb.append(" 0");
105 if (node.taxon != null)
106 sb.append(" " + node.taxon.toString());
107 sb.append("\n");
108 }
109 sb.append("Root = " + ((Node) getRoot()).number + "\n");
110 return new String(sb);
111 }
112
113 public ARGTree(ARGModel arg, int partition) {
114 this.argModel = arg;
115 mapARGNodesToTreeNodes = new HashMap<NodeRef,NodeRef>(arg.getNodeCount());
116
117 this.partition = partition;
118 ARGModel.Node node = arg.new Node(((Node) arg.getRoot()), partition);
119 initialRoot = node;
120
121 int j = arg.externalNodeCount;
122 node.stripOutDeadEnds();
123 root = node.stripOutSingleChildNodes(node);
124 node = root;
125 nodeCount = 2 * j - 1;
126 externalNodeCount = j;
127 internalNodeCount = j - 1;
128 nodes = new Node[nodeCount];
129
130 do {
131 node = (Node) TreeUtils.postorderSuccessor(this, node);
132 if (node.isExternal()) {
133 // keep same order as ARG, so do not need to reload tipStates/Partials
134 nodes[node.number] = node;
135 mapARGNodesToTreeNodes.put(node.mirrorNode,node);
136 } else {
137 // Reorder in new post-order succession
138
139 nodes[j] = node;
140 node.number = j;
141 j++;
142 mapARGNodesToTreeNodes.put(node.mirrorNode, node);
143 }
144 } while (node != root);
145 hasRates = false;
146 }
147
148
149 public Map<NodeRef,NodeRef> getMapping() {
150 return mapARGNodesToTreeNodes;
151 }
152
153 // public Map<NodeRef,Integer> getMapARGNodesToInts() {
154 // // Only need to map internal nodes
155 // Map<NodeRef,Integer> map = new HashMap<NodeRef,Integer>(getInternalNodeCount());
156 // for(int i=0; i<getInternalNodeCount(); i++) {
157 // ARGModel.Node node = (ARGModel.Node) getInternalNode(i);
158 // map.put(node.mirrorNode,node.number);
159 // }
160 // return map;
161 // }
162 public boolean checkForNullRights(Node node) {
163 return node.checkForNullRights();
164 }
165
166 // *****************************************************************
167 // Interface Tree
168 // *****************************************************************
169
170 /**
171 * Return the units that this tree is expressed in.
172 */
173
174 public final Type getUnits() {
175 return units;
176 }
177
178 public void setUnits(Type units) {
179 this.units = units;
180
181 }
182
183 /**
184 * Sets the units that this tree is expressed in.
185 */
186
187
188 /**
189 * @return a count of the number of nodes (internal + external) in this
190 * tree.
191 */
192 public final int getNodeCount() {
193 return nodeCount;
194 }
195
196 public final boolean hasNodeHeights() {
197 return true;
198 }
199
200 public final double getNodeHeight(NodeRef node) {
201
202 //System.err.println(Tree.Utils.uniqueNewick(this, node));
203 //((Node)node))
204
205 return ((Node) node).getHeight();
206 }
207
208 public final double getNodeHeightUpper(NodeRef node) {
209 return ((Node) node).heightParameter.getBounds().getUpperLimit(0);
210 }
211
212 public final double getNodeHeightLower(NodeRef node) {
213 return ((Node) node).heightParameter.getBounds().getLowerLimit(0);
214 }
215
216
217 /**
218 * @param node
219 * @return the rate parameter associated with this node.
220 */
221 public final double getNodeRate(NodeRef node) {
222
223
224 if (!hasRates) {
225 return 1.0;
226 }
227
228 return ((Node) node).getRate(partition);
229 }
230
231 public Object getNodeAttribute(NodeRef node, String name) {
232 throw new UnsupportedOperationException("TreeModel does not use NodeAttributes");
233 }
234
235 public Iterator getNodeAttributeNames(NodeRef node) {
236 throw new UnsupportedOperationException("TreeModel does not use NodeAttributes");
237 }
238
239 public double getNodeTrait(NodeRef node) {
240 if (!hasTraits) throw new IllegalArgumentException("Trait parameters have not been created");
241 return ((Node) node).getTrait();
242 }
243
244 public final Taxon getNodeTaxon(NodeRef node) {
245 return ((Node) node).taxon;
246 }
247
248 public final boolean isExternal(NodeRef node) {
249 return ((Node) node).isExternal();
250 }
251
252 public final boolean isRoot(NodeRef node) {
253 return (node == root);
254 }
255
256 public final int getChildCount(NodeRef node) {
257 //System.err.println("Cn for "+((Node)node).number);
258 return ((Node) node).getChildCount();
259 }
260
261 public final NodeRef getChild(NodeRef node, int i) {
262 return ((Node) node).getChild(i);
263 }
264
265 public final NodeRef getParent(NodeRef node) {
266 //System.err.println("Gimme!");
267 return ((Node) node).leftParent;
268 }
269
270 public final boolean hasBranchLengths() {
271 return true;
272 }
273
274 public final double getBranchLength(NodeRef node) {
275 NodeRef parent = getParent(node);
276 if (parent == null) {
277 return 0.0;
278 }
279
280 return getNodeHeight(parent) - getNodeHeight(node);
281 }
282
283 public final NodeRef getExternalNode(int i) {
284 return nodes[i];
285 }
286
287 public final NodeRef getInternalNode(int i) {
288 return nodes[i + externalNodeCount];
289 }
290
291 public final NodeRef getNode(int i) {
292 return nodes[i];
293 }
294
295 /**
296 * Returns the number of external nodes.
297 */
298 public final int getExternalNodeCount() {
299 return externalNodeCount;
300 }
301
302 /**
303 * Returns the ith internal node.
304 */
305 public final int getInternalNodeCount() {
306 return internalNodeCount;
307 }
308
309 /**
310 * Returns the root node of this tree.
311 */
312 public final NodeRef getRoot() {
313 return root;
314 }
315
316 // **************************************************************
317 // TaxonList IMPLEMENTATION
318 // **************************************************************
319
320 /**
321 * @return a count of the number of taxa in the list.
322 */
323 public int getTaxonCount() {
324 return getExternalNodeCount();
325 }
326
327 /**
328 * @return the ith taxon in the list.
329 */
330 // public Taxon getTaxon(int taxonIndex) {
331 // return ((Node)getExternalNode(taxonIndex)).taxon;
332 // }
333
334 // public Taxon getTaxon(int taxonIndex) {
335 // if( taxonIndex >= taxaCount )
336 // return null;
337 // return taxaList[taxonIndex];
338 // }
339 public Taxon getTaxon(int taxonIndex) {
340 return ((Node) getExternalNode(taxonIndex)).taxon;
341 }
342
343
344 /**
345 * @return the ID of the taxon of the ith external node. If it doesn't have
346 * a taxon, returns the ID of the node itself.
347 */
348 public String getTaxonId(int taxonIndex) {
349 Taxon taxon = getTaxon(taxonIndex);
350 if (taxon != null) {
351 return taxon.getId();
352 } else {
353 return null;
354 }
355 }
356
357 /**
358 * returns the index of the taxon with the given id.
359 */
360 public int getTaxonIndex(String id) {
361 for (int i = 0, n = getTaxonCount(); i < n; i++) {
362 if (getTaxonId(i).equals(id)) return i;
363 }
364 return -1;
365 }
366
367 /**
368 * returns the index of the given taxon.
369 */
370 public int getTaxonIndex(Taxon taxon) {
371 for (int i = 0, n = getTaxonCount(); i < n; i++) {
372 if (getTaxon(i) == taxon) return i;
373 }
374 return -1;
375 }
376
377 public List<Taxon> asList() {
378 List<Taxon> taxa = new ArrayList<Taxon>();
379 for (int i = 0, n = getTaxonCount(); i < n; i++) {
380 taxa.add(getTaxon(i));
381 }
382 return taxa;
383 }
384
385 public Iterator<Taxon> iterator() {
386 return new Iterator<Taxon>() {
387 private int index = -1;
388
389 public boolean hasNext() {
390 return index < getTaxonCount() - 1;
391 }
392
393 public Taxon next() {
394 index ++;
395 return getTaxon(index);
396 }
397
398 public void remove() { /* do nothing */ }
399 };
400 }
401
402 /**
403 * @param taxonIndex the index of the taxon whose attribute is being fetched.
404 * @param name the name of the attribute of interest.
405 * @return an object representing the named attributed for the taxon of the given
406 * external node. If the node doesn't have a taxon then the nodes own attribute
407 * is returned.
408 */
409 public final Object getTaxonAttribute(int taxonIndex, String name) {
410 Taxon taxon = getTaxon(taxonIndex);
411 if (taxon != null) {
412 return taxon.getAttribute(name);
413 }
414 return null;
415 }
416
417 // **************************************************************
418 // MutableTaxonList IMPLEMENTATION
419 // **************************************************************
420
421 public int addTaxon(Taxon taxon) {
422 throw new IllegalArgumentException("Cannot add taxon to a TreeModel");
423 }
424
425 public boolean removeTaxon(Taxon taxon) {
426 throw new IllegalArgumentException("Cannot add taxon to a TreeModel");
427 }
428
429 public void setTaxonId(int taxonIndex, String id) {
430 throw new IllegalArgumentException("Cannot set taxon id in a TreeModel");
431 }
432
433 public void setTaxonAttribute(int taxonIndex, String name, Object value) {
434 throw new IllegalArgumentException("Cannot set taxon attribute in a TreeModel");
435 }
436
437 public void addMutableTreeListener(MutableTreeListener listener) {
438 } // Do nothing at the moment
439
440 public void addMutableTaxonListListener(MutableTaxonListListener listener) {
441 } // Do nothing at the moment
442
443 // **************************************************************
444 // Identifiable IMPLEMENTATION
445 // **************************************************************
446
447 protected String id = null;
448
449 /**
450 * @return the id.
451 */
452 public String getId() {
453 return id;
454 }
455
456 /**
457 * Sets the id.
458 */
459 public void setId(String id) {
460 this.id = id;
461 }
462
463 // **************************************************************
464 // Attributable IMPLEMENTATION
465 // **************************************************************
466
467 private Attributable.AttributeHelper treeAttributes = null;
468
469 /**
470 * Sets an named attribute for this object.
471 *
472 * @param name the name of the attribute.
473 * @param value the new value of the attribute.
474 */
475 public void setAttribute(String name, Object value) {
476 if (treeAttributes == null)
477 treeAttributes = new Attributable.AttributeHelper();
478 treeAttributes.setAttribute(name, value);
479 }
480
481 /**
482 * @param name the name of the attribute of interest.
483 * @return an object representing the named attributed for this object.
484 */
485 public Object getAttribute(String name) {
486 if (treeAttributes == null)
487 return null;
488 else
489 return treeAttributes.getAttribute(name);
490 }
491
492 /**
493 * @return an iterator of the attributes that this object has.
494 */
495 public Iterator<String> getAttributeNames() {
496 if (treeAttributes == null)
497 return null;
498 else
499 return treeAttributes.getAttributeNames();
500 }
501
502 /**
503 * @return a string containing a newick representation of the tree
504 */
505 public final String getNewick() {
506 return TreeUtils.newick(this);
507 }
508
509 public final String getUniqueNewick(){
510 return TreeUtils.uniqueNewick(this,this.getRoot());
511 }
512
513 /**
514 * @return a string containing a newick representation of the tree
515 */
516 public String toString() {
517 return getNewick();
518 }
519
520 public Tree getCopy() {
521 throw new UnsupportedOperationException("please don't call this function");
522 }
523
524
525 // ***********************************************************************
526 // Private members
527 // ***********************************************************************
528
529
530 /**
531 * root node
532 */
533 protected Node root = null;
534 protected int storedRootNumber;
535
536 /**
537 * list of internal nodes (including root)
538 */
539 protected Node[] nodes = null;
540 protected Node[] storedNodes = null;
541
542 /**
543 * number of nodes (including root and tips)
544 */
545 protected int nodeCount;
546
547 /**
548 * number of external nodes
549 */
550 protected int externalNodeCount;
551
552 /**
553 * number of internal nodes (including root)
554 */
555 protected int internalNodeCount;
556
557
558
559
560 /**
561 * holds the units of the trees branches.
562 */
563 // private int units = SUBSTITUTIONS;
564 private Type units;
565
566 protected boolean inEdit = false;
567
568 private final boolean hasRates;
569 private final boolean hasTraits = false;
570
571
572 }