| 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.oldevomodelxml.substmodel; |
| 27 |
|
| 28 |
import dr.evolution.datatype.DataType; |
| 29 |
import dr.oldevomodel.substmodel.AbstractSubstitutionModel; |
| 30 |
import dr.oldevomodel.substmodel.FrequencyModel; |
| 31 |
import dr.oldevomodel.substmodel.SubstitutionEpochModel; |
| 32 |
import dr.oldevomodel.substmodel.SubstitutionModel; |
| 33 |
import dr.inference.model.Parameter; |
| 34 |
import dr.xml.*; |
| 35 |
|
| 36 |
import java.util.ArrayList; |
| 37 |
import java.util.List; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class SubstitutionEpochModelParser extends AbstractXMLObjectParser { |
| 42 |
|
| 43 |
public static final String SUBSTITUTION_EPOCH_MODEL = "substitutionEpochModel"; |
| 44 |
public static final String MODELS = "models"; |
| 45 |
public static final String TRANSITION_TIMES = "transitionTimes"; |
| 46 |
|
| 47 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 48 |
|
| 49 |
DataType dataType = null; |
| 50 |
FrequencyModel freqModel = null; |
| 51 |
List<SubstitutionModel> modelList = new ArrayList<SubstitutionModel>(); |
| 52 |
XMLObject cxo = xo.getChild(MODELS); |
| 53 |
for (int i = 0; i < cxo.getChildCount(); i++) { |
| 54 |
SubstitutionModel model = (SubstitutionModel) cxo.getChild(i); |
| 55 |
|
| 56 |
if (dataType == null) { |
| 57 |
dataType = model.getDataType(); |
| 58 |
} else if (dataType != model.getDataType()) |
| 59 |
throw new XMLParseException("Substitution models across epoches must use the same data type."); |
| 60 |
|
| 61 |
if (freqModel == null) { |
| 62 |
freqModel = model.getFrequencyModel(); |
| 63 |
} else if (freqModel != model.getFrequencyModel()) |
| 64 |
throw new XMLParseException("Substitution models across epoches must currently use the same frequency model.\nHarass Marc to fix this."); |
| 65 |
|
| 66 |
modelList.add(model); |
| 67 |
} |
| 68 |
|
| 69 |
Parameter transitionTimes = (Parameter) xo.getChild(Parameter.class); |
| 70 |
|
| 71 |
if (transitionTimes.getDimension() != modelList.size() - 1) { |
| 72 |
throw new XMLParseException("# of transition times must equal # of substitution models - 1\n" + transitionTimes.getDimension() + "\n" + modelList.size()); |
| 73 |
} |
| 74 |
|
| 75 |
return new SubstitutionEpochModel(SUBSTITUTION_EPOCH_MODEL, modelList, transitionTimes, dataType, freqModel); |
| 76 |
} |
| 77 |
|
| 78 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 79 |
return new XMLSyntaxRule[]{ |
| 80 |
new ElementRule(MODELS, |
| 81 |
new XMLSyntaxRule[]{ |
| 82 |
new ElementRule(AbstractSubstitutionModel.class, 1, Integer.MAX_VALUE), |
| 83 |
}), |
| 84 |
new ElementRule(Parameter.class), |
| 85 |
}; |
| 86 |
} |
| 87 |
|
| 88 |
public String getParserDescription() { |
| 89 |
return null; |
| 90 |
} |
| 91 |
|
| 92 |
public Class getReturnType() { |
| 93 |
return SubstitutionEpochModel.class; |
| 94 |
} |
| 95 |
|
| 96 |
public String getParserName() { |
| 97 |
return SUBSTITUTION_EPOCH_MODEL; |
| 98 |
} |
| 99 |
} |