Debian Codegraph @

src/dr/evoxml/SitePatternsParser.java

raw symbols SCIP
1 /*
2 * SitePatternsParser.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.Alignment;
29 import dr.evolution.alignment.PatternList;
30 import dr.evolution.alignment.SitePatterns;
31 import dr.evolution.util.TaxonList;
32 import dr.inference.model.Parameter;
33 import dr.xml.*;
34
35 import java.util.logging.Logger;
36
37 /**
38 * @author Alexei Drummond
39 * @author Andrew Rambaut
40 * @author Marc A. Suchard
41 * @version $Id: SitePatternsParser.java,v 1.3 2005/07/11 14:06:25 rambaut Exp $
42 */
43 public class SitePatternsParser extends AbstractXMLObjectParser {
44
45 public static final String PATTERNS = "patterns";
46 public static final String FROM = "from";
47 public static final String TO = "to";
48 public static final String EVERY = "every";
49 public static final String TAXON_LIST = "taxonList";
50 public static final String STRIP = "strip";
51 public static final String UNIQUE = "unique";
52 public static final String CONSTANT_PATTERNS = "constantPatterns";
53
54
55 public String getParserName() {
56 return PATTERNS;
57 }
58
59 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
60
61 Alignment alignment = (Alignment) xo.getChild(Alignment.class);
62 TaxonList taxa = null;
63
64 int from = 0;
65 int to = -1;
66 int every = xo.getAttribute(EVERY, 1);
67
68 boolean strip = xo.getAttribute(STRIP, true);
69
70 boolean unique = xo.getAttribute(UNIQUE, true);
71
72 if (xo.hasAttribute(FROM)) {
73 from = xo.getIntegerAttribute(FROM) - 1;
74 if (from < 0)
75 throw new XMLParseException("illegal 'from' attribute in patterns element");
76 }
77
78 if (xo.hasAttribute(TO)) {
79 to = xo.getIntegerAttribute(TO) - 1;
80 if (to < 0 || to < from)
81 throw new XMLParseException("illegal 'to' attribute in patterns element");
82 }
83
84 if (every <= 0) throw new XMLParseException("illegal 'every' attribute in patterns element");
85
86 if (xo.hasChildNamed(TAXON_LIST)) {
87 taxa = (TaxonList) xo.getElementFirstChild(TAXON_LIST);
88 }
89
90 int[] constantPatternCounts = null;
91 if (xo.hasChildNamed(CONSTANT_PATTERNS)) {
92 Parameter param = (Parameter) xo.getElementFirstChild(CONSTANT_PATTERNS);
93 if (param.getDimension() != alignment.getStateCount()) {
94 throw new XMLParseException("The " + CONSTANT_PATTERNS + " parameter length should be equal to the number of states");
95 }
96 constantPatternCounts = new int[param.getDimension()];
97 int i = 0;
98 for (double value : param.getParameterValues()) {
99 constantPatternCounts[i] = (int)value;
100 i++;
101 }
102 }
103
104 if (from > alignment.getSiteCount()) {
105 throw new XMLParseException("illegal 'from' attribute in patterns element (selected attribute = " + from + " vs. actual site count = " + alignment.getSiteCount() + ")");
106 }
107
108 if (to > alignment.getSiteCount()) {
109 throw new XMLParseException("illegal 'to' attribute in patterns element (selected attribute = " + to + " vs. actual site count = " + alignment.getSiteCount() + ")");
110 }
111
112 SitePatterns patterns = new SitePatterns(alignment, taxa, from, to, every, strip, unique, constantPatternCounts);
113
114 int f = from + 1;
115 int t = to + 1; // fixed a *display* error by adding + 1 for consistency with f = from + 1
116 if (to == -1) t = alignment.getSiteCount();
117
118 if (xo.hasAttribute(XMLParser.ID)) {
119 final Logger logger = Logger.getLogger("dr.evoxml");
120 logger.info("Site patterns '" + xo.getId() + "' created from positions " +
121 Integer.toString(f) + "-" + Integer.toString(t) +
122 " of alignment '" + alignment.getId() + "'");
123
124 if (every > 1) {
125 logger.info(" only using every " + every + " site");
126 }
127 logger.info(" " + (unique ? "unique ": "") + "pattern count = " + patterns.getPatternCount());
128 }
129
130 return patterns;
131 }
132
133 //************************************************************************
134 // AbstractXMLObjectParser implementation
135 //************************************************************************
136
137 public XMLSyntaxRule[] getSyntaxRules() {
138 return rules;
139 }
140
141 private final XMLSyntaxRule[] rules = {
142 AttributeRule.newIntegerRule(FROM, true, "The site position to start at, default is 1 (the first position)"),
143 AttributeRule.newIntegerRule(TO, true, "The site position to finish at, must be greater than <b>" + FROM + "</b>, default is length of given alignment"),
144 AttributeRule.newIntegerRule(EVERY, true, "Determines how many sites are selected. A value of 3 will select every third site starting from <b>" + FROM + "</b>, default is 1 (every site)"),
145 new ElementRule(TAXON_LIST,
146 new XMLSyntaxRule[]{new ElementRule(TaxonList.class)}, true),
147 new ElementRule(CONSTANT_PATTERNS,
148 new XMLSyntaxRule[]{new ElementRule(Parameter.class)}, true),
149 new ElementRule(Alignment.class),
150 AttributeRule.newBooleanRule(STRIP, true, "Strip out completely ambiguous sites"),
151 AttributeRule.newBooleanRule(UNIQUE, true, "Return a weight list of unique patterns"),
152 };
153
154 public String getParserDescription() {
155 return "A weighted list of the unique site patterns (unique columns) in an alignment.";
156 }
157
158 public Class getReturnType() {
159 return PatternList.class;
160 }
161
162 }