| 1 |
package tax; |
| 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.FileFormat; |
| 10 |
import fileIO.ReadWrite; |
| 11 |
import shared.Parse; |
| 12 |
import shared.Parser; |
| 13 |
import shared.PreParser; |
| 14 |
import shared.Shared; |
| 15 |
import shared.Timer; |
| 16 |
import shared.Tools; |
| 17 |
import stream.ConcurrentReadInputStream; |
| 18 |
import stream.ConcurrentReadOutputStream; |
| 19 |
import stream.FASTQ; |
| 20 |
import stream.FastaReadInputStream; |
| 21 |
import stream.Read; |
| 22 |
import structures.ListNum; |
| 23 |
import tracker.ReadStats; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public class SplitByTaxa { |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public static void main(String[] args){ |
| 45 |
Timer t=new Timer(); |
| 46 |
SplitByTaxa x=new SplitByTaxa(args); |
| 47 |
x.process(t); |
| 48 |
|
| 49 |
|
| 50 |
Shared.closeStream(x.outstream); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public SplitByTaxa(String[] args){ |
| 58 |
|
| 59 |
{ |
| 60 |
PreParser pp=new PreParser(args, getClass(), false); |
| 61 |
args=pp.args; |
| 62 |
outstream=pp.outstream; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
Shared.capBuffers(4); |
| 67 |
ReadWrite.USE_UNPIGZ=true; |
| 68 |
ReadWrite.USE_PIGZ=false; |
| 69 |
ReadWrite.USE_GZIP=false; |
| 70 |
ReadWrite.setZipThreads(Shared.threads()); |
| 71 |
|
| 72 |
boolean setInterleaved=false; |
| 73 |
String giTableFile=null; |
| 74 |
String taxTreeFile=null; |
| 75 |
|
| 76 |
|
| 77 |
Parser parser=new Parser(); |
| 78 |
|
| 79 |
|
| 80 |
for(int i=0; i<args.length; i++){ |
| 81 |
String arg=args[i]; |
| 82 |
|
| 83 |
|
| 84 |
String[] split=arg.split("="); |
| 85 |
String a=split[0].toLowerCase(); |
| 86 |
String b=split.length>1 ? split[1] : null; |
| 87 |
|
| 88 |
if(parser.parse(arg, a, b)){ |
| 89 |
|
| 90 |
}else if(a.equals("verbose")){ |
| 91 |
verbose=Parse.parseBoolean(b); |
| 92 |
}else if(a.equals("taxlevel") || a.equals("tl") || a.equals("level") || a.equals("lv")){ |
| 93 |
taxLevelE=TaxTree.parseLevelExtended(b); |
| 94 |
}else if(a.equals("table") || a.equals("gi") || a.equals("gitable")){ |
| 95 |
giTableFile=b; |
| 96 |
}else if(a.equals("tree") || a.equals("taxtree")){ |
| 97 |
taxTreeFile=b; |
| 98 |
}else{ |
| 99 |
outstream.println("Unknown parameter "+args[i]); |
| 100 |
assert(false) : "Unknown parameter "+args[i]; |
| 101 |
|
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
if("auto".equalsIgnoreCase(taxTreeFile)){taxTreeFile=TaxTree.defaultTreeFile();} |
| 106 |
if("auto".equalsIgnoreCase(giTableFile)){giTableFile=TaxTree.defaultTableFile();} |
| 107 |
|
| 108 |
{ |
| 109 |
Parser.processQuality(); |
| 110 |
|
| 111 |
maxReads=parser.maxReads; |
| 112 |
|
| 113 |
overwrite=ReadStats.overwrite=parser.overwrite; |
| 114 |
append=ReadStats.append=parser.append; |
| 115 |
setInterleaved=parser.setInterleaved; |
| 116 |
|
| 117 |
in1=parser.in1; |
| 118 |
in2=parser.in2; |
| 119 |
|
| 120 |
out1=parser.out1; |
| 121 |
out2=parser.out2; |
| 122 |
|
| 123 |
extin=parser.extin; |
| 124 |
extout=parser.extout; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
if(in1!=null && in2==null && in1.indexOf('#')>-1 && !new File(in1).exists()){ |
| 129 |
in2=in1.replace("#", "2"); |
| 130 |
in1=in1.replace("#", "1"); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
if(out1!=null && out2==null && out1.indexOf('#')>-1){ |
| 135 |
out2=out1.replace("#", "2"); |
| 136 |
out1=out1.replace("#", "1"); |
| 137 |
} |
| 138 |
|
| 139 |
assert(out1==null || out1.contains("%")) : "Output filename must contain % symbol."; |
| 140 |
assert(out2==null || out2.contains("%")) : "Output filename must contain % symbol."; |
| 141 |
assert(taxTreeFile!=null) : "This requires a taxtree file. On NERSC systems, set tree=auto."; |
| 142 |
|
| 143 |
|
| 144 |
if(in2!=null){ |
| 145 |
if(FASTQ.FORCE_INTERLEAVED){outstream.println("Reset INTERLEAVED to false because paired input files were specified.");} |
| 146 |
FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false; |
| 147 |
} |
| 148 |
|
| 149 |
assert(FastaReadInputStream.settingsOK()); |
| 150 |
|
| 151 |
|
| 152 |
if(in1==null){throw new RuntimeException("Error - at least one input file is required.");} |
| 153 |
|
| 154 |
|
| 155 |
if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){ |
| 156 |
ByteFile.FORCE_MODE_BF2=true; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
if(out1==null && out2!=null){throw new RuntimeException("Error - cannot define out2 without defining out1.");} |
| 161 |
|
| 162 |
|
| 163 |
if(!setInterleaved){ |
| 164 |
assert(in1!=null && (out1!=null || out2==null)) : "\nin1="+in1+"\nin2="+in2+"\nout1="+out1+"\nout2="+out2+"\n"; |
| 165 |
if(in2!=null){ |
| 166 |
FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false; |
| 167 |
outstream.println("Set INTERLEAVED to "+FASTQ.FORCE_INTERLEAVED); |
| 168 |
}else{ |
| 169 |
if(out2!=null){ |
| 170 |
FASTQ.FORCE_INTERLEAVED=true; |
| 171 |
FASTQ.TEST_INTERLEAVED=false; |
| 172 |
outstream.println("Set INTERLEAVED to "+FASTQ.FORCE_INTERLEAVED); |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
if(!Tools.testOutputFiles(overwrite, append, false, out1, out2)){ |
| 179 |
outstream.println((out1==null)+", "+(out2==null)+", "+out1+", "+out2); |
| 180 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out1+", "+out2+"\n"); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
if(!Tools.testInputFiles(false, true, in1, in2)){ |
| 185 |
throw new RuntimeException("\nCan't read some input files.\n"); |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
if(!Tools.testForDuplicateFiles(true, in1, in2, out1, out2)){ |
| 190 |
throw new RuntimeException("\nSome file names were specified multiple times.\n"); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
ffin1=FileFormat.testInput(in1, FileFormat.FASTQ, extin, true, true); |
| 195 |
ffin2=FileFormat.testInput(in2, FileFormat.FASTQ, extin, true, true); |
| 196 |
|
| 197 |
TaxFilter.loadGiTable(giTableFile); |
| 198 |
tree=TaxFilter.loadTree(taxTreeFile); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
public void process(Timer t){ |
| 207 |
|
| 208 |
|
| 209 |
final ConcurrentReadInputStream cris; |
| 210 |
{ |
| 211 |
cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffin1, ffin2, null, null); |
| 212 |
cris.start(); |
| 213 |
if(verbose){outstream.println("Started cris");} |
| 214 |
} |
| 215 |
boolean paired=cris.paired(); |
| 216 |
if(!ffin1.samOrBam()){outstream.println("Input is being processed as "+(paired ? "paired" : "unpaired"));} |
| 217 |
|
| 218 |
|
| 219 |
readsProcessed=0; |
| 220 |
basesProcessed=0; |
| 221 |
|
| 222 |
final HashMap<String, ConcurrentReadOutputStream> map=new HashMap<String, ConcurrentReadOutputStream>(); |
| 223 |
|
| 224 |
|
| 225 |
processInner(cris, map); |
| 226 |
|
| 227 |
if(verbose){outstream.println("Finished; closing streams.");} |
| 228 |
|
| 229 |
|
| 230 |
errorState|=ReadStats.writeAll(); |
| 231 |
|
| 232 |
errorState|=ReadWrite.closeStream(cris); |
| 233 |
|
| 234 |
for(ConcurrentReadOutputStream ros : map.values()){ |
| 235 |
ReadWrite.closeStream(ros); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
t.stop(); |
| 240 |
outstream.println("Reads In: \t"+readsProcessed+" reads \t"+basesProcessed+" bases"); |
| 241 |
outstream.println(); |
| 242 |
outstream.println(Tools.timeReadsBasesProcessed(t, readsProcessed, basesProcessed, 8)); |
| 243 |
|
| 244 |
|
| 245 |
if(errorState){ |
| 246 |
throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt."); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
void processInner(final ConcurrentReadInputStream cris, HashMap<String, ConcurrentReadOutputStream> map){ |
| 252 |
|
| 253 |
{ |
| 254 |
|
| 255 |
ListNum<Read> ln=cris.nextList(); |
| 256 |
|
| 257 |
ArrayList<Read> reads=(ln!=null ? ln.list : null); |
| 258 |
|
| 259 |
|
| 260 |
if(reads!=null && !reads.isEmpty()){ |
| 261 |
Read r=reads.get(0); |
| 262 |
assert((ffin1==null || ffin1.samOrBam()) || (r.mate!=null)==cris.paired()); |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
while(ln!=null && reads!=null && reads.size()>0){ |
| 267 |
if(verbose){outstream.println("Fetched "+reads.size()+" reads.");} |
| 268 |
|
| 269 |
|
| 270 |
for(int idx=0; idx<reads.size(); idx++){ |
| 271 |
final Read r1=reads.get(idx); |
| 272 |
|
| 273 |
|
| 274 |
final int initialLength1=r1.length(); |
| 275 |
final int initialLength2=(r1.mateLength()); |
| 276 |
|
| 277 |
|
| 278 |
readsProcessed+=r1.pairCount(); |
| 279 |
basesProcessed+=initialLength1+initialLength2; |
| 280 |
|
| 281 |
TaxNode tn=tree.parseNodeFromHeader(r1.id, true); |
| 282 |
if(tn==null){tn=tree.getNodeByName(r1.id);} |
| 283 |
if(tn==null){tn=unknown;} |
| 284 |
while(tn.levelExtended<taxLevelE && tn.id!=tn.pid){tn=tree.getNode(tn.pid);} |
| 285 |
|
| 286 |
if(out1!=null){ |
| 287 |
ConcurrentReadOutputStream ros=map.get(tn.name); |
| 288 |
if(ros==null){ |
| 289 |
final int buff=4; |
| 290 |
FileFormat ffout1=null, ffout2=null; |
| 291 |
ffout1=FileFormat.testOutput(out1.replaceFirst("%", tn.name.replaceAll("\\s+", "_").replaceAll("[/\\\\]", "")), FileFormat.FASTQ, extout, false, overwrite, append, false); |
| 292 |
if(out2!=null){ffout2=FileFormat.testOutput(out2.replaceFirst("%", tn.name.replaceAll("\\s+", "_").replaceAll("[/\\\\]", "")), FileFormat.FASTQ, extout, false, overwrite, append, false);} |
| 293 |
ros=ConcurrentReadOutputStream.getStream(ffout1, ffout2, null, null, buff, null, false); |
| 294 |
ros.start(); |
| 295 |
map.put(tn.name, ros); |
| 296 |
} |
| 297 |
ArrayList<Read> temp=new ArrayList<Read>(1); |
| 298 |
temp.add(r1); |
| 299 |
ros.add(temp, 0); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
cris.returnList(ln); |
| 305 |
if(verbose){outstream.println("Returned a list.");} |
| 306 |
|
| 307 |
|
| 308 |
ln=cris.nextList(); |
| 309 |
reads=(ln!=null ? ln.list : null); |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
if(ln!=null){ |
| 314 |
cris.returnList(ln.id, ln.list==null || ln.list.isEmpty()); |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
private String in1=null; |
| 332 |
|
| 333 |
private String in2=null; |
| 334 |
|
| 335 |
|
| 336 |
private String out1=null; |
| 337 |
|
| 338 |
private String out2=null; |
| 339 |
|
| 340 |
|
| 341 |
private String extin=null; |
| 342 |
|
| 343 |
private String extout=null; |
| 344 |
|
| 345 |
|
| 346 |
private int taxLevelE=TaxTree.stringToLevelExtended("phylum"); |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
protected long readsProcessed=0; |
| 352 |
|
| 353 |
protected long basesProcessed=0; |
| 354 |
|
| 355 |
|
| 356 |
private long maxReads=-1; |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
private final FileFormat ffin1; |
| 364 |
|
| 365 |
private final FileFormat ffin2; |
| 366 |
|
| 367 |
private final TaxTree tree; |
| 368 |
|
| 369 |
private final TaxNode unknown=new TaxNode(-99, -99, TaxTree.LIFE, TaxTree.LIFE_E, "UNKNOWN"); |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
private PrintStream outstream=System.err; |
| 377 |
|
| 378 |
public static boolean verbose=false; |
| 379 |
|
| 380 |
public boolean errorState=false; |
| 381 |
|
| 382 |
private boolean overwrite=true; |
| 383 |
|
| 384 |
private boolean append=false; |
| 385 |
|
| 386 |
private final boolean ordered=false; |
| 387 |
|
| 388 |
} |