| 1 |
package driver; |
| 2 |
|
| 3 |
import java.io.PrintStream; |
| 4 |
import java.util.ArrayList; |
| 5 |
import java.util.HashSet; |
| 6 |
|
| 7 |
import fileIO.ByteFile; |
| 8 |
import fileIO.FileFormat; |
| 9 |
import fileIO.ReadWrite; |
| 10 |
import shared.Parse; |
| 11 |
import shared.Parser; |
| 12 |
import shared.PreParser; |
| 13 |
import shared.Shared; |
| 14 |
import shared.Timer; |
| 15 |
import shared.Tools; |
| 16 |
import stream.ConcurrentReadInputStream; |
| 17 |
import stream.ConcurrentReadOutputStream; |
| 18 |
import stream.FASTQ; |
| 19 |
import stream.FastaReadInputStream; |
| 20 |
import stream.Read; |
| 21 |
import structures.ListNum; |
| 22 |
import tracker.ReadStats; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public class ReduceSilva { |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public static void main(String[] args){ |
| 40 |
Timer t=new Timer(); |
| 41 |
ReduceSilva x=new ReduceSilva(args); |
| 42 |
x.process(t); |
| 43 |
|
| 44 |
|
| 45 |
Shared.closeStream(x.outstream); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public ReduceSilva(String[] args){ |
| 53 |
|
| 54 |
{ |
| 55 |
PreParser pp=new PreParser(args, getClass(), false); |
| 56 |
args=pp.args; |
| 57 |
outstream=pp.outstream; |
| 58 |
} |
| 59 |
|
| 60 |
Shared.capBuffers(4); |
| 61 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 62 |
ReadWrite.setZipThreads(Shared.threads()); |
| 63 |
FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false; |
| 64 |
|
| 65 |
Parser parser=new Parser(); |
| 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(parser.parse(arg, a, b)){ |
| 73 |
|
| 74 |
}else if(a.equals("verbose")){ |
| 75 |
verbose=Parse.parseBoolean(b); |
| 76 |
}else if(a.equals("column")){ |
| 77 |
column=Integer.parseInt(b); |
| 78 |
}else if(a.equals("parse_flag_goes_here")){ |
| 79 |
|
| 80 |
}else{ |
| 81 |
outstream.println("Unknown parameter "+args[i]); |
| 82 |
assert(false) : "Unknown parameter "+args[i]; |
| 83 |
|
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
{ |
| 88 |
Parser.processQuality(); |
| 89 |
|
| 90 |
maxReads=parser.maxReads; |
| 91 |
|
| 92 |
overwrite=ReadStats.overwrite=parser.overwrite; |
| 93 |
append=ReadStats.append=parser.append; |
| 94 |
|
| 95 |
in1=parser.in1; |
| 96 |
|
| 97 |
out1=parser.out1; |
| 98 |
|
| 99 |
extin=parser.extin; |
| 100 |
extout=parser.extout; |
| 101 |
} |
| 102 |
|
| 103 |
assert(FastaReadInputStream.settingsOK()); |
| 104 |
|
| 105 |
if(in1==null){throw new RuntimeException("Error - at least one input file is required.");} |
| 106 |
if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){ |
| 107 |
ByteFile.FORCE_MODE_BF2=true; |
| 108 |
} |
| 109 |
|
| 110 |
if(out1!=null && out1.equalsIgnoreCase("null")){out1=null;} |
| 111 |
|
| 112 |
if(!Tools.testOutputFiles(overwrite, append, false, out1)){ |
| 113 |
outstream.println((out1==null)+", "+out1); |
| 114 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out1+"\n"); |
| 115 |
} |
| 116 |
|
| 117 |
ffout1=FileFormat.testOutput(out1, FileFormat.FASTQ, extout, true, overwrite, append, false); |
| 118 |
|
| 119 |
ffin1=FileFormat.testInput(in1, FileFormat.FASTQ, extin, true, true); |
| 120 |
} |
| 121 |
|
| 122 |
public boolean parseArgument(String arg, String a, String b){ |
| 123 |
if(a.equals("reads") || a.equals("maxreads")){ |
| 124 |
maxReads=Parse.parseKMG(b); |
| 125 |
return true; |
| 126 |
}else if(a.equals("some_argument")){ |
| 127 |
maxReads=Parse.parseKMG(b); |
| 128 |
return true; |
| 129 |
} |
| 130 |
return false; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
void process(Timer t){ |
| 140 |
|
| 141 |
final ConcurrentReadInputStream cris; |
| 142 |
{ |
| 143 |
cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffin1, null); |
| 144 |
cris.start(); |
| 145 |
if(verbose){outstream.println("Started cris");} |
| 146 |
} |
| 147 |
boolean paired=cris.paired(); |
| 148 |
if(!ffin1.samOrBam()){outstream.println("Input is being processed as "+(paired ? "paired" : "unpaired"));} |
| 149 |
|
| 150 |
final ConcurrentReadOutputStream ros; |
| 151 |
if(out1!=null){ |
| 152 |
final int buff=4; |
| 153 |
|
| 154 |
assert(!out1.equalsIgnoreCase(in1) && !out1.equalsIgnoreCase(in1)) : "Input file and output file have same name."; |
| 155 |
|
| 156 |
ros=ConcurrentReadOutputStream.getStream(ffout1, null, buff, null, false); |
| 157 |
ros.start(); |
| 158 |
}else{ros=null;} |
| 159 |
|
| 160 |
readsProcessed=0; |
| 161 |
basesProcessed=0; |
| 162 |
|
| 163 |
readsOut=0; |
| 164 |
basesOut=0; |
| 165 |
|
| 166 |
|
| 167 |
processInner(cris, ros); |
| 168 |
|
| 169 |
ReadWrite.closeStreams(cris, ros); |
| 170 |
if(verbose){outstream.println("Finished.");} |
| 171 |
|
| 172 |
errorState|=ReadStats.writeAll(); |
| 173 |
errorState|=ReadWrite.closeStreams(cris, ros); |
| 174 |
|
| 175 |
t.stop(); |
| 176 |
|
| 177 |
double rpnano=readsProcessed/(double)(t.elapsed); |
| 178 |
double bpnano=basesProcessed/(double)(t.elapsed); |
| 179 |
|
| 180 |
String rpstring=""+readsProcessed; |
| 181 |
String bpstring=""+basesProcessed; |
| 182 |
String rostring=""+readsOut; |
| 183 |
String bostring=""+basesOut; |
| 184 |
|
| 185 |
final int digits=10; |
| 186 |
while(rpstring.length()<digits){rpstring=" "+rpstring;} |
| 187 |
while(bpstring.length()<digits){bpstring=" "+bpstring;} |
| 188 |
while(rostring.length()<digits){rostring=" "+rostring;} |
| 189 |
while(bostring.length()<digits){bostring=" "+bostring;} |
| 190 |
|
| 191 |
outstream.println("Time: \t"+t); |
| 192 |
outstream.println("Reads Processed: "+rpstring+" \t"+Tools.format("%.2fk reads/sec", rpnano*1000000)); |
| 193 |
outstream.println("Bases Processed: "+bpstring+" \t"+Tools.format("%.2fm bases/sec", bpnano*1000)); |
| 194 |
outstream.println(); |
| 195 |
outstream.println("Reads Out: "+rostring+" \t"+Tools.format("%.2f%%", readsOut*100.0/readsProcessed)); |
| 196 |
outstream.println("Bases Out: "+bostring+" \t"+Tools.format("%.2f%%", basesOut*100.0/basesProcessed)); |
| 197 |
|
| 198 |
if(errorState){ |
| 199 |
throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt."); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
void processInner(final ConcurrentReadInputStream cris, final ConcurrentReadOutputStream ros){ |
| 205 |
|
| 206 |
readsProcessed=0; |
| 207 |
basesProcessed=0; |
| 208 |
|
| 209 |
{ |
| 210 |
|
| 211 |
ListNum<Read> ln=cris.nextList(); |
| 212 |
ArrayList<Read> reads=(ln!=null ? ln.list : null); |
| 213 |
|
| 214 |
if(reads!=null && !reads.isEmpty()){ |
| 215 |
Read r=reads.get(0); |
| 216 |
assert((ffin1==null || ffin1.samOrBam()) || (r.mate!=null)==cris.paired()); |
| 217 |
} |
| 218 |
|
| 219 |
while(ln!=null && reads!=null && reads.size()>0){ |
| 220 |
if(verbose){outstream.println("Fetched "+reads.size()+" reads.");} |
| 221 |
|
| 222 |
for(int idx=0; idx<reads.size(); idx++){ |
| 223 |
final Read r1=reads.get(idx); |
| 224 |
|
| 225 |
final int initialLength1=r1.length(); |
| 226 |
|
| 227 |
{ |
| 228 |
readsProcessed++; |
| 229 |
basesProcessed+=initialLength1; |
| 230 |
} |
| 231 |
|
| 232 |
boolean keep=processRead(r1); |
| 233 |
if(keep){ |
| 234 |
readsOut++; |
| 235 |
basesOut+=initialLength1; |
| 236 |
}else{ |
| 237 |
reads.set(idx, null); |
| 238 |
} |
| 239 |
|
| 240 |
} |
| 241 |
|
| 242 |
if(ros!=null){ros.add(reads, ln.id);} |
| 243 |
|
| 244 |
cris.returnList(ln); |
| 245 |
if(verbose){outstream.println("Returned a list.");} |
| 246 |
ln=cris.nextList(); |
| 247 |
reads=(ln!=null ? ln.list : null); |
| 248 |
} |
| 249 |
if(ln!=null){ |
| 250 |
cris.returnList(ln.id, ln.list==null || ln.list.isEmpty()); |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
boolean processRead(final Read r1){ |
| 265 |
String[] split=r1.id.split(";"); |
| 266 |
if(split.length<=column){return true;} |
| 267 |
String taxa=split[split.length-column-1]; |
| 268 |
boolean present=table.contains(taxa); |
| 269 |
if(present){return false;} |
| 270 |
table.add(taxa); |
| 271 |
return true; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
private HashSet<String> table=new HashSet<String>(); |
| 279 |
|
| 280 |
private String in1=null; |
| 281 |
|
| 282 |
private String out1=null; |
| 283 |
|
| 284 |
private String extin=null; |
| 285 |
private String extout=null; |
| 286 |
|
| 287 |
private int column=1; |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
protected long readsProcessed=0; |
| 292 |
protected long basesProcessed=0; |
| 293 |
|
| 294 |
protected long readsOut=0; |
| 295 |
protected long basesOut=0; |
| 296 |
|
| 297 |
private long maxReads=-1; |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
private final FileFormat ffin1; |
| 304 |
|
| 305 |
private final FileFormat ffout1; |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
private PrintStream outstream=System.err; |
| 312 |
public static boolean verbose=false; |
| 313 |
public boolean errorState=false; |
| 314 |
private boolean overwrite=true; |
| 315 |
private boolean append=false; |
| 316 |
|
| 317 |
} |