annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/opt/bbmap-39.01-1/current/clump/Hasher.java @ 68:5028fdace37b

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