| 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 | |
| 9 | import shared.Parser; |
| 10 | import shared.PreParser; |
| 11 | import shared.Timer; |
| 12 | |
| 13 | |
| 14 | |
| 15 | public class SparseTreeValidate { |
| 16 | |
| 17 | /*--------------------------------------------------------------*/ |
| 18 | /*---------------- Initialization ----------------*/ |
| 19 | /*--------------------------------------------------------------*/ |
| 20 | |
| 21 | public static void main(String[] args) throws FileNotFoundException, IOException { |
| 22 | |
| 23 | //Start a timer immediately upon code entrance. |
| 24 | Timer t=new Timer(); |
| 25 | |
| 26 | //Create an instance of this class |
| 27 | SparseTreeValidate x=new SparseTreeValidate(args); |
| 28 | |
| 29 | //Run the object |
| 30 | x.process(t); |
| 31 | |
| 32 | |
| 33 | |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Handles pre-parsing and parsing of user flags. |
| 38 | * Reads in the sketch similarity file (sim) and organism-parent relationship file (tree). |
| 39 | * |
| 40 | * @param args string of the arguments input at the commandline. |
| 41 | */ |
| 42 | public SparseTreeValidate(String[] args) { |
| 43 | |
| 44 | {//Preparse block for help, config files, and outstream |
| 45 | PreParser pp=new PreParser(args, getClass(), false); |
| 46 | args=pp.args; |
| 47 | outstream=pp.outstream; |
| 48 | } |
| 49 | |
| 50 | //Primary parsing of standard arguments found in all bbmap programs (maxReads, parseSam, parseZip, etc). |
| 51 | Parser parser=new Parser(); |
| 52 | |
| 53 | //Loop through arguments up to the maximum number of arguments input. |
| 54 | //process all remaining arguments. |
| 55 | for(int i=0; i<args.length; i++){ |
| 56 | |
| 57 | //Grab argument string at index. |
| 58 | String arg=args[i]; |
| 59 | |
| 60 | //Split argument string on "=". |
| 61 | String[] split=arg.split("="); |
| 62 | |
| 63 | //Convert the left side to lowercase. |
| 64 | String a=split[0].toLowerCase(); |
| 65 | |
| 66 | //Ternary conditional statement: is the length of the split greater than 1 (thus, an actual input)? |
| 67 | //if so, the right side of the split is the b variable, if not, b is null. |
| 68 | String b=split.length>1 ? split[1] : null; |
| 69 | |
| 70 | //If b isn't null but a string "null" was input, convert b to null. |
| 71 | if(b!=null && b.equalsIgnoreCase("null")){b=null;} |
| 72 | |
| 73 | //Unused example statement. does nothing currently. start here for adding new flag parsing. |
| 74 | if(a.equals("parse_flag_goes_here")){ |
| 75 | |
| 76 | //Handle similarities file variable assignment. |
| 77 | }else if(a.equals("sim")){ |
| 78 | sim=b; |
| 79 | |
| 80 | //Handle tree file variable assignment. |
| 81 | }else if(a.equals("tree")){ |
| 82 | tree=b; |
| 83 | |
| 84 | //Tells program to write tree graphs in .dot format. |
| 85 | }else if(a.equals("writetrees")) { |
| 86 | writeTrees = true; |
| 87 | |
| 88 | //If writetrees is true and the path to an output dir is included |
| 89 | //set variable to the path to the desired directory |
| 90 | }else if(a.equals("outpath") && writeTrees == true) { |
| 91 | outpath=b; |
| 92 | if(outpath.endsWith("/")) {System.out.println("output path correct");} |
| 93 | else {outpath = outpath + "/";} |
| 94 | |
| 95 | //Parses in and out flags, handles all flags not recognized earlier in class. |
| 96 | }else if(parser.parse(arg, a, b)){ |
| 97 | |
| 98 | //If not one of the known parameters, let the user know they made a mistake. |
| 99 | }else{ |
| 100 | assert(false) : "Unknown parameter "+args[i]; |
| 101 | outstream.println("Unknown parameter "+args[i]); |
| 102 | } |
| 103 | |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | |
| 108 | /*--------------------------------------------------------------*/ |
| 109 | /*---------------- Outer Methods ----------------*/ |
| 110 | /*--------------------------------------------------------------*/ |
| 111 | |
| 112 | |
| 113 | /** |
| 114 | * Creates the similarity matrix and the relationship tree. |
| 115 | * Also passes these objects to the inner processing method for analysis. |
| 116 | * |
| 117 | * @param t |
| 118 | * @throws FileNotFoundException |
| 119 | * @throws IOException |
| 120 | */ |
| 121 | void process(Timer t) throws FileNotFoundException, IOException{ |
| 122 | |
| 123 | //Pass input file to Tree class to create tree |
| 124 | SparseTree relationshipTree=new SparseTree(tree); |
| 125 | |
| 126 | //Pass similarity file to create similarity matrix object |
| 127 | SparseSimilarityMatrix matrix=new SparseSimilarityMatrix(sim, relationshipTree); |
| 128 | |
| 129 | //Add parent node similarity percentages to each node in the tree. |
| 130 | //addRelationSims(relationshipTree, matrix); |
| 131 | |
| 132 | System.out.println(relationshipTree.getNode(5)); |
| 133 | |
| 134 | //Traverse the tree and add levels to all nodes. |
| 135 | //Hardcoded to start at node "0" or "life" node. |
| 136 | relationshipTree.beginTraverse("0"); |
| 137 | |
| 138 | //Sets the identities by beginning at a particular node and working backwards. |
| 139 | //relationshipTree.setIdentity(relationshipTree.getNode(10), matrix); |
| 140 | |
| 141 | //TODO: remove 10 since this is just part of testing/percolate |
| 142 | //relationshipTree.root.percolateIdentityUp(10); |
| 143 | |
| 144 | //Check similarities. |
| 145 | checkSimilarities(relationshipTree, matrix); |
| 146 | |
| 147 | //System.out.println(relationshipTree); |
| 148 | |
| 149 | |
| 150 | t.stop(); |
| 151 | |
| 152 | outstream.println("Time: \t"+t); |
| 153 | |
| 154 | } |
| 155 | |
| 156 | /*--------------------------------------------------------------*/ |
| 157 | /*---------------- Inner Methods ----------------*/ |
| 158 | /*--------------------------------------------------------------*/ |
| 159 | // /** |
| 160 | // * Iterate over nodes in the Tree and compare present relationships |
| 161 | // * with values found in the similarity matrix. |
| 162 | // * |
| 163 | // * @param tree Tree object containing TreeNode objects detailing the parent and children of each node. |
| 164 | // * @param matrix SimilarityMatrix2 object containing percentage similarity of sketches. |
| 165 | // */ |
| 166 | // void addRelationSims(SparseTree tree, SparseSimilarityMatrix matrix){ |
| 167 | // |
| 168 | // //Iterate over organisms/nodes in the tree. |
| 169 | // for ( String keyOrg : tree.keySet() ) { |
| 170 | // |
| 171 | // //add the node of current focus to a variable in the loop. |
| 172 | // TreeNode keyNode = tree.getNode(keyOrg); |
| 173 | // |
| 174 | // //Get Parent node of keyNode. |
| 175 | // TreeNode parentNode = keyNode.parentNode; |
| 176 | // |
| 177 | // //If statement to ignore the root/"life" node when obtaining similarities. |
| 178 | // //Then get the parent nodes similarity to the key node. |
| 179 | // //add the similarity value to the key node. |
| 180 | // if(!parentNode.orgName.equals("0")) { |
| 181 | // |
| 182 | // keyNode.addParSim(parentNode.averageIdentity()); |
| 183 | // System.out.println(parentNode.averageIdentity()); |
| 184 | // } |
| 185 | // } |
| 186 | // } |
| 187 | |
| 188 | |
| 189 | /** |
| 190 | * Method to check tree for surprising similarity values for each node in the tree. |
| 191 | * Ignores the root/"life" node, any node without any sequence. |
| 192 | * Any node with a higher similarity to another node than to its parent that |
| 193 | * isn't an descendant is flagged as possibly erroneous. |
| 194 | * |
| 195 | * @param tree The tree object storing taxon nodes (TreeNode objects). |
| 196 | * @param matrix The sparse similarity matrix containing all possible pairwise similarity values. |
| 197 | */ |
| 198 | void checkSimilarities(SparseTree tree, SparseSimilarityMatrix matrix) { |
| 199 | |
| 200 | //Iterate over organisms/nodes in the tree. |
| 201 | for ( String keyOrg : tree.keySet() ) { |
| 202 | |
| 203 | //If the organism isn't the life/0 node. |
| 204 | if(!keyOrg.equals("0")) { |
| 205 | |
| 206 | //Get the node from the tree |
| 207 | TreeNode keyNode = tree.getNode(keyOrg); |
| 208 | |
| 209 | //Reset the identity values of the TreeNodes |
| 210 | //This is done so all identity values are relative to the current keyNode. |
| 211 | tree.root.resetIdentity(); |
| 212 | |
| 213 | //Set the identities of the tree based on the current keyNode. |
| 214 | tree.setIdentity(keyNode, matrix); |
| 215 | |
| 216 | //Percolate the average identities of each node upwards through the tree |
| 217 | //relative to the current keyNode. |
| 218 | tree.root.percolateIdentityUp(keyNode.nodeId); |
| 219 | |
| 220 | //System.out.println(tree.root.toDot()); |
| 221 | |
| 222 | //Handle writing tree relationships in .dot format. |
| 223 | //If selected by user. |
| 224 | if(writeTrees == true) { |
| 225 | |
| 226 | //Creates the tree output file name along with the path to the desired directory. |
| 227 | String currentTreeName = outpath + "SimilarityTree_" + keyNode.orgName + "_.dot"; |
| 228 | |
| 229 | //Create the output file. |
| 230 | createFile(currentTreeName); |
| 231 | |
| 232 | //Write the .dot formatted tree to the file. |
| 233 | writeToFile(currentTreeName, tree.root.toDot()); |
| 234 | |
| 235 | //assert false; |
| 236 | } |
| 237 | //Prevents analysis of "empty" nodes that don't contain sequences (genus/phylum/etc). |
| 238 | //TODO: if there are no sibling nodes, the parent sim could be 0. |
| 239 | if(keyNode.parentNode.averageIdentity() != 0.0) { |
| 240 | |
| 241 | //Identify parent node. |
| 242 | String parentName = keyNode.getParentName(); |
| 243 | |
| 244 | //Get parent node ID |
| 245 | //int parentID = tree.getNode(parentName).getNodeId(); |
| 246 | |
| 247 | //Get the row of similarity values associated with |
| 248 | //the key node and each other node. |
| 249 | ArrayList<Comparison> keyOrgRow = matrix.getOrgRow(keyOrg); |
| 250 | |
| 251 | //Iterate over the node organism names. |
| 252 | for(Comparison rowOrgComparison : keyOrgRow) { |
| 253 | |
| 254 | //Get the node being iterated over in the tree. |
| 255 | TreeNode matrixOrgNode = tree.getNode(rowOrgComparison.refID); |
| 256 | |
| 257 | //if we aren't comparing similarities of the node to itself and |
| 258 | //if we aren't examining a child node and |
| 259 | //if we aren't examining a parent node |
| 260 | if(!matrixOrgNode.isDescendantOf(keyNode.parentNode) && |
| 261 | !matrixOrgNode.isAncestorOf(keyNode)) { |
| 262 | |
| 263 | //Get similarity between the key node and any pairwise compared node |
| 264 | double matrixOrgSim = rowOrgComparison.identity; |
| 265 | |
| 266 | //If the similarity value is higher than the similarity |
| 267 | //between the key node and its parent. |
| 268 | if(matrixOrgSim > keyNode.parentSimilarity()) { |
| 269 | |
| 270 | //Currently prints out a bunch of node/similarity info |
| 271 | //TODO: fix flagged node handling |
| 272 | System.out.println(); |
| 273 | System.out.println("problem"); |
| 274 | System.out.println("key org " + keyOrg); |
| 275 | |
| 276 | System.out.println("par name " + parentName); |
| 277 | System.out.println("other org " + rowOrgComparison); |
| 278 | System.out.println("par sim " + keyNode.parentSimilarity()); |
| 279 | |
| 280 | System.out.println("matrix sim " + matrixOrgSim); |
| 281 | |
| 282 | //keyNode.flagRelation(rowOrgComparison, matrixOrgSim); |
| 283 | System.out.println(keyNode.getFlaggedRelations()); |
| 284 | } |
| 285 | |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Method for creating a file with an input file name. |
| 295 | * @param fileName String for the file name. |
| 296 | */ |
| 297 | void createFile(String fileName) { |
| 298 | |
| 299 | try { |
| 300 | |
| 301 | //Creates a file object using the input file name. |
| 302 | File myObj = new File(fileName); |
| 303 | |
| 304 | //If the file is successfully created, print a message saying so. |
| 305 | if (myObj.createNewFile()) { |
| 306 | System.out.println("File created: " + myObj.getName()); |
| 307 | |
| 308 | //If the file cannot be created, the file already exists |
| 309 | } else { |
| 310 | System.out.println("File already exists."); |
| 311 | } |
| 312 | |
| 313 | //Catch a message if their are exceptions. |
| 314 | } catch (IOException e) { |
| 315 | System.out.println("An error occurred."); |
| 316 | e.printStackTrace(); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Method to write information to a file. |
| 322 | * @param fileName Name of the file created using the createFile method. |
| 323 | * @param sb StringBuilder containing information to be written to the file. |
| 324 | */ |
| 325 | void writeToFile(String fileName, StringBuilder sb) { |
| 326 | |
| 327 | try { |
| 328 | |
| 329 | //Create a FileWriter object using the name of the file. |
| 330 | FileWriter myWriter = new FileWriter(fileName); |
| 331 | |
| 332 | //Write the StringBuilder contents to the file |
| 333 | myWriter.write(sb.toString()); |
| 334 | |
| 335 | //Close the FileWriter object. |
| 336 | myWriter.close(); |
| 337 | |
| 338 | //Message on success. |
| 339 | System.out.println("Successfully wrote to the file."); |
| 340 | |
| 341 | //Catch exceptions and write a message to alert the user. |
| 342 | } catch (IOException e) { |
| 343 | System.out.println("An error occurred."); |
| 344 | e.printStackTrace(); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | |
| 349 | /*--------------------------------------------------------------*/ |
| 350 | /*---------------- Fields ----------------*/ |
| 351 | /*--------------------------------------------------------------*/ |
| 352 | |
| 353 | /** |
| 354 | * Pairwise similarity file generated by BBSketch. |
| 355 | */ |
| 356 | private String sim=null; |
| 357 | |
| 358 | /** |
| 359 | * Tree file containing taxonomic relationships. |
| 360 | */ |
| 361 | private String tree=null; |
| 362 | |
| 363 | /** |
| 364 | * Output file name. |
| 365 | */ |
| 366 | private String out=null; |
| 367 | |
| 368 | /** |
| 369 | * Output path for any output files. |
| 370 | */ |
| 371 | private String outpath=null; |
| 372 | |
| 373 | /** |
| 374 | * Flag for indicating whether to write tree .dot files. |
| 375 | * If present in commandline args, set to true. |
| 376 | */ |
| 377 | private boolean writeTrees = false; |
| 378 | |
| 379 | /*--------------------------------------------------------------*/ |
| 380 | |
| 381 | private long linesProcessed=0; |
| 382 | private long linesOut=0; |
| 383 | private long bytesProcessed=0; |
| 384 | private long bytesOut=0; |
| 385 | private long taxa=0; |
| 386 | |
| 387 | /*--------------------------------------------------------------*/ |
| 388 | /*---------------- Final Fields ----------------*/ |
| 389 | /*--------------------------------------------------------------*/ |
| 390 | |
| 391 | private String[] header; |
| 392 | //private final FileFormat ffin; |
| 393 | //private final FileFormat ffout; |
| 394 | |
| 395 | /*--------------------------------------------------------------*/ |
| 396 | /*---------------- Common Fields ----------------*/ |
| 397 | /*--------------------------------------------------------------*/ |
| 398 | |
| 399 | //private PrintStream outstream=System.err; |
| 400 | public static boolean verbose=false; |
| 401 | public boolean errorState=false; |
| 402 | private boolean overwrite=true; |
| 403 | private boolean append=false; |
| 404 | /**Output stream that output statistics are piped through to the output file. */ |
| 405 | private java.io.PrintStream outstream=System.err; |
| 406 | |
| 407 | |
| 408 | } |