Debian Codegraph @

src/dr/evomodelxml/tree/EmpiricalTreeDistributionModelParser.java

raw symbols SCIP
1 /*
2 * EmpiricalTreeDistributionModelParser.java
3 *
4 * Copyright (c) 2002-2016 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.evomodelxml.tree;
27
28 import dr.util.FileHelpers;
29 import dr.xml.*;
30 import dr.evolution.io.NexusImporter;
31 import dr.evolution.io.Importer;
32 import dr.evolution.tree.Tree;
33 import dr.evolution.util.TaxonList;
34 import dr.evomodel.tree.EmpiricalTreeDistributionModel;
35
36 import java.io.*;
37 import java.util.logging.Logger;
38
39 /**
40 * @author Andrew Rambaut
41 * <p/>
42 * Reads a list of trees from a NEXUS file.
43 */
44 public class EmpiricalTreeDistributionModelParser extends AbstractXMLObjectParser {
45
46 public static final String RATE_ATTRIBUTE_NAME = "rateAttribute";
47 public static final String STARTING_TREE = "startingTree";
48 public static final String ITERATE = "iterate";
49
50 public String getParserName() {
51 return EmpiricalTreeDistributionModel.EMPIRICAL_TREE_DISTRIBUTION_MODEL;
52 }
53
54 public String getParserDescription() {
55 return "Read a list of trees from a NEXUS file.";
56 }
57
58 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
59 final String fileName = xo.getStringAttribute(FILE_NAME);
60
61 int startingTree = xo.getAttribute(STARTING_TREE, -1); // default is random tree
62
63 boolean iterate = xo.getAttribute(ITERATE, false); // default is random draw
64 if (iterate && startingTree < 0) {
65 startingTree = 0;
66 }
67
68 Logger.getLogger("dr.evomodel").info("Creating the empirical tree distribution model, '" + xo.getId() + "'");
69
70 TaxonList taxa = (TaxonList)xo.getChild(TaxonList.class);
71
72 final File file = FileHelpers.getFile(fileName);
73
74 Tree[] trees = null;
75 NexusImporter importer = null;
76 try {
77 FileReader reader = new FileReader(file);
78 importer = new NexusImporter(reader);
79 if (!iterate) {
80 trees = importer.importTrees(taxa, true); // Re-order taxon numbers to original TaxonList order
81 reader.close();
82 }
83 } catch (FileNotFoundException e) {
84 throw new XMLParseException(e.getMessage());
85 } catch (IOException e) {
86 throw new XMLParseException(e.getMessage());
87 } catch (Importer.ImportException e) {
88 throw new XMLParseException(e.getMessage());
89 }
90
91
92 if (iterate) {
93 Logger.getLogger("dr.evomodel").info(" Iterate over each tree from file, " + fileName);
94 return new EmpiricalTreeDistributionModel(importer, startingTree);
95 } else {
96 Logger.getLogger("dr.evomodel").info(" Randomly jump between " + trees.length + " trees from file, " + fileName);
97 return new EmpiricalTreeDistributionModel(trees, startingTree);
98 }
99 }
100
101 public static final String FILE_NAME = "fileName";
102 // public static final String BURNIN = "burnin";
103
104 public XMLSyntaxRule[] getSyntaxRules() {
105 return new XMLSyntaxRule[]{
106 AttributeRule.newIntegerRule(STARTING_TREE, true),
107 AttributeRule.newBooleanRule(ITERATE, true),
108 new StringAttributeRule(FILE_NAME,
109 "The name of a NEXUS tree file"),
110 // AttributeRule.newIntegerRule(BURNIN, true,
111 // "The number of trees to exclude"),
112 new ElementRule(TaxonList.class),
113 };
114 }
115
116
117 public Class getReturnType() {
118 return EmpiricalTreeDistributionModel.class;
119 }
120 }