| 1 |
package bin; |
| 2 |
|
| 3 |
import java.io.PrintStream; |
| 4 |
import java.util.Arrays; |
| 5 |
import java.util.Collection; |
| 6 |
|
| 7 |
import dna.AminoAcid; |
| 8 |
import ml.CellNet; |
| 9 |
import shared.Tools; |
| 10 |
import structures.IntHashMap; |
| 11 |
import tax.TaxTree; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
public class BinObject { |
| 20 |
|
| 21 |
public static void setQuant(int x) { |
| 22 |
quant=x; |
| 23 |
assert(quant>0); |
| 24 |
initialize(); |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
private static void initialize() { |
| 33 |
remapMatrix=makeRemapMatrix(2, 5); |
| 34 |
canonicalKmers=makeCanonicalKmers(); |
| 35 |
invCanonicalKmers=makeInvCanonicalKmers(); |
| 36 |
gcmapMatrix=makeGCMapMatrix(); |
| 37 |
} |
| 38 |
|
| 39 |
private static synchronized int[][] makeRemapMatrix(int mink, int maxk){ |
| 40 |
int[][] matrix=new int[maxk+1][]; |
| 41 |
for(int i=mink; i<=maxk; i++) { |
| 42 |
matrix[i]=makeRemap(i); |
| 43 |
} |
| 44 |
return matrix; |
| 45 |
} |
| 46 |
|
| 47 |
private static synchronized int[][] makeGCMapMatrix(){ |
| 48 |
int[][] matrix=new int[remapMatrix.length][]; |
| 49 |
for(int i=0; i<matrix.length; i++) { |
| 50 |
int[] remap=remapMatrix[i]; |
| 51 |
if(remap!=null) { |
| 52 |
matrix[i]=gcmap(i, remap); |
| 53 |
} |
| 54 |
} |
| 55 |
return matrix; |
| 56 |
} |
| 57 |
|
| 58 |
private static synchronized int[] makeCanonicalKmers() { |
| 59 |
int[] array=new int[remapMatrix.length]; |
| 60 |
for(int i=0; i<array.length; i++) { |
| 61 |
int[] remap=remapMatrix[i]; |
| 62 |
int max=(remap==null ? 1 : Tools.max(remap)+1); |
| 63 |
array[i]=max; |
| 64 |
} |
| 65 |
return array; |
| 66 |
} |
| 67 |
|
| 68 |
private static synchronized float[] makeInvCanonicalKmers() { |
| 69 |
float[] array=new float[canonicalKmers.length]; |
| 70 |
for(int i=0; i<array.length; i++) { |
| 71 |
array[i]=1f/canonicalKmers[i]; |
| 72 |
} |
| 73 |
return array; |
| 74 |
} |
| 75 |
|
| 76 |
public static int[] makeRemap(int k){ |
| 77 |
final int bits=2*k; |
| 78 |
final int max=(int)((1L<<bits)-1); |
| 79 |
int count=0; |
| 80 |
IntHashMap canonMap=new IntHashMap(); |
| 81 |
IntHashMap kmerMap=new IntHashMap(); |
| 82 |
for(int kmer=0; kmer<=max; kmer++){ |
| 83 |
|
| 84 |
int canon=Tools.min(kmer, AminoAcid.reverseComplementBinaryFast(kmer, k)); |
| 85 |
if(canon%quant==0 && !canonMap.containsKey(canon)) { |
| 86 |
canonMap.put(canon, count); |
| 87 |
count++; |
| 88 |
} |
| 89 |
int idx=canonMap.get(canon); |
| 90 |
kmerMap.put(kmer, idx); |
| 91 |
} |
| 92 |
int[] remap=new int[max+1]; |
| 93 |
Arrays.fill(remap, -1); |
| 94 |
for(int kmer=0; kmer<=max; kmer++){ |
| 95 |
remap[kmer]=kmerMap.get(kmer); |
| 96 |
|
| 97 |
} |
| 98 |
return remap; |
| 99 |
} |
| 100 |
|
| 101 |
public static int ungap(int kmer, int k, int gap) { |
| 102 |
if(gap<1) {return kmer;} |
| 103 |
int half=k/2; |
| 104 |
int halfbits=half*2; |
| 105 |
int gapbits=2*gap; |
| 106 |
int mask=~((-1)<<halfbits); |
| 107 |
int ungapped=(kmer&mask)|((kmer>>gapbits)&~mask); |
| 108 |
return ungapped; |
| 109 |
} |
| 110 |
|
| 111 |
public static int[] gcmap(int k, int[] remap){ |
| 112 |
int[] gcContent=new int[] {0, 1, 1, 0}; |
| 113 |
final int bits=2*k; |
| 114 |
final int max=(int)((1L<<bits)-1); |
| 115 |
int[] gcmap=new int[canonicalKmers[k]]; |
| 116 |
for(int kmer=0; kmer<=max; kmer++){ |
| 117 |
int gc=0; |
| 118 |
for(int i=0, kmer2=kmer; i<k; i++) { |
| 119 |
gc+=gcContent[kmer2&3]; |
| 120 |
kmer2>>=2; |
| 121 |
} |
| 122 |
int idx=remap[kmer]; |
| 123 |
gcmap[idx]=gc; |
| 124 |
} |
| 125 |
return gcmap; |
| 126 |
} |
| 127 |
|
| 128 |
public static int countKmers(final byte[] bases, final int[] counts, int k){ |
| 129 |
if(quant>1) {return countKmers_quantized(bases, counts, k);} |
| 130 |
if(bases==null || bases.length<k){return 0;} |
| 131 |
|
| 132 |
final int shift=2*k; |
| 133 |
final int mask=~((-1)<<shift); |
| 134 |
|
| 135 |
int kmer=0; |
| 136 |
|
| 137 |
int len=0; |
| 138 |
int valid=0; |
| 139 |
int[] remap=remapMatrix[k]; |
| 140 |
|
| 141 |
for(int i=0; i<bases.length; i++){ |
| 142 |
byte b=bases[i]; |
| 143 |
int x=AminoAcid.baseToNumber[b]; |
| 144 |
|
| 145 |
kmer=((kmer<<2)|x)&mask; |
| 146 |
|
| 147 |
if(x>=0){ |
| 148 |
len++; |
| 149 |
if(len>=k) { |
| 150 |
valid++; |
| 151 |
counts[remap[kmer]]++; |
| 152 |
} |
| 153 |
}else{len=kmer=0;} |
| 154 |
} |
| 155 |
return valid; |
| 156 |
} |
| 157 |
|
| 158 |
public static int countKmers_quantized(final byte[] bases, final int[] counts, int k){ |
| 159 |
if(bases==null || bases.length<k){return 0;} |
| 160 |
|
| 161 |
|
| 162 |
final int shift=2*k; |
| 163 |
final int mask=~((-1)<<shift); |
| 164 |
int[] remap=remapMatrix[k]; |
| 165 |
|
| 166 |
int kmer=0; |
| 167 |
int len=0; |
| 168 |
int valid=0; |
| 169 |
for(int i=0; i<bases.length; i++){ |
| 170 |
byte b=bases[i]; |
| 171 |
int x=AminoAcid.baseToNumber[b]; |
| 172 |
kmer=((kmer<<2)|x)&mask; |
| 173 |
if(x>=0){ |
| 174 |
len++; |
| 175 |
if(len>=k) { |
| 176 |
valid++; |
| 177 |
int pos=remap[kmer]; |
| 178 |
if(pos>=0) {counts[pos]++;} |
| 179 |
} |
| 180 |
}else{len=kmer=0;} |
| 181 |
} |
| 182 |
return valid; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
static final float absDif(float[] a, float[] b){ |
| 191 |
assert(a.length==b.length); |
| 192 |
double sum=0; |
| 193 |
for(int i=0; i<a.length; i++){ |
| 194 |
sum+=Math.abs(a[i]-b[i]); |
| 195 |
} |
| 196 |
|
| 197 |
return (float)sum; |
| 198 |
} |
| 199 |
|
| 200 |
static final float absDif(int[] a, int[] b){ |
| 201 |
return absDif(a, b, 1f/Tools.sum(a), 1f/Tools.sum(b)); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
static final float absDif(int[] a, int[] b, float inva, float invb){ |
| 210 |
assert(a.length==b.length); |
| 211 |
float sum=0; |
| 212 |
for(int i=0; i<a.length; i++){ |
| 213 |
float ai=a[i]*inva, bi=b[i]*invb; |
| 214 |
sum+=Math.abs(ai-bi); |
| 215 |
} |
| 216 |
return sum; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
static final float rmsDif(float[] a, float[] b){ |
| 225 |
assert(a.length==b.length); |
| 226 |
double sum=0; |
| 227 |
for(int i=0; i<a.length; i++){ |
| 228 |
|
| 229 |
double d=(a[i])-(b[i]); |
| 230 |
sum+=d*d; |
| 231 |
} |
| 232 |
|
| 233 |
return (float)Math.sqrt(sum/a.length); |
| 234 |
} |
| 235 |
|
| 236 |
static final float rmsDif(int[] a, int[] b){ |
| 237 |
return rmsDif(a, b, 1f/Tools.sum(a), 1f/Tools.sum(b)); |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
static final float rmsDif(int[] a, int[] b, float inva, float invb){ |
| 246 |
assert(a.length==b.length); |
| 247 |
long sum=0; |
| 248 |
for(int i=0; i<a.length; i++){ |
| 249 |
float ai=a[i]*inva, bi=b[i]*invb; |
| 250 |
float d=(ai-bi); |
| 251 |
sum+=d*d; |
| 252 |
} |
| 253 |
return (float)Math.sqrt(sum/a.length); |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
static final float ksFunction(float[] a, float[] b){ |
| 262 |
assert(a.length==b.length); |
| 263 |
double sum=0; |
| 264 |
for(int i=0; i<a.length; i++){ |
| 265 |
double ai=a[i]+0.0005; |
| 266 |
double bi=b[i]+0.0005; |
| 267 |
double d=(double)ai*Math.log(ai/bi); |
| 268 |
sum+=d; |
| 269 |
} |
| 270 |
|
| 271 |
return (float)sum; |
| 272 |
} |
| 273 |
|
| 274 |
static boolean isValid(Collection<? extends Bin> list, boolean allowLeafContigs) { |
| 275 |
for(Bin b : list) { |
| 276 |
if(b.isCluster()) { |
| 277 |
Cluster c=(Cluster)b; |
| 278 |
assert(c.isValid()); |
| 279 |
for(Contig x : c.contigs) {assert(x.isValid());} |
| 280 |
}else { |
| 281 |
Contig c=(Contig)b; |
| 282 |
assert(c.isValid()); |
| 283 |
assert(allowLeafContigs || c.cluster()==null); |
| 284 |
|
| 285 |
} |
| 286 |
} |
| 287 |
return true; |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
private static int quant=1; |
| 300 |
|
| 301 |
public static int[][] remapMatrix=makeRemapMatrix(2, 5); |
| 302 |
|
| 303 |
public static int[] canonicalKmers=makeCanonicalKmers(); |
| 304 |
public static float[] invCanonicalKmers=makeInvCanonicalKmers(); |
| 305 |
|
| 306 |
public static int[][] gcmapMatrix=makeGCMapMatrix(); |
| 307 |
|
| 308 |
|
| 309 |
static PrintStream outstream=System.err; |
| 310 |
static TaxTree tree=null; |
| 311 |
|
| 312 |
static int minClusterSize=50000; |
| 313 |
static int minContigsPerCluster=1; |
| 314 |
static float depthBoost=0.25f; |
| 315 |
|
| 316 |
public static boolean countPentamers=true; |
| 317 |
public static int minPentamerSizeCount=2000; |
| 318 |
public static int minPentamerSizeCompare=40000; |
| 319 |
|
| 320 |
static boolean loud=false; |
| 321 |
static boolean verbose; |
| 322 |
static float sketchDensity=1/100f; |
| 323 |
static boolean sketchContigs=false; |
| 324 |
static boolean sketchClusters=false; |
| 325 |
static boolean sketchOutput=false; |
| 326 |
static boolean sketchInBulk=true; |
| 327 |
static int sketchSize=20000; |
| 328 |
|
| 329 |
static boolean validation=false; |
| 330 |
static boolean grading=false; |
| 331 |
static boolean parseTaxid=true; |
| 332 |
static boolean depthZeroProxy=true; |
| 333 |
static int globalTime=0; |
| 334 |
|
| 335 |
static CellNet net0small=null; |
| 336 |
static CellNet net0mid=null; |
| 337 |
static CellNet net0large=null; |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
} |