Debian Codegraph @

src/dr/evomodelxml/substmodel/MarkovModulatedHiddenClassRewardParser.java

raw symbols SCIP
1 /*
2 * MarkovModulatedHiddenClassRewardParser.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.MarkovModulatedSubstitutionModel;
29 import dr.evolution.datatype.HiddenDataType;
30 import dr.inference.model.Parameter;
31 import dr.xml.*;
32
33 /**
34 * @author Marc A. Suchard
35 */
36
37 public class MarkovModulatedHiddenClassRewardParser extends AbstractXMLObjectParser {
38
39 public static final String PARSER_NAME = "hiddenClassRewardParameter";
40 public static final String NAME = "name";
41 public static final String CLASS_NUMBER = "class";
42
43 @Override
44 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
45
46 MarkovModulatedSubstitutionModel substitutionModel = (MarkovModulatedSubstitutionModel) xo.getChild(MarkovModulatedSubstitutionModel.class);
47 HiddenDataType hiddenDataType = (HiddenDataType) substitutionModel.getDataType();
48 int classNumber = xo.getIntegerAttribute(CLASS_NUMBER);
49 int hiddenClassCount = hiddenDataType.getHiddenClassCount();
50 if (classNumber < 1 || classNumber > hiddenClassCount) {
51 throw new XMLParseException("Invalid class number in " + xo.getId());
52 }
53 classNumber--; // Use zero-indexed number
54 int stateCount = hiddenDataType.getStateCount() / hiddenClassCount;
55
56 // Construct reward parameter
57 Parameter parameter = new Parameter.Default(stateCount * hiddenClassCount, 0.0);
58 for (int i = 0; i < stateCount; ++i) {
59 parameter.setParameterValue(i + classNumber * stateCount, 1.0);
60 }
61
62 if (xo.hasAttribute(NAME)) {
63 parameter.setId((String) xo.getAttribute(NAME));
64 } else {
65 parameter.setId(substitutionModel.getId() + "_" + Integer.toString(classNumber + 1));
66 }
67
68 return parameter;
69 }
70
71 /**
72 * @return an array of syntax rules required by this element.
73 * Order is not important.
74 */
75 @Override
76 public XMLSyntaxRule[] getSyntaxRules() {
77 return rules;
78 }
79
80 private XMLSyntaxRule[] rules = {
81 AttributeRule.newIntegerRule(CLASS_NUMBER),
82 AttributeRule.newStringRule(NAME, true),
83 new ElementRule(MarkovModulatedSubstitutionModel.class),
84 };
85
86 @Override
87 public String getParserDescription() {
88 return "Generates a reward parameter to log hidden classes in Markov-modulated substitutionProcess";
89 }
90
91 @Override
92 public Class getReturnType() {
93 return Parameter.class;
94 }
95
96 /**
97 * @return Parser name, which is identical to name of xml element parsed by it.
98 */
99 public String getParserName() {
100 return PARSER_NAME;
101 }
102 }