Debian Codegraph @

src/dr/oldevomodelxml/substmodel/MsatBMAParser.java

raw symbols SCIP
1 /*
2 * MsatBMAParser.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.xml.*;
29 import dr.evolution.datatype.Microsatellite;
30 import dr.inference.model.Parameter;
31 import dr.oldevomodel.substmodel.MsatBMA;
32
33 import java.util.HashMap;
34 import java.util.ArrayList;
35
36 /**
37 * @author Chieh-Hsi Wu
38 *
39 * Parser of MsatAveragingSubsetModel
40 */
41 public class MsatBMAParser extends AbstractXMLObjectParser{
42
43 public static final String MODELS = "models";
44 public static final String MODEL = "model";
45 public static final String BINARY = "binary";
46 public static final String CODE = "code";
47 public static final String LOGIT = "logit";
48 public static final String RATE_PROPS = "rateProps";
49 public static final String RATE_PROP = "rateProp";
50 public static final String RATE_QUADS = "rateQuads";
51 public static final String RATE_QUAD = "rateQuad";
52 public static final String BIAS_CONSTS = "biasConsts";
53 public static final String BIAS_CONST = "biasConst";
54 public static final String BIAS_LINS = "biasLins";
55 public static final String BIAS_LIN = "biasLin";
56 public static final String GEOS = "geos";
57 public static final String GEO = "geo";
58 public static final String PHASE_PROBS = "phaseProbs";
59 public static final String PHASE_PROB = "phaseProb";
60 public static final String IN_MODELS = "inModels";
61 public static final String MODEL_CHOOSE = "modelChoose";
62 public static final String MODEL_INDICATOR = "modelIndicator";
63 public static final String MSAT_BMA = "msatBMA";
64 public static final int PROP_RATES_MAX_COUNT = 6;
65 public static final int BIAS_CONST_MAX_COUNT = 8;
66 public static final int BIAS_LIN_MAX_COUNT = 4;
67 public static final int GEO_MAX_COUNT = 6;
68
69
70
71 public String getParserName(){
72 return MSAT_BMA;
73 }
74
75
76 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
77
78 //get microsatellite data type
79 Microsatellite dataType = (Microsatellite)xo.getChild(Microsatellite.class);
80
81 //whether mutational bias is in logit space
82 boolean logit = xo.getAttribute(LOGIT,true);
83
84 XMLObject modelsXO = xo.getChild(MODELS);
85 int modelCount = modelsXO.getChildCount();
86
87 HashMap<Integer, Integer> modelBitIndMap = new HashMap<Integer, Integer>(modelCount);
88 for(int i = 0; i < modelCount; i++){
89
90 XMLObject modelXO = (XMLObject)modelsXO.getChild(i);
91 String bitVec = modelXO.getStringAttribute(BINARY);
92 int bitVecVal = Integer.parseInt(bitVec,2);
93 int modelCode = modelXO.getIntegerAttribute(CODE);
94 modelBitIndMap.put(bitVecVal,modelCode);
95
96 }
97
98 Parameter[][] paramModelMap = new Parameter[6][modelCount];
99
100
101
102 XMLObject propRatesXO = xo.getChild(RATE_PROPS);
103 ArrayList<Parameter> rateProps =
104 processParameters(
105 propRatesXO,
106 paramModelMap,
107 MsatBMA.PROP_INDEX
108 );
109
110 XMLObject quadRatesXO = xo.getChild(RATE_QUADS);
111 ArrayList<Parameter> rateQuads =
112 processParameters(
113 quadRatesXO,
114 paramModelMap,
115 MsatBMA.QUAD_INDEX
116 );
117
118 XMLObject biasConstsXO = xo.getChild(BIAS_CONSTS);
119 ArrayList<Parameter> biasConsts =
120 processParameters(
121 biasConstsXO,
122 paramModelMap,
123 MsatBMA.BIAS_CONST_INDEX
124 );
125
126 XMLObject biasLinsXO = xo.getChild(BIAS_LINS);
127 ArrayList<Parameter> biasLins =
128 processParameters(
129 biasLinsXO,
130 paramModelMap,
131 MsatBMA.BIAS_LIN_INDEX
132 );
133
134 XMLObject geosXO = xo.getChild(GEOS);
135 ArrayList<Parameter> geos =
136 processParameters(
137 geosXO,
138 paramModelMap,
139 MsatBMA.GEO_INDEX
140 );
141
142 XMLObject phaseProbXO = xo.getChild(PHASE_PROBS);
143 ArrayList<Parameter> phaseProbs =
144 processParameters(
145 phaseProbXO,
146 paramModelMap,
147 MsatBMA.PHASE_PROB_INDEX
148 );
149
150 Parameter modelChoose = (Parameter) xo.getElementFirstChild(MODEL_CHOOSE);
151 Parameter modelIndicator = (Parameter) xo.getElementFirstChild(MODEL_INDICATOR);
152
153 printParameters(paramModelMap);
154 return new MsatBMA(
155 dataType,
156 logit,
157 rateProps,
158 rateQuads,
159 biasConsts,
160 biasLins,
161 geos,
162 phaseProbs,
163 paramModelMap,
164 modelChoose,
165 modelIndicator,
166 modelBitIndMap
167 );
168 }
169
170 public ArrayList<Parameter> processParameters(
171 XMLObject paramsXO,
172 Parameter[][] paramModelMap,
173 int paramIndex)throws XMLParseException{
174
175 ArrayList<Parameter> paramList = new ArrayList<Parameter>();
176 int paramsCount = paramsXO.getChildCount();
177
178 for(int i = 0; i < paramsCount; i++){
179
180 XMLObject paramXO = (XMLObject) paramsXO.getChild(i);
181 int[] inModels = paramXO.getIntegerArrayAttribute(IN_MODELS);
182 Parameter param = (Parameter)paramXO.getChild(Parameter.class);
183
184 for(int j = 0; j < inModels.length; j++){
185 if(paramModelMap[paramIndex][inModels[j]] == null){
186 paramModelMap[paramIndex][inModels[j]] = param;
187 }else{
188 throw new RuntimeException("Different objects cannot be assigned to the same parameter in a model");
189 }
190 }
191
192 paramList.add(param);
193
194 }
195 return paramList;
196 }
197
198 public void printParameters(Parameter[][] paramModelMap){
199 for(int i = 0; i < paramModelMap.length; i++){
200 for(int j = 0; j < paramModelMap[i].length; j++){
201 System.out.print(paramModelMap[i][j]+" ");
202 }
203 System.out.println();
204 }
205 }
206
207
208
209
210 public XMLSyntaxRule[] getSyntaxRules() {
211 return rules;
212 }
213
214 private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
215 new ElementRule(Microsatellite.class),
216 AttributeRule.newBooleanRule(LOGIT,true),
217 new ElementRule(
218 MODELS,
219 new XMLSyntaxRule[]{
220 new ElementRule(
221 MODEL,
222 new XMLSyntaxRule[]{
223 AttributeRule.newStringRule(BINARY),
224 AttributeRule.newStringRule(CODE)
225 },
226 1,
227 27
228 )
229 }
230 ),
231 new ElementRule(
232 RATE_PROPS,
233 new XMLSyntaxRule[]{
234 new ElementRule(
235 RATE_PROP,
236 new XMLSyntaxRule[]{
237 AttributeRule.newIntegerArrayRule(IN_MODELS, false),
238 new ElementRule(Parameter.class)
239 },
240 1,
241 18
242 )
243 }
244 ),
245 new ElementRule(
246 RATE_QUADS,
247 new XMLSyntaxRule[]{
248 new ElementRule(
249 RATE_QUAD,
250 new XMLSyntaxRule[]{
251 AttributeRule.newIntegerArrayRule(IN_MODELS, false),
252 new ElementRule(Parameter.class)
253 },
254 1,
255 9
256 )
257 }
258 ),
259 new ElementRule(
260 BIAS_CONSTS,
261 new XMLSyntaxRule[]{
262 new ElementRule(
263 BIAS_CONST,
264 new XMLSyntaxRule[]{
265 AttributeRule.newIntegerArrayRule(IN_MODELS, false),
266 new ElementRule(Parameter.class)
267 },
268 1,
269 18
270 )
271 }
272 ),
273 new ElementRule(
274 BIAS_LINS,
275 new XMLSyntaxRule[]{
276 new ElementRule(
277 BIAS_LIN,
278 new XMLSyntaxRule[]{
279 AttributeRule.newIntegerArrayRule(IN_MODELS, false),
280 new ElementRule(Parameter.class)
281 },
282 1,
283 9
284 )
285 }
286 ),
287 new ElementRule(
288 GEOS,
289 new XMLSyntaxRule[]{
290 new ElementRule(
291 GEO,
292 new XMLSyntaxRule[]{
293 AttributeRule.newIntegerArrayRule(IN_MODELS, false),
294 new ElementRule(Parameter.class)
295 },
296 1,
297 18
298 )
299 }
300 ),
301 new ElementRule(
302 PHASE_PROBS,
303 new XMLSyntaxRule[]{
304 new ElementRule(
305 PHASE_PROB,
306 new XMLSyntaxRule[]{
307 AttributeRule.newIntegerArrayRule(IN_MODELS, false),
308 new ElementRule(Parameter.class)
309 },
310 1,
311 9
312 )
313 }
314 ),
315 new ElementRule(MODEL_CHOOSE, new XMLSyntaxRule[]{new ElementRule(Parameter.class)}),
316 new ElementRule(MODEL_INDICATOR, new XMLSyntaxRule[]{new ElementRule(Parameter.class)})
317
318 };
319
320 public String getParserDescription() {
321 return "This element represents an instance of the Microsatellite Averaging Model of microsatellite evolution.";
322 }
323
324 public Class getReturnType(){
325 return MsatBMA.class;
326 }
327 }