Debian Codegraph @

src/dr/app/beauti/util/BeastImporter.java

raw symbols SCIP
1 /*
2 * BeastImporter.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.app.beauti.util;
27
28 import dr.evolution.alignment.Alignment;
29 import dr.evolution.alignment.SimpleAlignment;
30 import dr.evolution.io.Importer;
31 import dr.evolution.sequence.Sequence;
32 import dr.evolution.util.*;
33 import dr.evoxml.AlignmentParser;
34 import dr.evoxml.DateParser;
35 import dr.evoxml.SequenceParser;
36 import dr.evoxml.TaxaParser;
37 import dr.evoxml.TaxonParser;
38 import dr.xml.AttributeParser;
39 import dr.xml.XMLParser;
40 import org.jdom.Document;
41 import org.jdom.Element;
42 import org.jdom.JDOMException;
43 import org.jdom.input.SAXBuilder;
44
45 import java.io.IOException;
46 import java.io.Reader;
47 import java.text.DateFormat;
48 import java.text.ParseException;
49 import java.util.Calendar;
50 import java.util.List;
51 import java.util.Locale;
52
53 /**
54 * @author Andrew Rambaut
55 * @version $Id$
56 */
57 public class BeastImporter {
58
59 private final Element root;
60 private final DateFormat dateFormat;
61 private final java.util.Date origin;
62
63 public BeastImporter(Reader reader) throws IOException, JDOMException, Importer.ImportException {
64 SAXBuilder builder = new SAXBuilder();
65
66 Document doc = builder.build(reader);
67 root = doc.getRootElement();
68 if (!root.getName().equalsIgnoreCase("beast")) {
69 throw new Importer.ImportException("Unrecognized root element in XML file");
70 }
71
72 dateFormat = DateFormat.getDateInstance(java.text.DateFormat.SHORT, Locale.UK);
73 dateFormat.setLenient(true);
74
75 Calendar cal = Calendar.getInstance(Locale.getDefault());
76
77 // set uses a zero based month (Jan = 0):
78 cal.set(0000, 00, 01);
79 // origin = cal.getTime();
80
81 cal.setTimeInMillis(0);
82 origin = cal.getTime();
83 }
84
85 public void importBEAST(List<TaxonList> taxonLists, List<Alignment> alignments) throws Importer.ImportException {
86
87 TaxonList taxa = null;
88
89 List children = root.getChildren();
90 for(Object aChildren : children) {
91 Element child = (Element) aChildren;
92
93 if( child.getName().equalsIgnoreCase(TaxaParser.TAXA) ) {
94 if( taxa == null ) {
95 taxa = readTaxa(null, child);
96 taxonLists.add(taxa);
97 } else {
98 taxonLists.add(readTaxa(taxa, child));
99 }
100 } else if( child.getName().equalsIgnoreCase(AlignmentParser.ALIGNMENT) ) {
101 if( taxa == null ) {
102 throw new Importer.ImportException("taxa not defined");
103 }
104 alignments.add(readAlignment(child, taxa));
105 }
106 }
107 }
108
109 private TaxonList readTaxa(TaxonList primaryTaxa, Element e) throws Importer.ImportException {
110 Taxa taxa = new Taxa();
111
112 String id = e.getAttributeValue("id");
113 if (id != null) {
114 taxa.setId(id);
115 } else {
116 taxa.setId("taxa");
117 }
118
119 List children = e.getChildren();
120 for(Object aChildren : children) {
121 Element child = (Element) aChildren;
122
123 if( child.getName().equalsIgnoreCase(TaxonParser.TAXON) ) {
124 taxa.addTaxon(readTaxon(primaryTaxa, child));
125 }
126 }
127 return taxa;
128 }
129
130 private Taxon readTaxon(TaxonList primaryTaxa, Element e) throws Importer.ImportException {
131
132 String id = e.getAttributeValue(XMLParser.ID);
133
134 Taxon taxon = null;
135
136 if (id != null) {
137 taxon = new Taxon(id);
138
139 List children = e.getChildren();
140 for(Object aChildren : children) {
141 Element child = (Element) aChildren;
142
143 if( child.getName().equalsIgnoreCase(dr.evolution.util.Date.DATE) ) {
144 Date date = readDate(child);
145 taxon.setAttribute(dr.evolution.util.Date.DATE, date);
146 } else if( child.getName().equalsIgnoreCase(AttributeParser.ATTRIBUTE) ) {
147 String name = child.getAttributeValue(AttributeParser.NAME);
148 String value = child.getAttributeValue(AttributeParser.VALUE);
149 if (value == null) {
150 value = child.getTextTrim();
151 }
152 taxon.setAttribute(name, value);
153 }
154 }
155 } else {
156 String idref = e.getAttributeValue(XMLParser.IDREF);
157 int index = primaryTaxa.getTaxonIndex(idref);
158 if (index >= 0) {
159 taxon = primaryTaxa.getTaxon(index);
160 }
161 }
162
163 return taxon;
164 }
165
166 private Alignment readAlignment(Element e, TaxonList taxa) throws Importer.ImportException {
167 SimpleAlignment alignment = new SimpleAlignment();
168
169 List children = e.getChildren();
170 for(Object aChildren : children) {
171 Element child = (Element) aChildren;
172
173 if( child.getName().equalsIgnoreCase(SequenceParser.SEQUENCE) ) {
174 alignment.addSequence(readSequence(child, taxa));
175 }
176 }
177 return alignment;
178 }
179
180 private Sequence readSequence(Element e, TaxonList taxa) throws Importer.ImportException {
181
182 String taxonID = e.getChild(TaxonParser.TAXON).getAttributeValue(XMLParser.IDREF);
183 int index = taxa.getTaxonIndex(taxonID);
184 if (index < 0) {
185 throw new Importer.ImportException("Unknown taxon, " + taxonID + ", in alignment");
186 }
187 Taxon taxon = taxa.getTaxon(index);
188
189 String[] seq = e.getTextTrim().split("\\s");
190 StringBuilder sb = new StringBuilder();
191 for (String part : seq) {
192 sb.append(part);
193 }
194
195 return new Sequence(taxon, sb.toString());
196 }
197
198 private Date readDate(Element e) throws Importer.ImportException {
199
200 String value = e.getAttributeValue(DateParser.VALUE);
201 boolean backwards = true;
202 String direction = e.getAttributeValue(DateParser.DIRECTION);
203 if (direction != null && direction.equalsIgnoreCase(DateParser.FORWARDS)) {
204 backwards = false;
205 }
206 try {
207 return new Date(dateFormat.parse(value));
208 } catch (ParseException e1) {
209 // ignore the parse exception and try it just as a number
210 }
211
212 // try just parsing it as a number
213 return new Date(Double.valueOf(value), Units.Type.YEARS, backwards);
214
215 }
216
217
218 }