Debian Codegraph @

src/dr/evolution/io/TreeTrace.java

raw symbols SCIP
1 /*
2 * TreeTrace.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.evolution.io;
27
28 import dr.evolution.tree.Tree;
29 import dr.util.Identifiable;
30
31 import java.io.BufferedReader;
32 import java.io.EOFException;
33 import java.io.IOException;
34 import java.io.Reader;
35 import java.util.ArrayList;
36
37 /**
38 * @author Andrew Rambaut
39 * @author Alexei Drummond
40 * @version $Id: TreeTrace.java,v 1.10 2005/05/24 20:25:58 rambaut Exp $
41 */
42 public class TreeTrace implements Identifiable {
43
44 public TreeTrace() {
45 }
46
47 public int getTreeCount(int burnin) {
48 int startIndex = (burnin - minState) / stepSize;
49 if (startIndex < 0) {
50 startIndex = 0;
51 }
52 return trees.size() - startIndex;
53 }
54
55 public Tree getTree(int index, int burnin) {
56 int startIndex = (burnin - minState) / stepSize;
57 if (startIndex < 0) {
58 startIndex = 0;
59 }
60 return trees.get(index + startIndex);
61 }
62
63 public void add(Tree tree) {
64 trees.add(tree);
65 }
66
67 public void setMinimumState(int minState) {
68 this.minState = minState;
69 }
70
71 public int getMinimumState() {
72 return minState;
73 }
74
75 public void setStepSize(int stepSize) {
76 this.stepSize = stepSize;
77 }
78
79 public int getStepSize() {
80 return stepSize;
81 }
82
83 public int getMaximumState() {
84 return (trees.size() - 1) * stepSize + minState;
85 }
86
87 public String getId() {
88 return id;
89 }
90
91 public void setId(String id) {
92 this.id = id;
93 }
94
95 private ArrayList<Tree> trees = new ArrayList<Tree>();
96
97 private int minState;
98 private int stepSize;
99 private String id;
100
101 /**
102 * Loads the trace for with trees from a reader
103 *
104 * @param r the reader to load the trees from
105 * @return the TreeTrace
106 * @throws dr.evolution.io.Importer.ImportException
107 * thrown when tree file is not correctly formatted
108 * @throws java.io.IOException if general I/O error occurs
109 */
110 public static TreeTrace loadTreeTrace(Reader r) throws IOException, Importer.ImportException {
111
112 BufferedReader reader = new BufferedReader(r);
113
114 TreeTrace trace = new TreeTrace();
115
116 dr.evolution.util.TaxonList taxonList = null;
117
118 int minState = -1;
119 int stepSize = 0;
120
121 String line = reader.readLine();
122
123 if (line.toUpperCase().startsWith("#NEXUS")) {
124 NexusImporter importer = new NexusImporter(reader);
125 Tree[] trees = importer.importTrees(null);
126
127 if (trees.length < 2) {
128 throw new Importer.ImportException("Less than two trees in the trace file");
129 }
130
131 String id1 = trees[0].getId();
132 String id2 = trees[1].getId();
133
134 minState = getStateNumber(id1);
135 stepSize = getStateNumber(id2) - minState;
136
137 for (Tree tree : trees) {
138 trace.add(tree);
139 }
140 } else {
141 NewickImporter importer = new NewickImporter(reader);
142
143 while (true) {
144
145 int state = 0;
146 Tree tree;
147
148 try {
149 state = importer.readInteger();
150 tree = importer.importTree(taxonList);
151
152 if (taxonList == null) {
153 // The first tree becomes the taxon list. This means
154 // that all subsequent trees will look up their taxa
155 // in that taxon list rather than creating their own
156 // duplicitous ones.
157 taxonList = tree;
158 }
159 } catch (Importer.ImportException ie) {
160 System.out.println("Error reading tree for state " + state);
161 throw ie;
162 } catch (EOFException e) {
163 break;
164 }
165
166 if (minState == -1) {
167 minState = state;
168 } else if (stepSize == 0) {
169 stepSize = state - minState;
170 }
171
172 trace.add(tree);
173 }
174 }
175
176 trace.setMinimumState(minState);
177 trace.setStepSize(stepSize);
178
179 return trace;
180 }
181
182 private static int getStateNumber(String id) throws Importer.ImportException {
183 try {
184 if (id.indexOf('_') != -1) { // probably BEAST tree file
185 return Integer.parseInt(id.substring(id.indexOf('_') + 1));
186 } else if (id.indexOf('.') != -1) { // probably MrBayes tree file
187 int rep = Integer.parseInt(id.substring(id.indexOf('.') + 1));
188 if (rep == 1) rep = 0;
189 return rep;
190 }
191 throw new NumberFormatException();
192 } catch (NumberFormatException nfe) {
193 throw new Importer.ImportException("Bad state number in tree label '" + id + "', the state must be preceded by an underscore(_).");
194 }
195 }
196 }