Debian Codegraph @

src/dr/app/tools/GetAncestralSequenceFromSplitTrait.java

raw symbols SCIP
1 /*
2 * GetAncestralSequenceFromSplitTrait.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.tools;
27
28 import dr.app.beast.BeastVersion;
29 import dr.app.util.Arguments;
30 import dr.app.util.Utils;
31 import dr.evolution.io.Importer;
32 import dr.evolution.io.NexusImporter;
33 import dr.evolution.io.TreeExporter;
34 import dr.evolution.io.TreeImporter;
35 import dr.evolution.tree.FlexibleTree;
36 import dr.inference.trace.TraceException;
37 import dr.util.Version;
38 import java.util.*;
39 import java.io.*;
40
41 public class GetAncestralSequenceFromSplitTrait {
42
43 private final static Version version = new BeastVersion();
44
45
46 public GetAncestralSequenceFromSplitTrait(String treeAnnotatorFileName, String outputFileName) throws IOException, TraceException {
47
48 File outInputFile = new File(treeAnnotatorFileName);
49
50 if (outInputFile.isFile()) {
51 System.out.println("Analysing *.tree file: " + treeAnnotatorFileName);
52 } else {
53 System.err.println(treeAnnotatorFileName + " does not exist!");
54 System.exit(0);
55 }
56
57 if (outputFileName != null) {
58 FileOutputStream outputStream = new FileOutputStream(outputFileName);
59 System.setOut(new PrintStream(outputStream));
60 }
61
62 analyze(outInputFile);
63 }
64
65 /**
66 * Recursively analyzes log files.
67 *
68 * @param treeAnnotatorInputFile the file to analyze (if this is a directory then the files within it are analyzed)
69 * @throws dr.inference.trace.TraceException
70 * if the trace file is in the wrong format or corrupted
71 */
72 private void analyze(File treeAnnotatorInputFile) throws TraceException {
73
74 try {
75
76 FileReader fileReader = new FileReader(treeAnnotatorInputFile);
77 TreeImporter importer = new NexusImporter(fileReader);
78 FlexibleTree tree = (FlexibleTree) importer.importNextTree();
79 for(int i = 0; i<tree.getNodeCount(); i++) {
80 Hashtable<Integer, State> states = new Hashtable<Integer, State>();
81 for (Iterator<String> j = tree.getNodeAttributeNames(tree.getNode(i)); j.hasNext();) {
82 String name = j.next();
83 if(name.indexOf("states_")>=0) {
84 Integer d = Integer.parseInt(name.replaceFirst("states_", "").replaceFirst("\\..+", ""));
85 State s; //= new State(name.);
86 if(states.containsKey(d)) {
87 s = states.get(d);
88 }
89 else {
90 s = new State(d);
91 }
92
93 // if (tree.getNodeAttribute(tree.getNode(0), name) instanceof Object[]) {
94 // Object[] o = (Object[]) tree.getNodeAttribute(tree.getNode(0), name);
95 //
96 // if(name.matches("states_"+d+".prob")) {
97 // double[] probabilities = new double[o.length];
98 // for(int k=0; k<o.length; k++) {
99 // probabilities[k] = (Double) o[k];
100 // }
101 // s.setProbabilities(probabilities);
102 // }
103 // else if(name.matches("states_"+d)) {
104 // String[] set = new String[o.length];
105 // for(int k=0; k<o.length; k++) {
106 // set[k] = (String) o[k];
107 // }
108 // s.setSet(set);
109 // }
110 // }
111 // else {
112
113 if(name.matches("states_"+d+".prob")) {
114 Object o = tree.getNodeAttribute(tree.getNode(i), name);
115 double probability = (Double) o;
116 s.setProbability(probability);
117 }
118 else if(name.matches("states_"+d)) {
119 Object o = tree.getNodeAttribute(tree.getNode(i), name);
120 String value = (String) o;
121 s.setState(value.replaceAll("\"", ""));
122 }
123 else if(name.matches("states_"+d+".set.prob")) { /* Not necessary but lets parse it anyways */
124 Object[] o = (Object[]) tree.getNodeAttribute(tree.getNode(i), name);
125 double[] probabilities = new double[o.length];
126 for(int k=0; k<o.length; k++) {
127 probabilities[k] = (Double) o[k];
128 }
129 s.setProbabilities(probabilities);
130 }
131 else if(name.matches("states_"+d+".set")) { /* Not necessary but lets parse it anyways */
132 Object[] o = (Object[]) tree.getNodeAttribute(tree.getNode(i), name);
133 String[] set = new String[o.length];
134 for(int k=0; k<o.length; k++) {
135 set[k] = ((String) o[k]).replaceAll("\"", "");
136 }
137 s.setSet(set);
138 }
139 // }
140 states.put(d, s);
141 }
142 }
143
144 State[] statesArray = states.values().toArray(new State[states.size()]);
145 Arrays.sort(statesArray);
146 /* Set the default length to the number of characters that it would need */
147 StringBuffer sb = new StringBuffer(statesArray.length * statesArray[0].getState().length());
148 for(State s : statesArray) {
149 sb.append(s.getState());
150 }
151
152 tree.setNodeAttribute(tree.getNode(i), "seq", sb.toString());
153 }
154 /* Export the new tree with the new sequences */
155 TreeExporter exporter = new NexusExporter(System.out);
156 exporter.exportTree(tree);
157
158
159 System.out.println("Begin trees;");
160 System.out.println("\ttree max_tree = " + tree.toString());
161 System.out.println("End;");
162
163 } catch (IOException e) {
164 System.err.println("Error Parsing Input log: " + e.getMessage());
165 }
166 catch (Importer.ImportException e) {
167 System.err.println("Error Parsing Input Tree: " + e.getMessage());
168 }
169
170 }
171
172
173
174 public static void printTitle() {
175 System.out.println();
176 centreLine("GetAncestralSequenceFromSplitTrait " + version.getVersionString() + ", " + version.getDateString(), 60);
177 centreLine("BAli-Phy MCMC Output analysis", 60);
178 centreLine("by", 60);
179 centreLine("Wai Lok Sibon Li and Marc A. Suchard", 60);
180 System.out.println();
181 centreLine("David Geffen School of Medicine", 60);
182 centreLine("University of California, Los Angeles", 60);
183 centreLine("[email protected]", 60);
184 centreLine("and", 60);
185 centreLine("[email protected]",60);
186 System.out.println();
187 System.out.println();
188 }
189
190 public static void centreLine(String line, int pageWidth) {
191 int n = pageWidth - line.length();
192 int n1 = n / 2;
193 for (int i = 0; i < n1; i++) {
194 System.out.print(" ");
195 }
196 System.out.println(line);
197 }
198
199
200 public static void printUsage(Arguments arguments) {
201
202 arguments.printUsage("GetAncestralSequenceFromSplitTrait", "[<*.tree file-name> [<output-file-name>]]");
203 System.out.println();
204 System.out.println(" Example: ...");
205 System.out.println(" Example: ...");
206 System.out.println();
207
208 }
209
210 //Main method
211 public static void main(String[] args) throws IOException, TraceException {
212
213 printTitle();
214
215 Arguments arguments = new Arguments(
216 new Arguments.Option[]{
217 new Arguments.Option("help", "option to print this message")
218 });
219
220 try {
221 arguments.parseArguments(args);
222 } catch (Arguments.ArgumentException ae) {
223 System.out.println(ae);
224 printUsage(arguments);
225 System.exit(1);
226 }
227
228 if (arguments.hasOption("help")) {
229 printUsage(arguments);
230 System.exit(0);
231 }
232
233 String outInputFileName = null;
234 String outputFileName = null;
235 String[] args2 = arguments.getLeftoverArguments();
236
237 if (args2.length > 2) {
238 System.err.println("Unknown option: " + args2[2]);
239 System.err.println();
240 printUsage(arguments);
241 System.exit(1);
242 }
243
244 if (args2.length > 0) {
245 outInputFileName = args2[0];
246 }
247 if (args2.length > 1) {
248 outputFileName = args2[1];
249 }
250
251 if (outInputFileName == null) {
252 // No input file name was given so throw up a dialog box...
253 outInputFileName = Utils.getLoadFileName("GetAncestralSequenceFromSplitTrait " + version.getVersionString() + " - Select *.tree file to analyse");
254 }
255 new GetAncestralSequenceFromSplitTrait(outInputFileName, outputFileName);
256
257 System.exit(0);
258 }
259
260
261 private class State implements Comparable<State> {
262 private State(int stateIndex) {
263 this.stateIndex = stateIndex;
264 }
265
266 private int stateIndex;
267 private String[] set;
268 private double[] probabilities;
269 private String state;
270 private double probability;
271
272 public String getState() {
273 return state;
274 }
275
276 public void setState(String state) {
277 this.state = state;
278 }
279
280 public double getProbability() {
281 return probability;
282 }
283
284 public void setProbability(double probability) {
285 this.probability = probability;
286 }
287
288 public int getStateIndex() {
289 return stateIndex;
290 }
291
292 public void setStateIndex(int stateIndex) {
293 this.stateIndex = stateIndex;
294 }
295
296 public String[] getSet() {
297 return set;
298 }
299
300 public void setSet(String[] set) {
301 this.set = set;
302 }
303
304 public double[] getProbabilities() {
305 return probabilities;
306 }
307
308 public void setProbabilities(double[] probabilities) {
309 this.probabilities = probabilities;
310 }
311
312
313 public int compareTo(State o) {
314 return this.getStateIndex()-o.getStateIndex();
315 }
316
317 // public String getMaxProbState() {
318 // if(set.length != probabilities.length) {
319 // throw new RuntimeException("Set and probabilities arrays are not the same length: " + set.length +
320 // ", " + probabilities.length + ". State: " + getStateIndex());
321 // }
322 // if(set.length ==0 || probabilities.length ==0) {
323 // throw new RuntimeException("Array lengths are zero");
324 // }
325 // double max = 0;
326 // int maxIndex =0;
327 // for(int i=0; i<probabilities.length; i++) {
328 // if(probabilities[i]>max) {
329 // maxIndex=i;
330 // }
331 // }
332 // return set[maxIndex];
333 // }
334 }
335 }
336