Debian Codegraph @

src/dr/app/treestat/statistics/BetaTreeDiversityStatistic.java

raw symbols SCIP
1 /*
2 * BetaTreeDiversityStatistic.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.app.treestat.statistics;
27
28 import dr.evolution.io.Importer;
29 import dr.evolution.io.NewickImporter;
30 import dr.evolution.tree.NodeRef;
31 import dr.evolution.tree.Tree;
32 import dr.evolution.tree.TreeUtils;
33 import dr.evolution.util.Taxa;
34 import dr.evolution.util.Taxon;
35 import dr.evolution.util.TaxonList;
36
37 import java.io.IOException;
38 import java.util.Set;
39
40 /**
41 * @author Alexei Drummond
42 * @version $Id: BetaTreeDiversityStatistic.java,v 1.3 2006/05/09 10:24:27 rambaut Exp $
43 */
44 public class BetaTreeDiversityStatistic extends AbstractTreeSummaryStatistic {
45
46 private BetaTreeDiversityStatistic() {
47 this.taxonList = null;
48 }
49
50 public void setTaxonList(TaxonList taxonList) {
51 this.taxonList = taxonList;
52 }
53
54 public double[] getSummaryStatistic(Tree tree) {
55 if (taxonList == null) {
56 return new double[]{1.0};
57 }
58
59 double TL = TreeUtils.getTreeLength(tree, tree.getRoot());
60
61 double betaDiversity = (TL - getUniqueBranches(tree, tree.getRoot())) / TL;
62
63 return new double[]{betaDiversity};
64
65 }
66
67 private double getUniqueBranches(Tree tree, NodeRef node) {
68
69 if (tree.isExternal(node)) {
70 return tree.getBranchLength(node);
71 } else {
72 double length = 0;
73 if (isUnique(taxonList, tree, node)) {
74 length = tree.getBranchLength(node);
75 //System.out.println("length = " + length);
76 }
77 for (int i = 0; i < tree.getChildCount(node); i++) {
78 length += getUniqueBranches(tree, tree.getChild(node, i));
79 }
80 //System.out.println("length of node " + node + " = " + length);
81 return length;
82 }
83 }
84
85 private boolean isUnique(TaxonList taxonList, Tree tree, NodeRef node) {
86 Set<String> taxa = TreeUtils.getDescendantLeaves(tree, node);
87 int count = 0;
88 for (String taxon : taxa) {
89 count += (taxonList.getTaxonIndex(taxon) >= 0 ? 1 : 0);
90 //System.out.print(taxon + "\t");
91 }
92 boolean unique = (count == 0) || (count == taxa.size());
93
94 //System.out.println(count + "\t" + unique);
95
96 return unique;
97 }
98
99 public String getSummaryStatisticName() {
100 if (taxonList != null) {
101 return "betaDiversity(" + taxonList.getId() + ")";
102 } else {
103 return "betaDiversity(null)";
104 }
105 }
106
107 public String getSummaryStatisticDescription() {
108 return "The beta diversity of the given taxonList assuming the remaining taxa are from the second location.";
109 }
110
111 public String getSummaryStatisticReference() {
112 return FACTORY.getSummaryStatisticReference();
113 }
114
115 public boolean allowsPolytomies() {
116 return FACTORY.allowsPolytomies();
117 }
118
119 public boolean allowsNonultrametricTrees() {
120 return FACTORY.allowsNonultrametricTrees();
121 }
122
123 public boolean allowsUnrootedTrees() {
124 return FACTORY.allowsUnrootedTrees();
125 }
126
127 public Category getCategory() {
128 return FACTORY.getCategory();
129 }
130
131 public static final Factory FACTORY = new Factory() {
132
133 public TreeSummaryStatistic createStatistic() {
134 return new BetaTreeDiversityStatistic();
135 }
136
137 public String getSummaryStatisticName() {
138 return "Beta Diversity";
139 }
140
141 public String getSummaryStatisticDescription() {
142 return "The ratio of shared diversity to total diversity (tree length) between given taxa and the remainder.";
143 }
144
145 public String getSummaryStatisticReference() {
146 return "-";
147 }
148
149 public boolean allowsPolytomies() {
150 return true;
151 }
152
153 public boolean allowsNonultrametricTrees() {
154 return true;
155 }
156
157 public boolean allowsUnrootedTrees() {
158 return false;
159 }
160
161 public Category getCategory() {
162 return Category.GENERAL;
163 }
164
165 public boolean allowsWholeTree() {
166 return true;
167 }
168
169 public boolean allowsCharacter() {
170 return false;
171 }
172
173 public boolean allowsCharacterState() {
174 return false;
175 }
176
177 public boolean allowsTaxonList() {
178 return true;
179 }
180 };
181
182 private TaxonList taxonList = null;
183
184 public static void main(String[] arg) throws IOException, Importer.ImportException {
185 Tree tree = (new NewickImporter("((A:1,B:1):1,(C:1, D:1):1);")).importNextTree();
186
187 BetaTreeDiversityStatistic statistic = (BetaTreeDiversityStatistic) BetaTreeDiversityStatistic.FACTORY.createStatistic();
188
189 Taxa taxa = new Taxa();
190 taxa.addTaxon(new Taxon("A"));
191 taxa.addTaxon(new Taxon("C"));
192
193 statistic.setTaxonList(taxa);
194
195 System.out.println(statistic.getSummaryStatistic(tree)[0]);
196 }
197 }
198