| 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.substmodel; |
| 27 |
|
| 28 |
import dr.evolution.datatype.HiddenDataType; |
| 29 |
import dr.evomodel.siteratemodel.SiteRateModel; |
| 30 |
import dr.evomodel.substmodel.MarkovModulatedSubstitutionModel; |
| 31 |
import dr.evomodel.substmodel.SubstitutionModel; |
| 32 |
import dr.evolution.datatype.DataType; |
| 33 |
import dr.evoxml.util.DataTypeUtils; |
| 34 |
import dr.inference.model.Parameter; |
| 35 |
import dr.xml.*; |
| 36 |
|
| 37 |
import java.util.ArrayList; |
| 38 |
import java.util.List; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public class MarkovModulatedSubstitutionModelParser extends AbstractXMLObjectParser { |
| 44 |
|
| 45 |
public static final String MARKOV_MODULATED_MODEL = "markovModulatedSubstitutionModel"; |
| 46 |
|
| 47 |
public static final String SWITCHING_RATES = "switchingRates"; |
| 48 |
|
| 49 |
public static final String RATE_SCALAR = "rateScalar"; |
| 50 |
public static final String GEOMETRIC_RATES = "geometricRates"; |
| 51 |
public static final String RENORMALIZE = "renormalize"; |
| 52 |
|
| 53 |
public String getParserName() { |
| 54 |
return MARKOV_MODULATED_MODEL; |
| 55 |
} |
| 56 |
|
| 57 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 58 |
|
| 59 |
DataType dataType = DataTypeUtils.getDataType(xo); |
| 60 |
if (!(dataType instanceof HiddenDataType)) { |
| 61 |
throw new XMLParseException("Must construct " + MARKOV_MODULATED_MODEL + " with hidden data types"); |
| 62 |
} |
| 63 |
|
| 64 |
Parameter switchingRates = (Parameter) xo.getElementFirstChild(SWITCHING_RATES); |
| 65 |
|
| 66 |
List<SubstitutionModel> substModels = new ArrayList<SubstitutionModel>(); |
| 67 |
for (int i = 0; i < xo.getChildCount(); i++) { |
| 68 |
Object cxo = xo.getChild(i); |
| 69 |
if (cxo instanceof SubstitutionModel) { |
| 70 |
substModels.add((SubstitutionModel) cxo); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
boolean geometricRates = xo.getAttribute(GEOMETRIC_RATES, false); |
| 75 |
|
| 76 |
Parameter rateScalar = xo.hasChildNamed(RATE_SCALAR) ? |
| 77 |
(Parameter) xo.getChild(RATE_SCALAR).getChild(Parameter.class) : null; |
| 78 |
|
| 79 |
SiteRateModel siteRateModel = (SiteRateModel) xo.getChild(SiteRateModel.class); |
| 80 |
if (siteRateModel != null) { |
| 81 |
if (siteRateModel.getCategoryCount() != substModels.size() && substModels.size() % siteRateModel.getCategoryCount() != 0) { |
| 82 |
throw new XMLParseException( |
| 83 |
"Number of gamma categories must equal number of substitution models in " + xo.getId()); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
MarkovModulatedSubstitutionModel mmsm = new MarkovModulatedSubstitutionModel(xo.getId(), substModels, switchingRates, dataType, null, |
| 88 |
rateScalar, geometricRates, siteRateModel); |
| 89 |
|
| 90 |
if (xo.getAttribute(RENORMALIZE, false)) { |
| 91 |
mmsm.setNormalization(true); |
| 92 |
} |
| 93 |
|
| 94 |
return mmsm; |
| 95 |
} |
| 96 |
|
| 97 |
public String getParserDescription() { |
| 98 |
return "This element represents the a Markov-modulated substitution model."; |
| 99 |
} |
| 100 |
|
| 101 |
public Class getReturnType() { |
| 102 |
return MarkovModulatedSubstitutionModel.class; |
| 103 |
} |
| 104 |
|
| 105 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 106 |
return rules; |
| 107 |
} |
| 108 |
|
| 109 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 110 |
|
| 111 |
AttributeRule.newStringRule(DataType.DATA_TYPE), |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
new ElementRule(SWITCHING_RATES, |
| 118 |
new XMLSyntaxRule[]{new ElementRule(Parameter.class)}), |
| 119 |
|
| 120 |
|
| 121 |
new ElementRule(SubstitutionModel.class, 1, Integer.MAX_VALUE), |
| 122 |
AttributeRule.newBooleanRule(GEOMETRIC_RATES, true), |
| 123 |
AttributeRule.newBooleanRule(RENORMALIZE, true), |
| 124 |
new ElementRule(RATE_SCALAR, |
| 125 |
new XMLSyntaxRule[]{new ElementRule(Parameter.class)}, true), |
| 126 |
|
| 127 |
new ElementRule(SiteRateModel.class, true), |
| 128 |
}; |
| 129 |
} |