| 1 |
package stream; |
| 2 |
|
| 3 |
import java.io.PrintStream; |
| 4 |
import java.util.ArrayList; |
| 5 |
import java.util.concurrent.ArrayBlockingQueue; |
| 6 |
|
| 7 |
import fileIO.ByteFile; |
| 8 |
import fileIO.FileFormat; |
| 9 |
import shared.Shared; |
| 10 |
import shared.Timer; |
| 11 |
import structures.ListNum; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
public abstract class SamStreamer { |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public static final void main(String[] args){ |
| 31 |
|
| 32 |
Timer t=new Timer(); |
| 33 |
|
| 34 |
|
| 35 |
int threads=Shared.threads(); |
| 36 |
if(args.length>1){threads=Integer.parseInt(args[1]);} |
| 37 |
SamStreamer x=new SamReadStreamer(args[0], threads, false, -1); |
| 38 |
|
| 39 |
|
| 40 |
x.start(); |
| 41 |
x.test(); |
| 42 |
|
| 43 |
t.stop("Time: "); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public SamStreamer(String fname_, int threads_, boolean saveHeader_, long maxReads_){ |
| 50 |
this(FileFormat.testInput(fname_, FileFormat.SAM, null, true, false), threads_, saveHeader_, maxReads_); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public SamStreamer(FileFormat ffin_, int threads_, boolean saveHeader_, long maxReads_){ |
| 57 |
fname=ffin_.name(); |
| 58 |
threads=threads_; |
| 59 |
ffin=ffin_; |
| 60 |
saveHeader=saveHeader_; |
| 61 |
header=(saveHeader ? new ArrayList<byte[]>() : null); |
| 62 |
maxReads=(maxReads_<1 ? Long.MAX_VALUE : maxReads_); |
| 63 |
|
| 64 |
inq=new ArrayBlockingQueue<ListNum<byte[]>>(threads/2+2); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
final void test(){ |
| 73 |
for(ListNum<Read> list=nextReads(); list!=null; list=nextReads()){ |
| 74 |
if(verbose){outstream.println("Got list of size "+list.size());} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public final void start(){ |
| 81 |
|
| 82 |
|
| 83 |
readsProcessed=0; |
| 84 |
basesProcessed=0; |
| 85 |
|
| 86 |
|
| 87 |
spawnThreads(); |
| 88 |
|
| 89 |
if(verbose){outstream.println("Finished; closing streams.");} |
| 90 |
} |
| 91 |
|
| 92 |
public final ListNum<Read> nextList(){return nextReads();} |
| 93 |
public abstract ListNum<Read> nextReads(); |
| 94 |
public abstract ListNum<SamLine> nextLines(); |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public final void processBytes0(int tid){ |
| 106 |
if(verbose){outstream.println("tid "+tid+" started processBytes.");} |
| 107 |
|
| 108 |
|
| 109 |
ByteFile.FORCE_MODE_BF2=true; |
| 110 |
ByteFile bf=ByteFile.makeByteFile(ffin); |
| 111 |
|
| 112 |
long listNumber=0; |
| 113 |
long reads=0; |
| 114 |
|
| 115 |
final int limit=LIST_SIZE; |
| 116 |
ArrayList<byte[]> list=new ArrayList<byte[]>(limit); |
| 117 |
for(byte[] line=bf.nextLine(); line!=null && reads<maxReads; line=bf.nextLine()){ |
| 118 |
assert(line!=null); |
| 119 |
|
| 120 |
if(header!=null && line[0]=='@'){ |
| 121 |
if(Shared.TRIM_RNAME){line=SamReadInputStream.trimHeaderSQ(line);} |
| 122 |
header.add(line); |
| 123 |
}else{ |
| 124 |
if(header!=null){ |
| 125 |
SamReadInputStream.setSharedHeader(header); |
| 126 |
header=null; |
| 127 |
} |
| 128 |
reads++; |
| 129 |
list.add(line); |
| 130 |
if(list.size()>=limit){ |
| 131 |
|
| 132 |
|
| 133 |
putBytes(new ListNum<byte[]>(list, listNumber)); |
| 134 |
listNumber++; |
| 135 |
|
| 136 |
list=new ArrayList<byte[]>(limit); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
} |
| 141 |
if(header!=null){ |
| 142 |
SamReadInputStream.setSharedHeader(header); |
| 143 |
header=null; |
| 144 |
} |
| 145 |
if(verbose){outstream.println("tid "+tid+" ran out of input.");} |
| 146 |
if(list.size()>0){ |
| 147 |
putBytes(new ListNum<byte[]>(list, listNumber)); |
| 148 |
listNumber++; |
| 149 |
list=null; |
| 150 |
} |
| 151 |
if(verbose || verbose2){outstream.println("tid "+tid+" done reading bytes.");} |
| 152 |
putBytes(POISON_BYTES); |
| 153 |
if(verbose || verbose2){outstream.println("tid "+tid+" done poisoning.");} |
| 154 |
bf.close(); |
| 155 |
if(verbose || verbose2){outstream.println("tid "+tid+" closed stream.");} |
| 156 |
} |
| 157 |
|
| 158 |
final void putBytes(ListNum<byte[]> list){ |
| 159 |
|
| 160 |
while(list!=null){ |
| 161 |
try { |
| 162 |
inq.put(list); |
| 163 |
list=null; |
| 164 |
} catch (InterruptedException e) { |
| 165 |
|
| 166 |
e.printStackTrace(); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
} |
| 171 |
|
| 172 |
final ListNum<byte[]> takeBytes(){ |
| 173 |
|
| 174 |
ListNum<byte[]> list=null; |
| 175 |
while(list==null){ |
| 176 |
try { |
| 177 |
list=inq.take(); |
| 178 |
} catch (InterruptedException e) { |
| 179 |
|
| 180 |
e.printStackTrace(); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
return list; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
abstract void spawnThreads(); |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
protected String fname; |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
protected long readsProcessed=0; |
| 201 |
|
| 202 |
protected long basesProcessed=0; |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
private final long maxReads; |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
final boolean saveHeader; |
| 216 |
|
| 217 |
|
| 218 |
final FileFormat ffin; |
| 219 |
|
| 220 |
final ArrayBlockingQueue<ListNum<byte[]>> inq; |
| 221 |
|
| 222 |
final int threads; |
| 223 |
|
| 224 |
public ArrayList<byte[]> header; |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
static final ListNum<Read> POISON_READS=new ListNum<Read>(null, -1); |
| 231 |
static final ListNum<SamLine> POISON_LINES=new ListNum<SamLine>(null, -1); |
| 232 |
static final ListNum<byte[]> POISON_BYTES=new ListNum<byte[]>(null, -1); |
| 233 |
public static int LIST_SIZE=200; |
| 234 |
public static int DEFAULT_THREADS=6; |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
protected PrintStream outstream=System.err; |
| 242 |
|
| 243 |
public static final boolean verbose=false; |
| 244 |
public static final boolean verbose2=false; |
| 245 |
|
| 246 |
public boolean errorState=false; |
| 247 |
|
| 248 |
} |