Debian Codegraph @

src/dr/evoxml/SequenceParser.java

raw symbols SCIP
1 /*
2 * SequenceParser.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.datatype.*;
29 import dr.evolution.sequence.Sequence;
30 import dr.evolution.sequence.UncertainSequence;
31 import dr.evolution.util.Taxon;
32 import dr.xml.*;
33
34 import java.util.StringTokenizer;
35
36 /**
37 * @author Alexei Drummond
38 * @author Andrew Rambaut
39 * @author Marc A. Suchard
40 *
41 * @version $Id: SequenceParser.java,v 1.2 2005/05/24 20:25:59 rambaut Exp $
42 */
43 public class SequenceParser extends AbstractXMLObjectParser {
44
45 public static final String SEQUENCE = "sequence";
46
47 public String getParserName() { return SEQUENCE; }
48
49 /**
50 * @return a sequence object based on the XML element it was passed.
51 */
52 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
53
54 Taxon taxon = (Taxon)xo.getChild(Taxon.class);
55
56 DataType dataType = null;
57 if (xo.hasAttribute(DataType.DATA_TYPE)) {
58 String dataTypeStr = xo.getStringAttribute(DataType.DATA_TYPE);
59
60 if (dataTypeStr.equals(Nucleotides.DESCRIPTION)) {
61 dataType = Nucleotides.INSTANCE;
62 } else if (dataTypeStr.equals(AminoAcids.DESCRIPTION)) {
63 dataType = AminoAcids.INSTANCE;
64 } else if (dataTypeStr.equals(Codons.DESCRIPTION)) {
65 dataType = Codons.UNIVERSAL;
66 } else if (dataTypeStr.equals(TwoStates.DESCRIPTION)) {
67 dataType = TwoStates.INSTANCE;
68 }
69 }
70
71 StringBuffer seqBuf = new StringBuffer();
72
73 for (int i = 0; i < xo.getChildCount(); i++) {
74 Object child = xo.getChild(i);
75 if (child instanceof String) {
76 StringTokenizer st = new StringTokenizer((String)child);
77 while (st.hasMoreTokens()) {
78 seqBuf.append(st.nextToken());
79 }
80 }
81 }
82
83 // We really need to filter the input string to check for illegal characters.
84 // Perhaps sequence.setSequenceString could throw an exception if any characters
85 // don't fit the dataType.
86 String sequenceString = seqBuf.toString();
87
88 if (sequenceString.length() == 0) {
89 throw new XMLParseException("Sequence data missing from sequence element!");
90 }
91
92 Sequence sequence;
93 if (UncertainSequence.containsWeights(sequenceString)) {
94 sequence = new UncertainSequence();
95 } else {
96 sequence = new Sequence();
97 }
98
99 if (dataType != null) {
100 sequence.setDataType(dataType);
101 }
102
103 sequence.setSequenceString(sequenceString);
104 sequence.setTaxon(taxon);
105
106 return sequence;
107 }
108
109 public String getParserDescription() {
110 return "A biomolecular sequence.";
111 }
112
113 public Class getReturnType() { return Sequence.class; }
114
115 public XMLSyntaxRule[] getSyntaxRules() { return rules; }
116
117 private XMLSyntaxRule[] rules = new XMLSyntaxRule[] {
118 new ElementRule(Taxon.class),
119 new ElementRule(String.class, "A character string representing the aligned molecular sequence", "ACGACTAGCATCGAGCTTCG--GATAGCATGC")
120 };
121 }
122