Debian Codegraph @

current/prok/GeneModel.java

raw symbols SCIP
1 package prok;
2
3 import java.io.PrintStream;
4 import java.util.ArrayList;
5 import java.util.BitSet;
6 import java.util.HashMap;
7
8 import aligner.SingleStateAlignerFlat2;
9 import dna.AminoAcid;
10 import fileIO.FileFormat;
11 import gff.GffLine;
12 import shared.KillSwitch;
13 import shared.Shared;
14 import shared.Tools;
15 import stream.Read;
16 import stream.ReadInputStream;
17 import structures.ByteBuilder;
18 import structures.IntList;
19
20 /**
21 * This class is designed to store kmer frequencies related to gene
22 * starts, stops, and interiors. It can be loaded from a pgm file.
23 *
24 * It's possible to use multiple GeneModels; for example, one for
25 * each of several GC ranges or clades.
26 * @author Brian Bushnell
27 * @date Sep 24, 2018
28 *
29 */
30 public class GeneModel extends ProkObject {
31
32 /*--------------------------------------------------------------*/
33 /*---------------- Initialization ----------------*/
34 /*--------------------------------------------------------------*/
35
36 public GeneModel(boolean fill){
37 if(fill){
38 fillContainers();
39 }
40 }
41
42 void fillContainers(){
43 statsCDS.setInner(kInnerCDS, 3);
44 statsCDS.setStart(kStartCDS, startFrames, startLeftOffset);
45 statsCDS.setStop(kStopCDS, stopFrames, stopLeftOffset);
46
47 for(int i=0; i<rnaContainers.length; i++){
48 StatsContainer sc=rnaContainers[i];
49 sc.setInner(kInnerRNA, 1);
50 }
51
52 statstRNA.setStart(kStartRNA, 14, 4);
53 statstRNA.setStop(kStopRNA, 14, 6);
54
55 stats16S.setStart(kStartRNA, 20, 7);
56 stats16S.setStop(kStopRNA, 12, 16);
57
58 stats23S.setStart(kStartRNA, 17, 3);
59 stats23S.setStop(kStopRNA, 15, 12);
60
61 stats5S.setStart(kStartRNA, 20, 5);
62 stats5S.setStop(kStopRNA, 15, 5);
63
64 stats18S.setStart(kStartRNA, 20, 7);//TODO: 18S bounds are untested and should be empirically determined
65 stats18S.setStop(kStopRNA, 12, 16);
66 }
67
68 /*--------------------------------------------------------------*/
69 /*---------------- Public Methods ----------------*/
70 /*--------------------------------------------------------------*/
71
72 public boolean process(String genomeFname, String gffFname){
73 FileFormat fnaFile=FileFormat.testInput(genomeFname, FileFormat.FA, null, true, true);
74 FileFormat gffFile=FileFormat.testInput(gffFname, FileFormat.GFF, null, true, true);
75
76 if(fnaFile==null || gffFile==null){
77 errorState=true;
78 return true;
79 }
80
81 ArrayList<Read> reads=ReadInputStream.toReads(fnaFile, maxReads);
82 ArrayList<GffLine>[] allGffLines=GffLine.loadGffFileByType(gffFile, "CDS,rRNA,tRNA", true);
83 ArrayList<GffLine> cds=allGffLines[0];
84 ArrayList<GffLine> rrna=allGffLines[1];
85 ArrayList<GffLine> trna=allGffLines[2];
86
87 return process(reads, cds, rrna, trna);
88 }
89
90 public boolean process(ArrayList<Read> genome,
91 ArrayList<GffLine> cds, ArrayList<GffLine> rrna, ArrayList<GffLine> trna){
92 numFiles++;
93
94 if(genome==null || cds==null){
95 errorState=true;
96 return true;
97 }
98 filesProcessed++;
99
100 ArrayList<ScafData> scafList;
101 {//Scoped to save memory
102 readsProcessed+=genome.size();
103 scafList=new ArrayList<ScafData>(genome.size());
104 for(Read r : genome){
105 basesProcessed+=r.length();
106 scafList.add(new ScafData(r));
107 }
108 }
109 {//Scoped to save memory
110 genesProcessed+=cds.size();
111 genesProcessed+=(rrna==null ? 0 : rrna.size());
112 genesProcessed+=(trna==null ? 0 : trna.size());
113
114 HashMap<String, ScafData> scafMap=makeScafMap(scafList);
115 fillScafDataCDS(cds, scafMap);
116 fillScafDataRNA(rrna, scafMap);
117 fillScafDataRNA(trna, scafMap);
118 }
119
120 countBases(scafList);
121 if(PROCESS_PLUS_STRAND){
122 processStrand(scafList, Shared.PLUS);
123 }
124 if(PROCESS_MINUS_STRAND){
125 for(ScafData sd : scafList){
126 sd.clear();
127 sd.reverseComplement();
128 }
129 processStrand(scafList, Shared.MINUS);
130 for(ScafData sd : scafList){
131 sd.clear();
132 sd.reverseComplement();
133 }
134 }
135 return false;
136 }
137
138 public static boolean ADD_CDS_ONLY=false;//Sometimes merging in a custom model messes up non-CDS numbers
139 public void add(GeneModel pgm){
140 if(ADD_CDS_ONLY && readsProcessed>0) {
141 // System.err.println("Adding CDS; readsProcessed="+readsProcessed+", ADD_CDS_ONLY="+ADD_CDS_ONLY);
142 statsCDS.add(pgm.statsCDS);
143 }else {
144 // System.err.println("Adding all; readsProcessed="+readsProcessed+", ADD_CDS_ONLY="+ADD_CDS_ONLY);
145 for(int i=0; i<allContainers.length; i++){
146 // System.err.println("merging "+allContainers[i].name);
147 allContainers[i].add(pgm.allContainers[i]);
148 }
149 }
150
151 readsProcessed+=pgm.readsProcessed;
152 basesProcessed+=pgm.basesProcessed;
153 genesProcessed+=pgm.genesProcessed;
154 filesProcessed+=pgm.filesProcessed;
155
156 // geneStartsProcessed+=pgm.geneStartsProcessed;
157 // tRNAProcessed+=pgm.tRNAProcessed;
158 // r16SProcessed+=pgm.r16SProcessed;
159 // r23SProcessed+=pgm.r23SProcessed;
160 // r5SProcessed+=pgm.r5SProcessed;
161 // r18SProcessed+=pgm.r18SProcessed;
162
163 // fnames.addAll(pgm.fnames);
164 numFiles+=pgm.numFiles;
165 taxIds.addAll(pgm.taxIds);
166 Tools.add(baseCounts, pgm.baseCounts);
167 }
168
169 public void multiplyBy(double mult) {
170 for(int i=0; i<allContainers.length; i++){
171 allContainers[i].multiplyBy(mult);
172 }
173
174 readsProcessed=Math.round(readsProcessed*mult);
175 basesProcessed=Math.round(basesProcessed*mult);
176 genesProcessed=Math.round(genesProcessed*mult);
177 filesProcessed=Math.round(filesProcessed*mult);
178
179 for(int i=0; i<baseCounts.length; i++){
180 baseCounts[i]=Math.round(baseCounts[i]*mult);
181 }
182 }
183
184 /*--------------------------------------------------------------*/
185 /*---------------- Outer Methods ----------------*/
186 /*--------------------------------------------------------------*/
187
188 public float gc(){
189 long a=baseCounts[0];
190 long c=baseCounts[1];
191 long g=baseCounts[2];
192 long t=baseCounts[3];
193 return (float)((g+c)/Tools.max(1.0, a+t+g+c));
194 }
195
196 HashMap<String, ScafData> makeScafMap(ArrayList<ScafData> scafList){
197 HashMap<String, ScafData> scafMap=new HashMap<String, ScafData>(scafList.size()*3);
198 for(ScafData sd : scafList){scafMap.put(sd.name, sd);}
199 for(ScafData sd : scafList){
200 String name=sd.name;
201 int idx=name.indexOf(' ');
202 if(idx>=0){
203 String prefix=name.substring(0, idx);
204 if(scafMap.containsKey(prefix)){
205 assert(false) : "Duplicate degenerate name: '"+name+"', '"+prefix+"'";
206 }else{
207 scafMap.put(prefix, sd);
208 }
209 }
210 }
211 return scafMap;
212 }
213
214 public void fillScafDataCDS(ArrayList<GffLine> cdsLines, HashMap<String, ScafData> scafMap){
215 if(!callCDS){return;}
216 for(GffLine gline : cdsLines){
217 ScafData sd=scafMap.get(gline.seqid);
218 assert(sd!=null) : "Can't find scaffold for GffLine "+gline.seqid;
219 sd.addCDS(gline);
220 }
221 }
222
223 public void fillScafDataRNA(ArrayList<GffLine> rnaLines, HashMap<String, ScafData> scafMap){
224 for(GffLine gline : rnaLines){
225 ScafData sd=scafMap.get(gline.seqid);
226 assert(sd!=null) : "Can't find scaffold for GffLine "+gline.seqid;
227 if(processType(gline.prokType())){
228 sd.addRNA(gline);
229 }
230 }
231 }
232
233 public void processStrand(ArrayList<ScafData> scafList, int strand){
234 for(ScafData sd : scafList){
235 processCDS(sd, strand);
236 processRNA(sd, strand);
237 }
238 }
239
240 /*--------------------------------------------------------------*/
241 /*---------------- Inner Methods ----------------*/
242 /*--------------------------------------------------------------*/
243
244 private void countBases(ArrayList<ScafData> scafList){
245 for(ScafData sd : scafList){
246 countBases(sd.bases);
247 }
248 }
249
250 private void countBases(byte[] bases){
251 for(byte b : bases){
252 int x=AminoAcid.baseToNumberACGTother[b];
253 baseCounts[x]++;
254 }
255 }
256
257 /*--------------------------------------------------------------*/
258 /*---------------- Finding Codons ----------------*/
259 /*--------------------------------------------------------------*/
260
261 private static void findStopCodons(byte[] bases, IntList list, BitSet valid){
262 final int k=3;
263 final int mask=~((-1)<<(2*k));
264 int kmer=0;
265 int len=0;
266
267 for(int i=0; i<bases.length; i++){
268 byte b=bases[i];
269 int x=AminoAcid.baseToNumber[b];
270 kmer=((kmer<<2)|x)&mask;
271 if(x>=0){
272 len++;
273 if(len>=k){
274 int point=i;//End of the stop codon
275 if(isStopCodon(kmer) && !valid.get(point)){
276 list.add(point);
277 valid.set(point);
278 }
279 }
280 }else{len=0;}
281 }
282
283 for(int i=50; i<bases.length-3; i+=2000){//Add some non-canonical sites, aka noise
284 if(!valid.get(i)){
285 list.add(i);
286 }
287 }
288 }
289
290 private static void findStartCodons(byte[] bases, IntList list, BitSet valid){
291 final int k=3;
292 final int mask=~((-1)<<(2*k));
293 int kmer=0;
294 int len=0;
295
296 for(int i=0; i<bases.length; i++){
297 byte b=bases[i];
298 int x=AminoAcid.baseToNumber[b];
299 kmer=((kmer<<2)|x)&mask;
300 if(x>=0){
301 len++;
302 if(len>=k){
303 int point=i-k+1;//Start of the start codon
304 if(isStartCodon(kmer) && !valid.get(point)){
305 list.add(point);
306 valid.set(point);
307 }
308 }
309 }else{len=0;}
310 }
311
312 for(int i=50; i<bases.length-3; i+=2000){//Add some non-canonical sites, aka noise
313 if(!valid.get(i)){
314 list.add(i);
315 }
316 }
317 }
318
319 /*--------------------------------------------------------------*/
320 /*---------------- Processing GffLines ----------------*/
321 /*--------------------------------------------------------------*/
322
323 private static void processGene(GffLine gline, ScafData sd){
324 if(gline.length()<2){return;}
325 final int strand=gline.strand;
326 assert(strand==sd.strand());
327 final byte[] frames=sd.frames;
328 int start=gline.start-1, stop=gline.stop-1;
329 if(start<0 || stop>=sd.length()){return;}
330 // assert(start<stop) : gline; //Not always true for euks...
331 if(strand==Shared.MINUS){
332 int x=sd.length()-start-1;
333 int y=sd.length()-stop-1;
334 start=y;
335 stop=x;
336
337 // String a=new String(sd.bases, start, 3);
338 // String b=new String(sd.bases, stop-2, 3);
339 //// assert(false) : start+", "+stop+"\n"+gline+"\n"+new String(sd.bases, start, 3)+", "+new String(sd.bases, stop-2, 3);
340 // outstream.println(a+", "+b+", "+start+", "+stop);
341 }
342 assert(start>=0) : gline.toString()+"\n"+sd.length()+"\n"+sd.name;
343 markFrames(start, stop, frames, kInnerCDS);
344 sd.starts.add(start);
345 sd.stops.add(stop);
346 // assert(gline.start!=337) : gline+"\n"+start+", "+stop;
347 }
348
349 private boolean processRnaLine(final GffLine gline, final ScafData sd, final int type){
350 final int strand=gline.strand;
351 assert(strand==sd.strand());
352 final byte[] frames=sd.frames;
353 int start=gline.start-1, stop=gline.stop-1;
354 if(start<0 || stop>=sd.length()){return false;}
355 assert(start<=stop);
356 if(strand==Shared.MINUS){
357 int x=sd.length()-start-1;
358 int y=sd.length()-stop-1;
359 start=y;
360 stop=x;
361 }
362
363 if(AnalyzeGenes.alignRibo){
364 // byte[] seq=sd.fetch(start, stop);
365 Read[] consensusReads=ProkObject.consensusReads(type);
366 byte[] universal=(consensusReads!=null && consensusReads.length>0 ? consensusReads[0].bases : null);
367 float minIdentity=ProkObject.minID(type);
368 if(universal!=null){
369 int[] coords=KillSwitch.allocInt1D(2);
370 final int a=Tools.max(0, start-(AnalyzeGenes.adjustEndpoints ? 200 : 50));
371 final int b=Tools.min(sd.bases.length-1, stop+(AnalyzeGenes.adjustEndpoints ? 200 : 50));
372 float id1=align(universal, sd.bases, a, b, minIdentity, coords);
373 final int rstart=coords[0], rstop=coords[1];
374 // assert(false) : rstart+", "+rstop+", "+(rstop-rstart+1)+", "+start+", "+stop;
375 if(id1<minIdentity){
376 // System.err.println("Low identity: "+String.format("%.2s", 100*id1));
377 return false;
378 }else{
379 // System.err.println("Good identity: "+String.format("%.2s", 100*id1));
380 }
381 if(AnalyzeGenes.adjustEndpoints){
382 int startSlop=startSlop(type);
383 int stopSlop=stopSlop(type);
384 if(Tools.absdif(start, rstart)>startSlop){
385 // System.err.println("rstart:\t"+start+" -> "+rstart);
386 start=rstart;
387 }
388 if(Tools.absdif(stop, rstop)>stopSlop){
389 // System.err.println("rstop: \t"+stop+" -> "+rstop);
390 stop=rstop;
391 }
392 }
393 }
394 }
395
396 StatsContainer sc=allContainers[type];
397 sc.start.processPoint(sd.bases, start, 1);
398 sc.stop.processPoint(sd.bases, stop, 1);
399 assert(sc!=statsCDS);
400
401 assert(start>=0) : gline.toString()+"\n"+sd.length()+"\n"+sd.name;
402 final byte flag=typeToFlag(type);
403 for(int i=start+kInnerRNA-1; i<=stop; i++){
404 frames[i]|=flag;
405 }
406 return true;
407 }
408
409 private float align(byte[] query, byte[] ref, int start, int stop, float minIdentity, int[] coords){
410 // final int a=0, b=ref.length-1;
411 SingleStateAlignerFlat2 ssa=GeneCaller.getSSA();
412 final int minScore=ssa.minScoreByIdentity(query.length, minIdentity);
413 int[] max=ssa.fillUnlimited(query, ref, start, stop, minScore);
414 if(max==null){return 0;}
415
416 final int rows=max[0];
417 final int maxCol=max[1];
418 final int maxState=max[2];
419 // final int maxScore=max[3];
420 // final int maxStart=max[4];
421
422 //returns {score, bestRefStart, bestRefStop}
423 //padded: {score, bestRefStart, bestRefStop, padLeft, padRight};
424 final int[] score=ssa.score(query, ref, start, stop, rows, maxCol, maxState);
425 final int rstart=Tools.max(score[1], 0);
426 final int rstop=Tools.min(score[2], ref.length-1);
427 if(coords!=null){
428 coords[0]=rstart;
429 coords[1]=rstop;
430 }
431 final float id=ssa.tracebackIdentity(query, ref, start, stop, rows, maxCol, maxState, null);
432 return id;
433 }
434
435 /**
436 * Each frame byte has a bit marked for valid coding frames.
437 * For example, if frames[23]=0b100, then base 23 is the last base in a kmer starting at the 3rd base in a codon.
438 * If frames[23]=0, then no coding kmer end at that location on this strand.
439 * @param start
440 * @param stop
441 * @param frames
442 * @param k
443 */
444 private static void markFrames(int start, int stop, byte[] frames, int k){
445 assert(start<=stop) : start+", "+stop;
446 for(int i=start+k-1, frameBit=(1<<((k-1)%3)), max=Tools.min(stop-3, frames.length-1); i<=max; i++){
447 frames[i]=(byte)(frames[i]|frameBit);
448 frameBit<<=1;
449 if(frameBit>4){frameBit=1;}
450 }
451 // assert(false) : Arrays.toString(Arrays.copyOfRange(frames, start, start+20))+"\n"+start; //This is correct
452 }
453
454 /*--------------------------------------------------------------*/
455 /*---------------- Counting Kmers ----------------*/
456 /*--------------------------------------------------------------*/
457
458 private void processCDS(ScafData sd, int strand){
459 if(!callCDS){return;}
460 ArrayList<GffLine> glines=sd.cdsLines[strand];
461 for(GffLine gline : glines){
462 assert(gline.strand==strand);
463 processGene(gline, sd);
464 statsCDS.addLength(gline.length());
465 }
466
467 statsCDS.inner.processCDSFrames(sd.bases, sd.frames);
468 BitSet startSet=processEnds(sd.bases, statsCDS.start, sd.starts, 1);
469 BitSet stopSet=processEnds(sd.bases, statsCDS.stop, sd.stops, 1);
470 // outstream.println("Processed "+sd.starts.size+" valid starts and "+sd.stops.size+" stops.");
471 sd.clear();
472 findStartCodons(sd.bases, sd.starts, startSet);
473 findStopCodons(sd.bases, sd.stops, stopSet);
474 // outstream.println("Found "+sd.starts.size+" invalid starts and "+sd.stops.size+" stops.");
475 processEnds(sd.bases, statsCDS.start, sd.starts, 0);
476 processEnds(sd.bases, statsCDS.stop, sd.stops, 0);
477 }
478
479 private static int getGlineType(GffLine gline, ScafData sd){
480 if(!gline.inbounds(sd.length()) || gline.partial()){return -1;}
481
482 final int length=gline.length();
483 final int type=gline.prokType();
484 if(type<0){
485 return type;
486 }else if(type==CDS){
487 return type;
488 }else if(type==tRNA && length>=40 && length<=120){
489 return type;
490 }else if(type==r16S && length>=1440 && length<=1620){
491 return type;
492 }else if(type==r23S && length>=2720 && length<=3170){
493 return type;
494 }else if(type==r5S && length>=90 && length<=150){
495 return type;
496 }else if(type==r18S && length>=1400 && length<=2000){ //TODO: Check length range
497 return type;
498 }
499 return -1;
500 }
501
502 private void processRNA(ScafData sd, int strand){
503 sd.clear();
504 ArrayList<GffLine> lines=sd.rnaLines[strand];
505 for(GffLine gline : lines){
506 assert(gline.strand==strand);
507 final int type=getGlineType(gline, sd);
508 if(type>0){
509 StatsContainer sc=allContainers[type];
510 sc.addLength(gline.length());
511 processRnaLine(gline, sd, type);
512 }
513 }
514 processRnaInner(sd);
515 processRnaEnds(sd);
516 }
517
518 void processRnaInner(ScafData sd){
519 byte[] bases=sd.bases;
520 byte[] frames=sd.frames;
521 final int k=kInnerRNA;//TODO: Note! This is linked to a single static variable for all RNAs.
522 final int mask=~((-1)<<(2*k));
523 int kmer=0;
524 int len=0;
525
526 for(int i=0; i<bases.length; i++){
527 byte b=bases[i];
528 int x=AminoAcid.baseToNumber[b];
529 kmer=((kmer<<2)|x)&mask;
530 if(x>=0){
531 len++;
532 if(len>=k){
533 int vf=frames[i];
534 for(int type=0; type<5; type++){
535 int valid=vf&1;
536 rnaContainers[type].inner.add(kmer, 0, valid);
537 vf=(vf>>1);
538 }
539 }
540 }else{len=0;}
541 }
542 }
543
544 void processRnaEnds(ScafData sd){
545 byte[] bases=sd.bases;
546
547 final int k=stats16S.start.k;
548 final int kMax=stats16S.start.kMax;
549 final int mask=stats16S.start.mask;
550 final long[] counts=new long[kMax];//TODO: Slow
551
552 int kmer=0;
553 int len=0;
554
555 for(int i=0; i<bases.length; i++){
556 byte b=bases[i];
557 int x=AminoAcid.baseToNumber[b];
558 kmer=((kmer<<2)|x)&mask;
559
560 if(x>=0){
561 len++;
562 if(len>=k){
563 counts[kmer]++;
564 }
565 }else{len=0;}
566 }
567 for(StatsContainer sc : rnaContainers){
568 FrameStats fs=sc.start;
569 for(long[] array : fs.countsFalse){
570 Tools.add(array, counts);
571 }
572 fs=sc.stop;
573 for(long[] array : fs.countsFalse){
574 Tools.add(array, counts);
575 }
576 }
577 }
578
579 private static BitSet processEnds(byte[] bases, FrameStats stats, IntList list, int valid){
580 BitSet points=new BitSet(bases.length);
581 for(int i=0; i<list.size; i++){
582 int point=list.get(i);
583 stats.processPoint(bases, list.get(i), valid);
584 points.set(point);
585 }
586 return points;
587 }
588
589 /*--------------------------------------------------------------*/
590 /*---------------- Scoring ----------------*/
591 /*--------------------------------------------------------------*/
592
593 // //Assumes bases are in the correct strand
594 // public float calcStartScore(int start, int stop, byte[] bases){
595 // float f=scorePoint(start, bases, startStats);
596 //// float ss=scoreStart2(start, bases, stop, innerKmerStats);
597 //// if(ss>0){f=(f+0.0005f*ss);} //Does not seem to help; needs more study.
598 // return f;
599 // }
600 //
601 // //Assumes bases are in the correct strand
602 // public float calcStopScore(int stop, byte[] bases){
603 // float f=scorePoint(stop, bases, stopStats);
604 // return f;
605 // }
606 //
607 // //Assumes bases are in the correct strand
608 // public float calcRnaStartScore(int start, int stop, byte[] bases){
609 // float f=scorePoint(start, bases, rrnaStartStats);
610 // return f;
611 // }
612 //
613 // //Assumes bases are in the correct strand
614 // public float calcRnaStopScore(int stop, byte[] bases){
615 // float f=scorePoint(stop, bases, rrnaStopStats);
616 // return f;
617 // }
618
619 // public static float calcKmerScore(int start, int stop, int startFrame, byte[] bases, FrameStats stats){
620 //
621 // assert(stats.frames==3);
622 // final int k=stats.k;
623 // final int mask=~((-1)<<(2*k));
624 //
625 // int kmer=0;
626 // int len=0;
627 // float score=0;
628 // int numKmers=0;
629 //
630 // for(int pos=start, currentFrame=startFrame; pos<stop; pos++){
631 // final byte b=bases[pos];
632 // final int x=AminoAcid.baseToNumber[b];
633 //
634 // if(x>=0){
635 // kmer=((kmer<<2)|x)&mask;
636 // len++;
637 // if(len>=k){
638 // float prob=stats.probs[currentFrame][kmer];
639 // float dif=prob-0.99f;//Prob above 1 is more likely than average
640 // score+=dif;
641 // numKmers++;
642 // }
643 // }else{
644 // len=0;
645 // kmer=0;
646 // }
647 //
648 // currentFrame++;
649 // if(currentFrame>2){currentFrame=0;}
650 // }
651 // return score/Tools.max(1f, numKmers);
652 // }
653 //
654 // /**
655 // * TODO
656 // * Evaluate the relative difference between left and right frequencies.
657 // * The purpose is to find locations where the left side looks noncoding and the right side looks coding.
658 // * Does not currently yield useful results.
659 // */
660 // public static float scoreStart2(int point, byte[] bases, int stop, FrameStats stats){
661 // final int k=stats.k;
662 //
663 // int start=point-45;
664 // if(start<0 || stop>bases.length){return 0.5f;}
665 //
666 // float left=calcKmerScore(start, Tools.min(point+k-2, bases.length), 0, bases, stats);
667 // float right=calcKmerScore(point, stop-3, 0, bases, stats);
668 // return right-left; //High numbers are likely to be starts; non-starts should be near 0.
669 // }
670
671 /*--------------------------------------------------------------*/
672 /*---------------- toString ----------------*/
673 /*--------------------------------------------------------------*/
674
675 @Override
676 public String toString(){
677 return appendTo(new ByteBuilder()).toString();
678 }
679
680 public ByteBuilder appendTo(ByteBuilder bb){
681
682 // Collections.sort(fnames);
683 taxIds.sort();
684
685 bb.append("#BBMap "+Shared.BBTOOLS_VERSION_STRING+" Prokaryotic Gene Model\n");
686 bb.append("#files");
687 bb.tab().append(numFiles);
688 // if(fnames.size()>5){
689 // bb.tab().append(fnames.size());
690 // }else{
691 // for(String fname : fnames){
692 // bb.tab().append(fname);
693 // }
694 // }
695 bb.nl();
696 bb.append("#taxIDs");
697 for(int i=0; i<taxIds.size; i++){
698 bb.tab().append(taxIds.get(i));
699 }
700 bb.nl();
701 // bb.append("#k_inner\t").append(innerKmerLength).nl();
702 // bb.append("#k_end\t").append(endKmerLength).nl();
703 // bb.append("#start_left_offset\t").append(startLeftOffset).nl();
704 // bb.append("#start_right_offset\t").append(startRightOffset).nl();
705 // bb.append("#stop_left_offset\t").append(stopLeftOffset).nl();
706 // bb.append("#stop_right_offset\t").append(stopRightOffset).nl();
707 bb.append("#scaffolds\t").append(readsProcessed).nl();
708 bb.append("#bases\t").append(basesProcessed).nl();
709 bb.append("#genes\t").append(genesProcessed).nl();
710 bb.append("#GC\t").append(gc(),2).nl();
711 bb.append("#ACGTN");
712 for(long x : baseCounts){
713 bb.tab().append(x);
714 }
715 bb.nl();
716
717 for(StatsContainer sc : allContainers){
718 sc.appendTo(bb);
719 }
720 assert(allContainers.length>5) : allContainers.length;
721
722 return bb;
723 }
724
725 /*--------------------------------------------------------------*/
726 /*---------------- Stats ----------------*/
727 /*--------------------------------------------------------------*/
728
729 public final StatsContainer statsCDS=new StatsContainer(CDS);
730 public final StatsContainer statstRNA=new StatsContainer(tRNA);
731 public final StatsContainer stats16S=new StatsContainer(r16S);
732 public final StatsContainer stats23S=new StatsContainer(r23S);
733 public final StatsContainer stats5S=new StatsContainer(r5S);
734 public final StatsContainer stats18S=new StatsContainer(r18S);
735
736 final StatsContainer[] rnaContainers=new StatsContainer[] {statstRNA, stats16S, stats23S, stats5S, stats18S};
737 final StatsContainer[] allContainers=new StatsContainer[] {statsCDS, statstRNA, stats16S, stats23S, stats5S, stats18S};
738 //public static int CDS=0, tRNA=1, r16S=2, r23S=3, r5S=4, r18S=5, r28S=6, RNA=7;
739
740 // public final FrameStats innerKmerStats=new FrameStats("innerKmerStats", innerKmerLength, 3, 0);
741 // public final FrameStats startStats=new FrameStats("startStats", endKmerLength, startFrames, startLeftOffset);
742 // public final FrameStats stopStats=new FrameStats("stopStats", endKmerLength, stopFrames, stopLeftOffset);
743 //
744 // public final FrameStats rrnaStartStats=new FrameStats("rrnaStart", 2, 16, 8);
745 // public final FrameStats rrnaStopStats=new FrameStats("rrnaStop", 2, 16, 8);
746 //
747 // public final FrameStats trnaStats=new FrameStats("tRNA", rnaKmerLength, 1, 0);
748 // public final FrameStats rrna16Sstats=new FrameStats("16S", rnaKmerLength, 1, 0);
749 // public final FrameStats rrna23Sstats=new FrameStats("23S", rnaKmerLength, 1, 0);
750 // public final FrameStats rrna5Sstats=new FrameStats("5S", rnaKmerLength, 1, 0);
751 // public final FrameStats[] rnaKmerStats=new FrameStats[] {trnaStats, rrna16Sstats, rrna23Sstats, rrna5Sstats};
752
753 /*--------------------------------------------------------------*/
754
755 // public ArrayList<String> fnames=new ArrayList<String>();
756 public int numFiles=0;
757 public IntList taxIds=new IntList();
758
759
760 /*--------------------------------------------------------------*/
761 /*---------------- Fields ----------------*/
762 /*--------------------------------------------------------------*/
763
764 private long maxReads=-1;
765
766 long readsProcessed=0;
767 long basesProcessed=0;
768 long genesProcessed=0;
769 long filesProcessed=0;
770 long[] baseCounts=new long[5];
771
772 /*--------------------------------------------------------------*/
773 /*---------------- Static Setters ----------------*/
774 /*--------------------------------------------------------------*/
775
776 public synchronized void setStatics(){
777 // assert(!setStatics);
778 kInnerCDS=statsCDS.inner.k;
779 kStartCDS=statsCDS.start.k;
780 kStopCDS=statsCDS.stop.k;
781
782 setStartLeftOffset(statsCDS.start.leftOffset);
783 setStartRightOffset(statsCDS.start.rightOffset());
784
785 setStopLeftOffset(statsCDS.stop.leftOffset);
786 setStopRightOffset(statsCDS.stop.rightOffset());
787
788 kInnerRNA=stats16S.inner.k;//TODO: Why is 16S used here?
789 kStartRNA=stats16S.start.k;
790 kStopRNA=stats16S.stop.k;
791 setStatics=true;
792 }
793
794 public static void setInnerK(int k){
795 kInnerCDS=k;
796 }
797
798 public static void setStartK(int k){
799 kStartCDS=k;
800 }
801
802 public static void setStopK(int k){
803 kStopCDS=k;
804 }
805
806 public static void setStartLeftOffset(int x){
807 startLeftOffset=x;
808 startFrames=startLeftOffset+startRightOffset+1;
809 // System.err.println("startLeftOffset="+startLeftOffset+", startRightOffset="+startRightOffset+", frames="+startFrames);
810 }
811
812 public static void setStartRightOffset(int x){
813 startRightOffset=x;
814 startFrames=startLeftOffset+startRightOffset+1;
815 // System.err.println("startLeftOffset="+startLeftOffset+", startRightOffset="+startRightOffset+", frames="+startFrames);
816 // assert(false) : endLeftOffset+", "+endRightOffset+", "+endFrames;
817 }
818
819 public static void setStopLeftOffset(int x){
820 stopLeftOffset=x;
821 stopFrames=stopLeftOffset+stopRightOffset+1;
822 // System.err.println("stopLeftOffset="+stopLeftOffset+", stopRightOffset="+stopRightOffset+", frames="+stopFrames);
823 }
824
825 public static void setStopRightOffset(int x){
826 stopRightOffset=x;
827 stopFrames=stopLeftOffset+stopRightOffset+1;
828 // System.err.println("stopLeftOffset="+stopLeftOffset+", stopRightOffset="+stopRightOffset+", frames="+stopFrames);
829 // assert(false) : endLeftOffset+", "+endRightOffset+", "+endFrames;
830 }
831
832 public static final boolean isStartCodon(int code){
833 return code>=0 && code<=63 && isStartCodon[code];
834 }
835 public static final boolean isStopCodon(int code){
836 return code>=0 && code<=63 && isStopCodon[code];
837 }
838
839 /*--------------------------------------------------------------*/
840 /*---------------- Class Init ----------------*/
841 /*--------------------------------------------------------------*/
842
843 private static boolean[] makeIsCodon(String[] codons){
844 boolean[] array=new boolean[64];
845 for(String s : codons){
846 int x=AminoAcid.toNumber(s);
847 array[x]=true;
848 }
849 return array;
850 }
851
852 public static int kInnerCDS=6;
853 public static int kStartCDS=3;
854 public static int kStopCDS=3;
855
856 static int startLeftOffset(){return startLeftOffset;}
857 static int startRightOffset(){return startRightOffset;}
858 static int startFrames(){return startFrames;}
859
860 private static int startLeftOffset=21; //21 works well for k=4
861 private static int startRightOffset=8; //10 works well for k=4
862 private static int startFrames=startLeftOffset+startRightOffset+1;
863
864 private static int stopLeftOffset=9;
865 private static int stopRightOffset=12;
866 private static int stopFrames=stopLeftOffset+stopRightOffset+1;
867
868 private static boolean setStatics=false;
869
870 /*--------------------------------------------------------------*/
871 /*---------------- More Statics ----------------*/
872 /*--------------------------------------------------------------*/
873
874 //E. coli uses 83% AUG (3542/4284), 14% (612) GUG, 3% (103) UUG[7] and one or two others (e.g., an AUU and possibly a CUG).[8][9]
875 public static String[] startCodons=new String[] {"ATG", "GTG", "TTG"};
876 public static String[] extendedStartCodons=new String[] {"ATG", "GTG", "TTG", "ATT", "CTG", "ATA"};
877 public static String[] stopCodons=new String[] {"TAG", "TAA", "TGA"};
878 public static boolean[] isStartCodon=makeIsCodon(startCodons);
879 public static boolean[] isStopCodon=makeIsCodon(stopCodons);
880
881 /*--------------------------------------------------------------*/
882
883 private static PrintStream outstream=System.err;
884 public static boolean verbose=false;
885 public static boolean errorState=false;
886
887 }