Debian Codegraph @

current/stream/ConcurrentLegacyReadInputStream.java

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