package align2; import java.util.Arrays; import dna.AminoAcid; import shared.KillSwitch; import shared.Tools; import stream.SiteScore; /** * Modification of MultiStateAligner9ts to replace fixed affine steps with an array */ public final class MultiStateAligner11ts extends MSA{ public static void main(String[] args){ int x=Integer.parseInt(args[0]); MultiStateAligner11ts msa=new MultiStateAligner11ts(100, 100); System.out.println(msa.calcDelScore(x, true)); // byte[] read=args[0].getBytes(); // byte[] ref=args[1].getBytes(); // // byte[] original=ref; // // MultiStateAligner11ts msa=new MultiStateAligner11ts(read.length, ref.length); // // System.out.println("Initial: "); // printMatrix(msa.packed, read.length, ref.length, TIMEMASK, SCOREOFFSET); // // int[] max=msa.fillLimited(read, ref, 0, ref.length-1, 0, null); // // System.out.println("Max: "+Arrays.toString(max)); // // System.out.println("Final: "); // printMatrix(msa.packed, read.length, ref.length, TIMEMASK, SCOREOFFSET); // // byte[] out=msa.traceback(read, ref, 0, ref.length-1, max[0], max[1], max[2], false); // // int[] score=null; // score=msa.score(read, ref, 0, ref.length-1, max[0], max[1], max[2], false); // // System.out.println(new String(ref)); // System.out.println(new String(read)); // System.out.println(new String(out)); // System.out.println("Score: "+Arrays.toString(score)); } public MultiStateAligner11ts(int maxRows_, int maxColumns_){ super(maxRows_, maxColumns_); packed=KillSwitch.allocInt3D(3, maxRows+1, maxColumns+1); grefbuffer=KillSwitch.allocByte1D(maxColumns+2); vertLimit=KillSwitch.allocInt1D(maxRows+1); horizLimit=KillSwitch.allocInt1D(maxColumns+1); Arrays.fill(vertLimit, BADoff); Arrays.fill(horizLimit, BADoff); // for(int i=0; i0 && greflimit<500){ System.err.println(new String(gref, 0, greflimit)); } assert(gref!=null) : "Excessively long read:\n"+new String(read); return fillLimitedX(read, gref, 0, greflimit, minScore); } } /** return new int[] {rows, maxC, maxS, max}; * Will not fill areas that cannot match minScore */ private final int[] fillLimitedX(byte[] read, byte[] ref, int refStartLoc, int refEndLoc, int minScore){ if(verbose){System.err.println("fillLimitedX");} // minScore=0; // assert(minScore>0); rows=read.length; columns=refEndLoc-refStartLoc+1; final int halfband=(bandwidth<1 && bandwidthRatio<=0) ? 0 : Tools.max(Tools.min(bandwidth<1 ? 9999999 : bandwidth, bandwidthRatio<=0 ? 9999999 : 8+(int)(rows*bandwidthRatio)), (columns-rows+8))/2; if(minScore<1 || (columns+rows<90) || ((halfband<1 || halfband*3>columns) && (columns>read.length+Tools.min(170, read.length+20)))){ // assert(false) : minScore; // assert(minScore>0) : minScore; // assert(false) : +minScore+", "+columns+", "+read.length+", "+Tools.min(100, read.length); return fillUnlimited(read, ref, refStartLoc, refEndLoc); } // final int BARRIER_I2=columns-BARRIER_I1; final int BARRIER_I2=rows-BARRIER_I1, BARRIER_I2b=columns-1; final int BARRIER_D2=rows-BARRIER_D1; minScore-=MIN_SCORE_ADJUST; //Increases quality trivially assert(rows<=maxRows) : "Check that values are in-bounds before calling this function: "+rows+", "+maxRows+"\n"+ refStartLoc+", "+refEndLoc+", "+rows+", "+maxRows+", "+columns+", "+maxColumns+"\n"+new String(read)+"\n"; assert(columns<=maxColumns) : "Check that values are in-bounds before calling this function: "+columns+", "+maxColumns+"\n"+ refStartLoc+", "+refEndLoc+", "+rows+", "+maxRows+", "+columns+", "+maxColumns+"\n"+new String(read)+"\n"; assert(refStartLoc>=0) : "Check that values are in-bounds before calling this function: "+refStartLoc+"\n"+ refStartLoc+", "+refEndLoc+", "+rows+", "+maxRows+", "+columns+", "+maxColumns+"\n"+new String(read)+"\n"; assert(refEndLocBADoff); //TODO: Actually, it needs to be substantially more. assert(subfloor=0; i--){ byte c=read[i]; if(AminoAcid.isFullyDefined(c)){ vertLimit[i]=Tools.max(vertLimit[i+1]-(prevDefined ? POINTSoff_MATCH2 : POINTSoff_MATCH), floor); prevDefined=true; }else{ vertLimit[i]=Tools.max(vertLimit[i+1]-POINTSoff_NOCALL, floor); prevDefined=false; } } horizLimit[columns]=minScore_off; prevDefined=false; for(int i=columns-1; i>=0; i--){ byte c=ref[refStartLoc+i]; if(AminoAcid.isFullyDefined(c)){ horizLimit[i]=Tools.max(horizLimit[i+1]-(prevDefined ? POINTSoff_MATCH2 : POINTSoff_MATCH), floor); prevDefined=true; }else{ horizLimit[i]=Tools.max(horizLimit[i+1]-(prevDefined && c==GAPC ? POINTSoff_DEL : POINTSoff_NOREF), floor); prevDefined=false; } } // vertLimit[rows]=minScore_off; // for(int i=rows-1; i>=0; i--){ // vertLimit[i]=Tools.max(vertLimit[i+1]-POINTSoff_MATCH2, floor); // } // // horizLimit[columns]=minScore_off; // for(int i=columns-1; i>=0; i--){ // horizLimit[i]=Tools.max(horizLimit[i+1]-POINTSoff_MATCH2, floor); // } for(int row=1; row<=rows; row++){ final int colStart=(halfband<1 ? minGoodCol : Tools.max(minGoodCol, row-halfband)); final int colStop=(halfband<1 ? maxGoodCol : Tools.min(maxGoodCol, row+halfband*2-1)); minGoodCol=-1; maxGoodCol=-2; final int vlimit=vertLimit[row]; if(verbose2){ System.out.println(); System.out.println("row="+row); System.out.println("colStart="+colStart); System.out.println("colStop="+colStop); System.out.println("vlimit="+(vlimit>>SCOREOFFSET)+"\t"+(vlimit)); } if(colStart<0 || colStop1){ assert(row>0); packed[MODE_MS][row][colStart-1]=subfloor; packed[MODE_INS][row][colStart-1]=subfloor; packed[MODE_DEL][row][colStart-1]=subfloor; } for(int col=colStart; col<=columns; col++){ if(verbose2){ System.err.println("\ncol "+col); } final byte call0=(row<2 ? (byte)'?' : read[row-2]); final byte call1=read[row-1]; final byte ref0=(col<2 ? (byte)'!' : ref[refStartLoc+col-2]); final byte ref1=ref[refStartLoc+col-1]; final boolean gap=(ref1==GAPC); assert(call1!=GAPC); // final boolean match=(read[row-1]==ref[refStartLoc+col-1]); // final boolean prevMatch=(row<2 || col<2 ? false : read[row-2]==ref[refStartLoc+col-2]); final boolean match=(call1==ref1 && ref1!='N'); final boolean prevMatch=(call0==ref0 && ref0!='N'); // System.err.println("") iterationsLimited++; final int limit=Tools.max(vlimit, horizLimit[col]); final int limit3=Tools.max(floor, (match ? limit-POINTSoff_MATCH2 : limit-POINTSoff_SUB3)); final int delNeeded=Tools.max(0, row-col-1); final int insNeeded=Tools.max(0, (rows-row)-(columns-col)-1); final int delPenalty=calcDelScoreOffset(delNeeded); final int insPenalty=calcInsScoreOffset(insNeeded); final int scoreFromDiag_MS=packed[MODE_MS][row-1][col-1]&SCOREMASK; final int scoreFromDel_MS=packed[MODE_DEL][row-1][col-1]&SCOREMASK; final int scoreFromIns_MS=packed[MODE_INS][row-1][col-1]&SCOREMASK; final int scoreFromDiag_DEL=packed[MODE_MS][row][col-1]&SCOREMASK; final int scoreFromDel_DEL=packed[MODE_DEL][row][col-1]&SCOREMASK; final int scoreFromDiag_INS=packed[MODE_MS][row-1][col]&SCOREMASK; final int scoreFromIns_INS=packed[MODE_INS][row-1][col]&SCOREMASK; // if(scoreFromDiag_MS=scoreD && scoreMS>=scoreI){ score=scoreMS; time=(prevMatch ? streak+1 : 1); prevState=MODE_MS; }else if(scoreD>=scoreI){ score=scoreD; time=1; prevState=MODE_DEL; }else{ score=scoreI; time=1; prevState=MODE_INS; } // if(time>MAX_TIME){time=MAX_TIME-MASK5;} // assert(score>=MINoff_SCORE) : "Score overflow - use MSA2 instead"; // assert(score<=MAXoff_SCORE) : "Score overflow - use MSA2 instead"; //// packed[MODE_MS][row][col]=(score|prevState|time); // packed[MODE_MS][row][col]=(score|time); // assert((score&SCOREMASK)==score); //// assert((prevState&MODEMASK)==prevState); // assert((time&TIMEMASK)==time); }else{ int scoreMS; if(ref1!='N' && call1!='N'){ scoreMS=scoreFromDiag_MS+(prevMatch ? (streak<=1 ? POINTSoff_SUBR : POINTSoff_SUB) : POINTSoff_SUB_ARRAY[streak+1]); // scoreMS=scoreFromDiag_MS+(prevMatch ? (streak<=1 ? POINTSoff_SUBR : POINTSoff_SUB) : // (streak==0 ? POINTSoff_SUB : streak=scoreD && scoreMS>=scoreI){ score=scoreMS; time=(prevMatch ? 1 : streak+1); // time=(prevMatch ? (streak==1 ? 3 : 1) : streak+1); prevState=MODE_MS; }else if(scoreD>=scoreI){ score=scoreD; time=1; prevState=MODE_DEL; }else{ score=scoreI; time=1; prevState=MODE_INS; } // if(time>MAX_TIME){time=MAX_TIME-MASK5;} // assert(score>=MINoff_SCORE) : "Score overflow - use MSA2 instead"; // assert(score<=MAXoff_SCORE) : "Score overflow - use MSA2 instead"; //// packed[MODE_MS][row][col]=(score|prevState|time); // packed[MODE_MS][row][col]=(score|time); // assert((score&SCOREMASK)==score); //// assert((prevState&MODEMASK)==prevState); // assert((time&TIMEMASK)==time); } final int limit2; if(delNeeded>0){ limit2=limit-delPenalty; }else if(insNeeded>0){ limit2=limit-insPenalty; }else{ limit2=limit; } assert(limit2>=limit); if(verbose2){System.err.println("MS: \tlimit2="+(limit2>>SCOREOFFSET)+"\t, score="+(score>>SCOREOFFSET));} if(score>=limit2){ maxGoodCol=col; if(minGoodCol<0){minGoodCol=col;} }else{ score=subfloor; } if(time>MAX_TIME){time=MAX_TIME-MASK5;} assert(score>=MINoff_SCORE || score==BADoff) : "Score overflow - use MSA2 instead"; assert(score<=MAXoff_SCORE) : "Score overflow - use MSA2 instead"; // packed[MODE_MS][row][col]=(score|prevState|time); packed[MODE_MS][row][col]=(score|time); assert((score&SCOREMASK)==score); // assert((prevState&MODEMASK)==prevState); assert((time&TIMEMASK)==time); } } if((scoreFromDiag_DEL<=limit && scoreFromDel_DEL<=limit) || rowBARRIER_D2){ // assert((scoreFromDiag_DEL<=limit && scoreFromDel_DEL<=limit)) : scoreFromDiag_DEL+", "+row; packed[MODE_DEL][row][col]=subfloor; }else{//Calculate DEL score final int streak=packed[MODE_DEL][row][col-1]&TIMEMASK; int scoreMS=scoreFromDiag_DEL+POINTSoff_DEL; int scoreD=scoreFromDel_DEL+(streak==0 ? POINTSoff_DEL : streak=scoreD){ score=scoreMS; time=1; prevState=MODE_MS; }else{ score=scoreD; time=streak+1; prevState=MODE_DEL; } final int limit2; if(insNeeded>0){ limit2=limit-insPenalty; }else if(delNeeded>0){ limit2=limit-calcDelScoreOffset(time+delNeeded)+calcDelScoreOffset(time); }else{ limit2=limit; } assert(limit2>=limit); if(verbose2){System.err.println("DEL: \tlimit2="+(limit2>>SCOREOFFSET)+"\t, score="+(score>>SCOREOFFSET));} if(score>=limit2){ maxGoodCol=col; if(minGoodCol<0){minGoodCol=col;} }else{ score=subfloor; } if(time>MAX_TIME){time=MAX_TIME-MASK5;} assert(score>=MINoff_SCORE || score==BADoff) : "Score overflow - use MSA2 instead"; assert(score<=MAXoff_SCORE) : "Score overflow - use MSA2 instead"; // packed[MODE_DEL][row][col]=(score|prevState|time); packed[MODE_DEL][row][col]=(score|time); assert((score&SCOREMASK)==score); // assert((prevState&MODEMASK)==prevState); assert((time&TIMEMASK)==time); } // if(gap || (scoreFromDiag_INS<=limit && scoreFromIns_INS<=limit) || colBARRIER_I2){ if(gap || (scoreFromDiag_INS<=limit && scoreFromIns_INS<=limit) || (row1) || (row>BARRIER_I2 && col=colStop){ if(col>colStop && (maxGoodCol0)){break;} if(row>1){ packed[MODE_MS][row-1][col+1]=subfloor; packed[MODE_INS][row-1][col+1]=subfloor; packed[MODE_DEL][row-1][col+1]=subfloor; } } } } int maxCol=-1; int maxState=-1; int maxScore=Integer.MIN_VALUE; for(int state=0; statemaxScore){ maxScore=x; maxCol=col; maxState=state; } } } assert(maxScore>=BADoff); // if(maxScore==BADoff){ // return null; // } // if(maxScore *
* Uses 3 matrices to optimize score with multiple affine transforms. * These correspond to 3 states:
* MODE_MS (match/substitution, aka diagonal)
* MODE_DEL (deletion, aka vertical)
* MODE_INS (insertion, aka vertical)
*
* Each cell is a 32-bit signed int, with score in the upper bits and * a counter for time spent in the current state in the lower bits.
* * @param read Query bases * @param ref Reference bases, or gapped reference subsequence with gap symbols. * @param refStartLoc Column 1 starts at this position of the reference (0 for gapped reference). * @param refEndLoc Align up to this position in the reference. * @return int[] {rows, maxCol, maxState, maxScore} */ private final int[] fillUnlimited(byte[] read, byte[] ref, int refStartLoc, int refEndLoc){ rows=read.length; //Number rows to fill, equal to query length. columns=refEndLoc-refStartLoc+1; //Number of columns to fill, equal to relevant portion of reference. //Ensure the required matrix size is within the preallocated matrix size assert(rows<=maxRows) : "Check that values are in-bounds before calling this function: rows="+rows+"/"+maxRows+new String(read)+"\n"; assert(columns<=maxColumns) : "Check that values are in-bounds before calling this function: columns="+columns+"/"+maxColumns+new String(read)+"\n"; //Ensure reference coordinates are within the array length assert(refStartLoc>=0) : "Check that values are in-bounds before calling this function: "+refStartLoc; assert(refEndLocBADoff && subfloor*2L>BADoff) : (read.length-1)+", "+maxGain+", "+subfloor+", "+(subfloor*2)+", "+BADoff+"\n" +rows+", "+columns+", "+POINTSoff_MATCH2+", "+SCOREOFFSET+"\n"+new String(read)+"\n"; //Barriers are used to prevent indel alignments at query tips. //Only diagonals (match/sub) are allowed in those regions, e.g., the first and last 2 or 3 bp. // final int BARRIER_I2=columns-BARRIER_I1; final int BARRIER_I2=rows-BARRIER_I1, BARRIER_I2b=columns-1; final int BARRIER_D2=rows-BARRIER_D1; //Iterate through all rows to fill, except row 0 which is constant; query base 0 corresponds to row 1. for(int row=1; row<=rows; row++){ //Iterate through all columns to fill, except column 0 which is constant; ref base 0 corresponds to column 1. for(int col=1; col<=columns; col++){ iterationsUnlimited++; //Counter for tracking performance; not necessary final byte call0=(row<2 ? (byte)'?' : read[row-2]); //Previous query base final byte call1=read[row-1]; //Current query base final byte ref0=(col<2 ? (byte)'!' : ref[refStartLoc+col-2]); //Previous reference base final byte ref1=ref[refStartLoc+col-1]; //current reference base //Determine whether the current and previous (from the diagonal) bases were matches. //If either the query or ref is N, this is not a match. It's also not a substitution, though; it's neither. final boolean match=(call1==ref1 && ref1!='N'); final boolean prevMatch=(call0==ref0 && ref0!='N'); //If the reference base is the special gap character, a deletion is forced (this actually represents 128 consecutive deletions) final boolean gap=(ref1==GAPC); assert(call1!=GAPC); //Next, each of the 3 matrices is updated. There is one block (technically an if/else statement) for each matrix. //The first block fills in the cell for the MS (match/sub) matrix. //The MS matrix always comes from the diagonal. if(gap){ //In this case a deletion is forced, so the MS cell is marked invalid packed[MODE_MS][row][col]=subfloor; }else{//Calculate match and sub scores //In each case the previous cell is the diagonal, but it can be from any of the 3 matrices. final int scoreFromDiag=packed[MODE_MS][row-1][col-1]&SCOREMASK; final int scoreFromDel=packed[MODE_DEL][row-1][col-1]&SCOREMASK; final int scoreFromIns=packed[MODE_INS][row-1][col-1]&SCOREMASK; //Time from previous state; this only matters here when coming from MS matrix //It can either represent time spent in MATCH or SUB depending on whether prevMatch is true. //So, it is not time spent in MS (diagonal), just consecutive match or sub. //In other words, the MS matrix contains 2 states which are differentiated. final int streak=(packed[MODE_MS][row-1][col-1]&TIMEMASK); //This block has 2 sub-blocks, on for match and one for sub. if(match){ //The current symbols match, so increment using a MATCH score //Consecutive matches give a slightly higher score than the initial match //Only the maximum of these 3 scores will be used int scoreMS=scoreFromDiag+(prevMatch ? POINTSoff_MATCH2 : POINTSoff_MATCH); int scoreD=scoreFromDel+POINTSoff_MATCH; int scoreI=scoreFromIns+POINTSoff_MATCH; int score; //Score field to update; upper bits of the cell value int time; //Time field to update; lower bits of cell value. //prevState is the "arrow" used in many Smith-Waterman implementations. //It's not necessary, but makes traceback simpler. // byte prevState; //Pick the highest score. In the event of a tie, MS is preferred over D over I. //The counter only gets incremented if the previous state was the same as the current state (match) //Otherwise it gets reset to 1 if(scoreMS>=scoreD && scoreMS>=scoreI){ score=scoreMS; time=(prevMatch ? streak+1 : 1); // prevState=MODE_MS; }else if(scoreD>=scoreI){ score=scoreD; time=1; // prevState=MODE_DEL; }else{ score=scoreI; time=1; // prevState=MODE_INS; } if(time>MAX_TIME){time=MAX_TIME-MASK5;}//If the time counter overflows, reset it assert(score>=MINoff_SCORE) : "Score overflow - use MSA2 instead"; assert(score<=MAXoff_SCORE) : "Score overflow - use MSA2 instead"; //packed[MODE_MS][row][col]=(score|prevState|time); packed[MODE_MS][row][col]=(score|time); assert((score&SCOREMASK)==score); //assert((prevState&MODEMASK)==prevState); assert((time&TIMEMASK)==time); }else{ //The current symbols do not match, so increment using a either a SUB or NOCALL (N) score. //SUB score is negative and depends on the counter; longer streaks of substitutions reduce the penalty //NOCALL score is zero int scoreMS; if(ref1!='N' && call1!='N'){ //This uses an array for the substitution penalty scoreMS=scoreFromDiag+(prevMatch ? (streak<=1 ? POINTSoff_SUBR : POINTSoff_SUB) : POINTSoff_SUB_ARRAY[streak+1]); //Alternative to using the array, but it is branchy // scoreMS=scoreFromDiag+(prevMatch ? (streak<=1 ? POINTSoff_SUBR : POINTSoff_SUB) : // (streak==0 ? POINTSoff_SUB : streak=scoreD && scoreMS>=scoreI){ score=scoreMS; time=(prevMatch ? 1 : streak+1); // prevState=MODE_MS; }else if(scoreD>=scoreI){ score=scoreD; time=1; // prevState=MODE_DEL; }else{ score=scoreI; time=1; // prevState=MODE_INS; } if(time>MAX_TIME){time=MAX_TIME-MASK5;}//If the time counter overflows, reset it assert(score>=MINoff_SCORE && score<=MAXoff_SCORE) : "Score overflow: "+score+" "+new String(read); // packed[MODE_MS][row][col]=(score|prevState|time); packed[MODE_MS][row][col]=(score|time); assert((score&SCOREMASK)==score); // assert((prevState&MODEMASK)==prevState); assert((time&TIMEMASK)==time); } } //The second block fills in the cell for the DEL (deletion) matrix. //The DEL matrix always comes from the left (horizontal). if(rowBARRIER_D2){ //In these cases (at the query tips) deletions are not allowed so the DEL cell is marked invalid. packed[MODE_DEL][row][col]=subfloor; }else{//Calculate DEL score //Time spent in DEL state final int streak=packed[MODE_DEL][row][col-1]&TIMEMASK; //Only 2 scores are needed since adjacent D and I operations are not allowed final int scoreFromDiag=packed[MODE_MS][row][col-1]&SCOREMASK; final int scoreFromDel=packed[MODE_DEL][row][col-1]&SCOREMASK; int scoreMS=scoreFromDiag+POINTSoff_DEL; //This could use an array but it would be a super big array (>1 million) which is hard to cache //Also it could be moved to a function //There are 5 levels of penalties. At the 5th level, a penalty is applied only once every 4 operations. //This allows deletion events 4 times as long to still get positive scores. int scoreD=scoreFromDel+(streak==0 ? POINTSoff_DEL : streak=scoreD){ score=scoreMS; time=1; // prevState=MODE_MS; }else{ score=scoreD; time=streak+1; // prevState=MODE_DEL; } if(time>MAX_TIME){time=MAX_TIME-MASK5;}//If the time counter overflows, reset it assert(score>=MINoff_SCORE && score<=MAXoff_SCORE) : "Score overflow: "+score+" "+new String(read); // packed[MODE_DEL][row][col]=(score|prevState|time); packed[MODE_DEL][row][col]=(score|time); assert((score&SCOREMASK)==score); // assert((prevState&MODEMASK)==prevState); assert((time&TIMEMASK)==time); } //The third block fills in the cell for the INS (insertion) matrix. //The INS matrix always comes from above (vertical). if(gap || (row1) || (row>BARRIER_I2 && col=scoreI){ score=scoreMS; time=1; // prevState=MODE_MS; }else{ score=scoreI; time=streak+1; // prevState=MODE_INS; } if(time>MAX_TIME){time=MAX_TIME-MASK5;}//If the time counter overflows, reset it assert(score>=MINoff_SCORE && score<=MAXoff_SCORE) : "Score overflow: "+score+" "+new String(read); // packed[MODE_INS][row][col]=(score|prevState|time); packed[MODE_INS][row][col]=(score|time); assert((score&SCOREMASK)==score); // assert((prevState&MODEMASK)==prevState); assert((time&TIMEMASK)==time); } } } //The matrices are now full. //So, the top score is selected from the final row, since this is a glocal alignment int maxCol=-1; int maxState=-1; int maxScore=Integer.MIN_VALUE; //TODO: Should I just look in the MS matrix? At least, it shouldn't be necessary to look at the DEL state //To accomplish that I could change to state+=2 for(int state=0; statemaxScore){ maxScore=x; maxCol=col; maxState=state; } } } //Shift the score from the upper bits down to be a "normal" score. //Because everything in this function uses already-shifted (offset) scores, this only needs to be done once, at the end. maxScore>>=SCOREOFFSET; // System.err.println("Returning "+rows+", "+maxCol+", "+maxState+", "+maxScore); return new int[] {rows, maxCol, maxState, maxScore}; } @Override @Deprecated /** return new int[] {rows, maxC, maxS, max}; */ public final int[] fillQ(byte[] read, byte[] ref, byte[] baseScores, int refStartLoc, int refEndLoc){ assert(false) : "Needs to be redone to work with score cutoffs. Not difficult."; rows=read.length; columns=refEndLoc-refStartLoc+1; 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=scoreD && scoreMS>=scoreI){ score=scoreMS; time=(prevMatch ? streak+1 : 1); // prevState=MODE_MS; }else if(scoreD>=scoreI){ score=scoreD; time=1; // prevState=MODE_DEL; }else{ score=scoreI; time=1; // prevState=MODE_INS; } score+=(((int)baseScores[row-1])<MAX_TIME){time=MAX_TIME-MASK5;} assert(score>=MINoff_SCORE) : "Score overflow - use MSA2 instead"; assert(score<=MAXoff_SCORE) : "Score overflow - use MSA2 instead"; // packed[MODE_MS][row][col]=(score|prevState|time); packed[MODE_MS][row][col]=(score|time); assert((score&SCOREMASK)==score); // assert((prevState&MODEMASK)==prevState); assert((time&TIMEMASK)==time); }else{ int scoreMS=scoreFromDiag+(prevMatch ? (streak<=1 ? POINTSoff_SUBR : POINTSoff_SUB) : POINTSoff_SUB_ARRAY[streak+1]); // int scoreMS=scoreFromDiag+(prevMatch ? (streak<=1 ? POINTSoff_SUBR : POINTSoff_SUB) : // (streak==0 ? POINTSoff_SUB : streak=scoreD && scoreMS>=scoreI){ score=scoreMS; time=(prevMatch ? 1 : streak+1); // time=(prevMatch ? (streak==1 ? 3 : 1) : streak+1); prevState=MODE_MS; }else if(scoreD>=scoreI){ score=scoreD; time=1; prevState=MODE_DEL; }else{ score=scoreI; time=1; prevState=MODE_INS; } if(time>MAX_TIME){time=MAX_TIME-MASK5;} assert(score>=MINoff_SCORE) : "Score overflow - use MSA2 instead"; assert(score<=MAXoff_SCORE) : "Score overflow - use MSA2 instead"; // packed[MODE_MS][row][col]=(score|prevState|time); packed[MODE_MS][row][col]=(score|time); assert((score&SCOREMASK)==score); // assert((prevState&MODEMASK)==prevState); assert((time&TIMEMASK)==time); } } } {//Calculate DEL score final int streak=packed[MODE_DEL][row][col-1]&TIMEMASK; final int scoreFromDiag=packed[MODE_MS][row][col-1]&SCOREMASK; final int scoreFromDel=packed[MODE_DEL][row][col-1]&SCOREMASK; int scoreMS=scoreFromDiag+POINTSoff_DEL; int scoreD=scoreFromDel+(streak==0 ? POINTSoff_DEL : streak=scoreD){ score=scoreMS; time=1; prevState=MODE_MS; }else{ score=scoreD; time=streak+1; prevState=MODE_DEL; } if(time>MAX_TIME){time=MAX_TIME-MASK5;} assert(score>=MINoff_SCORE) : "Score overflow - use MSA2 instead"; assert(score<=MAXoff_SCORE) : "Score overflow - use MSA2 instead"; // packed[MODE_DEL][row][col]=(score|prevState|time); packed[MODE_DEL][row][col]=(score|time); assert((score&SCOREMASK)==score); // assert((prevState&MODEMASK)==prevState); assert((time&TIMEMASK)==time); } {//Calculate INS score final int streak=packed[MODE_INS][row-1][col]&TIMEMASK; final int scoreFromDiag=packed[MODE_MS][row-1][col]&SCOREMASK; final int scoreFromIns=packed[MODE_INS][row-1][col]&SCOREMASK; int scoreMS=scoreFromDiag+POINTSoff_INS; // int scoreD=scoreFromDel+POINTSoff_INS; int scoreI=scoreFromIns+POINTSoff_INS_ARRAY[streak+1]; // int scoreI=scoreFromIns_INS+(streak==0 ? POINTSoff_INS : // streakmaxScore){ maxScore=x; maxCol=col; maxState=state; } } } maxScore>>=SCOREOFFSET; // System.err.println("Returning "+rows+", "+maxCol+", "+maxState+", "+maxScore); return new int[] {rows, maxCol, maxState, maxScore}; } @Override /** @return {score, bestRefStart, bestRefStop} */ /** Generates the match string */ public final byte[] traceback(byte[] read, byte[] ref, int refStartLoc, int refEndLoc, int row, int col, int state, boolean gapped){ if(gapped){ final byte[] gref=grefbuffer; int gstart=translateToGappedCoordinate(refStartLoc, gref, read); int gstop=translateToGappedCoordinate(refEndLoc, gref, read); byte[] out=traceback2(read, gref, gstart, gstop, row, col, state); return out; }else{ return traceback2(read, ref, refStartLoc, refEndLoc, row, col, state); } } @Override /** Generates the match string */ public final byte[] traceback2(byte[] read, byte[] ref, int refStartLoc, int refEndLoc, int row, int col, int state){ // assert(false); assert(refStartLoc<=refEndLoc) : refStartLoc+", "+refEndLoc; assert(row==rows); byte[] out=new byte[row+col-1]; //TODO if an out of bound crash occurs, try removing the "-1". int outPos=0; int gaps=0; if(state==MODE_INS){ //TODO ? Maybe not needed. } while(row>0 && col>0){ // byte prev0=(byte)(packed[state][row][col]&MODEMASK); final int time=packed[state][row][col]&TIMEMASK; final byte prev; // System.err.println("state="+state+", prev="+prev+", row="+row+", col="+col+", score="+scores[state][row][col]); if(state==MODE_MS){ if(time>1){prev=(byte)state;} else{ final int scoreFromDiag=packed[MODE_MS][row-1][col-1]&SCOREMASK; final int scoreFromDel=packed[MODE_DEL][row-1][col-1]&SCOREMASK; final int scoreFromIns=packed[MODE_INS][row-1][col-1]&SCOREMASK; if(scoreFromDiag>=scoreFromDel && scoreFromDiag>=scoreFromIns){prev=MODE_MS;} else if(scoreFromDel>=scoreFromIns){prev=MODE_DEL;} else{prev=MODE_INS;} } byte c=read[row-1]; byte r=ref[refStartLoc+col-1]; if(c==r){ out[outPos]='m'; }else{ if(!AminoAcid.isFullyDefined(c)){ out[outPos]='N'; }else if(!AminoAcid.isFullyDefined(r)){ // out[outPos]='X'; out[outPos]='N'; }else{ out[outPos]='S'; } } row--; col--; }else if(state==MODE_DEL){ if(time>1){prev=(byte)state;} else{ final int scoreFromDiag=packed[MODE_MS][row][col-1]&SCOREMASK; final int scoreFromDel=packed[MODE_DEL][row][col-1]&SCOREMASK; if(scoreFromDiag>=scoreFromDel){prev=MODE_MS;} else{prev=MODE_DEL;} } byte r=ref[refStartLoc+col-1]; if(r==GAPC){ out[outPos]='-'; gaps++; }else{ out[outPos]='D'; } col--; }else{ if(time>1){prev=(byte)state;} else{ final int scoreFromDiag=packed[MODE_MS][row-1][col]&SCOREMASK; final int scoreFromIns=packed[MODE_INS][row-1][col]&SCOREMASK; if(scoreFromDiag>=scoreFromIns){prev=MODE_MS;} else{prev=MODE_INS;} } assert(state==MODE_INS) : state; if(col==0){ out[outPos]='X'; }else if(col>=columns){ out[outPos]='Y'; }else{ out[outPos]='I'; } row--; } // assert(prev==prev0); state=prev; outPos++; } assert(row==0 || col==0); if(col!=row){ while(row>0){ out[outPos]='X'; outPos++; row--; col--; } if(col>0){ //do nothing } } //Shrink and reverse the string byte[] out2=new byte[outPos]; for(int i=0; i "+translateFromGappedCoordinate(out[1], gref)+" -> "+ translateToGappedCoordinate(translateFromGappedCoordinate(out[1], gref), gref, read); assert(out[2]==translateToGappedCoordinate(translateFromGappedCoordinate(out[2], gref), gref, read)); out[1]=translateFromGappedCoordinate(out[1], gref); out[2]=translateFromGappedCoordinate(out[2], gref); if(verbose){System.err.println("returning score "+Arrays.toString(out));} return out; }else{ return score2(read, ref, refStartLoc, refEndLoc, maxRow, maxCol, maxState); } } @Override /** @return {score, bestRefStart, bestRefStop, maxRow, maxCol, maxState},
* or {score, bestRefStart, bestRefStop, maxRow, maxCol, maxState, padLeft, padRight}
* if more padding is needed */ public final int[] score2(final byte[] read, final byte[] ref, final int refStartLoc, final int refEndLoc, final int maxRow, final int maxCol, final int maxState){ 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; if(verbose){System.err.println("Scoring.");} int stateTime=0; while(row>0 && col>0){ if(verbose){System.err.println("state="+state+", row="+row+", col="+col);} final int time=packed[state][row][col]&TIMEMASK; final byte prev; if(state==MODE_MS){ if(time>1){prev=(byte)state;} else{ final int scoreFromDiag=packed[MODE_MS][row-1][col-1]&SCOREMASK; final int scoreFromDel=packed[MODE_DEL][row-1][col-1]&SCOREMASK; final int scoreFromIns=packed[MODE_INS][row-1][col-1]&SCOREMASK; if(scoreFromDiag>=scoreFromDel && scoreFromDiag>=scoreFromIns){prev=MODE_MS;} else if(scoreFromDel>=scoreFromIns){prev=MODE_DEL;} else{prev=MODE_INS;} } row--; col--; }else if(state==MODE_DEL){ if(time>1){prev=(byte)state;} else{ final int scoreFromDiag=packed[MODE_MS][row][col-1]&SCOREMASK; final int scoreFromDel=packed[MODE_DEL][row][col-1]&SCOREMASK; if(scoreFromDiag>=scoreFromDel){prev=MODE_MS;} else{prev=MODE_DEL;} } col--; }else{ assert(state==MODE_INS); if(time>1){prev=(byte)state;} else{ final int scoreFromDiag=packed[MODE_MS][row-1][col]&SCOREMASK; final int scoreFromIns=packed[MODE_INS][row-1][col]&SCOREMASK; if(scoreFromDiag>=scoreFromIns){prev=MODE_MS;} else{prev=MODE_INS;} } row--; } if(col<0){ if(verbose){ System.err.println("Warning, column went below 0 at row="+row); } break; //prevents an out of bounds access } // assert(prev==prev0); if(state==prev){stateTime++;}else{stateTime=0;} state=prev; if(verbose){System.err.println("state2="+state+", time="+time+", stateTime="+stateTime+", row2="+row+", col2="+col+"\n");} } // assert(false) : row+", "+col; if(row>col){ col-=row; } final int bestRefStart=refStartLoc+col; score>>=SCOREOFFSET; if(verbose){ System.err.println("bestRefStart="+bestRefStart+", refStartLoc="+refStartLoc); System.err.println("bestRefStop="+bestRefStop+", refEndLoc="+refEndLoc); } int padLeft=0; int padRight=0; if(bestRefStartrefEndLoc){ padRight=Tools.max(0, bestRefStop-refEndLoc); }else if(bestRefStop==refEndLoc && maxState==MODE_INS){ padRight=packed[maxState][maxRow][maxCol]&TIMEMASK; } int[] rvec; if(padLeft>0 || padRight>0){ //Suggest extra padding in cases of overflow rvec=new int[] {score, bestRefStart, bestRefStop, maxRow, maxCol, maxState, padLeft, padRight}; }else{ rvec=new int[] {score, bestRefStart, bestRefStop, maxRow, maxCol, maxState}; } return rvec; } /** * Fills grefbuffer * @param ref * @param a * @param b * @param gaps * @return gref */ private final byte[] makeGref(byte[] ref, int[] gaps, int refStartLoc, int refEndLoc){ assert(gaps!=null && gaps.length>0); assert(refStartLoc<=gaps[0]) : refStartLoc+", "+refEndLoc+", "+Arrays.toString(gaps); assert(refEndLoc>=gaps[gaps.length-1]); final int g0_old=gaps[0]; final int gN_old=gaps[gaps.length-1]; gaps[0]=Tools.min(gaps[0], refStartLoc); gaps[gaps.length-1]=Tools.max(gN_old, refEndLoc); grefRefOrigin=gaps[0]; if(verbose){System.err.println("\ngaps2: "+Arrays.toString(gaps));} // grefRefOrigin=Tools.min(gaps[0], refStartLoc); // //This block is no longer needed since the array is preallocated. // int len=0; // final int gb2=GAPBUFFER*2; // for(int i=0; iy); // int gap=z-y-1; // if(gapy); int gap=z-y-1; assert(gap>=MINGAP) : gap+"\t"+MINGAP; if(gapgref.length){ System.err.println("gref buffer overflow: "+lim+" > "+gref.length); return null; } for(int i=greflimit, r=refEndLoc+1; i "+j);} return j; } j+=(c==GAPC ? GAPLEN : 1); // if(c!=GAPC){j++;} // else{j+=GAPLEN;} } System.err.println(grefRefOrigin); System.err.println(point); System.err.println(new String(gref)); throw new RuntimeException("Out of bounds."); } private final int translateToGappedCoordinate(int point, byte[] gref, byte[] read){ if(verbose){System.err.println("translateToGappedCoordinate("+point+"), gro="+grefRefOrigin+", grl="+greflimit);} if(point<=grefRefOrigin){return point-grefRefOrigin;} for(int i=0, j=grefRefOrigin; i "+i);} return i; } j+=(c==GAPC ? GAPLEN : 1); // if(c!=GAPC){j++;} // else{j+=GAPLEN;} } System.err.println("\ngrefRefOrigin="+grefRefOrigin); System.err.println("point="+point); System.err.println("read="+new String(read)); System.err.println("gref="+new String(gref)); throw new RuntimeException("Out of bounds."); } /** Calculates score based on an array from Index */ private final int calcAffineScore(int[] locArray){ int score=0; int lastLoc=-2; //Last true location int lastValue=-1; int timeInMode=0; for(int i=0; i0){//match if(loc==lastValue){//contiguous match score+=POINTS_MATCH2; }else if(loc==lastLoc || lastLoc<0){//match after a sub, or first match score+=POINTS_MATCH; }else if(loc=0); score+=POINTS_MATCH; score+=POINTS_DEL; int dif=lastLoc-loc+1; if(dif>MINGAP){ int rem=dif%GAPLEN; int div=(dif-GAPBUFFER2)/GAPLEN; score+=(div*POINTS_GAP); assert(rem+GAPBUFFER2LIMIT_FOR_COST_4); //and probably LIMIT_FOR_COST_5 // assert(false) : div; } if(dif>LIMIT_FOR_COST_5){ score+=((dif-LIMIT_FOR_COST_5+MASK5)/TIMESLIP)*POINTS_DEL5; dif=LIMIT_FOR_COST_5; } if(dif>LIMIT_FOR_COST_4){ score+=(dif-LIMIT_FOR_COST_4)*POINTS_DEL4; dif=LIMIT_FOR_COST_4; } if(dif>LIMIT_FOR_COST_3){ score+=(dif-LIMIT_FOR_COST_3)*POINTS_DEL3; dif=LIMIT_FOR_COST_3; } if(dif>1){ score+=(dif-1)*POINTS_DEL2; } timeInMode=1; }else if(loc>lastLoc){//insertion assert(lastLoc>=0); score+=(POINTS_MATCH+POINTS_INS_ARRAY_C[lastLoc-loc]); // score+=POINTS_MATCH; // score+=POINTS_INS; // int dif=lastLoc-loc+1; // if(dif>LIMIT_FOR_COST_4){ // score+=(dif-LIMIT_FOR_COST_4)*POINTS_INS4; // dif=LIMIT_FOR_COST_4; // } // if(dif>LIMIT_FOR_COST_3){ // score+=(dif-LIMIT_FOR_COST_3)*POINTS_INS3; // dif=LIMIT_FOR_COST_3; // } // if(dif>1){ // score+=(dif-1)*POINTS_INS2; // } timeInMode=1; }else{ assert(false); } lastLoc=loc; }else{//substitution if(lastValue<0 && timeInMode>0){//contiguous // if(timeInMode0){//match if(loc==lastValue){//contiguous match score+=(POINTS_MATCH2+baseScores[i]); }else if(loc==lastLoc || lastLoc<0){//match after a sub, or first match score+=(POINTS_MATCH+baseScores[i]); }else if(loc=0); score+=(POINTS_MATCH+baseScores[i]); score+=POINTS_DEL; int dif=lastLoc-loc+1; if(dif>MINGAP){ int rem=dif%GAPLEN; int div=(dif-GAPBUFFER2)/GAPLEN; score+=(div*POINTS_GAP); assert(rem+GAPBUFFER2LIMIT_FOR_COST_4); //and probably LIMIT_FOR_COST_5 // assert(false) : div; } if(dif>LIMIT_FOR_COST_5){ score+=((dif-LIMIT_FOR_COST_5+MASK5)/TIMESLIP)*POINTS_DEL5; dif=LIMIT_FOR_COST_5; } if(dif>LIMIT_FOR_COST_4){ score+=(dif-LIMIT_FOR_COST_4)*POINTS_DEL4; dif=LIMIT_FOR_COST_4; } if(dif>LIMIT_FOR_COST_3){ score+=(dif-LIMIT_FOR_COST_3)*POINTS_DEL3; dif=LIMIT_FOR_COST_3; } if(dif>1){ score+=(dif-1)*POINTS_DEL2; } timeInMode=1; }else if(loc>lastLoc){//insertion assert(lastLoc>=0); score+=(POINTS_MATCH+baseScores[i]+POINTS_INS_ARRAY_C[Tools.min(loc-lastLoc, 5)]); // score+=(POINTS_MATCH+baseScores[i]); // score+=POINTS_INS; // int dif=lastLoc-loc+1; // if(dif>LIMIT_FOR_COST_4){ // score+=(dif-LIMIT_FOR_COST_4)*POINTS_INS4; // dif=LIMIT_FOR_COST_4; // } // if(dif>LIMIT_FOR_COST_3){ // score+=(dif-LIMIT_FOR_COST_3)*POINTS_INS3; // dif=LIMIT_FOR_COST_3; // } // if(dif>1){ // score+=(dif-1)*POINTS_INS2; // } timeInMode=1; }else{ assert(false); } lastLoc=loc; }else if(loc==-1){//substitution if(lastValue<0 && timeInMode>0){//contiguous // if(timeInMode1) : minContig; int contig=0; int maxContig=0; int score=0; int lastLoc=-3; //Last true location int lastValue=-1; int timeInMode=0; for(int i=0; i0){//match if(loc==lastValue){//contiguous match contig++; score+=(POINTS_MATCH2+baseScores[i]); }else if(loc==lastLoc || lastLoc<0){//match after a sub, or first match maxContig=Tools.max(maxContig, contig); contig=1; score+=(POINTS_MATCH+baseScores[i]); }else if(loc=0); score+=(POINTS_MATCH+baseScores[i]); score+=POINTS_DEL; int dif=lastLoc-loc+1; if(dif>MINGAP){ int rem=dif%GAPLEN; int div=(dif-GAPBUFFER2)/GAPLEN; score+=(div*POINTS_GAP); assert(rem+GAPBUFFER2LIMIT_FOR_COST_4); //and probably LIMIT_FOR_COST_5 // assert(false) : div; } if(dif>LIMIT_FOR_COST_5){ score+=((dif-LIMIT_FOR_COST_5+MASK5)/TIMESLIP)*POINTS_DEL5; dif=LIMIT_FOR_COST_5; } if(dif>LIMIT_FOR_COST_4){ score+=(dif-LIMIT_FOR_COST_4)*POINTS_DEL4; dif=LIMIT_FOR_COST_4; } if(dif>LIMIT_FOR_COST_3){ score+=(dif-LIMIT_FOR_COST_3)*POINTS_DEL3; dif=LIMIT_FOR_COST_3; } if(dif>1){ score+=(dif-1)*POINTS_DEL2; } timeInMode=1; }else if(loc>lastLoc){//insertion maxContig=Tools.max(maxContig, contig); contig=0; assert(lastLoc>=0); score+=(POINTS_MATCH+baseScores[i]+POINTS_INS_ARRAY_C[Tools.min(loc-lastLoc, 5)]); // score+=(POINTS_MATCH+baseScores[i]); // score+=POINTS_INS; // int dif=lastLoc-loc+1; // if(dif>LIMIT_FOR_COST_4){ // score+=(dif-LIMIT_FOR_COST_4)*POINTS_INS4; // dif=LIMIT_FOR_COST_4; // } // if(dif>LIMIT_FOR_COST_3){ // score+=(dif-LIMIT_FOR_COST_3)*POINTS_INS3; // dif=LIMIT_FOR_COST_3; // } // if(dif>1){ // score+=(dif-1)*POINTS_INS2; // } timeInMode=1; }else{ assert(false); } lastLoc=loc; }else if(loc==-1){//substitution if(lastValue<0 && timeInMode>0){//contiguous // if(timeInModeref.length){ int dif=(refStop-ref.length); readStop-=dif; score+=POINTS_NOREF*dif; norefs+=dif; } // if(refStart<0 || refStart+read.length>ref.length){return -99999;} //No longer needed. for(int i=readStart; i=ref.length) ? (byte)'N' : ref[j]; if(c=='N' || r=='N'){match[i]='N';} else if(c==r){match[i]='m';} else{match[i]='S';} } return match; } @Override public final int scoreNoIndels(byte[] read, byte[] ref, byte[] baseScores, final int refStart){ return scoreNoIndels(read, ref, baseScores, refStart, null); } @Override public final int scoreNoIndels(byte[] read, byte[] ref, byte[] baseScores, final int refStart, SiteScore ss){ int score=0; int mode=-1; int timeInMode=0; int norefs=0; //This block handles cases where the read runs outside the reference //Of course, padding the reference with 'N' would be better, but... int readStart=0; int readStop=read.length; final int refStop=refStart+read.length; boolean semiperfect=true; if(refStart<0){ readStart=0-refStart; score+=POINTS_NOREF*readStart; norefs+=readStart; } if(refStop>ref.length){ int dif=(refStop-ref.length); readStop-=dif; score+=POINTS_NOREF*dif; norefs+=dif; } // if(refStart<0 || refStart+read.length>ref.length){return -99999;} //No longer needed. for(int i=readStart; iref.length){return -99999;} if(refStart<0){ readStart=0-refStart; score+=POINTS_NOREF*readStart; } if(refStop>ref.length){ int dif=(refStop-ref.length); System.err.println(new String(read)+"\ndif="+dif+", ref.length="+ref.length+", refStop="+refStop); readStop-=dif; score+=POINTS_NOREF*dif; } assert(refStart+readStop<=ref.length) : "readStart="+readStart+", readStop="+readStop+ ", refStart="+refStart+", refStop="+refStop+", ref.length="+ref.length+", read.length="+read.length; assert(matchReturn!=null); assert(matchReturn.length==1); if(matchReturn[0]==null || matchReturn[0].length!=read.length){ assert(matchReturn[0]==null || matchReturn[0].lengthref.length){return -99999;} if(refStart<0){ readStart=0-refStart; score+=POINTS_NOREF*readStart; } if(refStop>ref.length){ int dif=(refStop-ref.length); System.err.println(new String(read)+"\ndif="+dif+", ref.length="+ref.length+", refStop="+refStop); readStop-=dif; score+=POINTS_NOREF*dif; } assert(refStart+readStop<=ref.length) : "readStart="+readStart+", readStop="+readStop+ ", refStart="+refStart+", refStop="+refStop+", ref.length="+ref.length+", read.length="+read.length; assert(matchReturn!=null); assert(matchReturn.length==1); if(matchReturn[0]==null || matchReturn[0].length!=read.length){ assert(matchReturn[0]==null || matchReturn[0].length>SCOREOFFSET).append(",");} return sb; } public static float minIdToMinRatio(double minid){ if(minid>1){minid=minid/100;} assert(minid>0 && minid<=1) : "Min identity should be between 0 and 1. Values above 1 will be assumed to be percent and divided by 100."; double matchdif=POINTS_MATCH-POINTS_MATCH2; double match=POINTS_MATCH2; double sub=-POINTS_MATCH2+0.5*(matchdif+POINTS_SUB)+0.5*POINTS_SUB2; double del=0.1*(matchdif+POINTS_DEL)+0.2*POINTS_DEL2+0.4*POINTS_DEL3+0.3*POINTS_DEL4; double ins=-POINTS_MATCH2+0.4*(matchdif+POINTS_INS)+0.3*(POINTS_INS2)+0.3*(POINTS_INS3); double badAvg=.7*sub+.2*del+.1*ins; double badFraction=1-minid; double minratio=(match+badFraction*badAvg)/match; assert(minratio<=1); minratio=Tools.max(0.1, minratio); return (float)minratio; } public static final int TIMEBITS=11; public static final int SCOREBITS=32-TIMEBITS; public static final int MAX_TIME=((1<LIMIT_FOR_COST_4){ pts=POINTS_INS4; ptsoff=POINTSoff_INS4; }else if(i>LIMIT_FOR_COST_3){ pts=POINTS_INS3; ptsoff=POINTSoff_INS3; }else if(i>1){ pts=POINTS_INS2; ptsoff=POINTSoff_INS2; }else{ pts=POINTS_INS; ptsoff=POINTSoff_INS; } POINTS_INS_ARRAY[i]=pts; POINTSoff_INS_ARRAY[i]=ptsoff; POINTS_INS_ARRAY_C[i]=Tools.max(MIN_SCORE, pts+POINTS_INS_ARRAY_C[i-1]); POINTSoff_INS_ARRAY_C[i]=Tools.max(MINoff_SCORE, ptsoff+POINTSoff_INS_ARRAY_C[i-1]); } POINTS_SUB_ARRAY=new int[604]; POINTSoff_SUB_ARRAY=new int[604]; POINTS_SUB_ARRAY_C=new int[604]; POINTSoff_SUB_ARRAY_C=new int[604]; for(int i=1; iLIMIT_FOR_COST_3){ pts=POINTS_SUB3; ptsoff=POINTSoff_SUB3; }else if(i>1){ pts=POINTS_SUB2; ptsoff=POINTSoff_SUB2; }else{ pts=POINTS_SUB; ptsoff=POINTSoff_SUB; } POINTS_SUB_ARRAY[i]=pts; POINTSoff_SUB_ARRAY[i]=ptsoff; POINTS_SUB_ARRAY_C[i]=Tools.max(MIN_SCORE, pts+POINTS_SUB_ARRAY_C[i-1]); POINTSoff_SUB_ARRAY_C[i]=Tools.max(MINoff_SCORE, ptsoff+POINTSoff_SUB_ARRAY_C[i-1]); } } @Override public final int POINTS_NOREF(){return POINTS_NOREF;} @Override public final int POINTS_NOCALL(){return POINTS_NOCALL;} @Override public final int POINTS_MATCH(){return POINTS_MATCH;} @Override public final int POINTS_MATCH2(){return POINTS_MATCH2;} @Override public final int POINTS_COMPATIBLE(){return POINTS_COMPATIBLE;} @Override public final int POINTS_SUB(){return POINTS_SUB;} @Override public final int POINTS_SUBR(){return POINTS_SUBR;} @Override public final int POINTS_SUB2(){return POINTS_SUB2;} @Override public final int POINTS_SUB3(){return POINTS_SUB3;} @Override public final int POINTS_MATCHSUB(){return POINTS_MATCHSUB;} @Override public final int POINTS_INS(){return POINTS_INS;} @Override public final int POINTS_INS2(){return POINTS_INS2;} @Override public final int POINTS_INS3(){return POINTS_INS3;} @Override public final int POINTS_INS4(){return POINTS_INS4;} @Override public final int POINTS_DEL(){return POINTS_DEL;} @Override public final int POINTS_DEL2(){return POINTS_DEL2;} @Override public final int POINTS_DEL3(){return POINTS_DEL3;} @Override public final int POINTS_DEL4(){return POINTS_DEL4;} @Override public final int POINTS_DEL5(){return POINTS_DEL5;} @Override public final int POINTS_DEL_REF_N(){return POINTS_DEL_REF_N;} @Override public final int POINTS_GAP(){return POINTS_GAP;} @Override public final int TIMESLIP(){return TIMESLIP;} @Override public final int MASK5(){return MASK5;} @Override public final int SCOREOFFSET(){return SCOREOFFSET();} @Override final int BARRIER_I1(){return BARRIER_I1;} @Override final int BARRIER_D1(){return BARRIER_D1;} @Override public final int LIMIT_FOR_COST_3(){return LIMIT_FOR_COST_3;} @Override public final int LIMIT_FOR_COST_4(){return LIMIT_FOR_COST_4;} @Override public final int LIMIT_FOR_COST_5(){return LIMIT_FOR_COST_5;} @Override public final int BAD(){return BAD;} private int rows; private int columns; }