Debian Codegraph @

src/dr/evomodelxml/substmodel/MG94CodonModelParser.java

raw symbols SCIP
1 /*
2 * MG94CodonModelParser.java
3 *
4 * Copyright (c) 2002-2016 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.evomodelxml.substmodel;
27
28 import dr.evomodel.substmodel.FrequencyModel;
29 import dr.evomodel.substmodel.codon.MG94CodonModel;
30 import dr.evomodel.substmodel.codon.MG94HKYCodonModel;
31 import dr.evolution.datatype.Codons;
32 import dr.evolution.datatype.GeneticCode;
33 import dr.evomodel.substmodel.nucleotide.GTR;
34 import dr.inference.model.Parameter;
35 import dr.xml.*;
36
37 /**
38 * @author Marc A. Suchard
39 * @author Guy Baele
40 * @author Philippe Lemey
41 */
42 public class MG94CodonModelParser extends AbstractXMLObjectParser {
43
44 public static final String MUSE_CODON_MODEL = "museGautCodonModel";
45 public static final String ALPHA = "alpha";
46 public static final String BETA = "beta";
47 public static final String KAPPA = GY94CodonModelParser.KAPPA;
48 public static final String NORMALIZED = ComplexSubstitutionModelParser.NORMALIZED;
49
50 //for GTR extension
51 public static final String GTR_MODEL = GTRParser.GTR_MODEL;
52 public static final String A_TO_C = GTRParser.A_TO_C;
53 public static final String A_TO_G = GTRParser.A_TO_G;
54 public static final String A_TO_T = GTRParser.A_TO_T;
55 public static final String C_TO_G = GTRParser.C_TO_G;
56 public static final String C_TO_T = GTRParser.C_TO_T;
57 public static final String G_TO_T = GTRParser.G_TO_T;
58
59
60 public String getParserName() {
61 return MUSE_CODON_MODEL;
62 }
63
64 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
65
66 Codons codons = Codons.UNIVERSAL;
67 if (xo.hasAttribute(GeneticCode.GENETIC_CODE)) {
68 String codeStr = xo.getStringAttribute(GeneticCode.GENETIC_CODE);
69 codons = Codons.findByName(codeStr);
70 }
71
72 Parameter alphaParam = (Parameter) xo.getElementFirstChild(ALPHA);
73 Parameter betaParam = (Parameter) xo.getElementFirstChild(BETA);
74 FrequencyModel freqModel = (FrequencyModel) xo.getChild(FrequencyModel.class);
75
76 MG94CodonModel codonModel;
77 if (xo.hasChildNamed(GTR_MODEL)) {
78 //TODO: change this into constructing a MG94CodonModel (needs to be written), which is started underneath
79 codonModel = new MG94CodonModel(codons, alphaParam, betaParam, freqModel);
80
81 Parameter rateACValue = null;
82 if (xo.hasChildNamed(A_TO_C)) {
83 rateACValue = (Parameter) xo.getElementFirstChild(A_TO_C);
84 }
85 Parameter rateAGValue = null;
86 if (xo.hasChildNamed(A_TO_G)) {
87 rateAGValue = (Parameter) xo.getElementFirstChild(A_TO_G);
88 }
89 Parameter rateATValue = null;
90 if (xo.hasChildNamed(A_TO_T)) {
91 rateATValue = (Parameter) xo.getElementFirstChild(A_TO_T);
92 }
93 Parameter rateCGValue = null;
94 if (xo.hasChildNamed(C_TO_G)) {
95 rateCGValue = (Parameter) xo.getElementFirstChild(C_TO_G);
96 }
97 Parameter rateCTValue = null;
98 if (xo.hasChildNamed(C_TO_T)) {
99 rateCTValue = (Parameter) xo.getElementFirstChild(C_TO_T);
100 }
101 Parameter rateGTValue = null;
102 if (xo.hasChildNamed(G_TO_T)) {
103 rateGTValue = (Parameter) xo.getElementFirstChild(G_TO_T);
104 }
105 int countNull = 0;
106 if (rateACValue == null) countNull++;
107 if (rateAGValue == null) countNull++;
108 if (rateATValue == null) countNull++;
109 if (rateCGValue == null) countNull++;
110 if (rateCTValue == null) countNull++;
111 if (rateGTValue == null) countNull++;
112
113 if (countNull != 1)
114 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.");
115 //TODO: turn this on below on and delete kappa.
116 //return new new MG94GTRCodonModel(codons, alphaParam, betaParam, rateACValue, rateAGValue, rateATValue, rateCGValue, rateCTValue, rateGTValue, freqModel);
117
118 if (xo.hasAttribute(KAPPA)){
119 System.err.print("using GTR rates -- overrides KAPPA");
120 }
121
122 } else if (xo.hasChildNamed(KAPPA)) {
123 Parameter kappaParam = (Parameter)xo.getElementFirstChild(KAPPA);
124 codonModel = new MG94HKYCodonModel(codons, alphaParam, betaParam, kappaParam, freqModel);
125 // System.err.println("setting up MG94HKYCodonModel");
126 } else {
127 //resort to standard MG94 without nucleotide rate bias
128 codonModel = new MG94CodonModel(codons, alphaParam, betaParam, freqModel);
129 }
130
131
132 if (!xo.getAttribute(NORMALIZED, true)) {
133 // codonModel.setNormalization(false);
134 // Logger.getLogger("dr.app.beagle.evomodel").info("MG94CodonModel normalization: false");
135 }
136
137 return codonModel;
138 }
139
140 //************************************************************************
141 // AbstractXMLObjectParser implementation
142 //************************************************************************
143
144 public String getParserDescription() {
145 return "This element represents the Yang model of codon evolution.";
146 }
147
148 public Class getReturnType() {
149 return MG94CodonModel.class;
150 }
151
152 public XMLSyntaxRule[] getSyntaxRules() {
153 return rules;
154 }
155
156 private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
157 new StringAttributeRule(GeneticCode.GENETIC_CODE,
158 "The genetic code to use",
159 GeneticCode.GENETIC_CODE_NAMES, true),
160 new ElementRule(ALPHA,
161 new XMLSyntaxRule[]{new ElementRule(Parameter.class)}),
162 new ElementRule(BETA,
163 new XMLSyntaxRule[]{new ElementRule(Parameter.class)}),
164 new ElementRule(KAPPA,
165 new XMLSyntaxRule[] { new ElementRule(Parameter.class) }, true),
166 new ElementRule(A_TO_C,
167 new XMLSyntaxRule[]{new ElementRule(Parameter.class)}, true),
168 new ElementRule(A_TO_G,
169 new XMLSyntaxRule[]{new ElementRule(Parameter.class)}, true),
170 new ElementRule(A_TO_T,
171 new XMLSyntaxRule[]{new ElementRule(Parameter.class)}, true),
172 new ElementRule(C_TO_G,
173 new XMLSyntaxRule[]{new ElementRule(Parameter.class)}, true),
174 new ElementRule(C_TO_T,
175 new XMLSyntaxRule[]{new ElementRule(Parameter.class)}, true),
176 new ElementRule(G_TO_T,
177 new XMLSyntaxRule[]{new ElementRule(Parameter.class)}, true),
178
179 new ElementRule(FrequencyModel.class),
180 AttributeRule.newBooleanRule(NORMALIZED, true),
181 };
182
183 }