| 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.branchmodel; |
| 27 |
|
| 28 |
import java.util.ArrayList; |
| 29 |
import java.util.Collections; |
| 30 |
import java.util.List; |
| 31 |
import java.util.logging.Logger; |
| 32 |
|
| 33 |
import dr.evomodel.branchmodel.EpochBranchModel; |
| 34 |
import dr.evomodel.substmodel.SubstitutionModel; |
| 35 |
import dr.evomodel.branchratemodel.RateEpochBranchRateModel; |
| 36 |
import dr.evomodel.tree.TreeModel; |
| 37 |
import dr.inference.model.CompoundParameter; |
| 38 |
import dr.inference.model.Parameter; |
| 39 |
import dr.xml.AbstractXMLObjectParser; |
| 40 |
import dr.xml.AttributeRule; |
| 41 |
import dr.xml.ElementRule; |
| 42 |
import dr.xml.XMLObject; |
| 43 |
import dr.xml.XMLParseException; |
| 44 |
import dr.xml.XMLSyntaxRule; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public class EpochBranchModelParser extends AbstractXMLObjectParser { |
| 49 |
|
| 50 |
public static final String EPOCH_BRANCH_MODEL = "epochBranchModel"; |
| 51 |
public static final String EPOCH = "epoch"; |
| 52 |
public static final String TRANSITION_TIME = "transitionTime"; |
| 53 |
|
| 54 |
public String getParserName() { |
| 55 |
return EPOCH_BRANCH_MODEL; |
| 56 |
} |
| 57 |
|
| 58 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 59 |
|
| 60 |
Logger.getLogger("dr.evomodel").info("\nUsing multi-epoch branch model."); |
| 61 |
|
| 62 |
TreeModel tree = (TreeModel) xo.getChild(TreeModel.class); |
| 63 |
SubstitutionModel ancestralSubstitutionModel = (SubstitutionModel) xo.getChild(SubstitutionModel.class); |
| 64 |
|
| 65 |
List<Epoch> epochs = new ArrayList<Epoch>(); |
| 66 |
|
| 67 |
for (int i = 0; i < xo.getChildCount(); i++) { |
| 68 |
if (xo.getChild(i) instanceof XMLObject) { |
| 69 |
XMLObject xoc = (XMLObject) xo.getChild(i); |
| 70 |
if (xoc.getName().equals(EPOCH)) { |
| 71 |
|
| 72 |
Parameter tt = null; |
| 73 |
|
| 74 |
if (xoc.hasAttribute(TRANSITION_TIME)) { |
| 75 |
double t = xoc.getAttribute(TRANSITION_TIME, 0.0); |
| 76 |
tt = new Parameter.Default(1, t); |
| 77 |
} |
| 78 |
|
| 79 |
SubstitutionModel s = (SubstitutionModel) xoc.getChild(SubstitutionModel.class); |
| 80 |
|
| 81 |
if (xoc.hasChildNamed(TRANSITION_TIME)) { |
| 82 |
if (tt != null) { |
| 83 |
throw new XMLParseException("An epoch cannot have a transitionTime attribute and a parameter"); |
| 84 |
} |
| 85 |
|
| 86 |
tt = (Parameter) xoc.getElementFirstChild(TRANSITION_TIME); |
| 87 |
} |
| 88 |
epochs.add(new Epoch(s, tt)); |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
Collections.sort(epochs); |
| 94 |
List<SubstitutionModel> substitutionModels = new ArrayList<SubstitutionModel>(); |
| 95 |
CompoundParameter transitionTimes = new CompoundParameter("epochTimes"); |
| 96 |
|
| 97 |
for (Epoch epoch : epochs) { |
| 98 |
substitutionModels.add(epoch.substitutionModel); |
| 99 |
transitionTimes.addParameter(epoch.timeParameter); |
| 100 |
} |
| 101 |
|
| 102 |
substitutionModels.add(ancestralSubstitutionModel); |
| 103 |
|
| 104 |
return new EpochBranchModel(tree, substitutionModels, transitionTimes); |
| 105 |
} |
| 106 |
|
| 107 |
class Epoch implements Comparable<Object> { |
| 108 |
|
| 109 |
private final double transitionTime; |
| 110 |
private final SubstitutionModel substitutionModel; |
| 111 |
private final Parameter timeParameter; |
| 112 |
|
| 113 |
public Epoch(SubstitutionModel substitutionModel, Parameter timeParameter) { |
| 114 |
this.transitionTime = timeParameter.getParameterValue(0); |
| 115 |
this.substitutionModel = substitutionModel; |
| 116 |
this.timeParameter = timeParameter; |
| 117 |
} |
| 118 |
|
| 119 |
public int compareTo(Object o) { |
| 120 |
return Double.compare(transitionTime, ((Epoch) o).transitionTime); |
| 121 |
} |
| 122 |
|
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public String getParserDescription() { |
| 129 |
return |
| 130 |
"This element provides a branch model which has multiple epoch. " + |
| 131 |
"All branches (or portions of them) have the same substitution " + |
| 132 |
"model within a given epoch. If parameters are used to sample " + |
| 133 |
"transition times, these must be kept in ascending order by judicious " + |
| 134 |
"use of bounds or priors."; |
| 135 |
} |
| 136 |
|
| 137 |
public Class<RateEpochBranchRateModel> getReturnType() { |
| 138 |
return RateEpochBranchRateModel.class; |
| 139 |
} |
| 140 |
|
| 141 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 142 |
return rules; |
| 143 |
} |
| 144 |
|
| 145 |
private final XMLSyntaxRule[] rules = { |
| 146 |
new ElementRule(TreeModel.class, "The tree across which the epochs will be assigned"), |
| 147 |
new ElementRule(SubstitutionModel.class, "The ancestral substitution model after the last epoch"), |
| 148 |
new ElementRule(EPOCH, |
| 149 |
new XMLSyntaxRule[]{ |
| 150 |
AttributeRule.newDoubleRule(TRANSITION_TIME, true, "The time of transition between this epoch and the previous one"), |
| 151 |
new ElementRule(SubstitutionModel.class, "The substitution model for this epoch"), |
| 152 |
new ElementRule(TRANSITION_TIME, Parameter.class, "The transition time parameter for this epoch", true) |
| 153 |
}, "An epoch that lasts until transitionTime", |
| 154 |
1, Integer.MAX_VALUE |
| 155 |
), |
| 156 |
}; |
| 157 |
|
| 158 |
} |