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