| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
} catch (FileNotFoundException e) { |
| 110 |
|
| 111 |
e.printStackTrace(); |
| 112 |
} catch (IOException e) { |
| 113 |
|
| 114 |
e.printStackTrace(); |
| 115 |
} catch (ImportException e) { |
| 116 |
|
| 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 |
|
| 126 |
int index = findTree(currentTree); |
| 127 |
if (index != -1) { |
| 128 |
lastKnownTree = currentTree; |
| 129 |
} |
| 130 |
} else { |
| 131 |
|
| 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 |
|
| 143 |
int index = findTree(currentTree); |
| 144 |
if (index == -1) { |
| 145 |
|
| 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 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 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 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 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 |
|
| 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 |
} |