1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
package clump;
import java.util.Random;
import shared.Tools;
import stream.Read;
public class Hasher {
private static synchronized long[][] makeCodes2(int modes){
long[][] r=makeCodes(128, modes);
for(int i=0; i<26; i++){
char c=(char)('A'+i);
r[Tools.toLowerCase(c)]=r[c];
}
return r;
}
private static synchronized long[][] makeCodes(int symbols, int modes){
Random randy=new Random(1);
long[][] r=new long[symbols][modes];
for(int i=0; i<symbols; i++){
for(int j=0; j<modes; j++){
r[i][j]=randy.nextLong();
}
}
return r;
}
public static long hash(byte[] bases){
long code=bases.length;
for(int i=0; i<bases.length; i++){
byte b=bases[i];
int mode=(int)(code&31);
assert(hashcodes[b]!=null) : "Invalid sequence character: '"+(char)b+"'";
code=code^hashcodes[b][mode];
code=Long.rotateLeft(code, 1);
}
return code;
}
public static final long hash(Read r){
return hash(r.bases);
}
public static final long hashPair(Read r){
long a=hash(r);
if(r.mate==null){return a;}
long b=hash(r.mate);
return a^Long.rotateLeft(b, 1);
}
public static final boolean equalsPaired(Read a, Read b){
return equals(a, b) && equals(a.mate, b.mate);
}
public static final boolean equals(Read a, Read b){
if(a==b){return true;}
if(a==null || b==null){
assert(a!=null || b!=null);
return false;
}
if(a.length()!=b.length()){return false;}
if(a.length()==0){return true;}
byte[] ab=a.bases, bb=b.bases;
for(int i=0; i<ab.length; i++){
if(ab[i]!=bb[i]){return false;}
}
return true;
}
private static final long[][] hashcodes=makeCodes2(32);
}
|