Debian Codegraph @

src/dr/evoxml/MaskedPatternsParser.java

raw symbols SCIP
1 /*
2 * MaskedPatternsParser.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.SiteList;
31 import dr.evolution.alignment.SitePatterns;
32 import dr.xml.*;
33
34 import java.util.logging.Logger;
35
36 /**
37 * @author Andrew Rambaut
38 *
39 * @version $Id$
40 */
41 public class MaskedPatternsParser extends AbstractXMLObjectParser {
42
43 public static final String MASKED_PATTERNS = "maskedPatterns";
44 public static final String MASK = "mask";
45 public static final String NEGATIVE = "negative";
46
47 public String getParserName() { return MASKED_PATTERNS; }
48
49 /**
50 * Parses a patterns element and returns a patterns object.
51 */
52 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
53
54 SiteList siteList = (SiteList)xo.getChild(SiteList.class);
55
56 boolean negativeMask = xo.getBooleanAttribute(NEGATIVE);
57 String maskString = (String)xo.getElementFirstChild(MASK);
58
59 boolean[] mask = new boolean[siteList.getSiteCount()];
60 int k = 0;
61 for (char c : maskString.toCharArray()) {
62 if (Character.isDigit(c)) {
63 if (k >= mask.length) {
64 break;
65 }
66 mask[k] = (c == '0' ? negativeMask : !negativeMask);
67 k++;
68 }
69 }
70
71 if (k != mask.length) {
72 throw new XMLParseException("The mask needs to be the same length as the alignment (spaces are ignored)");
73 }
74
75 SitePatterns patterns = new SitePatterns(siteList, mask, false, false);
76
77 if (patterns == null) {
78 throw new XMLParseException("The mask needs include at least one pattern");
79 }
80
81 if (xo.hasAttribute(XMLParser.ID)) {
82 final Logger logger = Logger.getLogger("dr.evoxml");
83 logger.info("Site patterns '" + xo.getId() + "' created by masking alignment with id '" + siteList.getId() + "'");
84 logger.info(" pattern count = " + patterns.getPatternCount());
85 }
86
87 return patterns;
88 }
89
90 public XMLSyntaxRule[] getSyntaxRules() { return rules; }
91
92 private final XMLSyntaxRule[] rules = {
93 AttributeRule.newBooleanRule(NEGATIVE, true),
94 new ElementRule(SiteList.class),
95 new ElementRule(MASK, String.class, "A parameter of 1s and 0s that represent included and excluded sites")
96 };
97
98 public String getParserDescription() {
99 return "A weighted list of the unique site patterns (unique columns) in an alignment.";
100 }
101
102 public Class getReturnType() { return PatternList.class; }
103
104 }