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