Debian Codegraph @

src/dr/oldevomodelxml/substmodel/FrequencyModelParser.java

raw symbols SCIP
1 /*
2 * FrequencyModelParser.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.alignment.PatternList;
29 import dr.evolution.datatype.DataType;
30 import dr.oldevomodel.substmodel.FrequencyModel;
31 import dr.evoxml.util.DataTypeUtils;
32 import dr.inference.model.Parameter;
33 import dr.xml.*;
34
35 import java.text.NumberFormat;
36 import java.util.logging.Logger;
37
38 /**
39 * Reads a frequency model from an XMLObject.
40 */
41 public class FrequencyModelParser extends AbstractXMLObjectParser {
42 public static final String FREQUENCIES = "frequencies";
43 public static final String FREQUENCY_MODEL = "frequencyModel";
44 public static final String NORMALIZE = "normalize";
45
46
47 public String[] getParserNames() {
48 return new String[]{
49 getParserName(), "beast_" + getParserName()
50 };
51 }
52
53 public String getParserName() {
54 return FREQUENCY_MODEL;
55 }
56
57 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
58
59 DataType dataType = DataTypeUtils.getDataType(xo);
60
61 Parameter freqsParam = (Parameter) xo.getElementFirstChild(FREQUENCIES);
62 double[] frequencies = null;
63
64 for (int i = 0; i < xo.getChildCount(); i++) {
65 Object obj = xo.getChild(i);
66 if (obj instanceof PatternList) {
67 frequencies = ((PatternList) obj).getStateFrequencies();
68 break;
69 }
70 }
71
72 StringBuilder sb = new StringBuilder("Creating state frequencies model '" + freqsParam.getParameterName() + "': ");
73 if (frequencies != null) {
74 if (freqsParam.getDimension() != frequencies.length) {
75 throw new XMLParseException("dimension of frequency parameter and number of sequence states don't match!");
76 }
77 for (int j = 0; j < frequencies.length; j++) {
78 freqsParam.setParameterValue(j, frequencies[j]);
79 }
80 sb.append("Using empirical frequencies from data ");
81 } else {
82 sb.append("Initial frequencies ");
83 }
84 sb.append("= {");
85
86 double sum = 0;
87 for (int j = 0; j < freqsParam.getDimension(); j++) {
88 sum += freqsParam.getParameterValue(j);
89 }
90
91 if (xo.getAttribute(NORMALIZE, false)) {
92 for (int j = 0; j < freqsParam.getDimension(); j++) {
93 if (sum != 0)
94 freqsParam.setParameterValue(j, freqsParam.getParameterValue(j) / sum);
95 else
96 freqsParam.setParameterValue(j, 1.0 / freqsParam.getDimension());
97 }
98 sum = 1.0;
99 }
100
101 if (Math.abs(sum - 1.0) > 1e-8) {
102 throw new XMLParseException("Frequencies do not sum to 1 (they sum to " + sum + ")");
103 }
104
105
106 NumberFormat format = NumberFormat.getNumberInstance();
107 format.setMaximumFractionDigits(5);
108
109 sb.append(format.format(freqsParam.getParameterValue(0)));
110 for (int j = 1; j < freqsParam.getDimension(); j++) {
111 sb.append(", ");
112 sb.append(format.format(freqsParam.getParameterValue(j)));
113 }
114 sb.append("}");
115 Logger.getLogger("dr.evomodel").info(sb.toString());
116
117 return new FrequencyModel(dataType, freqsParam);
118 }
119
120 public String getParserDescription() {
121 return "A model of equilibrium base frequencies.";
122 }
123
124 public Class getReturnType() {
125 return FrequencyModel.class;
126 }
127
128 public XMLSyntaxRule[] getSyntaxRules() {
129 return rules;
130 }
131
132 private final XMLSyntaxRule[] rules = {
133 AttributeRule.newBooleanRule(NORMALIZE, true),
134
135 new ElementRule(PatternList.class, "Initial value", 0, 1),
136
137 new XORRule(
138 new StringAttributeRule(DataType.DATA_TYPE, "The type of sequence data",
139 DataType.getRegisteredDataTypeNames(), false),
140 new ElementRule(DataType.class)
141 ),
142
143 new ElementRule(FREQUENCIES,
144 new XMLSyntaxRule[]{new ElementRule(Parameter.class)}),
145
146 };
147
148
149 }