package stream; import java.io.Serializable; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import align2.GapTools; import align2.MSA; import dna.AminoAcid; import dna.ChromosomeArray; import dna.Data; import shared.Shared; import shared.Tools; import structures.ByteBuilder; public final class SiteScore implements Comparable, Cloneable, Serializable{ /** * */ private static final long serialVersionUID = -8096245242590075081L; public SiteScore(int chrom_, byte strand_, int start_, int stop_, int hits_, int quickScore_){ start=start_; stop=stop_; hits=hits_; quickScore=quickScore_; score=quickScore_; chrom=chrom_; strand=strand_; // assert(chrom_>=0) : this.toText()+"\nchrom_="+chrom_+", strand_="+strand_+", start_="+start_+", stop_="+stop_+", hits_="+hits_+", quickScore_="+quickScore_; assert(start_<=stop_) : this.toText()+"\nchrom_="+chrom_+", strand_="+strand_+", start_="+start_+", stop_="+stop_+", hits_="+hits_+", quickScore_="+quickScore_; } public SiteScore(int chrom_, byte strand_, int start_, int stop_, int hits_, int quickScore_, boolean rescued_, boolean perfect_){ start=start_; stop=stop_; hits=hits_; quickScore=quickScore_; score=quickScore_; chrom=chrom_; strand=strand_; rescued=rescued_; perfect=perfect_; semiperfect=perfect; assert(start_<=stop_) : this.toText(); } @Override public int compareTo(SiteScore other) { int x=other.score-score; if(x!=0){return x;} x=other.slowScore-slowScore; if(x!=0){return x;} x=other.pairedScore-pairedScore; if(x!=0){return x;} x=other.quickScore-quickScore; if(x!=0){return x;} x=chrom-other.chrom; if(x!=0){return x;} x=start-other.start; return x; } @Override public boolean equals(Object other){ return compareTo((SiteScore)other)==0; } @Override public int hashCode() { assert(false) : "This class should not be hashed."; return super.hashCode(); } @Override public String toString(){ return toText().toString(); } // 9+2+1+9+9+1+1+4+4+4+4+gaps public CharSequence toText(){ StringBuilder sb=new StringBuilder(53+(gaps==null ? 0 : gaps.length*10)); sb.append(chrom); sb.append(','); sb.append(strand); sb.append(','); sb.append(start); sb.append(','); sb.append(stop); sb.append(','); sb.append((rescued ? 1 : 0)); sb.append(','); sb.append((semiperfect ? 1 : 0)); sb.append((perfect ? 1 : 0)); sb.append(','); sb.append(hits); sb.append(','); sb.append(quickScore); sb.append(','); sb.append(slowScore); sb.append(','); sb.append(pairedScore); sb.append(','); sb.append(score); if(gaps!=null){ sb.append(','); for(int i=0; i0){sb.append('~');} sb.append(gaps[i]); } } if(match!=null){ if(gaps==null){sb.append(',');} sb.append(','); final char[] buffer=Shared.getTLCB(match.length); for(int i=0; i0){sb.append('~');} sb.append(gaps[i]); } } if(match!=null){ if(gaps==null){sb.append(',');} sb.append(','); sb.append(match); } return sb; // chrom+","+strand+","+start+","+stop+","+(rescued ? 1 : 0)+","+ // (perfect ? 1 : 0)+","+quickScore+","+slowScore+","+pairedScore+","+score; } public boolean isSemiPerfect(byte[] bases){ if(bases.length!=stop-start+1){return false;} byte[] ref=Data.getChromosome(chrom).array; //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=bases.length; final int refStop=start+bases.length; int maxNoref=bases.length/2; if(start<0){ readStart=0-start; } if(refStop>ref.length){ int dif=(refStop-ref.length); readStop-=dif; } for(int i=readStart; i=ref.length){return false;} for(int i=0; i=ref.length){ N+=(stop-ref.length+1); assert(!perfect || !assumePerfectCorrect); perfect=false; } if(N>nlimit){ perfect=semiperfect=false; assert(Read.CHECKSITE(this, bases, 0)); //123 return perfect; } final byte bn=(byte)'N'; for(; refloc<=max; refloc++, readloc++){ final byte c=bases[readloc]; final byte r=ref[refloc]; assert(Tools.isUpperCase(r) && Tools.isUpperCase(c)) : "\nAn input read appears to contain a non-upper-case base. Please rerun with the 'touppercase' flag.\n"+ "ref base = "+r+", read base = "+c+", TO_UPPER_CASE = "+Read.TO_UPPER_CASE+"\n"+(bases.length<=500 ? new String(bases) : "")+"\n"; if(c!=r || c==bn){ perfect=false; if(c==bn){semiperfect=false;} if(r!=bn || (N=N+1)>nlimit){ semiperfect=false; assert(Read.CHECKSITE(this, bases, 0)); //123 return semiperfect; } } } semiperfect=(semiperfect && (N<=nlimit)); perfect=(perfect && semiperfect && (N==0)); assert(Read.CHECKSITE(this, bases, 0)); //123 return perfect; } public final boolean overlaps(SiteScore ss){ return chrom==ss.chrom && strand==ss.strand && overlap(start, stop, ss.start, ss.stop); } public final boolean overlaps(SiteScore ss, boolean ignoreStrand){ return chrom==ss.chrom && (ignoreStrand || strand==ss.strand) && overlap(start, stop, ss.start, ss.stop); } private static boolean overlap(int a1, int b1, int a2, int b2){ assert(a1<=b1 && a2<=b2) : a1+", "+b1+", "+a2+", "+b2; return a2<=b1 && b2>=a1; } public static String header() { return "chrom,strand,start,stop,rescued,semiperfect+perfect,hits,quickScore,slowScore,pairedScore,score,match"; } public static SiteScore fromText(String s){ // System.err.println("Trying to make a SS from "+s); String line[]=s.split(","); SiteScore ss; assert(line.length>=11 && line.length<=13) : "\n"+line.length+"\n"+s+"\n"+Arrays.toString(line)+"\n"; int chrom=Byte.parseByte(line[0].charAt(0)=='*' ? line[0].substring(1) : line[0]); byte strand=Byte.parseByte(line[1]); int start=Integer.parseInt(line[2]); int stop=Integer.parseInt(line[3]); boolean rescued=Integer.parseInt(line[4])==1; // [1, 1, 9397398, 9398220, 0, 00, 20, 8701, 9084, 0, 9084, 9397398~9397471~9398145~9398220] int p=Integer.parseInt(line[5], 2); // assert(false) : line[5]+"->"+p; boolean perfect=(p&1)==1; boolean semiperfect=(p&2)==2; int hits=Integer.parseInt(line[6]); int quickScore=Integer.parseInt(line[7]); int swscore=Integer.parseInt(line[8]); int pairedScore=Integer.parseInt(line[9]); int score=Integer.parseInt(line[10]); ss=new SiteScore(chrom, strand, start, stop, hits, quickScore, rescued, perfect); ss.setScore(score); ss.setSlowPairedScore(swscore, pairedScore); ss.semiperfect=semiperfect; if(line.length>11){ if(line[11]!=null && line[11].length()>0){ String[] gstring=line[11].split("~"); ss.gaps=new int[gstring.length]; for(int i=0; i12){ ss.match=line[12].getBytes(); } return ss; } public boolean positionalMatch(SiteScore b, boolean testGaps){ // return chrom==b.chrom && strand==b.strand && start==b.start && stop==b.stop; if(chrom!=b.chrom || strand!=b.strand || start!=b.start || stop!=b.stop){ return false; } if(!testGaps || (gaps==null && b.gaps==null)){return true;} if((gaps==null) != (b.gaps==null)){return false;} if(gaps.length!=b.gaps.length){return false;} for(int i=0; i{ private PositionComparator(){} @Override public int compare(SiteScore a, SiteScore b) { if(a.chrom!=b.chrom){return a.chrom-b.chrom;} if(a.start!=b.start){return a.start-b.start;} if(a.stop!=b.stop){return a.stop-b.stop;} if(a.strand!=b.strand){return a.strand-b.strand;} if(a.score!=b.score){return b.score-a.score;} if(a.slowScore!=b.slowScore){return b.slowScore-a.slowScore;} if(a.quickScore!=b.quickScore){return b.quickScore-a.quickScore;} if(a.perfect!=b.perfect){return a.perfect ? -1 : 1;} if(a.rescued!=b.rescued){return a.rescued ? 1 : -1;} return 0; } public void sort(ArrayList list){ if(list==null || list.size()<2){return;} Shared.sort(list, this); } public void sort(SiteScore[] list){ if(list==null || list.length<2){return;} Arrays.sort(list, this); } } public SiteScore copy(){ SiteScore ss2=this.clone(); if(gaps!=null){ss2.gaps=ss2.gaps.clone();} return ss2; } @Override public SiteScore clone(){ try { return (SiteScore)super.clone(); } catch (CloneNotSupportedException e) { // TODO Auto-generated catch block e.printStackTrace(); } throw new RuntimeException(); } public boolean isInBounds(){ ChromosomeArray cha=Data.getChromosome(chrom); return (start>=0 && stop<=cha.maxIndex); } public boolean matchContainsXY(){ if(match==null || match.length<1){return false;} final byte a=match[0], b=match[match.length-1]; return (a=='X' ||a=='Y' || b=='X' || b=='Y'); } public boolean matchContainsAB(){ if(match==null || match.length<1){return false;} final byte a=match[0], b=match[match.length-1]; return (a=='A' ||a=='B' || b=='A' || b=='B'); } public boolean matchContainsC(){ if(match==null || match.length<1){return false;} final byte a=match[0], b=match[match.length-1]; return (a=='C' || b=='C'); } public boolean isCorrect(int chrom_, byte strand_, int start_, int stop_, int thresh){ if(chrom_!=chrom || strand_!=strand){return false;} if(thresh<=0){return start_==start && stop_==stop;} return Tools.absdif(start_, start)<=thresh || Tools.absdif(stop_, stop)<=thresh; } public int leftPaddingNeeded(int tiplen, int maxIndel){ if(match==null || match.length<1){return 0;} int neutral=0, insertion=0, deletion=0, xy=0; { int mloc=0; for(; mloc=tiplen){break;} } } } if(insertion>maxIndel || xy>0 || match[0]=='I'){return insertion+xy;} return 0; } public int rightPaddingNeeded(int tiplen, int maxIndel){ if(match==null || match.length<1){return 0;} final int lastIndex=match.length-1; int neutral=0, insertion=0, deletion=0, xy=0; { int mloc=lastIndex; for(int min=lastIndex-tiplen; mloc>=0; mloc--){ byte c=match[mloc]; if(c=='I'){insertion++;} else if(c=='X' || c=='Y'){xy++;} else if(c=='D'){return insertion+xy;} else{ neutral++; if(mloc>=tiplen){break;} } } } if(insertion>maxIndel || xy>0 || match[lastIndex]=='I'){return insertion+xy;} return 0; } /** Assumes bases are rcomped as needed */ public int unclip(byte[] bases, byte[] rbases){ int unclipped=0; if(match==null || match.length<1){return unclipped;} assert(lengthsAgree()); for(int mpos=0, bpos=0, rpos=start; mpos=0){ byte b=bases[bpos]; byte r=rbases[rpos]; if(!AminoAcid.isFullyDefined(b) || !AminoAcid.isFullyDefined(r)){match[mpos]='N';} else if(b==r){match[mpos]='m';} else{match[mpos]='S';} unclipped++; } } assert(lengthsAgree()); for(int mpos=match.length-1, bpos=bases.length-1, rpos=stop; mpos>=0 && rpos>=0; mpos--, bpos--, rpos--){ if(match[mpos]!='C'){break;} if(rpos=0){ break; }else{ assert(m=='N') : (char)m+"\n"+mpos+", "+rpos+"\n"+this; rpos++; } clipped++; match[mpos]='C'; } } assert(clipped==0 || lengthsAgree()); for(int mpos=match.length-1, rpos=stop; mpos>=0; mpos--){ final byte m=match[mpos]; if(m=='C'){ //Do nothing rpos--; }else if(m=='m' || m=='S'){ break; }else{ if(m=='I'){ stop++; }else if(m=='X'){//Should not happen assert(false); rpos--; }else if(m=='Y'){ stop++; }else if(m=='D'){ //Do nothing }else if(rpos=0; mpos--){ // final byte m=match[mpos]; // if(m=='I'){ // start++; // }else if(m=='X' || m=='Y' || m=='D'){ // //Do nothing // }else{ // break; // } // clipped++; // match[mpos]='C'; // } // assert(clipped==0 || lengthsAgree()); // // return clipped; // } public boolean clipTipIndels(byte[] bases, byte[] basesM, int tiplen, int maxIndel, MSA msa){ return this.plus() ? clipTipIndels(bases, tiplen, maxIndel, msa) : clipTipIndels(basesM, tiplen, maxIndel, msa); } public boolean clipTipIndels(byte[] bases, int tiplen, int maxIndel, MSA msa){ if(match==null || match.length=tiplen){break;} } } while(mloc>=0 && match[mloc]=='m'){mloc--; neutral--;} } if(insertion<=maxIndel && deletion<=4*maxIndel){return false;} assert(mappedLength()==matchLength() || matchContainsXY()) : "start="+start+", stop="+stop+", maplen="+mappedLength()+", matchlen="+matchLength()+"\n"+new String(match)+"\n"+new String(bases)+"\n\n"+this; int sum=neutral+insertion+deletion; if(deletion>0){ byte[] temp=new byte[match.length-deletion]; int i=0, j=0; for(; i0){ byte[] temp=new byte[match.length-deletion]; int i=0, j=0; for(; i=match.length || mloc>=bases.length){success=false;} else if(mloc>0){ mloc--;//Location of last X or Y on left side final int numX=mloc+1; int rloc=start+mloc, cloc=mloc; int subs=0, firstSub=-1; while(mloc>=0){ byte m=match[mloc]; byte c=bases[cloc]; byte r=ca.get(rloc); assert(m=='X' || m=='Y') : (char)m+", "+mloc+", "+(char)c+", "+(char)r+"\n"+new String(bases)+"\n"+this.toString(); if(r=='N' || c=='N'){match[mloc]='N';} else if(c==r){match[mloc]='m';} // else if(mloc<=tip){match[mloc]='S';} else{ match[mloc]='S'; subs++; if(subs==1){firstSub=mloc;} } // else{ // if(verbose){System.err.println("A: Set success to false.");} // success=false; // break; // } mloc--; rloc--; cloc--; } if(success && mappedLength()!=matchLength()){incrementStart(-numX);} if(subs>maxSubs && subs>numX*maxSubRate){ if(verbose){System.err.println("Failed to correct alignment; clipping left side of read.");} for(int i=0; i<=firstSub; i++){ match[i]='C'; } if(!lengthsAgree()){ assert(false); incrementStart(firstSub+1); assert(lengthsAgree()); } } } if(verbose){System.err.println("Finished left side. Success="+success+", start="+start+", stop="+stop+", match=\n"+new String(match));} if(verbose){System.err.println("lengthsAgree: "+this.lengthsAgree());} } if(success){//Process right side if(verbose){System.err.println("Processing right side. Success="+success+", start="+start+", stop="+stop+", match=\n"+new String(match));} int mloc=match.length-1; while(mloc>=0 && (match[mloc]=='X' || match[mloc]=='Y')){mloc--;} int dif=match.length-1-mloc; if(mloc<0){ if(verbose){System.err.println("B: Set success to false.");} success=false; } else if(dif>0){ mloc++;//Location of first X or Y on right side final int numX=match.length-mloc; int rloc=stop-dif+1, cloc=bases.length-dif; int subs=0, firstSub=-1; if(cloc<0){ if(verbose){System.err.println("C: Set success to false.");} success=false; }else{ // final int tip2=match.length-tip; while(mloc=tip2){match[mloc]='S';} else{ match[mloc]='S'; subs++; if(subs==1){firstSub=mloc;} } // else{ // if(verbose){System.err.println("D: Set success to false.");} // success=false; // break; // } mloc++; rloc++; cloc++; } } if(success){ if(verbose){System.err.println("A: Start="+start+", stop="+stop+", numX="+numX+", lengthsAgree()="+lengthsAgree());} if(mappedLength()!=matchLength()){incrementStop(numX);} if(verbose){System.err.println("B: Start="+start+", stop="+stop+", numX="+numX+", lengthsAgree()="+lengthsAgree());} if(subs>maxSubs && subs>numX*maxSubRate){ if(verbose){System.err.println("Failed to correct alignment; clipping right side of read.");} for(int i=firstSub; i=0; i--){ // if(match[i]=='Y'){y++;}else{break;} // } //// if((x!=0 || y!=0) && !lengthsAgree()){ //// setLimits(start-x, stop+y); //// } // if((y!=0)){ //// setLimits(start, stop+y); // } } public void setStart(int a){ start=a; if(gaps!=null){ gaps[0]=a; if(gaps[0]>gaps[1]){ gaps=GapTools.fixGaps(this); } assert(CHECKGAPS()) : Arrays.toString(gaps); } } public void setStop(int b){ stop=b; if(gaps!=null){ gaps[gaps.length-1]=b; if(gaps.length-1>gaps.length-2){gaps=GapTools.fixGaps(this);} assert(CHECKGAPS()) : Arrays.toString(gaps); } } public boolean CHECKGAPS(){ if(gaps==null){return true;} if(gaps.length==0 || ((gaps.length&1)==1)){return false;} for(int i=1; igaps[i]){return false;} } return gaps[0]==start && gaps[gaps.length-1]==stop; } public int start(){return start;} public int stop(){return stop;} public void setSlowScore(int x){ // assert(x!=-1); if(verbose){System.err.println("Before setSlowScore: x="+x+", quick="+quickScore+", slow="+slowScore+", paired="+pairedScore);} // assert(slowScore<=0 || pairedScore<=0 || pairedScore>=slowScore) : "x="+x+", quick="+quickScore+", slow="+slowScore+", paired="+pairedScore+"\n"+this; //Correct, but temporarily disabled for stability if(x<=0){ pairedScore=slowScore=x; }else if(pairedScore<=0){ slowScore=x; }else{ assert(pairedScore>=slowScore) : this; if(pairedScore>0){ if(slowScore>0){ pairedScore=x+(pairedScore-slowScore); }else{ pairedScore=x+1; } } } slowScore=x; if(verbose){System.err.println("After setSlowScore: "+this);} // assert(pairedScore<=0 || pairedScore>=slowScore) : "quick="+quickScore+", slow="+slowScore+", paired="+pairedScore+"\n"+this; //Correct, but temporarily disabled for stability } public void setPairedScore(int x){ // assert(x==0 || slowScore<=0 || x>=slowScore) : "x="+x+", quick="+quickScore+", slow="+slowScore+", paired="+pairedScore+"\n"+this; //Correct, but temporarily disabled for stability pairedScore=x; // assert(slowScore<=0 || pairedScore<=0 || pairedScore>=slowScore) : "x="+x+", quick="+quickScore+", slow="+slowScore+", paired="+pairedScore+"\n"+this; //Correct, but temporarily disabled for stability } public void setSlowPairedScore(int x, int y){ // assert(slowScore<=0 || pairedScore<=0 || pairedScore>=slowScore) : "x="+x+", quick="+quickScore+", slow="+slowScore+", paired="+pairedScore+"\n"+this; //Correct, but temporarily disabled for stability slowScore=x; pairedScore=y; // assert(pairedScore<=0 || pairedScore>=slowScore) : this; //Correct, but temporarily disabled for stability } public void setScore(int x){ score=x; } public int start; public int stop; public int quickScore; public int score; public int slowScore; public int pairedScore; public int hits; public final int chrom; public long flags; //TODO Use this instead of fields public int[] gaps; //Limits of large gaps public byte[] match; public static final PositionComparator PCOMP=new PositionComparator(); public static final long strandMask=(1L<<0); public static final long rescuedMask=(1L<<1); public static final long perfectMask=(1L<<2); public static final long semiperfectMask=(1L<<3); public static boolean verbose=false; }