package clump; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import bloom.KCountArray; import dna.AminoAcid; import jgi.BBMerge; import shared.Shared; import shared.Tools; import sketch.SketchTool; import stream.Read; /** * @author Brian Bushnell * @date Nov 4, 2015 * */ public class KmerComparator implements Comparator { public KmerComparator(int k_, boolean addName_, boolean rcomp_){ this(k_, defaultSeed, defaultBorder, defaultHashes, addName_, rcomp_); } public KmerComparator(int k_, long seed_, int border_, int hashes_, boolean addName_, boolean rcomp_){ k=k_; assert(k>0 && k<32); shift=2*k; shift2=shift-2; mask=(shift>63 ? -1L : ~((-1L)<0) : minCount+", "+topCode+", "+topCount; // if(topCode<0 && minCount>1){//Not needed // return fillMax(r, kmers, null, 0); // } // r.id+=" "+key.position+","+rcomp+","+(bases.length-key.position+k-2); if(key.kmerMinusStrand && rcompReads){ key.flip(r, k); } // if(shortName){//This actually takes a lot of time! // r.id=r.numericID+" 1:"+(rcomp ? "t" : "f"); // if(r.mate!=null){ // r.mate.id=r.numericID+" 2:f"; // } // }else if(addName){r.id+=" "+key;} assert(key.kmer>=0 && key.position>=0) : key+"\n"+r; return key.kmer; } /* For teaching */ /** Finds the global maximum, forward only */ public long example1(Read r){ //Handle degenerate reads shorter than kmer length if(r.length()=k){//If the kmer is valid and full length, hash it final long code=hash(kmer);//Some arbitrary hash function if(code>topCode){//If this is a new maximum, retain it topKmer=kmer; topCode=code; } } } //If for whatever reason it failed (e.g., Ns), use the short method if(topCode==Long.MIN_VALUE){return fillShort(r, null);} //Return the pivot kmer. //It's also useful to store (and return) the position and orientation. return topKmer; } /* For teaching */ /** Finds the global maximum, forward and reverse */ public long example2(Read r){ if(r.length()>>2)|(x2<=k){ final long kmax=Tools.max(kmer, rkmer); final long code=hash(kmax); if(code>topCode){ topKmer=kmax; topCode=code; } } } if(topCode==Long.MIN_VALUE){ return fillShort(r, null); } return topKmer; } /* For teaching */ /** Finds the global maximum, forward and reverse, with qualities and border */ public long example3(Read r){ if(r.length()k+4*border ? border : 0); final int max=bases.length-localBorder; for(int i=localBorder; i>>2)|(x2<0 && quals!=null){//Update probability prob=prob*PROB_CORRECT[quals[i]]; if(len>k){ byte oldq=quals[i-k]; prob=prob*PROB_CORRECT_INVERSE[oldq]; } } if(x<0){ len=0; prob=1; }else{len++;} if(len>=k){ final long kmax=Tools.max(kmer, rkmer); final long code=hash(kmax); boolean accept=false; if(code>topCode){ accept=(prob>=minProb || topProb=minProb && topProb>>2)|(x2<>=8; code^=codes[i][x]; } return code&Long.MAX_VALUE; } private class HashThread extends Thread{ HashThread(int id_, int threads_, ArrayList list_, KCountArray table_, int minCount_){ id=id_; threads=threads_; list=list_; table=table_; minCount=minCount_; } @Override public void run(){ for(int i=id; i list; final KCountArray table; final int minCount; } static ReadKey getLocalKey(){ ReadKey key=localReadKey.get(); if(key==null){ localReadKey.set(key=new ReadKey()); } key.clear(); return key; } public final int k; final int shift; final int shift2; final long mask; final long seed; final int border; final int hashes; final boolean addName; final boolean rcompReads; private final long[][] codes; static long defaultSeed=1; static int defaultHashes=4; static int defaultBorder=1; public static float minProb=0f; public static boolean verbose=true; public static boolean mergeFirst=false; public static boolean compareSequence=true; public static ThreadLocal localReadKey=new ThreadLocal(); public static final float[] PROB_CORRECT=Arrays.copyOf(align2.QualityTools.PROB_CORRECT, 127); public static final float[] PROB_CORRECT_INVERSE=Arrays.copyOf(align2.QualityTools.PROB_CORRECT_INVERSE, 127); }