Debian Codegraph @

src/dr/evomodel/tree/TreeSpaceLogger.java

raw symbols SCIP
1 /*
2 * TreeSpaceLogger.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 /**
27 *
28 */
29 package dr.evomodel.tree;
30
31 import dr.evolution.io.Importer.ImportException;
32 import dr.evolution.io.NewickImporter;
33 import dr.evolution.tree.Tree;
34 import dr.evolution.tree.TreeUtils;
35
36 import java.io.*;
37 import java.util.ArrayList;
38 import java.util.List;
39
40 /**
41 * @author shhn001
42 */
43 public class TreeSpaceLogger {
44
45 private String[] trees;
46 private int[] islands;
47 private int[][] transitions;
48 private int[][] steps;
49 private int[][] islandTransitions;
50 private int[] islandDwellTimes;
51 private String lastKnownTree;
52 private List<String> currentPath;
53 // private HashMap<String, Integer>[][] pathes;
54
55 /**
56 *
57 */
58 public TreeSpaceLogger(String filename) {
59 readTrees(filename);
60 currentPath = new ArrayList<String>();
61 }
62
63 private void readTrees(String filename) {
64 try {
65 FileReader fr = new FileReader(new File(filename));
66 BufferedReader br = new BufferedReader(fr);
67 String line;
68 List<Tree> trees = new ArrayList<Tree>();
69 List<Integer> islands = new ArrayList<Integer>();
70
71 while ((line = br.readLine()) != null) {
72 // read trees
73 String[] tokens = line.split("\\s++");
74
75 NewickImporter importer = new NewickImporter(line);
76 Tree t = importer.importNextTree();
77 trees.add(t);
78 islands.add(Integer.parseInt(tokens[0]));
79 }
80
81 br.close();
82 fr.close();
83
84 this.trees = new String[trees.size()];
85 this.islands = new int[trees.size()];
86 int maxIsland = 0;
87 for (int i = 0; i < trees.size(); i++) {
88 Tree tree = trees.get(i);
89 String newick = TreeUtils.uniqueNewick(tree, tree.getRoot());
90 this.trees[i] = newick;
91 this.islands[i] = islands.get(i) - 1;
92 if (islands.get(i) > maxIsland) {
93 maxIsland = islands.get(i);
94 }
95 }
96
97 transitions = new int[trees.size()][trees.size()];
98 steps = new int[trees.size()][trees.size()];
99 islandTransitions = new int[maxIsland][maxIsland];
100 islandDwellTimes = new int[maxIsland];
101
102 // pathes = new HashMap[trees.size()][trees.size()];
103 // for (int i = 0; i < trees.size(); i++) {
104 // for (int j = 0; j < trees.size(); j++) {
105 // pathes[i][j] = new HashMap<String, Integer>();
106 // }
107 // }
108
109 } catch (FileNotFoundException e) {
110 // TODO Auto-generated catch block
111 e.printStackTrace();
112 } catch (IOException e) {
113 // TODO Auto-generated catch block
114 e.printStackTrace();
115 } catch (ImportException e) {
116 // TODO Auto-generated catch block
117 e.printStackTrace();
118 }
119 }
120
121 public void log(Tree tree) {
122 String currentTree = TreeUtils.uniqueNewick(tree, tree.getRoot());
123
124 if (lastKnownTree == null || lastKnownTree.equals("")) {
125 // check if we ended up at another known tree
126 int index = findTree(currentTree);
127 if (index != -1) {
128 lastKnownTree = currentTree;
129 }
130 } else {
131 // check if we left our current tree or if went in a unlikely circle
132 if (currentTree.equals(lastKnownTree)) {
133 if (currentPath.size() >= 1) {
134 int startIndex = findTree(lastKnownTree);
135 transitions[startIndex][startIndex]++;
136 steps[startIndex][startIndex] += currentPath.size();
137 islandTransitions[islands[startIndex]][islands[startIndex]]++;
138 islandDwellTimes[islands[startIndex]] += currentPath.size();
139 }
140 currentPath.clear();
141 } else {
142 // check if we ended up at another known tree
143 int index = findTree(currentTree);
144 if (index == -1) {
145 // we didn't end at a know tree yet
146 currentPath.add(currentTree);
147 } else {
148 currentPath.add(currentTree);
149 int startIndex = findTree(lastKnownTree);
150 transitions[startIndex][index]++;
151 steps[startIndex][index] += currentPath.size() - 1;
152 islandDwellTimes[islands[index]] += currentPath.size() - 1;
153 islandTransitions[islands[startIndex]][islands[index]]++;
154
155 // if (pathes[startIndex][index].containsKey(currentPath
156 // .toString())) {
157 // int trans = pathes[startIndex][index].get(currentPath
158 // .toString());
159 // pathes[startIndex][index].put(currentPath.toString(),
160 // trans + 1);
161 // } else {
162 // pathes[startIndex][index]
163 // .put(currentPath.toString(), 1);
164 // }
165
166 currentPath.clear();
167 lastKnownTree = currentTree;
168 }
169 }
170 }
171 }
172
173 private int findTree(String tree) {
174 for (int i = 0; i < trees.length; i++) {
175 if (trees[i].equals(tree)) {
176 return i;
177 }
178 }
179 return -1;
180 }
181
182 public void printTransitionMatrix() {
183 System.out.println("\n\nPrint transition matrix:\n");
184
185 for (int i = 0; i < trees.length; i++) {
186 for (int j = 0; j < trees.length; j++) {
187 System.out.print(transitions[i][j] + "\t");
188 }
189 System.out.println();
190 }
191 }
192
193 // public void printPathesMatrix() {
194 // System.out.println("\n\nPrint # of pathes matrix:\n");
195 //
196 // for (int i = 0; i < trees.length; i++) {
197 // for (int j = 0; j < trees.length; j++) {
198 // System.out.print(pathes[i][j].size() + "\t");
199 // }
200 // System.out.println();
201 // }
202 // }
203
204 public void printIslandTransitionMatrix() {
205 System.out.println("\n\nPrint island transition matrix:\n");
206
207 for (int i = 0; i < islandTransitions.length; i++) {
208 for (int j = 0; j < islandTransitions.length; j++) {
209 System.out.print(islandTransitions[i][j] + "\t");
210 }
211 System.out.println();
212 }
213 }
214
215 public void printMeanTopologyMatrix() {
216 System.out.println("\n\nPrint mean # of topologies on path matrix:\n");
217
218 for (int i = 0; i < trees.length; i++) {
219 for (int j = 0; j < trees.length; j++) {
220 int trans = transitions[i][j];
221 if (trans == 0) {
222 trans = 1;
223 }
224 System.out.print((steps[i][j] / trans) + "\t");
225 }
226 System.out.println();
227 }
228 }
229
230 public void printIslandDwellMatrix() {
231 System.out.println("\n\nPrint mean dwell time matrix:\n");
232
233 for (int i = 0; i < islandDwellTimes.length; i++) {
234 int transitions = 0;
235 for (int j = 0; j < islandDwellTimes.length; j++) {
236 if (i != j) {
237 transitions += islandTransitions[i][j];
238 }
239 }
240 if (transitions == 0) {
241 transitions = 1;
242 }
243 System.out.print((islandDwellTimes[i] / transitions) + "\t");
244 System.out.println();
245 }
246 }
247
248 public void finishLogging() {
249 printTransitionMatrix();
250 // printPathesMatrix();
251 printIslandTransitionMatrix();
252 printMeanTopologyMatrix();
253 printIslandDwellMatrix();
254 }
255
256 public static void main(String[] args) {
257 TreeSpaceLogger logger = new TreeSpaceLogger("DS1_Treespace.trees");
258 logger.finishLogging();
259 }
260
261 }