Debian Codegraph @

src/dr/evoxml/AlignmentParser.java

raw symbols SCIP
1 /*
2 * AlignmentParser.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.SimpleAlignment;
30 import dr.evolution.datatype.DataType;
31 import dr.evolution.datatype.Nucleotides;
32 import dr.evolution.sequence.Sequence;
33 import dr.evolution.sequence.UncertainSequence;
34 import dr.evoxml.util.DataTypeUtils;
35 import dr.xml.*;
36
37 import java.util.logging.Logger;
38
39 /**
40 * @author Alexei Drummond
41 * @author Andrew Rambaut
42 * @version $Id: AlignmentParser.java,v 1.3 2005/07/11 14:06:25 rambaut Exp $
43 */
44 public class AlignmentParser extends AbstractXMLObjectParser {
45
46 public static final String ALIGNMENT = "alignment";
47
48 public String getParserName() {
49 return ALIGNMENT;
50 }
51
52 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
53
54 final SimpleAlignment alignment = new SimpleAlignment();
55
56 final DataType dataType = DataTypeUtils.getDataType(xo);
57
58 if (dataType == null) {
59 throw new XMLParseException("dataType attribute expected for alignment element");
60 }
61
62 alignment.setDataType(dataType);
63
64 for (int i = 0; i < xo.getChildCount(); i++) {
65
66 final Object child = xo.getChild(i);
67 if (child instanceof UncertainSequence) {
68 alignment.addSequence((UncertainSequence) child);
69 } else if (child instanceof Sequence) {
70 alignment.addSequence((Sequence) child);
71 } else if (child instanceof DataType) {
72 // already dealt with
73 } else {
74 throw new XMLParseException("Unknown child element found in alignment");
75 }
76 }
77
78 final Logger logger = Logger.getLogger("dr.evoxml");
79 logger.info("\nRead alignment" + (xo.hasAttribute(XMLParser.ID) ? ": " + xo.getId() : "") +
80 "\n Sequences = " + alignment.getSequenceCount() +
81 "\n Sites = " + alignment.getSiteCount() +
82 "\n Datatype = " + alignment.getDataType().getDescription());
83
84 return alignment;
85 }
86
87 public String getParserDescription() {
88 return "This element represents an alignment of molecular sequences.";
89 }
90
91 public Class getReturnType() {
92 return Alignment.class;
93 }
94
95 public String getExample() {
96
97 return
98 "<!-- An alignment of three short DNA sequences -->\n" +
99 "<alignment missing=\"-?\" dataType=\"" + Nucleotides.DESCRIPTION + "\">\n" +
100 " <sequence>\n" +
101 " <taxon idref=\"taxon1\"/>\n" +
102 " ACGACTAGCATCGAGCTTCG--GATAGCAGGC\n" +
103 " </sequence>\n" +
104 " <sequence>\n" +
105 " <taxon idref=\"taxon2\"/>\n" +
106 " ACGACTAGCATCGAGCTTCGG-GATAGCATGC\n" +
107 " </sequence>\n" +
108 " <sequence>\n" +
109 " <taxon idref=\"taxon3\"/>\n" +
110 " ACG?CTAGAATCGAGCTTCGAGGATAGCATGC\n" +
111 " </sequence>\n" +
112 "</alignment>\n";
113 }
114
115 public XMLSyntaxRule[] getSyntaxRules() {
116 return rules;
117 }
118
119 private final XMLSyntaxRule[] rules = {
120 new XORRule(
121 new StringAttributeRule(
122 DataType.DATA_TYPE,
123 "The data type",
124 DataType.getRegisteredDataTypeNames(), false),
125 new ElementRule(DataType.class)
126 ),
127 new ElementRule(Sequence.class, 1, Integer.MAX_VALUE)
128 };
129 }