| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 38 |
|
| 39 |
|
| 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 |
|
| 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 |
} |