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