Debian Codegraph @

src/dr/evolution/tree/CladeMetric.java

raw symbols SCIP
1 /*
2 * CladeMetric.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.evolution.tree;
27
28 import jebl.evolution.graphs.Node;
29 import jebl.evolution.taxa.Taxon;
30 import jebl.evolution.treemetrics.RootedTreeMetric;
31 import jebl.evolution.trees.RootedTree;
32
33 import java.util.*;
34
35 /**
36 * @author Sebastian Hehna
37 * @version 1.0
38 *
39 */
40
41 public class CladeMetric implements RootedTreeMetric {
42 public CladeMetric() {
43 taxonMap = null;
44 }
45
46 public CladeMetric(List<Taxon> taxa) {
47 taxonMap = new HashMap<Taxon, Integer>();
48 for (int i = 0; i < taxa.size(); i++) {
49 taxonMap.put(taxa.get(i), i);
50 }
51 }
52
53 public double getMetric(RootedTree tree1, RootedTree tree2) {
54 // tree1 is the clade for which we are looking
55 // tree2 is the current tree
56
57 Map<Taxon, Integer> tm = taxonMap;
58
59 if (tm == null) {
60 List<Taxon> taxa = new ArrayList<Taxon>(tree2.getTaxa());
61
62 if (!tree2.getTaxa().equals(taxa))
63 tm = new HashMap<Taxon, Integer>();
64 for (int i = 0; i < taxa.size(); i++) {
65 tm.put(taxa.get(i), i);
66 }
67 }
68
69 Clade reference = getClade(tm, tree1, tree1.getRootNode(), null);
70
71 List<Clade> clades = new ArrayList<Clade>();
72 getClades(tm, tree2, tree2.getRootNode(), clades, null);
73
74 if (contains(clades, reference)){
75 return 1.0;
76 }
77 else {
78 return 0.0;
79 }
80 }
81
82 private void getClades(Map<Taxon, Integer> taxonMap, RootedTree tree,
83 Node node, List<Clade> clades, BitSet bits) {
84
85 BitSet bits2 = new BitSet();
86
87 if (tree.isExternal(node)) {
88
89 int index = taxonMap.get(tree.getTaxon(node));
90 bits2.set(index);
91
92 } else {
93
94 for (Node child : tree.getChildren(node)) {
95 getClades(taxonMap, tree, child, clades, bits2);
96 }
97 clades.add(new Clade(bits2, tree.getHeight(node)));
98 }
99
100 if (bits != null) {
101 bits.or(bits2);
102 }
103 }
104
105 private Clade getClade(Map<Taxon, Integer> taxonMap, RootedTree tree,
106 Node node, BitSet bits) {
107
108 BitSet bits2 = new BitSet();
109 Clade c = null;
110
111 if (tree.isExternal(node)) {
112
113 int index = taxonMap.get(tree.getTaxon(node));
114 bits2.set(index);
115
116 } else {
117
118 for (Node child : tree.getChildren(node)) {
119 getClade(taxonMap, tree, child, bits2);
120 }
121 c = new Clade(bits2, tree.getHeight(node));
122 }
123
124 if (bits != null) {
125 bits.or(bits2);
126 }
127
128 return c;
129
130 }
131
132 private boolean contains (List<Clade> list, Clade c){
133 for (Clade cl : list){
134 if (c.equals(cl)){
135 return true;
136 }
137 }
138 return false;
139 }
140
141 BitSet tmpBits = new BitSet();
142
143 private final Map<Taxon, Integer> taxonMap;
144 }