| 1 |
package jasper; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.FileNotFoundException; |
| 5 |
import java.io.FileWriter; |
| 6 |
import java.io.IOException; |
| 7 |
import java.util.ArrayList; |
| 8 |
import java.util.Collections; |
| 9 |
|
| 10 |
import shared.Parser; |
| 11 |
import shared.PreParser; |
| 12 |
import shared.Timer; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
public class NCBISparseTreeValidate { |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
public static void main(String[] args) throws FileNotFoundException, IOException { |
| 23 |
|
| 24 |
|
| 25 |
Timer t=new Timer(); |
| 26 |
|
| 27 |
|
| 28 |
NCBISparseTreeValidate x=new NCBISparseTreeValidate(args); |
| 29 |
|
| 30 |
|
| 31 |
x.process(t); |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public NCBISparseTreeValidate(String[] args) { |
| 44 |
|
| 45 |
{ |
| 46 |
PreParser pp=new PreParser(args, getClass(), false); |
| 47 |
args=pp.args; |
| 48 |
outstream=pp.outstream; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
Parser parser=new Parser(); |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
for(int i=0; i<args.length; i++){ |
| 57 |
|
| 58 |
|
| 59 |
String arg=args[i]; |
| 60 |
|
| 61 |
|
| 62 |
String[] split=arg.split("="); |
| 63 |
|
| 64 |
|
| 65 |
String a=split[0].toLowerCase(); |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
String b=split.length>1 ? split[1] : null; |
| 70 |
|
| 71 |
|
| 72 |
if(b!=null && b.equalsIgnoreCase("null")){b=null;} |
| 73 |
|
| 74 |
|
| 75 |
if(a.equals("parse_flag_goes_here")){ |
| 76 |
|
| 77 |
|
| 78 |
}else if(a.equals("sim")){ |
| 79 |
sim=b; |
| 80 |
|
| 81 |
|
| 82 |
}else if(a.equals("tree")){ |
| 83 |
treeFileName=b; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
}else if(a.equals("test")) { |
| 88 |
test=true; |
| 89 |
ncbi=false; |
| 90 |
|
| 91 |
|
| 92 |
}else if(a.equals("mode")) { |
| 93 |
if(b.equals("average")){mode=AVERAGE_IDENTITY_MODE;} |
| 94 |
else if(b.equals("identity")) {mode=IDENTITY_MODE;} |
| 95 |
else if(b.equals("vote")) {mode=VOTE_MODE;} |
| 96 |
else if(b.equals("both")) {mode=BOTH_MODE;} |
| 97 |
else {assert false : "Unknown Mode: " + arg;} |
| 98 |
|
| 99 |
|
| 100 |
}else if(a.equals("writetrees")) { |
| 101 |
writeTrees = true; |
| 102 |
|
| 103 |
}else if(a.equals("allnodes")) { |
| 104 |
printAllNodes = true; |
| 105 |
|
| 106 |
}else if(a.equals("treestoprint")) { |
| 107 |
goodTreesToPrint = badTreesToPrint = Integer.parseInt(b); |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
}else if(a.equals("outpath")) { |
| 112 |
outpath=b; |
| 113 |
if(outpath.endsWith("/")) {System.out.println("output path correct");} |
| 114 |
else {outpath = outpath + "/";} |
| 115 |
|
| 116 |
|
| 117 |
}else if(parser.parse(arg, a, b)){ |
| 118 |
|
| 119 |
|
| 120 |
}else{ |
| 121 |
assert(false) : "Unknown parameter "+args[i]; |
| 122 |
outstream.println("Unknown parameter "+args[i]); |
| 123 |
} |
| 124 |
|
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
void process(Timer t) throws FileNotFoundException, IOException{ |
| 143 |
|
| 144 |
|
| 145 |
tree=new NCBISparseTree(treeFileName); |
| 146 |
|
| 147 |
System.out.println(tree.getOrgCount()); |
| 148 |
|
| 149 |
System.out.println("Mode of analysis: " + mode); |
| 150 |
|
| 151 |
System.out.println("If writing out trees, write all nodes: " + printAllNodes); |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
NCBISparseSimilarityMatrix matrix=new NCBISparseSimilarityMatrix(sim, tree, ncbi); |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
tree.beginTraverse(1); |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
checkSimilarities(tree, matrix); |
| 175 |
|
| 176 |
System.out.println("Node placement check complete."); |
| 177 |
|
| 178 |
|
| 179 |
t.stop(); |
| 180 |
|
| 181 |
outstream.println("Time: \t"+t); |
| 182 |
|
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
void checkSimilarities(NCBISparseTree tree, NCBISparseSimilarityMatrix matrix) { |
| 228 |
|
| 229 |
System.out.println("Begining to compare sequence similarity values to the stated tree structure."); |
| 230 |
|
| 231 |
|
| 232 |
for(NCBITreeNode keyNode : tree.nodeList) { |
| 233 |
|
| 234 |
Integer keyTaxonID = keyNode.taxID; |
| 235 |
|
| 236 |
System.out.println("Analyzing Taxon ID :" + keyTaxonID); |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
if(!keyTaxonID.equals(1) && matrix.numComparisonsByTaxonID(keyTaxonID) > 0) { |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
tree.root.resetRecursively(); |
| 246 |
|
| 247 |
|
| 248 |
tree.setIdentity(keyNode, matrix); |
| 249 |
|
| 250 |
keyNode.votes++; |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
tree.root.percolateIdentityUp(keyNode.nodeId); |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
if(writeTrees == true && outpath != null) { |
| 259 |
|
| 260 |
|
| 261 |
String currentTreeName = outpath + "SimilarityTree_" + keyNode.orgName + "_.dot"; |
| 262 |
|
| 263 |
|
| 264 |
createFile(currentTreeName); |
| 265 |
|
| 266 |
|
| 267 |
writeToFile(currentTreeName, tree.root.toDot(printAllNodes)); |
| 268 |
|
| 269 |
|
| 270 |
} |
| 271 |
|
| 272 |
if(mode==AVERAGE_IDENTITY_MODE) { |
| 273 |
|
| 274 |
NCBITreeNode problemNode = checkAverageIdentityForOneNode(keyNode); |
| 275 |
|
| 276 |
if(problemNode != null) { |
| 277 |
System.out.println("Highest problem node for Taxon ID: " + keyNode.taxID + " is: " + problemNode); |
| 278 |
if(badTreesToPrint > 0) { |
| 279 |
writeTrees(keyNode, tree); |
| 280 |
badTreesToPrint = badTreesToPrint - 1; |
| 281 |
} |
| 282 |
}else if(problemNode == null && goodTreesToPrint > 0) { |
| 283 |
writeTrees(keyNode, tree); |
| 284 |
goodTreesToPrint = goodTreesToPrint - 1; |
| 285 |
} |
| 286 |
} |
| 287 |
else if(mode==VOTE_MODE) { |
| 288 |
NCBITreeNode problemNode = checkVotesForOneNode(keyNode); |
| 289 |
|
| 290 |
if(problemNode != null) { |
| 291 |
System.out.println("Highest problem node for Taxon ID: " + keyNode.taxID + " is: " + problemNode); |
| 292 |
if(badTreesToPrint > 0) { |
| 293 |
writeTrees(keyNode, tree); |
| 294 |
badTreesToPrint--; |
| 295 |
} |
| 296 |
}else if(problemNode == null && goodTreesToPrint > 0) { |
| 297 |
writeTrees(keyNode, tree); |
| 298 |
goodTreesToPrint--; |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
}else if(mode==BOTH_MODE) { |
| 303 |
|
| 304 |
NCBITreeNode voteProblemNode = checkVotesForOneNode(keyNode); |
| 305 |
|
| 306 |
if(voteProblemNode != null) { |
| 307 |
|
| 308 |
NCBITreeNode avgIDProblemNode = checkAverageIdentityForOneNode(keyNode); |
| 309 |
|
| 310 |
if(avgIDProblemNode != null) { |
| 311 |
|
| 312 |
setNodeColors(keyNode, voteProblemNode, avgIDProblemNode); |
| 313 |
|
| 314 |
System.out.println("Problem found at Taxon ID: " + keyNode.taxID |
| 315 |
+ " The highest vote problem node is: " + voteProblemNode |
| 316 |
+ " and the highest identity problem node is: " + avgIDProblemNode); |
| 317 |
|
| 318 |
if(badTreesToPrint > 0) { |
| 319 |
writeTrees(keyNode, tree); |
| 320 |
badTreesToPrint--; |
| 321 |
} |
| 322 |
}else if(avgIDProblemNode == null) { |
| 323 |
keyNode.color = "blue"; |
| 324 |
if(goodTreesToPrint > 0) { |
| 325 |
writeTrees(keyNode, tree); |
| 326 |
goodTreesToPrint--; |
| 327 |
} |
| 328 |
} |
| 329 |
} |
| 330 |
}else { throw new RuntimeException("Uknown Mode" + mode);} |
| 331 |
|
| 332 |
|
| 333 |
} |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
public NCBITreeNode checkVotesForOneNode(final NCBITreeNode keyNode) { |
| 344 |
|
| 345 |
NCBITreeNode current=keyNode.parentNode; |
| 346 |
|
| 347 |
NCBITreeNode previous=keyNode; |
| 348 |
|
| 349 |
NCBITreeNode highestProblemNode = null; |
| 350 |
|
| 351 |
NCBITreeNode problemChild = null; |
| 352 |
|
| 353 |
while(current.parentNode != current) { |
| 354 |
|
| 355 |
int currentMaxVotes = previous.votes; |
| 356 |
|
| 357 |
NCBITreeNode maxChild = previous; |
| 358 |
|
| 359 |
for(NCBITreeNode descendantNode : current.childNodes) { |
| 360 |
|
| 361 |
if(descendantNode.votes > currentMaxVotes) { |
| 362 |
|
| 363 |
currentMaxVotes = descendantNode.votes; |
| 364 |
maxChild = descendantNode; |
| 365 |
|
| 366 |
} |
| 367 |
|
| 368 |
} |
| 369 |
|
| 370 |
if(maxChild != previous) { |
| 371 |
|
| 372 |
current.flaggedNode = true; |
| 373 |
System.out.println("Problem found at: [" + current.taxID + "] Taxon rank: [" + current.taxonomicRank + "]"); |
| 374 |
highestProblemNode = current; |
| 375 |
problemChild = maxChild; |
| 376 |
} |
| 377 |
|
| 378 |
previous = current; |
| 379 |
current = current.parentNode; |
| 380 |
|
| 381 |
} |
| 382 |
if(highestProblemNode != null && highestProblemNode != keyNode.parentNode) { |
| 383 |
problemChild.color = "pink"; |
| 384 |
} |
| 385 |
|
| 386 |
return highestProblemNode == keyNode.parentNode ? null : highestProblemNode; |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
@Deprecated |
| 395 |
public void checkSimilaritiesForOneNode(NCBITreeNode keyNode, NCBISparseSimilarityMatrix matrix) { |
| 396 |
|
| 397 |
|
| 398 |
if(keyNode.parentNode.averageIdentity() != 0.0) { |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
ArrayList<NCBIComparison> keyOrgRow = matrix.getOrgRowByTaxonID(keyNode.taxID); |
| 403 |
|
| 404 |
Collections.sort(keyOrgRow); |
| 405 |
|
| 406 |
|
| 407 |
for(NCBIComparison rowOrgComparison : keyOrgRow) { |
| 408 |
|
| 409 |
NCBITreeNode matrixOrgNode = tree.getNodeByNodeID(rowOrgComparison.refNodeID); |
| 410 |
|
| 411 |
boolean b = isSuspicious(keyNode, matrixOrgNode, rowOrgComparison); |
| 412 |
keyNode.flaggedNode = b; |
| 413 |
if(b) { |
| 414 |
printSuspicious(keyNode, rowOrgComparison, matrixOrgNode); |
| 415 |
} |
| 416 |
} |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
public NCBITreeNode checkAverageIdentityForOneNode(final NCBITreeNode keyNode) { |
| 422 |
NCBITreeNode current=keyNode.parentNode; |
| 423 |
|
| 424 |
NCBITreeNode previous=keyNode; |
| 425 |
|
| 426 |
NCBITreeNode highestProblemNode = null; |
| 427 |
|
| 428 |
NCBITreeNode problemChild = null; |
| 429 |
|
| 430 |
while(current.parentNode != current) { |
| 431 |
|
| 432 |
double currentMaxAverage = previous.averageIdentity(); |
| 433 |
NCBITreeNode maxChild = previous; |
| 434 |
|
| 435 |
for(NCBITreeNode descendantNode : current.childNodes) { |
| 436 |
|
| 437 |
if(descendantNode.averageIdentity() > currentMaxAverage) { |
| 438 |
|
| 439 |
currentMaxAverage = descendantNode.averageIdentity(); |
| 440 |
maxChild = descendantNode; |
| 441 |
|
| 442 |
} |
| 443 |
|
| 444 |
} |
| 445 |
|
| 446 |
if(maxChild != previous) { |
| 447 |
|
| 448 |
current.flaggedNode = true; |
| 449 |
System.out.println("Problem found at: [" + current.taxID + "] Taxon rank: [" + current.taxonomicRank + "]"); |
| 450 |
highestProblemNode = current; |
| 451 |
problemChild = maxChild; |
| 452 |
|
| 453 |
} |
| 454 |
|
| 455 |
previous = current; |
| 456 |
current = current.parentNode; |
| 457 |
|
| 458 |
} |
| 459 |
|
| 460 |
if(highestProblemNode != null && highestProblemNode != keyNode.parentNode) { |
| 461 |
problemChild.color = "yellow"; |
| 462 |
} |
| 463 |
|
| 464 |
return highestProblemNode == keyNode.parentNode ? null : highestProblemNode; |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
public boolean isSuspicious(NCBITreeNode keyNode, NCBITreeNode matrixOrgNode, NCBIComparison rowOrgComparison) { |
| 477 |
|
| 478 |
if(!matrixOrgNode.isDescendantOf(keyNode.parentNode) && |
| 479 |
!matrixOrgNode.isAncestorOf(keyNode)) { |
| 480 |
|
| 481 |
|
| 482 |
double matrixOrgSim = rowOrgComparison.identity; |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
if(matrixOrgSim > keyNode.parentSimilarity()) { |
| 487 |
|
| 488 |
return true; |
| 489 |
} |
| 490 |
|
| 491 |
} |
| 492 |
return false; |
| 493 |
} |
| 494 |
|
| 495 |
public void setNodeColors(NCBITreeNode keyNode, NCBITreeNode voteProblemNode, NCBITreeNode avgIDProblemNode) { |
| 496 |
keyNode.color = "blue"; |
| 497 |
voteProblemNode.color = "red"; |
| 498 |
avgIDProblemNode.color = "green"; |
| 499 |
} |
| 500 |
|
| 501 |
public void printSuspicious(NCBITreeNode keyNode, NCBIComparison rowOrgComparison, NCBITreeNode matrixOrgNode) { |
| 502 |
System.out.println(); |
| 503 |
System.out.println("problem"); |
| 504 |
System.out.println("key org taxon ID " + keyNode.taxID); |
| 505 |
System.out.println("matrix org " + matrixOrgNode.taxID); |
| 506 |
System.out.println("par org taxon ID " + keyNode.getParentTaxonID()); |
| 507 |
System.out.println("other org " + rowOrgComparison); |
| 508 |
System.out.println("par sim " + keyNode.parentSimilarity()); |
| 509 |
|
| 510 |
System.out.println("matrix sim " + rowOrgComparison.identity); |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
void createFile(String fileName) { |
| 519 |
|
| 520 |
try { |
| 521 |
|
| 522 |
|
| 523 |
File myObj = new File(fileName); |
| 524 |
|
| 525 |
|
| 526 |
if (myObj.createNewFile()) { |
| 527 |
System.out.println("File created: " + myObj.getName()); |
| 528 |
|
| 529 |
|
| 530 |
} else { |
| 531 |
System.out.println("File already exists."); |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
} catch (IOException e) { |
| 536 |
System.out.println("An error occurred."); |
| 537 |
e.printStackTrace(); |
| 538 |
} |
| 539 |
} |
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
void writeToFile(String fileName, StringBuilder sb) { |
| 547 |
|
| 548 |
try { |
| 549 |
|
| 550 |
|
| 551 |
FileWriter myWriter = new FileWriter(fileName); |
| 552 |
|
| 553 |
|
| 554 |
myWriter.write(sb.toString()); |
| 555 |
|
| 556 |
|
| 557 |
myWriter.close(); |
| 558 |
|
| 559 |
|
| 560 |
System.out.println("Successfully wrote to the file."); |
| 561 |
|
| 562 |
|
| 563 |
} catch (IOException e) { |
| 564 |
System.out.println("An error occurred."); |
| 565 |
e.printStackTrace(); |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
public void writeTrees(NCBITreeNode keyNode, NCBISparseTree tree) { |
| 570 |
|
| 571 |
String currentTreeName = outpath + "SimilarityTree_" + keyNode.orgName + "_.dot"; |
| 572 |
|
| 573 |
|
| 574 |
createFile(currentTreeName); |
| 575 |
|
| 576 |
|
| 577 |
writeToFile(currentTreeName, tree.root.toDot(printAllNodes)); |
| 578 |
} |
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
private String sim=null; |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
private String treeFileName=null; |
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
private String out=null; |
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
private String outpath=null; |
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
private boolean writeTrees = false; |
| 609 |
|
| 610 |
private boolean test=false; |
| 611 |
|
| 612 |
private boolean ncbi=true; |
| 613 |
|
| 614 |
private int mode=BOTH_MODE; |
| 615 |
|
| 616 |
private NCBISparseTree tree = null; |
| 617 |
|
| 618 |
private boolean printAllNodes = false; |
| 619 |
|
| 620 |
private int goodTreesToPrint=0; |
| 621 |
|
| 622 |
private int badTreesToPrint=0; |
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
private long linesProcessed=0; |
| 627 |
private long linesOut=0; |
| 628 |
private long bytesProcessed=0; |
| 629 |
private long bytesOut=0; |
| 630 |
private long taxa=0; |
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
public static int MAX_VOTES=20; |
| 635 |
public static final int VOTE_MODE=0; |
| 636 |
public static final int IDENTITY_MODE=1; |
| 637 |
public static final int AVERAGE_IDENTITY_MODE=2; |
| 638 |
public static final int BOTH_MODE=3; |
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
private String[] header; |
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
public static boolean verbose=false; |
| 655 |
public boolean errorState=false; |
| 656 |
private boolean overwrite=true; |
| 657 |
private boolean append=false; |
| 658 |
|
| 659 |
private java.io.PrintStream outstream=System.err; |
| 660 |
|
| 661 |
|
| 662 |
} |