| 1 |
package jgi; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.PrintStream; |
| 5 |
import java.util.ArrayList; |
| 6 |
import java.util.HashMap; |
| 7 |
|
| 8 |
import fileIO.ByteFile; |
| 9 |
import fileIO.ByteFile1; |
| 10 |
import fileIO.ByteFile2; |
| 11 |
import fileIO.FileFormat; |
| 12 |
import fileIO.ReadWrite; |
| 13 |
import fileIO.TextFile; |
| 14 |
import fileIO.TextStreamWriter; |
| 15 |
import shared.Parse; |
| 16 |
import shared.Parser; |
| 17 |
import shared.PreParser; |
| 18 |
import shared.Shared; |
| 19 |
import shared.Timer; |
| 20 |
import shared.Tools; |
| 21 |
import shared.TrimRead; |
| 22 |
import stream.ConcurrentGenericReadInputStream; |
| 23 |
import stream.ConcurrentReadInputStream; |
| 24 |
import stream.ConcurrentReadOutputStream; |
| 25 |
import stream.FASTQ; |
| 26 |
import stream.FastaReadInputStream; |
| 27 |
import stream.Read; |
| 28 |
import structures.ListNum; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class FilterByCoverage { |
| 36 |
|
| 37 |
public static void main(String[] args){ |
| 38 |
Timer t=new Timer(); |
| 39 |
FilterByCoverage x=new FilterByCoverage(args); |
| 40 |
x.process(t); |
| 41 |
|
| 42 |
|
| 43 |
Shared.closeStream(x.outstream); |
| 44 |
} |
| 45 |
|
| 46 |
public FilterByCoverage(String[] args){ |
| 47 |
|
| 48 |
{ |
| 49 |
PreParser pp=new PreParser(args, getClass(), false); |
| 50 |
args=pp.args; |
| 51 |
outstream=pp.outstream; |
| 52 |
} |
| 53 |
|
| 54 |
FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false; |
| 55 |
|
| 56 |
Shared.capBufferLen(20); |
| 57 |
Shared.capBuffers(4); |
| 58 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 59 |
ReadWrite.setZipThreads(Shared.threads()); |
| 60 |
|
| 61 |
|
| 62 |
Parser parser=new Parser(); |
| 63 |
for(int i=0; i<args.length; i++){ |
| 64 |
String arg=args[i]; |
| 65 |
String[] split=arg.split("="); |
| 66 |
String a=split[0].toLowerCase(); |
| 67 |
String b=split.length>1 ? split[1] : null; |
| 68 |
|
| 69 |
if(parser.parse(arg, a, b)){ |
| 70 |
|
| 71 |
}else if(a.equals("verbose")){ |
| 72 |
verbose=Parse.parseBoolean(b); |
| 73 |
ByteFile1.verbose=verbose; |
| 74 |
ByteFile2.verbose=verbose; |
| 75 |
stream.FastaReadInputStream.verbose=verbose; |
| 76 |
ConcurrentGenericReadInputStream.verbose=verbose; |
| 77 |
stream.FastqReadInputStream.verbose=verbose; |
| 78 |
ReadWrite.verbose=verbose; |
| 79 |
}else if(a.equals("coverage") || a.equals("cov") || a.equals("covstats") || a.equals("coverage1") || a.equals("cov1") || a.equals("covstats1")){ |
| 80 |
covStatsAfter=b; |
| 81 |
}else if(a.equals("coverage0") || a.equals("cov0") || a.equals("covstats0")){ |
| 82 |
covStatsBefore=b; |
| 83 |
}else if(a.equals("minc") || a.equals("mincov") || a.equals("mincoverage")){ |
| 84 |
minCoverage=Double.parseDouble(b); |
| 85 |
}else if(a.equals("minp") || a.equals("minpercent")){ |
| 86 |
minCoveredPercent=Double.parseDouble(b); |
| 87 |
}else if(a.equals("minr") || a.equals("minreads")){ |
| 88 |
minReads=Parse.parseKMG(b); |
| 89 |
}else if(a.equals("minratio") || a.equals("ratio")){ |
| 90 |
minRatio=Double.parseDouble(b); |
| 91 |
}else if(a.equals("basesundermin")){ |
| 92 |
basesUnderMin=Integer.parseInt(b); |
| 93 |
}else if(a.equals("minl") || a.equals("minlen") || a.equals("minlength")){ |
| 94 |
minLength=Integer.parseInt(b); |
| 95 |
}else if(a.equals("trim") || a.equals("trimends")){ |
| 96 |
if(b==null || Character.isLetter(b.charAt(0))){ |
| 97 |
trimEnds=Parse.parseBoolean(b) ? 100 : 0; |
| 98 |
}else{ |
| 99 |
trimEnds=Integer.parseInt(b); |
| 100 |
} |
| 101 |
trimEnds=Tools.max(trimEnds, 0); |
| 102 |
}else if(a.equals("appendresults") || a.equals("logappend") || a.equals("appendlog") || a.equals("appendtolog")){ |
| 103 |
logappend=Parse.parseBoolean(b); |
| 104 |
}else if(a.equals("log") || a.equals("results")){ |
| 105 |
logfile=b; |
| 106 |
}else if(a.equals("logheader")){ |
| 107 |
logheader=Parse.parseBoolean(b); |
| 108 |
}else if(a.equals("outd") || a.equals("outdirty")){ |
| 109 |
outdirty=b; |
| 110 |
}else if(parser.in1==null && i==0 && Tools.looksLikeInputStream(arg)){ |
| 111 |
parser.in1=arg; |
| 112 |
if(arg.indexOf('#')>-1 && !new File(arg).exists()){ |
| 113 |
parser.in1=arg.replace("#", "1"); |
| 114 |
parser.in2=arg.replace("#", "2"); |
| 115 |
} |
| 116 |
}else if(parser.out1==null && i==1 && !arg.contains("=")){ |
| 117 |
parser.out1=arg; |
| 118 |
}else{ |
| 119 |
outstream.println("Unknown parameter "+args[i]); |
| 120 |
assert(false) : "Unknown parameter "+args[i]; |
| 121 |
|
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
{ |
| 126 |
Parser.processQuality(); |
| 127 |
|
| 128 |
maxReads=parser.maxReads; |
| 129 |
|
| 130 |
overwrite=parser.overwrite; |
| 131 |
append=parser.append; |
| 132 |
|
| 133 |
if(parser.minReadLength>0){minLength=parser.minReadLength;} |
| 134 |
|
| 135 |
in1=parser.in1; |
| 136 |
qfin1=parser.qfin1; |
| 137 |
|
| 138 |
outclean=parser.out1; |
| 139 |
qfoutclean=parser.qfout1; |
| 140 |
|
| 141 |
extin=parser.extin; |
| 142 |
extout=parser.extout; |
| 143 |
} |
| 144 |
minLength=Tools.max(1, minLength); |
| 145 |
|
| 146 |
assert(FastaReadInputStream.settingsOK()); |
| 147 |
|
| 148 |
if(in1==null){throw new RuntimeException("Error - at least one input file is required.");} |
| 149 |
name=ReadWrite.stripToCore(in1); |
| 150 |
|
| 151 |
if(!ByteFile.FORCE_MODE_BF2){ |
| 152 |
ByteFile.FORCE_MODE_BF2=false; |
| 153 |
ByteFile.FORCE_MODE_BF1=true; |
| 154 |
} |
| 155 |
|
| 156 |
if(outclean!=null && outclean.equalsIgnoreCase("null")){outclean=null;} |
| 157 |
if(outdirty!=null && outdirty.equalsIgnoreCase("null")){outdirty=null;} |
| 158 |
|
| 159 |
if(!Tools.testOutputFiles(overwrite, append, false, outclean, outdirty)){ |
| 160 |
outstream.println((outclean==null)+", "+outclean+", "+(outdirty==null)+", "+outdirty); |
| 161 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+outclean+", "+outdirty+"\n"); |
| 162 |
} |
| 163 |
|
| 164 |
ffoutclean=FileFormat.testOutput(outclean, FileFormat.FASTA, extout, true, overwrite, append, false); |
| 165 |
ffoutdirty=FileFormat.testOutput(outdirty, FileFormat.FASTA, extout, true, overwrite, append, false); |
| 166 |
|
| 167 |
ffin1=FileFormat.testInput(in1, FileFormat.FASTA, extin, true, true); |
| 168 |
ffCov0=FileFormat.testInput(covStatsBefore, FileFormat.TEXT, ".txt", true, false); |
| 169 |
ffCov1=FileFormat.testInput(covStatsAfter, FileFormat.TEXT, ".txt", true, false); |
| 170 |
|
| 171 |
assert(covStatsAfter!=null) : "No coverage file specified."; |
| 172 |
} |
| 173 |
|
| 174 |
void process(Timer t){ |
| 175 |
|
| 176 |
final HashMap<String, CovStatsLine> cslMap0=new HashMap<String, CovStatsLine>(1024); |
| 177 |
final HashMap<String, CovStatsLine> cslMap1=new HashMap<String, CovStatsLine>(1024); |
| 178 |
if(ffCov0!=null){ |
| 179 |
TextFile tf=new TextFile(ffCov0); |
| 180 |
int i=0; |
| 181 |
for(String s=tf.nextLine(); s!=null; s=tf.nextLine()){ |
| 182 |
if(i==0){ |
| 183 |
assert(s.startsWith("#")) : "Expected a header line starting with #"; |
| 184 |
CovStatsLine.initializeHeader(s); |
| 185 |
}else{ |
| 186 |
CovStatsLine csl=new CovStatsLine(s); |
| 187 |
CovStatsLine old=cslMap0.put(csl.id, csl); |
| 188 |
assert(old==null); |
| 189 |
} |
| 190 |
i++; |
| 191 |
} |
| 192 |
tf.close(); |
| 193 |
} |
| 194 |
if(ffCov1!=null){ |
| 195 |
TextFile tf=new TextFile(ffCov1); |
| 196 |
int i=0; |
| 197 |
for(String s=tf.nextLine(); s!=null; s=tf.nextLine()){ |
| 198 |
if(i==0){ |
| 199 |
assert(s.startsWith("#")) : "Expected a header line starting with #"; |
| 200 |
CovStatsLine.initializeHeader(s); |
| 201 |
}else{ |
| 202 |
CovStatsLine csl=new CovStatsLine(s); |
| 203 |
CovStatsLine old=cslMap1.put(csl.id, csl); |
| 204 |
assert(old==null); |
| 205 |
} |
| 206 |
i++; |
| 207 |
} |
| 208 |
tf.close(); |
| 209 |
} |
| 210 |
|
| 211 |
final ConcurrentReadInputStream cris; |
| 212 |
{ |
| 213 |
cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffin1, null, qfin1, null); |
| 214 |
if(verbose){outstream.println("Started cris");} |
| 215 |
cris.start(); |
| 216 |
} |
| 217 |
assert(!cris.paired()); |
| 218 |
|
| 219 |
final ConcurrentReadOutputStream rosClean; |
| 220 |
if(outclean!=null){ |
| 221 |
final int buff=4; |
| 222 |
|
| 223 |
assert(!outclean.equalsIgnoreCase(in1) && !outclean.equalsIgnoreCase(in1)) : "Input file and output file have same name."; |
| 224 |
|
| 225 |
rosClean=ConcurrentReadOutputStream.getStream(ffoutclean, null, qfoutclean, null, buff, null, false); |
| 226 |
rosClean.start(); |
| 227 |
}else{rosClean=null;} |
| 228 |
|
| 229 |
final ConcurrentReadOutputStream rosDirty; |
| 230 |
if(outdirty!=null){ |
| 231 |
final int buff=4; |
| 232 |
|
| 233 |
assert(!outdirty.equalsIgnoreCase(in1) && !outdirty.equalsIgnoreCase(in1)) : "Input file and output file have same name."; |
| 234 |
|
| 235 |
rosDirty=ConcurrentReadOutputStream.getStream(ffoutdirty, null, qfoutdirty, null, buff, null, false); |
| 236 |
rosDirty.start(); |
| 237 |
}else{rosDirty=null;} |
| 238 |
|
| 239 |
long readsProcessed=0; |
| 240 |
long basesProcessed=0; |
| 241 |
|
| 242 |
long basesTrimmed=0; |
| 243 |
|
| 244 |
long readsOut=0; |
| 245 |
long basesOut=0; |
| 246 |
|
| 247 |
long readsFiltered=0; |
| 248 |
long basesFiltered=0; |
| 249 |
|
| 250 |
final TextStreamWriter tsw=(logfile==null ? null : new TextStreamWriter(logfile, (overwrite && !logappend), logappend, true)); |
| 251 |
|
| 252 |
if(tsw!=null){ |
| 253 |
tsw.start(); |
| 254 |
if(logheader){tsw.print("#assembly\tcontig\tcontam\tlength\tavgFold\treads\tpercentCovered"+(ffCov0==null ? "" : "\tavgFold0\treads0\tnormRatio")+"\n");} |
| 255 |
} |
| 256 |
|
| 257 |
{ |
| 258 |
|
| 259 |
ListNum<Read> ln=cris.nextList(); |
| 260 |
ArrayList<Read> reads=(ln!=null ? ln.list : null); |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
if(reads!=null && !reads.isEmpty()){ |
| 265 |
Read r=reads.get(0); |
| 266 |
assert((ffin1==null || ffin1.samOrBam()) || (r.mate!=null)==cris.paired()); |
| 267 |
} |
| 268 |
|
| 269 |
while(ln!=null && reads!=null && reads.size()>0){ |
| 270 |
|
| 271 |
final ArrayList<Read> cleanList=new ArrayList<Read>(reads.size()); |
| 272 |
final ArrayList<Read> dirtyList=new ArrayList<Read>(reads.size()); |
| 273 |
for(int idx=0; idx<reads.size(); idx++){ |
| 274 |
final Read r1=reads.get(idx); |
| 275 |
assert(r1.mate==null); |
| 276 |
|
| 277 |
final int initialLength1=r1.length(); |
| 278 |
|
| 279 |
readsProcessed++; |
| 280 |
basesProcessed+=initialLength1; |
| 281 |
|
| 282 |
if(trimEnds>0){ |
| 283 |
if(initialLength1-trimEnds*2<minLength){ |
| 284 |
r1.bases=r1.quality=null; |
| 285 |
}else{ |
| 286 |
TrimRead.trimByAmount(r1, trimEnds, trimEnds, 0); |
| 287 |
} |
| 288 |
} |
| 289 |
final int length=r1.length(); |
| 290 |
basesTrimmed+=(initialLength1-length); |
| 291 |
|
| 292 |
final double covRatio; |
| 293 |
final boolean contam; |
| 294 |
|
| 295 |
final CovStatsLine csl0=cslMap0.get(r1.id); |
| 296 |
final CovStatsLine csl1=cslMap1.get(r1.id); |
| 297 |
if(csl1!=null){ |
| 298 |
|
| 299 |
if(csl0!=null){ |
| 300 |
covRatio=csl0.avgFold/Tools.max(0.01, csl1.avgFold); |
| 301 |
int underMin=csl0.underMin-csl1.underMin; |
| 302 |
|
| 303 |
if(csl1.reads()<minReads || length<minLength || csl1.coveredPercent()<minCoveredPercent){ |
| 304 |
contam=true; |
| 305 |
}else if((csl1.avgFold<minCoverage && covRatio>minRatio) || csl1.avgFold<0.5){ |
| 306 |
contam=true; |
| 307 |
}else if(basesUnderMin>0 && underMin>basesUnderMin){ |
| 308 |
contam=true; |
| 309 |
}else{ |
| 310 |
contam=false; |
| 311 |
} |
| 312 |
}else{ |
| 313 |
covRatio=0; |
| 314 |
int underMin=csl1.underMin; |
| 315 |
|
| 316 |
if(csl1.reads()<minReads || length<minLength || csl1.coveredPercent()<minCoveredPercent || csl1.avgFold<minCoverage){ |
| 317 |
contam=true; |
| 318 |
}else if(basesUnderMin>0 && underMin>basesUnderMin){ |
| 319 |
contam=true; |
| 320 |
}else{ |
| 321 |
contam=false; |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
}else{ |
| 326 |
contam=true; |
| 327 |
covRatio=0; |
| 328 |
} |
| 329 |
|
| 330 |
if(!contam){ |
| 331 |
cleanList.add(r1); |
| 332 |
readsOut++; |
| 333 |
basesOut+=length; |
| 334 |
}else{ |
| 335 |
dirtyList.add(r1); |
| 336 |
readsFiltered++; |
| 337 |
basesFiltered+=length; |
| 338 |
} |
| 339 |
if(tsw!=null && (length>=minLength || PRINT_SHORT_CONTIG_RESULTS)){ |
| 340 |
if(csl1==null){ |
| 341 |
if(ffCov0==null){ |
| 342 |
tsw.print(Tools.format("%s\t%s\t%s\t%d\t%.2f\t%d\t%.2f\n", name, r1.id, contam ? "1" : "0", length, 0, 0, 0)); |
| 343 |
}else{ |
| 344 |
tsw.print(Tools.format("%s\t%s\t%s\t%d\t%.2f\t%d\t%.2f\t%.2f\t%d\t%.2f\n", |
| 345 |
name, r1.id, contam ? "1" : "0", length, 0, 0, 0, 0, 0, 0)); |
| 346 |
} |
| 347 |
|
| 348 |
}else if(csl0==null){ |
| 349 |
tsw.print(Tools.format("%s\t%s\t%s\t%d\t%.2f\t%d\t%.2f\n", name, csl1.id, contam ? "1" : "0", length, |
| 350 |
csl1.avgFold, csl1.plusReads+csl1.minusReads, csl1.coveredPercent())); |
| 351 |
}else{ |
| 352 |
tsw.print(Tools.format("%s\t%s\t%s\t%d\t%.2f\t%d\t%.2f\t%.2f\t%d\t%.2f\n", name, csl1.id, contam ? "1" : "0", length, |
| 353 |
csl1.avgFold, csl1.plusReads+csl1.minusReads, csl1.coveredPercent(), csl0.avgFold, csl0.plusReads+csl0.minusReads, covRatio)); |
| 354 |
} |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
if(rosClean!=null){rosClean.add(cleanList, ln.id);} |
| 359 |
if(rosDirty!=null){rosDirty.add(dirtyList, ln.id);} |
| 360 |
|
| 361 |
cris.returnList(ln); |
| 362 |
ln=cris.nextList(); |
| 363 |
reads=(ln!=null ? ln.list : null); |
| 364 |
} |
| 365 |
if(ln!=null){ |
| 366 |
cris.returnList(ln.id, ln.list==null || ln.list.isEmpty()); |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
errorState|=ReadWrite.closeStreams(cris, rosClean, rosDirty); |
| 371 |
if(tsw!=null){errorState|=tsw.poisonAndWait();} |
| 372 |
|
| 373 |
t.stop(); |
| 374 |
|
| 375 |
double rpnano=readsProcessed/(double)(t.elapsed); |
| 376 |
double bpnano=basesProcessed/(double)(t.elapsed); |
| 377 |
|
| 378 |
outstream.println("Time: "+t); |
| 379 |
outstream.println("Reads In: "+readsProcessed+" \t"+Tools.format("%.2fk reads/sec", rpnano*1000000)); |
| 380 |
outstream.println("Bases In: "+basesProcessed+" \t"+Tools.format("%.2fm bases/sec", bpnano*1000)); |
| 381 |
outstream.println("Reads Out: "+readsOut); |
| 382 |
outstream.println("Bases Out: "+basesOut); |
| 383 |
outstream.println("Reads Filtered: "+readsFiltered); |
| 384 |
outstream.println("Bases Filtered: "+basesFiltered); |
| 385 |
if(trimEnds>0){ |
| 386 |
outstream.println("Bases Trimmed: "+basesTrimmed); |
| 387 |
} |
| 388 |
|
| 389 |
if(errorState){ |
| 390 |
throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt."); |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
private String in1=null; |
| 399 |
private String covStatsBefore=null; |
| 400 |
private String covStatsAfter=null; |
| 401 |
private String name=null; |
| 402 |
|
| 403 |
private String qfin1=null; |
| 404 |
|
| 405 |
private String outclean=null; |
| 406 |
private String outdirty=null; |
| 407 |
|
| 408 |
private String qfoutclean=null; |
| 409 |
private String qfoutdirty=null; |
| 410 |
|
| 411 |
private String extin=null; |
| 412 |
private String extout=null; |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
private long maxReads=-1; |
| 417 |
|
| 418 |
|
| 419 |
private int minLength=0; |
| 420 |
|
| 421 |
private long minReads=0; |
| 422 |
|
| 423 |
private double minCoverage=5; |
| 424 |
|
| 425 |
private double minCoveredPercent=40; |
| 426 |
|
| 427 |
private double minRatio=0; |
| 428 |
|
| 429 |
private int basesUnderMin=-1; |
| 430 |
|
| 431 |
|
| 432 |
private int trimEnds=0; |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
private final FileFormat ffin1; |
| 437 |
private final FileFormat ffCov0; |
| 438 |
private final FileFormat ffCov1; |
| 439 |
|
| 440 |
private final FileFormat ffoutclean; |
| 441 |
private final FileFormat ffoutdirty; |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
private PrintStream outstream=System.err; |
| 447 |
public static boolean verbose=false; |
| 448 |
public boolean errorState=false; |
| 449 |
private boolean overwrite=true; |
| 450 |
private boolean append=false; |
| 451 |
private boolean logappend=false; |
| 452 |
private String logfile=null; |
| 453 |
private boolean logheader=true; |
| 454 |
private static boolean PRINT_SHORT_CONTIG_RESULTS=false; |
| 455 |
|
| 456 |
} |