Debian Codegraph @

src/dr/evoxml/AttributePatternsParser.java

raw symbols SCIP
1 /*
2 * AttributePatternsParser.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.alignment.PatternList;
29 import dr.evolution.alignment.Patterns;
30 import dr.evolution.alignment.SimpleSiteList;
31 import dr.evolution.alignment.SitePatterns;
32 import dr.evolution.datatype.DataType;
33 import dr.evolution.util.Taxon;
34 import dr.evolution.util.TaxonList;
35 import dr.evoxml.util.DataTypeUtils;
36 import dr.xml.*;
37
38 import java.util.logging.Logger;
39
40 /**
41 * @author Alexei Drummond
42 * @author Andrew Rambaut
43 *
44 * @version $Id: SitePatternsParser.java,v 1.3 2005/07/11 14:06:25 rambaut Exp $
45 */
46 public class AttributePatternsParser extends AbstractXMLObjectParser {
47
48 public static final String ATTRIBUTE = "attribute";
49 public static final String SECONDARY_ATTRIBUTE = "secondary";
50
51 public static final String PATTERNS = "Patterns";
52 public static final String ATTRIBUTE_PATTERNS = ATTRIBUTE + PATTERNS;
53
54 public String getParserName() { return ATTRIBUTE_PATTERNS; }
55
56 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
57
58 String attributeName = xo.getStringAttribute(ATTRIBUTE);
59 String secondaryAttributeName = xo.getAttribute(SECONDARY_ATTRIBUTE, (String)null);
60 TaxonList taxa = (TaxonList)xo.getChild(TaxonList.class);
61 DataType dataType = DataTypeUtils.getDataType(xo);
62
63 if (dataType == null) {
64 throw new XMLParseException("dataType expected for attributePatterns element");
65 }
66
67 // using a SimpleSiteList rather than Patterns to allow ancestral reconstruction
68 SimpleSiteList patterns = new SimpleSiteList(dataType, taxa);
69
70 int[] pattern = new int[taxa.getTaxonCount()];
71
72 boolean attributeFound = false;
73
74 for (int i = 0; i < taxa.getTaxonCount(); i++) {
75 Taxon taxon = taxa.getTaxon(i);
76
77 if (secondaryAttributeName == null || secondaryAttributeName.isEmpty()) {
78 Object value = taxon.getAttribute(attributeName);
79
80 if (value != null) {
81 int state = dataType.getState(value.toString());
82 if (state < 0) {
83 throw new XMLParseException("State for attribute, " + attributeName + ", in taxon, " + taxon.getId() + ", is unknown: " + value.toString());
84 }
85 pattern[i] = state;
86 attributeFound = true;
87 } else {
88 pattern[i] = dataType.getUnknownState();
89 }
90 } else {
91 Object value1 = taxon.getAttribute(attributeName);
92 Object value2 = taxon.getAttribute(secondaryAttributeName);
93
94 if (value1 != null && value2 != null) {
95 String code = value1.toString() + CompositeDataTypeParser.COMPOSITE_STATE_SEPARATOR + value2.toString();
96 int state = dataType.getState(code);
97 if (state < 0) {
98 throw new XMLParseException("State for attributes, " + attributeName + " & " + secondaryAttributeName + ", in taxon, " + taxon.getId() + ", is unknown: " + code);
99 }
100 pattern[i] = state;
101 attributeFound = true;
102 } else {
103 pattern[i] = dataType.getUnknownState();
104 }
105 }
106 }
107
108 if (!attributeFound) {
109 throw new XMLParseException("The attribute, " + attributeName + " was missing in all taxa. Check the name of the attribute.");
110 }
111
112 patterns.addPattern(pattern);
113
114 if (xo.hasAttribute(XMLParser.ID)) {
115 Logger.getLogger("dr.evoxml").info("Read attribute patterns, '" + xo.getId() + "' for attribute, "+ attributeName);
116 } else {
117 Logger.getLogger("dr.evoxml").info("Read attribute patterns for attribute, "+ attributeName);
118 }
119
120 return patterns;
121 }
122
123 //************************************************************************
124 // AbstractXMLObjectParser implementation
125 //************************************************************************
126
127 public XMLSyntaxRule[] getSyntaxRules() { return rules; }
128
129 private XMLSyntaxRule[] rules = new XMLSyntaxRule[] {
130 new XORRule(
131 new StringAttributeRule(
132 DataType.DATA_TYPE,
133 "The data type",
134 DataType.getRegisteredDataTypeNames(), false),
135 new ElementRule(DataType.class)
136 ),
137 AttributeRule.newStringRule(ATTRIBUTE),
138 AttributeRule.newStringRule(SECONDARY_ATTRIBUTE, true),
139 new ElementRule(TaxonList.class, "The taxon set")
140 };
141
142 public String getParserDescription() {
143 return "A site pattern defined by an attribute in a set of taxa.";
144 }
145
146 public Class getReturnType() { return PatternList.class; }
147
148 }