| 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.TaxonList; |
| 32 |
|
| 33 |
import java.util.Set; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 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 |
} |