Debian Codegraph @

src/dr/oldevomodelxml/substmodel/CovarionHKYParser.java

raw symbols SCIP
1 /*
2 * CovarionHKYParser.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.evolution.datatype.OldHiddenNucleotides;
29 import dr.oldevomodel.substmodel.AbstractCovarionDNAModel;
30 import dr.oldevomodel.substmodel.CovarionHKY;
31 import dr.oldevomodel.substmodel.FrequencyModel;
32 import dr.inference.model.Parameter;
33 import dr.xml.*;
34
35 /**
36 * Parses an element from an DOM document into a DemographicModel. Recognises
37 * ConstantPopulation and ExponentialGrowth.
38 */
39 public class CovarionHKYParser extends AbstractXMLObjectParser {
40 public static final String COVARION_HKY = "CovarionHKYModel";
41 public static final String KAPPA = HKYParser.KAPPA;
42
43 public String getParserName() {
44 return COVARION_HKY;
45 }
46
47 public String getParserDescription() {
48 return "A covarion HKY model.";
49 }
50
51 public Class getReturnType() {
52 return CovarionHKY.class;
53 }
54
55 public XMLSyntaxRule[] getSyntaxRules() {
56 return rules;
57 }
58
59 private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
60 new ElementRule(KAPPA, new XMLSyntaxRule[]{
61 new ElementRule(Parameter.class,
62 "A parameter representing the transition transversion bias")}),
63 new ElementRule(AbstractCovarionDNAModel.SWITCHING_RATES, new XMLSyntaxRule[]{
64 new ElementRule(Parameter.class,
65 "A parameter representing the rate of change between the different classes")}),
66 new ElementRule(AbstractCovarionDNAModel.HIDDEN_CLASS_RATES, new XMLSyntaxRule[]{
67 new ElementRule(Parameter.class,
68 "A parameter representing the rates of the hidden classes relative to the first hidden class.")})
69 };
70
71 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
72
73 Parameter kappaParam;
74 Parameter switchingRates;
75 Parameter hiddenClassRates;
76 FrequencyModel freqModel;
77
78 kappaParam = (Parameter) xo.getElementFirstChild(KAPPA);
79 switchingRates = (Parameter) xo.getElementFirstChild(AbstractCovarionDNAModel.SWITCHING_RATES);
80 hiddenClassRates = (Parameter) xo.getElementFirstChild(AbstractCovarionDNAModel.HIDDEN_CLASS_RATES);
81 freqModel = (FrequencyModel) xo.getElementFirstChild(AbstractCovarionDNAModel.FREQUENCIES);
82
83 if (!(freqModel.getDataType() instanceof OldHiddenNucleotides)) {
84 throw new IllegalArgumentException("Datatype must be hidden nucleotides!!");
85 }
86
87 OldHiddenNucleotides dataType = (OldHiddenNucleotides) freqModel.getDataType();
88
89 int hiddenStateCount = dataType.getHiddenClassCount();
90
91 int switchingRatesCount = hiddenStateCount * (hiddenStateCount - 1) / 2;
92
93 if (switchingRates.getDimension() != switchingRatesCount) {
94 throw new IllegalArgumentException("switching rates parameter must have " +
95 switchingRatesCount + " dimensions, for " + hiddenStateCount +
96 " hidden categories");
97 }
98
99 CovarionHKY model = new CovarionHKY(dataType, kappaParam, hiddenClassRates, switchingRates, freqModel);
100 System.out.println(model);
101 return model;
102 }
103
104 }