| 1 |
package driver; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.PrintStream; |
| 5 |
import java.util.ArrayList; |
| 6 |
import java.util.LinkedHashSet; |
| 7 |
|
| 8 |
import fileIO.ByteFile; |
| 9 |
import fileIO.ByteFile1; |
| 10 |
import fileIO.ByteFile2; |
| 11 |
import fileIO.FileFormat; |
| 12 |
import fileIO.ReadWrite; |
| 13 |
import hiseq.IlluminaHeaderParser2; |
| 14 |
import shared.Parse; |
| 15 |
import shared.Parser; |
| 16 |
import shared.PreParser; |
| 17 |
import shared.Shared; |
| 18 |
import shared.Timer; |
| 19 |
import shared.Tools; |
| 20 |
import shared.TrimRead; |
| 21 |
import stream.ConcurrentGenericReadInputStream; |
| 22 |
import stream.ConcurrentReadInputStream; |
| 23 |
import stream.ConcurrentReadOutputStream; |
| 24 |
import stream.FASTQ; |
| 25 |
import stream.FastaReadInputStream; |
| 26 |
import stream.Read; |
| 27 |
import stream.ReadStreamWriter; |
| 28 |
import stream.SamLine; |
| 29 |
import structures.ByteBuilder; |
| 30 |
import structures.ListNum; |
| 31 |
import tracker.ReadStats; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class FilterReadsByName { |
| 39 |
|
| 40 |
public static void main(String[] args){ |
| 41 |
Timer t=new Timer(); |
| 42 |
FilterReadsByName x=new FilterReadsByName(args); |
| 43 |
x.process(t); |
| 44 |
|
| 45 |
|
| 46 |
Shared.closeStream(x.outstream); |
| 47 |
} |
| 48 |
|
| 49 |
public FilterReadsByName(String[] args){ |
| 50 |
|
| 51 |
{ |
| 52 |
PreParser pp=new PreParser(args, getClass(), false); |
| 53 |
args=pp.args; |
| 54 |
outstream=pp.outstream; |
| 55 |
} |
| 56 |
|
| 57 |
Shared.capBuffers(4); |
| 58 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 59 |
ReadWrite.setZipThreads(Shared.threads()); |
| 60 |
SamLine.SET_FROM_OK=true; |
| 61 |
ReadStreamWriter.USE_ATTACHED_SAMLINE=true; |
| 62 |
|
| 63 |
boolean setInterleaved=false; |
| 64 |
Parser parser=new Parser(); |
| 65 |
|
| 66 |
for(int i=0; i<args.length; i++){ |
| 67 |
String arg=args[i]; |
| 68 |
String[] split=arg.split("="); |
| 69 |
String a=split[0].toLowerCase(); |
| 70 |
String b=split.length>1 ? split[1] : null; |
| 71 |
|
| 72 |
if(a.equals("verbose")){ |
| 73 |
verbose=Parse.parseBoolean(b); |
| 74 |
ByteFile1.verbose=verbose; |
| 75 |
ByteFile2.verbose=verbose; |
| 76 |
stream.FastaReadInputStream.verbose=verbose; |
| 77 |
ConcurrentGenericReadInputStream.verbose=verbose; |
| 78 |
stream.FastqReadInputStream.verbose=verbose; |
| 79 |
ReadWrite.verbose=verbose; |
| 80 |
}else if(a.equals("names")){ |
| 81 |
if(b!=null){ |
| 82 |
String[] x=b.split(","); |
| 83 |
for(String s : x){ |
| 84 |
names.add(s); |
| 85 |
} |
| 86 |
} |
| 87 |
}else if(a.equals("substrings") || a.equals("substring")){ |
| 88 |
if(b==null){b="t";} |
| 89 |
if(b.equals("header")){ |
| 90 |
headerSubstringOfName=true; |
| 91 |
}else if(b.equals("name")){ |
| 92 |
nameSubstringOfHeader=true; |
| 93 |
}else{ |
| 94 |
nameSubstringOfHeader=headerSubstringOfName=Parse.parseBoolean(b); |
| 95 |
} |
| 96 |
}else if(a.equals("casesensitive") || a.equals("case")){ |
| 97 |
ignoreCase=!Parse.parseBoolean(b); |
| 98 |
}else if(a.equals("include") || a.equals("retain")){ |
| 99 |
exclude=!Parse.parseBoolean(b); |
| 100 |
}else if(a.equals("exclude") || a.equals("remove")){ |
| 101 |
exclude=Parse.parseBoolean(b); |
| 102 |
}else if(a.equals("prefix") || a.equals("prefixmode")){ |
| 103 |
prefixmode=Parse.parseBoolean(b); |
| 104 |
}else if(a.equals("coord") || a.equals("coordinate") || a.equals("coordinates")){ |
| 105 |
coordinate=Parse.parseBoolean(b); |
| 106 |
}else if(a.equals("minlen") || a.equals("minlength")){ |
| 107 |
minLength=Parse.parseIntKMG(b); |
| 108 |
}else if(a.equals("from")){ |
| 109 |
fromPos=Parse.parseIntKMG(b); |
| 110 |
}else if(a.equals("to")){ |
| 111 |
toPos=Parse.parseIntKMG(b); |
| 112 |
}else if(a.equals("pos") || a.equals("range")){ |
| 113 |
assert(b!=null) : "Bad parameter: "+arg; |
| 114 |
String[] split2=b.split("-"); |
| 115 |
fromPos=Parse.parseIntKMG(split2[0]); |
| 116 |
toPos=Parse.parseIntKMG(split2[1]); |
| 117 |
}else if(a.equals("truncate")){ |
| 118 |
trimWhitespace=truncateHeaderSymbol=Parse.parseBoolean(b); |
| 119 |
}else if(a.equals("truncatewhitespace") || a.equals("tws")){ |
| 120 |
trimWhitespace=Parse.parseBoolean(b); |
| 121 |
}else if(a.equals("truncateheadersymbol") || a.equals("ths")){ |
| 122 |
truncateHeaderSymbol=Parse.parseBoolean(b); |
| 123 |
}else if(a.equals("ignoreafterwhitespace") || a.equals("iaw")){ |
| 124 |
|
| 125 |
}else if(parser.in1==null && i==0 && Tools.looksLikeInputStream(arg)){ |
| 126 |
parser.in1=arg; |
| 127 |
}else if(parser.parse(arg, a, b)){ |
| 128 |
|
| 129 |
}else{ |
| 130 |
outstream.println("Unknown parameter "+args[i]); |
| 131 |
assert(false) : "Unknown parameter "+args[i]; |
| 132 |
|
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
{ |
| 137 |
String[] x=names.toArray(new String[names.size()]); |
| 138 |
names.clear(); |
| 139 |
for(String s : x){ |
| 140 |
Tools.addNames(s, names, true); |
| 141 |
} |
| 142 |
} |
| 143 |
if(ignoreCase){ |
| 144 |
String[] x=names.toArray(new String[names.size()]); |
| 145 |
names.clear(); |
| 146 |
for(String s : x){ |
| 147 |
names.add(s.toLowerCase()); |
| 148 |
} |
| 149 |
} |
| 150 |
if(truncateHeaderSymbol || trimWhitespace ){ |
| 151 |
String[] x=names.toArray(new String[names.size()]); |
| 152 |
names.clear(); |
| 153 |
for(String s : x){ |
| 154 |
String s2=s; |
| 155 |
if(truncateHeaderSymbol && s.length()>1 && (s.charAt(0)=='@' || s.charAt(0)=='>')){s2=s.substring(1);} |
| 156 |
if(trimWhitespace){s2=s.trim();} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
if(s2.length()>0){ |
| 161 |
names.add(s2); |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
{ |
| 167 |
Parser.processQuality(); |
| 168 |
|
| 169 |
maxReads=parser.maxReads; |
| 170 |
|
| 171 |
overwrite=ReadStats.overwrite=parser.overwrite; |
| 172 |
append=ReadStats.append=parser.append; |
| 173 |
|
| 174 |
setInterleaved=parser.setInterleaved; |
| 175 |
|
| 176 |
in1=parser.in1; |
| 177 |
in2=parser.in2; |
| 178 |
qfin1=parser.qfin1; |
| 179 |
qfin2=parser.qfin2; |
| 180 |
|
| 181 |
out1=parser.out1; |
| 182 |
out2=parser.out2; |
| 183 |
qfout1=parser.qfout1; |
| 184 |
qfout2=parser.qfout2; |
| 185 |
|
| 186 |
extin=parser.extin; |
| 187 |
extout=parser.extout; |
| 188 |
} |
| 189 |
|
| 190 |
if(in1!=null && in2==null && in1.indexOf('#')>-1 && !new File(in1).exists()){ |
| 191 |
in2=in1.replace("#", "2"); |
| 192 |
in1=in1.replace("#", "1"); |
| 193 |
} |
| 194 |
if(out1!=null && out2==null && out1.indexOf('#')>-1){ |
| 195 |
out2=out1.replace("#", "2"); |
| 196 |
out1=out1.replace("#", "1"); |
| 197 |
} |
| 198 |
if(in2!=null){ |
| 199 |
if(FASTQ.FORCE_INTERLEAVED){outstream.println("Reset INTERLEAVED to false because paired input files were specified.");} |
| 200 |
FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false; |
| 201 |
} |
| 202 |
|
| 203 |
assert(FastaReadInputStream.settingsOK()); |
| 204 |
|
| 205 |
if(in1==null){throw new RuntimeException("Error - at least one input file is required.");} |
| 206 |
if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){ |
| 207 |
ByteFile.FORCE_MODE_BF2=true; |
| 208 |
} |
| 209 |
|
| 210 |
if(out1==null && out2!=null){throw new RuntimeException("Error - cannot define out2 without defining out1.");} |
| 211 |
|
| 212 |
if(!setInterleaved){ |
| 213 |
assert(in1!=null && (out1!=null || out2==null)) : "\nin1="+in1+"\nin2="+in2+"\nout1="+out1+"\nout2="+out2+"\n"; |
| 214 |
if(in2!=null){ |
| 215 |
FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false; |
| 216 |
outstream.println("Set INTERLEAVED to "+FASTQ.FORCE_INTERLEAVED); |
| 217 |
}else{ |
| 218 |
if(out2!=null){ |
| 219 |
FASTQ.FORCE_INTERLEAVED=true; |
| 220 |
FASTQ.TEST_INTERLEAVED=false; |
| 221 |
outstream.println("Set INTERLEAVED to "+FASTQ.FORCE_INTERLEAVED); |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
if(out1!=null && out1.equalsIgnoreCase("null")){out1=null;} |
| 227 |
if(out2!=null && out2.equalsIgnoreCase("null")){out2=null;} |
| 228 |
|
| 229 |
if(!Tools.testOutputFiles(overwrite, append, false, out1, out2)){ |
| 230 |
outstream.println((out1==null)+", "+(out2==null)+", "+out1+", "+out2); |
| 231 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out1+", "+out2+"\n"); |
| 232 |
} |
| 233 |
|
| 234 |
ffout1=FileFormat.testOutput(out1, FileFormat.FASTQ, extout, true, overwrite, append, false); |
| 235 |
ffout2=FileFormat.testOutput(out2, FileFormat.FASTQ, extout, true, overwrite, append, false); |
| 236 |
|
| 237 |
ffin1=FileFormat.testInput(in1, FileFormat.FASTQ, extin, true, true); |
| 238 |
ffin2=FileFormat.testInput(in2, FileFormat.FASTQ, extin, true, true); |
| 239 |
|
| 240 |
if(ffin1!=null && ffout1!=null && ffin1.samOrBam() && ffout1.samOrBam()){ |
| 241 |
useSharedHeader=true; |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
private static String substringUntilWhitespace(String s){ |
| 246 |
for(int i=0; i<s.length(); i++){ |
| 247 |
char c=s.charAt(i); |
| 248 |
if(c==' ' || c=='\t'){return s.substring(0, i);} |
| 249 |
} |
| 250 |
return s; |
| 251 |
} |
| 252 |
|
| 253 |
void process(Timer t){ |
| 254 |
|
| 255 |
|
| 256 |
final ConcurrentReadInputStream cris; |
| 257 |
{ |
| 258 |
cris=ConcurrentReadInputStream.getReadInputStream(maxReads, useSharedHeader, ffin1, ffin2, qfin1, qfin2); |
| 259 |
if(verbose){outstream.println("Started cris");} |
| 260 |
cris.start(); |
| 261 |
} |
| 262 |
boolean paired=cris.paired(); |
| 263 |
|
| 264 |
if(!ffin1.samOrBam()){outstream.println("Input is being processed as "+(paired ? "paired" : "unpaired"));} |
| 265 |
|
| 266 |
|
| 267 |
final ConcurrentReadOutputStream ros; |
| 268 |
if(out1!=null){ |
| 269 |
final int buff=4; |
| 270 |
|
| 271 |
if(cris.paired() && out2==null && (in1==null || !in1.contains(".sam"))){ |
| 272 |
outstream.println("Writing interleaved."); |
| 273 |
} |
| 274 |
|
| 275 |
assert(!out1.equalsIgnoreCase(in1) && !out1.equalsIgnoreCase(in1)) : "Input file and output file have same name."; |
| 276 |
assert(out2==null || (!out2.equalsIgnoreCase(in1) && !out2.equalsIgnoreCase(in2))) : "out1 and out2 have same name."; |
| 277 |
|
| 278 |
ros=ConcurrentReadOutputStream.getStream(ffout1, ffout2, qfout1, qfout2, buff, null, useSharedHeader); |
| 279 |
ros.start(); |
| 280 |
}else{ros=null;} |
| 281 |
|
| 282 |
long readsProcessed=0; |
| 283 |
long basesProcessed=0; |
| 284 |
|
| 285 |
long readsOut=0; |
| 286 |
long basesOut=0; |
| 287 |
|
| 288 |
final IlluminaHeaderParser2 ihp=new IlluminaHeaderParser2(); |
| 289 |
final ByteBuilder bb=new ByteBuilder(); |
| 290 |
{ |
| 291 |
|
| 292 |
ListNum<Read> ln=cris.nextList(); |
| 293 |
ArrayList<Read> reads=(ln!=null ? ln.list : null); |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
if(reads!=null && !reads.isEmpty()){ |
| 298 |
Read r=reads.get(0); |
| 299 |
assert((ffin1==null || ffin1.samOrBam()) || (r.mate!=null)==cris.paired()); |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
while(ln!=null && reads!=null && reads.size()>0){ |
| 305 |
|
| 306 |
ArrayList<Read> retain=new ArrayList<Read>(reads.size()); |
| 307 |
for(int idx=0; idx<reads.size(); idx++){ |
| 308 |
final Read r1=reads.get(idx); |
| 309 |
final Read r2=r1.mate; |
| 310 |
|
| 311 |
final int initialLength1=r1.length(); |
| 312 |
final int initialLength2=(r1.mateLength()); |
| 313 |
readsProcessed+=r1.pairCount(); |
| 314 |
basesProcessed+=initialLength1+initialLength2; |
| 315 |
|
| 316 |
final String header; |
| 317 |
if(coordinate) { |
| 318 |
ihp.parse(r1.id); |
| 319 |
header=ihp.appendCoordinates(bb.clear()).toString(); |
| 320 |
}else { |
| 321 |
String temp=(ignoreCase ? r1.id.toLowerCase() : r1.id); |
| 322 |
temp=trimWhitespace ? temp.trim() : temp; |
| 323 |
|
| 324 |
header=temp; |
| 325 |
} |
| 326 |
|
| 327 |
String prefix=null; |
| 328 |
if(!coordinate){ |
| 329 |
for(int x=1; x<header.length(); x++){ |
| 330 |
char prev=x>=2 ? header.charAt(x-2) : 'X'; |
| 331 |
char c=header.charAt(x-1); |
| 332 |
char next=header.charAt(x); |
| 333 |
if(Character.isWhitespace(c) || (c=='/' && (next=='1' || next=='2'))){ |
| 334 |
prefix=header.substring(0, x).trim(); |
| 335 |
break; |
| 336 |
}else if(Character.isWhitespace(prev) && (c=='1' || c=='2') && next==':'){ |
| 337 |
prefix=header.substring(0, x).trim(); |
| 338 |
break; |
| 339 |
} |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
boolean keepThisRead=(initialLength1>=minLength || initialLength2>=minLength); |
| 344 |
boolean match=false; |
| 345 |
if(keepThisRead){ |
| 346 |
match=(names.contains(header) || (prefix!=null && names.contains(prefix))); |
| 347 |
if(!match && (nameSubstringOfHeader || headerSubstringOfName)){ |
| 348 |
for(String name : names){ |
| 349 |
if((headerSubstringOfName && name.contains(header)) || (nameSubstringOfHeader && header.contains(name))){match=true;} |
| 350 |
else if(prefix!=null && ((headerSubstringOfName && name.contains(prefix)) || (nameSubstringOfHeader && prefix.contains(name)))){match=true;} |
| 351 |
} |
| 352 |
}else if(!match && prefixmode){ |
| 353 |
for(String name : names){ |
| 354 |
if(header.startsWith(name)){match=true;} |
| 355 |
} |
| 356 |
} |
| 357 |
keepThisRead=(match!=exclude); |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
if(keepThisRead){ |
| 363 |
if(fromPos>=0){ |
| 364 |
TrimRead.trimToPosition(r1, fromPos, toPos, 1); |
| 365 |
} |
| 366 |
retain.add(r1); |
| 367 |
readsOut+=r1.pairCount(); |
| 368 |
basesOut+=r1.pairLength(); |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
final ArrayList<Read> listOut=retain; |
| 373 |
|
| 374 |
if(ros!=null){ros.add(listOut, ln.id);} |
| 375 |
|
| 376 |
cris.returnList(ln); |
| 377 |
ln=cris.nextList(); |
| 378 |
reads=(ln!=null ? ln.list : null); |
| 379 |
} |
| 380 |
if(ln!=null){ |
| 381 |
cris.returnList(ln.id, ln.list==null || ln.list.isEmpty()); |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
errorState|=ReadStats.writeAll(); |
| 386 |
|
| 387 |
errorState|=ReadWrite.closeStreams(cris, ros); |
| 388 |
|
| 389 |
t.stop(); |
| 390 |
outstream.println(Tools.timeReadsBasesProcessed(t, readsProcessed, basesProcessed, 8)); |
| 391 |
outstream.println("Reads Out: "+readsOut); |
| 392 |
outstream.println("Bases Out: "+basesOut); |
| 393 |
|
| 394 |
if(errorState){ |
| 395 |
throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt."); |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
private String in1=null; |
| 402 |
private String in2=null; |
| 403 |
|
| 404 |
private String qfin1=null; |
| 405 |
private String qfin2=null; |
| 406 |
|
| 407 |
private String out1=null; |
| 408 |
private String out2=null; |
| 409 |
|
| 410 |
private String qfout1=null; |
| 411 |
private String qfout2=null; |
| 412 |
|
| 413 |
private String extin=null; |
| 414 |
private String extout=null; |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
private long maxReads=-1; |
| 419 |
private boolean exclude=true; |
| 420 |
private boolean prefixmode=false; |
| 421 |
private boolean nameSubstringOfHeader=false; |
| 422 |
private boolean headerSubstringOfName=false; |
| 423 |
private boolean ignoreCase=true; |
| 424 |
private boolean truncateHeaderSymbol=false; |
| 425 |
private boolean trimWhitespace=false; |
| 426 |
private boolean coordinate=false; |
| 427 |
|
| 428 |
|
| 429 |
private int minLength=0; |
| 430 |
|
| 431 |
private int fromPos=-1; |
| 432 |
private int toPos=-1; |
| 433 |
|
| 434 |
private LinkedHashSet<String> names=new LinkedHashSet<String>(); |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
private final FileFormat ffin1; |
| 439 |
private final FileFormat ffin2; |
| 440 |
|
| 441 |
private final FileFormat ffout1; |
| 442 |
private final FileFormat ffout2; |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
private PrintStream outstream=System.err; |
| 448 |
public static boolean verbose=false; |
| 449 |
public boolean errorState=false; |
| 450 |
private boolean overwrite=true; |
| 451 |
private boolean append=false; |
| 452 |
private boolean useSharedHeader=false; |
| 453 |
|
| 454 |
} |