Debian Codegraph @

current/tax/AccessionToTaxid.java

raw symbols SCIP
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.Collections;
8 import java.util.HashMap;
9 import java.util.concurrent.atomic.AtomicLongArray;
10
11 import fileIO.ByteFile;
12 import fileIO.ByteFile1;
13 import fileIO.ByteFile2;
14 import fileIO.FileFormat;
15 import fileIO.ReadWrite;
16 import json.JsonObject;
17 import json.JsonParser;
18 import kmer.HashBuffer;
19 import kmer.KmerTableSet;
20 import server.ServerTools;
21 import shared.Parse;
22 import shared.Parser;
23 import shared.PreParser;
24 import shared.Shared;
25 import shared.Timer;
26 import shared.Tools;
27 import stream.ConcurrentGenericReadInputStream;
28 import stream.FastaReadInputStream;
29 import structures.LongLongHashMap;
30 import structures.StringNum;
31
32 /**
33 * New version loads with multiple threads per input file.
34 * @author Brian Bushnell
35 * @date December 16, 2016
36 *
37 */
38 public class AccessionToTaxid {
39
40 public static void load(String files){
41 final boolean oldBf2=ByteFile.FORCE_MODE_BF2;
42 final boolean oldBf1=ByteFile.FORCE_MODE_BF1;
43 final boolean oldUnpigz=ReadWrite.USE_UNPIGZ;
44 final boolean oldGunzip=ReadWrite.USE_UNPIGZ;
45
46 main(new String[] {"in="+files, "unpigz="+ReadWrite.USE_UNPIGZ, "gunzip="+ReadWrite.USE_GUNZIP});
47
48 ByteFile.FORCE_MODE_BF2=oldBf2;
49 ByteFile.FORCE_MODE_BF1=oldBf1;
50 ReadWrite.USE_UNPIGZ=oldUnpigz;
51 ReadWrite.USE_UNPIGZ=oldGunzip;
52 }
53
54 public static void main(String[] args){
55 Timer t=new Timer();
56 AccessionToTaxid x=new AccessionToTaxid(args);
57 x.process(t);
58
59 //Close the print stream if it was redirected
60 Shared.closeStream(x.outstream);
61 }
62
63 public AccessionToTaxid(String[] args){
64
65 {//Preparse block for help, config files, and outstream
66 PreParser pp=new PreParser(args, getClass(), false);
67 args=pp.args;
68 outstream=pp.outstream;
69 }
70
71 ReadWrite.USE_UNPIGZ=true;
72
73 Parser parser=new Parser();
74 for(int i=0; i<args.length; i++){
75 String arg=args[i];
76 String[] split=arg.split("=");
77 String a=split[0].toLowerCase();
78 String b=split.length>1 ? split[1] : null;
79
80 if(a.equals("verbose")){
81 verbose=Parse.parseBoolean(b);
82 ByteFile1.verbose=verbose;
83 ByteFile2.verbose=verbose;
84 stream.FastaReadInputStream.verbose=verbose;
85 ConcurrentGenericReadInputStream.verbose=verbose;
86 stream.FastqReadInputStream.verbose=verbose;
87 ReadWrite.verbose=verbose;
88 }else if(a.equals("stripunderscore")){
89 // STRIP_UNDERSCORE=Parse.parseBoolean(b);
90 assert(false) : "stripunderscore is disabled.";
91 }else if(a.equals("usetables")){
92 // USE_TABLES=Parse.parseBoolean(b);
93 }else if(a.equals("usetables")){
94 // USE_TABLES=Parse.parseBoolean(b);
95 }else if(a.equals("skipparse")){
96 skipParse=Parse.parseBoolean(b);
97 }else if(a.equals("skiphash")){
98 skipHash=Parse.parseBoolean(b);
99 }else if(a.equals("prealloc")){
100 if(b==null || Character.isLetter(b.charAt(0))){
101 if(Parse.parseBoolean(b)){
102 prealloc=0.78f;
103 }else{
104 prealloc=0;
105 }
106 }else{
107 prealloc=Float.parseFloat(b);
108 }
109 }else if(a.equals("maxpigzprocesses")){
110 maxPigzProcesses=Integer.parseInt(b);
111 }else if(a.equals("in")){
112 assert(b!=null) : "Bad parameter: "+arg;
113 String[] temp=b.split(",");
114 for(String s : temp){in.add(s);}
115 }else if(parser.parse(arg, a, b)){
116 //do nothing
117 }else if(b==null){
118 if(new File(arg).exists()){
119 in.add(arg);
120 }
121 }else{
122 outstream.println("Unknown parameter "+args[i]);
123 assert(false) : "Unknown parameter "+args[i];
124 // throw new RuntimeException("Unknown parameter "+args[i]);
125 }
126 }
127
128 {//Process parser fields
129 overwrite=parser.overwrite;
130
131 // out=parser.out1;
132 }
133
134 assert(FastaReadInputStream.settingsOK());
135
136 if(in==null || in.size()==0){throw new RuntimeException("Error - at least one input file is required.");}
137
138 if(ReadWrite.USE_UNPIGZ && !ByteFile.FORCE_MODE_BF2){
139 ByteFile.FORCE_MODE_BF2=false;
140 ByteFile.FORCE_MODE_BF1=true;
141 }
142
143 // if(out!=null && out.equalsIgnoreCase("null")){out=null;}
144
145 // if(!Tools.testOutputFiles(overwrite, false, false, out)){
146 // outstream.println((out==null)+", "+out);
147 // throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out+"\n");
148 // }
149
150 {//Reorder by size, ascending
151 ArrayList<StringNum> list=new ArrayList<StringNum>();
152 for(String s : in){
153 list.add(new StringNum(s, new File(s).length()));
154 }
155 Collections.sort(list);
156 in.clear();
157 for(StringNum sn : list){
158 in.add(sn.s);
159 }
160 }
161
162 // ffout=FileFormat.testOutput(out, FileFormat.TXT, null, true, overwrite, false, false);
163 ffin=new FileFormat[in.size()];
164
165 /* Note */
166 /* Java 1.7 works fine here (54 seconds skipping parsing). */
167 /* Java 1.8 has immense speed-downs if pigz is used (80-100s normally, >1000s with unpigz). */
168 /* Java 1.8_144 is unpredictable and incredibly slow (80-900s normally, 500-1800 with unpigz) */
169
170 int processes=0;
171 for(int i=0; i<in.size(); i++){
172 String s=in.get(i);
173 if(!new File(s).exists()){
174 if(s.startsWith("shrunk.") && new File(s.substring(7)).exists()){
175 s=s.substring(7);
176 }
177 }
178 FileFormat ff=FileFormat.testInput(s, FileFormat.TXT, null, true, false);
179 if(ff.gzip() && processes>maxPigzProcesses){
180 processes++;
181 // if(processes>maxPigzProcesses){
182 ff=FileFormat.testInput(s, FileFormat.TXT, null, false, false);
183 // }
184 }
185 ffin[i]=ff;
186 }
187 }
188
189 @SuppressWarnings("unchecked")
190 void process(Timer t){
191
192 // if(USE_MAPS){
193 assert(maps==null);
194 maps=new HashMap[128];
195 for(int i=0; i<maps.length; i++){
196 maps[i]=new HashMap<String, Integer>();
197 }
198 // }
199
200 assert(tables==null);
201 if(USE_TABLES){
202 tables=new KmerTableSet(new String[] {"ways=31",("prealloc="+(prealloc>0 ? prealloc : "f"))}, 12);
203 tables.allocateTables();
204 }
205
206 imgCache=new LongLongHashMap(2000);
207
208 if(ffin.length>4){//Addresses a multithreaded read bug in Java
209 // FileFormat[] ffa1=Arrays.copyOf(ffin, 2);
210 // FileFormat[] ffa2=Arrays.copyOfRange(ffin, 2, ffin.length);
211 // spawnThreads(ffa1);
212 // spawnThreads(ffa2);
213
214 FileFormat[] ffa1=Arrays.copyOf(ffin, 2);
215 FileFormat[] ffa2=Arrays.copyOfRange(ffin, 2, ffin.length);
216 spawnThreads(ffa1, 2);
217 spawnThreads(ffa2, 200);
218 }else{
219 spawnThreads(ffin, 200);
220 }
221
222 //Do anything necessary after processing
223 System.gc();
224
225 t.stop();
226 outstream.println(Tools.timeLinesBytesProcessed(t, linesProcessed, bytesProcessed, 8));
227
228 outstream.println();
229 outstream.println("Valid Lines: \t"+linesValid);
230 outstream.println("Invalid Lines: \t"+(linesProcessed-linesValid));
231
232 if(lengthCounts!=null){
233 outstream.println();
234 outstream.println("Length counts:");
235
236 for(int i=0; i<lengthCounts.length(); i++){
237 long count=lengthCounts.get(i);
238 if(count>0){outstream.println(i+"\t"+count);}
239 }
240 }
241
242 if(symbolCounts!=null){
243 outstream.println();
244 outstream.println("Symbols:");
245
246 String comma="";
247 for(int i=0; i<symbolCounts.length(); i++){
248 long count=symbolCounts.get(i);
249 if(count>0){
250 outstream.print(comma+i);
251 comma=",";
252 }
253 }
254 }
255
256 if(counts_underscore!=null){
257 outstream.println();
258 outstream.println("Length_underscore counts:");
259
260 for(int i=0; i<counts_underscore.length(); i++){
261 long count=counts_underscore.get(i);
262 if(count>0){outstream.println(i+"\t"+count);}
263 }
264 }
265
266 if(counts_underscore2!=null){
267 outstream.println();
268 outstream.println("Length_underscore2 counts:");
269
270 for(int i=0; i<counts_underscore2.length(); i++){
271 long count=counts_underscore2.get(i);
272 if(count>0){outstream.println(i+"\t"+count);}
273 }
274 }
275 outstream.println();
276 Shared.printMemory();
277
278 if(errorState){
279 throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt.");
280 }
281
282 LOADED=true;
283 }
284
285 /** Spawn process threads */
286 private void spawnThreads(FileFormat[] ffa, int threadLimit){
287
288 //Do anything necessary prior to processing
289 Tools.reverseInPlace(ffa, 0, ffa.length);
290
291 //Fill a list with ProcessThreads
292 ArrayList<ByteFile> albf=new ArrayList<ByteFile>(ffa.length);
293 for(FileFormat ff : ffa){
294 if(ff!=null){
295 System.err.println("Loading "+ff.name());
296 ByteFile bf=ByteFile.makeByteFile(ff, 1);
297 albf.add(bf);
298 }
299 }
300 final int threads=Tools.min(threadLimit, Tools.max(albf.size(), Shared.threads()));
301 ArrayList<HashThread> alht=new ArrayList<HashThread>(threads);
302
303 for(int i=0; i<threads; i++){
304 ByteFile bf=albf.get(i%albf.size());
305 alht.add(new HashThread(bf));
306 }
307
308 //Start the threads
309 for(HashThread pt : alht){
310 pt.start();
311 }
312
313 //Wait for completion of all threads
314 boolean success=true;
315 for(HashThread pt : alht){
316
317 //Wait until this thread has terminated
318 while(pt.getState()!=Thread.State.TERMINATED){
319 try {
320 //Attempt a join operation
321 pt.join();
322 } catch (InterruptedException e) {
323 //Potentially handle this, if it is expected to occur
324 e.printStackTrace();
325 }
326 }
327
328 linesProcessed+=pt.linesProcessedT;
329 linesValid+=pt.linesValidT;
330 bytesProcessed+=pt.bytesProcessedT;
331
332 accumulate(lengthCounts, pt.lengthCountsT);
333 accumulate(symbolCounts, pt.symbolCountsT);
334 accumulate(counts_underscore, pt.counts_underscoreT);
335 accumulate(counts_underscore2, pt.counts_underscore2T);
336
337 success&=pt.success;
338 }
339
340 //Close the byte files
341 for(ByteFile bf : albf){
342 errorState=bf.close()|errorState;
343 }
344
345 //Track whether any threads failed
346 if(!success){errorState=true;}
347 }
348
349 private static void accumulate(AtomicLongArray a, long[] b){
350 if(a==null || b==null){return;}
351 for(int i=0; i<b.length; i++){
352 a.getAndAdd(i, b[i]);
353 }
354 }
355
356 /*--------------------------------------------------------------*/
357
358 public static int get(String accession){
359 if(accession==null){return -1;}
360 // if(STRIP_UNDERSCORE){
361 // accession=accession.replaceAll("[_-]", "");
362 // }
363
364 int len=accession.length();
365 for(int i=0; i<len; i++){
366 char c=accession.charAt(i);
367 if(c=='.' || c==':' || c==','){
368 len=i; break;
369 }
370 }
371
372 if(USE_TABLES){
373 if(AnalyzeAccession.codeMap!=null){
374 // if(dot>AnalyzeAccession.longestPattern){return false;}
375 final long number=AnalyzeAccession.digitize(accession);
376 if(number>=0){
377 int value=tables.getCount(number);
378 return value<0 ? -1 : value;
379 }
380 }else if(len<=12){
381 long number=hash(accession);
382
383 int value=tables.getCount(number);
384 return value<1 ? -1 : value;
385 }
386 }
387
388 if(len<accession.length()){accession=accession.substring(0, len);}
389 if(accession.length()<1){return -1;}
390 int way=accession.charAt(0);
391 Integer value=maps[way].get(accession);
392 return value==null ? -1 : value.intValue();
393 }
394
395 public static boolean isValidAccession(String s){
396 if(s==null || s.length()<4){return false;}
397 for(int i=0; i<s.length(); i++){
398 char c=s.charAt(i);
399 if((c>='0' && c<='9') || (c>='A' && c<='Z') /*|| (c>='a' && c<='z')*/
400 || c=='.' || c=='_' || c=='-' || c==':' || c==','){
401 //do nothing
402 }else{
403 return false;
404 }
405 }
406 return true;
407 }
408
409 static long hash(String accession){
410 long number=0;
411 for(int i=0, max=accession.length(); i<max; i++){
412 long c=accession.charAt(i);
413 if(c=='.' || c==':' || c==','){break;}
414 if(c>='0' && c<='9'){c=c-'0';}
415 else if(c>='A' && c<='Z'){c=c+offset;}
416 else if(c=='_' || c=='-'){c=10;}//Collision, but should be OK
417 else if(c>='a' && c<='z'){c=c+offsetLower;}
418 else{
419 assert(false) : accession;
420 }
421 number=(number*37)+c;
422 }
423 return number;
424 }
425
426 static long hash(final byte[] line, final int limit){
427 long number=0;
428 for(int i=0; i<limit; i++){
429 long c=line[i];
430 if(c=='.' || c==':' || c==','){break;}
431 if(c>='0' && c<='9'){c=c-'0';}
432 else if(c>='A' && c<='Z'){c=c+offset;}
433 else if(c=='_' || c=='-'){c=10;}//Collision, but should be OK
434 else if(c>='a' && c<='z'){c=c+offsetLower;}
435 else{
436 assert(false) : new String(line);
437 }
438 number=(number*37)+c;
439 }
440 return number;
441 }
442
443 public static int parseLineToTaxid(final byte[] line, final byte delimiter){
444 int a=0, b=0;
445
446 final int ncbi;
447
448 while(b<line.length && line[b]!=delimiter){b++;}
449 assert(b>a) : "Missing field 0: "+new String(line);
450 b++;
451 a=b;
452
453 while(b<line.length && line[b]!=delimiter){b++;}
454 // assert(b>a) : "Missing field 1: "+new String(line);
455 assert(b>=a) : "Missing field 1: "+new String(line)+"\n"+a+", "+b;
456 //accession2=new String(line, a, b-a);
457 b++;
458 a=b;
459
460 while(b<line.length && line[b]!=delimiter){b++;}
461 assert(b>a) : "Missing field 2: "+new String(line);
462 ncbi=Parse.parseInt(line, a, b);
463 b++;
464 a=b;
465
466 return ncbi;
467 }
468
469 public static int parseLineToTaxid_2col(final byte[] line, final byte delimiter){
470 int a=0, b=0;
471
472 final int ncbi;
473
474 while(b<line.length && line[b]!=delimiter){b++;}
475 assert(b>a) : "Missing field 0: "+new String(line);
476 b++;
477 a=b;
478
479 while(b<line.length && line[b]!=delimiter){b++;}
480 assert(b>a) : "Missing field 1: "+new String(line);
481 ncbi=Parse.parseInt(line, a, b);
482 b++;
483 a=b;
484
485 return ncbi;
486 }
487
488 /*--------------------------------------------------------------*/
489
490 public static class HashThread extends Thread {
491
492 @SuppressWarnings("unchecked")
493 public HashThread(ByteFile bf_){
494 // if(USE_MAPS){
495 mapsT=new HashMap[128];
496 for(int i=0; i<mapsT.length; i++){
497 mapsT[i]=new HashMap<String, Integer>();
498 }
499 // }
500 if(USE_TABLES){
501 table=new HashBuffer(tables.tables(), 1000, 31, true, true);
502 }
503 bf=bf_;
504 }
505
506 ArrayList<byte[]> fetch(int limit){
507 ArrayList<byte[]> list=new ArrayList<byte[]>(limit);
508 synchronized(bf){
509 byte[] line=bf.nextLine();
510 // while(line!=null && Tools.startsWith(line, "accession")){line=bf.nextLine();}
511 if(line==null){return null;}
512 for(int i=0; line!=null;){
513 list.add(line);
514 i++;
515 if(i>=limit){break;}
516 line=bf.nextLine();
517 }
518 }
519 return list.size()>0 ? list : null;
520 }
521
522 @Override
523 public void run(){
524 // System.err.println("Processing "+bf.name());
525 final int fetchSize=1000;
526 for(ArrayList<byte[]> list=fetch(fetchSize); list!=null; list=fetch(fetchSize)){
527 for(byte[] line : list){
528 if(line.length>0){
529 linesProcessedT++;
530 bytesProcessedT+=line.length;
531
532 // final boolean valid=(!Tools.startsWith(line, "accession\t")) & !skipParse;
533 final boolean valid=(!Tools.startsWith(line, "accession")) & !skipParse;
534 // assert(valid); //Not true if concatenated
535
536 // if(Tools.startsWith(line, "NZ_LM994619")){
537 // boolean b=parseLine2(line, (byte)'\t');
538 // assert(false) : b+", "+new String(line);
539 // }
540
541 if(valid){
542 boolean b=parseLine2(line, (byte)'\t');
543 if(b){linesValidT++;}
544 }
545 }
546 }
547 }
548
549 // if(USE_MAPS){
550 for(int i=0; i<mapsT.length; i++){
551 if(mapsT[i].size()>0){
552 synchronized(maps[i]){
553 maps[i].putAll(mapsT[i]);
554 }
555 }
556 mapsT[i]=null;
557 }
558 // }
559 if(USE_TABLES){
560 long temp=table.flush();
561 }
562
563 success=true;
564 }
565
566 // public boolean parseLineNumeric(final byte[] line, final byte delimiter){
567 // int a=0, b=0;
568 //
569 // long accession=0;
570 // final int ncbi, gi;
571 //
572 // while(b<line.length && line[b]!=delimiter){b++;}
573 // assert(b>a) : "Missing field 0: "+new String(line);
574 // for(int i=a; i<b; i++){
575 // long c=line[i];
576 // if(c=='.'){break;}
577 // if(c<='9'){c=c-'0';}
578 // else{c=c-'A'+10;}
579 // accession=(accession*36)+c;
580 // }
581 // b++;
582 // a=b;
583 //
584 // while(b<line.length && line[b]!=delimiter){b++;}
585 // assert(b>a) : "Missing field 1: "+new String(line);
586 // //accession2=new String(line, a, b-a);
587 // b++;
588 // a=b;
589 //
590 // while(b<line.length && line[b]!=delimiter){b++;}
591 // assert(b>a) : "Missing field 2: "+new String(line);
592 // ncbi=Parse.parseInt(line, a, b);
593 // b++;
594 // a=b;
595 //
596 //// while(b<line.length && line[b]!=delimiter){b++;}
597 //// assert(b>a) : "Missing field 3: "+new String(line);
598 ////// gi=Parse.parseInt(line, a, b);
599 //// b++;
600 //// a=b;
601 //
602 // if(ncbi<1){return false;}
603 //
604 // if(tree!=null){
605 // if(ncbi>=tree.nodes.length){return false;}
606 // TaxNode tn=tree.getNode(ncbi);
607 // if(tn==null || tn.level==TaxTree.NO_RANK || tn.level==TaxTree.LIFE || tn.level==TaxTree.DOMAIN){return false;}
608 // if(tn.pid>=tree.nodes.length){return false;}
609 // tn=tree.getNode(tn.pid);
610 // if(tn==null || tn.level==TaxTree.NO_RANK || tn.level==TaxTree.LIFE){return false;}
611 // }
612 // assert(accession>=0) : new String(line);
613 // table.set(accession, ncbi);
614 // return true;
615 // }
616
617 //This code is no longer used and can be safely deleted.
618 @Deprecated
619 public boolean parseLine(final byte[] line, final byte delimiter){
620 int a=0, b=0;
621
622 String accession;
623 final int ncbi, gi;
624
625 while(b<line.length && line[b]!=delimiter){b++;}
626 assert(b>a) : "Missing field 0: "+new String(line);
627 accession=new String(line, a, b-a);
628 final int dot=accession.indexOf('.');//and :, but this is deprecated.
629 if(dot>=0){//Should never happen
630 // System.err.println(accession);
631 // assert(dot==accession.length()-2) : accession;
632 accession=accession.substring(0, dot);
633 }
634 // if(STRIP_UNDERSCORE){
635 // accession=accession.replaceAll("[_-]", "");
636 // }
637 if(lengthCountsT!=null){lengthCountsT[b-a]++;}
638 if(symbolCountsT!=null){
639 for(int i=a; i<b; i++){symbolCountsT[line[i]]++;}
640 }
641 final int underscore=accession.indexOf('_');
642 if(underscore>=0){
643 if(counts_underscoreT!=null){counts_underscoreT[b-a]++;}
644 if(counts_underscore2T!=null && underscore==2){counts_underscore2T[b-a]++;}
645 }
646 b++;
647 a=b;
648
649 while(b<line.length && line[b]!=delimiter){b++;}
650 // assert(b>a) : "Missing field 1: "+new String(line);
651 assert(b>=a) : "Missing field 1: "+new String(line)+"\n"+a+", "+b;
652 //accession2=new String(line, a, b-a);
653 b++;
654 a=b;
655
656 while(b<line.length && line[b]!=delimiter){b++;}
657 assert(b>a) : "Missing field 2: "+new String(line);
658 ncbi=Parse.parseInt(line, a, b);
659 b++;
660 a=b;
661
662 // while(b<line.length && line[b]!=delimiter){b++;}
663 // assert(b>a) : "Missing field 3: "+new String(line);
664 //// gi=Parse.parseInt(line, a, b);
665 // b++;
666 // a=b;
667
668 if(ncbi<1){return false;}
669
670 if(tree!=null){
671 if(ncbi>=tree.nodes.length){return false;}
672 TaxNode tn=tree.getNode(ncbi);
673 if(tn==null || tn.levelExtended==TaxTree.NO_RANK_E || tn.levelExtended==TaxTree.LIFE_E || tn.levelExtended==TaxTree.DOMAIN_E){return false;}
674 if(tn.pid>=tree.nodes.length){return false;}
675 tn=tree.getNode(tn.pid);
676 if(tn==null || tn.levelExtended==TaxTree.NO_RANK_E || tn.levelExtended==TaxTree.LIFE_E){return false;}
677 }
678
679 if(accession.length()<13 && USE_TABLES){
680 long number=hash(accession);
681 assert(number>=0) : new String(line);
682 table.set(number, ncbi);
683 return true;
684 }
685
686 int way=accession.charAt(0);
687 mapsT[way].put(accession, ncbi);
688 // Integer old=mapsT[way].put(accession, ncbi);
689 // assert(old==null || old==ncbi) : "'"+accession+"': "+old+" -> "+ncbi;
690 // System.err.println("'"+accession+"': "+old+" -> "+ncbi);
691 // assert(dot==-1) : "'"+accession+"': "+old+" -> "+ncbi;
692 return true;
693 }
694
695 public boolean parseLine2(final byte[] line, final byte delimiter){
696 int a=0, b=0;
697
698 final int ncbi, gi;
699
700 while(b<line.length && line[b]!=delimiter
701 && line[b]!='.' && line[b]!=':' && line[b]!=','){b++;}//parse unique part of accession
702 final int dot=b;
703 assert(b>a) : "Missing field 0: "+new String(line);
704 while(b<line.length && line[b]!=delimiter){b++;}//skip the rest of the accession
705
706 //System.err.println("Line: "+new String(line)+"\n"+Arrays.toString(line));
707 //System.err.println("A: dot="+dot+", a="+a+", b="+b);
708
709 {//Optional block
710 if(lengthCountsT!=null){lengthCountsT[dot]++;}
711 if(symbolCountsT!=null){
712 for(int i=0; i<dot; i++){symbolCountsT[line[i]]++;}
713 }
714 if(counts_underscoreT!=null || counts_underscore2T!=null){
715 int underscore=-1;
716 for(int i=0; i<dot; i++){
717 if(line[i]=='_'){
718 underscore=i;
719 break;
720 }
721 }
722 if(underscore>=0){
723 if(counts_underscoreT!=null){counts_underscoreT[dot]++;}
724 if(counts_underscore2T!=null && underscore==2){counts_underscore2T[dot]++;}
725 }
726 }
727 }
728 b++;
729 a=b;
730
731 //System.err.println("B: a="+a+", b="+b);
732
733 while(b<line.length && line[b]!=delimiter){b++;}
734 // assert(b>a) : "Missing field 1: "+new String(line);
735 assert(b>=a) : "Missing field 1: "+new String(line)+"\n"+a+", "+b;
736 //accession2=new String(line, a, b-a);
737 b++;
738 a=b;
739
740 //System.err.println("C: a="+a+", b="+b);
741
742 while(b<line.length && line[b]!=delimiter){b++;}
743 assert(b>a) : "Missing field 2: "+new String(line);
744 ncbi=Parse.parseInt(line, a, b);
745 //System.err.println("D: a="+a+", b="+b+", ncbi="+ncbi+", '"+(new String(line, a, b-a))+"'");
746 b++;
747 a=b;
748
749 // while(b<line.length && line[b]!=delimiter){b++;}
750 // assert(b>a) : "Missing field 3: "+new String(line);
751 //// gi=Parse.parseInt(line, a, b);
752 // b++;
753 // a=b;
754
755 if(ncbi<1){return false;}
756 //System.err.println("E: a="+a+", b="+b);
757 if(skipHash){return false;}//123
758 //System.err.println("F: a="+a+", b="+b);
759
760 if(tree!=null){
761 if(ncbi>=tree.nodes.length){return false;}
762 //System.err.println("G");
763 TaxNode tn=tree.getNode(ncbi);
764 if(tn==null || /*tn.levelExtended==TaxTree.NO_RANK_E ||*/ tn.levelExtended==TaxTree.LIFE_E || tn.levelExtended==TaxTree.DOMAIN_E){return false;}
765 //System.err.println("H: "+tn);
766 if(tn.pid>=tree.nodes.length){return false;}
767 //System.err.println("I: "+tn);
768 // TaxNode parent=tree.getNode(tn.pid);
769 // System.err.println("J: "+tn);
770 // if(tn==null || tn.levelExtended==TaxTree.NO_RANK_E || tn.levelExtended==TaxTree.LIFE_E){return false;}
771 // System.err.println("K");
772 }
773
774 if(distributed){
775 String accession=new String(line, 0, dot);//slow
776 assert(accession.equals(accession.toUpperCase()));//TODO: Disable. (slow)
777 if(accession.hashCode()%serverCount!=serverNum){return false;}
778 }
779
780 if(USE_TABLES){
781 if(AnalyzeAccession.codeMap!=null){
782 // if(dot>AnalyzeAccession.longestPattern){return false;}
783 final long number=AnalyzeAccession.digitize(line);
784 if(number>=0){
785 table.set(number, ncbi);
786 return true;
787 }
788 assert(number==-1) : number+", "+new String(line);
789 }else{
790 if(dot<13){
791 // long number=hash(accession);
792 final long number=hash(line, dot);
793 assert(number>=0) : new String(line);
794 table.set(number, ncbi);
795 return true;
796 }
797 }
798 }
799
800 String accession=new String(line, 0, dot);
801 int way=accession.charAt(0);
802 mapsT[way].put(accession, ncbi);
803 // Integer old=mapsT[way].put(accession, ncbi);
804 // assert(old==null || old==ncbi) : "'"+accession+"': "+old+" -> "+ncbi;
805 // System.err.println("'"+accession+"': "+old+" -> "+ncbi);
806 // assert(dot==-1) : "'"+accession+"': "+old+" -> "+ncbi;
807 return true;
808 }
809
810 private long linesProcessedT=0;
811 private long linesValidT=0;
812 private long bytesProcessedT=0;
813
814 final ByteFile bf;
815 HashMap<String, Integer>[] mapsT;
816 HashBuffer table;
817 boolean success=false;
818
819 private long[] lengthCountsT=null;//new AtomicLongArray(20);
820 private long[] symbolCountsT=null;//new AtomicLongArray(255);
821 private long[] counts_underscoreT=null;//new AtomicLongArray(20);
822 private long[] counts_underscore2T=null;//new AtomicLongArray(20);
823 }
824
825 /*--------------------------------------------------------------*/
826
827 public static int getImg(long imgID) {
828 long ncbi=-1;
829 synchronized(imgCache) {
830 long time=System.currentTimeMillis();
831 if(time-lastCacheClear>cacheClearInterval) {
832 imgCache.clear();
833 lastCacheClear=time;
834 }
835 ncbi=imgCache.get(imgID);
836 }
837 if(ncbi>-1) {return (int)ncbi;}
838 ncbi=getImgFromServer(imgID);
839 if(ncbi<0) {return -1;}
840 synchronized(imgCache) {imgCache.put(imgID, ncbi);}
841 return (int)ncbi;
842 }
843
844 public static int getImgFromServer(long imgID) {
845 try {
846 byte[] message=null;
847 String path="https://img.jgi.doe.gov/cgi-bin/genomesMetadata.cgi?ids="+imgID;
848 StringNum sn=ServerTools.sendAndReceive(message, path);
849 if(sn==null) {return -1;}
850 String response=sn.s;
851 if(response==null) {return 0-(int)sn.n;}
852 JsonObject all=JsonParser.parseJsonObjectStatic(response);
853 JsonObject body=all.getJson(""+imgID);
854 long ncbi=body.getLong("ncbi_taxon_id");
855 return (int)ncbi;
856 } catch (Exception e) {
857 // TODO Auto-generated catch block
858 e.printStackTrace();
859 }
860 return -1;
861 }
862
863 /*--------------------------------------------------------------*/
864
865 private ArrayList<String> in=new ArrayList<String>();
866 // private String out=null;
867
868 static int maxPigzProcesses=12;
869
870 /*--------------------------------------------------------------*/
871
872 private long linesProcessed=0;
873 private long linesValid=0;
874 private long bytesProcessed=0;
875
876 private AtomicLongArray lengthCounts=null;//new AtomicLongArray(20);
877 private AtomicLongArray symbolCounts=null;//new AtomicLongArray(255);
878 private AtomicLongArray counts_underscore=null;//new AtomicLongArray(20);
879 private AtomicLongArray counts_underscore2=null;//new AtomicLongArray(20);
880
881 /*--------------------------------------------------------------*/
882
883 private final FileFormat ffin[];
884 // private final FileFormat ffout;
885
886
887 /*--------------------------------------------------------------*/
888
889 public static boolean LOADED(){return LOADED;}
890
891 private static boolean LOADED=false;
892 private static HashMap<String, Integer>[] maps=null;
893 private static KmerTableSet tables;
894 private static LongLongHashMap imgCache;
895 private static final long cacheClearInterval=7L*24L*3600L*1000L;//millis
896 private static long lastCacheClear=0;
897 public static TaxTree tree=null;
898 // public static final boolean USE_MAPS=true;
899 public static final boolean USE_TABLES=true;
900 // public static boolean STRIP_UNDERSCORE=false;
901 public static boolean skipParse=false;
902 public static boolean skipHash=false;
903 public static float prealloc=0;
904 private static final long offset=-'A'+11;
905 private static final long offsetLower=-'a'+11;
906
907 public static int serverNum=0;
908 public static int serverCount=1;
909 public static boolean distributed=false;
910
911 /*--------------------------------------------------------------*/
912
913 private PrintStream outstream=System.err;
914 public static boolean verbose=false;
915 public boolean errorState=false;
916 private boolean overwrite=true;
917
918 }