| 1 |
package cardinality; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.PrintStream; |
| 5 |
import java.util.ArrayList; |
| 6 |
import java.util.Random; |
| 7 |
|
| 8 |
import dna.AminoAcid; |
| 9 |
import fileIO.FileFormat; |
| 10 |
import fileIO.ReadWrite; |
| 11 |
import shared.Parse; |
| 12 |
import shared.Parser; |
| 13 |
import shared.Shared; |
| 14 |
import shared.Timer; |
| 15 |
import shared.Tools; |
| 16 |
import stream.ConcurrentGenericReadInputStream; |
| 17 |
import stream.ConcurrentReadInputStream; |
| 18 |
import stream.FastaReadInputStream; |
| 19 |
import stream.Read; |
| 20 |
import structures.ListNum; |
| 21 |
import structures.LongList; |
| 22 |
import tracker.ReadStats; |
| 23 |
|
| 24 |
class LogLogWrapper { |
| 25 |
|
| 26 |
public static final void main(String[] args){ |
| 27 |
LogLogWrapper llw=new LogLogWrapper(args); |
| 28 |
|
| 29 |
final boolean vic=Read.VALIDATE_IN_CONSTRUCTOR; |
| 30 |
Read.VALIDATE_IN_CONSTRUCTOR=Shared.threads()<4; |
| 31 |
|
| 32 |
Timer t=new Timer(); |
| 33 |
LongList results=new LongList(); |
| 34 |
for(int i=0; i<llw.trials; i++){ |
| 35 |
long card=llw.process(); |
| 36 |
results.add(card); |
| 37 |
} |
| 38 |
|
| 39 |
if(llw.trials>1){ |
| 40 |
results.sort(); |
| 41 |
|
| 42 |
long kmers=llw.kmersProcessed; |
| 43 |
double mean=results.mean(); |
| 44 |
double hmean=results.harmonicMean(); |
| 45 |
double gmean=results.geometricMean(); |
| 46 |
final double div=hmean; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
long median=results.median(); |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
long min=results.min(); |
| 55 |
long max=results.max(); |
| 56 |
long p05=results.percentile(0.05); |
| 57 |
long p95=results.percentile(0.95); |
| 58 |
double stdev=results.stdev(); |
| 59 |
double avgDif=results.avgDif(mean); |
| 60 |
|
| 61 |
|
| 62 |
double range=(p95-p05)/div; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
stdev=stdev/div; |
| 68 |
avgDif=avgDif/div; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
t.stopAndPrint(); |
| 76 |
System.err.println("#Trials\tkmers\thmean\tmedian\tmin\tmax\t5%ile\t95%ile\trange\tstdev\tavgDif"); |
| 77 |
System.err.println(llw.trials+"\t"+kmers+"\t"+String.format("%.2f", hmean)+"\t"+median+"\t"+min+"\t"+max |
| 78 |
+"\t"+p05+"\t"+p95+"\t"+String.format("%.5f", range)+"\t"+String.format("%.5f", stdev)+"\t"+String.format("%.5f", avgDif)); |
| 79 |
|
| 80 |
if(CardinalityTracker.trackCounts){ |
| 81 |
System.err.println("Avg Count:\t"+String.format("%.4f", llw.countSum/(llw.trials*(double)llw.buckets))); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
} |
| 88 |
|
| 89 |
Read.VALIDATE_IN_CONSTRUCTOR=vic; |
| 90 |
} |
| 91 |
|
| 92 |
public LogLogWrapper(String[] args){ |
| 93 |
|
| 94 |
Shared.capBufferLen(200); |
| 95 |
Shared.capBuffers(8); |
| 96 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 97 |
ReadWrite.setZipThreads(Shared.threads()); |
| 98 |
|
| 99 |
Parser parser=new Parser(); |
| 100 |
for(int i=0; i<args.length; i++){ |
| 101 |
String arg=args[i]; |
| 102 |
String[] split=arg.split("="); |
| 103 |
String a=split[0].toLowerCase(); |
| 104 |
String b=split.length>1 ? split[1] : null; |
| 105 |
|
| 106 |
if(a.equals("buckets") || a.equals("loglogbuckets")){ |
| 107 |
buckets=Parse.parseIntKMG(b); |
| 108 |
}else if(a.equals("k") || a.equals("loglogk")){ |
| 109 |
k=Integer.parseInt(b); |
| 110 |
}else if(a.equals("seed") || a.equals("loglogseed")){ |
| 111 |
seed=Long.parseLong(b); |
| 112 |
}else if(a.equals("seed2")){ |
| 113 |
seed2=Long.parseLong(b); |
| 114 |
}else if(a.equals("minprob") || a.equals("loglogminprob")){ |
| 115 |
minProb=Float.parseFloat(b); |
| 116 |
}else if(a.equals("synth")){ |
| 117 |
synth=Parse.parseBoolean(b); |
| 118 |
}else if(a.equals("trials")){ |
| 119 |
trials=Parse.parseIntKMG(b); |
| 120 |
}else if(a.equals("verbose")){ |
| 121 |
verbose=Parse.parseBoolean(b); |
| 122 |
}else if(a.equals("loglogcounts") || a.equals("loglogcount") || |
| 123 |
a.equals("count") || a.equals("counts") || a.equals("trackcounts")){ |
| 124 |
CardinalityTracker.trackCounts=Parse.parseBoolean(b); |
| 125 |
}else if(a.equals("atomic")){ |
| 126 |
assert(false) : "Atomic flag disabled."; |
| 127 |
|
| 128 |
}else if(parser.parse(arg, a, b)){ |
| 129 |
|
| 130 |
}else if(a.equals("parse_flag_goes_here")){ |
| 131 |
|
| 132 |
}else if(in1==null && i==0 && Tools.looksLikeInputStream(arg)){ |
| 133 |
parser.in1=b; |
| 134 |
} |
| 135 |
|
| 136 |
else{ |
| 137 |
outstream.println("Unknown parameter "+args[i]); |
| 138 |
assert(false) : "Unknown parameter "+args[i]; |
| 139 |
|
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
{ |
| 144 |
Parser.processQuality(); |
| 145 |
|
| 146 |
maxReads=parser.maxReads; |
| 147 |
|
| 148 |
overwrite=ReadStats.overwrite=parser.overwrite; |
| 149 |
append=ReadStats.append=parser.append; |
| 150 |
|
| 151 |
in1=(parser.in1==null ? null : parser.in1.split(",")); |
| 152 |
in2=(parser.in2==null ? null : parser.in2.split(",")); |
| 153 |
out=parser.out1; |
| 154 |
} |
| 155 |
|
| 156 |
assert(synth || (in1!=null && in1.length>0)) : "No primary input file specified."; |
| 157 |
if(synth){ |
| 158 |
ffin1=ffin2=null; |
| 159 |
}else{ |
| 160 |
ffin1=new FileFormat[in1.length]; |
| 161 |
ffin2=new FileFormat[in1.length]; |
| 162 |
|
| 163 |
for(int i=0; i<in1.length; i++){ |
| 164 |
String a=in1[i]; |
| 165 |
String b=(in2!=null && in2.length>i ? in2[i] : null); |
| 166 |
assert(a!=null) : "Null input filename."; |
| 167 |
if(b==null && a.indexOf('#')>-1 && !new File(a).exists()){ |
| 168 |
b=a.replace("#", "2"); |
| 169 |
a=a.replace("#", "1"); |
| 170 |
} |
| 171 |
|
| 172 |
ffin1[i]=FileFormat.testInput(a, FileFormat.FASTQ, null, true, true); |
| 173 |
ffin2[i]=FileFormat.testInput(b, FileFormat.FASTQ, null, true, true); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
threads=Shared.threads(); |
| 178 |
assert(FastaReadInputStream.settingsOK()); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
long process(){ |
| 183 |
Timer t=new Timer(); |
| 184 |
readsProcessed=basesProcessed=kmersProcessed=0; |
| 185 |
|
| 186 |
CardinalityTracker log=CardinalityTracker.makeTracker(buckets, k, seed, minProb); |
| 187 |
|
| 188 |
for(int ffnum=0, max=(synth ? 1 : ffin1.length); ffnum<max; ffnum++){ |
| 189 |
ConcurrentReadInputStream cris=null; |
| 190 |
if(!synth){ |
| 191 |
cris=ConcurrentGenericReadInputStream.getReadInputStream(maxReads, false, ffin1[ffnum], ffin2[ffnum]); |
| 192 |
cris.start(); |
| 193 |
} |
| 194 |
|
| 195 |
LogLogThread[] threadArray=new LogLogThread[threads]; |
| 196 |
for(int tid=0; tid<threadArray.length; tid++){ |
| 197 |
threadArray[tid]=new LogLogThread((CardinalityTracker.atomic ? log : CardinalityTracker.makeTracker(buckets, k, seed, minProb)), cris, tid); |
| 198 |
} |
| 199 |
for(LogLogThread llt : threadArray){ |
| 200 |
llt.start(); |
| 201 |
} |
| 202 |
for(LogLogThread llt : threadArray){ |
| 203 |
while(llt.getState()!=Thread.State.TERMINATED){ |
| 204 |
try { |
| 205 |
llt.join(); |
| 206 |
} catch (InterruptedException e) { |
| 207 |
|
| 208 |
e.printStackTrace(); |
| 209 |
} |
| 210 |
} |
| 211 |
readsProcessed+=llt.readsProcessedT; |
| 212 |
basesProcessed+=llt.basesProcessedT; |
| 213 |
kmersProcessed+=llt.kmersProcessedT; |
| 214 |
if(!CardinalityTracker.atomic){log.add(llt.log);} |
| 215 |
} |
| 216 |
|
| 217 |
if(cris!=null){errorState|=ReadWrite.closeStreams(cris);} |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
t.stop(); |
| 229 |
|
| 230 |
|
| 231 |
long cardinality=log.cardinality(); |
| 232 |
countSum+=(CardinalityTracker.trackCounts ? log.countSum() : 0); |
| 233 |
|
| 234 |
if(out!=null){ |
| 235 |
ReadWrite.writeString(cardinality+"\n", out); |
| 236 |
} |
| 237 |
|
| 238 |
if(!Parser.silent) { |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
System.err.println("Cardinality: "+cardinality); |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
System.err.println("Time: \t"+t); |
| 250 |
} |
| 251 |
|
| 252 |
return cardinality; |
| 253 |
} |
| 254 |
|
| 255 |
private static Read makeRead(int len, Random randy, Read r){ |
| 256 |
if(r==null || r.bases==null || r.bases.length!=len){ |
| 257 |
r=new Read(null, null, 0); |
| 258 |
r.bases=new byte[len]; |
| 259 |
} |
| 260 |
byte[] bases=r.bases; |
| 261 |
|
| 262 |
int pos=0; |
| 263 |
final int basesPerRand=4; |
| 264 |
for(int max=bases.length-(bases.length%basesPerRand); pos<max;){ |
| 265 |
int x=randy.nextInt()%prime; |
| 266 |
for(int i=0; i<basesPerRand; i++){ |
| 267 |
int num=x&3; |
| 268 |
byte b=AminoAcid.numberToBase[num]; |
| 269 |
bases[pos]=b; |
| 270 |
pos++; |
| 271 |
x>>=2; |
| 272 |
} |
| 273 |
} |
| 274 |
for(; pos<bases.length; pos++){ |
| 275 |
int x=randy.nextInt()%prime; |
| 276 |
int num=x&3; |
| 277 |
byte b=AminoAcid.numberToBase[num]; |
| 278 |
bases[pos]=b; |
| 279 |
} |
| 280 |
return r; |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
private class LogLogThread extends Thread{ |
| 288 |
|
| 289 |
LogLogThread(CardinalityTracker log_, ConcurrentReadInputStream cris_, int tid_){ |
| 290 |
log=log_; |
| 291 |
cris=cris_; |
| 292 |
tid=tid_; |
| 293 |
} |
| 294 |
|
| 295 |
@Override |
| 296 |
public void run(){ |
| 297 |
if(cris!=null){runCris();} |
| 298 |
else{runSynth();} |
| 299 |
} |
| 300 |
|
| 301 |
public void runCris(){ |
| 302 |
final int kt=k; |
| 303 |
ListNum<Read> ln=cris.nextList(); |
| 304 |
ArrayList<Read> reads=(ln!=null ? ln.list : null); |
| 305 |
while(ln!=null && reads!=null && reads.size()>0){ |
| 306 |
|
| 307 |
for(Read r : reads){ |
| 308 |
|
| 309 |
|
| 310 |
log.hash(r); |
| 311 |
readsProcessedT+=r.pairCount(); |
| 312 |
basesProcessedT+=r.pairLength(); |
| 313 |
kmersProcessedT+=r.numPairKmers(kt); |
| 314 |
} |
| 315 |
|
| 316 |
cris.returnList(ln); |
| 317 |
ln=cris.nextList(); |
| 318 |
reads=(ln!=null ? ln.list : null); |
| 319 |
} |
| 320 |
cris.returnList(ln); |
| 321 |
} |
| 322 |
|
| 323 |
public void runSynth(){ |
| 324 |
final int kt=k; |
| 325 |
assert(maxReads>0 && maxReads<Long.MAX_VALUE); |
| 326 |
long readsLeft=maxReads/threads; |
| 327 |
readsLeft+=(maxReads%threads>tid ? 1 : 0); |
| 328 |
Random randy=Shared.threadLocalRandom(seed2<0 ? seed2 : seed2+999999L); |
| 329 |
|
| 330 |
Read r=null; |
| 331 |
while(readsLeft>0){ |
| 332 |
r=makeRead(150, randy, r); |
| 333 |
log.hash(r); |
| 334 |
readsProcessedT+=r.pairCount(); |
| 335 |
basesProcessedT+=r.pairLength(); |
| 336 |
kmersProcessedT+=r.numPairKmers(kt); |
| 337 |
readsLeft--; |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
private final CardinalityTracker log; |
| 342 |
private final ConcurrentReadInputStream cris; |
| 343 |
private final int tid; |
| 344 |
|
| 345 |
protected long readsProcessedT=0; |
| 346 |
protected long basesProcessedT=0; |
| 347 |
protected long kmersProcessedT=0; |
| 348 |
|
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
private int buckets=2048; |
| 356 |
private int k=31; |
| 357 |
private long seed=-1; |
| 358 |
private long seed2=-1; |
| 359 |
private float minProb=0; |
| 360 |
private long countSum=0; |
| 361 |
|
| 362 |
private String[] in1=null; |
| 363 |
private String[] in2=null; |
| 364 |
private String out=null; |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
protected long readsProcessed=0; |
| 369 |
protected long basesProcessed=0; |
| 370 |
protected long kmersProcessed=0; |
| 371 |
|
| 372 |
private long maxReads=-1; |
| 373 |
|
| 374 |
boolean overwrite=true; |
| 375 |
boolean append=false; |
| 376 |
boolean errorState=false; |
| 377 |
|
| 378 |
private int trials=1; |
| 379 |
private boolean synth=false; |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
private final FileFormat[] ffin1; |
| 386 |
private final FileFormat[] ffin2; |
| 387 |
|
| 388 |
final int threads; |
| 389 |
private static final int prime=32452843; |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
private static PrintStream outstream=System.err; |
| 396 |
public static boolean verbose=false; |
| 397 |
} |