Debian Codegraph @

current/stream/ConcurrentCollectionReadInputStream.java

raw symbols SCIP
1 package stream;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.concurrent.TimeUnit;
6
7 import dna.Data;
8 import structures.ListNum;
9
10 public class ConcurrentCollectionReadInputStream extends ConcurrentReadInputStream {
11
12 public ConcurrentCollectionReadInputStream(List<Read> source1, List<Read> source2, long maxReadsToGenerate){
13 super("list");
14 assert(source1!=source2);
15 producer1=source1;
16 depot=new ConcurrentDepot<Read>(BUF_LEN, NUM_BUFFS);
17 producer2=source2;
18 maxReads=maxReadsToGenerate>=0 ? maxReadsToGenerate : Long.MAX_VALUE;
19 if(maxReads==0){
20 System.err.println("Warning - created a read stream for 0 reads.");
21 assert(false);
22 }
23
24 }
25
26 @Override
27 public synchronized ListNum<Read> nextList() {
28 ArrayList<Read> list=null;
29 if(verbose){System.err.println("**************** nextList() was called; shutdown="+shutdown+", depot.full="+depot.full.size());}
30 while(list==null){
31 if(shutdown){
32 if(verbose){System.err.println("**************** nextList() returning null; shutdown="+shutdown+", depot.full="+depot.full.size());}
33 return null;
34 }
35 try {
36 list=depot.full.take();
37 assert(list!=null);
38 } catch (InterruptedException e) {
39 // TODO Auto-generated catch block
40 e.printStackTrace();
41 }
42 }
43
44 if(verbose){System.err.println("**************** nextList() returning list of size "+list.size()+"; shutdown="+shutdown+", depot.full="+depot.full.size());}
45 ListNum<Read> ln=new ListNum<Read>(list, listnum);
46 listnum++;
47 return ln;
48 }
49
50 @Override
51 public void returnList(long listNumber, boolean poison){
52 if(poison){
53 if(verbose){System.err.println("crisC: A: Adding empty list to full.");}
54 depot.full.add(new ArrayList<Read>(0));
55 }else{
56 if(verbose){System.err.println("crisC: A: Adding empty list to empty.");}
57 depot.empty.add(new ArrayList<Read>(BUF_LEN));
58 }
59 }
60
61 @Override
62 public void run() {
63 // producer.start();
64 threads=new Thread[] {Thread.currentThread()};
65 if(verbose){System.err.println("crisC started, thread="+threads[0]);}
66
67 // readLists();
68 readSingles();
69
70 addPoison();
71
72 //End thread
73
74 while(!depot.empty.isEmpty() && !shutdown){
75 // System.out.println("Ending");
76 if(verbose){System.err.println("B: Adding empty lists to full.");}
77 depot.full.add(depot.empty.poll());
78 }
79 // System.err.println("cris thread terminated. Final depot size: "+depot.full.size()+", "+depot.empty.size());
80 }
81
82 private final void addPoison(){
83 //System.err.println("Adding poison.");
84 //Add poison pills
85 if(verbose){System.err.println("C: Adding poison to full.");}
86 depot.full.add(new ArrayList<Read>());
87 for(int i=1; i<depot.bufferCount; i++){
88 ArrayList<Read> list=null;
89 while(list==null){
90 try {
91 list=depot.empty.poll(1000, TimeUnit.MILLISECONDS);
92 } catch (InterruptedException e) {
93 // TODO Auto-generated catch block
94 // System.err.println("Do not be alarmed by the following error message:");
95 // e.printStackTrace();
96 if(shutdown){
97 i=depot.bufferCount;
98 break;
99 }
100 }
101 }
102 if(list!=null){
103 if(verbose){System.err.println("D: Adding list("+list.size()+") to full "+depot.full.size()+"/"+depot.bufferCount);}
104 depot.full.add(list);
105 }
106 }
107 //System.err.println("Added poison.");
108 }
109
110 private final void readSingles(){
111
112 for(int i=0; !shutdown && i<producer1.size() && generated<maxReads; i++){
113 ArrayList<Read> list=null;
114 while(list==null){
115 try {
116 list=depot.empty.take();
117 } catch (InterruptedException e) {
118 // TODO Auto-generated catch block
119 e.printStackTrace();
120 if(shutdown){break;}
121 }
122 }
123 if(shutdown || list==null){break;}
124
125 long bases=0;
126 final long lim=producer1.size();
127 while(list.size()<depot.bufferSize && generated<maxReads && bases<MAX_DATA && generated<lim){
128 Read a=producer1.get((int)generated);
129 Read b=(producer2==null ? null : producer2.get((int)generated));
130 if(a==null){break;}
131 readsIn++;
132 basesIn+=a.length();
133 if(b!=null){
134 readsIn++;
135 basesIn+=b.length();
136 }
137 if(randy==null || randy.nextFloat()<samplerate){
138 list.add(a);
139 if(b!=null){
140 assert(a.numericID==b.numericID) : "\n"+a.numericID+", "+b.numericID+"\n"+a.toText(false)+"\n"+b.toText(false)+"\n";
141 a.mate=b;
142 b.mate=a;
143
144 assert(a.pairnum()==0);
145 b.setPairnum(1);
146 bases+=(b.bases==null ? 0 : b.length());
147 }
148 bases+=(a.bases==null ? 0 : a.length());
149 }
150 incrementGenerated(1);
151 }
152
153 if(verbose){System.err.println("E: Adding list("+list.size()+") to full "+depot.full.size()+"/"+depot.bufferCount);}
154 depot.full.add(list);
155 }
156 }
157
158 private boolean shutdown=false;
159
160 @Override
161 public void shutdown(){
162 if(verbose){System.out.println("Called shutdown.");}
163 shutdown=true;
164 if(!shutdown){
165 if(verbose){System.out.println("shutdown 2.");}
166 for(Thread t : threads){
167 if(verbose){System.out.println("shutdown 3.");}
168 if(t!=null && t.isAlive()){
169 if(verbose){System.out.println("shutdown 4.");}
170 t.interrupt();
171 if(verbose){System.out.println("shutdown 5.");}
172 }
173 }
174 }
175 if(verbose){System.out.println("shutdown 6.");}
176 }
177
178 @Override
179 public synchronized void restart(){
180 shutdown=false;
181 depot=new ConcurrentDepot<Read>(BUF_LEN, NUM_BUFFS);
182 generated=0;
183 basesIn=0;
184 readsIn=0;
185 nextProgress=PROGRESS_INCR;
186 }
187
188 @Override
189 public synchronized void close(){
190 if(verbose){System.out.println("Thread "+Thread.currentThread().getId()+" called close.");}
191 shutdown();
192 // producer1.close();
193 // if(producer2!=null){producer2.close();}
194 // System.out.println("A");
195 if(threads!=null && threads[0]!=null && threads[0].isAlive()){
196 if(verbose){System.out.println("close 1.");}
197
198 while(threads[0].isAlive()){
199 if(verbose){System.out.println("close 2: Thread "+Thread.currentThread().getId()+" closing thread "+threads[0].getId()+" "+threads[0].getState());}
200 // System.out.println("B");
201 ArrayList<Read> list=null;
202 for(int i=0; i<1 && list==null && threads[0].isAlive(); i++){
203 if(verbose){System.out.println("close 3.");}
204 try {
205 if(verbose){System.out.println("close 4.");}
206 list=depot.full.poll(100, TimeUnit.MILLISECONDS);
207 if(verbose){System.out.println("close 5; list.size()="+depot.full.size()+", list="+(list==null ? "null" : list.size()+""));}
208 } catch (InterruptedException e) {
209 // TODO Auto-generated catch block
210 System.err.println("Do not be alarmed by the following error message:");
211 e.printStackTrace();
212 break;
213 }
214 }
215
216 if(list!=null){
217 list.clear();
218 depot.empty.add(list);
219 }
220 if(verbose){System.out.println("close 6.");}
221
222 // System.out.println("isAlive? "+threads[0].isAlive());
223 }
224 if(verbose){System.out.println("close 7.");}
225
226 }
227 if(verbose){System.out.println("close 8.");}
228
229 if(threads!=null){
230 if(verbose){System.out.println("close 9.");}
231 for(int i=1; i<threads.length; i++){
232 if(verbose){System.out.println("close 10.");}
233 while(threads[i]!=null && threads[i].isAlive()){
234 if(verbose){System.out.println("close 11.");}
235 try {
236 if(verbose){System.out.println("close 12.");}
237 threads[i].join();
238 if(verbose){System.out.println("close 13.");}
239 } catch (InterruptedException e) {
240 // TODO Auto-generated catch block
241 e.printStackTrace();
242 }
243 }
244 }
245 }
246 if(verbose){System.out.println("close 14.");}
247
248 }
249
250 @Override
251 public boolean paired() {
252 return producer2!=null ? true : (producer1==null || producer1.isEmpty() ? false : producer1.get(0).mate!=null);
253 }
254
255 @Override
256 public boolean verbose(){return verbose;}
257
258 private void incrementGenerated(long amt){
259 generated+=amt;
260 if(SHOW_PROGRESS && generated>=nextProgress){
261 Data.sysout.print('.');
262 nextProgress+=PROGRESS_INCR;
263 }
264 }
265
266 @Override
267 public void setSampleRate(float rate, long seed){
268 samplerate=rate;
269 if(rate>=1f){
270 randy=null;
271 }else if(seed>-1){
272 randy=new java.util.Random(seed);
273 }else{
274 randy=new java.util.Random();
275 }
276 }
277
278 @Override
279 public long basesIn(){return basesIn;}
280 @Override
281 public long readsIn(){return readsIn;}
282
283 @Override
284 public boolean errorState(){return errorState;}
285 /** TODO */
286 private boolean errorState=false;
287
288 private float samplerate=1f;
289 private java.util.Random randy=null;
290
291 private Thread[] threads;
292
293 @Override
294 public Object[] producers(){return new Object[] {producer1, producer2};}
295
296 public final List<Read> producer1;
297 public final List<Read> producer2;
298 private ConcurrentDepot<Read> depot;
299
300 private long basesIn=0;
301 private long readsIn=0;
302
303 private long maxReads;
304 private long generated=0;
305 private long listnum=0;
306 private long nextProgress=PROGRESS_INCR;
307
308 public static boolean verbose=false;
309
310 private static final ArrayList<Read> poison=new ArrayList<Read>(0);
311
312 }