1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 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
|
package jgi;
import java.io.File;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicLong;
import assemble.Tadpole;
import fileIO.ByteFile;
import fileIO.ReadWrite;
import kmer.AbstractKmerTableSet;
import kmer.KmerTableSet;
import shared.Parse;
import shared.Parser;
import shared.PreParser;
import shared.ReadStats;
import shared.Shared;
import shared.Timer;
import shared.Tools;
import stream.FastaReadInputStream;
import ukmer.KmerTableSetU;
/**
* @author Brian Bushnell
* @date Nov 22, 2013
*
*/
public class KmerFilterSetMaker {
/**
* Code entrance from the command line.
* @param args Command line arguments
*/
public static void main(String[] args){
Timer t=new Timer(), t2=new Timer();
t.start();
t2.start();
//Create a new CountKmersExact instance
KmerFilterSetMaker x=new KmerFilterSetMaker(args);
t2.stop();
// outstream.println("Initialization Time: \t"+t2);
///And run it
x.process(t);
//Close the print stream if it was redirected
Shared.closeStream(outstream);
}
/**
* Constructor.
* @param args Command line arguments
*/
public KmerFilterSetMaker(String[] args){
{//Preparse block for help, config files, and outstream
PreParser pp=new PreParser(args, getClass(), false);
args=pp.args;
outstream=pp.outstream;
}
PreParser.silent=true;
/* Set global defaults */
ReadWrite.ZIPLEVEL=2;
ReadWrite.USE_UNPIGZ=true;
if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){
ByteFile.FORCE_MODE_BF2=true;
}
/* Initialize local variables with defaults */
boolean useForest_=false, useTable_=false, useArray_=true;
Parser parser=new Parser();
/* Parse arguments */
for(int i=0; i<args.length; i++){
final String arg=args[i];
String[] split=arg.split("=");
String a=split[0].toLowerCase();
String b=split.length>1 ? split[1] : null;
if(Parser.parseCommonStatic(arg, a, b)){
//do nothing
}else if(Parser.parseZip(arg, a, b)){
//do nothing
}else if(Parser.parseQuality(arg, a, b)){
//do nothing
}else if(Parser.parseFasta(arg, a, b)){
//do nothing
}else if(parser.parseInterleaved(arg, a, b)){
//do nothing
}else if(parser.parseTrim(arg, a, b)){
//do nothing
}else if(a.equals("out") || a.equals("out1") || a.equals("outkmers") || a.equals("outk") || a.equals("dump")){
kmerOutFile=b;
}else if(a.equals("in") || a.equals("in1")){
inFile=b;
}else if(a.equals("initial") || a.equals("starting") || a.equals("initialset") || a.equals("startingset")){
initialKmerFile=b;
}else if(a.equals("temp") || a.equals("pattern")){
outTemp=b;
assert(outTemp.contains("#"));
}else if(a.equals("mincount") || a.equals("min")){
minCount=Integer.parseInt(b);
}else if(a.equals("maxns")){
maxNs=Integer.parseInt(b);
AbstractKmerTableSet.maxNs=maxNs;
}else if(a.equals("minlen")){
minLen=Integer.parseInt(b);
AbstractKmerTableSet.minLen=minLen;
}else if(a.equals("passes") || a.equals("maxpasses")){
maxPasses=Integer.parseInt(b);
}else if(a.equals("minkmers") || a.equals("minkmersperpass") || a.equals("minkpp")){
minKmersPerIteration=Integer.parseInt(b);
}else if(a.equals("maxkmers") || a.equals("maxkmersperpass") || a.equals("maxkpp")){
maxKmersPerIteration=Integer.parseInt(b);
if(maxKmersPerIteration<1){
maxKmersPerIteration=Integer.MAX_VALUE;
}
}
// else if(a.equals("maxcounttodump") || a.equals("maxdump") || a.equals("maxcount")){
// maxToDump=Integer.parseInt(b);
// }
else if(a.equals("append") || a.equals("app")){
append=ReadStats.append=Parse.parseBoolean(b);
}else if(a.equals("overwrite") || a.equals("ow")){
overwrite=Parse.parseBoolean(b);
}else if(a.equals("threads") || a.equals("t")){
THREADS=(b==null || b.equalsIgnoreCase("auto") ? Shared.threads() : Integer.parseInt(b));
}else if(a.equals("verbose")){
assert(false) : "Verbose flag is currently static final; must be recompiled to change.";
// verbose=Parse.parseBoolean(b);
}
else if(KmerTableSet.isValidArgument(a)){
tableArgs.add(arg);
}
else{
throw new RuntimeException("Unknown parameter "+args[i]);
}
}
{//Process parser fields
Parser.processQuality();
}
if(outTemp==null){
outTemp=makeTempFile("rtemp_#", ReadWrite.getExtension(inFile));
}
/* Adjust I/O settings and filenames */
assert(FastaReadInputStream.settingsOK());
assert(kmerOutFile!=null) : "Kmer output file is required.";
if(kmerOutFile!=null && !Tools.canWrite(kmerOutFile, overwrite)){throw new RuntimeException("Output file "+kmerOutFile+" already exists, and overwrite="+overwrite);}
assert(THREADS>0);
k=Tadpole.preparseK(args);
}
/*--------------------------------------------------------------*/
/*---------------- Outer Methods ----------------*/
/*--------------------------------------------------------------*/
public void process(Timer t){
/* Check for output file collisions */
Tools.testOutputFiles(overwrite, append, false, kmerOutFile);
/* Count kmers */
process2();
/* Stop timer and calculate speed statistics */
t.stop();
/* Throw an exception if errors were detected */
if(errorState){
throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt.");
}
}
public void process2(){
/* Start phase timer */
Timer t=new Timer();
AbstractKmerTableSet.DISPLAY_STATS=false;
runAllPasses(inFile, outTemp);
t.stop();
outstream.println();
outstream.println("Input: \t"+readsIn+" reads \t\t"+basesIn+" bases.");
outstream.println("Output: \t"+kmersOut+" kmers.");
outstream.println("Passes: \t"+numPasses);
// if(shave || rinse){
// kmersRemoved=shave(shave, rinse, shaveDepth);
// }
//
// outstream.println("\nFor K="+tables.kbig());
// outstream.println("Unique Kmers: \t"+tables.kmersLoaded);
// if(shave || rinse){
// outstream.println("After Shaving: \t"+(tables.kmersLoaded-kmersRemoved));
// }
outstream.println();
outstream.println("Time: \t"+t);
}
int runAllPasses(String initialInputFile, String tempPattern){
//Handle initial input set
if(initialKmerFile==null){
AbstractKmerTableSet.overwrite=true;
AbstractKmerTableSet.append=false;
}else{
AbstractKmerTableSet.overwrite=false;
AbstractKmerTableSet.append=true;
boolean same=initialKmerFile.equals(kmerOutFile) || new File(initialKmerFile).equals(new File(kmerOutFile));
ReformatReads x=new ReformatReads(new String[] {
"in="+initialKmerFile, "out="+(same ? null : kmerOutFile), "ow", "silent"});
x.process(new Timer());
initialSetSize=x.readsProcessed;
}
//Process primary input
int maxCount=20000000; //Initial array size
String in=initialInputFile;
String lastOut=null;
for(int pass=0; pass<maxPasses && maxCount>=minCount; pass++){
String out=tempPattern.replace("#", ""+pass);
maxCount=runOnePass(in, out, kmerOutFile, maxCount, pass);
if(pass>0){new File(in).delete();}
in=lastOut=out;
AbstractKmerTableSet.overwrite=false;
AbstractKmerTableSet.append=true;
}
if(lastOut!=null){new File(lastOut).delete();}//This does not actually need to be created most of the time
return maxCount;
}
int runOnePass(String inFile, String outFile, String kmerFile, int lastMaxSeen, int pass){
Timer t=new Timer();
@SuppressWarnings("unchecked")
ArrayList<String> tableArgs2=(ArrayList<String>) tableArgs.clone();
tableArgs2.add("k="+k);
tableArgs2.add("in="+inFile);
// tableArgs2.add("showstats=f");
// tableArgs2.add("showprogress=f");
AbstractKmerTableSet.DISPLAY_STATS=AbstractKmerTableSet.DISPLAY_PROGRESS=false;
System.err.print("Pass "+pass+" \t");
//Read input file
AbstractKmerTableSet tables;
if(k<=31){//TODO: 123 add "false" to the clause to force KmerTableSetU usage.
tables=new KmerTableSet(tableArgs2.toArray(new String[0]), 12);
}else{
tables=new KmerTableSetU(tableArgs2.toArray(new String[0]), 0);
}
tables.process(t);
System.err.print(tables.readsIn+" reads \t"+tables.kmersIn+" kmers \t");
if(pass==0){
readsIn+=tables.readsIn;
basesIn+=tables.basesIn;
kmersIn+=tables.kmersIn;
}
numPasses++;
//Summarize counts
long[] counts=tables.fillHistogram(lastMaxSeen);
int max=0;
for(int i=counts.length-1; i>=1; i--){
if(counts[i]>0){
max=i;
break;
}
}
System.err.print(max+" max depth \t");
//Determine minimum count to retain
int numGood=0;
int minCountToKeep=-1;
for(int i=max; i>=1 && numGood<minKmersPerIteration; i--){
if(i==1 && numGood>0){break;}
numGood+=counts[i];
minCountToKeep=i;
}
System.err.print(numGood+" high kmers \t");
if(numGood<1){
System.err.println(0+" retained");
return -1;
}
final int maxToKeep=(int)Tools.min(maxKmersPerIteration, tables.readsIn);
final int numKept=Tools.min(maxToKeep, numGood);
// final long outSize=initialSetSize+kmersOut+numKept;
// System.err.println("\nmaxToKeep="+maxToKeep+", numKept="+numKept+", numGood="+numGood+
// ", maxKmersPerIteration="+maxKmersPerIteration+", tables.readsIn="+tables.readsIn);
//Append retained kmers to a file
if(numKept>0){
tables.dumpKmersAsBytes_MT(kmerFile, minCountToKeep, Integer.MAX_VALUE, false, new AtomicLong(numKept));
// tables.dumpKmersAsBytes(kmerFile, minCountToKeep, Integer.MAX_VALUE, false, new AtomicLong(numKept));
}
System.err.println(numKept+" retained");
kmersOut+=numKept;
//Filter to retain only unmatched reads
if(minCountToKeep>=1 && tables.readsIn>=1){
ArrayList<String> bbdukArgs=new ArrayList<String>();
bbdukArgs.add("in="+inFile);
bbdukArgs.add("outu="+outFile);
bbdukArgs.add("ref="+kmerFile); //Technically this can only be the latest kmers, in case this file gets big.
bbdukArgs.add("k="+k);
bbdukArgs.add("mm=f");
bbdukArgs.add("rcomp="+tables.rcomp());
bbdukArgs.add("silent=t");
if(maxNs>=0 && maxNs<Integer.MAX_VALUE){bbdukArgs.add("maxns="+maxNs);}
if(minLen>0){bbdukArgs.add("minlen="+minLen);}
bbdukArgs.add("ordered");
BBDuk.main(bbdukArgs.toArray(new String[0]));
}
return minCountToKeep;
}
/*--------------------------------------------------------------*/
/*---------------- Helper Methods ----------------*/
/*--------------------------------------------------------------*/
/*--------------------------------------------------------------*/
/*---------------- Helper Classes ----------------*/
/*--------------------------------------------------------------*/
/*--------------------------------------------------------------*/
/*---------------- Fields ----------------*/
/*--------------------------------------------------------------*/
// /** Hold kmers. */
// private final AbstractKmerTableSet tables;
// private boolean shave=false;
// private boolean rinse=false;
// private int shaveDepth=1;
private ArrayList<String> tableArgs=new ArrayList<String>();
private long basesIn=0;
private long readsIn=0;
private long kmersIn=0;
private long kmersOut=0;
private long numPasses;
private long initialSetSize=0;
private int maxPasses=3000;
private int minCount=1;
private int minKmersPerIteration=1;
private int maxKmersPerIteration=2;
private int maxNs=Integer.MAX_VALUE;
private int minLen=1;
/** Kmer count output file */
private String kmerOutFile=null;
private String inFile=null;
private String initialKmerFile=null;
private String outTemp=null;
private String tempKmerFile=makeTempFile("ktemp",".fa");
private boolean errorState=false;
private String makeTempFile(String prefix, String ext){
if(!ext.startsWith(".")){ext="."+ext;}
// try {
return prefix+"_"+(System.nanoTime()&0xFFFFF)+"_"+((int)(10000000*(2+Math.random())))+ext;
// return File.createTempFile("temp#", ".fq").getAbsolutePath();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// return null;
}
/*--------------------------------------------------------------*/
/*---------------- Final Primitives ----------------*/
/*--------------------------------------------------------------*/
final int k;
/*--------------------------------------------------------------*/
/*---------------- Static Fields ----------------*/
/*--------------------------------------------------------------*/
/** Print messages to this stream */
private static PrintStream outstream=System.err;
/** Permission to overwrite existing files */
public static boolean overwrite=false;
/** Permission to append to existing files */
public static boolean append=false;
/** Display progress messages such as memory usage */
public static boolean DISPLAY_PROGRESS=false;
/** Verbose messages */
public static final boolean verbose=false;
/** Number of ProcessThreads */
public static int THREADS=Shared.threads();
}
|