Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * CladeMeanAttributeStatistic.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.tree.NodeRef;
29 import dr.evolution.tree.Tree;
30 import dr.evolution.tree.TreeUtils;
31 import dr.evolution.util.*;
32
33 import java.util.*;
34
35 /**
36 *
37 * @version $Id: TMRCASummaryStatistic.java,v 1.3 2006/05/09 10:24:27 rambaut Exp $
38 *
39 * @author Alexei Drummond
40 */
41 public class CladeMeanAttributeStatistic extends AbstractTreeSummaryStatistic {
42
43 private CladeMeanAttributeStatistic() {
44 this.attributeName = "";
45 }
46
47 public void setTaxonList(TaxonList taxonList) {
48 this.taxonList = taxonList;
49 }
50
51 public void setString(String value) {
52 this.attributeName = value;
53 }
54
55
56 public double[] getSummaryStatistic(Tree tree) {
57 NodeRef node;
58 if (taxonList == null) {
59 node = tree.getRoot();
60 } else {
61 try {
62 Set<String> leafSet = TreeUtils.getLeavesForTaxa(tree, taxonList);
63 node = TreeUtils.getCommonAncestorNode(tree, leafSet);
64 if (node == null) throw new RuntimeException("No clade found that contains " + leafSet);
65 } catch (TreeUtils.MissingTaxonException e) {
66 throw new RuntimeException("Missing taxon!");
67 }
68 }
69
70 sumAttribute = 0.0;
71 nodeCount = 0;
72 getAttribute(tree, node);
73 return new double[] { sumAttribute / nodeCount };
74 }
75
76 private double sumAttribute;
77 private int nodeCount;
78
79 private void getAttribute(Tree tree, NodeRef node) {
80 if (!tree.isExternal(node)) {
81 for (int i = 0; i < tree.getChildCount(node); i++) {
82 getAttribute(tree, tree.getChild(node, i));
83 }
84 }
85
86 Object item = tree.getNodeAttribute(node, attributeName);
87 if (item != null && item instanceof Number) {
88 sumAttribute += ((Number)item).doubleValue();
89 nodeCount ++;
90 } else {
91 // just ignore it
92 }
93 }
94
95 public String getSummaryStatisticName() {
96 if (taxonList != null) {
97 return attributeName + "(" + taxonList.getId() + ")";
98 } else {
99 return attributeName + "(root)";
100 }
101 }
102
103 public String getSummaryStatisticDescription() {
104 if (taxonList != null) {
105 return "Gets the mean of an attribute for a clade defined by a taxon set";
106 }
107 return "Gets the mean of an attribute across the tree.";
108 }
109
110 public String getSummaryStatisticReference() { return FACTORY.getSummaryStatisticReference(); }
111 public boolean allowsPolytomies() { return FACTORY.allowsPolytomies(); }
112 public boolean allowsNonultrametricTrees() { return FACTORY.allowsNonultrametricTrees(); }
113 public boolean allowsUnrootedTrees() { return FACTORY.allowsUnrootedTrees(); }
114 public Category getCategory() { return FACTORY.getCategory(); }
115
116 public static final Factory FACTORY = new Factory() {
117
118 public TreeSummaryStatistic createStatistic() {
119 return new CladeMeanAttributeStatistic();
120 }
121
122 public String getSummaryStatisticName() {
123 return "Mean Clade Attribute";
124 }
125
126 public String getSummaryStatisticDescription() {
127 return "Extracts the mean of a named attribute for a clade defined by a taxon set";
128 }
129
130 public String getSummaryStatisticReference() {
131 return "-";
132 }
133
134 public String getValueName() { return "The attribute name:"; }
135
136 public boolean allowsPolytomies() { return true; }
137
138 public boolean allowsNonultrametricTrees() { return true; }
139
140 public boolean allowsUnrootedTrees() { return false; }
141
142 public Category getCategory() { return Category.GENERAL; }
143
144 public boolean allowsWholeTree() { return true; }
145
146 public boolean allowsTaxonList() { return true; }
147
148 public boolean allowsString() { return true; }
149 };
150
151 private String attributeName = null;
152 private TaxonList taxonList = null;
153 }