Debian Codegraph @

src/dr/evomodel/arg/ARGTrace.java

raw symbols SCIP
1 /*
2 * ARGTrace.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.evomodel.arg;
27
28 import dr.evolution.io.Importer;
29 import dr.evolution.util.TaxonList;
30 import dr.util.Identifiable;
31
32 import java.io.BufferedReader;
33 import java.io.IOException;
34 import java.io.Reader;
35 import java.util.ArrayList;
36 import java.util.StringTokenizer;
37
38 /**
39 * Created by IntelliJ IDEA.
40 * User: msuchard
41 * Date: Jul 19, 2007
42 * Time: 5:30:44 PM
43 * To change this template use File | Settings | File Templates.
44 */
45
46 public class ARGTrace implements Identifiable {
47
48 public ARGTrace() {
49 }
50
51 public int getTreeCount(int burnin) {
52 int startIndex = (burnin - minState) / stepSize;
53 if (startIndex < 0) {
54 startIndex = 0;
55 }
56 return args.size() - startIndex;
57 }
58
59 public ARGModel getARG(int index, int burnin) {
60 int startIndex = (burnin - minState) / stepSize;
61 if (startIndex < 0) {
62 startIndex = 0;
63 }
64 return (ARGModel) args.get(index + startIndex);
65 }
66
67 public void add(ARGModel arg) {
68 args.add(arg);
69 }
70
71 public void setMinimumState(int minState) {
72 this.minState = minState;
73 }
74
75 public int getMinimumState() {
76 return minState;
77 }
78
79 public void setStepSize(int stepSize) {
80 this.stepSize = stepSize;
81 }
82
83 public int getStepSize() {
84 return stepSize;
85 }
86
87 public int getMaximumState() {
88 return (args.size() - 1) * stepSize + minState;
89 }
90
91 public String getId() {
92 return id;
93 }
94
95 public void setId(String id) {
96 this.id = id;
97 }
98
99 private ArrayList<ARGModel> args = new ArrayList<ARGModel>();
100
101 private int minState;
102 private int stepSize;
103 private String id;
104
105 /**
106 * Loads the trace for with trees from a reader
107 *
108 * @return the TreeTrace
109 */
110 public static ARGTrace loadARGTrace(Reader r) throws IOException, Importer.ImportException {
111
112 BufferedReader reader = new BufferedReader(r);
113
114 ARGTrace trace = new ARGTrace();
115
116 dr.evolution.util.TaxonList taxonList = null;
117
118 int minState = -1;
119 int stepSize = 0;
120
121 String line;
122 ARGModel nullARG = new ARGModel(null, null, 0, 0);
123 ArrayList<String> nameList = new ArrayList<String>();
124
125 taxonList = (TaxonList) nullARG;
126
127 while ((line = reader.readLine()) != null) {
128
129 // String line = reader.readLine();
130
131 line.trim();
132
133 if (line.toUpperCase().startsWith("ARG")) {
134 StringTokenizer st = new StringTokenizer(line, "=");
135 nameList.add(st.nextToken());
136 ARGModel arg = nullARG.fromGraphStringCompressed(st.nextToken().trim());
137 trace.add(arg);
138
139 }
140
141 }
142
143 if (nameList.size() < 2) {
144 throw new Importer.ImportException("Less than two ARGs in the trace file");
145 }
146
147 minState = getStateNumber(nameList.get(0));
148 stepSize = getStateNumber(nameList.get(1)) - minState;
149
150 // if (line.toUpperCase().startsWith("#NEXUS")) {
151 // NexusImporter importer = new NexusImporter(reader);
152 // Tree [] trees = importer.importTrees(null);
153 //
154 // if (trees.length < 2) {
155 // throw new Importer.ImportException("Less than two trees in the trace file");
156 // }
157 //
158 // String id1 = trees[0].getId();
159 // String id2 = trees[1].getId();
160 //
161 // minState = getStateNumber(id1);
162 // stepSize = getStateNumber(id2) - minState;
163 //
164 // for (int i = 0; i < trees.length; i++) {
165 // args.add(args[i]);
166 // }
167 // } else {
168 // NewickImporter importer = new NewickImporter(reader);
169 //
170 // while (true) {
171 //
172 // int state = 0;
173 // Tree tree;
174 //
175 // try {
176 // state = importer.readInteger();
177 // tree = importer.importTree(taxonList);
178 //
179 // if (taxonList == null) {
180 // // The first tree becomes the taxon list. This means
181 // // that all subsequent trees will look up their taxa
182 // // in that taxon list rather than creating their own
183 // // duplicitous ones.
184 // taxonList = tree;
185 // }
186 // } catch (Importer.ImportException ie) {
187 // System.out.println("Error reading tree for state " + state);
188 // throw ie;
189 // } catch (EOFException e) {
190 // break;
191 // }
192 //
193 // if (minState == -1) {
194 // minState = state;
195 // } else if (stepSize == 0) {
196 // stepSize = state - minState;
197 // }
198 //
199 // trace.add(tree);
200 // }
201 // }
202
203 trace.setMinimumState(minState);
204 trace.setStepSize(stepSize);
205
206 return trace;
207 }
208
209 private final static int getStateNumber(String id) throws Importer.ImportException {
210 try {
211 return Integer.parseInt(id.substring(id.indexOf('_') + 1).trim());
212 } catch (NumberFormatException nfe) {
213 throw new Importer.ImportException("Bad state number in tree label '" + id + "', the state must be preceded by an underscore(_).");
214 }
215 }
216 }