Debian Codegraph @

src/dr/app/tools/ConvertMigrateTrees.java

raw symbols SCIP
1 /*
2 * ConvertMigrateTrees.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.evolution.io.Importer;
29 import dr.evolution.io.MigrateTreeImporter;
30 import dr.evolution.tree.*;
31
32 import java.io.*;
33
34 /**
35 * @author Alexei Drummond
36 */
37 public class ConvertMigrateTrees {
38
39 static boolean forceDiscrete = false;
40
41 public static final String LABEL = "pop";
42
43
44 public static void main(String[] args) throws IOException, Importer.ImportException {
45
46 String migrateFile = null;
47 if (args.length == 1) {
48 migrateFile = args[0];
49 } else if (args.length == 2 && args[0].equals("-d")) {
50 forceDiscrete = true;
51 migrateFile = args[1];
52 } else {
53 System.out.println("USAGE: ConvertMigrateTrees [-d] <migrateTreeFileName>");
54 System.exit(0);
55 }
56
57 File file = makeInputNexus(migrateFile);
58
59 MigrateTreeImporter importer = new MigrateTreeImporter(new FileReader(file));
60
61 System.out.println("Starting conversion...");
62 final Tree[] trees = importer.importTrees(null);
63
64 writeNexus(migrateFile + ".figtree.trees", trees);
65 }
66
67 private static void writeNexus(String filename, Tree[] trees) throws IOException {
68
69 PrintWriter out = new PrintWriter(new FileWriter(filename));
70
71 TreeTraitProvider.Helper popAttributes = new TreeTraitProvider.Helper();
72 popAttributes.addTrait( new TreeTrait.S() {
73 public String getTraitName() {
74 return LABEL;
75 }
76
77 public Intent getIntent() {
78 return Intent.NODE;
79 }
80
81 public int getDimension() {
82 return 1;
83 }
84
85 public String getTrait(Tree tree, NodeRef node) {
86 Object attribute = tree.getNodeAttribute(node, MigrateTreeImporter.POP);
87 if (attribute == null) {
88 throw new RuntimeException(MigrateTreeImporter.POP + " is null for node " + node.getNumber());
89 }
90
91 return (forceDiscrete ? "d" : "") + attribute.toString();
92 }
93 });
94
95 writeNexusHeader(out);
96 for (int i = 0; i < trees.length; i++) {
97 out.println("tree tree_" + i + " = " +
98 TreeUtils.newick(trees[i], new TreeTraitProvider[] { popAttributes}));
99 }
100 out.println("end;");
101
102 }
103
104 private static void writeNexusHeader(PrintWriter out) {
105 out.println("#NEXUS");
106 out.println();
107 out.println("begin trees;");
108 out.println();
109 }
110
111 private static File makeInputNexus(String migrateFile) throws IOException {
112 File temp = File.createTempFile(migrateFile, ".nexus");
113
114 System.out.println("Making temporary file: " + temp);
115
116 PrintWriter writer = new PrintWriter(new FileWriter(temp));
117 writeNexusHeader(writer);
118
119 BufferedReader reader = new BufferedReader(new FileReader(migrateFile));
120
121 String line = reader.readLine();
122 int treeCount = 0;
123 while (line != null) {
124 if (line.startsWith("[& Locus")) {
125 writer.write("tree tree" + treeCount + " = " + line);
126 treeCount += 1;
127
128 if (treeCount % 100 == 0) {
129 System.out.print(".");
130 System.out.flush();
131 }
132 } else {
133 writer.println(line);
134 }
135 line = reader.readLine();
136 }
137 writer.println("end;");
138 writer.close();
139 reader.close();
140
141 return temp;
142 }
143 }