Debian Codegraph @

src/dr/oldevomodelxml/substmodel/SubstitutionEpochModelParser.java

raw symbols SCIP
1 /*
2 * SubstitutionEpochModelParser.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.DataType;
29 import dr.oldevomodel.substmodel.AbstractSubstitutionModel;
30 import dr.oldevomodel.substmodel.FrequencyModel;
31 import dr.oldevomodel.substmodel.SubstitutionEpochModel;
32 import dr.oldevomodel.substmodel.SubstitutionModel;
33 import dr.inference.model.Parameter;
34 import dr.xml.*;
35
36 import java.util.ArrayList;
37 import java.util.List;
38
39 /**
40 */
41 public class SubstitutionEpochModelParser extends AbstractXMLObjectParser {
42
43 public static final String SUBSTITUTION_EPOCH_MODEL = "substitutionEpochModel";
44 public static final String MODELS = "models";
45 public static final String TRANSITION_TIMES = "transitionTimes";
46
47 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
48
49 DataType dataType = null;
50 FrequencyModel freqModel = null;
51 List<SubstitutionModel> modelList = new ArrayList<SubstitutionModel>();
52 XMLObject cxo = xo.getChild(MODELS);
53 for (int i = 0; i < cxo.getChildCount(); i++) {
54 SubstitutionModel model = (SubstitutionModel) cxo.getChild(i);
55
56 if (dataType == null) {
57 dataType = model.getDataType();
58 } else if (dataType != model.getDataType())
59 throw new XMLParseException("Substitution models across epoches must use the same data type.");
60
61 if (freqModel == null) {
62 freqModel = model.getFrequencyModel();
63 } else if (freqModel != model.getFrequencyModel())
64 throw new XMLParseException("Substitution models across epoches must currently use the same frequency model.\nHarass Marc to fix this.");
65
66 modelList.add(model);
67 }
68
69 Parameter transitionTimes = (Parameter) xo.getChild(Parameter.class);
70
71 if (transitionTimes.getDimension() != modelList.size() - 1) {
72 throw new XMLParseException("# of transition times must equal # of substitution models - 1\n" + transitionTimes.getDimension() + "\n" + modelList.size());
73 }
74
75 return new SubstitutionEpochModel(SUBSTITUTION_EPOCH_MODEL, modelList, transitionTimes, dataType, freqModel);
76 }
77
78 public XMLSyntaxRule[] getSyntaxRules() {
79 return new XMLSyntaxRule[]{
80 new ElementRule(MODELS,
81 new XMLSyntaxRule[]{
82 new ElementRule(AbstractSubstitutionModel.class, 1, Integer.MAX_VALUE),
83 }),
84 new ElementRule(Parameter.class),
85 };
86 }
87
88 public String getParserDescription() {
89 return null;
90 }
91
92 public Class getReturnType() {
93 return SubstitutionEpochModel.class;
94 }
95
96 public String getParserName() {
97 return SUBSTITUTION_EPOCH_MODEL;
98 }
99 }