Debian Codegraph @

src/dr/inference/trace/TracesReader.java

raw symbols SCIP
1 /*
2 * TracesReader.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.inference.trace;
27
28 import java.io.Reader;
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.StringTokenizer;
32
33 /**
34 * @author Alexei Drummond
35 */
36 public class TracesReader {
37
38 public TracesReader() {
39 }
40
41 public void addTracesListener(TracesListener listener) {
42 listeners.add(listener);
43 }
44
45 public void removeTracesListener(TracesListener listener) {
46 listeners.remove(listener);
47 }
48
49 /**
50 * Reads all the traces in a file, but does not store in memory.
51 *
52 * @param r the reader to read traces from
53 * @throws TraceException when trace contents is not valid
54 * @throws java.io.IOException low level problems with file
55 */
56 public void readTraces(Reader r) throws TraceException, java.io.IOException {
57
58 LogFileTraces.TrimLineReader reader = new LogFileTraces.TrimLineReader(r);
59
60 // Read through to first token
61 StringTokenizer tokens = reader.tokenizeLine();
62
63 // read over empty lines
64 while (!tokens.hasMoreTokens()) {
65 tokens = reader.tokenizeLine();
66 }
67
68 // skip the first column which should be the state number
69 String token = tokens.nextToken();
70
71 // lines starting with [ are ignored, assuming comments in MrBayes file
72 // lines starting with # are ignored, assuming comments in Migrate or BEAST file
73 while (token.startsWith("[") || token.startsWith("#")) {
74
75 tokens = reader.tokenizeLine();
76
77 // read over empty lines
78 while (!tokens.hasMoreTokens()) {
79 tokens = reader.tokenizeLine();
80 }
81
82 // read state token and ignore
83 token = tokens.nextToken();
84 }
85
86 // read label tokens
87
88 String[] labels = new String[tokens.countTokens()];
89
90 for (int i = 0; i < labels.length; i++) {
91 labels[i] = tokens.nextToken();
92 }
93 for (TracesListener listener : listeners) {
94 listener.traceNames(labels);
95 }
96
97 boolean firstState = true;
98
99 tokens = reader.tokenizeLine();
100 while (tokens != null && tokens.hasMoreTokens()) {
101
102 String stateString = tokens.nextToken();
103 int state = 0;
104 try {
105 try {
106 // Changed this to parseDouble because LAMARC uses scientific notation for the state number
107 state = (int) Double.parseDouble(stateString);
108 } catch (NumberFormatException nfe) {
109 throw new TraceException("Unable to parse state number in column 1 (Line " + reader.getLineNumber() + ")");
110 }
111
112 if (firstState) {
113 // MrBayes puts 1 as the first state, BEAST puts 0
114 // In order to get the same gap between subsequent samples,
115 // we force this to 0.
116 if (state == 1) state = 0;
117 firstState = false;
118 }
119
120 } catch (NumberFormatException nfe) {
121 throw new TraceException("State " + state + ":Expected real value in column " + reader.getLineNumber());
122 }
123
124 double[] values = new double[labels.length];
125 for (int i = 0; i < values.length; i++) {
126 if (tokens.hasMoreTokens()) {
127
128 try {
129 values[i] = Double.parseDouble(tokens.nextToken());
130 } catch (NumberFormatException nfe) {
131 throw new TraceException("State " + state + ": Expected real value in column " + (i + 1) +
132 " (Line " + reader.getLineNumber() + ")");
133 }
134 } else {
135 throw new TraceException("State " + state + ": missing values at line " + reader.getLineNumber());
136 }
137 }
138
139 for (TracesListener listener : listeners) {
140 listener.traceRow(state, values);
141 }
142 tokens = reader.tokenizeLine();
143 }
144 }
145
146 private List<TracesListener> listeners = new ArrayList<TracesListener>();
147 }