package aligner; import dna.AminoAcid; import shared.KillSwitch; import shared.Tools; /** * Based on MSA9PBA, but reduced to a single matrix. */ public final class SingleStateAlignerFlat implements Aligner { public SingleStateAlignerFlat(){} private void prefillTopRow(){ final int[] header=packed[0]; final int qlen=rows; for(int i=0; i<=columns; i++){ int x=columns-i+1; int qbases=qlen-x; //Minimal points to prefer a leftmost alignment header[i]=qbases<=0 ? 0 : ((-qbases)<maxColumns0){//Make a new matrix packed=KillSwitch.allocInt2D(maxRows+1, maxColumns+1); prefillLeftColumnStartingAt(1); }else{//Copy old rows assert(maxRows0>0 && maxColumns0>0); assert(maxRows>maxRows0 && maxColumns<=maxColumns0); packed=KillSwitch.allocInt2D(maxRows+1); for(int i=0; imaxRows || columns>maxColumns){ throw new RuntimeException("rows="+rows+", maxRows="+maxRows+", cols="+columns+", maxCols="+maxColumns+"\n"+new String(read)+"\n"); } assert(rows<=maxRows) : "Check that values are in-bounds before calling this function: "+rows+", "+maxRows; assert(columns<=maxColumns) : "Check that values are in-bounds before calling this function: "+columns+", "+maxColumns; assert(refStartLoc>=0) : "Check that values are in-bounds before calling this function: "+refStartLoc; assert(refEndLoc=delScore ? diagScore : delScore; score=score>=insScore ? score : insScore; packed[row][col]=score; } // iterationsUnlimited+=columns; } int maxCol=-1; int maxState=-1; int maxStart=-1; int maxScore=Integer.MIN_VALUE; for(int col=1; col<=columns; col++){ int x=packed[rows][col]; if((x&SCOREMASK)>maxScore){ maxScore=x; maxCol=col; maxState=x&MODEMASK; maxStart=x&STARTMASK; } } maxScore>>=SCOREOFFSET; // System.err.println("Returning "+rows+", "+maxCol+", "+maxState+", "+maxScore+"; minScore="+minScore); return maxScore0 && col>0){ byte q=query[row-1]; byte r=ref[refStartLoc+col-1]; boolean defined=(AminoAcid.isFullyDefined(q) && AminoAcid.isFullyDefined(r)); state=(packed[row][col]&MODEMASK); if(state==MODE_MATCH){ col--; row--; out[outPos]=defined ? (byte)'m' : (byte)'N'; }else if(state==MODE_SUB){ col--; row--; out[outPos]=defined ? (byte)'S' : (byte)'N'; }else if(state==MODE_N){ col--; row--; out[outPos]='N'; }else if(state==MODE_DEL){ col--; out[outPos]='D'; }else if(state==MODE_INS){ row--; // out[outPos]='I'; if(col>=0 && col0){ out[outPos]='C'; outPos++; row--; col--; } if(col>0){ //do nothing } } //Shrink and reverse the string byte[] out2=new byte[outPos]; for(int i=0; i0 && col>0){ byte q=query[row-1]; byte r=ref[refStartLoc+col-1]; boolean defined=(AminoAcid.isFullyDefined(q) && AminoAcid.isFullyDefined(r)); state=(packed[row][col]&MODEMASK); if(state==MODE_MATCH){ col--; row--; match+=(defined ? 1 : 0); noref+=(defined ? 0 : 1); }else if(state==MODE_SUB){ col--; row--; sub+=(defined ? 1 : 0); noref+=(defined ? 0 : 1); }else if(state==MODE_N){//Can't currently happen col--; row--; noref++; }else if(state==MODE_DEL){ col--; del++; }else if(state==MODE_INS){ row--; boolean edge=(col<=1 || col>=columns); ins+=(edge ? 0 : 1); clip+=(edge ? 1 : 0); }else{ assert(false) : state; } } assert(row==0 || col==0); if(col!=row){//Not sure what this is doing while(row>0){ clip++; row--; col--; } if(col>0){ //do nothing } } if(extra!=null){ assert(extra.length==5); extra[0]=match; extra[1]=sub; extra[2]=del; extra[3]=ins; extra[4]=noref; extra[5]=clip; } float len=match+sub+ins+del+noref*0.1f; float id=match/Tools.max(1.0f, len); return id; } /** @return {score, bestRefStart, bestRefStop} */ @Override public final int[] score(final byte[] read, final byte[] ref, final int refStartLoc, final int refEndLoc, final int maxRow, final int maxCol, final int maxState/*, final int maxScore, final int maxStart*/){ int row=maxRow; int col=maxCol; int state=maxState; assert(maxState>=0 && maxState=0 && maxRow=0 && maxColdifC){ score+=POINTSoff_NOREF; difR--; } row+=difR; col+=difR; } assert(refStartLoc<=refEndLoc); assert(row==rows); final int bestRefStop=refStartLoc+col-1; while(row>0 && col>0){ // final byte c=read[row-1]; // final byte r=ref[refStartLoc+col-1]; // final boolean defined=(AminoAcid.isFullyDefined(c) && AminoAcid.isFullyDefined(r)); state=(packed[row][col]&MODEMASK); if(state==MODE_MATCH){ col--; row--; }else if(state==MODE_SUB){ col--; row--; }else if(state==MODE_N){ col--; row--; }else if(state==MODE_DEL){ col--; }else if(state==MODE_INS){ row--; }else{ assert(false) : state; } } // assert(false) : row+", "+col; if(row>col){ col-=row; } final int bestRefStart=refStartLoc+col; score>>=SCOREOFFSET; // System.err.println("t2\t"+score+", "+maxScore+", "+maxStart+", "+bestRefStart); int[] rvec; if(bestRefStartrefEndLoc){ //Suggest extra padding in cases of overflow int padLeft=Tools.max(0, refStartLoc-bestRefStart); int padRight=Tools.max(0, bestRefStop-refEndLoc); rvec=new int[] {score, bestRefStart, bestRefStop, padLeft, padRight}; }else{ rvec=new int[] {score, bestRefStart, bestRefStop}; } return rvec; } /** Will not fill areas that cannot match minScore. * @return {score, bestRefStart, bestRefStop} */ @Override public final int[] fillAndScoreLimited(byte[] read, byte[] ref, int refStartLoc, int refEndLoc, int minScore){ int a=Tools.max(0, refStartLoc); int b=Tools.min(ref.length-1, refEndLoc); assert(b>=a); if(b-a>=maxColumns){ System.err.println("Warning: Max alignment columns exceeded; restricting range. "+(b-a+1)+" > "+maxColumns); assert(false) : refStartLoc+", "+refEndLoc; b=Tools.min(ref.length-1, a+maxColumns-1); } int[] max=fillLimited(read, ref, a, b, minScore); // return max==null ? null : new int[] {max[3], 0, max[1]}; int[] score=(max==null ? null : score(read, ref, a, b, max[0], max[1], max[2]/*, max[3], max[4]*/)); return score; } public static final String toString(byte[] ref, int startLoc, int stopLoc){ StringBuilder sb=new StringBuilder(stopLoc-startLoc+1); for(int i=startLoc; i<=stopLoc; i++){sb.append((char)ref[i]);} return sb.toString(); } // public int maxScoreByIdentity(int len, float identity){ // assert(identity>=0 && identity<=1); // return (int)(len*(identity*POINTS_MATCH+(1-identity)*POINTS_SUB)); // } @Override public int minScoreByIdentity(int len, float identity){ assert(identity>=0 && identity<=1); int a=(int)(len*(identity*POINTS_MATCH+(1-identity)*POINTS_SUB)); int b=(int)(len*(identity*POINTS_MATCH+(1-identity)*POINTS_INS)); int c=(int)(len*(1*POINTS_MATCH+((1/(Tools.max(identity, 0.000001f)))-1)*POINTS_DEL)); return Tools.min(a, b, c); } private static int calcDelScoreOffset(int len){ if(len<=0){return 0;} int score=POINTSoff_DEL*len; return score; } // // public static int calcInsScore(int len){ // if(len<=0){return 0;} // int score=POINTS_INS; // // if(len>1){ // score+=(len-1)*POINTS_INS2; // } // return score; // } // // private static int calcInsScoreOffset(int len){ // if(len<=0){return 0;} // int score=POINTSoff_INS; // // if(len>1){ // score+=(len-1)*POINTSoff_INS2; // } // return score; // } @Override public int rows(){return rows;} @Override public int columns(){return columns;} private int maxRows; private int maxColumns; private int[][] packed; public static final int MODEBITS=3; public static final int STARTBITS=9; public static final int LOWBITS=MODEBITS+STARTBITS; public static final int SCOREBITS=32-STARTBITS; public static final int MAX_START=((1<