| 1 |
package tax; |
| 2 |
|
| 3 |
import java.io.PrintStream; |
| 4 |
|
| 5 |
import fileIO.ByteFile; |
| 6 |
import fileIO.ByteFile1; |
| 7 |
import fileIO.ByteFile2; |
| 8 |
import fileIO.ByteStreamWriter; |
| 9 |
import fileIO.FileFormat; |
| 10 |
import fileIO.ReadWrite; |
| 11 |
import shared.Parse; |
| 12 |
import shared.Parser; |
| 13 |
import shared.PreParser; |
| 14 |
import shared.Shared; |
| 15 |
import shared.Timer; |
| 16 |
import shared.Tools; |
| 17 |
import stream.ConcurrentGenericReadInputStream; |
| 18 |
import stream.FastaReadInputStream; |
| 19 |
import structures.ByteBuilder; |
| 20 |
import structures.IntList; |
| 21 |
import tracker.ReadStats; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public class FindAncestor { |
| 29 |
|
| 30 |
public static void main(String[] args){ |
| 31 |
Timer t=new Timer(); |
| 32 |
FindAncestor x=new FindAncestor(args); |
| 33 |
x.process(t); |
| 34 |
|
| 35 |
|
| 36 |
Shared.closeStream(x.outstream); |
| 37 |
} |
| 38 |
|
| 39 |
public FindAncestor(String[] args){ |
| 40 |
|
| 41 |
{ |
| 42 |
PreParser pp=new PreParser(args, getClass(), false); |
| 43 |
args=pp.args; |
| 44 |
outstream=pp.outstream; |
| 45 |
} |
| 46 |
|
| 47 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 48 |
ReadWrite.setZipThreads(Shared.threads()); |
| 49 |
|
| 50 |
Parser parser=new Parser(); |
| 51 |
for(int i=0; i<args.length; i++){ |
| 52 |
String arg=args[i]; |
| 53 |
String[] split=arg.split("="); |
| 54 |
String a=split[0].toLowerCase(); |
| 55 |
String b=split.length>1 ? split[1] : null; |
| 56 |
|
| 57 |
if(parser.parse(arg, a, b)){ |
| 58 |
|
| 59 |
}else if(a.equals("table") || a.equals("gi") || a.equals("gitable")){ |
| 60 |
giTableFile=b; |
| 61 |
}else if(a.equals("tree") || a.equals("taxtree")){ |
| 62 |
taxTreeFile=b; |
| 63 |
}else if(a.equals("invalid")){ |
| 64 |
outInvalid=b; |
| 65 |
}else if(a.equals("lines")){ |
| 66 |
maxLines=Long.parseLong(b); |
| 67 |
if(maxLines<0){maxLines=Long.MAX_VALUE;} |
| 68 |
}else if(a.equals("verbose")){ |
| 69 |
verbose=Parse.parseBoolean(b); |
| 70 |
ByteFile1.verbose=verbose; |
| 71 |
ByteFile2.verbose=verbose; |
| 72 |
stream.FastaReadInputStream.verbose=verbose; |
| 73 |
ConcurrentGenericReadInputStream.verbose=verbose; |
| 74 |
stream.FastqReadInputStream.verbose=verbose; |
| 75 |
ReadWrite.verbose=verbose; |
| 76 |
}else{ |
| 77 |
outstream.println("Unknown parameter "+args[i]); |
| 78 |
assert(false) : "Unknown parameter "+args[i]; |
| 79 |
|
| 80 |
} |
| 81 |
} |
| 82 |
if("auto".equalsIgnoreCase(taxTreeFile)){taxTreeFile=TaxTree.defaultTreeFile();} |
| 83 |
if("auto".equalsIgnoreCase(giTableFile)){giTableFile=TaxTree.defaultTableFile();} |
| 84 |
|
| 85 |
{ |
| 86 |
overwrite=ReadStats.overwrite=parser.overwrite; |
| 87 |
append=ReadStats.append=parser.append; |
| 88 |
|
| 89 |
in1=parser.in1; |
| 90 |
|
| 91 |
out1=parser.out1; |
| 92 |
} |
| 93 |
|
| 94 |
assert(FastaReadInputStream.settingsOK()); |
| 95 |
|
| 96 |
if(in1==null){throw new RuntimeException("Error - at least one input file is required.");} |
| 97 |
|
| 98 |
if(!ByteFile.FORCE_MODE_BF2){ |
| 99 |
ByteFile.FORCE_MODE_BF2=false; |
| 100 |
ByteFile.FORCE_MODE_BF1=true; |
| 101 |
} |
| 102 |
|
| 103 |
if(out1!=null && out1.equalsIgnoreCase("null")){out1=null;} |
| 104 |
|
| 105 |
if(!Tools.testOutputFiles(overwrite, append, false, out1)){ |
| 106 |
outstream.println((out1==null)+", "+out1); |
| 107 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out1+"\n"); |
| 108 |
} |
| 109 |
|
| 110 |
ffout1=FileFormat.testOutput(out1, FileFormat.TXT, null, true, overwrite, append, false); |
| 111 |
ffoutInvalid=FileFormat.testOutput(outInvalid, FileFormat.TXT, null, true, overwrite, append, false); |
| 112 |
ffin1=FileFormat.testInput(in1, FileFormat.TXT, null, true, true); |
| 113 |
|
| 114 |
if(giTableFile!=null){ |
| 115 |
outstream.println("Loading gi table."); |
| 116 |
GiToTaxid.initialize(giTableFile); |
| 117 |
} |
| 118 |
if(taxTreeFile!=null){ |
| 119 |
tree=TaxTree.loadTaxTree(taxTreeFile, outstream, true, true); |
| 120 |
assert(tree.nameMap!=null); |
| 121 |
}else{ |
| 122 |
tree=null; |
| 123 |
throw new RuntimeException("No tree specified."); |
| 124 |
} |
| 125 |
lifeNode=tree.getNodeByName("life"); |
| 126 |
} |
| 127 |
|
| 128 |
void process(Timer t){ |
| 129 |
|
| 130 |
ByteFile bf=ByteFile.makeByteFile(ffin1); |
| 131 |
ByteStreamWriter bsw=new ByteStreamWriter(ffout1); |
| 132 |
bsw.start(); |
| 133 |
|
| 134 |
bsw.print("#Name\tAncestor\tMajority\tTaxonomy...\n".getBytes()); |
| 135 |
|
| 136 |
ByteStreamWriter bswInvalid=null; |
| 137 |
if(ffoutInvalid!=null){ |
| 138 |
bswInvalid=new ByteStreamWriter(ffoutInvalid); |
| 139 |
bswInvalid.start(); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
final IntList giList=new IntList(); |
| 144 |
final IntList tidList=new IntList(); |
| 145 |
final IntList traversal=new IntList(); |
| 146 |
|
| 147 |
byte[] line=bf.nextLine(); |
| 148 |
ByteBuilder bb=new ByteBuilder(); |
| 149 |
|
| 150 |
while(line!=null){ |
| 151 |
if(line.length>0){ |
| 152 |
if(maxLines>0 && linesProcessed>=maxLines){break;} |
| 153 |
linesProcessed++; |
| 154 |
bytesProcessed+=line.length; |
| 155 |
|
| 156 |
giList.clear(); |
| 157 |
tidList.clear(); |
| 158 |
traversal.clear(); |
| 159 |
|
| 160 |
final int giCount=getGiNumbers(line, giList, ','); |
| 161 |
final int ncbiCount=getTaxidNumbers(giList, tidList); |
| 162 |
|
| 163 |
taxaCounted+=giCount; |
| 164 |
taxaValid+=ncbiCount; |
| 165 |
final boolean valid=(ncbiCount>0); |
| 166 |
|
| 167 |
if(valid){ |
| 168 |
linesValid++; |
| 169 |
int ancestor=findAncestor(tidList); |
| 170 |
int majority=findMajority(tidList); |
| 171 |
|
| 172 |
for(int i=0; i<line.length && line[i]!='\t'; i++){ |
| 173 |
bb.append(line[i]); |
| 174 |
} |
| 175 |
bb.tab(); |
| 176 |
bb.append(ancestor); |
| 177 |
bb.tab(); |
| 178 |
bb.append(majority); |
| 179 |
bb.tab(); |
| 180 |
|
| 181 |
fillTraversal(majority, traversal, true); |
| 182 |
writeTraversal(traversal, bb); |
| 183 |
bb.nl(); |
| 184 |
|
| 185 |
for(int i=0; i<tidList.size; i++){ |
| 186 |
final int id=tidList.get(i); |
| 187 |
fillTraversal(id, traversal, true); |
| 188 |
writeTraversal(traversal, bb); |
| 189 |
bb.nl(); |
| 190 |
} |
| 191 |
|
| 192 |
bsw.print(bb.toBytes()); |
| 193 |
bb.clear(); |
| 194 |
}else{ |
| 195 |
if(bswInvalid!=null){ |
| 196 |
bswInvalid.println(line); |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
line=bf.nextLine(); |
| 201 |
} |
| 202 |
|
| 203 |
errorState|=bf.close(); |
| 204 |
if(bsw!=null){errorState|=bsw.poisonAndWait();} |
| 205 |
if(bswInvalid!=null){errorState|=bswInvalid.poisonAndWait();} |
| 206 |
|
| 207 |
t.stop(); |
| 208 |
outstream.println(Tools.timeLinesBytesProcessed(t, linesProcessed, bytesProcessed, 8)); |
| 209 |
|
| 210 |
outstream.println(); |
| 211 |
outstream.println("Valid Lines: \t"+linesValid); |
| 212 |
outstream.println("Invalid Lines: \t"+(linesProcessed-linesValid)); |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
if(errorState){ |
| 218 |
throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt."); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
private void fillTraversal(int id, IntList traversal, boolean addLife){ |
| 223 |
traversal.clear(); |
| 224 |
for(TaxNode node=tree.getNode(id); node!=null && node!=lifeNode; node=tree.getNode(node.pid)){ |
| 225 |
traversal.add(node.id); |
| 226 |
} |
| 227 |
if(addLife || traversal.size==0){traversal.add(lifeNode.id);} |
| 228 |
} |
| 229 |
|
| 230 |
private void writeTraversal(IntList traversal, ByteBuilder bb){ |
| 231 |
for(int i=traversal.size-1; i>=0; i--){ |
| 232 |
final int id=traversal.get(i); |
| 233 |
if(id>=0){ |
| 234 |
TaxNode tn=tree.getNode(id); |
| 235 |
|
| 236 |
bb.append(tn.name); |
| 237 |
if(i>0){bb.tab();} |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
private int getGiNumbers(final byte[] line, final IntList list, final char delimiter){ |
| 243 |
int i=0; |
| 244 |
|
| 245 |
|
| 246 |
while(i<line.length && line[i]!='\t'){i++;} |
| 247 |
|
| 248 |
|
| 249 |
while(i<line.length && Character.isWhitespace(line[i])){i++;} |
| 250 |
|
| 251 |
while(i<line.length){ |
| 252 |
while(i<line.length && line[i]==delimiter){i++;} |
| 253 |
int start=i; |
| 254 |
while(i<line.length && line[i]!=delimiter){i++;} |
| 255 |
final int stop=i; |
| 256 |
if(Tools.startsWith(line, prefix, start)){start+=3;} |
| 257 |
assert(start<stop) : "Badly formatted line at "+start+":\n"+new String(line); |
| 258 |
|
| 259 |
if(start<stop){ |
| 260 |
final int number=Parse.parseInt(line, start, stop); |
| 261 |
list.add(number); |
| 262 |
} |
| 263 |
} |
| 264 |
return list.size; |
| 265 |
} |
| 266 |
|
| 267 |
private static int getTaxidNumbers(final IntList giList, final IntList ncbiList){ |
| 268 |
final int size=giList.size; |
| 269 |
for(int i=0; i<size; i++){ |
| 270 |
final int gi=giList.get(i); |
| 271 |
final int ncbi=GiToTaxid.getID(gi); |
| 272 |
|
| 273 |
if(ncbi>=0){ncbiList.add(ncbi);} |
| 274 |
} |
| 275 |
return ncbiList.size; |
| 276 |
} |
| 277 |
|
| 278 |
private int findAncestor(IntList list){ |
| 279 |
return findAncestor(tree, list); |
| 280 |
} |
| 281 |
|
| 282 |
public static int findAncestor(TaxTree tree, IntList list){ |
| 283 |
if(list.size<1){ |
| 284 |
assert(false); |
| 285 |
return -1; |
| 286 |
} |
| 287 |
int ancestor=list.get(0); |
| 288 |
for(int i=1; i<list.size && ancestor>-1; i++){ |
| 289 |
final int id=list.get(i); |
| 290 |
|
| 291 |
int x=tree.commonAncestor(ancestor, id); |
| 292 |
if(x>-1){ |
| 293 |
ancestor=x; |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
return ancestor; |
| 300 |
} |
| 301 |
|
| 302 |
private int findMajority(IntList list){ |
| 303 |
if(list.size<3){return findAncestor(list);} |
| 304 |
final int majority=list.size/2+1; |
| 305 |
|
| 306 |
|
| 307 |
for(int i=0; i<list.size; i++){ |
| 308 |
final int id=list.get(i); |
| 309 |
TaxNode tn=tree.getNode(id); |
| 310 |
|
| 311 |
assert(tn!=null) : "No node for id "+id; |
| 312 |
if(tn!=null){ |
| 313 |
tree.percolateUp(tn, 1); |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
TaxNode best=lifeNode; |
| 318 |
for(int i=0; i<list.size; i++){ |
| 319 |
final int id=list.get(i); |
| 320 |
TaxNode tn=tree.getNode(id); |
| 321 |
while(tn!=null && tn!=lifeNode){ |
| 322 |
if(tn.countSum>=majority && tn.levelExtended<best.levelExtended){ |
| 323 |
best=tn; |
| 324 |
break; |
| 325 |
} |
| 326 |
tn=tree.getNode(tn.pid); |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
for(int i=0; i<list.size; i++){ |
| 333 |
final int id=list.get(i); |
| 334 |
TaxNode tn=tree.getNode(id); |
| 335 |
if(tn!=null){ |
| 336 |
tree.percolateUp(tn, -1); |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
return best.id; |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
private String in1=null; |
| 349 |
private String out1=null; |
| 350 |
private String outInvalid=null; |
| 351 |
|
| 352 |
private String giTableFile=null; |
| 353 |
private String taxTreeFile=null; |
| 354 |
|
| 355 |
private final TaxTree tree; |
| 356 |
|
| 357 |
private final TaxNode lifeNode; |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
private long taxaCounted=0; |
| 362 |
private long taxaValid=0; |
| 363 |
private long linesProcessed=0; |
| 364 |
private long linesValid=0; |
| 365 |
private long bytesProcessed=0; |
| 366 |
|
| 367 |
private long maxLines=Long.MAX_VALUE; |
| 368 |
|
| 369 |
|
| 370 |
private boolean countTable=true; |
| 371 |
|
| 372 |
|
| 373 |
private final String prefix="gi|"; |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
private final FileFormat ffin1; |
| 378 |
private final FileFormat ffout1; |
| 379 |
private final FileFormat ffoutInvalid; |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
private PrintStream outstream=System.err; |
| 385 |
public static boolean verbose=false; |
| 386 |
public boolean errorState=false; |
| 387 |
private boolean overwrite=true; |
| 388 |
private boolean append=false; |
| 389 |
|
| 390 |
} |