Debian Codegraph @

src/dr/oldevomodelxml/substmodel/GTRParser.java

raw symbols SCIP
1 /*
2 * GTRParser.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
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 * Parses an element from an DOM document into a DemographicModel. Recognises
36 * ConstantPopulation and ExponentialGrowth.
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 // AbstractXMLObjectParser implementation
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 }