Debian Codegraph @

src/dr/evomodelxml/branchmodel/BranchAssignmentModelParser.java

raw symbols SCIP
1 /*
2 * BranchAssignmentModelParser.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.branchmodel;
27
28 import java.util.LinkedHashMap;
29 import java.util.logging.Logger;
30
31 import dr.evomodel.branchmodel.BranchAssignmentModel;
32 import dr.evomodel.substmodel.SubstitutionModel;
33 import dr.evomodel.tree.TreeModel;
34 import dr.xml.AbstractXMLObjectParser;
35 import dr.xml.AttributeRule;
36 import dr.xml.ElementRule;
37 import dr.xml.XMLObject;
38 import dr.xml.XMLParseException;
39 import dr.xml.XMLSyntaxRule;
40
41 public class BranchAssignmentModelParser extends AbstractXMLObjectParser {
42
43 public static final String ANNOTATION = "annotation";
44 public static final String ANNOTATION_VALUE = "annotationValue";
45 public static final String BASE_MODEL = "baseModel";
46 public static final String ASSIGNMENT = "assignment";
47
48 @Override
49 public String getParserName() {
50 return BranchAssignmentModel.BRANCH_ASSIGNMENT_MODEL;
51 }
52
53 @Override
54 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
55
56 Logger.getLogger("dr.evomodel").info(
57 "\nUsing branch assignment branch model.");
58 TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
59 String annotation = xo.getStringAttribute(ANNOTATION);
60
61 LinkedHashMap<Integer, SubstitutionModel> modelIndexMap = new LinkedHashMap<Integer, SubstitutionModel>();
62
63 for (int i = 0; i < xo.getChildCount(); i++) {
64 if (xo.getChild(i) instanceof XMLObject) {
65 XMLObject xoc = (XMLObject) xo.getChild(i);
66 if (xoc.getName().equals(ASSIGNMENT)) {
67
68 Integer index = null;
69 if (xoc.hasAttribute(ANNOTATION_VALUE)) {
70 index = xoc.getIntegerAttribute(ANNOTATION_VALUE);
71 }
72
73 SubstitutionModel model = (SubstitutionModel) xoc
74 .getChild(SubstitutionModel.class);
75
76 modelIndexMap.put(index, model);
77
78 }
79 }
80 }
81
82 SubstitutionModel baseModel = (SubstitutionModel) xo
83 .getElementFirstChild(BASE_MODEL);
84
85 return new BranchAssignmentModel(treeModel, annotation, modelIndexMap,
86 baseModel);
87 }// END: parseXMLObject
88
89 @Override
90 public XMLSyntaxRule[] getSyntaxRules() {
91
92 return new XMLSyntaxRule[] {
93
94 new ElementRule(TreeModel.class, false), //
95 new ElementRule(ASSIGNMENT, //
96 new XMLSyntaxRule[] { //
97 AttributeRule.newIntegerRule(ANNOTATION_VALUE,
98 false), //
99 new ElementRule(SubstitutionModel.class, false), // model
100 },//
101 1, Integer.MAX_VALUE), //
102 new ElementRule(BASE_MODEL, //
103 new XMLSyntaxRule[] { //
104 new ElementRule(SubstitutionModel.class, false) //
105 }, false) // base model
106
107 };
108 }// END: getSyntaxRules
109
110 @Override
111 public String getParserDescription() {
112 return "This element provides a branch model which assigns "
113 + "substitution models to branches on the tree ";
114 }
115
116 @SuppressWarnings("rawtypes")
117 @Override
118 public Class getReturnType() {
119 return BranchAssignmentModelParser.class;
120 }
121
122 }// END: class