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