| 1 |
package bbmin; |
| 2 |
|
| 3 |
import java.util.Arrays; |
| 4 |
|
| 5 |
import structures.LongHashSet; |
| 6 |
import structures.LongList; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
public class Minimizer { |
| 19 |
|
| 20 |
public static void main(String[] args){ |
| 21 |
int k=4, w=7; |
| 22 |
String seq="ACGTCTGAGCCTTGACACATGACT"; |
| 23 |
try { |
| 24 |
k=Integer.parseInt(args[0]); |
| 25 |
w=Integer.parseInt(args[1]); |
| 26 |
seq=args[2]; |
| 27 |
} catch (NumberFormatException e) { |
| 28 |
|
| 29 |
System.err.println("Usage: bbmin.Minimizer kmerlen window seq\n" |
| 30 |
+ "E.G.\n" |
| 31 |
+ "bbmin.Minimizer 4 7 ACGTCTGAGCCTTGACACATGACT"); |
| 32 |
System.exit(1); |
| 33 |
} |
| 34 |
Minimizer minnow=new Minimizer(k, w); |
| 35 |
long[] array=minnow.minimize(seq.getBytes()); |
| 36 |
System.err.println(Arrays.toString(array)); |
| 37 |
} |
| 38 |
|
| 39 |
public Minimizer(int k_, int window_){this(k_, window_, 2);} |
| 40 |
public Minimizer(int k_, int window_, int bitsPerSymbol_){ |
| 41 |
k=k_; |
| 42 |
window=window_; |
| 43 |
bitsPerSymbol=bitsPerSymbol_; |
| 44 |
shift=bitsPerSymbol*k; |
| 45 |
shift2=shift-bitsPerSymbol; |
| 46 |
mask=(shift>63 ? -1L : ~((-1L)<<shift)); |
| 47 |
} |
| 48 |
|
| 49 |
public long[] minimize(String str){ |
| 50 |
return minimize(str.getBytes()); |
| 51 |
} |
| 52 |
|
| 53 |
public long[] minimize(byte[] bases){ |
| 54 |
return minimize(bases, new LongList(16), new LongHashSet(16)); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
public long[] minimize(byte[] bases, LongList list, LongHashSet set){ |
| 59 |
list.clear(); |
| 60 |
|
| 61 |
if(set.capacity()*(long)window>100L+16L*bases.length){ |
| 62 |
set.resizeDestructive(16); |
| 63 |
}else{ |
| 64 |
set.clear(); |
| 65 |
} |
| 66 |
|
| 67 |
long kmersProcessed=0; |
| 68 |
long kmer=0; |
| 69 |
long rkmer=0; |
| 70 |
int len=0; |
| 71 |
|
| 72 |
long bestCode=Long.MAX_VALUE; |
| 73 |
int bestPosition=-1; |
| 74 |
long bestKmer=-1; |
| 75 |
long bestRkmer=-1; |
| 76 |
int currentWindow=0; |
| 77 |
|
| 78 |
for(int i=0; i<bases.length; i++){ |
| 79 |
byte b=bases[i]; |
| 80 |
long x=baseToNumber[b]; |
| 81 |
long x2=baseToComplementNumber[b]; |
| 82 |
|
| 83 |
kmer=((kmer<<2)|x)&mask; |
| 84 |
rkmer=((rkmer>>>2)|(x2<<shift2))&mask; |
| 85 |
if(x<0){ |
| 86 |
len=0; |
| 87 |
rkmer=0; |
| 88 |
}else{ |
| 89 |
len++; |
| 90 |
} |
| 91 |
|
| 92 |
if(len>=k){ |
| 93 |
kmersProcessed++; |
| 94 |
currentWindow++; |
| 95 |
|
| 96 |
final long hashcode=hash(kmer, rkmer); |
| 97 |
System.err.println("i="+i+", code="+hashcode); |
| 98 |
|
| 99 |
|
| 100 |
if(hashcode>=minCode && hashcode<=bestCode){ |
| 101 |
bestCode=hashcode; |
| 102 |
bestPosition=i; |
| 103 |
bestKmer=kmer; |
| 104 |
bestRkmer=rkmer; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
if(currentWindow>=window && bestPosition>=0){ |
| 110 |
if(!set.contains(bestCode)){ |
| 111 |
set.add(bestCode); |
| 112 |
list.add(bestCode); |
| 113 |
} |
| 114 |
i=bestPosition; |
| 115 |
kmer=bestKmer; |
| 116 |
rkmer=bestRkmer; |
| 117 |
len=k; |
| 118 |
|
| 119 |
bestCode=Long.MAX_VALUE; |
| 120 |
bestPosition=-1; |
| 121 |
currentWindow=0; |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
list.sort(); |
| 126 |
return list.toArray(); |
| 127 |
} |
| 128 |
|
| 129 |
public static long canon(long kmer, long rkmer){return max(kmer, rkmer);} |
| 130 |
public static long hash(long kmer, long rkmer){return hash(canon(kmer, rkmer));} |
| 131 |
public static long hash(long key) { |
| 132 |
key = (~key) + (key << 21); |
| 133 |
key = key ^ (key >>> 24); |
| 134 |
key = (key + (key << 3)) + (key << 8); |
| 135 |
key = key ^ (key >>> 14); |
| 136 |
key = (key + (key << 2)) + (key << 4); |
| 137 |
key = key ^ (key >>> 28); |
| 138 |
key = key + (key << 31); |
| 139 |
return key; |
| 140 |
} |
| 141 |
private static final long max(long x, long y){return x>y ? x : y;} |
| 142 |
|
| 143 |
public final int k; |
| 144 |
public final int window; |
| 145 |
public final int bitsPerSymbol; |
| 146 |
private final int shift; |
| 147 |
private final int shift2; |
| 148 |
private final long mask; |
| 149 |
private final long minCode=0; |
| 150 |
|
| 151 |
static final byte[] baseToNumber = new byte[128]; |
| 152 |
static final byte[] baseToComplementNumber = new byte[128]; |
| 153 |
|
| 154 |
static { |
| 155 |
Arrays.fill(baseToNumber, (byte)-1); |
| 156 |
Arrays.fill(baseToComplementNumber, (byte)-1); |
| 157 |
baseToNumber['A']=baseToNumber['a']=baseToComplementNumber['T']=baseToComplementNumber['t']=0; |
| 158 |
baseToNumber['C']=baseToNumber['c']=baseToComplementNumber['G']=baseToComplementNumber['c']=1; |
| 159 |
baseToNumber['G']=baseToNumber['g']=baseToComplementNumber['C']=baseToComplementNumber['g']=2; |
| 160 |
baseToNumber['T']=baseToNumber['t']=baseToComplementNumber['A']=baseToComplementNumber['a']=3; |
| 161 |
} |
| 162 |
} |