| 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.evomodel.substmodel.FrequencyModel; |
| 29 |
import dr.evomodel.substmodel.nucleotide.GTR; |
| 30 |
import dr.inference.model.Parameter; |
| 31 |
import dr.inference.model.Variable; |
| 32 |
import dr.xml.*; |
| 33 |
|
| 34 |
import static dr.evomodel.substmodel.nucleotide.GTR.*; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public class GTRParser extends AbstractXMLObjectParser { |
| 41 |
public static final String GTR_MODEL = "gtrModel"; |
| 42 |
|
| 43 |
public static final String FREQUENCIES = "frequencies"; |
| 44 |
public static final String RATES = "rates"; |
| 45 |
|
| 46 |
public static final String A_TO_C = "rateAC"; |
| 47 |
public static final String A_TO_G = "rateAG"; |
| 48 |
public static final String A_TO_T = "rateAT"; |
| 49 |
public static final String C_TO_G = "rateCG"; |
| 50 |
public static final String C_TO_T = "rateCT"; |
| 51 |
public static final String G_TO_T = "rateGT"; |
| 52 |
|
| 53 |
public String getParserName() { |
| 54 |
return GTR_MODEL; |
| 55 |
} |
| 56 |
|
| 57 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 58 |
|
| 59 |
FrequencyModel freqModel = (FrequencyModel) xo.getElementFirstChild(FREQUENCIES); |
| 60 |
|
| 61 |
Parameter rates = null; |
| 62 |
|
| 63 |
if (xo.hasChildNamed(RATES)) { |
| 64 |
rates = (Parameter) xo.getElementFirstChild(RATES); |
| 65 |
rates.setDimensionNames(new String[] { |
| 66 |
rates.getId() + "." + A_TO_C, |
| 67 |
rates.getId() + "." + A_TO_G, |
| 68 |
rates.getId() + "." + A_TO_T, |
| 69 |
rates.getId() + "." + C_TO_G, |
| 70 |
rates.getId() + "." + C_TO_T, |
| 71 |
rates.getId() + "." + G_TO_T}); |
| 72 |
|
| 73 |
return new GTR(rates, freqModel); |
| 74 |
} else { |
| 75 |
|
| 76 |
Variable<Double> rateACVariable = null; |
| 77 |
if (xo.hasChildNamed(A_TO_C)) { |
| 78 |
rateACVariable = (Variable<Double>) xo.getElementFirstChild(A_TO_C); |
| 79 |
} |
| 80 |
Variable<Double> rateAGVariable = null; |
| 81 |
if (xo.hasChildNamed(A_TO_G)) { |
| 82 |
rateAGVariable = (Variable<Double>) xo.getElementFirstChild(A_TO_G); |
| 83 |
} |
| 84 |
Variable<Double> rateATVariable = null; |
| 85 |
if (xo.hasChildNamed(A_TO_T)) { |
| 86 |
rateATVariable = (Variable<Double>) xo.getElementFirstChild(A_TO_T); |
| 87 |
} |
| 88 |
Variable<Double> rateCGVariable = null; |
| 89 |
if (xo.hasChildNamed(C_TO_G)) { |
| 90 |
rateCGVariable = (Variable<Double>) xo.getElementFirstChild(C_TO_G); |
| 91 |
} |
| 92 |
Variable<Double> rateCTVariable = null; |
| 93 |
if (xo.hasChildNamed(C_TO_T)) { |
| 94 |
rateCTVariable = (Variable<Double>) xo.getElementFirstChild(C_TO_T); |
| 95 |
} |
| 96 |
Variable<Double> rateGTVariable = null; |
| 97 |
if (xo.hasChildNamed(G_TO_T)) { |
| 98 |
rateGTVariable = (Variable<Double>) xo.getElementFirstChild(G_TO_T); |
| 99 |
} |
| 100 |
int countNull = 0; |
| 101 |
if (rateACVariable == null) countNull++; |
| 102 |
if (rateAGVariable == null) countNull++; |
| 103 |
if (rateATVariable == null) countNull++; |
| 104 |
if (rateCGVariable == null) countNull++; |
| 105 |
if (rateCTVariable == null) countNull++; |
| 106 |
if (rateGTVariable == null) countNull++; |
| 107 |
|
| 108 |
if (countNull != 1) { |
| 109 |
throw new XMLParseException("Only five parameters may be specified in GTR, leave exactly one out, the others will be specifed relative to the one left out."); |
| 110 |
} |
| 111 |
return new GTR(rateACVariable, rateAGVariable, rateATVariable, rateCGVariable, rateCTVariable, rateGTVariable, freqModel); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public String getParserDescription() { |
| 120 |
return "A general reversible model of nucleotide sequence substitution."; |
| 121 |
} |
| 122 |
|
| 123 |
public String getExample() { |
| 124 |
|
| 125 |
return |
| 126 |
"<!-- A general time reversible model for DNA. -->\n" + |
| 127 |
"<!-- This element must have parameters for exactly five of the six rates -->\n" + |
| 128 |
"<!-- The sixth rate has an implied value of 1.0 and all other rates are relative to it -->\n" + |
| 129 |
"<!-- This example parameterizes the rate matrix relative to the A<->G transition -->\n" + |
| 130 |
"<" + getParserName() + " id=\"gtr1\">\n" + |
| 131 |
" <" + FREQUENCIES + "> <frequencyModel idref=\"freqs\"/> </" + FREQUENCIES + ">\n" + |
| 132 |
" <" + A_TO_C + "> <parameter id=\"rateAC\" value=\"1.0\"/> </" + A_TO_C + ">\n" + |
| 133 |
" <" + A_TO_T + "> <parameter id=\"rateAT\" value=\"1.0\"/> </" + A_TO_T + ">\n" + |
| 134 |
" <" + C_TO_G + "> <parameter id=\"rateCG\" value=\"1.0\"/> </" + C_TO_G + ">\n" + |
| 135 |
" <" + C_TO_T + "> <parameter id=\"rateCT\" value=\"1.0\"/> </" + C_TO_T + ">\n" + |
| 136 |
" <" + G_TO_T + "> <parameter id=\"rateGT\" value=\"1.0\"/> </" + G_TO_T + ">\n" + |
| 137 |
"</" + getParserName() + ">\n"; |
| 138 |
} |
| 139 |
|
| 140 |
public Class getReturnType() { |
| 141 |
return GTR.class; |
| 142 |
} |
| 143 |
|
| 144 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 145 |
return rules; |
| 146 |
} |
| 147 |
|
| 148 |
private final XMLSyntaxRule[] rules = { |
| 149 |
new ElementRule(FREQUENCIES, |
| 150 |
new XMLSyntaxRule[]{new ElementRule(FrequencyModel.class)}), |
| 151 |
new ElementRule(RATES, |
| 152 |
new XMLSyntaxRule[]{new ElementRule(Parameter.class)}, true), |
| 153 |
new ElementRule(A_TO_C, |
| 154 |
new XMLSyntaxRule[]{new ElementRule(Variable.class)}, true), |
| 155 |
new ElementRule(A_TO_G, |
| 156 |
new XMLSyntaxRule[]{new ElementRule(Variable.class)}, true), |
| 157 |
new ElementRule(A_TO_T, |
| 158 |
new XMLSyntaxRule[]{new ElementRule(Variable.class)}, true), |
| 159 |
new ElementRule(C_TO_G, |
| 160 |
new XMLSyntaxRule[]{new ElementRule(Variable.class)}, true), |
| 161 |
new ElementRule(C_TO_T, |
| 162 |
new XMLSyntaxRule[]{new ElementRule(Variable.class)}, true), |
| 163 |
new ElementRule(G_TO_T, |
| 164 |
new XMLSyntaxRule[]{new ElementRule(Variable.class)}, true) |
| 165 |
}; |
| 166 |
} |