| 1 |
package bin; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.HashMap; |
| 5 |
import java.util.Random; |
| 6 |
|
| 7 |
import fileIO.ByteStreamWriter; |
| 8 |
import fileIO.FileFormat; |
| 9 |
import shared.Parse; |
| 10 |
import shared.Parser; |
| 11 |
import shared.PreParser; |
| 12 |
import shared.Shared; |
| 13 |
import shared.Timer; |
| 14 |
import shared.Tools; |
| 15 |
import structures.ByteBuilder; |
| 16 |
import structures.FloatList; |
| 17 |
import structures.IntHashSet; |
| 18 |
import structures.LongHashSet; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
public class AllToAllVectorMaker extends BinObject { |
| 26 |
|
| 27 |
public static void main(String[] args){ |
| 28 |
|
| 29 |
|
| 30 |
Timer t=new Timer(); |
| 31 |
|
| 32 |
|
| 33 |
AllToAllVectorMaker x=new AllToAllVectorMaker(args); |
| 34 |
|
| 35 |
|
| 36 |
x.process(t); |
| 37 |
|
| 38 |
|
| 39 |
Shared.closeStream(x.outstream); |
| 40 |
} |
| 41 |
|
| 42 |
public AllToAllVectorMaker(String[] args){ |
| 43 |
|
| 44 |
{ |
| 45 |
PreParser pp=new PreParser(args, getClass(), false); |
| 46 |
args=pp.args; |
| 47 |
outstream=pp.outstream; |
| 48 |
} |
| 49 |
loader=new DataLoader(outstream); |
| 50 |
loader.netFileLarge=loader.netFileMid=loader.netFileSmall=null; |
| 51 |
|
| 52 |
Parser parser=new Parser(); |
| 53 |
for(int i=0; i<args.length; i++){ |
| 54 |
String arg=args[i]; |
| 55 |
String[] split=arg.split("="); |
| 56 |
String a=split[0].toLowerCase(); |
| 57 |
String b=split.length>1 ? split[1] : null; |
| 58 |
if(b!=null && b.equalsIgnoreCase("null")){b=null;} |
| 59 |
|
| 60 |
if(a.equals("parse_flag_goes_here")){ |
| 61 |
|
| 62 |
}else if(a.equals("seed")){ |
| 63 |
seed=Long.parseLong(b); |
| 64 |
}else if(a.equals("rate") || a.equals("positivity")){ |
| 65 |
positiveRate=Float.parseFloat(b); |
| 66 |
}else if(a.equals("edgefraction")){ |
| 67 |
edgeFraction=Float.parseFloat(b); |
| 68 |
}else if(a.equals("gcdif") || a.equals("maxgcdif")){ |
| 69 |
maxGCDif=Float.parseFloat(b); |
| 70 |
}else if(a.equals("maxkmerdif")){ |
| 71 |
maxKmerDif=Float.parseFloat(b); |
| 72 |
}else if(a.equals("maxdepthratio")){ |
| 73 |
maxDepthRatio=Float.parseFloat(b); |
| 74 |
}else if(a.equals("lines")){ |
| 75 |
lines=Parse.parseKMG(b); |
| 76 |
}else if(a.equals("rolls")){ |
| 77 |
baseRolls=Integer.parseInt(b); |
| 78 |
}else if(a.equals("kmerdif") || a.equals("outkmerdif")){ |
| 79 |
outKmerDif=b; |
| 80 |
}else if(a.equals("kmerfraction") || a.equals("outkmerfraction")){ |
| 81 |
outKmerFraction=b; |
| 82 |
}else if(a.equals("minlen")){ |
| 83 |
minlen=Parse.parseIntKMG(b); |
| 84 |
}else if(a.equals("maxlen")){ |
| 85 |
maxlen=Parse.parseIntKMG(b); |
| 86 |
}else if(a.equalsIgnoreCase("printSizeInVector")){ |
| 87 |
Oracle.printSizeInVector=Parse.parseBoolean(b); |
| 88 |
}else if(loader.parse(arg, a, b)){ |
| 89 |
|
| 90 |
}else if(SimilarityMeasures.parse(arg, a, b)){ |
| 91 |
|
| 92 |
}else if(parser.parse(arg, a, b)){ |
| 93 |
|
| 94 |
}else{ |
| 95 |
|
| 96 |
assert(false) : "Unknown parameter "+args[i]; |
| 97 |
outstream.println("Unknown parameter "+args[i]); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
{ |
| 102 |
Parser.processQuality(); |
| 103 |
out1=parser.out1; |
| 104 |
} |
| 105 |
maxProduct=maxKmerDif*maxDepthRatio*0.75f; |
| 106 |
KmerProb.load(); |
| 107 |
ffout1=FileFormat.testOutput(out1, FileFormat.TXT, null, true, true, false, false); |
| 108 |
} |
| 109 |
|
| 110 |
void process(Timer t){ |
| 111 |
|
| 112 |
validation=true; |
| 113 |
ArrayList<Contig> contigs=allContigs=loader.loadData(); |
| 114 |
if(verbose){outstream.println("Finished reading data.");} |
| 115 |
loader.trimEdges(contigs, Binner.maxEdges, Binner.minEdgeWeight, true); |
| 116 |
|
| 117 |
HashMap<Integer, ArrayList<Contig>> map=new HashMap<Integer, ArrayList<Contig>>(); |
| 118 |
for(Contig c : contigs) { |
| 119 |
if(c.labelTaxid>0) { |
| 120 |
ArrayList<Contig> list=map.get(c.labelTaxid); |
| 121 |
if(list==null) {map.put(c.labelTaxid, list=new ArrayList<Contig>());} |
| 122 |
list.add(c); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
outputResults(contigs, map); |
| 127 |
if(outKmerDif!=null) { |
| 128 |
outputKmerDifs(outKmerDif, 0); |
| 129 |
outputKmerDifs(outKmerDif, 1); |
| 130 |
} |
| 131 |
if(outKmerFraction!=null) { |
| 132 |
outputKmerDifFraction(outKmerFraction, 1.0/1024, 0.25); |
| 133 |
} |
| 134 |
|
| 135 |
t.stop(); |
| 136 |
outstream.println("Positive: \t"+positiveLines); |
| 137 |
outstream.println("Negative: \t"+negativeLines); |
| 138 |
outstream.println("Time: \t"+t); |
| 139 |
outstream.println("Reads Processed: "+loader.contigsLoaded+ |
| 140 |
" \t"+Tools.format("%.2fk bases/sec", (loader.basesLoaded/(double)(t.elapsed))*1000000)); |
| 141 |
assert(!errorState) : "An error was encountered."; |
| 142 |
} |
| 143 |
|
| 144 |
private void outputResults(ArrayList<Contig> contigs, HashMap<Integer, ArrayList<Contig>> map){ |
| 145 |
LongHashSet used=new LongHashSet(); |
| 146 |
ByteStreamWriter bsw=ByteStreamWriter.makeBSW(ffout1); |
| 147 |
|
| 148 |
randy=Shared.threadLocalRandom(seed); |
| 149 |
Oracle oracle=new Oracle(999999, 0); |
| 150 |
if(bsw!=null) { |
| 151 |
vecBuffer.clear(); |
| 152 |
oracle.toVector(contigs.get(0), contigs.get(1), vecBuffer, true); |
| 153 |
bsw.print("#dims\t").print(vecBuffer.size-1).tab().println(1); |
| 154 |
} |
| 155 |
ArrayList<ArrayList<Contig>> clusters=new ArrayList<ArrayList<Contig>>(map.values()); |
| 156 |
|
| 157 |
while(linesOut<lines) { |
| 158 |
final boolean positive=(randy.nextFloat()<=positiveRate); |
| 159 |
ByteBuilder bb=null; |
| 160 |
while(bb==null) {bb=makeLine(clusters, oracle, positive);} |
| 161 |
if(bb!=null) { |
| 162 |
if(bsw!=null) { |
| 163 |
bsw.print(bb.nl()); |
| 164 |
bb.clear(); |
| 165 |
} |
| 166 |
linesOut++; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
if(bsw!=null) { |
| 171 |
errorState=bsw.poisonAndWait() | errorState; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
private void outputKmerDifs(String fname, int sign) { |
| 176 |
fname=fname.replaceFirst("%", sign+""); |
| 177 |
ByteStreamWriter bsw=ByteStreamWriter.makeBSW(fname, true, false, true); |
| 178 |
FloatList[] matrix=kmerDifMatrix[sign]; |
| 179 |
assert(matrix!=null); |
| 180 |
for(int lenIdx=0; lenIdx<matrix.length; lenIdx++) { |
| 181 |
FloatList list=matrix[lenIdx]; |
| 182 |
|
| 183 |
if(list!=null && list.size()>=100) { |
| 184 |
int length=KmerProb.dequantizeLength(lenIdx); |
| 185 |
list.sort(); |
| 186 |
bsw.print(length).tab().print(list.size()); |
| 187 |
for(int i=0; i<=100; i++) { |
| 188 |
bsw.tab().print(list.percentile(i*0.01), 8); |
| 189 |
} |
| 190 |
bsw.println(); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
int x=Tools.binarySearch(new float[1], 1); |
| 195 |
|
| 196 |
|
| 197 |
bsw.poisonAndWait(); |
| 198 |
} |
| 199 |
|
| 200 |
private void outputKmerDifFraction(String fname, double incr, double max) { |
| 201 |
ByteStreamWriter bsw=ByteStreamWriter.makeBSW(fname, true, false, true); |
| 202 |
bsw.println("#ceil(size)\tcount\tprobs"); |
| 203 |
FloatList[] matrix1=kmerDifMatrix[1]; |
| 204 |
FloatList[] matrix0=kmerDifMatrix[0]; |
| 205 |
|
| 206 |
bsw.print(" \t "); |
| 207 |
float key=0; |
| 208 |
for(int i=0; key<=max; i++) { |
| 209 |
key=(float)(i*incr); |
| 210 |
bsw.tab().print(key, 10); |
| 211 |
} |
| 212 |
|
| 213 |
for(int lenIdx=0; lenIdx<matrix1.length; lenIdx++) { |
| 214 |
FloatList list1=matrix1[lenIdx]; |
| 215 |
FloatList list0=matrix0[lenIdx]; |
| 216 |
int size=Tools.min(list1==null ? 0 : list1.size(), list0==null ? 0 : list0.size()); |
| 217 |
if(size>100) { |
| 218 |
int length=KmerProb.dequantizeLength(lenIdx); |
| 219 |
FloatList fractions=fractionIndexedByKmerDif(list1, list0, incr, max); |
| 220 |
bsw.print(length).tab().print(size); |
| 221 |
for(int i=0; i<fractions.size(); i++) { |
| 222 |
bsw.tab().print(fractions.get(i), 8); |
| 223 |
} |
| 224 |
bsw.println(); |
| 225 |
|
| 226 |
|
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
int x=Tools.binarySearch(new float[1], 1); |
| 231 |
|
| 232 |
|
| 233 |
bsw.poisonAndWait(); |
| 234 |
} |
| 235 |
|
| 236 |
private FloatList fractionIndexedByKmerDif(FloatList plus, FloatList minus, double incr, double max) { |
| 237 |
plus.shrink().sort(); |
| 238 |
minus.shrink().sort(); |
| 239 |
float invPlus=1f/Math.max(1, plus.size()); |
| 240 |
float invMinus=1f/Math.max(1, minus.size()); |
| 241 |
FloatList fractions=new FloatList(1+(int)Math.ceil(max/incr)); |
| 242 |
float key=0; |
| 243 |
for(int i=0; key<=max; i++) { |
| 244 |
key=(float)(i*incr); |
| 245 |
int idxPlus=Tools.max(1, Tools.binarySearch(plus.array, key)); |
| 246 |
int idxMinus=Tools.binarySearch(minus.array, key); |
| 247 |
float fractionPlus=idxPlus*invPlus; |
| 248 |
float fractionMinus=idxMinus*invMinus; |
| 249 |
float fraction=fractionPlus/(fractionPlus+fractionMinus); |
| 250 |
fractions.add(fraction); |
| 251 |
} |
| 252 |
for(int i=fractions.size()-2; i>=0; i--) { |
| 253 |
fractions.set(i, Tools.max(fractions.get(i), fractions.get(i+1))); |
| 254 |
} |
| 255 |
return fractions; |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
private ByteBuilder makeLine(ArrayList<ArrayList<Contig>> clusters, Oracle oracle, final boolean positive) { |
| 288 |
ArrayList<Contig> alist=clusters.get(randy.nextInt(clusters.size())); |
| 289 |
ArrayList<Contig> blist=alist; |
| 290 |
while(!positive && alist==blist) {blist=clusters.get(randy.nextInt(clusters.size()));} |
| 291 |
FloatList vector=null; |
| 292 |
for(int i=0; i<9 && vector==null; i++) { |
| 293 |
vector=makeVector(alist, blist, minlen, maxlen, oracle); |
| 294 |
} |
| 295 |
|
| 296 |
return vector==null ? null : toLine(vector); |
| 297 |
} |
| 298 |
|
| 299 |
private FloatList makeVector(ArrayList<Contig> alist, ArrayList<Contig> blist, |
| 300 |
int minSize, int maxSize, Oracle oracle) { |
| 301 |
int numClusters=randy.nextInt(3); |
| 302 |
|
| 303 |
|
| 304 |
if(numClusters==0) { |
| 305 |
IntHashSet used=new IntHashSet(7); |
| 306 |
Contig a=selectContig(alist, minSize, maxSize, used); |
| 307 |
Contig b=selectContig(blist, minSize, Integer.MAX_VALUE, used); |
| 308 |
if(!passesFilter(a, b)) {return null;} |
| 309 |
assert(a!=b); |
| 310 |
vecBuffer.clear(); |
| 311 |
|
| 312 |
return oracle.toVector(a, b, vecBuffer, true); |
| 313 |
}else if(numClusters==1) { |
| 314 |
IntHashSet used=new IntHashSet(7); |
| 315 |
Contig a=selectContig(alist, minSize, maxSize, used); |
| 316 |
if(a==null) {return null;} |
| 317 |
Cluster b=selectCluster(blist, 2+randy.nextInt(maxClusterContigs-1), minSize, Integer.MAX_VALUE, used); |
| 318 |
if(!passesFilter(a, b)) {return null;} |
| 319 |
vecBuffer.clear(); |
| 320 |
|
| 321 |
FloatList fl=oracle.toVector(a, b, vecBuffer, true); |
| 322 |
decluster(b); |
| 323 |
return fl; |
| 324 |
}else { |
| 325 |
Cluster a=selectCluster(alist, 2+randy.nextInt(maxClusterContigs-1), minSize, maxSize, null); |
| 326 |
if(a==null) {return null;} |
| 327 |
Cluster b=selectCluster(blist, 2+randy.nextInt(maxClusterContigs-1), minSize, Integer.MAX_VALUE, a.contigSet); |
| 328 |
if(!passesFilter(a, b)) { |
| 329 |
decluster(a); |
| 330 |
return null; |
| 331 |
} |
| 332 |
vecBuffer.clear(); |
| 333 |
|
| 334 |
FloatList fl=oracle.toVector(a, b, vecBuffer, true); |
| 335 |
decluster(a); |
| 336 |
decluster(b); |
| 337 |
return fl; |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
private void decluster(Cluster clust) { |
| 342 |
for(Contig c : clust) {c.cluster=null; c.dest=-1;} |
| 343 |
clust.clear(); |
| 344 |
} |
| 345 |
|
| 346 |
private Bin selectBin(ArrayList<Contig> list, int maxElements, int minSize, int maxSize, IntHashSet used) { |
| 347 |
if(maxElements==1) {return selectContig(list, minSize, maxSize, used);} |
| 348 |
return selectCluster(list, maxElements, minSize, maxSize, used); |
| 349 |
} |
| 350 |
|
| 351 |
private Cluster selectCluster(ArrayList<Contig> list, int maxElements, int minSize, int maxSize, IntHashSet used) { |
| 352 |
IntHashSet set=new IntHashSet(7); |
| 353 |
long size=0; |
| 354 |
for(int i=0; i<100; i++) { |
| 355 |
Contig c=list.get(randy.nextInt(list.size())); |
| 356 |
long size2=size+c.size(); |
| 357 |
if(size2>=minSize && size2<=maxSize && !set.contains(c.id()) |
| 358 |
&& (used==null || !used.contains(c.id()))) { |
| 359 |
set.add(c.id()); |
| 360 |
size=size2; |
| 361 |
} |
| 362 |
if(set.size()>=maxElements) {break;} |
| 363 |
if(size>minSize) { |
| 364 |
if(i>20 && set.size()>=2) {break;} |
| 365 |
if(randy.nextFloat()<0.05f) {break;} |
| 366 |
} |
| 367 |
} |
| 368 |
if(size<minSize || size>maxSize) {return null;} |
| 369 |
Cluster clust=new Cluster(0); |
| 370 |
for(int i : set.toArray()) { |
| 371 |
clust.add(allContigs.get(i)); |
| 372 |
} |
| 373 |
return clust; |
| 374 |
} |
| 375 |
|
| 376 |
private Contig selectContig(ArrayList<Contig> list, int minSize, int maxSize, IntHashSet used) { |
| 377 |
for(int i=0; i<40; i++) { |
| 378 |
Contig c=list.get(randy.nextInt(list.size())); |
| 379 |
if(c.size()>=minSize && c.size()<=maxSize && (used==null || !used.contains(c.id()))) { |
| 380 |
used.add(c.id()); |
| 381 |
return c; |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
return null; |
| 389 |
} |
| 390 |
|
| 391 |
private ByteBuilder toLine(FloatList vector) { |
| 392 |
lineBuffer.clear(); |
| 393 |
for(int i=0; i<vector.size(); i++) { |
| 394 |
if(i>0) {lineBuffer.tab();} |
| 395 |
lineBuffer.append(vector.get(i), 7, true); |
| 396 |
} |
| 397 |
if(vector.lastElement()==1) {positiveLines++;} |
| 398 |
else {negativeLines++;} |
| 399 |
return lineBuffer; |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
private boolean passesFilter(Bin a, Bin b) { |
| 430 |
if(a==null || b==null || a==b) {return false;} |
| 431 |
final float gcDif=Tools.absdif(a.gc(), b.gc()); |
| 432 |
final boolean same=a.labelTaxid==b.labelTaxid; |
| 433 |
if(gcDif>maxGCDif) { |
| 434 |
|
| 435 |
return false; |
| 436 |
} |
| 437 |
final float depthRatio=a.depthRatio(b); |
| 438 |
if(depthRatio>maxDepthRatio) { |
| 439 |
|
| 440 |
return false; |
| 441 |
} |
| 442 |
final float kmerDif=SimilarityMeasures.calculateDifferenceAverage(a.tetramers, b.tetramers); |
| 443 |
if(kmerDif>maxKmerDif) { |
| 444 |
|
| 445 |
return false; |
| 446 |
} |
| 447 |
final float product=kmerDif*depthRatio; |
| 448 |
if(product>maxProduct) { |
| 449 |
|
| 450 |
return false; |
| 451 |
} |
| 452 |
return true; |
| 453 |
} |
| 454 |
|
| 455 |
private int randomIndex(Random randy, int max, int rolls) { |
| 456 |
int idx=randy.nextInt(max); |
| 457 |
for(int i=randy.nextInt(rolls+1); i>0; i--) { |
| 458 |
idx=Math.min(idx, randy.nextInt(max)); |
| 459 |
} |
| 460 |
return idx; |
| 461 |
} |
| 462 |
|
| 463 |
private static long toKey(int a, int b) { |
| 464 |
return (((long)Math.min(a, b))<<32)|(long)Math.max(a, b); |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
FloatList getDifList(int size, int sameGenome) { |
| 470 |
int idx=KmerProb.quantizeLength(size); |
| 471 |
FloatList[] matrix=kmerDifMatrix[sameGenome]; |
| 472 |
if(matrix[idx]==null) {matrix[idx]=new FloatList();} |
| 473 |
return matrix[idx]; |
| 474 |
} |
| 475 |
|
| 476 |
FloatList[][] kmerDifMatrix=new FloatList[2][38]; |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
private String out1=null; |
| 481 |
|
| 482 |
private String outKmerDif=null; |
| 483 |
private String outKmerFraction=null; |
| 484 |
|
| 485 |
private final FileFormat ffout1; |
| 486 |
|
| 487 |
DataLoader loader=null; |
| 488 |
long seed=-1; |
| 489 |
long lines=1000000; |
| 490 |
long linesOut=0; |
| 491 |
long posCount=0; |
| 492 |
long negCount=0; |
| 493 |
float positiveRate=0.5f; |
| 494 |
float edgeFraction=0.1f; |
| 495 |
int baseRolls=2; |
| 496 |
long positiveLines=0; |
| 497 |
long negativeLines=0; |
| 498 |
int maxClusterContigs=9; |
| 499 |
Random randy; |
| 500 |
|
| 501 |
float maxGCDif=1.0f; |
| 502 |
float maxDepthRatio=1000.0f; |
| 503 |
float maxKmerDif=1.0f; |
| 504 |
final float maxProduct; |
| 505 |
|
| 506 |
int minlen=100; |
| 507 |
int maxlen=2000000000; |
| 508 |
|
| 509 |
ArrayList<Contig> allContigs=null; |
| 510 |
ArrayList<ArrayList<Contig>> allSets=null; |
| 511 |
private final ByteBuilder lineBuffer=new ByteBuilder(); |
| 512 |
private final FloatList vecBuffer=new FloatList(); |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
private boolean errorState=false; |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
private java.io.PrintStream outstream=System.err; |
| 521 |
public static boolean verbose=false; |
| 522 |
|
| 523 |
} |