| 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.inference.trace.TraceException; |
| 32 |
import dr.util.Version; |
| 33 |
|
| 34 |
import java.io.*; |
| 35 |
import java.util.Hashtable; |
| 36 |
|
| 37 |
import dr.app.beast.BeastVersion; |
| 38 |
import dr.app.util.Arguments; |
| 39 |
import dr.app.util.Utils; |
| 40 |
import dr.inference.trace.TraceException; |
| 41 |
import dr.util.Version; |
| 42 |
|
| 43 |
import java.io.*; |
| 44 |
import java.util.*; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public class MakeAncestralSequenceAnnotatedTree { |
| 57 |
|
| 58 |
private final static Version version = new BeastVersion(); |
| 59 |
|
| 60 |
|
| 61 |
public MakeAncestralSequenceAnnotatedTree(int thin, int skip, String outInputFileName, String fastasInputFileName, |
| 62 |
String treesInputfileName, String outputFileName) throws IOException, TraceException { |
| 63 |
|
| 64 |
File outInputFile = new File(outInputFileName); |
| 65 |
|
| 66 |
if (outInputFile.isFile()) { |
| 67 |
System.out.println("Analysing *.out file: " + outInputFileName); |
| 68 |
} else { |
| 69 |
System.err.println(outInputFileName + " does not exist!"); |
| 70 |
System.exit(0); |
| 71 |
} |
| 72 |
|
| 73 |
File fastasInputFile = new File(fastasInputFileName); |
| 74 |
|
| 75 |
if (fastasInputFile.isFile()) { |
| 76 |
System.out.println("Analysing *.fastas file: " + fastasInputFileName); |
| 77 |
} else { |
| 78 |
System.err.println(fastasInputFileName + " does not exist!"); |
| 79 |
System.exit(0); |
| 80 |
} |
| 81 |
|
| 82 |
File treesInputfile = new File(treesInputfileName); |
| 83 |
|
| 84 |
if (treesInputfile.isFile()) { |
| 85 |
System.out.println("Analysing *.trees file: " + treesInputfileName); |
| 86 |
} else { |
| 87 |
System.err.println(treesInputfileName + " does not exist!"); |
| 88 |
System.exit(0); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
if (outputFileName != null) { |
| 93 |
FileOutputStream outputStream = new FileOutputStream(outputFileName); |
| 94 |
System.setOut(new PrintStream(outputStream)); |
| 95 |
} |
| 96 |
|
| 97 |
analyze(outInputFile, fastasInputFile, treesInputfile, skip, thin); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
private void analyze(File outInputFile, File fastasInputFile, File treesInputfileName, int skip, int thin) throws TraceException { |
| 106 |
|
| 107 |
try { |
| 108 |
BufferedReader outFileReader = new BufferedReader(new FileReader(outInputFile)); |
| 109 |
BufferedReader fastasFileReader = new BufferedReader(new FileReader(fastasInputFile)); |
| 110 |
BufferedReader treesFileReader = new BufferedReader(new FileReader(treesInputfileName)); |
| 111 |
|
| 112 |
|
| 113 |
String line; |
| 114 |
|
| 115 |
int count = 0; |
| 116 |
int printedCount = 0; |
| 117 |
String substModel = null; |
| 118 |
|
| 119 |
ArrayList<String> trees = new ArrayList<String>(); |
| 120 |
Hashtable<String, Boolean> translateHash = new Hashtable<String, Boolean>(); |
| 121 |
Hashtable<String, Boolean> taxaHash = new Hashtable<String, Boolean>(); |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
line = outFileReader.readLine(); |
| 129 |
while(line != null) { |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
if(line.matches("subst model\\d* = .*?")) { |
| 134 |
substModel = line.replaceFirst("subst model\\d* = ", "").replaceFirst("\\(.*\\/(.*)\\.dat\\)/\\(","").replaceFirst("\\.dat\\)", "").replaceAll(" \\+ ", "+"); |
| 135 |
line = outFileReader.readLine(); |
| 136 |
} |
| 137 |
else if(line.matches("iterations = \\d+")) { |
| 138 |
int iterations = Integer.parseInt(line.replaceFirst("iterations = ","")); |
| 139 |
if(iterations % 10 ==0) { |
| 140 |
|
| 141 |
double likelihood = 0; |
| 142 |
String model = null; |
| 143 |
search: while((line=outFileReader.readLine()) != null) { |
| 144 |
|
| 145 |
if(line.matches("iterations = \\d+")) { |
| 146 |
break search; |
| 147 |
} |
| 148 |
else if (line.matches(".*?likelihood = -\\d+.*?")) { |
| 149 |
likelihood = Double.parseDouble(line.replaceFirst(".+likelihood = ", "") |
| 150 |
.replaceFirst("\\s.+", "")); |
| 151 |
} |
| 152 |
else if (line.matches("\\s+Heat:beta = .+")) { |
| 153 |
model = line.replaceAll(" = ", "=").replaceAll("::", "_").replaceAll("\\*", "") |
| 154 |
.replaceAll("^ ", "").replaceAll("\\s+", ","); |
| 155 |
} |
| 156 |
|
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
String tree = treesFileReader.readLine(); |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
String line2; |
| 167 |
int previousLineLength=1; |
| 168 |
search: while((line2=fastasFileReader.readLine()) != null) { |
| 169 |
if(line2.matches("iterations = \\d+")) { |
| 170 |
int fastaIterations = Integer.parseInt(line2.replaceFirst("iterations = ","")); |
| 171 |
if(fastaIterations!=iterations) { |
| 172 |
throw new RuntimeException("Iterations is not the same: " + |
| 173 |
fastaIterations + ", " + iterations); |
| 174 |
} |
| 175 |
} |
| 176 |
else if(line2.length()==0 && previousLineLength==0) { |
| 177 |
break search; |
| 178 |
} |
| 179 |
else if(line2.matches(">.+")) { |
| 180 |
|
| 181 |
String sequence = fastasFileReader.readLine(); |
| 182 |
String sequenceName = line2.replaceFirst(">","").replaceFirst("\\s+.+",""); |
| 183 |
if(tree.matches(".*\\(\\s*"+sequenceName+":.+")) { |
| 184 |
taxaHash.put(sequenceName, false); |
| 185 |
tree = tree.replaceFirst("\\(\\s*"+sequenceName+":", "("+sequenceName+"[&states="+sequence+"]:"); |
| 186 |
} |
| 187 |
else if(tree.matches(".*,\\s*"+sequenceName+":.+")) { |
| 188 |
taxaHash.put(sequenceName, false); |
| 189 |
tree = tree.replaceFirst(",\\s*"+sequenceName+":", ","+sequenceName+"[&states="+sequence+"]:"); |
| 190 |
} |
| 191 |
else if(tree.matches(".*\\)\\s*"+sequenceName+":.+")) { |
| 192 |
tree = tree.replaceFirst("\\)\\s*"+sequenceName+":", ")"+sequenceName+"[&states="+sequence+"]:"); |
| 193 |
} |
| 194 |
else if(tree.matches(".*\\)\\s*"+sequenceName+";.*")) { |
| 195 |
tree = tree.replaceFirst("\\)\\s*"+sequenceName+";", ")"+sequenceName+"[&states="+sequence+"];"); |
| 196 |
} |
| 197 |
else { |
| 198 |
throw new RuntimeException("Sequence name " + sequenceName + " does not exist in tree " + tree); |
| 199 |
} |
| 200 |
|
| 201 |
translateHash.put(sequenceName, false); |
| 202 |
} |
| 203 |
previousLineLength = line2.length(); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
if((count % thin)==0 && count >= skip) { |
| 209 |
trees.add("tree STATE_"+iterations+" [&lnL="+likelihood+ |
| 210 |
",subst="+substModel+"," + model +"] = [&U] "+ tree); |
| 211 |
printedCount++; |
| 212 |
} |
| 213 |
count++; |
| 214 |
|
| 215 |
} |
| 216 |
else { |
| 217 |
line = outFileReader.readLine(); |
| 218 |
} |
| 219 |
|
| 220 |
} |
| 221 |
else { |
| 222 |
line = outFileReader.readLine(); |
| 223 |
} |
| 224 |
|
| 225 |
} |
| 226 |
outFileReader.close(); |
| 227 |
|
| 228 |
|
| 229 |
report(trees, translateHash.keySet().toArray(new String[translateHash.size()]), taxaHash.keySet().toArray(new String[taxaHash.size()])); |
| 230 |
} catch (IOException e) { |
| 231 |
System.err.println("Error Parsing Input log: " + e.getMessage()); |
| 232 |
} |
| 233 |
|
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
private void report(ArrayList<String> trees, String[] translateList, String[] taxaList) { |
| 240 |
Arrays.sort(taxaList); |
| 241 |
Arrays.sort(translateList); |
| 242 |
|
| 243 |
System.out.println("#NEXUS\n\nBegin taxa;"); |
| 244 |
System.out.println("\tDimensions ntaxa=" + taxaList.length + ";\n\tTaxlabels"); |
| 245 |
for(String taxaName : taxaList) { |
| 246 |
System.out.println("\t\t"+taxaName); |
| 247 |
} |
| 248 |
System.out.println("\t\t;\nEnd;\b\bBegin trees;\n\tTranslate"); |
| 249 |
for(int i=0; i<translateList.length; i++) { |
| 250 |
System.out.println("\t\t"+(i+1)+" "+translateList[i]); |
| 251 |
} |
| 252 |
for(String tree : trees) { |
| 253 |
for(int i=0; i<translateList.length; i++) { |
| 254 |
int index = -1; |
| 255 |
if((index = tree.indexOf("("+translateList[i]+"[")) > -1 ) { |
| 256 |
tree = tree.substring(0, index) + "(" + (i+1) + "[" + tree.substring(index+translateList[i].length()+2, tree.length()); |
| 257 |
} |
| 258 |
else if((index = tree.indexOf(","+translateList[i]+"[")) > -1 ) { |
| 259 |
tree = tree.substring(0, index) + "," + (i+1) + "[" + tree.substring(index+translateList[i].length()+2, tree.length()); |
| 260 |
} |
| 261 |
else if((index = tree.indexOf(")"+translateList[i]+"[")) > -1 ) { |
| 262 |
tree = tree.substring(0, index) + ")" + (i+1) + "[" + tree.substring(index+translateList[i].length()+2, tree.length()); |
| 263 |
} |
| 264 |
|
| 265 |
} |
| 266 |
System.out.println(tree); |
| 267 |
} |
| 268 |
System.out.println("End;"); |
| 269 |
|
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
public static void printTitle() { |
| 274 |
System.out.println(); |
| 275 |
centreLine("MakeAncestralSequenceAnnotatedTree " + version.getVersionString() + ", " + version.getDateString(), 60); |
| 276 |
centreLine("BAli-Phy MCMC Output analysis", 60); |
| 277 |
centreLine("by", 60); |
| 278 |
centreLine("Wai Lok Sibon Li and Marc A. Suchard", 60); |
| 279 |
System.out.println(); |
| 280 |
centreLine("David Geffen School of Medicine", 60); |
| 281 |
centreLine("University of California, Los Angeles", 60); |
| 282 |
centreLine("[email protected]", 60); |
| 283 |
centreLine("and", 60); |
| 284 |
centreLine("[email protected]",60); |
| 285 |
System.out.println(); |
| 286 |
System.out.println(); |
| 287 |
} |
| 288 |
|
| 289 |
public static void centreLine(String line, int pageWidth) { |
| 290 |
int n = pageWidth - line.length(); |
| 291 |
int n1 = n / 2; |
| 292 |
for (int i = 0; i < n1; i++) { |
| 293 |
System.out.print(" "); |
| 294 |
} |
| 295 |
System.out.println(line); |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
public static void printUsage(Arguments arguments) { |
| 300 |
|
| 301 |
arguments.printUsage("makeancestralsequenceannotatedtree", "[<*.out file-name> <*.fastas file-name> <*.trees file-name> [<output-file-name>]]"); |
| 302 |
System.out.println(); |
| 303 |
System.out.println(" Example: ..."); |
| 304 |
System.out.println(" Example: ..."); |
| 305 |
System.out.println(); |
| 306 |
|
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
public static void main(String[] args) throws IOException, TraceException { |
| 313 |
|
| 314 |
printTitle(); |
| 315 |
|
| 316 |
Arguments arguments = new Arguments( |
| 317 |
new Arguments.Option[]{ |
| 318 |
new Arguments.IntegerOption("thin", "thin"), |
| 319 |
new Arguments.IntegerOption("skip", "skip"), |
| 320 |
new Arguments.Option("help", "option to print this message") |
| 321 |
}); |
| 322 |
|
| 323 |
try { |
| 324 |
arguments.parseArguments(args); |
| 325 |
} catch (Arguments.ArgumentException ae) { |
| 326 |
System.out.println(ae); |
| 327 |
printUsage(arguments); |
| 328 |
System.exit(1); |
| 329 |
} |
| 330 |
|
| 331 |
if (arguments.hasOption("help")) { |
| 332 |
printUsage(arguments); |
| 333 |
System.exit(0); |
| 334 |
} |
| 335 |
|
| 336 |
int skip = -1; |
| 337 |
if (arguments.hasOption("skip")) { |
| 338 |
skip = arguments.getIntegerOption("skip"); |
| 339 |
} |
| 340 |
int thin = -1; |
| 341 |
if (arguments.hasOption("thin")) { |
| 342 |
thin = arguments.getIntegerOption("thin"); |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
String outInputFileName = null; |
| 351 |
String fastasInputFileName = null; |
| 352 |
String treesInputfileName = null; |
| 353 |
String outputFileName = null; |
| 354 |
|
| 355 |
String[] args2 = arguments.getLeftoverArguments(); |
| 356 |
|
| 357 |
if (args2.length > 4) { |
| 358 |
System.err.println("Unknown option: " + args2[2]); |
| 359 |
System.err.println(); |
| 360 |
printUsage(arguments); |
| 361 |
System.exit(1); |
| 362 |
} |
| 363 |
|
| 364 |
if (args2.length > 0) { |
| 365 |
outInputFileName = args2[0]; |
| 366 |
} |
| 367 |
if (args2.length > 0) { |
| 368 |
fastasInputFileName = args2[1]; |
| 369 |
} |
| 370 |
if (args2.length > 0) { |
| 371 |
treesInputfileName = args2[2]; |
| 372 |
} |
| 373 |
if (args2.length > 3) { |
| 374 |
outputFileName = args2[3]; |
| 375 |
} |
| 376 |
|
| 377 |
if (outInputFileName == null) { |
| 378 |
|
| 379 |
outInputFileName = Utils.getLoadFileName("MakeAncestralSequenceAnnotatedTree " + version.getVersionString() + " - Select *.out file to analyse"); |
| 380 |
} |
| 381 |
|
| 382 |
if (fastasInputFileName == null) { |
| 383 |
|
| 384 |
fastasInputFileName = Utils.getLoadFileName("MakeAncestralSequenceAnnotatedTree " + version.getVersionString() + " - Select *.fastas file to analyse"); |
| 385 |
} |
| 386 |
|
| 387 |
if (treesInputfileName == null) { |
| 388 |
|
| 389 |
treesInputfileName = Utils.getLoadFileName("MakeAncestralSequenceAnnotatedTree " + version.getVersionString() + " - Select *.trees file to analyse"); |
| 390 |
} |
| 391 |
|
| 392 |
if(skip==-1) { |
| 393 |
System.out.println("Enter skip: "); |
| 394 |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 395 |
skip = Integer.parseInt(br.readLine()); |
| 396 |
} |
| 397 |
if(thin==-1) { |
| 398 |
System.out.println("Enter thin: "); |
| 399 |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 400 |
thin = Integer.parseInt(br.readLine()); |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
new MakeAncestralSequenceAnnotatedTree(thin, skip, outInputFileName, fastasInputFileName, treesInputfileName, outputFileName); |
| 409 |
|
| 410 |
System.exit(0); |
| 411 |
} |
| 412 |
} |
| 413 |
|