| 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.app.tools; |
| 27 |
|
| 28 |
import dr.app.beast.BeastVersion; |
| 29 |
import dr.app.util.Arguments; |
| 30 |
import dr.app.util.Utils; |
| 31 |
import dr.app.treestat.statistics.TreeLength; |
| 32 |
import dr.inference.trace.TraceException; |
| 33 |
import dr.util.Version; |
| 34 |
import dr.evolution.io.TreeImporter; |
| 35 |
import dr.evolution.io.NexusImporter; |
| 36 |
import dr.evolution.io.Importer; |
| 37 |
import dr.evolution.tree.Tree; |
| 38 |
|
| 39 |
import java.io.*; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public class TreeLengthFinder { |
| 46 |
|
| 47 |
private final static Version version = new BeastVersion(); |
| 48 |
|
| 49 |
|
| 50 |
public TreeLengthFinder(int burnin, String inputFileName, String outputFileName |
| 51 |
|
| 52 |
) throws IOException, TraceException { |
| 53 |
|
| 54 |
File parentFile = new File(inputFileName); |
| 55 |
|
| 56 |
if (parentFile.isDirectory()) { |
| 57 |
System.out.println("Analysing all trees files below directory: " + inputFileName); |
| 58 |
} else if (parentFile.isFile()) { |
| 59 |
System.out.println("Analysing tree file: " + inputFileName); |
| 60 |
} else { |
| 61 |
System.err.println(inputFileName + " does not exist!"); |
| 62 |
System.exit(0); |
| 63 |
} |
| 64 |
|
| 65 |
if (outputFileName != null) { |
| 66 |
FileOutputStream outputStream = new FileOutputStream(outputFileName); |
| 67 |
System.setOut(new PrintStream(outputStream)); |
| 68 |
} |
| 69 |
|
| 70 |
analyze(parentFile, burnin); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
private void analyze(File file, int burnin) throws TraceException { |
| 82 |
|
| 83 |
if (file.isFile()) { |
| 84 |
try { |
| 85 |
|
| 86 |
String name = file.getCanonicalPath(); |
| 87 |
|
| 88 |
report(name, burnin); |
| 89 |
|
| 90 |
|
| 91 |
} catch (IOException e) { |
| 92 |
|
| 93 |
} |
| 94 |
} else { |
| 95 |
File[] files = file.listFiles(); |
| 96 |
for (File f : files) { |
| 97 |
if (f.isDirectory()) { |
| 98 |
analyze(f, burnin); |
| 99 |
|
| 100 |
} else if (f.getName().endsWith(".trees")) { |
| 101 |
analyze(f, burnin); |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
private void report(String name, int burnin) { |
| 115 |
double treeLength = 0.0; |
| 116 |
int count = 0; |
| 117 |
|
| 118 |
try { |
| 119 |
FileReader fileReader = new FileReader(new File(name)); |
| 120 |
TreeImporter importer = new NexusImporter(fileReader); |
| 121 |
while (importer.hasTree()) { |
| 122 |
Tree tree = importer.importNextTree(); |
| 123 |
if(count>=burnin) { |
| 124 |
treeLength += TreeLength.FACTORY.createStatistic().getSummaryStatistic(tree)[0]; |
| 125 |
|
| 126 |
} |
| 127 |
count++; |
| 128 |
} |
| 129 |
treeLength /= (count-burnin); |
| 130 |
System.out.println(name + "\t" + burnin + "\t" + treeLength); |
| 131 |
} catch (Importer.ImportException e) { |
| 132 |
System.err.println("Error Parsing Input Tree: " + e.getMessage()); |
| 133 |
} catch (IOException e) { |
| 134 |
System.err.println("Error Parsing Input Tree: " + e.getMessage()); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
public static void printTitle() { |
| 139 |
System.out.println(); |
| 140 |
centreLine("TreeLengthFinder " + version.getVersionString() + ", " + version.getDateString(), 60); |
| 141 |
centreLine("MCMC Output analysis", 60); |
| 142 |
centreLine("by", 60); |
| 143 |
centreLine("Andrew Rambaut and Alexei J. Drummond", 60); |
| 144 |
System.out.println(); |
| 145 |
centreLine("Institute of Evolutionary Biology", 60); |
| 146 |
centreLine("University of Edinburgh", 60); |
| 147 |
centreLine("[email protected]", 60); |
| 148 |
System.out.println(); |
| 149 |
centreLine("Department of Computer Science", 60); |
| 150 |
centreLine("University of Auckland", 60); |
| 151 |
centreLine("[email protected]", 60); |
| 152 |
System.out.println(); |
| 153 |
System.out.println(); |
| 154 |
} |
| 155 |
|
| 156 |
public static void centreLine(String line, int pageWidth) { |
| 157 |
int n = pageWidth - line.length(); |
| 158 |
int n1 = n / 2; |
| 159 |
for (int i = 0; i < n1; i++) { |
| 160 |
System.out.print(" "); |
| 161 |
} |
| 162 |
System.out.println(line); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
public static void printUsage(Arguments arguments) { |
| 167 |
|
| 168 |
|
| 169 |
arguments.printUsage("treelengthfinder", "[-burnin <burnin>][<input-file-name> [<output-file-name>]]"); |
| 170 |
System.out.println(); |
| 171 |
System.out.println(" Example: treelengthfinder test.tree"); |
| 172 |
System.out.println(" Example: treelengthfinder -burnin 10000 trees.log out.txt"); |
| 173 |
System.out.println(); |
| 174 |
|
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
public static void main(String[] args) throws IOException, TraceException { |
| 179 |
|
| 180 |
printTitle(); |
| 181 |
|
| 182 |
Arguments arguments = new Arguments( |
| 183 |
new Arguments.Option[]{ |
| 184 |
new Arguments.IntegerOption("burnin", "the number of states to be considered as 'burn-in'"), |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
new Arguments.Option("help", "option to print this message") |
| 193 |
}); |
| 194 |
|
| 195 |
try { |
| 196 |
arguments.parseArguments(args); |
| 197 |
} catch (Arguments.ArgumentException ae) { |
| 198 |
System.out.println(ae); |
| 199 |
printUsage(arguments); |
| 200 |
System.exit(1); |
| 201 |
} |
| 202 |
|
| 203 |
if (arguments.hasOption("help")) { |
| 204 |
printUsage(arguments); |
| 205 |
System.exit(0); |
| 206 |
} |
| 207 |
|
| 208 |
int burnin = -1; |
| 209 |
if (arguments.hasOption("burnin")) { |
| 210 |
burnin = arguments.getIntegerOption("burnin"); |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
String inputFileName = null; |
| 227 |
String outputFileName = null; |
| 228 |
|
| 229 |
String[] args2 = arguments.getLeftoverArguments(); |
| 230 |
|
| 231 |
if (args2.length > 2) { |
| 232 |
System.err.println("Unknown option: " + args2[2]); |
| 233 |
System.err.println(); |
| 234 |
printUsage(arguments); |
| 235 |
System.exit(1); |
| 236 |
} |
| 237 |
|
| 238 |
if (args2.length > 0) { |
| 239 |
inputFileName = args2[0]; |
| 240 |
} |
| 241 |
if (args2.length > 1) { |
| 242 |
outputFileName = args2[1]; |
| 243 |
} |
| 244 |
|
| 245 |
if (inputFileName == null) { |
| 246 |
|
| 247 |
inputFileName = Utils.getLoadFileName("TreeLengthFinder " + version.getVersionString() + " - Select tree file to analyse"); |
| 248 |
} |
| 249 |
|
| 250 |
if(burnin==-1) { |
| 251 |
System.out.println("Enter number of trees to burn-in (integer): "); |
| 252 |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 253 |
burnin = Integer.parseInt(br.readLine()); |
| 254 |
} |
| 255 |
|
| 256 |
new TreeLengthFinder(burnin, inputFileName, outputFileName); |
| 257 |
|
| 258 |
System.exit(0); |
| 259 |
} |
| 260 |
} |