Debian Codegraph @

current/stream/SamStreamerMF.java

raw symbols SCIP
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 * Loads multiple sam files rapidly with multiple threads.
14 *
15 * @author Brian Bushnell
16 * @date March 6, 2019
17 *
18 */
19 public class SamStreamerMF {
20
21 /*--------------------------------------------------------------*/
22 /*---------------- Initialization ----------------*/
23 /*--------------------------------------------------------------*/
24
25 /**
26 * Code entrance from the command line.
27 * @param args Command line arguments
28 */
29 public static final void main(String[] args){
30 //Start a timer immediately upon code entrance.
31 Timer t=new Timer();
32
33 //Create an instance of this class
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 //Run the object
39 x.start();
40 x.test();
41
42 t.stop("Time: ");
43 }
44
45 /**
46 * Constructor.
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 * Constructor.
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 /*---------------- Outer Methods ----------------*/
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 /** Create read streams and process all data */
75 public final void start(){
76 //Reset counters
77 readsProcessed=0;
78 basesProcessed=0;
79
80 //Process the reads in separate threads
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 // public abstract ListNum<SamLine> nextLines();
114
115 /*--------------------------------------------------------------*/
116 /*---------------- Inner Methods ----------------*/
117 /*--------------------------------------------------------------*/
118
119 /** Spawn process threads */
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 /*---------------- Fields ----------------*/
137 /*--------------------------------------------------------------*/
138
139 /** Primary input file path */
140 protected String fname;
141
142 /*--------------------------------------------------------------*/
143
144 /** Number of reads processed */
145 protected long readsProcessed=0;
146 /** Number of bases processed */
147 protected long basesProcessed=0;
148
149 /** Quit after processing this many input reads; -1 means no limit */
150 protected long maxReads=-1;
151
152 /*--------------------------------------------------------------*/
153 /*---------------- Final Fields ----------------*/
154 /*--------------------------------------------------------------*/
155
156 final boolean saveHeader;
157
158 /** Primary input file */
159 final FileFormat[] ffin;
160
161 /** Readers */
162 // final SamReadStreamer[] streamers;
163 private ArrayDeque<SamReadStreamer> streamerSource;
164 private ArrayDeque<SamReadStreamer> activeStreamers;
165
166 final int threads;
167
168 /*--------------------------------------------------------------*/
169 /*---------------- Static Fields ----------------*/
170 /*--------------------------------------------------------------*/
171
172 public static int DEFAULT_THREADS=6;
173 public static int MAX_FILES=8;
174
175 /*--------------------------------------------------------------*/
176 /*---------------- Common Fields ----------------*/
177 /*--------------------------------------------------------------*/
178
179 /** Print status messages to this output stream */
180 protected PrintStream outstream=System.err;
181 /** Print verbose messages */
182 public static final boolean verbose=false;
183 public static final boolean verbose2=false;
184 /** True if an error was encountered */
185 public boolean errorState=false;
186
187 }