Debian Codegraph @

current/stream/SamLineStreamer.java

raw symbols SCIP
1 package stream;
2
3 import java.util.ArrayList;
4 import java.util.concurrent.ArrayBlockingQueue;
5
6 import fileIO.FileFormat;
7 import shared.KillSwitch;
8 import structures.ListNum;
9
10 /**
11 * Loads sam files rapidly with multiple threads.
12 *
13 * @author Brian Bushnell
14 * @date November 4, 2016
15 *
16 */
17 public class SamLineStreamer extends SamStreamer {
18
19 /*--------------------------------------------------------------*/
20 /*---------------- Initialization ----------------*/
21 /*--------------------------------------------------------------*/
22
23 /**
24 * Constructor.
25 */
26 public SamLineStreamer(String fname_, int threads_, boolean saveHeader_, long maxReads_){
27 this(FileFormat.testInput(fname_, FileFormat.SAM, null, true, false), threads_, saveHeader_, maxReads_);
28 }
29
30 /**
31 * Constructor.
32 */
33 public SamLineStreamer(FileFormat ffin_, int threads_, boolean saveHeader_, long maxReads_){
34 super(ffin_, threads_, saveHeader_, maxReads_);
35 outq=new ArrayBlockingQueue<ListNum<SamLine>>(threads+2);
36 }
37
38 /*--------------------------------------------------------------*/
39 /*---------------- Outer Methods ----------------*/
40 /*--------------------------------------------------------------*/
41
42 @Override
43 public ListNum<SamLine> nextLines(){
44 ListNum<SamLine> list=null;
45 while(list==null){
46 try {
47 list=outq.take();
48 } catch (InterruptedException e) {
49 // TODO Auto-generated catch block
50 e.printStackTrace();
51 }
52 if(verbose){outstream.println("a. Got list size "+list.size());}
53 }
54 while(list==POISON_LINES){
55 if(verbose){outstream.println("b. Got poison.");}
56 try {
57 outq.put(list);
58 list=null;
59 } catch (InterruptedException e) {
60 // TODO Auto-generated catch block
61 e.printStackTrace();
62 }
63 }
64 if(verbose){outstream.println("c. done.");}
65 return list;
66 }
67
68 @Override
69 public ListNum<Read> nextReads(){
70 KillSwitch.kill("Unsupported.");
71 return null;
72 }
73
74 /*--------------------------------------------------------------*/
75 /*---------------- Inner Methods ----------------*/
76 /*--------------------------------------------------------------*/
77
78
79 /** Spawn process threads */
80 @Override
81 void spawnThreads(){
82
83 //Do anything necessary prior to processing
84
85 //Determine how many threads may be used
86 final int threads=this.threads+1;
87
88 //Fill a list with ProcessThreads
89 ArrayList<ProcessThread> alpt=new ArrayList<ProcessThread>(threads);
90 for(int i=0; i<threads; i++){
91 alpt.add(new ProcessThread(i, alpt));
92 }
93 if(verbose){outstream.println("Spawned threads.");}
94
95 //Start the threads
96 for(ProcessThread pt : alpt){
97 pt.start();
98 }
99 if(verbose){outstream.println("Started threads.");}
100
101 //Do anything necessary after processing
102
103 }
104
105 /*--------------------------------------------------------------*/
106 /*---------------- Inner Classes ----------------*/
107 /*--------------------------------------------------------------*/
108
109 /** This class is static to prevent accidental writing to shared variables.
110 * It is safe to remove the static modifier. */
111 private class ProcessThread extends Thread {
112
113 //Constructor
114 ProcessThread(final int tid_, ArrayList<ProcessThread> alpt_){
115 tid=tid_;
116 alpt=(tid==0 ? alpt_ : null);
117 }
118
119 //Called by start()
120 @Override
121 public void run(){
122 //Do anything necessary prior to processing
123
124 //Process the reads
125 if(tid==0){
126 processBytes();
127 }else{
128 makeReads();
129 }
130
131 //Indicate successful exit status
132 success=true;
133 }
134
135 void processBytes(){
136 processBytes0(tid);
137
138 success=true;
139
140 //Wait for completion of all threads
141 boolean allSuccess=true;
142 for(ProcessThread pt : alpt){
143
144 //Wait until this thread has terminated
145 if(pt!=this){
146 if(verbose){outstream.println("Waiting for thread "+pt.tid);}
147 while(pt.getState()!=Thread.State.TERMINATED){
148 try {
149 //Attempt a join operation
150 pt.join();
151 } catch (InterruptedException e) {
152 //Potentially handle this, if it is expected to occur
153 e.printStackTrace();
154 }
155 }
156
157 //Accumulate per-thread statistics
158 readsProcessed+=pt.readsProcessedT;
159 basesProcessed+=pt.basesProcessedT;
160 allSuccess&=pt.success;
161 }
162 }
163
164 putReads(POISON_LINES);
165 if(verbose || verbose2){outstream.println("tid "+tid+" done poisoning reads.");}
166
167 //Track whether any threads failed
168 if(!allSuccess){errorState=true;}
169 if(verbose || verbose2){outstream.println("tid "+tid+" finished!");}
170
171 }
172
173 void putReads(ListNum<SamLine> list){
174 if(verbose){outstream.println("tid "+tid+" putting rlist size "+list.size());}
175 while(list!=null){
176 try {
177 outq.put(list);
178 list=null;
179 } catch (InterruptedException e) {
180 // TODO Auto-generated catch block
181 e.printStackTrace();
182 }
183 }
184 if(verbose){outstream.println("tid "+tid+" done putting rlist");}
185 }
186
187 /** Iterate through the reads */
188 void makeReads(){
189 if(verbose){outstream.println("tid "+tid+" started makeReads.");}
190
191 ListNum<byte[]> list=takeBytes();
192 while(list!=POISON_BYTES){
193 ListNum<SamLine> reads=new ListNum<SamLine>(new ArrayList<SamLine>(list.size()), list.id);
194 for(byte[] line : list){
195 if(line[0]=='@'){
196 //ignore;
197 }else{
198 SamLine sl=new SamLine(line);
199 reads.add(sl);
200
201 readsProcessedT++;
202 basesProcessedT+=(sl.seq==null ? 0 : sl.length());
203 }
204 }
205 if(reads.size()>0){putReads(reads);}
206 list=takeBytes();
207 }
208 if(verbose || verbose2){outstream.println("tid "+tid+" done making reads.");}
209
210 putBytes(POISON_BYTES);
211 if(verbose || verbose2){outstream.println("tid "+tid+" done poisoning bytes.");}
212
213 // putReads(POISON_LINES);
214 // if(verbose || verbose2){outstream.println("tid "+tid+" done poisoning reads.");}
215 }
216
217 /** Number of reads processed by this thread */
218 protected long readsProcessedT=0;
219 /** Number of bases processed by this thread */
220 protected long basesProcessedT=0;
221
222 /** True only if this thread has completed successfully */
223 boolean success=false;
224
225 /** Thread ID */
226 final int tid;
227
228 ArrayList<ProcessThread> alpt;
229 }
230
231 /*--------------------------------------------------------------*/
232 /*---------------- Final Fields ----------------*/
233 /*--------------------------------------------------------------*/
234
235 final ArrayBlockingQueue<ListNum<SamLine>> outq;
236
237 }