| 1 |
package stream; |
| 2 |
|
| 3 |
import java.io.PrintStream; |
| 4 |
import java.util.ArrayDeque; |
| 5 |
|
| 6 |
import fileIO.FileFormat; |
| 7 |
import shared.Shared; |
| 8 |
import shared.Timer; |
| 9 |
import shared.Tools; |
| 10 |
import structures.ListNum; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
public class SamStreamerMF { |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public static final void main(String[] args){ |
| 30 |
|
| 31 |
Timer t=new Timer(); |
| 32 |
|
| 33 |
|
| 34 |
int threads=Shared.threads(); |
| 35 |
if(args.length>1){threads=Integer.parseInt(args[1]);} |
| 36 |
SamStreamerMF x=new SamStreamerMF(args[0].split(","), threads, false, -1); |
| 37 |
|
| 38 |
|
| 39 |
x.start(); |
| 40 |
x.test(); |
| 41 |
|
| 42 |
t.stop("Time: "); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public SamStreamerMF(String[] fnames_, int threads_, boolean saveHeader_, long maxReads_){ |
| 49 |
this(FileFormat.testInput(fnames_, FileFormat.SAM, null, true, false), threads_, saveHeader_, maxReads_); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public SamStreamerMF(FileFormat[] ffin_, int threads_, boolean saveHeader_, long maxReads_){ |
| 56 |
fname=ffin_[0].name(); |
| 57 |
threads=threads_; |
| 58 |
ffin=ffin_; |
| 59 |
saveHeader=saveHeader_; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
final void test(){ |
| 68 |
for(ListNum<Read> list=nextReads(); list!=null; list=nextReads()){ |
| 69 |
if(verbose){outstream.println("Got list of size "+list.size());} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public final void start(){ |
| 76 |
|
| 77 |
readsProcessed=0; |
| 78 |
basesProcessed=0; |
| 79 |
|
| 80 |
|
| 81 |
spawnThreads(); |
| 82 |
|
| 83 |
if(verbose){outstream.println("Finished; closing streams.");} |
| 84 |
} |
| 85 |
|
| 86 |
public final ListNum<Read> nextList(){return nextReads();} |
| 87 |
public final ListNum<Read> nextReads(){ |
| 88 |
ListNum<Read> list=null; |
| 89 |
assert(activeStreamers!=null); |
| 90 |
synchronized(activeStreamers){ |
| 91 |
if(activeStreamers.isEmpty()){return null;} |
| 92 |
while(list==null && !activeStreamers.isEmpty()){ |
| 93 |
SamReadStreamer srs=activeStreamers.poll(); |
| 94 |
list=srs.nextReads(); |
| 95 |
if(list!=null){activeStreamers.add(srs);} |
| 96 |
else{ |
| 97 |
readsProcessed+=srs.readsProcessed; |
| 98 |
basesProcessed+=srs.basesProcessed; |
| 99 |
if(srs.header!=null){ |
| 100 |
SamReadInputStream.setSharedHeader(srs.header); |
| 101 |
} |
| 102 |
|
| 103 |
if(!streamerSource.isEmpty()){ |
| 104 |
srs=streamerSource.poll(); |
| 105 |
srs.start(); |
| 106 |
activeStreamers.add(srs); |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
return list; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
void spawnThreads(){ |
| 121 |
final int maxActive=Tools.max(2, Tools.min((Shared.threads()+4)/5, ffin.length, MAX_FILES)); |
| 122 |
streamerSource=new ArrayDeque<SamReadStreamer>(ffin.length); |
| 123 |
activeStreamers=new ArrayDeque<SamReadStreamer>(maxActive); |
| 124 |
for(int i=0; i<ffin.length; i++){ |
| 125 |
SamReadStreamer srs=new SamReadStreamer(ffin[i], threads, saveHeader, maxReads); |
| 126 |
streamerSource.add(srs); |
| 127 |
} |
| 128 |
while(activeStreamers.size()<maxActive && !streamerSource.isEmpty()){ |
| 129 |
SamReadStreamer srs=streamerSource.poll(); |
| 130 |
srs.start(); |
| 131 |
activeStreamers.add(srs); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
protected String fname; |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
protected long readsProcessed=0; |
| 146 |
|
| 147 |
protected long basesProcessed=0; |
| 148 |
|
| 149 |
|
| 150 |
protected long maxReads=-1; |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
final boolean saveHeader; |
| 157 |
|
| 158 |
|
| 159 |
final FileFormat[] ffin; |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
private ArrayDeque<SamReadStreamer> streamerSource; |
| 164 |
private ArrayDeque<SamReadStreamer> activeStreamers; |
| 165 |
|
| 166 |
final int threads; |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public static int DEFAULT_THREADS=6; |
| 173 |
public static int MAX_FILES=8; |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
protected PrintStream outstream=System.err; |
| 181 |
|
| 182 |
public static final boolean verbose=false; |
| 183 |
public static final boolean verbose2=false; |
| 184 |
|
| 185 |
public boolean errorState=false; |
| 186 |
|
| 187 |
} |