Debian Codegraph @

src/dr/oldevomodel/lineage/LineageSitePatternsParser.java

raw symbols SCIP
1 /*
2 * LineageSitePatternsParser.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.oldevomodel.lineage;
27
28 import dr.evolution.alignment.Alignment;
29 import dr.evolution.alignment.PatternList;
30 import dr.evolution.util.TaxonList;
31 import dr.xml.*;
32
33 import java.util.logging.Logger;
34
35 /**
36 * @author Alexander Alekseyenko
37 * @author Jack O'Brien
38 */
39 public class LineageSitePatternsParser extends AbstractXMLObjectParser {
40
41 public static final String PATTERNS = "lineageSNPS";
42 public static final String FROM = "from";
43 public static final String TO = "to";
44 public static final String EVERY = "every";
45 public static final String TAXON_LIST = "taxonList";
46 public static final String STRIP = "strip";
47 public static final String UNIQUE = "unique";
48
49
50 public String getParserName() {
51 return PATTERNS;
52 }
53
54 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
55
56 Alignment alignment = (Alignment) xo.getChild(Alignment.class);
57 TaxonList taxa = null;
58
59 int from = 0;
60 int to = -1;
61 int every = xo.getAttribute(EVERY, 1);
62
63 boolean strip = xo.getAttribute(STRIP, true);
64
65 boolean unique = xo.getAttribute(UNIQUE, true);
66
67 if (xo.hasAttribute(FROM)) {
68 from = xo.getIntegerAttribute(FROM) - 1;
69 if (from < 0)
70 throw new XMLParseException("illegal 'from' attribute in patterns element");
71 }
72
73 if (xo.hasAttribute(TO)) {
74 to = xo.getIntegerAttribute(TO) - 1;
75 if (to < 0 || to < from)
76 throw new XMLParseException("illegal 'to' attribute in patterns element");
77 }
78
79 if (every <= 0) throw new XMLParseException("illegal 'every' attribute in patterns element");
80
81 if (xo.hasChildNamed(TAXON_LIST)) {
82 taxa = (TaxonList) xo.getElementFirstChild(TAXON_LIST);
83 }
84
85 if (from > alignment.getSiteCount())
86 throw new XMLParseException("illegal 'from' attribute in patterns element");
87
88 if (to > alignment.getSiteCount())
89 throw new XMLParseException("illegal 'to' attribute in patterns element");
90
91 LineageSitePatterns patterns = new LineageSitePatterns(alignment, taxa, from, to, every, strip, unique);
92
93 int f = from + 1;
94 int t = to + 1; // fixed a *display* error by adding + 1 for consistency with f = from + 1
95 if (to == -1) t = alignment.getSiteCount();
96
97 if (xo.hasAttribute(XMLParser.ID)) {
98 final Logger logger = Logger.getLogger("dr.evoxml");
99 logger.info("Site patterns '" + xo.getId() + "' created from positions " +
100 Integer.toString(f) + "-" + Integer.toString(t) +
101 " of alignment '" + alignment.getId() + "'");
102
103 if (every > 1) {
104 logger.info(" only using every " + every + " site");
105 }
106 logger.info(" pattern count = " + patterns.getPatternCount());
107 }
108
109 return patterns;
110 }
111
112 //************************************************************************
113 // AbstractXMLObjectParser implementation
114 //************************************************************************
115
116 public XMLSyntaxRule[] getSyntaxRules() {
117 return rules;
118 }
119
120 private final XMLSyntaxRule[] rules = {
121 AttributeRule.newIntegerRule(FROM, true, "The site position to start at, default is 1 (the first position)"),
122 AttributeRule.newIntegerRule(TO, true, "The site position to finish at, must be greater than <b>" + FROM + "</b>, default is length of given alignment"),
123 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)"),
124 new ElementRule(TAXON_LIST,
125 new XMLSyntaxRule[]{new ElementRule(TaxonList.class)}, true),
126 new ElementRule(Alignment.class),
127 AttributeRule.newBooleanRule(STRIP, true, "Strip out completely ambiguous sites"),
128 AttributeRule.newBooleanRule(UNIQUE, true, "Return a weight list of unique patterns"),
129 };
130
131 public String getParserDescription() {
132 return "A weighted list of the unique site patterns (unique columns) in an alignment.";
133 }
134
135 public Class getReturnType() {
136 return PatternList.class;
137 }
138
139 }