jpayne@68
|
1 package sketch;
|
jpayne@68
|
2
|
jpayne@68
|
3 import java.io.PrintStream;
|
jpayne@68
|
4 import java.util.HashMap;
|
jpayne@68
|
5
|
jpayne@68
|
6 import fileIO.FileFormat;
|
jpayne@68
|
7 import fileIO.ReadWrite;
|
jpayne@68
|
8 import shared.Shared;
|
jpayne@68
|
9 import stream.ConcurrentReadInputStream;
|
jpayne@68
|
10 import stream.FASTQ;
|
jpayne@68
|
11 import stream.Read;
|
jpayne@68
|
12 import structures.ListNum;
|
jpayne@68
|
13 import tax.GiToTaxid;
|
jpayne@68
|
14 import tax.TaxTree;
|
jpayne@68
|
15
|
jpayne@68
|
16 public class SSUMap {
|
jpayne@68
|
17
|
jpayne@68
|
18 public static synchronized void load(PrintStream outstream){
|
jpayne@68
|
19 if(r16SFile!=null && r16SMap==null){
|
jpayne@68
|
20 r16SMap=load(r16SFile, TaxTree.default16SFile(), outstream);
|
jpayne@68
|
21 }
|
jpayne@68
|
22 if(r18SFile!=null && r18SMap==null){
|
jpayne@68
|
23 r18SMap=load(r18SFile, TaxTree.default18SFile(), outstream);
|
jpayne@68
|
24 }
|
jpayne@68
|
25 }
|
jpayne@68
|
26
|
jpayne@68
|
27 private static synchronized HashMap<Integer, byte[]> load(String ssuFile, String defaultFile, PrintStream outstream){
|
jpayne@68
|
28 HashMap<Integer, byte[]> map=null;
|
jpayne@68
|
29 if(ssuFile!=null){
|
jpayne@68
|
30 final boolean oldAminoIn=Shared.AMINO_IN;
|
jpayne@68
|
31 final boolean oldInterleaved=FASTQ.FORCE_INTERLEAVED;
|
jpayne@68
|
32 Shared.AMINO_IN=false;//SSUs are nucleotide, which can cause a crash, esp. with IUPAC symbols
|
jpayne@68
|
33 FASTQ.FORCE_INTERLEAVED=false;
|
jpayne@68
|
34
|
jpayne@68
|
35 String fname=ssuFile;
|
jpayne@68
|
36 if("auto".equalsIgnoreCase(fname)){fname=defaultFile;}
|
jpayne@68
|
37 final FileFormat ffssu=FileFormat.testInput(fname, FileFormat.FA, null, true, false);
|
jpayne@68
|
38 map=loadSSU(ffssu, outstream);
|
jpayne@68
|
39
|
jpayne@68
|
40 Shared.AMINO_IN=oldAminoIn;
|
jpayne@68
|
41 FASTQ.FORCE_INTERLEAVED=oldInterleaved;
|
jpayne@68
|
42 }
|
jpayne@68
|
43 return map;
|
jpayne@68
|
44 }
|
jpayne@68
|
45
|
jpayne@68
|
46 private static HashMap<Integer, byte[]> loadSSU(FileFormat ff, PrintStream outstream){
|
jpayne@68
|
47 ConcurrentReadInputStream cris=makeCris(ff, outstream);
|
jpayne@68
|
48 HashMap<Integer, byte[]> map=new HashMap<Integer, byte[]>(1000000);
|
jpayne@68
|
49
|
jpayne@68
|
50 //Grab the first ListNum of reads
|
jpayne@68
|
51 ListNum<Read> ln=cris.nextList();
|
jpayne@68
|
52
|
jpayne@68
|
53 //Check to ensure pairing is as expected
|
jpayne@68
|
54 if(ln!=null && !ln.isEmpty()){
|
jpayne@68
|
55 // if(verbose){outstream.println("Fetched "+ln.size()+" reads.");}
|
jpayne@68
|
56 Read r=ln.get(0);
|
jpayne@68
|
57 assert(ff.samOrBam() || (r.mate!=null)==cris.paired());
|
jpayne@68
|
58 }
|
jpayne@68
|
59
|
jpayne@68
|
60 //As long as there is a nonempty read list...
|
jpayne@68
|
61 while(ln!=null && ln.size()>0){
|
jpayne@68
|
62 if(verbose){outstream.println("Fetched "+ln.size()+" reads.");}
|
jpayne@68
|
63 for(Read r : ln){
|
jpayne@68
|
64 final int tid=GiToTaxid.getID(r.id);
|
jpayne@68
|
65 if(tid>=0 && r.length()>1000){
|
jpayne@68
|
66 byte[] old=map.get(tid);
|
jpayne@68
|
67 if(old==null || old.length<r.length()){map.put(tid, r.bases);}
|
jpayne@68
|
68 }
|
jpayne@68
|
69 }
|
jpayne@68
|
70 cris.returnList(ln.id, ln.list==null || ln.list.isEmpty());
|
jpayne@68
|
71
|
jpayne@68
|
72 //Fetch a new list
|
jpayne@68
|
73 ln=cris.nextList();
|
jpayne@68
|
74 }
|
jpayne@68
|
75
|
jpayne@68
|
76 //Notify the input stream that the final list was used
|
jpayne@68
|
77 if(ln!=null){
|
jpayne@68
|
78 cris.returnList(ln.id, ln.list==null || ln.list.isEmpty());
|
jpayne@68
|
79 }
|
jpayne@68
|
80 errorState|=ReadWrite.closeStream(cris);
|
jpayne@68
|
81
|
jpayne@68
|
82 return map;
|
jpayne@68
|
83 }
|
jpayne@68
|
84
|
jpayne@68
|
85 private static ConcurrentReadInputStream makeCris(FileFormat ff, PrintStream outstream){
|
jpayne@68
|
86 if(verbose){outstream.println("makeCris");}
|
jpayne@68
|
87 ConcurrentReadInputStream cris=ConcurrentReadInputStream.getReadInputStream(-1, false, ff, null);
|
jpayne@68
|
88 cris.start(); //Start the stream
|
jpayne@68
|
89 if(verbose){outstream.println("Loading "+ff.name());}
|
jpayne@68
|
90 boolean paired=cris.paired();
|
jpayne@68
|
91 assert(!paired);
|
jpayne@68
|
92 // if(!ffin1.samOrBam()){outstream.println("Input is being processed as "+(paired ? "paired" : "unpaired"));}
|
jpayne@68
|
93 return cris;
|
jpayne@68
|
94 }
|
jpayne@68
|
95
|
jpayne@68
|
96 public static boolean hasMap(){return r16SMap!=null || r18SMap!=null;}
|
jpayne@68
|
97 public static int r16SCount(){return r16SMap==null ? 0 : r16SMap.size();}
|
jpayne@68
|
98 public static int r18SCount(){return r18SMap==null ? 0 : r18SMap.size();}
|
jpayne@68
|
99
|
jpayne@68
|
100 public static String r16SFile=null;
|
jpayne@68
|
101 public static String r18SFile=null;
|
jpayne@68
|
102 public static HashMap<Integer, byte[]> r16SMap=null;
|
jpayne@68
|
103 public static HashMap<Integer, byte[]> r18SMap=null;
|
jpayne@68
|
104 static boolean verbose=false;
|
jpayne@68
|
105 static boolean errorState=false;
|
jpayne@68
|
106
|
jpayne@68
|
107 }
|