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