| 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 |
package dr.evolution.io; |
| 27 |
|
| 28 |
import dr.evolution.tree.Tree; |
| 29 |
import dr.util.Identifiable; |
| 30 |
|
| 31 |
import java.io.BufferedReader; |
| 32 |
import java.io.EOFException; |
| 33 |
import java.io.IOException; |
| 34 |
import java.io.Reader; |
| 35 |
import java.util.ArrayList; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class TreeTrace implements Identifiable { |
| 43 |
|
| 44 |
public TreeTrace() { |
| 45 |
} |
| 46 |
|
| 47 |
public int getTreeCount(int burnin) { |
| 48 |
int startIndex = (burnin - minState) / stepSize; |
| 49 |
if (startIndex < 0) { |
| 50 |
startIndex = 0; |
| 51 |
} |
| 52 |
return trees.size() - startIndex; |
| 53 |
} |
| 54 |
|
| 55 |
public Tree getTree(int index, int burnin) { |
| 56 |
int startIndex = (burnin - minState) / stepSize; |
| 57 |
if (startIndex < 0) { |
| 58 |
startIndex = 0; |
| 59 |
} |
| 60 |
return trees.get(index + startIndex); |
| 61 |
} |
| 62 |
|
| 63 |
public void add(Tree tree) { |
| 64 |
trees.add(tree); |
| 65 |
} |
| 66 |
|
| 67 |
public void setMinimumState(int minState) { |
| 68 |
this.minState = minState; |
| 69 |
} |
| 70 |
|
| 71 |
public int getMinimumState() { |
| 72 |
return minState; |
| 73 |
} |
| 74 |
|
| 75 |
public void setStepSize(int stepSize) { |
| 76 |
this.stepSize = stepSize; |
| 77 |
} |
| 78 |
|
| 79 |
public int getStepSize() { |
| 80 |
return stepSize; |
| 81 |
} |
| 82 |
|
| 83 |
public int getMaximumState() { |
| 84 |
return (trees.size() - 1) * stepSize + minState; |
| 85 |
} |
| 86 |
|
| 87 |
public String getId() { |
| 88 |
return id; |
| 89 |
} |
| 90 |
|
| 91 |
public void setId(String id) { |
| 92 |
this.id = id; |
| 93 |
} |
| 94 |
|
| 95 |
private ArrayList<Tree> trees = new ArrayList<Tree>(); |
| 96 |
|
| 97 |
private int minState; |
| 98 |
private int stepSize; |
| 99 |
private String id; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public static TreeTrace loadTreeTrace(Reader r) throws IOException, Importer.ImportException { |
| 111 |
|
| 112 |
BufferedReader reader = new BufferedReader(r); |
| 113 |
|
| 114 |
TreeTrace trace = new TreeTrace(); |
| 115 |
|
| 116 |
dr.evolution.util.TaxonList taxonList = null; |
| 117 |
|
| 118 |
int minState = -1; |
| 119 |
int stepSize = 0; |
| 120 |
|
| 121 |
String line = reader.readLine(); |
| 122 |
|
| 123 |
if (line.toUpperCase().startsWith("#NEXUS")) { |
| 124 |
NexusImporter importer = new NexusImporter(reader); |
| 125 |
Tree[] trees = importer.importTrees(null); |
| 126 |
|
| 127 |
if (trees.length < 2) { |
| 128 |
throw new Importer.ImportException("Less than two trees in the trace file"); |
| 129 |
} |
| 130 |
|
| 131 |
String id1 = trees[0].getId(); |
| 132 |
String id2 = trees[1].getId(); |
| 133 |
|
| 134 |
minState = getStateNumber(id1); |
| 135 |
stepSize = getStateNumber(id2) - minState; |
| 136 |
|
| 137 |
for (Tree tree : trees) { |
| 138 |
trace.add(tree); |
| 139 |
} |
| 140 |
} else { |
| 141 |
NewickImporter importer = new NewickImporter(reader); |
| 142 |
|
| 143 |
while (true) { |
| 144 |
|
| 145 |
int state = 0; |
| 146 |
Tree tree; |
| 147 |
|
| 148 |
try { |
| 149 |
state = importer.readInteger(); |
| 150 |
tree = importer.importTree(taxonList); |
| 151 |
|
| 152 |
if (taxonList == null) { |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
taxonList = tree; |
| 158 |
} |
| 159 |
} catch (Importer.ImportException ie) { |
| 160 |
System.out.println("Error reading tree for state " + state); |
| 161 |
throw ie; |
| 162 |
} catch (EOFException e) { |
| 163 |
break; |
| 164 |
} |
| 165 |
|
| 166 |
if (minState == -1) { |
| 167 |
minState = state; |
| 168 |
} else if (stepSize == 0) { |
| 169 |
stepSize = state - minState; |
| 170 |
} |
| 171 |
|
| 172 |
trace.add(tree); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
trace.setMinimumState(minState); |
| 177 |
trace.setStepSize(stepSize); |
| 178 |
|
| 179 |
return trace; |
| 180 |
} |
| 181 |
|
| 182 |
private static int getStateNumber(String id) throws Importer.ImportException { |
| 183 |
try { |
| 184 |
if (id.indexOf('_') != -1) { |
| 185 |
return Integer.parseInt(id.substring(id.indexOf('_') + 1)); |
| 186 |
} else if (id.indexOf('.') != -1) { |
| 187 |
int rep = Integer.parseInt(id.substring(id.indexOf('.') + 1)); |
| 188 |
if (rep == 1) rep = 0; |
| 189 |
return rep; |
| 190 |
} |
| 191 |
throw new NumberFormatException(); |
| 192 |
} catch (NumberFormatException nfe) { |
| 193 |
throw new Importer.ImportException("Bad state number in tree label '" + id + "', the state must be preceded by an underscore(_)."); |
| 194 |
} |
| 195 |
} |
| 196 |
} |