Debian Codegraph @

src/dr/evoxml/MutationDeathTypeParser.java

raw symbols SCIP
1 /*
2 * MutationDeathTypeParser.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.evoxml;
27
28 import dr.evolution.datatype.DataType;
29 import dr.evolution.datatype.MutationDeathType;
30 import dr.evoxml.util.DataTypeUtils;
31 import dr.xml.*;
32
33 import java.util.logging.Logger;
34
35 /**
36 *
37 */
38 public class MutationDeathTypeParser extends AbstractXMLObjectParser {
39
40 public static final String MODEL_NAME = "extendedDataType";
41 public static final String STATE = "deathState";
42 public static final String CODE = "code";
43 public static final String STATES = "states";
44 public static final String AMBIGUITY = "ambiguity";
45 public static final String EXTANT = "extantState";
46
47 public String getParserName() {
48 return MODEL_NAME;
49 }
50
51 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
52 Logger.getLogger("dr.evolution").info("\nCreating an extended data type.");
53 Logger.getLogger("dr.evolution").info("\tIf you publish results using this model, please reference Alekseyenko, Lee and Suchard (in submision).\n");
54
55 DataType dataType = DataTypeUtils.getDataType(xo);
56
57 char extantChar = '\0';
58 XMLObject cxo = xo.getChild(EXTANT);
59 if (cxo != null) {
60 extantChar = cxo.getStringAttribute(CODE).charAt(0);
61 }
62
63
64 cxo = xo.getChild(STATE);
65 char stateChar;
66 if (cxo != null) {
67 stateChar = cxo.getStringAttribute(CODE).charAt(0);
68 } else {
69 stateChar = dataType.getChar(dataType.getGapState());
70 }
71
72 Logger.getLogger("dr.evolution").info("\tNon-existent code: " + stateChar);
73
74 if (dataType == null && extantChar == '\0')
75 throw new XMLParseException("In " + xo.getName() + " you must either provide a data type or a code for extant state");
76
77 MutationDeathType mdt;
78 if (dataType != null) {
79 Logger.getLogger("dr.evolution").info("\tBase type: " + dataType.toString());
80 mdt = new MutationDeathType(dataType, stateChar);
81 } else {
82 Logger.getLogger("dr.evolution").info("\tExtant code: " + extantChar);
83 mdt = new MutationDeathType(stateChar, extantChar);
84 }
85
86 char ambiguityChar = '\0';
87 String states;
88 Object dxo;
89 for (int i = 0; i < xo.getChildCount(); ++i) {
90 dxo = xo.getChild(i);
91 if (dxo instanceof XMLObject) {
92 cxo = (XMLObject) dxo;
93 if (cxo.getName().equals(AMBIGUITY)) {
94 ambiguityChar = cxo.getStringAttribute(CODE).charAt(0);
95 if (cxo.hasAttribute(STATES)) {
96 states = cxo.getStringAttribute(STATES);
97 } else {
98 states = "";
99 }
100 mdt.addAmbiguity(ambiguityChar, states);
101 Logger.getLogger("dr.evolution").info("\tAmbiguity code: " + ambiguityChar);
102 }
103 }
104 }
105 return mdt;
106 }
107
108 //************************************************************************
109 // AbstractXMLObjectParser implementation
110 //************************************************************************
111
112 public String getParserDescription() {
113 return "This element represents an instance of the MutationDeathType which extends a base datatype with an additional \"death\" state.";
114 }
115
116 public Class getReturnType() {
117 return MutationDeathType.class;
118 }
119
120 public XMLSyntaxRule[] getSyntaxRules() {
121 return rules;
122 }
123
124 private final XMLSyntaxRule[] rules = {
125 new StringAttributeRule(DataType.DATA_TYPE, "Base datatype name", DataType.getRegisteredDataTypeNames(), true),
126 new ElementRule(DataType.class, true),
127 new ElementRule(STATE, new XMLSyntaxRule[]{AttributeRule.newStringRule(CODE)}, true),
128 new ElementRule(EXTANT, new XMLSyntaxRule[]{AttributeRule.newStringRule(CODE)}, true),
129 new ContentRule("<ambiguity code=\"Z\" states=\"XY\"/>")
130 };
131 }