package tracker; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.Map.Entry; import align2.QualityTools; import barcode.Barcode; import dna.AminoAcid; import fileIO.ByteStreamWriter; import fileIO.TextStreamWriter; import shared.Shared; import shared.Tools; import shared.TrimRead; import stream.Read; import stream.SamLine; import structures.ByteBuilder; import structures.LongList; import structures.SuperLongList; /** * @author Brian Bushnell * @date Mar 18, 2013 * */ public class ReadStats { public ReadStats(){this(true);} public ReadStats(boolean addToList){ if(addToList){ synchronized(ReadStats.class){ objectList.add(this); } } if(COLLECT_QUALITY_STATS){ aqualArray=new long[2][QMAX2]; qualLength=new long[2][MAXLEN]; qualSum=new long[2][MAXLEN]; qualSumDouble=new double[2][MAXLEN]; bqualHistOverall=new long[QMAX2]; }else{ aqualArray=null; qualLength=null; qualSum=null; qualSumDouble=null; bqualHistOverall=null; } if(BQUAL_HIST_FILE!=null || COLLECT_QUALITY_STATS || COLLECT_QUALITY_ACCURACY){ bqualHist=new long[2][MAXLEN][QMAX2]; }else{ bqualHist=null; } if(QUAL_COUNT_HIST_FILE!=null){ qcountHist=new long[2][QMAX2]; }else{ qcountHist=null; } if(COLLECT_MATCH_STATS){ matchSum=new long[2][MAXLEN]; delSum=new long[2][MAXLEN]; insSum=new long[2][MAXLEN]; subSum=new long[2][MAXLEN]; nSum=new long[2][MAXLEN]; clipSum=new long[2][MAXLEN]; otherSum=new long[2][MAXLEN]; }else{ matchSum=null; delSum=null; insSum=null; subSum=null; nSum=null; clipSum=null; otherSum=null; } if(COLLECT_QUALITY_ACCURACY){ qualMatch=new long[QMAX2]; qualSub=new long[QMAX2]; qualIns=new long[QMAX2]; qualDel=new long[QMAX2]; }else{ qualMatch=null; qualSub=null; qualIns=null; qualDel=null; } if(COLLECT_INSERT_STATS){ insertHist=new LongList(MAXLEN); }else{ insertHist=null; } if(COLLECT_BASE_STATS){ baseHist=new LongList[2][5]; for(int i=0; i(); }else { barcodeMap=null; } } public static ReadStats mergeAll(){ if(objectList==null || objectList.isEmpty()){return merged=null;} if(objectList.size()==1){return merged=objectList.get(0);} ReadStats x=new ReadStats(false); for(ReadStats rs : objectList){ x.read2Count+=rs.read2Count; if(COLLECT_QUALITY_STATS){ for(int i=0; i e : rs.barcodeMap.entrySet()) { final String key=e.getKey(); final Barcode brs=e.getValue(); Barcode bx=x.barcodeMap.get(key); if(bx==null) { x.barcodeMap.put(key, brs); }else{ bx.increment(brs); } } } x.gcMaxReadLen=Tools.max(x.gcMaxReadLen, rs.gcMaxReadLen); x.idMaxReadLen=Tools.max(x.idMaxReadLen, rs.idMaxReadLen); } merged=x; return x; } public void addToHistograms(Read r) { if(COLLECT_QUALITY_STATS){addToQualityHistogram(r);} if(COLLECT_BASE_STATS){addToBaseHistogram(r);} if(COLLECT_MATCH_STATS){addToMatchHistogram(r);} if(COLLECT_QUALITY_ACCURACY){addToQualityAccuracy(r);} if(COLLECT_ERROR_STATS){addToErrorHistogram(r);} if(COLLECT_INDEL_STATS){addToIndelHistogram(r);} if(COLLECT_LENGTH_STATS){addToLengthHistogram(r);} if(COLLECT_GC_STATS){addToGCHistogram(r);} if(COLLECT_ENTROPY_STATS){addToEntropyHistogram(r);} if(COLLECT_IDENTITY_STATS){addToIdentityHistogram(r);} if(COLLECT_BARCODE_STATS){addToBarcodeStats(r);} } public void addToQualityHistogram(final Read r){ if(r==null){return;} addToQualityHistogram2(r); if(r.mate!=null){addToQualityHistogram2(r.mate);} } private void addToQualityHistogram2(final Read r){ final int pairnum=r.samline==null ? r.pairnum() : r.samline.pairnum(); if(r==null || r.quality==null || r.quality.length<1){return;} byte[] quals=r.quality, bases=r.bases; final Object obj=r.obj; if(obj!=null && obj.getClass()==TrimRead.class){ quals=(pairnum==0 ? ((TrimRead)obj).qual1 : ((TrimRead)obj).qual2); bases=(pairnum==0 ? ((TrimRead)obj).bases1 : ((TrimRead)obj).bases2); } if(pairnum==1){read2Count++;} addToQualityHistogram(quals, pairnum); int x=Read.avgQualityByProbabilityInt(bases, quals, true, 0); aqualArray[pairnum][x]++; if(bqualHist!=null){ addToBQualityHistogram(quals, pairnum); } if(QUAL_COUNT_HIST_FILE!=null){ addToQCountHistogram(quals, pairnum); } } public void addToQualityHistogram(byte[] qual, int pairnum){ if(qual==null || qual.length<1){return;} final int limit=Tools.min(qual.length, MAXLEN); final long[] ql=qualLength[pairnum], qs=qualSum[pairnum]; final double[] qsd=qualSumDouble[pairnum]; ql[limit-1]++; for(int i=0; i=0){ if(AminoAcid.isFullyDefined(bases[y])){ qualDel[qual[y]]++; } } } bpos--; }else if(m=='d'){ bpos--; }else{ assert(!Tools.isDigit(m)) : ((char)m); } } bpos++; lastm=m; } } public void addToErrorHistogram(final Read r){ if(r==null){return;} addToErrorHistogram(r, 0); if(r.mate!=null){addToErrorHistogram(r.mate, 1);} } private void addToErrorHistogram(final Read r, int pairnum){ if(r==null || r.bases==null || r.length()<1 || !r.mapped() || r.match==null/* || r.discarded()*/){return;} r.toLongMatchString(false); int x=r.countSubs(); errorHist.increment(x, 1); } public void addToLengthHistogram(final Read r){ if(r==null){return;} addToLengthHistogram(r, 0); if(r.mate!=null){addToLengthHistogram(r.mate, 1);} } private void addToLengthHistogram(final Read r, int pairnum){ if(r==null || r.bases==null){return;} int x=r.length();//Tools.min(r.length(), MAXLENGTHLEN); Old style before SLL lengthHist.increment(x, 1); } public void addToGCHistogram(final Read r1){ if(r1==null){return;} final Read r2=r1.mate; final int len1=r1.length(), len2=r1.mateLength(); final float gc1=(len1>0 ? r1.gc() : -1); final float gc2=(len2>0 ? r2.gc() : -1); if(usePairGC){ final float gc; if(r2==null){ gc=gc1; }else{ gc=(gc1*len1+gc2*len2)/(len1+len2); } addToGCHistogram(gc, len1+len2); }else{ addToGCHistogram(gc1, len1); addToGCHistogram(gc2, len2); } } private void addToGCHistogram(final float gc, final int len){ if(gc<0 || len<1){return;} gcHist[Tools.min(GC_BINS, (int)(gc*(GC_BINS+1)))]++; gcMaxReadLen=Tools.max(len, gcMaxReadLen); } public void addToEntropyHistogram(final Read r1){ if(r1==null){return;} final Read r2=r1.mate; final int len1=r1.length(), len2=r1.mateLength(); final float entropy1=(len1>0 ? eTracker.averageEntropy(r1.bases, allowEntropyNs) : -1); final float entropy2=(len2>0 ? eTracker.averageEntropy(r2.bases, allowEntropyNs) : -1); if(/* usePairEntropy */ false){ final float entropy; if(r2==null){ entropy=entropy1; }else{ entropy=(entropy1*len1+entropy2*len2)/(len1+len2); } addToEntropyHistogram(entropy, len1+len2); }else{ addToEntropyHistogram(entropy1, len1); addToEntropyHistogram(entropy2, len2); } } private void addToEntropyHistogram(final float entropy, final int len){ if(entropy<0 || len<1){return;} entropyHist[Tools.min(ENTROPY_BINS, (int)(entropy*(ENTROPY_BINS+1)))]++; } public void addToIdentityHistogram(final Read r){ if(r==null){return;} addToIdentityHistogram(r, 0); if(r.mate!=null){addToIdentityHistogram(r.mate, 1);} } private void addToIdentityHistogram(final Read r, int pairnum){ if(r==null || r.bases==null || r.length()<1 || !r.mapped() || r.match==null/* || r.discarded()*/){return;} float id=r.identity(); idHist[(int)(id*ID_BINS)]++; idBaseHist[(int)(id*ID_BINS)]+=r.length(); idMaxReadLen=Tools.max(r.length(), idMaxReadLen); } public void addToTimeHistogram(final Read r){ if(r==null){return;} addToTimeHistogram(r, 0);//Time for pairs is the same. } private void addToTimeHistogram(final Read r, int pairnum){ if(r==null){return;} assert(r.obj!=null && r.obj.getClass()==Long.class); int x=(int)Tools.min(((Long)r.obj).longValue(), MAXTIMELEN); timeHist.increment(x, 1); } public boolean addToIndelHistogram(final Read r){ if(r==null){return false;} boolean success=addToIndelHistogram(r, 0); if(r.mate!=null){success=addToIndelHistogram(r.mate, 1)|success;} return success; } /** Handles short match, long match, and reads with attached SamLines */ private boolean addToIndelHistogram(final Read r, int pairnum){ if(r==null || !r.mapped()){return false;} if(r.samline!=null){ boolean success=addToIndelHistogram(r.samline); if(success){return true;} } if(r.match==null/* || r.discarded()*/){return false;} final byte[] match=r.match; byte lastLetter='?'; boolean digit=false; int streak=0; for(int mpos=0; mpos0 || (streak==0 && lastm=='?')); if(!digit){streak++;} digit=false; if(lastLetter=='D'){ streak=Tools.min(streak, MAXDELLEN2); if(streak0){ insertHist.increment(x, 1); pairedCount++; }else{ unpairedCount++; } // assert(x!=1) : "\n"+r+"\n\n"+r.mate+"\n"; // System.out.println("Incrementing "+x); } public void addToInsertHistogram(final SamLine r1){ int x=r1.tlen; if(x<0) {x=-x;} x=Tools.min(MAXINSERTLEN, x); if(r1.pairedOnSameChrom() && x>0){ pairedCount++; insertHist.increment(x, 1); }else { unpairedCount++; } } public void addToInsertHistogram(final SamLine r1, final SamLine r2){ if(r1==null){return;} int x=insertSizeMapped(r1, r2, REQUIRE_PROPER_PAIR); if(verbose){ System.err.println(r1.qname+"\t"+x); } x=Tools.min(MAXINSERTLEN, x); if(x>0){ insertHist.increment(x, 1); pairedCount++; }else{ unpairedCount++; } } /** This is untested and only gives approximate answers when overlapping reads contain indels. * It may give incorrect answers for same-strange pairs that are shorter than read length. * It might give negative answers but that would be a bug. */ public static int insertSizeMapped(SamLine r1, SamLine r2, boolean requireProperPair){ if(r2==null){return r1.length();} if(!r1.mapped() || !r2.mapped() || !r1.pairedOnSameChrom() || (requireProperPair && !r1.properPair())){ return -1; } int a1=r1.start(true, false); int a2=r2.start(true, false); if(r1.strand()!=r2.strand()){ if(r1.strand()==1){return insertSizeMapped(r2, r1, requireProperPair);} }else if(a1>a2){ return insertSizeMapped(r2, r1, requireProperPair); } int b1=r1.stop(a1, true, false); int b2=r2.stop(a2, true, false); int clen1=r1.calcCigarLength(true, false); int clen2=r2.calcCigarLength(true, false); int mlen1=b1-a1+1; int mlen2=b2-a2+1; int dif1=mlen1-clen1; int dif2=mlen2-clen2; int mlen12=b2-a1+1; if(Tools.overlap(a1, b1, a2, b2)){//hard case return mlen12-Tools.max(dif1, dif2); //Approximate }else{//easy case return mlen12-dif1-dif2; } } public void addToBaseHistogram(final Read r){ addToBaseHistogram2(r); if(r.mate!=null){addToBaseHistogram2(r.mate);} } public void addToBaseHistogram2(final Read r){ if(r==null || r.bases==null){return;} final int pairnum=r.samline==null ? r.pairnum() : r.samline.pairnum(); if(pairnum==1){read2Count++;} final byte[] bases=r.bases; final LongList[] lists=baseHist[pairnum]; for(int i=0; i0; if(AVG_QUAL_HIST_FILE!=null){rs.writeAverageQualityToFile(AVG_QUAL_HIST_FILE, paired);} if(QUAL_HIST_FILE!=null){rs.writeQualityToFile(QUAL_HIST_FILE, paired);} if(BQUAL_HIST_FILE!=null){rs.writeBQualityToFile(BQUAL_HIST_FILE, paired);} if(BQUAL_HIST_OVERALL_FILE!=null){rs.writeBQualityOverallToFile(BQUAL_HIST_OVERALL_FILE);} if(QUAL_COUNT_HIST_FILE!=null){rs.writeQCountToFile(QUAL_COUNT_HIST_FILE, paired);} if(MATCH_HIST_FILE!=null){rs.writeMatchToFile(MATCH_HIST_FILE, paired);} if(INSERT_HIST_FILE!=null){rs.writeInsertToFile(INSERT_HIST_FILE);} if(BASE_HIST_FILE!=null){rs.writeBaseContentToFile(BASE_HIST_FILE, paired);} if(QUAL_ACCURACY_FILE!=null){rs.writeQualityAccuracyToFile(QUAL_ACCURACY_FILE);} if(INDEL_HIST_FILE!=null){rs.writeIndelToFile(INDEL_HIST_FILE);} if(ERROR_HIST_FILE!=null){rs.writeErrorToFile(ERROR_HIST_FILE);} if(LENGTH_HIST_FILE!=null){rs.writeLengthToFile(LENGTH_HIST_FILE);} if(GC_HIST_FILE!=null){rs.writeGCToFile(GC_HIST_FILE, true);} if(ENTROPY_HIST_FILE!=null){rs.writeEntropyToFile(ENTROPY_HIST_FILE, true);} if(IDENTITY_HIST_FILE!=null){rs.writeIdentityToFile(IDENTITY_HIST_FILE, true);} if(TIME_HIST_FILE!=null){rs.writeTimeToFile(TIME_HIST_FILE);} if(BARCODE_STATS_FILE!=null){rs.writeBarcodesToFile(BARCODE_STATS_FILE);} boolean b=rs.errorState; clear(); return b; } clear(); return false; } public void writeAverageQualityToFile(String fname, boolean writePaired){ TextStreamWriter tsw=new TextStreamWriter(fname, overwrite, append, false); tsw.start(); tsw.print("#Quality\tcount1\tfraction1"+(writePaired ? "\tcount2\tfraction2" : "")+"\n"); long sum1=shared.Vector.sum(aqualArray[0]); long sum2=shared.Vector.sum(aqualArray[1]); double mult1=1.0/Tools.max(1, sum1); double mult2=1.0/Tools.max(1, sum2); long y=sum1+sum2; for(int i=0; i=0; i--){ql[i]+=ql[i+1];} for(int i=0; i=0; i--){ ql1[i]+=ql1[i+1]; ql2[i]+=ql2[i+1]; } double div1sum=0; double div2sum=0; double deviation1sum=0; double deviation2sum=0; if(writePaired){ for(int i=0; i0 || ql2[i]>0); i++){ final int a=i+1; double blin, clin, blog, clog; final double div1=(double)Tools.max(1, ql1[i]); final double div2=(double)Tools.max(1, ql2[i]); blin=qs1[i]/div1; clin=qs2[i]/div2; blog=qsd1[i]/div1; clog=qsd2[i]/div2; blog=QualityTools.probErrorToPhredDouble(blog); clog=QualityTools.probErrorToPhredDouble(clog); if(measure){ double bcalc=calcQualityAtPosition(i, 0); double ccalc=calcQualityAtPosition(i, 1); div1sum+=div1; div2sum+=div2; deviation1sum+=Math.abs(blog-bcalc)*div1; deviation2sum+=Math.abs(clog-ccalc)*div2; sb.append(Tools.format("%d\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\n", a, blin, blog, bcalc, clin, clog, ccalc)); }else{ sb.append(Tools.format("%d\t%.3f\t%.3f\t%.3f\t%.3f\n", a, blin, blog, clin, clog)); } } }else{ for(int i=0; i0; i++){ final int a=i+1; double blin, blog; final double div1=(double)Tools.max(1, ql1[i]); blin=qs1[i]/div1; blog=qsd1[i]/div1; blog=QualityTools.probErrorToPhredDouble(blog); if(measure){ double bcalc=calcQualityAtPosition(i, 0); div1sum+=div1; deviation1sum+=Math.abs(blog-bcalc)*div1; sb.append(Tools.format("%d\t%.3f\t%.3f\t%.3f\n", a, blin, blog, bcalc)); }else{ sb.append(Tools.format("%d\t%.3f\t%.3f\n", a, blin, blog)); } } } final double stdev; final double variance; final double entropy; { double stdevSum=0; double varianceSum=0; double entropySum=0; long values=0; for(int pos=0; pos0) { values++;; count+=sum; final double avg=Tools.averageHistogram(array); final double stdev0=Tools.standardDeviationHistogram(array); final double entropy0=calcEntropySuperSlow(array); entropySum+=entropy0; double varSum=0; for(int i=0; i=0; i--){ if(qualMatch[i]+qualSub[i]+qualIns[i]+qualDel[i]>0){break;} max=i; } double devsum=0; double devsumSub=0; long observations=0; for(int i=0; i0){ double mult=1.0/sum; double subRate=(qs)*mult; double errorRate=(qs+qi+qd)*mult; phredSub=QualityTools.probErrorToPhredDouble(subRate); phred=QualityTools.probErrorToPhredDouble(errorRate); double deviation=phred-i; double deviationSub=phredSub-i; if(i==QMIN && deviation<0){deviation=0;} else if(i==QMAX && max==QMAX2 && deviation>0){deviation=0;} if(i==QMIN && deviationSub<0){deviationSub=0;} else if(i==QMAX && max==QMAX2 && deviationSub>0){deviationSub=0;} devsum+=(Math.abs(deviation)*sum); devsumSub+=(Math.abs(deviationSub)*sum); observations+=sum; } } double stdevSum=0; double varianceSum=0; double entropySum=0; long values=0; for(int pos=0; pos0) { values++;; count+=sum; final double avg=Tools.averageHistogram(array); final double stdev=Tools.standardDeviationHistogram(array); final double entropy=calcEntropySuperSlow(array); entropySum+=entropy; double varSum=0; for(int i=0; i0){ double mult=1.0/sum; double subRate=(qs)*mult; double errorRate=(qs+qi+qd)*mult; phredSub=QualityTools.probErrorToPhredDouble(subRate); phred=QualityTools.probErrorToPhredDouble(errorRate); // System.err.println("sub: "+qs+"/"+sum+" -> "+subRate+" -> "+phredSub); } tsw.print(i+"\t"+qm+"\t"+qs+"\t"+qi+"\t"+qd); tsw.print(phred>=0 ? Tools.format("\t%.2f", phred) : "\t"); tsw.print(phredSub>=0 ? Tools.format("\t%.2f\n", phredSub) : "\t\n"); // System.err.println(qm+"\t"+qs+"\t"+qi+"\t"+qd); } tsw.poisonAndWait(); errorState|=tsw.errorState; } private float calcEntropySuperSlow(long[] counts){ //Sum of entropy contributions from each term double eSum=0; final double total=Tools.sum(counts); //Loop over all nonzero kmer counts for(long count : counts){ //Prevent NaN and INF // System.err.println("count="+count); if(count>0){ //Fraction of total represented by this term double pk=count/(double)total; // System.err.println("pk="+pk); //Entropy contribution for this kmer's count double npklogpk=-pk*Math.log(pk); // System.err.println("npklogpk="+npklogpk); eSum+=npklogpk; // System.err.println("eSum="+eSum); } } //eSum now holds entropy in bits. //Multiplier to convert entropy to 0-1 scale. double multiplier=1/Math.log(total); //Adjust entropy to 0-1 scale based on window size float e=(float)(eSum*multiplier); // System.err.println("e="+e); assert(e>=0 && e<=1) : e+", "+eSum+", "+total+"\n"+Arrays.toString(counts)+"\n"+this; //Get rid of negative zero if(e<=0){e=0;} return e; } public void writeMatchToFile(String fname, boolean writePaired){ if(!writePaired){ writeMatchToFileUnpaired(fname); return; } TextStreamWriter tsw=new TextStreamWriter(fname, overwrite, false, false); tsw.start(); tsw.print("#BaseNum\tMatch1\tSub1\tDel1\tIns1\tN1\tOther1\tMatch2\tSub2\tDel2\tIns2\tN2\tOther2\n"); final long[] ms1=matchSum[0], ds1=delSum[0], is1=insSum[0], ss1=subSum[0], ns1=nSum[0], cs1=clipSum[0], os1=otherSum[0]; final long[] ms2=matchSum[1], ds2=delSum[1], is2=insSum[1], ss2=subSum[1], ns2=nSum[1], cs2=clipSum[1], os2=otherSum[1]; for(int i=0; i0 || y>0 || !skipZeroIndel){ bb.clear(); bb.append(i).tab().append(x).tab().append(y).nl(); bsw.print(bb); } } //TODO: Disabled because it was irritating when graphing. Should write to a different file. // tsw.print("#Length_bin\tDeletions\n"); // max=delHist2.size; // for(int i=0; i0 || !skipZeroIndel){ // tsw.print((i*DEL_BIN)+"\t"+x+"\n"); // } // } bsw.poisonAndWait(); errorState|=bsw.errorState; } public void writeErrorToFile(String fname){ writeHistogramToFile(fname, "#Errors\tCount\n", errorHist, false); } public void writeLengthToFile(String fname){ writeHistogramToFile(fname, "#Length\tCount\n", lengthHist, false); } public void writeTimeToFile(String fname){ writeHistogramToFile(fname, "#Time\tCount\n", timeHist, false); } public void writeBarcodesToFile(String fname) { ArrayList barcodes=new ArrayList(); for(Barcode b : barcodeMap.values()) { barcodes.add(b); } Collections.sort(barcodes); ByteStreamWriter bsw=new ByteStreamWriter(fname, overwrite, false, false); bsw.start(); long sum=0; for(Barcode b : barcodes) { sum+=b.count(); } bsw.print("#Reads\t").print(sum).nl(); bsw.print("#Barcodes\t").print(barcodes.size()).nl(); ByteBuilder bb=new ByteBuilder(40); for(Barcode b : barcodes) { bb.clear().append(b.name).tab().append(b.count()).nl(); bsw.print(bb); } bsw.poisonAndWait(); errorState|=bsw.errorState; } public void writeHistogramToFile(String fname, String header, LongList hist, boolean printZeros){ ByteStreamWriter bsw=new ByteStreamWriter(fname, overwrite, false, false); bsw.start(); bsw.print(header); int max=hist.size; ByteBuilder bb=new ByteBuilder(40); for(int i=0; i0 || printZeros){ bb.clear().append(i).tab().append(x).nl(); bsw.print(bb); } } bsw.poisonAndWait(); errorState|=bsw.errorState; } public void writeHistogramToFile(String fname, String header, SuperLongList hist, boolean printZeros){ ByteStreamWriter bsw=new ByteStreamWriter(fname, overwrite, false, false); bsw.start(); bsw.print(header); hist.sort(); long max=hist.max(); long[] array=hist.array(); LongList list=hist.list(); ByteBuilder bb=new ByteBuilder(40); for(int i=0; i0 || printZeros){ bb.clear().append(i).tab().append(x).nl(); bsw.print(bb); } } {//TODO: This part does not bother printing zeros regardless of the flag. long prev=-1; int streak=0; for(int i=0; i0){//Print previous key bb.clear().append(prev).tab().append(streak).nl(); bsw.print(bb); } streak=1; prev=x; } } if(streak>0){//Print final key bb.clear().append(prev).tab().append(streak).nl(); bsw.print(bb); } } bsw.poisonAndWait(); errorState|=bsw.errorState; } public void writeGCToFile(String fname, boolean printZeros){ final long[] hist; if(GC_BINS_AUTO && gcMaxReadLen+10 || printZeros){ //This assumes GC_BINS==100 // tsw.print(i+"\t"+x+"\n"); if(GC_PLOT_X){ bb.clear(); bb.append(i*gcMult, 1).tab().append(x).tab(); bb.append(sum*fractionMult, 3).tab(); int len=(int)((x*1000)/countsPerX); for(int j=0; j0){ if((x*1000f)/countsPerX>0.1f){bb.append('x');} else{bb.append('.');} } bb.append('\n'); bsw.print(bb); }else{ bb.clear().append(i*gcMult, 1).tab().append(x).nl(); bsw.print(bb); } } } bsw.poisonAndWait(); errorState|=bsw.errorState; } public void writeEntropyToFile(String fname, boolean printZeros){ final long[] hist=entropyHist; final int bins=hist.length; final double mult=1.0/Tools.max(1, bins-1); final long total=shared.Vector.sum(hist); final long max=Tools.max(hist); final double countsPerX=Tools.max(1, ((max*1000.0)/40)); final double fractionMult=1.0/Tools.max(1, total); long sum=0; double mean=Tools.averageHistogram(hist)*mult; double median=Tools.percentileHistogram(hist, 0.5)*mult; double mode=Tools.calcModeHistogram(hist)*mult; double stdev=Tools.standardDeviationHistogram(hist)*mult; ByteBuilder bb=new ByteBuilder(256); bb.append("#Mean\t").append(mean, 6).nl(); bb.append("#Median\t").append(median, 6).nl(); bb.append("#Mode\t").append(mode, 6).nl(); bb.append("#STDev\t").append(stdev, 6).nl(); bb.append("#Value\tCount\n"); ByteStreamWriter bsw=new ByteStreamWriter(fname, overwrite, false, false); bsw.start(); bsw.print(bb); for(int i=0; i0 || printZeros){ bb.clear().append(i*mult, 4).tab().append(x).nl(); bsw.print(bb); } } bsw.poisonAndWait(); errorState|=bsw.errorState; } public void writeIdentityToFile(String fname, boolean printZeros){ final long[] hist, histb; if(ID_BINS_AUTO && idMaxReadLen+10 || printZeros){ tsw.print(Tools.format("%.1f", i*mult)+"\t"+x+"\t"+x2+"\n"); } } tsw.poisonAndWait(); errorState|=tsw.errorState; } //Tracks to see if read2s have been encountered, for displaying stats. private long read2Count=0; public long pairedCount=0; public long unpairedCount=0; private final int QMIN=Read.MIN_CALLED_QUALITY(); private final int QMAX=Read.MAX_CALLED_QUALITY(); private final int QMAX2=QMAX+1; public final long[][] aqualArray; public final long[][] qualLength; public final long[][] qualSum; public final long[][][] bqualHist; public final long[] bqualHistOverall; public final long[][] qcountHist; public final double[][] qualSumDouble; public final long[][] matchSum; public final long[][] delSum; public final long[][] insSum; public final long[][] subSum; public final long[][] nSum; public final long[][] clipSum; public final long[][] otherSum; public final long[] qualMatch; public final long[] qualSub; public final long[] qualIns; public final long[] qualDel; public final long[] gcHist; public final long[] entropyHist; public final EntropyTracker eTracker; public final long[] idHist; public final long[] idBaseHist; private int gcMaxReadLen=1; private int idMaxReadLen=1; public final LongList[][] baseHist; public final HashMap barcodeMap; /** Insert size */ public final LongList insertHist; /** Read length */ public final SuperLongList lengthHist; /** Number errors per read */ public final LongList errorHist; /** Insertion length */ public final LongList insHist; /** Deletion length */ public final LongList delHist; /** Deletion length, binned */ public final LongList delHist2; /** Time */ public final LongList timeHist; public static boolean REQUIRE_PROPER_PAIR=true; public static int MAXLEN=6000; public static int MAXINSERTLEN=80000; @Deprecated public static int MAXLENGTHLEN=80000; public static final int MAXTIMELEN=80000; public static final int MAXINSLEN=1000; public static final int MAXDELLEN=1000; public static final int MAXDELLEN2=1000000; public static final int DEL_BIN=100; public static int ID_BINS=100; public static boolean ID_BINS_AUTO=false; public static int GC_BINS=100; public static boolean GC_BINS_AUTO=false; public static boolean GC_PLOT_X=false; public static int ENTROPY_BINS=1000; public static double GCMean; public static double GCMedian; public static double GCMode; public static double GCSTDev; // public static double entropyMean; // public static double entropyMedian; // public static double entropyMode; // public static double entropySTDev; public boolean errorState=false; public static ReadStats merged=null; // public static double matedPercent=0; public static void clear(){ COLLECT_QUALITY_STATS=false; COLLECT_QUALITY_ACCURACY=false; COLLECT_MATCH_STATS=false; COLLECT_INSERT_STATS=false; COLLECT_BASE_STATS=false; COLLECT_INDEL_STATS=false; COLLECT_GC_STATS=false; COLLECT_ENTROPY_STATS=false; COLLECT_ERROR_STATS=false; COLLECT_LENGTH_STATS=false; COLLECT_IDENTITY_STATS=false; COLLECT_TIME_STATS=false; COLLECT_BARCODE_STATS=false; AVG_QUAL_HIST_FILE=null; QUAL_HIST_FILE=null; BQUAL_HIST_FILE=null; QUAL_COUNT_HIST_FILE=null; BQUAL_HIST_OVERALL_FILE=null; QUAL_ACCURACY_FILE=null; MATCH_HIST_FILE=null; INSERT_HIST_FILE=null; BASE_HIST_FILE=null; INDEL_HIST_FILE=null; ERROR_HIST_FILE=null; LENGTH_HIST_FILE=null; GC_HIST_FILE=null; ENTROPY_HIST_FILE=null; IDENTITY_HIST_FILE=null; TIME_HIST_FILE=null; BARCODE_STATS_FILE=null; if(objectList!=null) {objectList.clear();} // objectList=null; } public static ArrayList objectList=new ArrayList(); public static boolean COLLECT_QUALITY_STATS=false; public static boolean COLLECT_QUALITY_ACCURACY=false; public static boolean COLLECT_MATCH_STATS=false; public static boolean COLLECT_INSERT_STATS=false; public static boolean COLLECT_BASE_STATS=false; public static boolean COLLECT_INDEL_STATS=false; public static boolean COLLECT_GC_STATS=false; public static boolean COLLECT_ENTROPY_STATS=false; public static boolean COLLECT_ERROR_STATS=false; public static boolean COLLECT_LENGTH_STATS=false; public static boolean COLLECT_IDENTITY_STATS=false; public static boolean COLLECT_TIME_STATS=false; public static boolean COLLECT_BARCODE_STATS=false; public static boolean collectingStats(){ return COLLECT_QUALITY_STATS || COLLECT_QUALITY_ACCURACY || COLLECT_MATCH_STATS || COLLECT_INSERT_STATS || COLLECT_BASE_STATS || COLLECT_INDEL_STATS || COLLECT_GC_STATS || COLLECT_ENTROPY_STATS || COLLECT_ERROR_STATS || COLLECT_LENGTH_STATS || COLLECT_IDENTITY_STATS || COLLECT_TIME_STATS || COLLECT_BARCODE_STATS; } public static boolean usePairGC=true; public static boolean allowEntropyNs=true; public static String AVG_QUAL_HIST_FILE=null; public static String QUAL_HIST_FILE=null; public static String BQUAL_HIST_FILE=null; public static String QUAL_COUNT_HIST_FILE=null; public static String BQUAL_HIST_OVERALL_FILE=null; public static String QUAL_ACCURACY_FILE=null; public static String MATCH_HIST_FILE=null; public static String INSERT_HIST_FILE=null; public static String BASE_HIST_FILE=null; public static String INDEL_HIST_FILE=null; public static String ERROR_HIST_FILE=null; public static String LENGTH_HIST_FILE=null; public static String GC_HIST_FILE=null; public static String ENTROPY_HIST_FILE=null; public static String IDENTITY_HIST_FILE=null; public static String TIME_HIST_FILE=null; public static String BARCODE_STATS_FILE=null; public static boolean overwrite=true; public static boolean append=false; public static final boolean verbose=false; public static boolean skipZeroInsertCount=true; public static boolean skipZeroIndel=true; }