package aligner; import java.util.Arrays; import shared.KillSwitch; import shared.Tools; /** * Based on MSA9PBA, but reduced to a single matrix. */ public final class SingleStateAlignerPacBioAdapter { public SingleStateAlignerPacBioAdapter(int maxRows_, int maxColumns_, int qlen){ // assert(maxColumns_>=200); // assert(maxRows_>=200); maxRows=maxRows_; maxColumns=maxColumns_; packed=new int[maxRows+1][maxColumns+1]; insScoreArray=KillSwitch.allocInt1D(5); delScoreArray=KillSwitch.allocInt1D(5); matchScoreArray=KillSwitch.allocInt1D(5); subScoreArray=KillSwitch.allocInt1D(5); Arrays.fill(insScoreArray, POINTSoff_INS|MODE_INS); Arrays.fill(delScoreArray, POINTSoff_DEL|MODE_DEL); Arrays.fill(matchScoreArray, POINTSoff_MATCH|MODE_MATCH); Arrays.fill(subScoreArray, POINTSoff_SUB|MODE_SUB); insScoreArray[MODE_INS]=POINTSoff_INS2|MODE_INS; delScoreArray[MODE_DEL]=POINTSoff_DEL2|MODE_DEL; matchScoreArray[MODE_MATCH]=POINTSoff_MATCH2|MODE_MATCH; subScoreArray[MODE_SUB]=POINTSoff_SUB2|MODE_SUB; vertLimit=new int[maxRows+1]; horizLimit=new int[maxColumns+1]; Arrays.fill(vertLimit, BADoff); Arrays.fill(horizLimit, BADoff); // for(int i=0; i0){ packed[0][i]|=calcDelScoreOffset(qbases); //Forces consumption of query } } } /** return new int[] {rows, maxCol, maxState, maxScore, maxStart}; * Will not fill areas that cannot match minScore */ public final int[] fillLimited(byte[] read, byte[] ref, int refStartLoc, int refEndLoc, int minScore){ return fillLimitedX(read, ref, refStartLoc, refEndLoc, minScore); } /** return new int[] {rows, maxCol, maxState, maxScore, maxStart}; * Will not fill areas that cannot match minScore */ private final int[] fillLimitedX(byte[] read, byte[] ref, int refStartLoc, int refEndLoc, int minScore){ return fillUnlimited(read, ref, refStartLoc, refEndLoc, minScore); } /** return new int[] {rows, maxCol, maxState, maxScore, maxStart}; * Does not require a min score (ie, same as old method) */ private final int[] fillUnlimited(byte[] read, byte[] ref, int refStartLoc, int refEndLoc, final int minScore){ rows=read.length; columns=refEndLoc-refStartLoc+1; //temporary, for finding a bug if(rows>maxRows || 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; } } 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){ state=(packed[row][col]&MODEMASK); if(state==MODE_MATCH){ col--; row--; out[outPos]='m'; }else if(state==MODE_SUB){ col--; row--; out[outPos]='S'; }else if(state==MODE_DEL){ col--; out[outPos]='D'; }else if(state==MODE_INS){ row--; out[outPos]='I'; }else{ assert(false) : state; } 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=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){ state=(packed[row][col]&MODEMASK); if(state==MODE_MATCH){ col--; row--; }else if(state==MODE_SUB){ 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} */ 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 static int calcDelScore(int len){ if(len<=0){return 0;} int score=POINTS_DEL; if(len>1){ score+=(len-1)*POINTS_DEL2; } return score; } private static int calcDelScoreOffset(int len){ if(len<=0){return 0;} int score=POINTSoff_DEL; if(len>1){ score+=(len-1)*POINTSoff_DEL2; } 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; } private final int maxRows; public final int maxColumns; private final int[][] packed; private final int[] vertLimit; private final int[] horizLimit; private final int[] insScoreArray; private final int[] delScoreArray; private final int[] matchScoreArray; private final int[] subScoreArray; 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<