Debian Codegraph @

current/stream/OnelineReadInputStream.java

raw symbols SCIP
1 package stream;
2
3 import java.util.ArrayList;
4
5 import fileIO.ByteFile;
6 import fileIO.FileFormat;
7 import shared.KillSwitch;
8 import shared.Shared;
9 import shared.Tools;
10
11 public class OnelineReadInputStream extends ReadInputStream {
12
13 public static void main(String[] args){
14
15 OnelineReadInputStream fris=new OnelineReadInputStream(args[0], true);
16
17 Read r=fris.next();
18 System.out.println(r.toText(false));
19
20 }
21
22 public OnelineReadInputStream(String fname, boolean allowSubprocess_){
23 this(FileFormat.testInput(fname, FileFormat.ONELINE, null, allowSubprocess_, false));
24 }
25
26
27 public OnelineReadInputStream(FileFormat ff){
28 if(verbose){System.err.println("FastqReadInputStream("+ff+")");}
29
30 stdin=ff.stdio();
31 if(!ff.oneline()){
32 System.err.println("Warning: Did not find expected oneline file extension for filename "+ff.name());
33 }
34
35 // interleaved=false;
36 // assert(false) : "TODO: Detect interleaved.";
37 interleaved=FASTQ.FORCE_INTERLEAVED; //(ff.stdio()) ? FASTQ_X.FORCE_INTERLEAVED : FASTQ_X.isInterleaved(ff.name(), false);
38
39 tf=ByteFile.makeByteFile(ff);
40 // assert(false) : interleaved;
41 }
42
43 @Override
44 public void start() {
45 // if(cris!=null){cris.start();}
46 }
47
48
49 @Override
50 public boolean hasMore() {
51 if(buffer==null || next>=buffer.size()){
52 if(tf.isOpen()){
53 fillBuffer();
54 }else{
55 assert(generated>0) : "Was the file empty?";
56 }
57 }
58 return (buffer!=null && next<buffer.size());
59 }
60
61 @Override
62 public Read next() {
63 if(!hasMore()){return null;}
64 Read r=buffer.set(next, null);
65 next++;
66 consumed++;
67 return r;
68 }
69
70 @Override
71 public synchronized ArrayList<Read> nextList() {
72 if(next!=0){throw new RuntimeException("'next' should not be used when doing blockwise access.");}
73 if(buffer==null || next>=buffer.size()){fillBuffer();}
74 ArrayList<Read> list=buffer;
75 buffer=null;
76 if(list!=null && list.size()==0){list=null;}
77 consumed+=(list==null ? 0 : list.size());
78 return list;
79 }
80
81 private synchronized void fillBuffer(){
82
83 assert(buffer==null || next>=buffer.size());
84
85 buffer=null;
86 next=0;
87
88 buffer=toReadList();
89 int bsize=(buffer==null ? 0 : buffer.size());
90 // nextReadID+=bsize;
91 if(bsize<BUF_LEN){tf.close();}
92
93 generated+=bsize;
94 if(buffer==null){
95 if(!errorState){
96 errorState=true;
97 System.err.println("Null buffer in FastqReadInputStream.");
98 }
99 }
100 }
101
102 private ArrayList<Read> toReadList(){
103 ArrayList<Read> list=new ArrayList<Read>(BUF_LEN);
104 Read r1=null, r2=null;
105 long sum=0;
106 for(byte[] line=tf.nextLine(); line!=null; line=tf.nextLine()){
107 int index=Tools.lastIndexOf(line, (byte)'\t');
108 String id=new String(line, 0, index);
109 byte[] bases=KillSwitch.copyOfRange(line, index+1, line.length);
110 sum+=bases.length;
111 Read r=new Read(bases, null, id, nextReadID);
112 if(r1==null){
113 r1=r;
114 }else{
115 r2=r;
116 r1.mate=r2;
117 r2.mate=r1;
118 }
119 if(interleaved==(r2!=null)){
120 list.add(r1);
121 r1=r2=null;
122 nextReadID++;
123 if(list.size()>=BUF_LEN || sum>=MAX_DATA){break;}
124 }
125 }
126 return list;
127 }
128
129 @Override
130 public boolean close(){
131 if(verbose){System.err.println("Closing "+this.getClass().getName()+" for "+tf.name()+"; errorState="+errorState);}
132 errorState|=tf.close();
133 if(verbose){System.err.println("Closed "+this.getClass().getName()+" for "+tf.name()+"; errorState="+errorState);}
134 return errorState;
135 }
136
137 @Override
138 public synchronized void restart() {
139 generated=0;
140 consumed=0;
141 next=0;
142 nextReadID=0;
143 buffer=null;
144 tf.reset();
145 }
146
147 @Override
148 public boolean paired() {return interleaved;}
149
150 /** Return true if this stream has detected an error */
151 @Override
152 public boolean errorState(){return errorState;}
153
154 @Override
155 public String fname(){return tf.name();}
156
157 private ArrayList<Read> buffer=null;
158 private int next=0;
159
160 private final ByteFile tf;
161 private final boolean interleaved;
162
163 private final int BUF_LEN=Shared.bufferLen();;
164 private final long MAX_DATA=Shared.bufferData(); //TODO - lot of work for unlikely case of super-long fastq reads. Must be disabled for paired-ends.
165
166 public long generated=0;
167 public long consumed=0;
168 private long nextReadID=0;
169
170 public final boolean stdin;
171 public static boolean verbose=false;
172
173 }