| 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.branchratemodel; |
| 27 |
|
| 28 |
import dr.evolution.tree.NodeRef; |
| 29 |
import dr.evolution.tree.Tree; |
| 30 |
import dr.evomodel.tree.TreeModel; |
| 31 |
import dr.inference.model.Model; |
| 32 |
import dr.inference.model.Parameter; |
| 33 |
import dr.inference.model.Variable; |
| 34 |
import dr.xml.*; |
| 35 |
|
| 36 |
import java.util.logging.Logger; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class DecayingRateModel extends AbstractBranchRateModel { |
| 43 |
|
| 44 |
public static final String DECAYING_RATE_MODEL = "decayingRateModel"; |
| 45 |
public static final String MUTATION_RATE = "mutationRate"; |
| 46 |
public static final String HALF_LIFE = "halfLife"; |
| 47 |
public static final String SUBSTITUTION_RATE = "substitutionRate"; |
| 48 |
public static final String PROPORTION = "proportion"; |
| 49 |
public static final String AVERAGE = "average"; |
| 50 |
|
| 51 |
private final Parameter mutationRateParameter; |
| 52 |
private final Parameter substitutionRateParameter; |
| 53 |
private final Parameter proportionParameter; |
| 54 |
private final Parameter halfLifeParameter; |
| 55 |
|
| 56 |
private final boolean useAveraging; |
| 57 |
|
| 58 |
private final double[] rates; |
| 59 |
private boolean ratesCalculated = false; |
| 60 |
|
| 61 |
public DecayingRateModel(TreeModel treeModel, Parameter mutationRateParameter, Parameter substitutionRateParameter, |
| 62 |
Parameter proportionParameter, Parameter halfLifeParameter, boolean useAveraging) { |
| 63 |
|
| 64 |
super(DECAYING_RATE_MODEL); |
| 65 |
|
| 66 |
rates = new double[treeModel.getNodeCount()]; |
| 67 |
|
| 68 |
addModel(treeModel); |
| 69 |
|
| 70 |
this.mutationRateParameter = mutationRateParameter; |
| 71 |
this.substitutionRateParameter = substitutionRateParameter; |
| 72 |
this.proportionParameter = proportionParameter; |
| 73 |
this.halfLifeParameter = halfLifeParameter; |
| 74 |
|
| 75 |
addVariable(mutationRateParameter); |
| 76 |
if (proportionParameter != null) { |
| 77 |
addVariable(proportionParameter); |
| 78 |
} else { |
| 79 |
addVariable(substitutionRateParameter); |
| 80 |
} |
| 81 |
addVariable(halfLifeParameter); |
| 82 |
|
| 83 |
this.useAveraging = useAveraging; |
| 84 |
} |
| 85 |
|
| 86 |
public void handleModelChangedEvent(Model model, Object object, int index) { |
| 87 |
|
| 88 |
ratesCalculated = false; |
| 89 |
fireModelChanged(); |
| 90 |
} |
| 91 |
|
| 92 |
protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 93 |
|
| 94 |
|
| 95 |
ratesCalculated = false; |
| 96 |
fireModelChanged(); |
| 97 |
} |
| 98 |
|
| 99 |
protected void storeState() { |
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
protected void restoreState() { |
| 104 |
ratesCalculated = false; |
| 105 |
|
| 106 |
} |
| 107 |
|
| 108 |
protected void acceptState() { |
| 109 |
|
| 110 |
} |
| 111 |
|
| 112 |
public double getBranchRate(final Tree tree, final NodeRef node) { |
| 113 |
|
| 114 |
if (!ratesCalculated) { |
| 115 |
double mu = mutationRateParameter.getParameterValue(0); |
| 116 |
double k; |
| 117 |
if (proportionParameter != null) { |
| 118 |
k = mu * proportionParameter.getParameterValue(0); |
| 119 |
} else { |
| 120 |
k = substitutionRateParameter.getParameterValue(0); |
| 121 |
} |
| 122 |
|
| 123 |
double lambda = Math.log(2) / halfLifeParameter.getParameterValue(0); |
| 124 |
|
| 125 |
calculateNodeRates(tree, tree.getRoot(), mu, k, lambda); |
| 126 |
|
| 127 |
ratesCalculated = true; |
| 128 |
} |
| 129 |
|
| 130 |
return rates[node.getNumber()]; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
private final double calculateNodeRates(Tree tree, NodeRef node, double mu, double k, double lambda) { |
| 139 |
|
| 140 |
NodeRef parent = tree.getParent(node); |
| 141 |
|
| 142 |
double time0 = 0.0; |
| 143 |
|
| 144 |
if (!tree.isExternal(node)) { |
| 145 |
|
| 146 |
|
| 147 |
NodeRef child1 = tree.getChild(node, 0); |
| 148 |
double t1 = calculateNodeRates(tree, child1, mu, k, lambda); |
| 149 |
|
| 150 |
NodeRef child2 = tree.getChild(node, 1); |
| 151 |
double t2 = calculateNodeRates(tree, child2, mu, k, lambda); |
| 152 |
|
| 153 |
if (useAveraging) { |
| 154 |
time0 = (t1 + t2) / 2.0; |
| 155 |
} else { |
| 156 |
|
| 157 |
if (t1 > t2) { |
| 158 |
time0 = t1; |
| 159 |
} else { |
| 160 |
time0 = t2; |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
if (parent == null) return 0; |
| 167 |
|
| 168 |
double branchTime = tree.getNodeHeight(parent) - tree.getNodeHeight(node); |
| 169 |
double time1 = time0 + branchTime; |
| 170 |
|
| 171 |
double branchRate = rateIntegral(time1, mu, k, lambda); |
| 172 |
|
| 173 |
if (time0 > 0.0) { |
| 174 |
branchRate -= rateIntegral(time0, mu, k, lambda); |
| 175 |
} |
| 176 |
|
| 177 |
rates[node.getNumber()] = branchRate / branchTime; |
| 178 |
|
| 179 |
return time1; |
| 180 |
} |
| 181 |
|
| 182 |
private static double rateIntegral(double time, double mu, double k, double lambda) { |
| 183 |
return ((k * time) + (((mu - k) / lambda) * (1.0 - Math.exp(-lambda * time)))); |
| 184 |
} |
| 185 |
|
| 186 |
private static double rate(double time, double mu, double k, double lambda) { |
| 187 |
return k + ((mu - k) * Math.exp(-lambda * time)); |
| 188 |
} |
| 189 |
|
| 190 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 191 |
|
| 192 |
public String getParserName() { |
| 193 |
return DECAYING_RATE_MODEL; |
| 194 |
} |
| 195 |
|
| 196 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 197 |
|
| 198 |
boolean useAveraging = xo.getBooleanAttribute(AVERAGE); |
| 199 |
|
| 200 |
TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class); |
| 201 |
|
| 202 |
Parameter mutationRateParameter = (Parameter) xo.getElementFirstChild(MUTATION_RATE); |
| 203 |
|
| 204 |
Parameter proportionParameter = null; |
| 205 |
Parameter substitutionRateParameter = null; |
| 206 |
|
| 207 |
if (xo.hasChildNamed(PROPORTION)) { |
| 208 |
proportionParameter = (Parameter) xo.getElementFirstChild(PROPORTION); |
| 209 |
} else { |
| 210 |
substitutionRateParameter = (Parameter) xo.getElementFirstChild(SUBSTITUTION_RATE); |
| 211 |
} |
| 212 |
Parameter halfLifeParameter = (Parameter) xo.getElementFirstChild(HALF_LIFE); |
| 213 |
|
| 214 |
Logger.getLogger("dr.evomodel").info("Using decaying-rate clock model."); |
| 215 |
|
| 216 |
return new DecayingRateModel(treeModel, mutationRateParameter, substitutionRateParameter, proportionParameter, halfLifeParameter, useAveraging); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
public String getParserDescription() { |
| 224 |
return |
| 225 |
"This element provides a clock model in which the rate of " + |
| 226 |
"molecular evolution decays to a substitution rate in the past."; |
| 227 |
} |
| 228 |
|
| 229 |
public Class getReturnType() { |
| 230 |
return DecayingRateModel.class; |
| 231 |
} |
| 232 |
|
| 233 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 234 |
return rules; |
| 235 |
} |
| 236 |
|
| 237 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 238 |
AttributeRule.newBooleanRule(AVERAGE, false), |
| 239 |
new ElementRule(TreeModel.class, "The tree model"), |
| 240 |
new ElementRule(MUTATION_RATE, Parameter.class, "The mutation rate parameter", false), |
| 241 |
new XORRule( |
| 242 |
new ElementRule(PROPORTION, Parameter.class, "The proportion of neutral mutations", false), |
| 243 |
new ElementRule(SUBSTITUTION_RATE, Parameter.class, "The long-term substitution", false) |
| 244 |
), |
| 245 |
new ElementRule(HALF_LIFE, Parameter.class, "The half-life of a deleterious mutation", false) |
| 246 |
}; |
| 247 |
}; |
| 248 |
|
| 249 |
public static void main(String[] argv) { |
| 250 |
double mu = 1E-4; |
| 251 |
double k = 1E-5; |
| 252 |
double lambda = Math.log(2) / 1.0; |
| 253 |
|
| 254 |
double time1 = 0.000001; |
| 255 |
for (int i = 0; i < 100; i++) { |
| 256 |
System.out.println(time1 + "\t" + rate(time1, mu, k, lambda) + "\t" + (rateIntegral(time1, mu, k, lambda)) / time1); |
| 257 |
time1 += 1.0; |
| 258 |
} |
| 259 |
} |
| 260 |
} |