| 1 |
package stream; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
|
| 5 |
import dna.Data; |
| 6 |
import fileIO.ByteFile; |
| 7 |
import fileIO.FileFormat; |
| 8 |
import shared.Shared; |
| 9 |
import shared.Tools; |
| 10 |
import structures.ByteBuilder; |
| 11 |
|
| 12 |
public class FastaQualReadInputStream extends ReadInputStream { |
| 13 |
|
| 14 |
public static void main(String[] args){ |
| 15 |
|
| 16 |
FastaQualReadInputStream fris=new FastaQualReadInputStream(args[0], args[1], true); |
| 17 |
|
| 18 |
Read r=fris.next(); |
| 19 |
int i=0; |
| 20 |
while(r!=null){ |
| 21 |
System.out.println(r.toText(false)); |
| 22 |
r=fris.next(); |
| 23 |
if(i++>3){break;} |
| 24 |
} |
| 25 |
|
| 26 |
} |
| 27 |
|
| 28 |
public FastaQualReadInputStream(String fname, String qfname, boolean allowSubprocess_){ |
| 29 |
this(FileFormat.testInput(fname, FileFormat.FASTA, null, allowSubprocess_, false), qfname); |
| 30 |
} |
| 31 |
|
| 32 |
public FastaQualReadInputStream(FileFormat ff, String qfname){ |
| 33 |
|
| 34 |
if(!ff.fasta() && !ff.stdio()){ |
| 35 |
System.err.println("Warning: Did not find expected fasta file extension for filename "+ff.name()); |
| 36 |
} |
| 37 |
|
| 38 |
btf=ByteFile.makeByteFile(ff); |
| 39 |
qtf=ByteFile.makeByteFile(FileFormat.testInput(qfname, FileFormat.QUAL, null, ff.allowSubprocess(), false)); |
| 40 |
interleaved=false; |
| 41 |
|
| 42 |
} |
| 43 |
|
| 44 |
@Override |
| 45 |
public void start() { |
| 46 |
|
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
@Override |
| 51 |
public boolean hasMore() { |
| 52 |
if(buffer==null || next>=buffer.size()){ |
| 53 |
if(btf.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()){ |
| 65 |
if(verbose){System.err.println("hasMore() returned false; buffer="+(buffer==null ? null : buffer.size())+", next="+next+", consumed="+consumed);} |
| 66 |
return null; |
| 67 |
} |
| 68 |
Read r=buffer.set(next, null); |
| 69 |
next++; |
| 70 |
consumed++; |
| 71 |
return r; |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public synchronized ArrayList<Read> nextList() { |
| 76 |
if(next!=0){throw new RuntimeException("'next' should not be used when doing blockwise access.");} |
| 77 |
if(buffer==null || next>=buffer.size()){fillBuffer();} |
| 78 |
ArrayList<Read> r=buffer; |
| 79 |
buffer=null; |
| 80 |
if(r!=null && r.size()==0){r=null;} |
| 81 |
consumed+=(r==null ? 0 : r.size()); |
| 82 |
|
| 83 |
return r; |
| 84 |
} |
| 85 |
|
| 86 |
private synchronized void fillBuffer(){ |
| 87 |
if(builder==null){builder=new ByteBuilder(2000);} |
| 88 |
if(verbose){System.err.println("Filling buffer. buffer="+(buffer==null ? null : buffer.size()));} |
| 89 |
assert(buffer==null || next>=buffer.size()); |
| 90 |
|
| 91 |
buffer=null; |
| 92 |
next=0; |
| 93 |
|
| 94 |
buffer=toReads(BUF_LEN, nextReadID, interleaved); |
| 95 |
final int count=(buffer==null ? 0 : buffer.size()); |
| 96 |
|
| 97 |
if(verbose){System.err.println("Filled buffer. size="+count);} |
| 98 |
|
| 99 |
nextReadID+=count; |
| 100 |
if(count<BUF_LEN){ |
| 101 |
if(verbose){System.err.println("Closing tf");} |
| 102 |
errorState|=close(); |
| 103 |
} |
| 104 |
|
| 105 |
generated+=count; |
| 106 |
if(verbose){System.err.println("generated="+generated);} |
| 107 |
} |
| 108 |
|
| 109 |
private ArrayList<Read> toReads(int maxReadsToReturn, long numericID, boolean interleaved){ |
| 110 |
ArrayList<Read> list=toReadList(maxReadsToReturn, numericID, interleaved); |
| 111 |
if(list==null){assert(finished);} |
| 112 |
else{assert(list.size()<=maxReadsToReturn);} |
| 113 |
return list; |
| 114 |
} |
| 115 |
|
| 116 |
private ArrayList<Read> toReadList(int maxReadsToReturn, long numericID, boolean interleaved){ |
| 117 |
if(finished){return null;} |
| 118 |
if(verbose){System.err.println("FastaQualRIS fetching a list.");} |
| 119 |
|
| 120 |
if(currentHeader==null && numericID==0){ |
| 121 |
|
| 122 |
nextBases(btf, builder); |
| 123 |
nextQualities(qtf, builder); |
| 124 |
if(nextHeaderB==null){ |
| 125 |
finish(); |
| 126 |
return null; |
| 127 |
} |
| 128 |
assert(Tools.equals(nextHeaderB, nextHeaderQ)) : "Quality and Base headers differ for read "+numericID; |
| 129 |
currentHeader=nextHeaderB; |
| 130 |
nextHeaderB=nextHeaderQ=null; |
| 131 |
if(currentHeader==null){ |
| 132 |
finish(); |
| 133 |
return null; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
ArrayList<Read> list=new ArrayList<Read>(Data.min(1000, maxReadsToReturn)); |
| 138 |
|
| 139 |
int added=0; |
| 140 |
|
| 141 |
Read prev=null; |
| 142 |
|
| 143 |
while(added<maxReadsToReturn){ |
| 144 |
Read r=makeRead(numericID); |
| 145 |
if(verbose){System.err.println("Made "+r);} |
| 146 |
if(r==null){ |
| 147 |
finish(); |
| 148 |
if(verbose){System.err.println("makeRead returned null.");} |
| 149 |
break; |
| 150 |
} |
| 151 |
if(interleaved){ |
| 152 |
if(prev==null){prev=r;} |
| 153 |
else{ |
| 154 |
prev.mate=r; |
| 155 |
r.mate=prev; |
| 156 |
list.add(prev); |
| 157 |
added++; |
| 158 |
numericID++; |
| 159 |
prev=null; |
| 160 |
} |
| 161 |
}else{ |
| 162 |
list.add(r); |
| 163 |
added++; |
| 164 |
numericID++; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
assert(list.size()<=maxReadsToReturn); |
| 169 |
if(verbose){System.err.println("FastaQualRIS returning a list. Size="+list.size());} |
| 170 |
return list; |
| 171 |
} |
| 172 |
|
| 173 |
private final byte[] nextBases(ByteFile btf, ByteBuilder bb){ |
| 174 |
assert(bb.length()==0); |
| 175 |
byte[] line=btf.nextLine(); |
| 176 |
while(line!=null && (line.length==0 || line[0]!=carrot)){ |
| 177 |
bb.append(line); |
| 178 |
line=btf.nextLine(); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
if(line==null){ |
| 183 |
|
| 184 |
}else{ |
| 185 |
assert(line.length>0); |
| 186 |
assert(line[0]==carrot); |
| 187 |
nextHeaderB=line; |
| 188 |
} |
| 189 |
final byte[] r=bb.toBytes(); |
| 190 |
bb.setLength(0); |
| 191 |
|
| 192 |
return r; |
| 193 |
} |
| 194 |
|
| 195 |
private final byte[] nextQualities(ByteFile qtf, ByteBuilder bb){ |
| 196 |
assert(bb.length()==0); |
| 197 |
byte[] line=qtf.nextLine(); |
| 198 |
while(line!=null && (line.length==0 || line[0]!=carrot)){ |
| 199 |
if(NUMERIC_QUAL && line.length>0){ |
| 200 |
int x=0; |
| 201 |
for(int i=0; i<line.length; i++){ |
| 202 |
byte b=line[i]; |
| 203 |
if(b==space){ |
| 204 |
assert(i>0); |
| 205 |
bb.append((byte)x); |
| 206 |
x=0; |
| 207 |
}else{ |
| 208 |
x=10*x+(b-zero); |
| 209 |
} |
| 210 |
} |
| 211 |
bb.append((byte)x); |
| 212 |
}else{ |
| 213 |
for(byte b : line){bb.append((byte)(b-FASTQ.ASCII_OFFSET));} |
| 214 |
} |
| 215 |
line=qtf.nextLine(); |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
if(line==null){ |
| 220 |
|
| 221 |
}else{ |
| 222 |
assert(line.length>0); |
| 223 |
assert(line[0]==carrot); |
| 224 |
nextHeaderQ=line; |
| 225 |
} |
| 226 |
final byte[] r=bb.toBytes(); |
| 227 |
bb.setLength(0); |
| 228 |
|
| 229 |
return r; |
| 230 |
} |
| 231 |
|
| 232 |
private Read makeRead(long numericID){ |
| 233 |
if(finished){ |
| 234 |
if(verbose){System.err.println("Returning null because finished.");} |
| 235 |
return null; |
| 236 |
} |
| 237 |
if(currentHeader==null){return null;} |
| 238 |
assert(nextHeaderB==null); |
| 239 |
assert(nextHeaderQ==null); |
| 240 |
|
| 241 |
final byte[] bases=nextBases(btf, builder); |
| 242 |
final byte[] quals=nextQualities(qtf, builder); |
| 243 |
final byte[] header=currentHeader; |
| 244 |
|
| 245 |
currentHeader=nextHeaderB; |
| 246 |
nextHeaderB=nextHeaderQ=null; |
| 247 |
|
| 248 |
if(bases==null){ |
| 249 |
if(verbose){System.err.println("Returning null because tf.nextLine()==null: A");} |
| 250 |
return null; |
| 251 |
} |
| 252 |
|
| 253 |
assert(bases.length==quals.length) : |
| 254 |
"\nFor sequence "+numericID+", name "+new String(header)+":\n" + |
| 255 |
"The bases and quality scores are different lengths, "+bases.length+" and "+quals.length; |
| 256 |
|
| 257 |
for(int i=0; i<bases.length; i++){ |
| 258 |
bases[i]=(byte)Tools.toUpperCase(bases[i]); |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
assert(bases[0]!=carrot) : new String(bases)+"\n"+numericID+"\n"+header[0]; |
| 265 |
String hd=new String(header, 1, header.length-1); |
| 266 |
Read r=new Read(bases, quals, hd, numericID); |
| 267 |
return r; |
| 268 |
} |
| 269 |
|
| 270 |
@Override |
| 271 |
public synchronized boolean close(){ |
| 272 |
if(closed){return errorState;} |
| 273 |
if(verbose){System.err.println("FastaQualRIS closing.");} |
| 274 |
|
| 275 |
builder=null; |
| 276 |
finish(); |
| 277 |
boolean a=btf.close(); |
| 278 |
boolean b=qtf.close(); |
| 279 |
closed=true; |
| 280 |
return a|b; |
| 281 |
} |
| 282 |
|
| 283 |
@Override |
| 284 |
public synchronized void restart() { |
| 285 |
if(verbose){System.err.println("FastaQualRIS restarting.");} |
| 286 |
generated=0; |
| 287 |
consumed=0; |
| 288 |
next=0; |
| 289 |
nextReadID=0; |
| 290 |
|
| 291 |
finished=false; |
| 292 |
closed=false; |
| 293 |
|
| 294 |
buffer=null; |
| 295 |
nextHeaderB=null; |
| 296 |
nextHeaderQ=null; |
| 297 |
currentHeader=null; |
| 298 |
builder=null; |
| 299 |
|
| 300 |
btf.reset(); |
| 301 |
qtf.reset(); |
| 302 |
} |
| 303 |
|
| 304 |
@Override |
| 305 |
public boolean paired() { |
| 306 |
return interleaved; |
| 307 |
} |
| 308 |
|
| 309 |
private synchronized void finish(){ |
| 310 |
if(verbose){System.err.println("FastaQualRIS setting finished "+finished+" -> "+true);} |
| 311 |
finished=true; |
| 312 |
} |
| 313 |
|
| 314 |
@Override |
| 315 |
public String fname(){return btf.name()+","+qtf.name();} |
| 316 |
|
| 317 |
private ArrayList<Read> buffer=null; |
| 318 |
private int next=0; |
| 319 |
|
| 320 |
private final ByteFile btf; |
| 321 |
private final ByteFile qtf; |
| 322 |
private final boolean interleaved; |
| 323 |
|
| 324 |
private final int BUF_LEN=Shared.bufferLen();; |
| 325 |
|
| 326 |
public long generated=0; |
| 327 |
public long consumed=0; |
| 328 |
private long nextReadID=0; |
| 329 |
|
| 330 |
public static boolean NUMERIC_QUAL=true; |
| 331 |
|
| 332 |
public static boolean verbose=false; |
| 333 |
|
| 334 |
private byte[] nextHeaderB=null; |
| 335 |
private byte[] nextHeaderQ=null; |
| 336 |
|
| 337 |
private byte[] currentHeader=null; |
| 338 |
|
| 339 |
private ByteBuilder builder=null; |
| 340 |
|
| 341 |
private boolean finished=false, closed=false; |
| 342 |
private final byte carrot='>', space=' ', zero='0'; |
| 343 |
|
| 344 |
} |