| 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.evolution.io.Importer; |
| 31 |
import dr.evolution.io.NexusImporter; |
| 32 |
import dr.evolution.io.TreeImporter; |
| 33 |
import dr.evolution.tree.Tree; |
| 34 |
import dr.util.Version; |
| 35 |
|
| 36 |
import java.io.FileReader; |
| 37 |
import java.io.IOException; |
| 38 |
import java.io.PrintWriter; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public class DensityPlotter { |
| 45 |
|
| 46 |
private final static Version version = new BeastVersion(); |
| 47 |
|
| 48 |
public DensityPlotter(int burnin, |
| 49 |
String inputFileName, |
| 50 |
String outputFileName, |
| 51 |
String trait1AttributeName, |
| 52 |
String trait2AttributeName, |
| 53 |
int timeBinCount, |
| 54 |
int value1BinCount, |
| 55 |
int value2BinCount, |
| 56 |
double timeUpper, |
| 57 |
double timeLower, |
| 58 |
double value1Upper, |
| 59 |
double value1Lower, |
| 60 |
double value2Upper, |
| 61 |
double value2Lower, |
| 62 |
boolean printHeaders, |
| 63 |
boolean outputTIFF, |
| 64 |
boolean logScale |
| 65 |
|
| 66 |
) throws IOException { |
| 67 |
|
| 68 |
System.out.println("Reading trees..."); |
| 69 |
|
| 70 |
DensityMap[] densityMaps; |
| 71 |
|
| 72 |
if (trait2AttributeName != null) { |
| 73 |
densityMaps = new DensityMap[timeBinCount]; |
| 74 |
for (int i = 0; i < densityMaps.length; i++) { |
| 75 |
densityMaps[i] = new DensityMap(i, value1BinCount, value2BinCount, |
| 76 |
value1Upper, value1Lower, value2Upper, value2Lower, logScale); |
| 77 |
} |
| 78 |
} else { |
| 79 |
densityMaps = new DensityMap[]{ |
| 80 |
new DensityMap(0, timeBinCount, value1BinCount, |
| 81 |
timeUpper, timeLower, value1Upper, value1Lower, logScale) |
| 82 |
|
| 83 |
}; |
| 84 |
} |
| 85 |
|
| 86 |
boolean firstTree = true; |
| 87 |
double maxTreeHeight = 0.0; |
| 88 |
FileReader fileReader = new FileReader(inputFileName); |
| 89 |
TreeImporter importer = new NexusImporter(fileReader); |
| 90 |
try { |
| 91 |
while (importer.hasTree()) { |
| 92 |
Tree tree = importer.importNextTree(); |
| 93 |
|
| 94 |
if (firstTree) { |
| 95 |
firstTree = false; |
| 96 |
} |
| 97 |
|
| 98 |
if (totalTrees >= burnin) { |
| 99 |
if (trait2AttributeName != null) { |
| 100 |
for (int i = 0; i < densityMaps.length; i++) { |
| 101 |
densityMaps[i].calibrate(tree, trait1AttributeName, trait2AttributeName); |
| 102 |
} |
| 103 |
} else { |
| 104 |
densityMaps[0].calibrate(tree, trait1AttributeName); |
| 105 |
} |
| 106 |
if (tree.getNodeHeight(tree.getRoot()) > maxTreeHeight) { |
| 107 |
maxTreeHeight = tree.getNodeHeight(tree.getRoot()); |
| 108 |
} |
| 109 |
|
| 110 |
totalTreesUsed += 1; |
| 111 |
} |
| 112 |
totalTrees += 1; |
| 113 |
|
| 114 |
} |
| 115 |
} catch (Importer.ImportException e) { |
| 116 |
System.err.println("Error Parsing Input Tree: " + e.getMessage()); |
| 117 |
return; |
| 118 |
} |
| 119 |
fileReader.close(); |
| 120 |
|
| 121 |
double startTime = 0.0; |
| 122 |
double endTime = maxTreeHeight; |
| 123 |
if (timeUpper != Double.POSITIVE_INFINITY) { |
| 124 |
endTime = timeUpper; |
| 125 |
} |
| 126 |
if (timeLower != Double.NEGATIVE_INFINITY) { |
| 127 |
startTime = timeLower; |
| 128 |
} |
| 129 |
double deltaTime = (endTime - startTime) / (double) (timeBinCount); |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
fileReader = new FileReader(inputFileName); |
| 135 |
importer = new NexusImporter(fileReader); |
| 136 |
try { |
| 137 |
totalTrees = 0; |
| 138 |
while (importer.hasTree()) { |
| 139 |
Tree tree = importer.importNextTree(); |
| 140 |
|
| 141 |
if (totalTrees >= burnin) { |
| 142 |
if (trait2AttributeName != null) { |
| 143 |
double sampleTime = startTime; |
| 144 |
for (int i = 0; i < densityMaps.length; i++) { |
| 145 |
densityMaps[i].addTree(tree, sampleTime, trait1AttributeName, trait2AttributeName); |
| 146 |
sampleTime += deltaTime; |
| 147 |
} |
| 148 |
} else { |
| 149 |
densityMaps[0].addTree(tree, trait1AttributeName); |
| 150 |
} |
| 151 |
} |
| 152 |
totalTrees += 1; |
| 153 |
|
| 154 |
} |
| 155 |
} catch (Importer.ImportException e) { |
| 156 |
System.err.println("Error Parsing Input Tree: " + e.getMessage()); |
| 157 |
return; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
if (trait2AttributeName != null) { |
| 162 |
for (int i = 0; i < densityMaps.length; i++) { |
| 163 |
if (outputTIFF) { |
| 164 |
densityMaps[i].writeAsTIFF(outputFileName + "." + String.format("%03d", i) + ".tif"); |
| 165 |
|
| 166 |
} else { |
| 167 |
PrintWriter printWriter = new PrintWriter(outputFileName + "." + String.format("%03d", i)); |
| 168 |
printWriter.println(densityMaps[i].toString(printHeaders)); |
| 169 |
printWriter.close(); |
| 170 |
} |
| 171 |
|
| 172 |
} |
| 173 |
} else { |
| 174 |
PrintWriter printWriter = new PrintWriter(outputFileName); |
| 175 |
printWriter.println(densityMaps[0].toString(printHeaders)); |
| 176 |
printWriter.close(); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
} |
| 182 |
|
| 183 |
int totalTrees = 0; |
| 184 |
int totalTreesUsed = 0; |
| 185 |
|
| 186 |
public static void printTitle() { |
| 187 |
System.out.println(); |
| 188 |
centreLine("DensityPlotter " + version.getVersionString() + ", " + version.getDateString(), 60); |
| 189 |
centreLine("BEAST time vs. parameter density analysis", 60); |
| 190 |
centreLine("by", 60); |
| 191 |
centreLine("Andrew Rambaut, Marc A. Suchard and Alexei J. Drummond", 60); |
| 192 |
System.out.println(); |
| 193 |
System.out.println(); |
| 194 |
} |
| 195 |
|
| 196 |
public static void centreLine(String line, int pageWidth) { |
| 197 |
int n = pageWidth - line.length(); |
| 198 |
int n1 = n / 2; |
| 199 |
for (int i = 0; i < n1; i++) { |
| 200 |
System.out.print(" "); |
| 201 |
} |
| 202 |
System.out.println(line); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
public static void printUsage(Arguments arguments) { |
| 207 |
|
| 208 |
arguments.printUsage("densityplotter", "<input-file-name> [<output-file-name>]"); |
| 209 |
System.out.println(); |
| 210 |
System.out.println(" Example: densityplotter -burnin 100 -trait rate test.trees density.plot"); |
| 211 |
System.out.println(); |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
public static void main(String[] args) throws IOException { |
| 216 |
|
| 217 |
String inputFileName = null; |
| 218 |
String outputFileName = null; |
| 219 |
|
| 220 |
printTitle(); |
| 221 |
|
| 222 |
Arguments arguments = new Arguments( |
| 223 |
new Arguments.Option[]{ |
| 224 |
new Arguments.IntegerOption("burnin", "the number of states to be considered as 'burn-in' [default = 0]"), |
| 225 |
new Arguments.StringOption("trait", "trait_name", "specifies an attribute to use to create a density map [default = rate]"), |
| 226 |
new Arguments.StringOption("trait2", "trait_name", "specifies a second attribute to use to create a density map [default = rate]"), |
| 227 |
new Arguments.IntegerOption("time_bins", "the number of bins for the time axis of the density map [default = 100]"), |
| 228 |
new Arguments.IntegerOption("value_bins", "the number of bins for the value axis of the density map [default = 20]"), |
| 229 |
new Arguments.RealOption("time_upper", "the upper time bound for the density map [default = max tree age]"), |
| 230 |
new Arguments.RealOption("time_lower", "the lower time bound for the density map [default = 0]"), |
| 231 |
new Arguments.RealOption("value_upper", "the upper value bound for the density map [default = max value]"), |
| 232 |
new Arguments.RealOption("value_lower", "the lower value bound for the density map [default = min value]"), |
| 233 |
new Arguments.RealOption("value2_upper", "the upper second value bound for the density map [default = max value]"), |
| 234 |
new Arguments.RealOption("value2_lower", "the lower second value bound for the density map [default = min value]"), |
| 235 |
new Arguments.StringOption("headers", "with_headers", "prints row/column labels in output [default = true"), |
| 236 |
new Arguments.Option("tiff", "output in TIFF format"), |
| 237 |
new Arguments.Option("help", "option to print this message"), |
| 238 |
new Arguments.Option("logScale", "transform trait to log scale") |
| 239 |
}); |
| 240 |
|
| 241 |
try { |
| 242 |
arguments.parseArguments(args); |
| 243 |
} catch (Arguments.ArgumentException ae) { |
| 244 |
System.out.println(ae); |
| 245 |
printUsage(arguments); |
| 246 |
System.exit(1); |
| 247 |
} |
| 248 |
|
| 249 |
if (arguments.hasOption("help")) { |
| 250 |
printUsage(arguments); |
| 251 |
System.exit(0); |
| 252 |
} |
| 253 |
|
| 254 |
int burnin = -1; |
| 255 |
if (arguments.hasOption("burnin")) { |
| 256 |
burnin = arguments.getIntegerOption("burnin"); |
| 257 |
} |
| 258 |
|
| 259 |
String trait1AttributeName = "rate"; |
| 260 |
String trait2AttributeName = null; |
| 261 |
int timeBinCount = 100; |
| 262 |
int valueBinCount = 25; |
| 263 |
int value2BinCount = 25; |
| 264 |
double timeUpper = Double.POSITIVE_INFINITY; |
| 265 |
double timeLower = Double.NEGATIVE_INFINITY; |
| 266 |
double valueUpper = Double.POSITIVE_INFINITY; |
| 267 |
double valueLower = Double.NEGATIVE_INFINITY; |
| 268 |
double value2Upper = Double.POSITIVE_INFINITY; |
| 269 |
double value2Lower = Double.NEGATIVE_INFINITY; |
| 270 |
boolean logScale = false; |
| 271 |
|
| 272 |
if (arguments.hasOption("logScale")) |
| 273 |
logScale = true; |
| 274 |
|
| 275 |
if (arguments.hasOption("trait")) { |
| 276 |
trait1AttributeName = arguments.getStringOption("trait"); |
| 277 |
} |
| 278 |
|
| 279 |
if (arguments.hasOption("trait2")) { |
| 280 |
trait2AttributeName = arguments.getStringOption("trait2"); |
| 281 |
} |
| 282 |
|
| 283 |
if (arguments.hasOption("time_bins")) { |
| 284 |
timeBinCount = arguments.getIntegerOption("time_bins"); |
| 285 |
} |
| 286 |
|
| 287 |
if (arguments.hasOption("value_bins")) { |
| 288 |
valueBinCount = arguments.getIntegerOption("value_bins"); |
| 289 |
} |
| 290 |
value2BinCount = valueBinCount; |
| 291 |
|
| 292 |
if (arguments.hasOption("value2_bins")) { |
| 293 |
value2BinCount = arguments.getIntegerOption("value2_bins"); |
| 294 |
} |
| 295 |
|
| 296 |
if (arguments.hasOption("time_upper")) { |
| 297 |
timeUpper = arguments.getRealOption("time_upper"); |
| 298 |
} |
| 299 |
|
| 300 |
if (arguments.hasOption("time_lower")) { |
| 301 |
timeLower = arguments.getRealOption("time_lower"); |
| 302 |
} |
| 303 |
|
| 304 |
if (arguments.hasOption("value_upper")) { |
| 305 |
valueUpper = arguments.getRealOption("value_upper"); |
| 306 |
} |
| 307 |
|
| 308 |
if (arguments.hasOption("value_lower")) { |
| 309 |
valueLower = arguments.getRealOption("value_lower"); |
| 310 |
} |
| 311 |
|
| 312 |
if (arguments.hasOption("value2_upper")) { |
| 313 |
value2Upper = arguments.getRealOption("value2_upper"); |
| 314 |
} |
| 315 |
|
| 316 |
if (arguments.hasOption("value2_lower")) { |
| 317 |
value2Lower = arguments.getRealOption("value2_lower"); |
| 318 |
} |
| 319 |
|
| 320 |
boolean printHeaders = true; |
| 321 |
if (arguments.hasOption("headers")) { |
| 322 |
String text = arguments.getStringOption("headers"); |
| 323 |
if (text.toUpperCase().compareTo("FALSE") == 0) |
| 324 |
printHeaders = false; |
| 325 |
} |
| 326 |
|
| 327 |
boolean outputTIFF = false; |
| 328 |
if (arguments.hasOption("tiff")) |
| 329 |
outputTIFF = true; |
| 330 |
|
| 331 |
String[] args2 = arguments.getLeftoverArguments(); |
| 332 |
|
| 333 |
if (args2.length > 2) { |
| 334 |
System.err.println("Unknown option: " + args2[2]); |
| 335 |
System.err.println(); |
| 336 |
printUsage(arguments); |
| 337 |
System.exit(1); |
| 338 |
} |
| 339 |
|
| 340 |
if (args2.length == 2) { |
| 341 |
inputFileName = args2[0]; |
| 342 |
outputFileName = args2[1]; |
| 343 |
} else { |
| 344 |
System.err.println("Missing input or output file name"); |
| 345 |
printUsage(arguments); |
| 346 |
System.exit(1); |
| 347 |
} |
| 348 |
|
| 349 |
new DensityPlotter(burnin, |
| 350 |
inputFileName, |
| 351 |
outputFileName, |
| 352 |
trait1AttributeName, |
| 353 |
trait2AttributeName, |
| 354 |
timeBinCount, |
| 355 |
valueBinCount, |
| 356 |
value2BinCount, |
| 357 |
timeUpper, |
| 358 |
timeLower, |
| 359 |
valueUpper, |
| 360 |
valueLower, |
| 361 |
value2Upper, |
| 362 |
value2Lower, |
| 363 |
printHeaders, outputTIFF, logScale |
| 364 |
); |
| 365 |
|
| 366 |
System.exit(0); |
| 367 |
} |
| 368 |
|
| 369 |
} |