| 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.evomodelxml.branchratemodel; |
| 27 |
|
| 28 |
import dr.evomodel.branchratemodel.ContinuousEpochBranchRateModel; |
| 29 |
import dr.evomodel.branchratemodel.RateEpochBranchRateModel; |
| 30 |
import dr.evomodelxml.tree.TreeModelParser; |
| 31 |
import dr.inference.model.Parameter; |
| 32 |
import dr.xml.*; |
| 33 |
|
| 34 |
import java.util.ArrayList; |
| 35 |
import java.util.Collections; |
| 36 |
import java.util.List; |
| 37 |
import java.util.logging.Logger; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class RateEpochBranchRateModelParser extends AbstractXMLObjectParser { |
| 42 |
|
| 43 |
public static final String RATE_EPOCH_BRANCH_RATES = "rateEpochBranchRates"; |
| 44 |
public static final String RATE = "rate"; |
| 45 |
public static final String EPOCH = "epoch"; |
| 46 |
public static final String TRANSITION_TIME = "transitionTime"; |
| 47 |
public static final String CONTINUOUS_NORMALIZATION = "continuousNormalization"; |
| 48 |
|
| 49 |
public String getParserName() { |
| 50 |
return RATE_EPOCH_BRANCH_RATES; |
| 51 |
} |
| 52 |
|
| 53 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 54 |
|
| 55 |
Logger.getLogger("dr.evomodel").info("\nUsing multi-epoch rate model."); |
| 56 |
|
| 57 |
List<Epoch> epochs = new ArrayList<Epoch>(); |
| 58 |
|
| 59 |
for (int i = 0; i < xo.getChildCount(); i++) { |
| 60 |
XMLObject xoc = (XMLObject) xo.getChild(i); |
| 61 |
if (xoc.getName().equals(EPOCH)) { |
| 62 |
double t = xoc.getAttribute(TRANSITION_TIME, 0.0); |
| 63 |
|
| 64 |
Parameter p = (Parameter) xoc.getChild(Parameter.class); |
| 65 |
|
| 66 |
Parameter tt = null; |
| 67 |
if (xoc.hasChildNamed(TRANSITION_TIME)) { |
| 68 |
tt = (Parameter) xoc.getElementFirstChild(TRANSITION_TIME); |
| 69 |
} |
| 70 |
epochs.add(new Epoch(t, p, tt)); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
Parameter ancestralRateParameter = (Parameter) xo.getElementFirstChild(RATE); |
| 75 |
|
| 76 |
Collections.sort(epochs); |
| 77 |
Parameter[] rateParameters = new Parameter[epochs.size() + 1]; |
| 78 |
Parameter[] timeParameters = new Parameter[epochs.size()]; |
| 79 |
|
| 80 |
int i = 0; |
| 81 |
for (Epoch epoch : epochs) { |
| 82 |
rateParameters[i] = epoch.rateParameter; |
| 83 |
if (epoch.timeParameter != null) { |
| 84 |
timeParameters[i] = epoch.timeParameter; |
| 85 |
} else { |
| 86 |
timeParameters[i] = new Parameter.Default(1); |
| 87 |
timeParameters[i].setParameterValue(0, epoch.transitionTime); |
| 88 |
} |
| 89 |
i++; |
| 90 |
} |
| 91 |
rateParameters[i] = ancestralRateParameter; |
| 92 |
|
| 93 |
if (xo.hasAttribute(CONTINUOUS_NORMALIZATION) && xo.getBooleanAttribute(CONTINUOUS_NORMALIZATION)) { |
| 94 |
Parameter rootHeight = (Parameter) xo.getChild(TreeModelParser.ROOT_HEIGHT).getChild(Parameter.class); |
| 95 |
return new ContinuousEpochBranchRateModel(timeParameters, rateParameters, rootHeight); |
| 96 |
} |
| 97 |
|
| 98 |
return new RateEpochBranchRateModel(timeParameters, rateParameters); |
| 99 |
} |
| 100 |
|
| 101 |
class Epoch implements Comparable { |
| 102 |
|
| 103 |
private final double transitionTime; |
| 104 |
private final Parameter rateParameter; |
| 105 |
private final Parameter timeParameter; |
| 106 |
|
| 107 |
public Epoch(double transitionTime, Parameter rateParameter, Parameter timeParameter) { |
| 108 |
this.transitionTime = transitionTime; |
| 109 |
this.rateParameter = rateParameter; |
| 110 |
this.timeParameter = timeParameter; |
| 111 |
} |
| 112 |
|
| 113 |
public int compareTo(Object o) { |
| 114 |
return Double.compare(transitionTime, ((Epoch) o).transitionTime); |
| 115 |
} |
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public String getParserDescription() { |
| 123 |
return "This element provides a multiple epoch molecular clock model. " + |
| 124 |
"All branches (or portions of them) have the same rate of molecular " + |
| 125 |
"evolution within a given epoch. If parameters are used to sample " + |
| 126 |
"transition times, these must be kept in ascending order by judicious " + |
| 127 |
"use of bounds or priors."; |
| 128 |
} |
| 129 |
|
| 130 |
public Class getReturnType() { |
| 131 |
return RateEpochBranchRateModel.class; |
| 132 |
} |
| 133 |
|
| 134 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 135 |
return rules; |
| 136 |
} |
| 137 |
|
| 138 |
private final XMLSyntaxRule[] rules = { |
| 139 |
new ElementRule(EPOCH, |
| 140 |
new XMLSyntaxRule[]{ |
| 141 |
AttributeRule.newDoubleRule(TRANSITION_TIME, true, "The time of transition between this epoch and the previous one"), |
| 142 |
new ElementRule(Parameter.class, "The evolutionary rate parameter for this epoch"), |
| 143 |
new ElementRule(TRANSITION_TIME, Parameter.class, "The transition time parameter for this epoch", true) |
| 144 |
}, "An epoch that lasts until transitionTime", |
| 145 |
1, Integer.MAX_VALUE |
| 146 |
), |
| 147 |
new ElementRule(RATE, Parameter.class, "The ancestral molecular evolutionary rate parameter", false), |
| 148 |
AttributeRule.newBooleanRule(CONTINUOUS_NORMALIZATION, true, "Special rate normalization for a Brownian diffusion process"), |
| 149 |
new ElementRule(TreeModelParser.ROOT_HEIGHT, |
| 150 |
new XMLSyntaxRule[]{ |
| 151 |
new ElementRule(Parameter.class, "The tree root height") |
| 152 |
}, "Parameterization may require the root height", 0, 1) |
| 153 |
}; |
| 154 |
|
| 155 |
} |