| 1 | package jasper; |
| 2 | |
| 3 | import java.io.FileNotFoundException; |
| 4 | import java.io.IOException; |
| 5 | import java.util.HashSet; |
| 6 | |
| 7 | import shared.Parser; |
| 8 | import shared.PreParser; |
| 9 | import shared.Timer; |
| 10 | |
| 11 | |
| 12 | |
| 13 | public class DenseTreeValidate { |
| 14 | |
| 15 | /*--------------------------------------------------------------*/ |
| 16 | /*---------------- Initialization ----------------*/ |
| 17 | /*--------------------------------------------------------------*/ |
| 18 | |
| 19 | public static void main(String[] args) throws FileNotFoundException, IOException { |
| 20 | |
| 21 | //Start a timer immediately upon code entrance. |
| 22 | Timer t=new Timer(); |
| 23 | |
| 24 | //Create an instance of this class |
| 25 | DenseTreeValidate x=new DenseTreeValidate(args); |
| 26 | |
| 27 | //Run the object |
| 28 | x.process(t); |
| 29 | |
| 30 | |
| 31 | |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Handles pre-parsing and parsing of user flags. |
| 36 | * Reads in the sketch similarity file (sim) and organism-parent relationship file (tree). |
| 37 | * |
| 38 | * @param args string of the arguments input at the commandline. |
| 39 | */ |
| 40 | public DenseTreeValidate(String[] args) { |
| 41 | |
| 42 | {//Preparse block for help, config files, and outstream |
| 43 | PreParser pp=new PreParser(args, getClass(), false); |
| 44 | args=pp.args; |
| 45 | outstream=pp.outstream; |
| 46 | } |
| 47 | |
| 48 | //Primary parsing of standard arguments found in all bbmap programs (maxReads, parseSam, parseZip, etc). |
| 49 | Parser parser=new Parser(); |
| 50 | |
| 51 | //Loop through arguments up to the maximum number of arguments input. |
| 52 | //process all remaining arguments. |
| 53 | for(int i=0; i<args.length; i++){ |
| 54 | |
| 55 | //Grab argument string at index. |
| 56 | String arg=args[i]; |
| 57 | |
| 58 | //Split argument string on "=". |
| 59 | String[] split=arg.split("="); |
| 60 | |
| 61 | //Convert the left side to lowercase. |
| 62 | String a=split[0].toLowerCase(); |
| 63 | |
| 64 | //Ternary conditional statement: is the length of the split greater than 1 (thus, an actual input)? |
| 65 | //if so, the right side of the split is the b variable, if not, b is null. |
| 66 | String b=split.length>1 ? split[1] : null; |
| 67 | |
| 68 | //If b isn't null but a string "null" was input, convert b to null. |
| 69 | if(b!=null && b.equalsIgnoreCase("null")){b=null;} |
| 70 | |
| 71 | //Unused example statement. does nothing currently. start here for adding new flag parsing. |
| 72 | if(a.equals("parse_flag_goes_here")){ |
| 73 | |
| 74 | //Handle reference variable assignment. |
| 75 | }else if(a.equals("sim")){ |
| 76 | sim=b; |
| 77 | |
| 78 | //Handle kmer variable assignment. |
| 79 | }else if(a.equals("tree")){ |
| 80 | tree=b; |
| 81 | |
| 82 | //Parses in and out flags, handles all flags not recognized earlier in class. |
| 83 | }else if(parser.parse(arg, a, b)){ |
| 84 | |
| 85 | //If not one of the known parameters, let the user know they made a mistake. |
| 86 | }else{ |
| 87 | assert(false) : "Unknown parameter "+args[i]; |
| 88 | outstream.println("Unknown parameter "+args[i]); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | } |
| 93 | |
| 94 | |
| 95 | /*--------------------------------------------------------------*/ |
| 96 | /*---------------- Outer Methods ----------------*/ |
| 97 | /*--------------------------------------------------------------*/ |
| 98 | |
| 99 | |
| 100 | /** |
| 101 | * Creates the similarity matrix and the relationship tree. |
| 102 | * Also passes these objects to the inner processing method for analysis. |
| 103 | * |
| 104 | * @param t |
| 105 | * @throws FileNotFoundException |
| 106 | * @throws IOException |
| 107 | */ |
| 108 | void process(Timer t) throws FileNotFoundException, IOException{ |
| 109 | |
| 110 | //Pass input file to Tree class to create tree |
| 111 | DenseTree relationshipTree=new DenseTree(tree); |
| 112 | |
| 113 | //Pass similarity file to create similarity matrix object |
| 114 | DenseSimilarityMatrix matrix=new DenseSimilarityMatrix(sim, relationshipTree); |
| 115 | |
| 116 | //Add parent node similarity percentages to each node in the tree. |
| 117 | //addRelationSims(relationshipTree, matrix); |
| 118 | |
| 119 | //Traverse the tree and add levels to all nodes. |
| 120 | //Hardcoded to start at node "0" or "life" node. |
| 121 | relationshipTree.beginTraverse("0"); |
| 122 | |
| 123 | //relationshipTree.setIdentity(relationshipTree.getNode(10), matrix); |
| 124 | |
| 125 | //TODO: remove 10 since this is just part of testing/percolate |
| 126 | //relationshipTree.root.percolateIdentityUp(10); |
| 127 | |
| 128 | //Check similarities. |
| 129 | checkSimilarities(relationshipTree, matrix); |
| 130 | |
| 131 | //System.out.println(relationshipTree); |
| 132 | |
| 133 | |
| 134 | t.stop(); |
| 135 | outstream.println("Time: \t"+t); |
| 136 | |
| 137 | } |
| 138 | |
| 139 | /*--------------------------------------------------------------*/ |
| 140 | /*---------------- Inner Methods ----------------*/ |
| 141 | /*--------------------------------------------------------------*/ |
| 142 | // /** |
| 143 | // * Iterate over nodes in the Tree and compare present relationships |
| 144 | // * with values found in the similarity matrix. |
| 145 | // * |
| 146 | // * @param tree Tree object containing TreeNode objects detailing the parent and children of each node. |
| 147 | // * @param matrix SimilarityMatrix2 object containing percentage similarity of sketches. |
| 148 | // */ |
| 149 | // void addRelationSims(DenseTree tree, DenseSimilarityMatrix matrix){ |
| 150 | // |
| 151 | // //Iterate over organisms/nodes in the tree. |
| 152 | // for ( String keyOrg : tree.keySet() ) { |
| 153 | // |
| 154 | // TreeNode keyNode = tree.getNode(keyOrg); |
| 155 | // |
| 156 | // //Identify parent node. |
| 157 | // String parentName = keyNode.getParentName(); |
| 158 | // |
| 159 | // //Get descendant nodes. |
| 160 | // HashSet<String> childNames = keyNode.getChildren(); |
| 161 | // |
| 162 | // if(!parentName.equals("0")) { |
| 163 | // |
| 164 | // double parSim = matrix.getSimilarity(keyOrg, parentName); |
| 165 | // |
| 166 | // keyNode.addParSim(parSim); |
| 167 | // } |
| 168 | // |
| 169 | // for(String kid : childNames) { |
| 170 | // |
| 171 | // if(!tree.getNode(kid).parentName.equals("0")) { |
| 172 | // |
| 173 | // double kidSim = matrix.getSimilarity(keyOrg, kid); |
| 174 | // |
| 175 | // keyNode.addChildSim(kid, kidSim); |
| 176 | // } |
| 177 | // } |
| 178 | // } |
| 179 | // } |
| 180 | |
| 181 | void checkSimilarities(DenseTree tree, DenseSimilarityMatrix matrix) { |
| 182 | |
| 183 | //Iterate over organisms/nodes in the tree. |
| 184 | for ( String keyOrgName : tree.keySet() ) { |
| 185 | |
| 186 | //If the organism isn't the life/0 node. |
| 187 | if(!keyOrgName.equals("0")) { |
| 188 | //System.out.println("key org " + keyOrg); |
| 189 | |
| 190 | //Get the node from the tree |
| 191 | TreeNode keyNode = tree.getNode(keyOrgName); |
| 192 | |
| 193 | int keyNodeID = keyNode.getNodeId(); |
| 194 | |
| 195 | tree.root.resetIdentity(); |
| 196 | |
| 197 | tree.setIdentity(keyNode, matrix); |
| 198 | |
| 199 | tree.root.percolateIdentityUp(keyNode.nodeId); |
| 200 | |
| 201 | //Prevents analysis of "empty" nodes that don't contain sequences (genus/phylum/etc). |
| 202 | //TODO: if there are no sibling nodes, the parent sim could be 0. |
| 203 | if(keyNode.parentNode.averageIdentity() != 0.0) { |
| 204 | |
| 205 | //Identify parent node. |
| 206 | String parentName = keyNode.getParentName(); |
| 207 | |
| 208 | double parentSimilarity = matrix.getSimilarity(keyOrgName, parentName); |
| 209 | |
| 210 | //Get the child node names. |
| 211 | HashSet<String> childNameSet = keyNode.getChildren(); |
| 212 | |
| 213 | //Iterate over the organisms in the matrix. |
| 214 | for(String matrixOrg : tree.keySet()) { |
| 215 | |
| 216 | TreeNode matrixOrgNode = tree.getNode(matrixOrg); |
| 217 | |
| 218 | //if we aren't comparing similarities of the node to itself and |
| 219 | //if we aren't examining a child node and |
| 220 | //if we aren't examining a parent node |
| 221 | if(!matrixOrgNode.isDescendantOf(keyNode) && !matrixOrgNode.isAncestorOf(keyNode) && !matrixOrg.equals(parentName) ) { |
| 222 | |
| 223 | double matrixOrgSim = matrix.getSimilarity(keyOrgName, matrixOrg); |
| 224 | |
| 225 | |
| 226 | if(matrixOrgSim > parentSimilarity) { |
| 227 | |
| 228 | |
| 229 | System.out.println(); |
| 230 | System.out.println("problem"); |
| 231 | System.out.println("key org " + keyOrgName); |
| 232 | //System.out.println("kid name " + minChildName); |
| 233 | System.out.println("par name " + parentName); |
| 234 | System.out.println("other org " + matrixOrg); |
| 235 | System.out.println("par sim " + parentSimilarity); |
| 236 | //System.out.println("child sim " + minChildSim); |
| 237 | System.out.println("matrix sim " + matrixOrgSim); |
| 238 | |
| 239 | keyNode.flagRelation(matrixOrg, matrixOrgSim); |
| 240 | System.out.println(keyNode.getFlaggedRelations()); |
| 241 | } |
| 242 | |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | /*--------------------------------------------------------------*/ |
| 254 | /*---------------- Fields ----------------*/ |
| 255 | /*--------------------------------------------------------------*/ |
| 256 | |
| 257 | private String sim=null; |
| 258 | private String tree=null; |
| 259 | private String out=null; |
| 260 | |
| 261 | /*--------------------------------------------------------------*/ |
| 262 | |
| 263 | private long linesProcessed=0; |
| 264 | private long linesOut=0; |
| 265 | private long bytesProcessed=0; |
| 266 | private long bytesOut=0; |
| 267 | private long taxa=0; |
| 268 | |
| 269 | /*--------------------------------------------------------------*/ |
| 270 | /*---------------- Final Fields ----------------*/ |
| 271 | /*--------------------------------------------------------------*/ |
| 272 | |
| 273 | private String[] header; |
| 274 | //private final FileFormat ffin; |
| 275 | //private final FileFormat ffout; |
| 276 | |
| 277 | /*--------------------------------------------------------------*/ |
| 278 | /*---------------- Common Fields ----------------*/ |
| 279 | /*--------------------------------------------------------------*/ |
| 280 | |
| 281 | //private PrintStream outstream=System.err; |
| 282 | public static boolean verbose=false; |
| 283 | public boolean errorState=false; |
| 284 | private boolean overwrite=true; |
| 285 | private boolean append=false; |
| 286 | /**Output stream that output statistics are piped through to the output file. */ |
| 287 | private java.io.PrintStream outstream=System.err; |
| 288 | |
| 289 | |
| 290 | } |