Debian Codegraph @

current/stream/ConcurrentReadOutputStream.java

raw symbols SCIP
1 package stream;
2
3 import java.util.ArrayList;
4
5 import fileIO.FileFormat;
6 import shared.Shared;
7
8 /**
9 * Abstract superclass for ConcurrentReadOutputStream implementations.
10 * These manage ReadStreamWriters, which write reads to a file in their own thread.
11 * ConcurrentReadOutputStreams allow paired reads output to twin files to be treated as a single stream.
12 * @author Brian Bushnell
13 * @date Jan 26, 2015
14 *
15 */
16 public abstract class ConcurrentReadOutputStream {
17
18 /*--------------------------------------------------------------*/
19 /*---------------- Factory ----------------*/
20 /*--------------------------------------------------------------*/
21
22 /** @See primary method */
23 public static ConcurrentReadOutputStream getStream(FileFormat ff1, int rswBuffers, CharSequence header, boolean useSharedHeader){
24 return getStream(ff1, null, null, null, rswBuffers, header, useSharedHeader, Shared.USE_MPI, Shared.MPI_KEEP_ALL);
25 }
26
27 /** @See primary method */
28 public static ConcurrentReadOutputStream getStream(FileFormat ff1, FileFormat ff2, int rswBuffers, CharSequence header, boolean useSharedHeader){
29 return getStream(ff1, ff2, null, null, rswBuffers, header, useSharedHeader, Shared.USE_MPI, Shared.MPI_KEEP_ALL);
30 }
31
32 /** @See primary method */
33 public static ConcurrentReadOutputStream getStream(FileFormat ff1, FileFormat ff2, String qf1, String qf2,
34 int rswBuffers, CharSequence header, boolean useSharedHeader){
35 return getStream(ff1, ff2, qf1, qf2, rswBuffers, header, useSharedHeader, Shared.USE_MPI, Shared.MPI_KEEP_ALL);
36 }
37
38 /**
39 * Create a ConcurrentReadOutputStream.
40 * @param ff1 Read 1 file (required)
41 * @param ff2 Read 2 file (optional)
42 * @param qf1 Qual file 1 (optional)
43 * @param qf2 Qual file 2 (optional)
44 * @param rswBuffers Maximum number of lists to buffer for each ReadStreamWriter
45 * @param header A header to write to each output file before anything else
46 * @param useSharedHeader Write the shared header to each output file (mainly for sam output)
47 * @param mpi True if MPI will be used
48 * @param keepAll In MPI mode, tells this stream to keep all reads instead of just a fraction
49 * @return
50 */
51 public static ConcurrentReadOutputStream getStream(FileFormat ff1, FileFormat ff2, String qf1, String qf2,
52 int rswBuffers, CharSequence header, boolean useSharedHeader, final boolean mpi, final boolean keepAll){
53 if(mpi){
54 final int rank=Shared.MPI_RANK;
55 final ConcurrentReadOutputStream cros0;
56 if(rank==0){
57 cros0=new ConcurrentGenericReadOutputStream(ff1, ff2, qf1, qf2, rswBuffers, header, useSharedHeader);
58 }else{
59 cros0=null;
60 }
61 final ConcurrentReadOutputStream crosD;
62 if(Shared.USE_CRISMPI){
63 assert(false) : "To support MPI, uncomment this.";
64 crosD=null;
65 // crosD=new ConcurrentReadOutputStreamMPI(cros0, rank==0);
66 }else{
67 crosD=new ConcurrentReadOutputStreamD(cros0, rank==0);
68 }
69 return crosD;
70 }else{
71 return new ConcurrentGenericReadOutputStream(ff1, ff2, qf1, qf2, rswBuffers, header, useSharedHeader);
72 }
73
74 }
75
76 /*--------------------------------------------------------------*/
77 /*---------------- Initialization ----------------*/
78 /*--------------------------------------------------------------*/
79
80 ConcurrentReadOutputStream(FileFormat ff1_, FileFormat ff2_){
81 ff1=ff1_;
82 ff2=ff2_;
83 ordered=(ff1==null ? true : ff1.ordered());
84 }
85
86 /** Must be called before writing to the stream */
87 public abstract void start();
88
89 public final boolean started(){return started;}
90
91 /*--------------------------------------------------------------*/
92 /*---------------- Outer Methods ----------------*/
93 /*--------------------------------------------------------------*/
94
95 /**
96 * Enqueue this list to be written.
97 * @param list List of reads
98 * @param listnum A number, starting at 0. In ordered mode, lists will only be written in numeric order, regardless of adding order.
99 */
100 public abstract void add(ArrayList<Read> list, long listnum);
101
102 public abstract void close();
103
104 public abstract void join();
105
106 public abstract void resetNextListID();
107
108 public abstract String fname();
109
110 /** Return true if this stream has detected an error */
111 public abstract boolean errorState();
112
113 public abstract boolean finishedSuccessfully();
114
115 /*--------------------------------------------------------------*/
116 /*---------------- Inner Methods ----------------*/
117 /*--------------------------------------------------------------*/
118
119 /*--------------------------------------------------------------*/
120 /*---------------- Getters ----------------*/
121 /*--------------------------------------------------------------*/
122
123 public long basesWritten(){
124 long x=0;
125 ReadStreamWriter rsw1=getRS1();
126 ReadStreamWriter rsw2=getRS2();
127 if(rsw1!=null){x+=rsw1.basesWritten();}
128 if(rsw2!=null){x+=rsw2.basesWritten();}
129 return x;
130 }
131
132 public long readsWritten(){
133 long x=0;
134 ReadStreamWriter rsw1=getRS1();
135 ReadStreamWriter rsw2=getRS2();
136 if(rsw1!=null){x+=rsw1.readsWritten();}
137 if(rsw2!=null){x+=rsw2.readsWritten();}
138 return x;
139 }
140
141 public abstract ReadStreamWriter getRS1();
142 public abstract ReadStreamWriter getRS2();
143
144 /*--------------------------------------------------------------*/
145 /*---------------- Fields ----------------*/
146 /*--------------------------------------------------------------*/
147
148 public final FileFormat ff1, ff2;
149 public final boolean ordered;
150
151 boolean errorState=false;
152 boolean finishedSuccessfully=false;
153 boolean started=false;
154
155 /*--------------------------------------------------------------*/
156 /*---------------- Static Fields ----------------*/
157 /*--------------------------------------------------------------*/
158
159 public static boolean verbose=false;
160
161 }