Debian Codegraph @

current/stream/MultiCros2.java

raw symbols SCIP
1 package stream;
2
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.LinkedHashMap;
6 import java.util.Map.Entry;
7 import java.util.Set;
8
9 import fileIO.FileFormat;
10 import fileIO.ReadWrite;
11 import shared.KillSwitch;
12 import shared.Shared;
13 import structures.ByteBuilder;
14 import structures.ListNum;
15
16 /**
17 * This implementation allows only a single, synchronous open stream.
18 *
19 * @author Brian Bushnell
20 * @date May 1, 2019
21 *
22 */
23 public class MultiCros2 extends BufferedMultiCros {
24
25 /** For testing */
26 public static void main(String[] args){
27 String in=args[0];
28 String pattern=args[1];
29 ArrayList<String> names=new ArrayList<String>();
30 for(int i=2; i<args.length; i++){
31 names.add(args[i]);
32 }
33 MultiCros2 mcros=new MultiCros2(pattern, null, false, false, false, false, FileFormat.FASTQ, false);
34
35 ConcurrentReadInputStream cris=ConcurrentReadInputStream.getReadInputStream(-1, true, false, in);
36 cris.start();
37
38 ListNum<Read> ln=cris.nextList();
39 ArrayList<Read> reads=(ln!=null ? ln.list : null);
40
41 while(ln!=null && reads!=null && reads.size()>0){//ln!=null prevents a compiler potential null access warning
42
43 for(Read r1 : reads){
44 mcros.add(r1, r1.barcode(true));
45 }
46 cris.returnList(ln);
47 ln=cris.nextList();
48 reads=(ln!=null ? ln.list : null);
49 }
50 cris.returnList(ln);
51 ReadWrite.closeStreams(cris);
52 mcros.dumpAll();
53 mcros.close();
54 }
55
56 /*--------------------------------------------------------------*/
57 /*---------------- Initialization ----------------*/
58 /*--------------------------------------------------------------*/
59
60 /** @See Details in superclass constructor */
61 public MultiCros2(String pattern1_, String pattern2_,
62 boolean overwrite_, boolean append_, boolean allowSubprocess_, boolean useSharedHeader_, int defaultFormat_, boolean threaded_){
63 super(pattern1_, pattern2_, overwrite_, append_, allowSubprocess_, useSharedHeader_, defaultFormat_, threaded_, 1);
64 bufferMap=new LinkedHashMap<String, Buffer>();
65 }
66
67 /*--------------------------------------------------------------*/
68 /*---------------- Outer Methods ----------------*/
69 /*--------------------------------------------------------------*/
70
71 @Override
72 public boolean finishedSuccessfully(){
73 return !errorState;
74 }
75
76 @Override
77 public void add(Read r, String name){
78 assert(!threaded);
79 Buffer b=bufferMap.get(name);
80 if(b==null){
81 b=new Buffer(name);
82 bufferMap.put(name, b);
83 //Note: I could adjust bytesPerBuffer threshold here in response to the number of buffers.
84 }
85 b.add(r);
86 // System.err.println("Added "+name);
87 }
88
89 @Override
90 public long dumpResidual(ConcurrentReadOutputStream rosu){
91 long dumped=0;
92
93 //For each Buffer, check if it contains residual reads
94 //If so, dump it into the stream
95 for(Entry<String, Buffer> e : bufferMap.entrySet()){
96 Buffer b=e.getValue();
97 assert((b.readsIn<minReadsToDump) == (b.list!=null && !b.list.isEmpty()));
98 if(b.readsIn>0 && b.readsIn<minReadsToDump){
99 assert(b.list!=null && !b.list.isEmpty());
100 residualReads+=b.readsIn;
101 residualBases+=b.basesIn;
102 if(rosu!=null){rosu.add(b.list, 0);}
103 }
104 b.list=null;
105 }
106 return dumped;
107 }
108
109 @Override
110 public ByteBuilder report(){
111 ByteBuilder bb=new ByteBuilder(1024);
112 if(minReadsToDump>0){
113 bb.append("Residual").tab().append(residualReads).tab().append(residualBases).nl();
114 }
115 for(Entry<String, Buffer> e : bufferMap.entrySet()){
116 Buffer b=e.getValue();
117 if(b.readsIn>=minReadsToDump){
118 bb.append(b.name).tab().append(b.readsIn).tab().append(b.basesIn).nl();
119 }
120 }
121 return bb;
122 }
123
124 @Override
125 public Set<String> getKeys(){return bufferMap.keySet();}
126
127 /*--------------------------------------------------------------*/
128 /*---------------- Inner Methods ----------------*/
129 /*--------------------------------------------------------------*/
130
131 @Override
132 long closeInner() {
133 long x=dumpAll();
134 return x;
135 }
136
137 @Override
138 long dumpAll(){
139 long dumped=0;
140 for(Entry<String, Buffer> e : bufferMap.entrySet()){
141 dumped+=e.getValue().dump();
142 }
143 return dumped;
144 }
145
146 /*--------------------------------------------------------------*/
147 /*---------------- Getters ----------------*/
148 /*--------------------------------------------------------------*/
149
150 /*--------------------------------------------------------------*/
151 /*---------------- Inner Classes ----------------*/
152 /*--------------------------------------------------------------*/
153
154 /**
155 * A Buffer holds reads destined for to a specific file.
156 * When sufficient reads are present, it opens a stream and writes them,
157 * then closes the stream.
158 */
159 private class Buffer {
160
161 Buffer(String name_){
162 name=name_;
163 String s1=pattern1.replaceFirst("%", name);
164 String s2=pattern2==null ? null : pattern2.replaceFirst("%", name);
165
166 //These are created with overwrite=false append=true because
167 //the files will be appended to if the stream gets prematurely retired.
168 //Therefore, files must be explicitly deleted first.
169 //Alternative would be to create a new FileFormat each time.
170 ff1=FileFormat.testOutput(s1, defaultFormat, null, allowSubprocess, false, true, false);
171 ff2=FileFormat.testOutput(s2, defaultFormat, null, allowSubprocess, false, true, false);
172
173 list=new ArrayList<Read>(readsPerBuffer);
174 if(verbose){System.err.println("Made buffer for "+name);}
175 }
176
177 /**
178 * Add a read to this buffer, and update all the tracking variables.
179 * This may trigger a dump.
180 */
181 void add(Read r){
182 list.add(r);
183 long size=r.countPairBytes();
184 int count=r.pairCount();
185 currentBytes+=size;
186 bytesInFlight+=size;
187 basesIn+=size;
188 readsInFlight+=count;
189 readsIn+=count;
190
191 if(list.size()>=readsPerBuffer || currentBytes>=bytesPerBuffer){
192 if(verbose){
193 System.err.println("list.size="+list.size()+"/"+readsPerBuffer+
194 ", bytes="+currentBytes+"/"+bytesPerBuffer+", bytesInFlight="+bytesInFlight+"/"+memLimitUpper);
195 }
196 dump();
197 }
198
199 //Too much buffered data; dump everything.
200 if(bytesInFlight>=memLimitUpper){
201 long dumped=dumpAll();
202 if(dumped<1 && Shared.EA()){
203 KillSwitch.kill("\nThis program ran out of memory."
204 + "\nTry increasing the -Xmx flag or get rid of the minreads flag,"
205 + "\nor disable assertions to skip this message and try anyway.");
206 }
207 }
208 }
209
210 /** Create a stream, dump buffered reads, and close the stream */
211 long dump(){
212 // System.err.println("Dumping "+name);
213 if(list.isEmpty() || readsIn<minReadsToDump){return 0;}
214 if(numDumps==0 && overwrite){
215 delete(ff1);
216 delete(ff2);
217 }
218 // assert(false) : counter+", "+overwrite;
219
220 final long size0=list.size();
221
222 ConcurrentReadOutputStream ros=ConcurrentReadOutputStream.getStream(ff1, ff2, rswBuffers, null, useSharedHeader && numDumps==0);
223 ros.start();
224 ros.add(list, 0);
225 errorState=ReadWrite.closeStream(ros) | errorState;
226 // System.err.println("Closed stream "+name);
227
228 bytesInFlight-=currentBytes;
229 readsInFlight-=size0;
230 readsWritten+=size0;
231 currentBytes=0;
232 numDumps++;
233 list=new ArrayList<Read>(readsPerBuffer);
234 return size0;
235 }
236
237 private void delete(FileFormat ff){
238 if(ff==null){return;}
239 File f=new File(ff.name());
240 if(f.exists()){f.delete();}
241 }
242
243 /** Current list of buffered reads */
244 private ArrayList<Read> list;
245
246 /** Stream name, which is the variable part of the file pattern */
247 private final String name;
248 /** Output file 1 */
249 private final FileFormat ff1;
250 /** Output file 2 */
251 private final FileFormat ff2;
252 /** Number of reads entering the buffer */
253 private long readsIn=0;
254 /** Number of bases entering the buffer */
255 private long basesIn=0;
256 /** Number of reads written to disk */
257 @SuppressWarnings("unused")
258 private long readsWritten=0;//This does not count read2!
259 /** Number of bytes currently in this buffer (estimated) */
260 private long currentBytes=0;
261
262 /** Number of dumps executed */
263 private long numDumps=0;
264
265 }
266
267 /*--------------------------------------------------------------*/
268 /*---------------- Fields ----------------*/
269 /*--------------------------------------------------------------*/
270
271 /** Map of names to buffers */
272 public final LinkedHashMap<String, Buffer> bufferMap;
273
274 }