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