| 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.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 |
|
| 42 |
|
| 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 |
|
| 76 |
} |
| 77 |
for (int i = 0; i < tree.getChildCount(node); i++) { |
| 78 |
length += getUniqueBranches(tree, tree.getChild(node, i)); |
| 79 |
} |
| 80 |
|
| 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 |
|
| 91 |
} |
| 92 |
boolean unique = (count == 0) || (count == taxa.size()); |
| 93 |
|
| 94 |
|
| 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 |
|