Debian Codegraph @

src/dr/evoxml/GeneralDataTypeParser.java

raw symbols SCIP
1 /*
2 * GeneralDataTypeParser.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.GeneralDataType;
30 import dr.xml.*;
31 import dr.util.Attribute;
32 import dr.util.Identifiable;
33
34 import java.util.ArrayList;
35 import java.util.List;
36
37 /**
38 * The XML for a nucleotide data type under this scheme would be:
39 * <generalDataType id="nucleotides">
40 * <state code="A"/>
41 * <state code="C"/>
42 * <state code="G"/>
43 * <state code="T"/>
44 * <alias code="U" state="T"/>
45 * <ambiguity code="R" states="AG"/>
46 * <ambiguity code="Y" states="CT"/>
47 * <ambiguity code="?" states="ACGT"/>
48 * <ambiguity code="-" states="ACGT"/>
49 * </generalDataType>
50 *
51 * @author Andrew Rambaut
52 * @author Alexei Drummond
53 *
54 * @version $Id: GeneralDataTypeParser.java,v 1.2 2005/05/24 20:25:59 rambaut Exp $
55 */
56 public class GeneralDataTypeParser extends AbstractXMLObjectParser {
57
58 /**
59 * Name of data type. For XML and human reading of data type.
60 */
61 public static final String GENERAL_DATA_TYPE = "generalDataType";
62 public static final String STATE = "state";
63 public static final String STATES = "states";
64 public static final String ALIAS = "alias";
65 public static final String AMBIGUITY = "ambiguity";
66 public static final String CODE = "code";
67
68 public String getParserName() { return GENERAL_DATA_TYPE; }
69
70 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
71
72 List<String> states = new ArrayList<String>();
73
74 for (int i =0; i < xo.getChildCount(); i++) {
75 if (xo.getChild(i) instanceof XMLObject) {
76 XMLObject cxo = (XMLObject)xo.getChild(i);
77
78 if (cxo.getName().equals(STATE)) {
79 states.add(cxo.getStringAttribute(CODE));
80 } else if (cxo.getName().equals(ALIAS)) {
81 // Do nothing just now
82 } else if (cxo.getName().equals(AMBIGUITY)) {
83 // Do nothing just now
84 } else {
85 throw new XMLParseException("illegal element, " + cxo.getName() + ", in " + getParserName() + " element");
86 }
87 } else if (xo.getChild(i) instanceof Identifiable) {
88 states.add(((Identifiable)xo.getChild(i)).getId());
89 } else {
90 throw new XMLParseException("illegal element in " + getParserName() + " element");
91 }
92 }
93
94 if (states.size() == 0) {
95 throw new XMLParseException("No state elements defined in " + getParserName() + " element");
96 } else if (states.size() < 2 ) {
97 throw new XMLParseException("Less than two state elements defined in " + getParserName() + " element");
98 }
99
100 GeneralDataType dataType = new GeneralDataType(states);
101
102 for (int i =0; i < xo.getChildCount(); i++) {
103 if (xo.getChild(i) instanceof XMLObject) {
104 XMLObject cxo = (XMLObject)xo.getChild(i);
105 if (cxo.getName().equals(ALIAS)) {
106
107 String alias = cxo.getStringAttribute(CODE);
108 // if (alias.length() != 1) {
109 // throw new XMLParseException("State alias codes in " + getParserName() + " element must be exactly one character");
110 // }
111
112 String state = cxo.getStringAttribute(STATE);
113 // if (state.length() != 1) {
114 // throw new XMLParseException("State codes in " + getParserName() + " element must be exactly one character");
115 // }
116
117 try {
118 dataType.addAlias(alias, state);
119 } catch (IllegalArgumentException iae) {
120 throw new XMLParseException(iae.getMessage() + "in " + getParserName() + " element");
121 }
122
123 } else if (cxo.getName().equals(AMBIGUITY)) {
124
125 String code = cxo.getStringAttribute(CODE);
126 // if (code.length() != 1) {
127 // throw new XMLParseException("State ambiguity codes in " + getParserName() + " element must be exactly one character");
128 // }
129
130 String[] ambiguities = cxo.getStringArrayAttribute(STATES);
131 if (ambiguities.length == 1) {
132 String codes = ambiguities[0];
133 if (codes.length() < 2) {
134 throw new XMLParseException("States for ambiguity code in " + getParserName() + " element are not ambiguous");
135 }
136 ambiguities = new String[codes.length()];
137 for (int j = 0; j < codes.length(); j++) {
138 ambiguities[j] = String.valueOf(codes.charAt(j));
139 }
140 }
141
142 try {
143 dataType.addAmbiguity(code, ambiguities);
144 } catch (IllegalArgumentException iae) {
145 throw new XMLParseException(iae.getMessage() + "in " + getParserName() + " element");
146 }
147
148 }
149 }
150 }
151
152 return dataType;
153 }
154
155 //************************************************************************
156 // AbstractXMLObjectParser implementation
157 //************************************************************************
158
159 public String getParserDescription() {
160 return "Defines a general DataType for any number of states";
161 }
162
163 public String getExample() {
164 return "<!-- The XML for a nucleotide data type under this scheme would be -->\n"+
165 "<generalDataType id=\"nucleotides\">\n"+
166 " <state code=\"A\"/>\n"+
167 " <state code=\"C\"/>\n"+
168 " <state code=\"G\"/>\n"+
169 " <state code=\"T\"/>\n"+
170 " <alias code=\"U\" state=\"T\"/>\n"+
171 " <ambiguity code=\"R\" states=\"AG\"/>\n"+
172 " <ambiguity code=\"Y\" states=\"CT\"/>\n"+
173 " <ambiguity code=\"?\" states=\"ACGT\"/>\n"+
174 " <ambiguity code=\"-\" states=\"ACGT\"/>\n"+
175 "</generalDataType>\n";
176 }
177
178 public Class getReturnType() { return DataType.class; }
179
180 public XMLSyntaxRule[] getSyntaxRules() { return rules; }
181
182 private XMLSyntaxRule[] rules = new XMLSyntaxRule[] {
183 new ElementRule(Identifiable.class, 0, Integer.MAX_VALUE),
184 new ContentRule("<state code=\"X\"/>"),
185 new ContentRule("<alias code=\"Y\" state=\"X\"/>"),
186 new ContentRule("<ambiguity code=\"Z\" states=\"XY\"/>")
187 };
188 }