Debian Codegraph @

src/dr/oldevomodelxml/substmodel/BinaryCovarionModelParser.java

raw symbols SCIP
1 /*
2 * BinaryCovarionModelParser.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.TwoStateCovarion;
29 import dr.oldevomodel.substmodel.BinaryCovarionModel;
30 import dr.inference.model.Parameter;
31 import dr.xml.*;
32
33 /**
34 * Parses an element from an DOM document into a TwoStateCovarionModel
35 */
36 public class BinaryCovarionModelParser extends AbstractXMLObjectParser {
37 public static final String COVARION_MODEL = "binaryCovarionModel";
38 public static final String ALPHA = "alpha";
39 public static final String SWITCHING_RATE = "switchingRate";
40 public static final String FREQUENCIES = "frequencies";
41 public static final String HIDDEN_FREQUENCIES = "hiddenFrequencies";
42 public static final String VERSION = "version";
43 public static final BinaryCovarionModel.Version DEFAULT_VERSION = BinaryCovarionModel.Version.VERSION1;
44
45 public String getParserName() {
46 return COVARION_MODEL;
47 }
48
49 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
50
51 Parameter alphaParameter;
52 Parameter switchingRateParameter;
53
54 XMLObject cxo = xo.getChild(FREQUENCIES);
55 Parameter frequencies = (Parameter) cxo.getChild(Parameter.class);
56
57 cxo = xo.getChild(HIDDEN_FREQUENCIES);
58 Parameter hiddenFrequencies = (Parameter) cxo.getChild(Parameter.class);
59
60 cxo = xo.getChild(ALPHA);
61 alphaParameter = (Parameter) cxo.getChild(Parameter.class);
62
63 // alpha must be positive and less than 1.0 because the fast rate is normalized to 1.0
64 alphaParameter.addBounds(new Parameter.DefaultBounds(1.0, 0.0, 1));
65 hiddenFrequencies.addBounds(new Parameter.DefaultBounds(1.0, 0.0, hiddenFrequencies.getDimension()));
66 frequencies.addBounds(new Parameter.DefaultBounds(1.0, 0.0, frequencies.getDimension()));
67
68 cxo = xo.getChild(SWITCHING_RATE);
69 switchingRateParameter = (Parameter) cxo.getChild(Parameter.class);
70
71 BinaryCovarionModel.Version version = DEFAULT_VERSION;
72 if (xo.hasAttribute(VERSION)) {
73 version = BinaryCovarionModel.Version.parseFromString(xo.getStringAttribute(VERSION));
74 }
75
76 BinaryCovarionModel model = new BinaryCovarionModel(TwoStateCovarion.INSTANCE,
77 frequencies, hiddenFrequencies, alphaParameter, switchingRateParameter, version);
78
79 System.out.println(model);
80
81 return model;
82 }
83
84 //************************************************************************
85 // AbstractXMLObjectParser implementation
86 //************************************************************************
87
88 public String getParserDescription() {
89 return "A covarion substitution model on binary data and a hidden rate state with two rates.";
90 }
91
92 public Class getReturnType() {
93 return BinaryCovarionModel.class;
94 }
95
96 public XMLSyntaxRule[] getSyntaxRules() {
97 return rules;
98 }
99
100 private final XMLSyntaxRule[] rules = {
101 new ElementRule(FREQUENCIES, Parameter.class),
102 new ElementRule(HIDDEN_FREQUENCIES, Parameter.class),
103 new ElementRule(ALPHA,
104 new XMLSyntaxRule[]{
105 new ElementRule(Parameter.class, true)}
106 ),
107 new ElementRule(SWITCHING_RATE,
108 new XMLSyntaxRule[]{
109 new ElementRule(Parameter.class, true)}
110 ),
111 AttributeRule.newStringRule(VERSION, true),
112 };
113
114
115 }