Debian Codegraph @

current/stream/SamStreamer.java

raw symbols SCIP
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 * Loads sam files rapidly with multiple threads.
15 *
16 * @author Brian Bushnell
17 * @date November 4, 2016
18 *
19 */
20 public abstract class SamStreamer {
21
22 /*--------------------------------------------------------------*/
23 /*---------------- Initialization ----------------*/
24 /*--------------------------------------------------------------*/
25
26 /**
27 * Code entrance from the command line.
28 * @param args Command line arguments
29 */
30 public static final void main(String[] args){
31 //Start a timer immediately upon code entrance.
32 Timer t=new Timer();
33
34 //Create an instance of this class
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 //Run the object
40 x.start();
41 x.test();
42
43 t.stop("Time: ");
44 }
45
46 /**
47 * Constructor.
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 * Constructor.
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 /*---------------- Outer Methods ----------------*/
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 /** Create read streams and process all data */
80 public final void start(){
81
82 //Reset counters
83 readsProcessed=0;
84 basesProcessed=0;
85
86 //Process the reads in separate threads
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 /*---------------- Inner Methods ----------------*/
98 /*--------------------------------------------------------------*/
99
100 /**
101 * Thread 0 reads the actual file, optionally processes the header,
102 * and produces lists of byte[] for other threads to format as SamLines.
103 * @param tid Thread number (should be 0).
104 */
105 public final void processBytes0(int tid){
106 if(verbose){outstream.println("tid "+tid+" started processBytes.");}
107
108 // ByteFile.FORCE_MODE_BF1=true;
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 // outstream.println("a");
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 // outstream.println("b");
132 // outstream.println(inq.size()+", "+inq.remainingCapacity());
133 putBytes(new ListNum<byte[]>(list, listNumber));
134 listNumber++;
135 // outstream.println("c");
136 list=new ArrayList<byte[]>(limit);
137 }
138 }
139 // outstream.println("d");
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 // if(verbose){outstream.println("tid "+tid+" putting blist size "+list.size());}
160 while(list!=null){
161 try {
162 inq.put(list);
163 list=null;
164 } catch (InterruptedException e) {
165 // TODO Auto-generated catch block
166 e.printStackTrace();
167 }
168 }
169 // if(verbose){outstream.println("tid "+tid+" done putting blist");}
170 }
171
172 final ListNum<byte[]> takeBytes(){
173 // if(verbose){outstream.println("tid "+tid+" taking blist");}
174 ListNum<byte[]> list=null;
175 while(list==null){
176 try {
177 list=inq.take();
178 } catch (InterruptedException e) {
179 // TODO Auto-generated catch block
180 e.printStackTrace();
181 }
182 }
183 // if(verbose){outstream.println("tid "+tid+" took blist size "+list.size());}
184 return list;
185 }
186
187 /** Spawn process threads */
188 abstract void spawnThreads();
189
190 /*--------------------------------------------------------------*/
191 /*---------------- Fields ----------------*/
192 /*--------------------------------------------------------------*/
193
194 /** Primary input file path */
195 protected String fname;
196
197 /*--------------------------------------------------------------*/
198
199 /** Number of reads processed */
200 protected long readsProcessed=0;
201 /** Number of bases processed */
202 protected long basesProcessed=0;
203
204 // public void setMaxReads(long x){
205 // maxReads=(x<1 ? Long.MAX_VALUE : x);
206 // }
207
208 /** Quit after processing this many input reads */
209 private final long maxReads;
210
211 /*--------------------------------------------------------------*/
212 /*---------------- Final Fields ----------------*/
213 /*--------------------------------------------------------------*/
214
215 final boolean saveHeader;
216
217 /** Primary input file */
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 /*---------------- Static Fields ----------------*/
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 /*---------------- Common Fields ----------------*/
238 /*--------------------------------------------------------------*/
239
240 /** Print status messages to this output stream */
241 protected PrintStream outstream=System.err;
242 /** Print verbose messages */
243 public static final boolean verbose=false;
244 public static final boolean verbose2=false;
245 /** True if an error was encountered */
246 public boolean errorState=false;
247
248 }