Debian Codegraph @

current/stream/GbkReadInputStream.java

raw symbols SCIP
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 GbkReadInputStream extends ReadInputStream {
13
14 public static void main(String[] args){
15
16 GbkReadInputStream fris=new GbkReadInputStream(args[0], true);
17
18 Read r=fris.next();
19 System.out.println(r.toText(false));
20
21 }
22
23 public GbkReadInputStream(String fname, boolean allowSubprocess_){
24 this(FileFormat.testInput(fname, FileFormat.GBK, null, allowSubprocess_, false));
25 }
26
27 public GbkReadInputStream(FileFormat ff){
28 if(verbose){System.err.println("FastqReadInputStream("+ff+")");}
29 flag=(Shared.AMINO_IN ? Read.AAMASK : 0);
30 stdin=ff.stdio();
31 if(!ff.gbk()){
32 System.err.println("Warning: Did not find expected fastq file extension for filename "+ff.name());
33 }
34 bf=ByteFile.makeByteFile(ff);
35 // assert(false) : interleaved;
36 }
37
38 @Override
39 public void start() {
40 // if(cris!=null){cris.start();}
41 }
42
43
44 @Override
45 public boolean hasMore() {
46 if(buffer==null || next>=buffer.size()){
47 if(bf.isOpen()){
48 fillBuffer();
49 }else{
50 assert(generated>0) : "Was the file empty?";
51 }
52 }
53 return (buffer!=null && next<buffer.size());
54 }
55
56 @Override
57 public Read next() {
58 if(!hasMore()){return null;}
59 Read r=buffer.set(next, null);
60 next++;
61 consumed++;
62 return r;
63 }
64
65 @Override
66 public synchronized ArrayList<Read> nextList() {
67 if(next!=0){throw new RuntimeException("'next' should not be used when doing blockwise access.");}
68 if(buffer==null || next>=buffer.size()){fillBuffer();}
69 ArrayList<Read> list=buffer;
70 buffer=null;
71 if(list!=null && list.size()==0){list=null;}
72 consumed+=(list==null ? 0 : list.size());
73 return list;
74 }
75
76 private synchronized void fillBuffer(){
77
78 assert(buffer==null || next>=buffer.size());
79
80 buffer=null;
81 next=0;
82 buffer=toReadList(bf, BUF_LEN, nextReadID, flag);
83 int bsize=(buffer==null ? 0 : buffer.size());
84 nextReadID+=bsize;
85 if(bsize<BUF_LEN){bf.close();}
86
87 generated+=bsize;
88 if(buffer==null){
89 if(!errorState){
90 errorState=true;
91 System.err.println("Null buffer in FastqReadInputStream.");
92 }
93 }
94 }
95
96
97
98 public static ArrayList<Read> toReadList(final ByteFile bf, final int maxReadsToReturn, long numericID, final int flag){
99 ArrayList<Read> list=new ArrayList<Read>(Data.min(8192, maxReadsToReturn));
100
101 int added=0;
102
103 String idLine=null;
104 ByteBuilder bb=new ByteBuilder();
105 for(byte[] s=bf.nextLine(); s!=null; s=bf.nextLine()){
106 // if(Tools.startsWith(s, "ID")){
107 // idLine=new String(s, 2, s.length-2).trim();
108 // }else
109 if(Tools.startsWith(s, "ORIGIN")){
110 // System.err.println(new String(s));
111 byte[] line=null;
112 for(line=bf.nextLine(); line!=null && line[0]!='/'; line=bf.nextLine()){
113 for(byte b : line){
114 if(Tools.isLetter(b)){
115 bb.append(Tools.toUpperCase(b));
116 }
117 }
118 }
119 assert(line==null || Tools.startsWith(line, "//")) : new String(line);
120
121 Read r=new Read(bb.toBytes(), null, idLine==null ? ""+numericID : idLine, numericID, flag);
122 list.add(r);
123 added++;
124 numericID++;
125
126 bb.clear();
127 idLine=null;
128
129 if(added>=maxReadsToReturn){break;}
130 }
131 }
132 assert(list.size()<=maxReadsToReturn);
133 return list;
134 }
135
136 @Override
137 public boolean close(){
138 if(verbose){System.err.println("Closing "+this.getClass().getName()+" for "+bf.name()+"; errorState="+errorState);}
139 errorState|=bf.close();
140 if(verbose){System.err.println("Closed "+this.getClass().getName()+" for "+bf.name()+"; errorState="+errorState);}
141 return errorState;
142 }
143
144 @Override
145 public synchronized void restart() {
146 generated=0;
147 consumed=0;
148 next=0;
149 nextReadID=0;
150 buffer=null;
151 bf.reset();
152 }
153
154 @Override
155 public boolean paired() {return false;}
156
157 @Override
158 public String fname(){return bf.name();}
159
160 /** Return true if this stream has detected an error */
161 @Override
162 public boolean errorState(){return errorState || FASTQ.errorState();}
163
164 private ArrayList<Read> buffer=null;
165 private int next=0;
166
167 private final ByteFile bf;
168 private final int flag;
169
170 private final int BUF_LEN=Shared.bufferLen();;
171 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.
172
173 public long generated=0;
174 public long consumed=0;
175 private long nextReadID=0;
176
177 public final boolean stdin;
178 public static boolean verbose=false;
179
180 }