jpayne@68
|
1 package bloom;
|
jpayne@68
|
2
|
jpayne@68
|
3 import java.util.ArrayList;
|
jpayne@68
|
4 import java.util.Locale;
|
jpayne@68
|
5
|
jpayne@68
|
6 import dna.AminoAcid;
|
jpayne@68
|
7 import fileIO.FileFormat;
|
jpayne@68
|
8 import shared.Timer;
|
jpayne@68
|
9 import stream.ConcurrentReadInputStream;
|
jpayne@68
|
10 import stream.FastaReadInputStream;
|
jpayne@68
|
11 import stream.Read;
|
jpayne@68
|
12 import structures.ListNum;
|
jpayne@68
|
13
|
jpayne@68
|
14 /**
|
jpayne@68
|
15 * @author Brian Bushnell
|
jpayne@68
|
16 * @date Jul 5, 2012
|
jpayne@68
|
17 *
|
jpayne@68
|
18 */
|
jpayne@68
|
19 public class KmerCount4 extends KmerCountAbstract {
|
jpayne@68
|
20
|
jpayne@68
|
21 public static void main(String[] args){
|
jpayne@68
|
22
|
jpayne@68
|
23 Timer t=new Timer();
|
jpayne@68
|
24
|
jpayne@68
|
25 String fname1=args[0];
|
jpayne@68
|
26 String fname2=(args.length>3 || args[1].contains(".") ? args[1] : null);
|
jpayne@68
|
27 int k=14;
|
jpayne@68
|
28 int cbits=16;
|
jpayne@68
|
29 int gap=0;
|
jpayne@68
|
30
|
jpayne@68
|
31 for(int i=(fname2==null ? 1 : 2); i<args.length; i++){
|
jpayne@68
|
32 final String arg=args[i];
|
jpayne@68
|
33 final String[] split=arg.split("=");
|
jpayne@68
|
34 String a=split[0].toLowerCase();
|
jpayne@68
|
35 String b=split.length>1 ? split[1] : null;
|
jpayne@68
|
36
|
jpayne@68
|
37 if(a.equals("k") || a.equals("kmer")){
|
jpayne@68
|
38 k=Integer.parseInt(b);
|
jpayne@68
|
39 }else if(a.startsWith("cbits") || a.startsWith("cellbits")){
|
jpayne@68
|
40 cbits=Integer.parseInt(b);
|
jpayne@68
|
41 }else if(a.startsWith("gap")){
|
jpayne@68
|
42 gap=Integer.parseInt(b);
|
jpayne@68
|
43 }else{
|
jpayne@68
|
44 throw new RuntimeException("Unknown parameter "+args[i]);
|
jpayne@68
|
45 }
|
jpayne@68
|
46 }
|
jpayne@68
|
47
|
jpayne@68
|
48 KCountArray2 count=null;
|
jpayne@68
|
49
|
jpayne@68
|
50 if(fileIO.FileFormat.hasFastaExtension(fname1)){
|
jpayne@68
|
51 assert(!FastaReadInputStream.SPLIT_READS);
|
jpayne@68
|
52 FastaReadInputStream.MIN_READ_LEN=k;
|
jpayne@68
|
53 }
|
jpayne@68
|
54
|
jpayne@68
|
55 if(gap==0){
|
jpayne@68
|
56 count=count(fname1, fname2, k, cbits, true);
|
jpayne@68
|
57 }else{
|
jpayne@68
|
58 count=countFastqSplit(fname1, fname2, (k+1)/2, k/2, gap, cbits, true, null);
|
jpayne@68
|
59 }
|
jpayne@68
|
60
|
jpayne@68
|
61
|
jpayne@68
|
62 t.stop();
|
jpayne@68
|
63 System.out.println("Finished counting; time = "+t);
|
jpayne@68
|
64
|
jpayne@68
|
65 printStatistics(count);
|
jpayne@68
|
66
|
jpayne@68
|
67 }
|
jpayne@68
|
68
|
jpayne@68
|
69 public static void printStatistics(KCountArray2 count){
|
jpayne@68
|
70 long[] freq=count.transformToFrequency();
|
jpayne@68
|
71
|
jpayne@68
|
72 // System.out.println(count+"\n");
|
jpayne@68
|
73 // System.out.println(Arrays.toString(freq)+"\n");
|
jpayne@68
|
74
|
jpayne@68
|
75 long sum=sum(freq);
|
jpayne@68
|
76 System.out.println("Kmer fraction:");
|
jpayne@68
|
77 int lim1=8, lim2=16;
|
jpayne@68
|
78 for(int i=0; i<lim1; i++){
|
jpayne@68
|
79 String prefix=i+"";
|
jpayne@68
|
80 while(prefix.length()<8){prefix=prefix+" ";}
|
jpayne@68
|
81 System.out.println(prefix+"\t"+String.format(Locale.ROOT, "%.3f%% ",(100l*freq[i]/(double)sum))+"\t"+freq[i]);
|
jpayne@68
|
82 }
|
jpayne@68
|
83 while(lim1<=freq.length){
|
jpayne@68
|
84 int x=0;
|
jpayne@68
|
85 for(int i=lim1; i<lim2; i++){
|
jpayne@68
|
86 x+=freq[i];
|
jpayne@68
|
87 }
|
jpayne@68
|
88 String prefix=lim1+"-"+(lim2-1);
|
jpayne@68
|
89 if(lim2>=freq.length){prefix=lim1+"+";}
|
jpayne@68
|
90 while(prefix.length()<8){prefix=prefix+" ";}
|
jpayne@68
|
91 System.out.println(prefix+"\t"+String.format(Locale.ROOT, "%.3f%% ",(100l*x/(double)sum))+"\t"+x);
|
jpayne@68
|
92 lim1*=2;
|
jpayne@68
|
93 lim2=min(lim2*2, freq.length);
|
jpayne@68
|
94 }
|
jpayne@68
|
95
|
jpayne@68
|
96 long sum2=sum-freq[0];
|
jpayne@68
|
97 long x=freq[1];
|
jpayne@68
|
98 System.out.println();
|
jpayne@68
|
99 System.out.println("Keys Counted: \t \t"+keysCounted);
|
jpayne@68
|
100 System.out.println("Unique: \t \t"+sum2);
|
jpayne@68
|
101 System.out.println("Avg Sites/Key: \t \t"+String.format(Locale.ROOT, "%.3f ",(keysCounted*1d/sum2)));
|
jpayne@68
|
102 System.out.println();
|
jpayne@68
|
103 System.out.println("Singleton: \t"+String.format(Locale.ROOT, "%.3f%% ",(100l*x/(double)sum2))+"\t"+x);
|
jpayne@68
|
104 x=sum2-x;
|
jpayne@68
|
105 System.out.println("Useful: \t"+String.format(Locale.ROOT, "%.3f%% ",(100l*x/(double)sum2))+"\t"+x);
|
jpayne@68
|
106 }
|
jpayne@68
|
107
|
jpayne@68
|
108 public static KCountArray2 count(String reads1, String reads2, int k, int cbits, boolean rcomp){
|
jpayne@68
|
109 return count(reads1, reads2, k, cbits, rcomp, null);
|
jpayne@68
|
110 }
|
jpayne@68
|
111
|
jpayne@68
|
112 public static KCountArray2 count(String reads1, String reads2, int k, int cbits, boolean rcomp, KCountArray2 count){
|
jpayne@68
|
113 assert(k>=1 && k<20);
|
jpayne@68
|
114 final int kbits=2*k;
|
jpayne@68
|
115 final long mask=(kbits>63 ? -1L : ~((-1L)<<kbits));
|
jpayne@68
|
116
|
jpayne@68
|
117 if(count==null){
|
jpayne@68
|
118 final long cells=1L<<kbits;
|
jpayne@68
|
119 if(verbose){System.err.println("k="+k+", kbits="+kbits+", cells="+cells+", mask="+Long.toHexString(mask));}
|
jpayne@68
|
120 count=new KCountArray2(cells, cbits);
|
jpayne@68
|
121 }
|
jpayne@68
|
122
|
jpayne@68
|
123 final ConcurrentReadInputStream cris;
|
jpayne@68
|
124 {
|
jpayne@68
|
125 FileFormat ff1=FileFormat.testInput(reads1, FileFormat.FASTQ, null, true, true);
|
jpayne@68
|
126 FileFormat ff2=FileFormat.testInput(reads2, FileFormat.FASTQ, null, true, true);
|
jpayne@68
|
127 cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ff1, ff2);
|
jpayne@68
|
128 cris.start(); //4567
|
jpayne@68
|
129 }
|
jpayne@68
|
130
|
jpayne@68
|
131 assert(cris!=null) : reads1;
|
jpayne@68
|
132 System.err.println("Started cris");
|
jpayne@68
|
133 boolean paired=cris.paired();
|
jpayne@68
|
134 if(verbose){System.err.println("Paired: "+paired);}
|
jpayne@68
|
135
|
jpayne@68
|
136 ListNum<Read> ln=cris.nextList();
|
jpayne@68
|
137 ArrayList<Read> reads=(ln!=null ? ln.list : null);
|
jpayne@68
|
138
|
jpayne@68
|
139 if(reads!=null && !reads.isEmpty()){
|
jpayne@68
|
140 Read r=reads.get(0);
|
jpayne@68
|
141 assert(paired==(r.mate!=null));
|
jpayne@68
|
142 }
|
jpayne@68
|
143
|
jpayne@68
|
144 while(ln!=null && reads!=null && reads.size()>0){//ln!=null prevents a compiler potential null access warning
|
jpayne@68
|
145 //System.err.println("reads.size()="+reads.size());
|
jpayne@68
|
146 for(Read r : reads){
|
jpayne@68
|
147 readsProcessed++;
|
jpayne@68
|
148
|
jpayne@68
|
149 addRead(r, count, k, mask, rcomp);
|
jpayne@68
|
150 if(r.mate!=null){
|
jpayne@68
|
151 addRead(r.mate, count, k, mask, rcomp);
|
jpayne@68
|
152 }
|
jpayne@68
|
153
|
jpayne@68
|
154 }
|
jpayne@68
|
155 //System.err.println("returning list");
|
jpayne@68
|
156 cris.returnList(ln);
|
jpayne@68
|
157 //System.err.println("fetching list");
|
jpayne@68
|
158 ln=cris.nextList();
|
jpayne@68
|
159 reads=(ln!=null ? ln.list : null);
|
jpayne@68
|
160 }
|
jpayne@68
|
161 if(verbose){System.err.println("Finished reading");}
|
jpayne@68
|
162 cris.returnList(ln);
|
jpayne@68
|
163 if(verbose){System.err.println("Returned list");}
|
jpayne@68
|
164 cris.close();
|
jpayne@68
|
165 if(verbose){System.err.println("Closed stream");}
|
jpayne@68
|
166 if(verbose){System.err.println("Processed "+readsProcessed+" reads.");}
|
jpayne@68
|
167
|
jpayne@68
|
168
|
jpayne@68
|
169 return count;
|
jpayne@68
|
170 }
|
jpayne@68
|
171
|
jpayne@68
|
172 public static KCountArray2 countFastqSplit(String reads1, String reads2, int k1, int k2, int gap, int cbits, boolean rcomp, KCountArray2 count){
|
jpayne@68
|
173 assert(k1+k2>=1 && k1+k2<20);
|
jpayne@68
|
174 assert(gap>=0);
|
jpayne@68
|
175 final int kbits1=2*k1;
|
jpayne@68
|
176 final int kbits2=2*k2;
|
jpayne@68
|
177 final long mask1=~((-1L)<<(kbits1));
|
jpayne@68
|
178 final long mask2=~((-1L)<<(kbits2));
|
jpayne@68
|
179
|
jpayne@68
|
180 if(count==null){
|
jpayne@68
|
181 final long cells=1L<<(kbits1+kbits2);
|
jpayne@68
|
182 if(verbose){System.err.println("k1="+k1+", k2="+k2+", kbits1="+kbits1+", kbits2="+kbits2+", cells="+cells+
|
jpayne@68
|
183 ", mask1="+Long.toHexString(mask1)+", mask2="+Long.toHexString(mask2));}
|
jpayne@68
|
184 count=new KCountArray2(cells, cbits, gap);
|
jpayne@68
|
185 }
|
jpayne@68
|
186 assert(count.gap==gap);
|
jpayne@68
|
187
|
jpayne@68
|
188 final ConcurrentReadInputStream cris;
|
jpayne@68
|
189 {
|
jpayne@68
|
190 FileFormat ff1=FileFormat.testInput(reads1, FileFormat.FASTQ, null, true, true);
|
jpayne@68
|
191 FileFormat ff2=FileFormat.testInput(reads2, FileFormat.FASTQ, null, true, true);
|
jpayne@68
|
192 cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ff1, ff2);
|
jpayne@68
|
193 cris.start(); //4567
|
jpayne@68
|
194 }
|
jpayne@68
|
195
|
jpayne@68
|
196 assert(cris!=null) : reads1;
|
jpayne@68
|
197 System.err.println("Started cris");
|
jpayne@68
|
198 boolean paired=cris.paired();
|
jpayne@68
|
199 if(verbose){System.err.println("Paired: "+paired);}
|
jpayne@68
|
200
|
jpayne@68
|
201 ListNum<Read> ln=cris.nextList();
|
jpayne@68
|
202 ArrayList<Read> reads=(ln!=null ? ln.list : null);
|
jpayne@68
|
203
|
jpayne@68
|
204 if(reads!=null && !reads.isEmpty()){
|
jpayne@68
|
205 Read r=reads.get(0);
|
jpayne@68
|
206 assert(paired==(r.mate!=null));
|
jpayne@68
|
207 }
|
jpayne@68
|
208
|
jpayne@68
|
209 while(ln!=null && reads!=null && reads.size()>0){//ln!=null prevents a compiler potential null access warning
|
jpayne@68
|
210 //System.err.println("reads.size()="+reads.size());
|
jpayne@68
|
211 for(Read r : reads){
|
jpayne@68
|
212 readsProcessed++;
|
jpayne@68
|
213
|
jpayne@68
|
214 addReadSplit(r, count, k1, k2, mask1, mask2, gap, rcomp);
|
jpayne@68
|
215 if(r.mate!=null){
|
jpayne@68
|
216 addReadSplit(r.mate, count, k1, k2, mask1, mask2, gap, rcomp);
|
jpayne@68
|
217 }
|
jpayne@68
|
218
|
jpayne@68
|
219 }
|
jpayne@68
|
220 //System.err.println("returning list");
|
jpayne@68
|
221 cris.returnList(ln);
|
jpayne@68
|
222 //System.err.println("fetching list");
|
jpayne@68
|
223 ln=cris.nextList();
|
jpayne@68
|
224 reads=(ln!=null ? ln.list : null);
|
jpayne@68
|
225 }
|
jpayne@68
|
226 if(verbose){System.err.println("Finished reading");}
|
jpayne@68
|
227 cris.returnList(ln);
|
jpayne@68
|
228 if(verbose){System.err.println("Returned list");}
|
jpayne@68
|
229 cris.close();
|
jpayne@68
|
230 if(verbose){System.err.println("Closed stream");}
|
jpayne@68
|
231 if(verbose){System.err.println("Processed "+readsProcessed+" reads.");}
|
jpayne@68
|
232
|
jpayne@68
|
233
|
jpayne@68
|
234 return count;
|
jpayne@68
|
235 }
|
jpayne@68
|
236
|
jpayne@68
|
237 public static void addRead(final Read r, final KCountArray2 count, final int k, final long mask, boolean rcomp){
|
jpayne@68
|
238 int len=0;
|
jpayne@68
|
239 long kmer=0;
|
jpayne@68
|
240 byte[] bases=r.bases;
|
jpayne@68
|
241 byte[] quals=r.quality;
|
jpayne@68
|
242 for(int i=0; i<bases.length; i++){
|
jpayne@68
|
243 byte b=bases[i];
|
jpayne@68
|
244 int x=AminoAcid.baseToNumber[b];
|
jpayne@68
|
245 if(x<0 || (quals!=null && quals[i]<minQuality)){
|
jpayne@68
|
246 len=0;
|
jpayne@68
|
247 kmer=0;
|
jpayne@68
|
248 }else{
|
jpayne@68
|
249 kmer=((kmer<<2)|x)&mask;
|
jpayne@68
|
250 len++;
|
jpayne@68
|
251 if(len>=k){
|
jpayne@68
|
252 keysCounted++;
|
jpayne@68
|
253 // System.out.print("Incrementing "+Long.toHexString(kmer)+": "+count.read(kmer));
|
jpayne@68
|
254 count.increment(kmer, 1);
|
jpayne@68
|
255 // System.out.println(" -> "+count.read(kmer));
|
jpayne@68
|
256 // System.out.print("Incrementing array for "+Long.toHexString(kmer)+": "+array[(int)kmer]);
|
jpayne@68
|
257 // array[(int)kmer]++;
|
jpayne@68
|
258 // System.out.println(" -> "+array[(int)kmer]+"\n");
|
jpayne@68
|
259 // assert(array[(int)kmer]==count.read(kmer) || array[(int)kmer]>3);
|
jpayne@68
|
260 }
|
jpayne@68
|
261 }
|
jpayne@68
|
262 }
|
jpayne@68
|
263 if(rcomp){
|
jpayne@68
|
264 r.reverseComplement();
|
jpayne@68
|
265 addRead(r, count, k, mask, false);
|
jpayne@68
|
266 }
|
jpayne@68
|
267 }
|
jpayne@68
|
268
|
jpayne@68
|
269 public static void addReadSplit(final Read r, final KCountArray2 count, final int k1, final int k2, final long mask1, final long mask2, final int gap, boolean rcomp){
|
jpayne@68
|
270 int len=0;
|
jpayne@68
|
271 int shift=k2*2;
|
jpayne@68
|
272 long kmer1=0;
|
jpayne@68
|
273 long kmer2=0;
|
jpayne@68
|
274 byte[] bases=r.bases;
|
jpayne@68
|
275 byte[] quals=r.quality;
|
jpayne@68
|
276
|
jpayne@68
|
277 assert(kmer1>=kmer2);
|
jpayne@68
|
278
|
jpayne@68
|
279 // assert(false) : k1+", "+k2+", "+mask1+", "+mask2+", "+gap;
|
jpayne@68
|
280
|
jpayne@68
|
281 for(int i=0, j=i+k1+gap; j<bases.length; i++, j++){
|
jpayne@68
|
282 int x1=AminoAcid.baseToNumber[bases[i]];
|
jpayne@68
|
283 int x2=AminoAcid.baseToNumber[bases[j]];
|
jpayne@68
|
284 if(x1<0 || x2<0 || (quals!=null && (quals[i]<minQuality || quals[j]<minQuality))){
|
jpayne@68
|
285 len=0;
|
jpayne@68
|
286 kmer1=0;
|
jpayne@68
|
287 kmer2=0;
|
jpayne@68
|
288 }else{
|
jpayne@68
|
289 kmer1=((kmer1<<2)|x1)&mask1;
|
jpayne@68
|
290 kmer2=((kmer2<<2)|x2)&mask2;
|
jpayne@68
|
291 len++;
|
jpayne@68
|
292 if(len>=k1){
|
jpayne@68
|
293 keysCounted++;
|
jpayne@68
|
294 // System.out.print("Incrementing "+Long.toHexString(kmer)+": "+count.read(kmer));
|
jpayne@68
|
295
|
jpayne@68
|
296 long key=(kmer1<<shift)|kmer2;
|
jpayne@68
|
297 // System.err.println(Long.toHexString(key));
|
jpayne@68
|
298 count.increment(key, 1);
|
jpayne@68
|
299 // System.out.println(" -> "+count.read(kmer));
|
jpayne@68
|
300 // System.out.print("Incrementing array for "+Long.toHexString(kmer)+": "+array[(int)kmer]);
|
jpayne@68
|
301 // array[(int)kmer]++;
|
jpayne@68
|
302 // System.out.println(" -> "+array[(int)kmer]+"\n");
|
jpayne@68
|
303 // assert(array[(int)kmer]==count.read(kmer) || array[(int)kmer]>3);
|
jpayne@68
|
304 }
|
jpayne@68
|
305 }
|
jpayne@68
|
306 }
|
jpayne@68
|
307 if(rcomp){
|
jpayne@68
|
308 r.reverseComplement();
|
jpayne@68
|
309 addReadSplit(r, count, k1, k2, mask1, mask2, gap, false);
|
jpayne@68
|
310 }
|
jpayne@68
|
311 }
|
jpayne@68
|
312
|
jpayne@68
|
313 public static void addReadSplit(final byte[] bases, final KCountArray2 count, final int k1, final int k2, final long mask1, final long mask2, final int gap, boolean rcomp){
|
jpayne@68
|
314 int len=0;
|
jpayne@68
|
315 int shift=k2*2;
|
jpayne@68
|
316 long kmer1=0;
|
jpayne@68
|
317 long kmer2=0;
|
jpayne@68
|
318 byte[] quals=null;
|
jpayne@68
|
319
|
jpayne@68
|
320 assert(kmer1>=kmer2);
|
jpayne@68
|
321
|
jpayne@68
|
322 // assert(false) : k1+", "+k2+", "+mask1+", "+mask2+", "+gap;
|
jpayne@68
|
323
|
jpayne@68
|
324 for(int i=0, j=i+k1+gap; j<bases.length; i++, j++){
|
jpayne@68
|
325 int x1=AminoAcid.baseToNumber[bases[i]];
|
jpayne@68
|
326 int x2=AminoAcid.baseToNumber[bases[j]];
|
jpayne@68
|
327 if(x1<0 || x2<0 || (quals!=null && (quals[i]<minQuality || quals[j]<minQuality))){
|
jpayne@68
|
328 len=0;
|
jpayne@68
|
329 kmer1=0;
|
jpayne@68
|
330 kmer2=0;
|
jpayne@68
|
331 }else{
|
jpayne@68
|
332 kmer1=((kmer1<<2)|x1)&mask1;
|
jpayne@68
|
333 kmer2=((kmer2<<2)|x2)&mask2;
|
jpayne@68
|
334 len++;
|
jpayne@68
|
335 if(len>=k1){
|
jpayne@68
|
336 keysCounted++;
|
jpayne@68
|
337 // System.out.print("Incrementing "+Long.toHexString(kmer)+": "+count.read(kmer));
|
jpayne@68
|
338
|
jpayne@68
|
339 long key=(kmer1<<shift)|kmer2;
|
jpayne@68
|
340 System.out.println(Long.toHexString(kmer1));
|
jpayne@68
|
341 System.out.println(Long.toHexString(kmer2));
|
jpayne@68
|
342 System.out.println(Long.toHexString(key));
|
jpayne@68
|
343 count.increment(key, 1);
|
jpayne@68
|
344 // System.out.println(" -> "+count.read(kmer));
|
jpayne@68
|
345 // System.out.print("Incrementing array for "+Long.toHexString(kmer)+": "+array[(int)kmer]);
|
jpayne@68
|
346 // array[(int)kmer]++;
|
jpayne@68
|
347 // System.out.println(" -> "+array[(int)kmer]+"\n");
|
jpayne@68
|
348 // assert(array[(int)kmer]==count.read(kmer) || array[(int)kmer]>3);
|
jpayne@68
|
349 }
|
jpayne@68
|
350 }
|
jpayne@68
|
351 }
|
jpayne@68
|
352 if(rcomp){
|
jpayne@68
|
353 AminoAcid.reverseComplementBasesInPlace(bases);
|
jpayne@68
|
354 addReadSplit(bases, count, k1, k2, mask1, mask2, gap, false);
|
jpayne@68
|
355 }
|
jpayne@68
|
356 }
|
jpayne@68
|
357
|
jpayne@68
|
358 }
|