Debian Codegraph @

current/stream/SamReadInputStream.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.Shared;
8 import shared.Tools;
9 import structures.ByteBuilder;
10
11 public class SamReadInputStream extends ReadInputStream {
12
13 public static void main(String[] args){
14
15 SamReadInputStream sris=new SamReadInputStream(args[0], false, false, true);
16
17 Read r=sris.next();
18 System.out.println(r.toText(false));
19 System.out.println();
20 System.out.println(r.obj.toString());
21 System.out.println();
22 }
23
24 public SamReadInputStream(String fname, boolean loadHeader_, boolean interleaved_, boolean allowSubprocess_){
25 this(FileFormat.testInput(fname, FileFormat.SAM, null, allowSubprocess_, false), loadHeader_, interleaved_);
26 }
27
28 public SamReadInputStream(FileFormat ff, boolean loadHeader_, boolean interleaved_){
29 loadHeader=loadHeader_;
30 // assert(loadHeader);
31 // interleaved=((tf.is==System.in || stdin) ? FASTQ.FORCE_INTERLEAVED : true);
32 interleaved=interleaved_;
33
34 stdin=ff.stdio();
35 if(!ff.samOrBam()){
36 System.err.println("Warning: Did not find expected sam file extension for filename "+ff.name());
37 }
38
39 tf=ByteFile.makeByteFile(ff);
40 header=new ArrayList<byte[]>();
41
42 }
43
44 @Override
45 public void start() {
46 // if(cris!=null){cris.start();}
47 }
48
49
50 @Override
51 public boolean hasMore() {
52 if(buffer==null || next>=buffer.size()){
53 if(tf.isOpen()){
54 fillBuffer();
55 }else{
56 assert(generated>0) : "Was the file empty?";
57 }
58 }
59 return (buffer!=null && next<buffer.size());
60 }
61
62 @Override
63 public Read next() {
64 if(!hasMore()){return null;}
65 Read r=buffer.set(next, null);
66 next++;
67 consumed++;
68 return r;
69 }
70
71 @Override
72 public synchronized ArrayList<Read> nextList() {
73 if(next!=0){throw new RuntimeException("'next' should not be used when doing blockwise access.");}
74 if(buffer==null || next>=buffer.size()){fillBuffer();}
75 ArrayList<Read> list=buffer;
76 buffer=null;
77 if(list!=null && list.size()==0){list=null;}
78 consumed+=(list==null ? 0 : list.size());
79 // System.err.println(hashCode()+" produced "+r[0].numericID);
80 return list;
81 }
82
83 private synchronized void fillBuffer(){
84
85 assert(buffer==null || next>=buffer.size());
86
87 buffer=null;
88 next=0;
89
90 buffer=toReadList(tf, BUF_LEN, nextReadID, FASTQ.PARSE_CUSTOM);
91 nextReadID+=buffer.size();
92 generated+=buffer.size();
93
94 if(buffer.size()<BUF_LEN){tf.close();}
95 }
96
97 /**
98 * @param tf2
99 * @param bUF_LEN2
100 * @param nextReadID2
101 * @param interleaved2
102 * @return
103 */
104 private final ArrayList<Read> toReadList(ByteFile tf2, int buflen, long nextReadID2, boolean parseCustom) {
105 ArrayList<Read> list=new ArrayList<Read>(buflen);
106 while(list.size()<buflen){
107 byte[] line=tf2.nextLine();
108 // System.out.println("A: Read line "+new String(line));
109 while(line!=null && line[0]=='@'){
110 // System.out.println(">"+new String(line));
111 if(Shared.TRIM_RNAME){line=trimHeaderSQ(line);}
112 if(loadHeader){header.add(line);}
113 line=tf2.nextLine();
114 // assert(false) : new String(line)+"\n"+header.size()+", "+SHARED_HEADER;
115 // System.out.println("B: Read line "+new String(line));
116 }
117 if(loadHeader && nextReadID2==0){setSharedHeader(header);}
118 if(line==null){return list;}
119 SamLine sl1=new SamLine(line);
120
121 Read r1=sl1.toRead(parseCustom);
122 r1.samline=sl1;
123 r1.numericID=nextReadID2;
124 list.add(r1);
125 if(interleaved && (sl1.flag&0x1)!=0){
126 assert((sl1.flag&0x40)!=0) : r1+"\n\n"+sl1;
127 byte[] line2=tf2.nextLine();
128 SamLine sl2=null;
129 Read r2=null;
130 if(line2!=null){
131 sl2=new SamLine(line2);
132 r2=sl2.toRead(parseCustom);
133 r2.numericID=nextReadID2;
134 }else{
135 assert(false) : r1+"\n\n"+sl1;
136 }
137 if(sl2!=null){
138 assert((sl2.flag&0x1)!=0);
139 assert((sl2.flag&0x80)!=0) : r2+"\n\n"+sl2+"\nflag="+Integer.toBinaryString(sl2.flag)+"\n";
140 r1.mate=r2;
141 r2.mate=r1;
142
143 int lim=Tools.min(sl1.qname.length(), sl2.qname.length());
144 for(int i=0; i<lim; i++){
145 char a=sl1.qname.charAt(i);
146 char b=sl2.qname.charAt(i);
147 if(a=='/' || b=='/' || Character.isWhitespace(a) || Character.isWhitespace(b)){break;}
148 assert(a==b) : "Name mismatch for paired reads: '"+sl1.qname+"' != '"+sl2.qname+"'\n\n"+sl1+"\n\n"+sl2;
149 }
150
151 }
152 }
153 nextReadID2++;
154 }
155 return list;
156 }
157
158 @Override
159 public boolean close(){
160 return tf.close();
161 }
162
163 @Override
164 public synchronized void restart() {
165 generated=0;
166 consumed=0;
167 next=0;
168 nextReadID=0;
169 buffer=null;
170 header=new ArrayList<byte[]>();
171 tf.reset();
172 }
173
174 public static synchronized ArrayList<byte[]> getSharedHeader(boolean wait){
175 if(!wait || SHARED_HEADER!=null){return SHARED_HEADER;}
176 System.err.println("Waiting on header to be read from a sam file.");
177 while(SHARED_HEADER==null){
178 try {
179 SamReadInputStream.class.wait(100);
180 } catch (InterruptedException e) {
181 // TODO Auto-generated catch block
182 e.printStackTrace();
183 }
184 }
185 return SHARED_HEADER;
186 }
187
188 public static synchronized void setSharedHeader(ArrayList<byte[]> list){
189 // assert(false) : list.size();
190 // if(list!=null && Shared.TRIM_RNAME){
191 // for(int i=0; i<list.size(); i++){
192 // byte[] line=list.get(i);
193 // list.set(i, trimHeaderSQ(line));
194 // }
195 // }
196 SHARED_HEADER=list;
197 SamReadInputStream.class.notifyAll();
198 }
199
200 static byte[] trimHeaderSQ(byte[] line){
201 if(line==null || !Tools.startsWith(line, "@SQ")){return line;}
202
203 final int idx=Tools.indexOfDelimited(line, "SN:", 2, (byte)'\t');
204 if(idx<0){
205 assert(false) : "Bad header: "+new String(line);
206 return line;
207 }
208
209 int trimStart=-1;
210 for(int i=idx; i<line.length; i++){
211 final byte b=line[i];
212 if(b=='\t'){return line;}
213 if(Character.isWhitespace(b)){
214 trimStart=i;
215 break;
216 }
217 }
218 if(trimStart<0){return line;}
219
220 final int trimStop=Tools.indexOf(line, (byte)'\t', trimStart+1);
221 final int bbLen=trimStart+(trimStop<0 ? 0 : line.length-trimStop);
222 final ByteBuilder bb=new ByteBuilder(bbLen);
223 for(int i=0; i<trimStart; i++){bb.append(line[i]);}
224 if(trimStop>=0){
225 for(int i=trimStop; i<line.length; i++){bb.append(line[i]);}
226 }
227 assert(bb.length==bbLen) : bbLen+", "+bb.length+", idx="+idx+", trimStart="+trimStart+", trimStop="+trimStop+"\n\n"+new String(line)+"\n\n"+bb+"\n\n";
228
229 return bb.array;
230 }
231
232 @Override
233 public String fname(){return tf.name();}
234
235 private static volatile ArrayList<byte[]> SHARED_HEADER;
236 // private static boolean SET_SHARED_HEADER;
237
238 @Override
239 public boolean paired() {return interleaved;}
240
241 private ArrayList<Read> buffer=null;
242 private ArrayList<byte[]> header=null;
243 private int next=0;
244
245 private final ByteFile tf;
246 private final boolean interleaved;
247 private final boolean loadHeader;
248
249 private final int BUF_LEN=Shared.bufferLen();;
250
251 public long generated=0;
252 public long consumed=0;
253 private long nextReadID=0;
254
255 public final boolean stdin;
256
257 }