Debian Codegraph @

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

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