Debian Codegraph @

src/dr/evolution/tree/Clade.java

raw symbols SCIP
1 /*
2 * Clade.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 /**
27 *
28 */
29 package dr.evolution.tree;
30
31 import java.io.BufferedWriter;
32 import java.io.File;
33 import java.io.FileWriter;
34 import java.io.IOException;
35 import java.util.*;
36
37 import dr.math.MathUtils;
38 import dr.math.distributions.Distribution;
39 import dr.math.distributions.NormalDistribution;
40
41 /**
42 * @author Sebastian Hoehna
43 *
44 */
45 public class Clade implements Comparable<Clade> {
46
47 private final BitSet bits;
48 private double height;
49 private List<Double> heights;
50 private final int size;
51
52 public Clade(final BitSet bits, final double height) {
53 this.bits = bits;
54 this.height = height;
55 size = bits.cardinality();
56 heights = new ArrayList<Double>();
57 }
58
59 public BitSet getBits() {
60 return bits;
61 }
62
63 public double getHeight() {
64 return height;
65 }
66
67 public double getHeight(int index) {
68 return heights.get(index);
69 }
70
71 public int getSampleCount(){
72 return heights.size();
73 }
74
75 public int getSize() {
76 return size;
77 }
78
79 public int compareTo(Clade clade) {
80 int setBitIndexI = -1;
81 int setBitIndexJ = -1;
82 BitSet otherBits = clade.getBits();
83 do {
84 setBitIndexI = bits.nextSetBit(setBitIndexI + 1);
85 setBitIndexJ = otherBits.nextSetBit(setBitIndexJ + 1);
86 } while (setBitIndexI == setBitIndexJ && setBitIndexI != -1);
87
88 return (setBitIndexI < setBitIndexJ ? -1
89 : (setBitIndexI > setBitIndexJ ? 1 : 0));
90 }
91
92 public boolean equals(Object o){
93 if (o instanceof Clade){
94 return equals((Clade)o);
95 }
96 return false;
97 }
98
99 public boolean equals(Clade c){
100 if (size != c.getSize()){
101 return false;
102 }
103 return compareTo(c) == 0;
104 }
105
106 public String toString(){
107 return bits.toString();
108 }
109
110 public int hashCode(){
111 return bits.hashCode();
112 }
113
114 public void addHeight(double height){
115 heights.add(height);
116 setHeight(height);
117 }
118
119 public void setHeight(double height){
120 this.height = height;
121 }
122
123 public static Set<Clade> getCladeSet(Tree tree) {
124 return new HashSet<Clade>(getCladeList(tree));
125 }
126
127 public static List<Clade> getCladeList(Tree tree) {
128 List<Clade> clades = new ArrayList<Clade>();
129 getClades(clades, tree, tree.getRoot());
130 return clades;
131 }
132
133 private static BitSet getClades(List<Clade> clades, Tree tree, NodeRef node) {
134
135 BitSet bits = new BitSet();
136
137 if (tree.isExternal(node)) {
138
139 bits.set(node.getNumber());
140
141 } else {
142 bits.or(getClades(clades, tree, tree.getChild(node, 0)));
143 bits.or(getClades(clades, tree, tree.getChild(node, 1)));
144
145 clades.add(new Clade(bits, tree.getNodeHeight(node)));
146 }
147
148 return bits;
149 }
150
151 }