| 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.evomodel.continuous; |
| 27 |
|
| 28 |
import dr.evolution.tree.MutableTreeModel; |
| 29 |
import dr.evolution.tree.NodeRef; |
| 30 |
import dr.evolution.tree.Tree; |
| 31 |
import dr.evomodel.tree.TreeModel; |
| 32 |
import dr.evomodel.tree.TreeStatistic; |
| 33 |
import dr.geo.math.SphericalPolarCoordinates; |
| 34 |
import dr.inference.model.Statistic; |
| 35 |
import dr.stats.DiscreteStatistics; |
| 36 |
import dr.xml.*; |
| 37 |
|
| 38 |
import java.util.ArrayList; |
| 39 |
import java.util.List; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public class DiffusionRateCovarianceStatistic extends Statistic.Abstract { |
| 47 |
|
| 48 |
public static final String DIFFUSION_RATE_COVARIANCE_STATISTIC = "diffusionRateCovarianceStatistic"; |
| 49 |
public static final String TREE_DISPERSION_COVARIANCE_STATISTIC = "treeDispersionCovarianceStatistic"; |
| 50 |
public static final String BOOLEAN_DIS_OPTION = "greatCircleDistance"; |
| 51 |
public static final String BOOLEAN_DC_OPTION = "diffusionCoefficient"; |
| 52 |
|
| 53 |
public DiffusionRateCovarianceStatistic(String name, TreeModel tree, List<AbstractMultivariateTraitLikelihood> traitLikelihoods, |
| 54 |
boolean option, boolean diffusionCoefficient) { |
| 55 |
super(name); |
| 56 |
this.traitLikelihoods = traitLikelihoods; |
| 57 |
useGreatCircleDistances = option; |
| 58 |
this.diffusionCoefficient = diffusionCoefficient; |
| 59 |
int n = tree.getExternalNodeCount(); |
| 60 |
childRate = new double[2 * n - 4]; |
| 61 |
parentRate = new double[childRate.length]; |
| 62 |
} |
| 63 |
|
| 64 |
public int getDimension() { |
| 65 |
return 1; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public double getStatisticValue(int dim) { |
| 72 |
|
| 73 |
String traitName = traitLikelihoods.get(0).getTraitName(); |
| 74 |
|
| 75 |
for (AbstractMultivariateTraitLikelihood traitLikelihood : traitLikelihoods) { |
| 76 |
MutableTreeModel tree = traitLikelihood.getTreeModel(); |
| 77 |
|
| 78 |
int counter = 0; |
| 79 |
int index = 0; |
| 80 |
|
| 81 |
for (int i = 0; i < tree.getNodeCount(); i++) { |
| 82 |
|
| 83 |
NodeRef child = tree.getNode(i); |
| 84 |
NodeRef parent = tree.getParent(child); |
| 85 |
if (parent != null & !tree.isRoot(parent)) { |
| 86 |
double[] childTrait = traitLikelihood.getTraitForNode(tree, child, traitName); |
| 87 |
double[] parentTrait = traitLikelihood.getTraitForNode(tree, parent, traitName); |
| 88 |
double childTime = tree.getBranchLength(child); |
| 89 |
double parentTime = tree.getBranchLength(parent); |
| 90 |
|
| 91 |
NodeRef grandParent = tree.getParent(parent); |
| 92 |
double[] grandParentTrait = traitLikelihood.getTraitForNode(tree, grandParent, traitName); |
| 93 |
|
| 94 |
if (useGreatCircleDistances && (childTrait.length == 2)) { |
| 95 |
SphericalPolarCoordinates coord1 = new SphericalPolarCoordinates(childTrait[0], childTrait[1]); |
| 96 |
SphericalPolarCoordinates coord2 = new SphericalPolarCoordinates(parentTrait[0], parentTrait[1]); |
| 97 |
double childDistance = coord1.distance(coord2); |
| 98 |
SphericalPolarCoordinates coord3 = new SphericalPolarCoordinates(grandParentTrait[0], grandParentTrait[1]); |
| 99 |
double parentDistance = coord2.distance(coord3); |
| 100 |
|
| 101 |
if (!diffusionCoefficient){ |
| 102 |
childRate[index] = childDistance/childTime; |
| 103 |
parentRate[index] = parentDistance/parentTime; |
| 104 |
} else { |
| 105 |
childRate[index] = Math.pow(childDistance,2)/(4*childTime); |
| 106 |
parentRate[index] = Math.pow(parentDistance,2)/(4*parentTime); |
| 107 |
} |
| 108 |
|
| 109 |
} else { |
| 110 |
double childDistance = getNativeDistance(childTrait, parentTrait); |
| 111 |
double parentDistance = getNativeDistance(parentTrait, grandParentTrait); |
| 112 |
|
| 113 |
if (!diffusionCoefficient){ |
| 114 |
childRate[index] = childDistance/childTime; |
| 115 |
parentRate[index] = parentDistance/parentTime; |
| 116 |
} else { |
| 117 |
childRate[index] = Math.pow(childDistance,2)/(4*childTime); |
| 118 |
parentRate[index] = Math.pow(parentDistance,2)/(4*parentTime); |
| 119 |
} |
| 120 |
|
| 121 |
} |
| 122 |
index += 1; |
| 123 |
} |
| 124 |
|
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
return DiscreteStatistics.covariance(childRate, parentRate); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
private double getNativeDistance(double[] location1, double[] location2) { |
| 136 |
int traitDimension = location1.length; |
| 137 |
double sum = 0; |
| 138 |
for (int i = 0; i < traitDimension; i++) { |
| 139 |
sum += Math.pow((location2[i] - location1[i]),2); |
| 140 |
} |
| 141 |
return Math.sqrt(sum); |
| 142 |
} |
| 143 |
|
| 144 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 145 |
|
| 146 |
public String getParserName() { |
| 147 |
return DIFFUSION_RATE_COVARIANCE_STATISTIC; |
| 148 |
} |
| 149 |
|
| 150 |
@Override |
| 151 |
public String[] getParserNames() { |
| 152 |
return new String[]{getParserName(), TREE_DISPERSION_COVARIANCE_STATISTIC}; |
| 153 |
} |
| 154 |
|
| 155 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 156 |
|
| 157 |
String name = xo.getAttribute(NAME, xo.getId()); |
| 158 |
TreeModel tree = (TreeModel) xo.getChild(Tree.class); |
| 159 |
|
| 160 |
boolean option = xo.getAttribute(BOOLEAN_DIS_OPTION, false); |
| 161 |
|
| 162 |
boolean diffCoeff = xo.getAttribute(BOOLEAN_DC_OPTION, false); |
| 163 |
|
| 164 |
List<AbstractMultivariateTraitLikelihood> traitLikelihoods = new ArrayList<AbstractMultivariateTraitLikelihood>(); |
| 165 |
|
| 166 |
for (int i = 0; i < xo.getChildCount(); i++) { |
| 167 |
if (xo.getChild(i) instanceof AbstractMultivariateTraitLikelihood) { |
| 168 |
traitLikelihoods.add((AbstractMultivariateTraitLikelihood) xo.getChild(i)); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
return new DiffusionRateCovarianceStatistic(name, tree, traitLikelihoods, option, diffCoeff); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public String getParserDescription() { |
| 180 |
return "A statistic that returns the average of the branch rates"; |
| 181 |
} |
| 182 |
|
| 183 |
public Class getReturnType() { |
| 184 |
return TreeStatistic.class; |
| 185 |
} |
| 186 |
|
| 187 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 188 |
return rules; |
| 189 |
} |
| 190 |
|
| 191 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 192 |
AttributeRule.newStringRule(NAME, true), |
| 193 |
AttributeRule.newBooleanRule(BOOLEAN_DIS_OPTION, true), |
| 194 |
AttributeRule.newBooleanRule(BOOLEAN_DC_OPTION, true), |
| 195 |
new ElementRule(MutableTreeModel.class), |
| 196 |
new ElementRule(AbstractMultivariateTraitLikelihood.class, 1, Integer.MAX_VALUE), |
| 197 |
}; |
| 198 |
}; |
| 199 |
|
| 200 |
private boolean useGreatCircleDistances; |
| 201 |
private List<AbstractMultivariateTraitLikelihood> traitLikelihoods; |
| 202 |
private double[] childRate = null; |
| 203 |
private double[] parentRate = null; |
| 204 |
private boolean diffusionCoefficient; |
| 205 |
} |