| 1 |
package tax; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.PrintStream; |
| 5 |
import java.util.ArrayList; |
| 6 |
import java.util.Arrays; |
| 7 |
import java.util.LinkedHashSet; |
| 8 |
|
| 9 |
import fileIO.ByteFile; |
| 10 |
import fileIO.ByteFile1; |
| 11 |
import fileIO.ByteFile2; |
| 12 |
import fileIO.ByteStreamWriter; |
| 13 |
import fileIO.FileFormat; |
| 14 |
import fileIO.ReadWrite; |
| 15 |
import kmer.HashArray1D; |
| 16 |
import shared.KillSwitch; |
| 17 |
import shared.Parse; |
| 18 |
import shared.Parser; |
| 19 |
import shared.PreParser; |
| 20 |
import shared.Shared; |
| 21 |
import shared.Timer; |
| 22 |
import shared.Tools; |
| 23 |
import stream.ConcurrentGenericReadInputStream; |
| 24 |
import stream.FASTQ; |
| 25 |
import stream.FastaReadInputStream; |
| 26 |
import structures.ByteBuilder; |
| 27 |
import structures.IntList; |
| 28 |
import tracker.ReadStats; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class RenameGiToTaxid { |
| 36 |
|
| 37 |
public static void main(String[] args){ |
| 38 |
Timer t=new Timer(); |
| 39 |
RenameGiToTaxid x=new RenameGiToTaxid(args); |
| 40 |
x.process(t); |
| 41 |
|
| 42 |
|
| 43 |
Shared.closeStream(x.outstream); |
| 44 |
} |
| 45 |
|
| 46 |
public RenameGiToTaxid(String[] args){ |
| 47 |
|
| 48 |
{ |
| 49 |
PreParser pp=new PreParser(args, getClass(), false); |
| 50 |
args=pp.args; |
| 51 |
outstream=pp.outstream; |
| 52 |
} |
| 53 |
|
| 54 |
Shared.capBuffers(4); |
| 55 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 56 |
ReadWrite.USE_BGZIP=ReadWrite.USE_UNBGZIP=ReadWrite.PREFER_BGZIP=true; |
| 57 |
ReadWrite.setZipThreads(Shared.threads()); |
| 58 |
FASTQ.TEST_INTERLEAVED=FASTQ.FORCE_INTERLEAVED=false; |
| 59 |
|
| 60 |
Parser parser=new Parser(); |
| 61 |
for(int i=0; i<args.length; i++){ |
| 62 |
String arg=args[i]; |
| 63 |
String[] split=arg.split("="); |
| 64 |
String a=split[0].toLowerCase(); |
| 65 |
String b=split.length>1 ? split[1] : null; |
| 66 |
|
| 67 |
if(a.equals("prefix")){ |
| 68 |
prefix=Parse.parseBoolean(b); |
| 69 |
|
| 70 |
}else if(a.equals("server") || a.equals("useserver")){ |
| 71 |
if(b!=null && b.startsWith("http")){ |
| 72 |
useServer=true; |
| 73 |
String path=b; |
| 74 |
if(!path.endsWith("/")){path+="/";} |
| 75 |
Shared.setTaxServer(path); |
| 76 |
}else{ |
| 77 |
useServer=Parse.parseBoolean(b); |
| 78 |
} |
| 79 |
}else if(a.equals("title")){ |
| 80 |
title=(b==null ? ">" : (">"+b+"|")).getBytes(); |
| 81 |
}else if(a.equals("table") || a.equals("gi") || a.equals("gitable")){ |
| 82 |
giTableFile=b; |
| 83 |
}else if(a.equals("accession")){ |
| 84 |
accessionFile=b; |
| 85 |
}else if(a.equals("pattern")){ |
| 86 |
patternFile=b; |
| 87 |
}else if(a.equals("tree") || a.equals("taxtree")){ |
| 88 |
taxTreeFile=b; |
| 89 |
}else if(a.equals("invalid")){ |
| 90 |
outInvalid=b; |
| 91 |
}else if(a.equals("deleteinvalid")){ |
| 92 |
deleteInvalid=Parse.parseBoolean(b); |
| 93 |
}else if(a.equals("badheaders")){ |
| 94 |
badHeaders=b; |
| 95 |
}else if(a.equals("maxbadheaders") || a.equals("maxinvalidheaders")){ |
| 96 |
maxInvalidHeaders=Parse.parseKMG(b); |
| 97 |
}else if(a.equals("keepall")){ |
| 98 |
keepAll=Parse.parseBoolean(b); |
| 99 |
}else if(a.equals("shrinknames")){ |
| 100 |
shrinkNames=Parse.parseBoolean(b); |
| 101 |
}else if(a.equals("warn")){ |
| 102 |
warnBadHeaders=Parse.parseBoolean(b); |
| 103 |
} |
| 104 |
|
| 105 |
else if(a.equals("maxpigzprocesses")){ |
| 106 |
AccessionToTaxid.maxPigzProcesses=Integer.parseInt(b); |
| 107 |
}else if(a.equals("skipparse")){ |
| 108 |
AccessionToTaxid.skipParse=Parse.parseBoolean(b); |
| 109 |
}else if(a.equals("skiphash")){ |
| 110 |
AccessionToTaxid.skipHash=Parse.parseBoolean(b); |
| 111 |
} |
| 112 |
|
| 113 |
else if(a.equals("mode")){ |
| 114 |
if(b!=null && Character.isDigit(b.charAt(0))){ |
| 115 |
mode=Integer.parseInt(b); |
| 116 |
}else if("accession".equalsIgnoreCase(b)){ |
| 117 |
mode=ACCESSION_MODE; |
| 118 |
}else if("unite".equalsIgnoreCase(b)){ |
| 119 |
mode=UNITE_MODE; |
| 120 |
TaxTree.UNITE_MODE=true; |
| 121 |
}else if("gi".equalsIgnoreCase(b)){ |
| 122 |
mode=GI_MODE; |
| 123 |
}else if("header".equalsIgnoreCase(b)){ |
| 124 |
mode=HEADER_MODE; |
| 125 |
}else{ |
| 126 |
assert(false) : "Bad mode: "+b; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
else if(a.equals("verbose")){ |
| 131 |
verbose=Parse.parseBoolean(b); |
| 132 |
ByteFile1.verbose=verbose; |
| 133 |
ByteFile2.verbose=verbose; |
| 134 |
stream.FastaReadInputStream.verbose=verbose; |
| 135 |
ConcurrentGenericReadInputStream.verbose=verbose; |
| 136 |
stream.FastqReadInputStream.verbose=verbose; |
| 137 |
ReadWrite.verbose=verbose; |
| 138 |
}else if(a.equals("in") || a.equals("in1")){ |
| 139 |
assert(b!=null) : "Bad parameter: "+arg; |
| 140 |
if(new File(b).exists()){ |
| 141 |
in1.add(b); |
| 142 |
}else{ |
| 143 |
for(String bb : b.split(",")){ |
| 144 |
in1.add(bb); |
| 145 |
} |
| 146 |
} |
| 147 |
}else if(new File(arg).exists()){ |
| 148 |
in1.add(arg); |
| 149 |
}else if(parser.parse(arg, a, b)){ |
| 150 |
|
| 151 |
}else{ |
| 152 |
outstream.println("Unknown parameter "+args[i]); |
| 153 |
assert(false) : "Unknown parameter "+args[i]; |
| 154 |
|
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
if(useServer){ |
| 159 |
giTableFile=null; |
| 160 |
accessionFile=null; |
| 161 |
patternFile=null; |
| 162 |
if(mode!=UNITE_MODE){taxTreeFile=null;} |
| 163 |
} |
| 164 |
|
| 165 |
{ |
| 166 |
Parser.processQuality(); |
| 167 |
|
| 168 |
maxReads=parser.maxReads; |
| 169 |
|
| 170 |
overwrite=ReadStats.overwrite=parser.overwrite; |
| 171 |
append=ReadStats.append=parser.append; |
| 172 |
|
| 173 |
out1=parser.out1; |
| 174 |
} |
| 175 |
|
| 176 |
if("auto".equalsIgnoreCase(taxTreeFile)){taxTreeFile=TaxTree.defaultTreeFile();} |
| 177 |
if("auto".equalsIgnoreCase(giTableFile)){giTableFile=TaxTree.defaultTableFile();} |
| 178 |
if("auto".equalsIgnoreCase(accessionFile)){accessionFile=TaxTree.defaultAccessionFile();} |
| 179 |
if("auto".equalsIgnoreCase(patternFile)){patternFile=TaxTree.defaultPatternFile();} |
| 180 |
|
| 181 |
assert(FastaReadInputStream.settingsOK()); |
| 182 |
|
| 183 |
if(in1==null || in1.isEmpty()){throw new RuntimeException("Error - at least one input file is required.");} |
| 184 |
if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2){ |
| 185 |
ByteFile.FORCE_MODE_BF2=false; |
| 186 |
ByteFile.FORCE_MODE_BF1=true; |
| 187 |
} |
| 188 |
|
| 189 |
if(out1!=null && out1.equalsIgnoreCase("null")){out1=null;} |
| 190 |
assert(out1!=null) : "This program requires an output file."; |
| 191 |
|
| 192 |
if(!Tools.testOutputFiles(overwrite, append, false, out1)){ |
| 193 |
outstream.println((out1==null)+", "+out1); |
| 194 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out1+"\n"); |
| 195 |
} |
| 196 |
if(!Tools.testInputFiles(false, true, in1.toArray(new String[0]))){ |
| 197 |
throw new RuntimeException("\nCan't read some input files.\n"); |
| 198 |
} |
| 199 |
|
| 200 |
ffout1=FileFormat.testOutput(out1, FileFormat.FA, null, true, overwrite, append, false); |
| 201 |
ffoutInvalid=FileFormat.testOutput(outInvalid, FileFormat.FA, null, true, overwrite, append, false); |
| 202 |
ffin1=new ArrayList<FileFormat>(in1.size()); |
| 203 |
for(String s : in1){ |
| 204 |
FileFormat ff=FileFormat.testInput(s, FileFormat.FA, null, true, true); |
| 205 |
ffin1.add(ff); |
| 206 |
} |
| 207 |
|
| 208 |
if(ffoutInvalid!=null){keepAll=false;} |
| 209 |
|
| 210 |
assert(giTableFile!=null || accessionFile!=null || TaxTree.SILVA_MODE || useServer) : "No gi or accession information loaded."; |
| 211 |
|
| 212 |
if(taxTreeFile!=null){ |
| 213 |
tree=TaxTree.loadTaxTree(taxTreeFile, outstream, true, false); |
| 214 |
assert(tree.nameMap!=null); |
| 215 |
}else{ |
| 216 |
tree=null; |
| 217 |
if(!useServer){throw new RuntimeException("No tree specified.");} |
| 218 |
} |
| 219 |
|
| 220 |
if(giTableFile!=null){ |
| 221 |
GiToTaxid.initialize(giTableFile); |
| 222 |
} |
| 223 |
|
| 224 |
if(patternFile!=null){ |
| 225 |
Timer t=new Timer(); |
| 226 |
AnalyzeAccession.loadCodeMap(patternFile); |
| 227 |
outstream.println("Loading pattern table."); |
| 228 |
t.stopAndPrint(); |
| 229 |
} |
| 230 |
|
| 231 |
if(accessionFile!=null){ |
| 232 |
AccessionToTaxid.tree=tree; |
| 233 |
outstream.println("Loading accession table."); |
| 234 |
AccessionToTaxid.load(accessionFile); |
| 235 |
|
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
void process(Timer t){ |
| 240 |
|
| 241 |
ByteStreamWriter bsw=(ffout1==null ? null : new ByteStreamWriter(ffout1)); |
| 242 |
if(bsw!=null){bsw.start();} |
| 243 |
|
| 244 |
ByteStreamWriter bswInvalid=null; |
| 245 |
if(ffoutInvalid!=null){ |
| 246 |
bswInvalid=new ByteStreamWriter(ffoutInvalid); |
| 247 |
bswInvalid.start(); |
| 248 |
} |
| 249 |
|
| 250 |
ByteStreamWriter bswBadHeaders=null; |
| 251 |
if(badHeaders!=null) { |
| 252 |
bswBadHeaders=new ByteStreamWriter(badHeaders, overwrite, append, false); |
| 253 |
bswBadHeaders.start(); |
| 254 |
} |
| 255 |
|
| 256 |
final HashArray1D counts=(countTable && !prefix) ? new HashArray1D(256000, -1L, true) : null; |
| 257 |
|
| 258 |
gffIn=false; |
| 259 |
for(FileFormat ffin : ffin1){ |
| 260 |
gffIn=gffIn||ffin.gff(); |
| 261 |
ByteFile bf=ByteFile.makeByteFile(ffin); |
| 262 |
if(useServer){ |
| 263 |
processInner_server(bf, bsw, bswInvalid, bswBadHeaders, counts, ffin.format()); |
| 264 |
}else{ |
| 265 |
|
| 266 |
processInner(bf, bsw, bswInvalid, bswBadHeaders, counts, null); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
if(bsw!=null){ |
| 271 |
errorState|=bsw.poisonAndWait(); |
| 272 |
if(deleteInvalid && invalidReads>0 && !ffout1.stdio()){ |
| 273 |
try { |
| 274 |
System.err.println("Deleting "+out1); |
| 275 |
new File(out1).delete(); |
| 276 |
} catch (Exception e) { |
| 277 |
System.err.println("An error occured while attempting to delete "+out1); |
| 278 |
e.printStackTrace(); |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
if(bswInvalid!=null){errorState|=bswInvalid.poisonAndWait();} |
| 283 |
if(bswBadHeaders!=null){errorState|=bswBadHeaders.poisonAndWait();} |
| 284 |
|
| 285 |
t.stop(); |
| 286 |
if(!gffIn) { |
| 287 |
outstream.println(Tools.timeReadsBasesProcessed(t, readsProcessed, basesProcessed, 8)); |
| 288 |
|
| 289 |
outstream.println(); |
| 290 |
outstream.println("Valid Sequences: \t"+validReads); |
| 291 |
outstream.println("Valid Bases: \t"+validBases); |
| 292 |
outstream.println("Invalid Sequences: \t"+invalidReads); |
| 293 |
outstream.println("Invalid Bases: \t"+invalidBases); |
| 294 |
}else{ |
| 295 |
outstream.println(Tools.timeLinesBytesProcessed(t, linesIn, basesProcessed, 8)); |
| 296 |
|
| 297 |
outstream.println(); |
| 298 |
outstream.println("Valid Lines: \t"+validLines); |
| 299 |
outstream.println("Valid Bytes: \t"+validBases); |
| 300 |
outstream.println("Invalid Lines: \t"+invalidLines); |
| 301 |
outstream.println("Invalid Bytes: \t"+invalidBases); |
| 302 |
} |
| 303 |
if(counts!=null){ |
| 304 |
outstream.println("Unique Taxa: \t"+taxaCounted); |
| 305 |
} |
| 306 |
|
| 307 |
if(errorState){ |
| 308 |
throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt."); |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
private void processInner(ByteFile bf, ByteStreamWriter bsw, ByteStreamWriter bswInvalid, ByteStreamWriter bswBadHeaders, HashArray1D counts, IntList ids){ |
| 368 |
|
| 369 |
int readsProcessedInner=0; |
| 370 |
|
| 371 |
byte[] line=bf.nextLine(); |
| 372 |
boolean valid=false; |
| 373 |
while(line!=null){ |
| 374 |
if(line.length>0 && line[0]=='>'){ |
| 375 |
readsProcessedInner++; |
| 376 |
readsProcessed++; |
| 377 |
if(maxReads>0 && readsProcessed>maxReads){break;} |
| 378 |
int initial=1, terminal=line.length; |
| 379 |
final int number; |
| 380 |
if(ids==null){ |
| 381 |
final TaxNode tn; |
| 382 |
|
| 383 |
{ |
| 384 |
{ |
| 385 |
|
| 386 |
if(Tools.startsWith(line, ">tid|")){ |
| 387 |
initial=6; |
| 388 |
while(initial<=line.length && line[initial-1]!='|'){initial++;} |
| 389 |
}else if(Tools.startsWith(line, ">ncbi|")){ |
| 390 |
initial=7; |
| 391 |
while(initial<=line.length && line[initial-1]!='|'){initial++;} |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
if(shrinkNames){ |
| 396 |
for(int i=initial; i<terminal; i++){ |
| 397 |
if(line[i]==1){ |
| 398 |
terminal=i; |
| 399 |
} |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
String s=new String(line, initial, terminal-initial); |
| 404 |
|
| 405 |
tn=tree.parseNodeFromHeader(s, true); |
| 406 |
} |
| 407 |
number=(tn==null ? -1 : tn.id); |
| 408 |
}else{ |
| 409 |
number=ids.get((int)(readsProcessedInner-1)); |
| 410 |
|
| 411 |
if(shrinkNames){ |
| 412 |
for(int i=initial; i<terminal; i++){ |
| 413 |
if(line[i]==1){ |
| 414 |
terminal=i; |
| 415 |
} |
| 416 |
} |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
valid=(number>=0); |
| 421 |
if(valid){ |
| 422 |
validReads++; |
| 423 |
bsw.print(title); |
| 424 |
bsw.print(number); |
| 425 |
if(prefix){ |
| 426 |
bsw.print('|'); |
| 427 |
for(int i=initial; i<terminal; i++){ |
| 428 |
bsw.print(line[i]); |
| 429 |
} |
| 430 |
}else if(counts!=null){ |
| 431 |
bsw.print('|'); |
| 432 |
int count=counts.increment(number, 1); |
| 433 |
bsw.print(count); |
| 434 |
if(count==1){taxaCounted++;} |
| 435 |
} |
| 436 |
bsw.println(); |
| 437 |
}else{ |
| 438 |
invalidReads++; |
| 439 |
if(deleteInvalid){ |
| 440 |
System.err.println("Invalid sequence detected; aborting.\n"); |
| 441 |
break; |
| 442 |
} |
| 443 |
if(bswBadHeaders!=null){bswBadHeaders.println(line);} |
| 444 |
if(maxInvalidHeaders>=0 && invalidReads>maxInvalidHeaders){ |
| 445 |
KillSwitch.kill("Maximum bad headers exceeded: "+maxInvalidHeaders+"\n"+new String(line)); |
| 446 |
} |
| 447 |
if(keepAll){ |
| 448 |
if(shrinkNames){ |
| 449 |
for(int i=0; i<terminal; i++){ |
| 450 |
bsw.print(line[i]); |
| 451 |
} |
| 452 |
bsw.println(); |
| 453 |
}else{ |
| 454 |
bsw.println(line); |
| 455 |
} |
| 456 |
}else if(bswInvalid!=null){ |
| 457 |
if(shrinkNames){ |
| 458 |
for(int i=0; i<terminal; i++){ |
| 459 |
bswInvalid.print(line[i]); |
| 460 |
} |
| 461 |
bswInvalid.println(); |
| 462 |
}else{ |
| 463 |
bswInvalid.println(line); |
| 464 |
} |
| 465 |
} |
| 466 |
} |
| 467 |
}else{ |
| 468 |
basesProcessed+=line.length; |
| 469 |
if(valid || keepAll){ |
| 470 |
if(valid){validBases+=line.length;} |
| 471 |
else{invalidBases+=line.length;} |
| 472 |
bsw.println(line); |
| 473 |
}else{ |
| 474 |
invalidBases+=line.length; |
| 475 |
if(bswInvalid!=null){ |
| 476 |
bswInvalid.println(line); |
| 477 |
} |
| 478 |
} |
| 479 |
} |
| 480 |
line=bf.nextLine(); |
| 481 |
} |
| 482 |
|
| 483 |
errorState|=bf.close(); |
| 484 |
} |
| 485 |
|
| 486 |
private static boolean looksLikeRealAccession(byte[] line){ |
| 487 |
int space=Tools.indexOf(line, ' '); |
| 488 |
if(space<0){space=line.length;} |
| 489 |
if(space>18 || space<4){return false;} |
| 490 |
|
| 491 |
int dot=-1; |
| 492 |
for(int i=0; i<space; i++){ |
| 493 |
if(line[i]=='.'){ |
| 494 |
if(dot>=0){return false;} |
| 495 |
dot=i; |
| 496 |
} |
| 497 |
} |
| 498 |
if(dot>0){ |
| 499 |
if(dot!=space-2){return false;} |
| 500 |
} |
| 501 |
for(int i=0; i<space; i++){ |
| 502 |
byte b=line[i]; |
| 503 |
if(b!='_' && b!='-' && b!='.' && !Tools.isLetterOrDigit(b)){return false;} |
| 504 |
} |
| 505 |
return true; |
| 506 |
} |
| 507 |
|
| 508 |
void appendHeaderLine(byte[] line, ByteBuilder bb){ |
| 509 |
assert(line[0]=='>' || line[0]=='@') : new String(line); |
| 510 |
|
| 511 |
if(mode==ACCESSION_MODE){ |
| 512 |
for(int i=1; i<line.length; i++){ |
| 513 |
byte b=line[i]; |
| 514 |
if(b==' ' || b=='.'){break;} |
| 515 |
else{bb.append(b);} |
| 516 |
} |
| 517 |
}else if(mode==GI_MODE){ |
| 518 |
for(int i=1; i<line.length; i++){ |
| 519 |
byte b=line[i]; |
| 520 |
if(b==' ' || b=='|'){break;} |
| 521 |
else{bb.append(b);} |
| 522 |
} |
| 523 |
}else if(mode==UNITE_MODE){ |
| 524 |
int initial=Tools.indexOf(line, '|'); |
| 525 |
for(int i=initial+1; i<line.length; i++){ |
| 526 |
byte b=line[i]; |
| 527 |
if(b==' ' || b=='.' || b=='|'){break;} |
| 528 |
else{bb.append(b);} |
| 529 |
} |
| 530 |
}else{ |
| 531 |
for(int i=1; i<line.length; i++){ |
| 532 |
byte b=line[i]; |
| 533 |
bb.append(b); |
| 534 |
} |
| 535 |
} |
| 536 |
bb.append(','); |
| 537 |
} |
| 538 |
|
| 539 |
private void updateHeadersFromServer(ArrayList<byte[]> lines, HashArray1D counts, ByteStreamWriter bswBadHeaders, int format){ |
| 540 |
if(format==FileFormat.FA){ |
| 541 |
updateHeadersFromServer_fasta(lines, counts, bswBadHeaders); |
| 542 |
}else if(format==FileFormat.GFF){ |
| 543 |
updateHeadersFromServer_gff(lines, counts, bswBadHeaders); |
| 544 |
}else{ |
| 545 |
assert(false) : "Unsupported type: "+format; |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
private void updateHeadersFromServer_fasta(ArrayList<byte[]> lines, HashArray1D counts, ByteStreamWriter bswBadHeaders){ |
| 550 |
ByteBuilder bb=new ByteBuilder(); |
| 551 |
ArrayList<String> names=new ArrayList<String>(); |
| 552 |
int terms=0; |
| 553 |
for(byte[] line : lines){ |
| 554 |
if(line[0]=='>' && !Tools.startsWith(line, ">tid")){ |
| 555 |
terms++; |
| 556 |
appendHeaderLine(line, bb); |
| 557 |
if(mode==UNITE_MODE){ |
| 558 |
int bar=Tools.indexOf(line, '|'); |
| 559 |
names.add(new String(line, 1, bar-1)); |
| 560 |
} |
| 561 |
} |
| 562 |
} |
| 563 |
if(bb.length()<1){return;} |
| 564 |
|
| 565 |
assert(terms>0) : bb.count(',')+", "+terms+"\n"+bb+"\n"; |
| 566 |
assert(bb.endsWith(',')) : bb.count(',')+", "+terms+"\n"+bb+"\n"; |
| 567 |
assert(bb.count(',')==terms) : bb.count(',')+", "+terms+"\n"+bb+"\n"; |
| 568 |
bb.length--; |
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
final int[] serverIds=translateToTaxIDs(bb, terms); |
| 573 |
assert(serverIds!=null) : "Null response for '"+bb.toString()+"'"; |
| 574 |
assert(serverIds.length==terms) : "Incorrect response length for query." |
| 575 |
+"\nQuery size "+terms+":\n"+bb |
| 576 |
+"\nResponse size "+serverIds.length+":\n"+Arrays.toString(serverIds)+"\n"; |
| 577 |
bb.clear(); |
| 578 |
|
| 579 |
if(!names.isEmpty()){ |
| 580 |
assert(tree!=null) : "Need to load a TaxTree."; |
| 581 |
assert(names.size()==serverIds.length); |
| 582 |
for(int i=0; i<serverIds.length; i++){ |
| 583 |
final String name=names.get(i); |
| 584 |
if(serverIds[i]<0){ |
| 585 |
TaxNode tn=tree.getNodeByName(name); |
| 586 |
if(tn!=null){serverIds[i]=tn.id;} |
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
}else{ |
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
} |
| 595 |
} |
| 596 |
} |
| 597 |
|
| 598 |
for(int lineNum=0, serverNum=0; lineNum<=lines.size(); lineNum++){ |
| 599 |
byte[] line=lines.get(lineNum); |
| 600 |
if(line[0]=='>' && !Tools.startsWith(line, ">tid")){ |
| 601 |
bb.clear(); |
| 602 |
final int tid=serverIds[serverNum]; |
| 603 |
if(tid<0){ |
| 604 |
|
| 605 |
if(bswBadHeaders!=null){ |
| 606 |
bswBadHeaders.print(tid).tab(); |
| 607 |
bswBadHeaders.print(looksLikeRealAccession(line)).tab(); |
| 608 |
bswBadHeaders.println(line); |
| 609 |
}else if(warnBadHeaders){ |
| 610 |
System.err.println(tid+"\t"+looksLikeRealAccession(line)+"\t"+new String(line)); |
| 611 |
} |
| 612 |
} |
| 613 |
int initial=1, terminal=line.length; |
| 614 |
if(shrinkNames){ |
| 615 |
for(int i=initial; i<terminal; i++){ |
| 616 |
if(line[i]==1){ |
| 617 |
terminal=i; |
| 618 |
} |
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
bb.append(title); |
| 623 |
bb.append(tid); |
| 624 |
if(prefix){ |
| 625 |
bb.append('|'); |
| 626 |
for(int i=initial; i<terminal; i++){ |
| 627 |
bb.append(line[i]); |
| 628 |
} |
| 629 |
}else if(counts!=null && tid>=0){ |
| 630 |
bb.append('|'); |
| 631 |
int count=counts.increment(tid, 1); |
| 632 |
bb.append(count); |
| 633 |
if(count==1){taxaCounted++;} |
| 634 |
} |
| 635 |
|
| 636 |
lines.set(lineNum, bb.toBytes()); |
| 637 |
|
| 638 |
serverNum++; |
| 639 |
if(serverNum>=serverIds.length){break;} |
| 640 |
} |
| 641 |
} |
| 642 |
if(maxInvalidHeaders>=0 && invalidReads>maxInvalidHeaders){ |
| 643 |
KillSwitch.kill("Maximum bad headers exceeded: "+maxInvalidHeaders); |
| 644 |
} |
| 645 |
} |
| 646 |
|
| 647 |
private int[] translateToTaxIDs(ByteBuilder bb, int terms) { |
| 648 |
|
| 649 |
|
| 650 |
int[] response=null; |
| 651 |
final String query=bb.toString(); |
| 652 |
for(int i=0; i<10 && response==null; i++) { |
| 653 |
if(mode==ACCESSION_MODE || mode==UNITE_MODE){ |
| 654 |
response=TaxClient.accessionToTaxidArray(query); |
| 655 |
}else if(mode==GI_MODE){ |
| 656 |
response=TaxClient.giToTaxidArray(query); |
| 657 |
}else{ |
| 658 |
response=TaxClient.headerToTaxidArray(query); |
| 659 |
} |
| 660 |
if(response==null) { |
| 661 |
System.err.println("Retrying null response for length "+terms+" query: "+query); |
| 662 |
}else if(response.length!=terms) { |
| 663 |
System.err.println("Retrying corrupt length "+response.length+" response for length "+terms+" query: "+query); |
| 664 |
response=null; |
| 665 |
} |
| 666 |
if(response==null) { |
| 667 |
try { |
| 668 |
Thread.sleep(500*(i+2)); |
| 669 |
} catch (InterruptedException e) { |
| 670 |
e.printStackTrace(); |
| 671 |
} |
| 672 |
} |
| 673 |
} |
| 674 |
return response; |
| 675 |
} |
| 676 |
|
| 677 |
private void updateHeadersFromServer_gff(ArrayList<byte[]> lines, HashArray1D counts, ByteStreamWriter bswBadHeaders){ |
| 678 |
ByteBuilder bb=new ByteBuilder(); |
| 679 |
ArrayList<String> names=new ArrayList<String>(); |
| 680 |
for(byte[] line : lines){ |
| 681 |
if(line[0]!='#' && !Tools.startsWith(line, "tid")){ |
| 682 |
if(bb.length()>0){bb.append(',');} |
| 683 |
for(byte b : line){ |
| 684 |
if(b=='\t'){break;} |
| 685 |
bb.append(b); |
| 686 |
} |
| 687 |
} |
| 688 |
} |
| 689 |
if(bb.length()<1){return;} |
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
int[] serverIds; |
| 696 |
if(mode==ACCESSION_MODE || mode==UNITE_MODE){ |
| 697 |
serverIds=TaxClient.accessionToTaxidArray(bb.toString()); |
| 698 |
}else if(mode==GI_MODE){ |
| 699 |
serverIds=TaxClient.giToTaxidArray(bb.toString()); |
| 700 |
}else{ |
| 701 |
serverIds=TaxClient.headerToTaxidArray(bb.toString()); |
| 702 |
} |
| 703 |
if(serverIds==null){ |
| 704 |
KillSwitch.kill("Null response for '"+bb.toString()+"'"); |
| 705 |
} |
| 706 |
|
| 707 |
bb.clear(); |
| 708 |
|
| 709 |
if(!names.isEmpty()){ |
| 710 |
assert(tree!=null) : "Need to load a TaxTree."; |
| 711 |
assert(names.size()==serverIds.length); |
| 712 |
for(int i=0; i<serverIds.length; i++){ |
| 713 |
final String name=names.get(i); |
| 714 |
if(serverIds[i]<0){ |
| 715 |
TaxNode tn=tree.getNodeByName(name); |
| 716 |
if(tn!=null){serverIds[i]=tn.id;} |
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
}else{ |
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
} |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
for(int lineNum=0, serverNum=0; lineNum<=lines.size(); lineNum++){ |
| 729 |
byte[] line=lines.get(lineNum); |
| 730 |
if(line[0]!='#' && !Tools.startsWith(line, "tid")){ |
| 731 |
bb.clear(); |
| 732 |
final int tid=serverIds[serverNum]; |
| 733 |
if(tid<0){ |
| 734 |
|
| 735 |
if(bswBadHeaders!=null){ |
| 736 |
bswBadHeaders.print(tid).tab(); |
| 737 |
bswBadHeaders.print(looksLikeRealAccession(line)).tab(); |
| 738 |
bswBadHeaders.println(line); |
| 739 |
}else if(warnBadHeaders){ |
| 740 |
System.err.println(tid+"\t"+looksLikeRealAccession(line)+"\t"+new String(line)); |
| 741 |
} |
| 742 |
} |
| 743 |
|
| 744 |
bb.append("tid|"); |
| 745 |
bb.append(tid); |
| 746 |
if(prefix){ |
| 747 |
bb.append('|'); |
| 748 |
bb.append(line); |
| 749 |
}else if(counts!=null && tid>=0){ |
| 750 |
bb.append('|'); |
| 751 |
int count=counts.increment(tid, 1); |
| 752 |
bb.append(count); |
| 753 |
if(count==1){taxaCounted++;} |
| 754 |
} |
| 755 |
|
| 756 |
lines.set(lineNum, bb.toBytes()); |
| 757 |
|
| 758 |
serverNum++; |
| 759 |
if(serverNum>=serverIds.length){break;} |
| 760 |
} |
| 761 |
} |
| 762 |
if(maxInvalidHeaders>=0 && invalidReads>maxInvalidHeaders){ |
| 763 |
KillSwitch.kill("Maximum bad headers exceeded: "+maxInvalidHeaders); |
| 764 |
} |
| 765 |
} |
| 766 |
|
| 767 |
private void processInner_server(ByteFile bf, ByteStreamWriter bsw, ByteStreamWriter bswInvalid, ByteStreamWriter bswBadHeaders, HashArray1D counts, int format){ |
| 768 |
|
| 769 |
ArrayList<byte[]> lines=new ArrayList<byte[]>(); |
| 770 |
byte[] line=bf.nextLine(); |
| 771 |
boolean valid=false; |
| 772 |
long storedBytes=0; |
| 773 |
|
| 774 |
while(line!=null){ |
| 775 |
|
| 776 |
if(line.length>0){ |
| 777 |
linesIn++; |
| 778 |
lines.add(line); |
| 779 |
storedBytes+=line.length; |
| 780 |
if(storedBytes>=maxStoredBytes){ |
| 781 |
updateHeadersFromServer(lines, counts, bswBadHeaders, format); |
| 782 |
valid=dumpBuffer(lines, valid, bsw, bswInvalid); |
| 783 |
lines=new ArrayList<byte[]>(); |
| 784 |
storedBytes=0; |
| 785 |
if(deleteInvalid && invalidReads>0){ |
| 786 |
System.err.println("Invalid sequence detected; aborting.\n" |
| 787 |
+ "Input file: \t"+bf.name()+"\n" |
| 788 |
+ "Output file: \t"+(bsw==null ? "null" : bsw.fname)+"\n" |
| 789 |
+ "Line: \t"+new String(line)+"\n"); |
| 790 |
break; |
| 791 |
} |
| 792 |
} |
| 793 |
} |
| 794 |
line=bf.nextLine(); |
| 795 |
} |
| 796 |
|
| 797 |
if(storedBytes>0){ |
| 798 |
updateHeadersFromServer(lines, counts, bswBadHeaders, format); |
| 799 |
valid=dumpBuffer(lines, valid, bsw, bswInvalid); |
| 800 |
lines=new ArrayList<byte[]>(); |
| 801 |
storedBytes=0; |
| 802 |
} |
| 803 |
|
| 804 |
errorState|=bf.close(); |
| 805 |
} |
| 806 |
|
| 807 |
private boolean dumpBuffer(ArrayList<byte[]> lines, boolean valid, ByteStreamWriter bsw, ByteStreamWriter bswInvalid){ |
| 808 |
|
| 809 |
for(byte[] line : lines){ |
| 810 |
|
| 811 |
if(line.length>0 && line[0]=='>'){ |
| 812 |
readsProcessed++; |
| 813 |
if(maxReads>0 && readsProcessed>maxReads){break;} |
| 814 |
|
| 815 |
if(Tools.startsWith(line, invalidTitle)){ |
| 816 |
valid=false; |
| 817 |
invalidReads++; |
| 818 |
invalidLines++; |
| 819 |
if(deleteInvalid){break;} |
| 820 |
}else{ |
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
assert(Tools.startsWith(line, title)) : "\nline: '"+new String(line)+"'\ntitle: '"+new String(title)+"'\n"; |
| 831 |
valid=true; |
| 832 |
validReads++; |
| 833 |
validLines++; |
| 834 |
} |
| 835 |
}else if(gffIn){ |
| 836 |
basesProcessed+=line.length; |
| 837 |
valid=!Tools.startsWith(line, invalidGffTitle); |
| 838 |
if(valid){ |
| 839 |
validBases+=line.length; |
| 840 |
validLines++; |
| 841 |
}else{ |
| 842 |
invalidBases+=line.length; |
| 843 |
invalidLines++; |
| 844 |
} |
| 845 |
}else{ |
| 846 |
basesProcessed+=line.length; |
| 847 |
if(valid){ |
| 848 |
validBases+=line.length; |
| 849 |
validLines++; |
| 850 |
}else{ |
| 851 |
invalidBases+=line.length; |
| 852 |
invalidLines++; |
| 853 |
} |
| 854 |
} |
| 855 |
|
| 856 |
if(valid || keepAll){ |
| 857 |
if(bsw!=null){bsw.println(line);} |
| 858 |
}else{ |
| 859 |
if(bswInvalid!=null){bswInvalid.println(line);} |
| 860 |
} |
| 861 |
} |
| 862 |
return valid; |
| 863 |
} |
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
private LinkedHashSet<String> in1=new LinkedHashSet<String>(); |
| 871 |
private String out1=null; |
| 872 |
private String outInvalid=null; |
| 873 |
private String badHeaders=null; |
| 874 |
|
| 875 |
private String taxTreeFile=null; |
| 876 |
private String giTableFile=null; |
| 877 |
private String accessionFile=null; |
| 878 |
private String patternFile=null; |
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
private long maxReads=-1; |
| 883 |
|
| 884 |
private long validReads=0; |
| 885 |
private long validBases=0; |
| 886 |
private long invalidReads=0; |
| 887 |
private long invalidBases=0; |
| 888 |
private long taxaCounted=0; |
| 889 |
|
| 890 |
private long linesIn=0; |
| 891 |
private long validLines=0; |
| 892 |
private long invalidLines=0; |
| 893 |
|
| 894 |
private long maxStoredBytes=10000000; |
| 895 |
|
| 896 |
private long readsProcessed=0, basesProcessed=0; |
| 897 |
|
| 898 |
private boolean prefix=true; |
| 899 |
private boolean countTable=true; |
| 900 |
private boolean keepAll=true; |
| 901 |
private boolean shrinkNames=false; |
| 902 |
private boolean warnBadHeaders=true; |
| 903 |
private boolean useServer=false; |
| 904 |
|
| 905 |
private long maxInvalidHeaders=-1; |
| 906 |
|
| 907 |
private boolean deleteInvalid=false; |
| 908 |
|
| 909 |
private int mode; |
| 910 |
private static final int ACCESSION_MODE=0, GI_MODE=1, HEADER_MODE=2, UNITE_MODE=3; |
| 911 |
|
| 912 |
private boolean gffIn=false; |
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
private final ArrayList<FileFormat> ffin1; |
| 917 |
private final FileFormat ffout1; |
| 918 |
private final FileFormat ffoutInvalid; |
| 919 |
private final TaxTree tree; |
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
private PrintStream outstream=System.err; |
| 924 |
public static boolean verbose=false; |
| 925 |
public boolean errorState=false; |
| 926 |
private boolean overwrite=true; |
| 927 |
private boolean append=false; |
| 928 |
|
| 929 |
private static byte[] title=">tid|".getBytes(); |
| 930 |
private static byte[] invalidTitle=">tid|-1".getBytes(); |
| 931 |
private static byte[] invalidGffTitle="tid|-1".getBytes(); |
| 932 |
|
| 933 |
} |