| 1 |
package jgi; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.PrintStream; |
| 5 |
import java.util.ArrayList; |
| 6 |
|
| 7 |
import align2.QualityTools; |
| 8 |
import dna.AminoAcid; |
| 9 |
import fileIO.ByteFile; |
| 10 |
import fileIO.ByteFile1; |
| 11 |
import fileIO.ByteFile2; |
| 12 |
import fileIO.FileFormat; |
| 13 |
import fileIO.ReadWrite; |
| 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 stream.ConcurrentGenericReadInputStream; |
| 21 |
import stream.ConcurrentReadInputStream; |
| 22 |
import stream.ConcurrentReadOutputStream; |
| 23 |
import stream.FASTQ; |
| 24 |
import stream.FastaReadInputStream; |
| 25 |
import stream.Read; |
| 26 |
import structures.ListNum; |
| 27 |
import tracker.ReadStats; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public class AddAdapters { |
| 35 |
|
| 36 |
public static void main(String[] args){ |
| 37 |
Timer t=new Timer(); |
| 38 |
AddAdapters x=new AddAdapters(args); |
| 39 |
if(x.writeMode){ |
| 40 |
x.write(t); |
| 41 |
}else{ |
| 42 |
x.read(t); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
Shared.closeStream(x.outstream); |
| 47 |
} |
| 48 |
|
| 49 |
public AddAdapters(String[] args){ |
| 50 |
|
| 51 |
{ |
| 52 |
PreParser pp=new PreParser(args, getClass(), false); |
| 53 |
args=pp.args; |
| 54 |
outstream=pp.outstream; |
| 55 |
} |
| 56 |
|
| 57 |
Parser parser=new Parser(); |
| 58 |
|
| 59 |
|
| 60 |
Shared.capBuffers(4); |
| 61 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 62 |
ReadWrite.setZipThreads(Shared.threads()); |
| 63 |
FASTQ.PARSE_CUSTOM=true; |
| 64 |
|
| 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(Parser.parseCommonStatic(arg, a, b)){ |
| 73 |
|
| 74 |
}else if(Parser.parseZip(arg, a, b)){ |
| 75 |
|
| 76 |
}else if(Parser.parseQuality(arg, a, b)){ |
| 77 |
|
| 78 |
}else if(Parser.parseFasta(arg, a, b)){ |
| 79 |
|
| 80 |
}else if(parser.parseInterleaved(arg, a, b)){ |
| 81 |
|
| 82 |
}else if(a.equals("verbose")){ |
| 83 |
verbose=Parse.parseBoolean(b); |
| 84 |
ByteFile1.verbose=verbose; |
| 85 |
ByteFile2.verbose=verbose; |
| 86 |
stream.FastaReadInputStream.verbose=verbose; |
| 87 |
ConcurrentGenericReadInputStream.verbose=verbose; |
| 88 |
stream.FastqReadInputStream.verbose=verbose; |
| 89 |
ReadWrite.verbose=verbose; |
| 90 |
}else if(a.equals("reads") || a.equals("maxreads")){ |
| 91 |
maxReads=Parse.parseKMG(b); |
| 92 |
}else if(a.equals("t") || a.equals("threads")){ |
| 93 |
Shared.setThreads(b); |
| 94 |
}else if(a.equals("in") || a.equals("input") || a.equals("in1") || a.equals("input1")){ |
| 95 |
in1=b; |
| 96 |
}else if(a.equals("in2") || a.equals("input2")){ |
| 97 |
in2=b; |
| 98 |
}else if(a.equals("out") || a.equals("output") || a.equals("out1") || a.equals("output1")){ |
| 99 |
out1=b; |
| 100 |
}else if(a.equals("out2") || a.equals("output2")){ |
| 101 |
out2=b; |
| 102 |
}else if(a.equals("extin")){ |
| 103 |
extin=b; |
| 104 |
}else if(a.equals("extout")){ |
| 105 |
extout=b; |
| 106 |
}else if(a.equals("adapter") || a.equals("adapters") || a.equals("ref")){ |
| 107 |
adapterFile=b; |
| 108 |
}else if(a.equals("literal") || a.equals("literals")){ |
| 109 |
literals=(b==null ? null : b.split(",")); |
| 110 |
}else if(a.equals("rate") || a.equals("prob")){ |
| 111 |
adapterProb=Float.parseFloat(b); |
| 112 |
}else if(a.equals("minlength") || a.equals("minlen") || a.equals("ml")){ |
| 113 |
minlen=Integer.parseInt(b); |
| 114 |
}else if(a.equals("3'") || a.equalsIgnoreCase("3prime") || a.equalsIgnoreCase("3-prime") || a.equalsIgnoreCase("right") || a.equalsIgnoreCase("r")){ |
| 115 |
right=Parse.parseBoolean(b); |
| 116 |
}else if(a.equals("5'") || a.equalsIgnoreCase("5prime") || a.equalsIgnoreCase("5-prime") || a.equalsIgnoreCase("left") || a.equalsIgnoreCase("l")){ |
| 117 |
right=!Parse.parseBoolean(b); |
| 118 |
}else if(a.equals("end")){ |
| 119 |
assert(b!=null) : "Bad parameter: "+arg; |
| 120 |
if(b.equals("3'") || b.equalsIgnoreCase("3prime") || b.equalsIgnoreCase("3-prime") || b.equalsIgnoreCase("right") || a.equalsIgnoreCase("r")){ |
| 121 |
right=true; |
| 122 |
}else if(b.equals("5'") || b.equalsIgnoreCase("5prime") || b.equalsIgnoreCase("5-prime") || b.equalsIgnoreCase("left") || a.equalsIgnoreCase("l")){ |
| 123 |
right=true; |
| 124 |
} |
| 125 |
}else if(a.equals("addslash")){ |
| 126 |
addslash=Parse.parseBoolean(b); |
| 127 |
}else if(a.equals("adderrors")){ |
| 128 |
adderrors=Parse.parseBoolean(b); |
| 129 |
}else if(a.equals("addreversecomplement") || a.equals("arc")){ |
| 130 |
addRC=Parse.parseBoolean(b); |
| 131 |
}else if(a.equals("addpaired")){ |
| 132 |
addPaired=Parse.parseBoolean(b); |
| 133 |
}else if(a.equals("append") || a.equals("app")){ |
| 134 |
append=ReadStats.append=Parse.parseBoolean(b); |
| 135 |
}else if(a.equals("overwrite") || a.equals("ow")){ |
| 136 |
overwrite=Parse.parseBoolean(b); |
| 137 |
}else if(a.equals("write")){ |
| 138 |
writeMode=Parse.parseBoolean(b); |
| 139 |
}else if(a.equals("grade")){ |
| 140 |
writeMode=!Parse.parseBoolean(b); |
| 141 |
}else if(a.equals("mode")){ |
| 142 |
if("grade".equalsIgnoreCase(b) || "read".equalsIgnoreCase(b)){ |
| 143 |
writeMode=false; |
| 144 |
}else if("generate".equalsIgnoreCase(b) || "write".equalsIgnoreCase(b) || "add".equalsIgnoreCase(b)){ |
| 145 |
writeMode=true; |
| 146 |
}else{ |
| 147 |
throw new RuntimeException("Unknown mode "+b); |
| 148 |
} |
| 149 |
}else if(in1==null && i==0 && Tools.looksLikeInputStream(arg)){ |
| 150 |
in1=arg; |
| 151 |
}else{ |
| 152 |
System.err.println("Unknown parameter "+args[i]); |
| 153 |
assert(false) : "Unknown parameter "+args[i]; |
| 154 |
|
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
{ |
| 159 |
Parser.processQuality(); |
| 160 |
} |
| 161 |
|
| 162 |
if(in1!=null && in2==null && in1.indexOf('#')>-1 && !new File(in1).exists()){ |
| 163 |
in2=in1.replace("#", "2"); |
| 164 |
in1=in1.replace("#", "1"); |
| 165 |
} |
| 166 |
if(out1!=null && out2==null && out1.indexOf('#')>-1){ |
| 167 |
out2=out1.replace("#", "2"); |
| 168 |
out1=out1.replace("#", "1"); |
| 169 |
} |
| 170 |
if(in2!=null){ |
| 171 |
if(FASTQ.FORCE_INTERLEAVED){System.err.println("Reset INTERLEAVED to false because paired input files were specified.");} |
| 172 |
FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false; |
| 173 |
} |
| 174 |
|
| 175 |
assert(FastaReadInputStream.settingsOK()); |
| 176 |
|
| 177 |
if(in1==null){throw new RuntimeException("Error - at least one input file is required.");} |
| 178 |
if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){ |
| 179 |
|
| 180 |
ByteFile.FORCE_MODE_BF2=true; |
| 181 |
} |
| 182 |
|
| 183 |
if(writeMode && out1==null){ |
| 184 |
if(out2!=null){throw new RuntimeException("Error - cannot define out2 without defining out1.");} |
| 185 |
System.err.println("No output stream specified. To write to stdout, please specify 'out=stdout.fq' or similar."); |
| 186 |
} |
| 187 |
|
| 188 |
if(!parser.setInterleaved){ |
| 189 |
assert(in1!=null && (!writeMode || out1!=null)) : "\nin1="+in1+"\nin2="+in2+"\nout1="+out1+"\nout2="+out2+"\n"; |
| 190 |
if(in2!=null){ |
| 191 |
FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false; |
| 192 |
outstream.println("Set INTERLEAVED to "+FASTQ.FORCE_INTERLEAVED); |
| 193 |
}else if(writeMode){ |
| 194 |
if(out2!=null){ |
| 195 |
FASTQ.FORCE_INTERLEAVED=true; |
| 196 |
FASTQ.TEST_INTERLEAVED=false; |
| 197 |
outstream.println("Set INTERLEAVED to "+FASTQ.FORCE_INTERLEAVED); |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
if(out1!=null && out1.equalsIgnoreCase("null")){out1=null;} |
| 203 |
if(out2!=null && out2.equalsIgnoreCase("null")){out2=null;} |
| 204 |
|
| 205 |
if(!Tools.testOutputFiles(overwrite, append, false, out1, out2)){ |
| 206 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out1+", "+out2+"\n"); |
| 207 |
} |
| 208 |
|
| 209 |
ffout1=FileFormat.testOutput(out1, FileFormat.FASTQ, extout, true, overwrite, append, false); |
| 210 |
ffout2=FileFormat.testOutput(out2, FileFormat.FASTQ, extout, true, overwrite, append, false); |
| 211 |
|
| 212 |
ffin1=FileFormat.testInput(in1, FileFormat.FASTQ, extin, true, true); |
| 213 |
ffin2=FileFormat.testInput(in2, FileFormat.FASTQ, extin, true, true); |
| 214 |
|
| 215 |
ffa=FileFormat.testInput(adapterFile, FileFormat.FASTA, null, true, true); |
| 216 |
|
| 217 |
adapters=makeAdapterList(); |
| 218 |
|
| 219 |
if(writeMode){ |
| 220 |
if(adapters==null || adapters.isEmpty()){ |
| 221 |
throw new RuntimeException("\n\nPlease specify adapters with 'adapters=file.fa' or 'literal=AGCTACGT'\n"); |
| 222 |
} |
| 223 |
randy=Shared.threadLocalRandom(); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
private final ArrayList<byte[]> makeAdapterList(){ |
| 228 |
boolean oldTI=FASTQ.TEST_INTERLEAVED; |
| 229 |
boolean oldFI=FASTQ.FORCE_INTERLEAVED; |
| 230 |
FASTQ.TEST_INTERLEAVED=false; |
| 231 |
FASTQ.FORCE_INTERLEAVED=false; |
| 232 |
ArrayList<byte[]> x=makeAdapterList2(); |
| 233 |
FASTQ.TEST_INTERLEAVED=oldTI; |
| 234 |
FASTQ.FORCE_INTERLEAVED=oldFI; |
| 235 |
return x; |
| 236 |
} |
| 237 |
|
| 238 |
private final ArrayList<byte[]> makeAdapterList2(){ |
| 239 |
if(ffa==null && literals==null){return null;} |
| 240 |
ArrayList<byte[]> list=new ArrayList<byte[]>(); |
| 241 |
if(ffa!=null){ |
| 242 |
FastaReadInputStream fris=new FastaReadInputStream(ffa, false, false, -1); |
| 243 |
for(Read r=fris.next(); r!=null; r=fris.next()){ |
| 244 |
if(r.bases!=null){ |
| 245 |
list.add(r.bases); |
| 246 |
} |
| 247 |
} |
| 248 |
fris.close(); |
| 249 |
} |
| 250 |
if(literals!=null){ |
| 251 |
for(String s : literals){ |
| 252 |
if(s!=null && !"null".equalsIgnoreCase(s)){ |
| 253 |
list.add(s.getBytes()); |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
if(addRC){ |
| 259 |
int x=list.size(); |
| 260 |
for(int i=0; i<x; i++){ |
| 261 |
list.add(AminoAcid.reverseComplementBases(list.get(i))); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
return list.size()>0 ? list : null; |
| 266 |
} |
| 267 |
|
| 268 |
void write(Timer t){ |
| 269 |
|
| 270 |
final ConcurrentReadInputStream cris; |
| 271 |
{ |
| 272 |
cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffin1, ffin2, null, null); |
| 273 |
if(verbose){System.err.println("Started cris");} |
| 274 |
cris.start(); |
| 275 |
} |
| 276 |
boolean paired=cris.paired(); |
| 277 |
if(verbose){System.err.println("Input is "+(paired ? "paired" : "unpaired"));} |
| 278 |
|
| 279 |
ConcurrentReadOutputStream ros=null; |
| 280 |
if(out1!=null){ |
| 281 |
final int buff=4; |
| 282 |
|
| 283 |
if(cris.paired() && out2==null && (in1==null || !in1.contains(".sam"))){ |
| 284 |
outstream.println("Writing interleaved."); |
| 285 |
} |
| 286 |
|
| 287 |
assert(!out1.equalsIgnoreCase(in1) && !out1.equalsIgnoreCase(in1)) : "Input file and output file have same name."; |
| 288 |
assert(out2==null || (!out2.equalsIgnoreCase(in1) && !out2.equalsIgnoreCase(in2))) : "out1 and out2 have same name."; |
| 289 |
|
| 290 |
ros=ConcurrentReadOutputStream.getStream(ffout1, ffout2, null, null, buff, null, false); |
| 291 |
ros.start(); |
| 292 |
} |
| 293 |
|
| 294 |
{ |
| 295 |
|
| 296 |
ListNum<Read> ln=cris.nextList(); |
| 297 |
ArrayList<Read> reads=(ln!=null ? ln.list : null); |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
if(reads!=null && !reads.isEmpty()){ |
| 302 |
Read r=reads.get(0); |
| 303 |
assert((ffin1==null || ffin1.samOrBam()) || (r.mate!=null)==cris.paired()); |
| 304 |
} |
| 305 |
|
| 306 |
while(ln!=null && reads!=null && reads.size()>0){ |
| 307 |
|
| 308 |
for(int idx=0; idx<reads.size(); idx++){ |
| 309 |
final Read r1=reads.get(idx); |
| 310 |
final Read r2=r1.mate; |
| 311 |
|
| 312 |
final int initialLength1=r1.length(); |
| 313 |
final int initialLength2=(r1.mateLength()); |
| 314 |
|
| 315 |
addAdapter(r1, addPaired); |
| 316 |
if(r2!=null && !addPaired){ |
| 317 |
addAdapter(r2, addPaired); |
| 318 |
} |
| 319 |
|
| 320 |
if(r2==null){ |
| 321 |
r1.id=r1.numericID+"_"+r1.id; |
| 322 |
}else{ |
| 323 |
String base=r1.numericID+"_"+r1.id+"_"+r2.id; |
| 324 |
if(addslash){ |
| 325 |
r1.id=base+" /1"; |
| 326 |
r2.id=base+" /2"; |
| 327 |
}else{ |
| 328 |
r1.id=base; |
| 329 |
r2.id=base; |
| 330 |
} |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
if(ros!=null){ros.add(reads, ln.id);} |
| 335 |
|
| 336 |
cris.returnList(ln); |
| 337 |
ln=cris.nextList(); |
| 338 |
reads=(ln!=null ? ln.list : null); |
| 339 |
} |
| 340 |
if(ln!=null){ |
| 341 |
cris.returnList(ln.id, ln.list==null || ln.list.isEmpty()); |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
errorState|=ReadWrite.closeStreams(cris, ros); |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
t.stop(); |
| 357 |
|
| 358 |
outstream.println("Adapters Added: \t"+adaptersAdded+" reads ("+Tools.format("%.2f",adaptersAdded*100.0/readsProcessed)+"%) \t"+ |
| 359 |
adapterBasesAdded+" bases ("+Tools.format("%.2f",adapterBasesAdded*100.0/basesProcessed)+"%)"); |
| 360 |
|
| 361 |
outstream.println("Valid Output: \t"+validReads+" reads ("+Tools.format("%.2f",validReads*100.0/readsProcessed)+"%) \t"+ |
| 362 |
validBases+" bases ("+Tools.format("%.2f",validBases*100.0/basesProcessed)+"%)"); |
| 363 |
|
| 364 |
outstream.println(); |
| 365 |
outstream.println(Tools.timeReadsBasesProcessed(t, readsProcessed, basesProcessed, 8)); |
| 366 |
|
| 367 |
if(errorState){ |
| 368 |
throw new RuntimeException("ReformatReads terminated in an error state; the output may be corrupt."); |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
private void addAdapter(Read r, final int loc){ |
| 373 |
final byte[] bases=r.bases; |
| 374 |
final byte[] quals=r.quality; |
| 375 |
final int remaining, initial=(bases==null ? 0 : bases.length); |
| 376 |
final byte[] adapter; |
| 377 |
int ab=0, rb=0; |
| 378 |
|
| 379 |
readsProcessed++; |
| 380 |
basesProcessed+=initial; |
| 381 |
|
| 382 |
if(bases==null){assert(false); return;} |
| 383 |
if(initial>0 && loc>=0 && loc<initial){ |
| 384 |
adapter=adapters.get(randy.nextInt(adapters.size())); |
| 385 |
adaptersAdded++; |
| 386 |
|
| 387 |
if(right){ |
| 388 |
final int lim=Tools.min(initial, adapter.length+loc); |
| 389 |
for(int i=loc, j=0; i<lim; i++, j++){ |
| 390 |
if(AminoAcid.isFullyDefined(bases[i])){ |
| 391 |
bases[i]=adapter[j]; |
| 392 |
if(adderrors){ |
| 393 |
byte q=(quals==null ? 30 : quals[i]); |
| 394 |
if(randy.nextFloat()<QualityTools.PROB_ERROR[q]){ |
| 395 |
int old=AminoAcid.baseToNumber[bases[i]]; |
| 396 |
bases[i]=AminoAcid.numberToBase[(old+randy.nextInt(3)+1)&3]; |
| 397 |
} |
| 398 |
} |
| 399 |
} |
| 400 |
ab++; |
| 401 |
} |
| 402 |
for(int i=lim; i<initial; i++){ |
| 403 |
if(AminoAcid.isFullyDefined(bases[i])){ |
| 404 |
bases[i]=AminoAcid.numberToBase[randy.nextInt(4)]; |
| 405 |
} |
| 406 |
rb++; |
| 407 |
} |
| 408 |
remaining=loc; |
| 409 |
}else{ |
| 410 |
final int lim=Tools.max(-1, loc-adapter.length); |
| 411 |
for(int i=loc, j=adapter.length-1; i>lim; i--, j--){ |
| 412 |
if(AminoAcid.isFullyDefined(bases[i])){ |
| 413 |
bases[i]=adapter[j]; |
| 414 |
if(adderrors){ |
| 415 |
byte q=(quals==null ? 30 : quals[i]); |
| 416 |
if(randy.nextFloat()<QualityTools.PROB_ERROR[q]){ |
| 417 |
int old=AminoAcid.baseToNumber[bases[i]]; |
| 418 |
bases[i]=AminoAcid.numberToBase[(old+randy.nextInt(3)+1)&3]; |
| 419 |
} |
| 420 |
} |
| 421 |
} |
| 422 |
ab++; |
| 423 |
} |
| 424 |
for(int i=lim; i>-1; i--){ |
| 425 |
if(AminoAcid.isFullyDefined(bases[i])){ |
| 426 |
bases[i]=AminoAcid.numberToBase[randy.nextInt(4)]; |
| 427 |
} |
| 428 |
rb++; |
| 429 |
} |
| 430 |
remaining=initial-loc-1; |
| 431 |
} |
| 432 |
assert(remaining<initial) : "\nremaining="+remaining+", initial="+initial+", rb="+rb+", ab="+ab+ |
| 433 |
", loc="+loc+", adapter.length="+(adapter==null ? 0 : adapter.length)+"\n"; |
| 434 |
}else{ |
| 435 |
adapter=null; |
| 436 |
remaining=initial; |
| 437 |
} |
| 438 |
|
| 439 |
assert(remaining==initial-(rb+ab)); |
| 440 |
assert(remaining>=0); |
| 441 |
|
| 442 |
adapterBasesAdded+=ab; |
| 443 |
randomBasesAdded+=rb; |
| 444 |
r.id=initial+"_"+remaining; |
| 445 |
if(remaining>=minlen){ |
| 446 |
validReads++; |
| 447 |
validBases+=remaining; |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
private void addAdapter(Read r, boolean addPaired){ |
| 452 |
final byte[] bases=r.bases; |
| 453 |
final int initial=(bases==null ? 0 : bases.length); |
| 454 |
final int loc; |
| 455 |
|
| 456 |
if(initial>0 && randy.nextFloat()<adapterProb){ |
| 457 |
loc=randy.nextInt(initial); |
| 458 |
}else{ |
| 459 |
loc=-1; |
| 460 |
} |
| 461 |
|
| 462 |
addAdapter(r, loc); |
| 463 |
if(addPaired && r.mate!=null){addAdapter(r.mate, loc);} |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
void read(Timer t){ |
| 469 |
|
| 470 |
final ConcurrentReadInputStream cris; |
| 471 |
{ |
| 472 |
cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffin1, ffin2, null, null); |
| 473 |
if(verbose){System.err.println("Started cris");} |
| 474 |
cris.start(); |
| 475 |
} |
| 476 |
boolean paired=cris.paired(); |
| 477 |
if(verbose){System.err.println("Input is "+(paired ? "paired" : "unpaired"));} |
| 478 |
|
| 479 |
{ |
| 480 |
|
| 481 |
ListNum<Read> ln=cris.nextList(); |
| 482 |
ArrayList<Read> reads=(ln!=null ? ln.list : null); |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
if(reads!=null && !reads.isEmpty()){ |
| 487 |
Read r=reads.get(0); |
| 488 |
assert((ffin1==null || ffin1.samOrBam()) || (r.mate!=null)==cris.paired()); |
| 489 |
} |
| 490 |
|
| 491 |
while(ln!=null && reads!=null && reads.size()>0){ |
| 492 |
|
| 493 |
for(int idx=0; idx<reads.size(); idx++){ |
| 494 |
final Read r1=reads.get(idx); |
| 495 |
final Read r2=r1.mate; |
| 496 |
|
| 497 |
grade(r1, r2); |
| 498 |
} |
| 499 |
|
| 500 |
cris.returnList(ln); |
| 501 |
ln=cris.nextList(); |
| 502 |
reads=(ln!=null ? ln.list : null); |
| 503 |
} |
| 504 |
if(ln!=null){ |
| 505 |
cris.returnList(ln.id, ln.list==null || ln.list.isEmpty()); |
| 506 |
} |
| 507 |
} |
| 508 |
|
| 509 |
errorState|=ReadWrite.closeStream(cris); |
| 510 |
|
| 511 |
t.stop(); |
| 512 |
|
| 513 |
long validBasesRemoved=validBasesExpected-validBasesCounted; |
| 514 |
long incorrect=readsProcessed-correct; |
| 515 |
long incorrectBases=basesProcessed-correctBases; |
| 516 |
|
| 517 |
outstream.println("Total output: \t"+readsProcessed+" reads \t"+basesProcessed+" bases "); |
| 518 |
outstream.println("Perfectly Correct (% of output): \t"+correct+" reads ("+Tools.format("%.3f",correct*100.0/readsProcessed)+ |
| 519 |
"%) \t"+correctBases+" bases ("+Tools.format("%.3f",correctBases*100.0/basesProcessed)+"%)"); |
| 520 |
outstream.println("Incorrect (% of output): \t"+incorrect+" reads ("+Tools.format("%.3f",incorrect*100.0/readsProcessed)+ |
| 521 |
"%) \t"+incorrectBases+" bases ("+Tools.format("%.3f",incorrectBases*100.0/basesProcessed)+"%)"); |
| 522 |
outstream.println(); |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
outstream.println("Adapters Remaining (% of adapters): \t"+(adapterReadsRemaining)+" reads ("+Tools.format("%.3f",adapterReadsRemaining*100.0/adapterReadsTotal)+ |
| 529 |
"%) \t"+adapterBasesRemaining+" bases ("+Tools.format("%.3f",adapterBasesRemaining*100.0/basesProcessed)+"%)"); |
| 530 |
outstream.println("Non-Adapter Removed (% of valid): \t"+tooShort+" reads ("+Tools.format("%.4f",tooShort*100.0/readsProcessed)+ |
| 531 |
"%) \t"+validBasesRemoved+" bases ("+Tools.format("%.4f",validBasesRemoved*100.0/validBasesExpected)+"%)"); |
| 532 |
|
| 533 |
if(broken>0 || mispaired>0){ |
| 534 |
outstream.println("Broken: \t"+broken+" reads ("+Tools.format("%.2f",broken*100.0/readsProcessed)+"%)"); |
| 535 |
outstream.println("Mispaired: \t"+mispaired+" reads ("+Tools.format("%.2f",mispaired*100.0/readsProcessed)+"%)"); |
| 536 |
} |
| 537 |
|
| 538 |
if(errorState){ |
| 539 |
throw new RuntimeException("ReformatReads terminated in an error state; the output may be corrupt."); |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
private void grade(Read r1, Read r2){ |
| 571 |
grade(r1); |
| 572 |
grade(r2); |
| 573 |
} |
| 574 |
|
| 575 |
private void grade(Read r){ |
| 576 |
if(r==null){return;} |
| 577 |
|
| 578 |
int insert=r.insert(); |
| 579 |
int originalLength=r.stop-r.start+1; |
| 580 |
int length=r.length(); |
| 581 |
|
| 582 |
final int offset=(2*r.pairnum()); |
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
final int initial=originalLength; |
| 587 |
final int remaining=Tools.min(initial, insert); |
| 588 |
final int actual=length; |
| 589 |
|
| 590 |
readsProcessed++; |
| 591 |
basesProcessed+=actual; |
| 592 |
|
| 593 |
assert(initial>=remaining); |
| 594 |
|
| 595 |
if(actual>initial){broken++;} |
| 596 |
|
| 597 |
validBasesExpected+=remaining; |
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
if(initial==remaining){ |
| 603 |
if(actual==remaining || (actual<2 && (remaining<1 || remaining<minlen))){ |
| 604 |
correct++; |
| 605 |
correctBases+=remaining; |
| 606 |
validBasesCounted+=remaining; |
| 607 |
trueNeg++; |
| 608 |
}else if(actual<remaining){ |
| 609 |
tooShort++; |
| 610 |
tooShortReadBases+=actual; |
| 611 |
tooShortBases+=(remaining-actual); |
| 612 |
validBasesCounted+=actual; |
| 613 |
falsePos++; |
| 614 |
}else if(actual>remaining){ |
| 615 |
tooLong++; |
| 616 |
tooLongReadBases+=remaining; |
| 617 |
tooLongBases+=(actual-remaining); |
| 618 |
validBasesCounted+=remaining; |
| 619 |
falseNeg++; |
| 620 |
} |
| 621 |
}else{ |
| 622 |
|
| 623 |
adapterBasesTotal+=(initial-remaining); |
| 624 |
adapterReadsTotal++; |
| 625 |
|
| 626 |
if(actual==remaining || (actual<2 && (remaining<1 || remaining<minlen))){ |
| 627 |
correct++; |
| 628 |
correctBases+=remaining; |
| 629 |
validBasesCounted+=remaining; |
| 630 |
truePos++; |
| 631 |
}else if(actual<remaining){ |
| 632 |
tooShort++; |
| 633 |
tooShortReadBases+=actual; |
| 634 |
tooShortBases+=(remaining-actual); |
| 635 |
validBasesCounted+=actual; |
| 636 |
truePos++; |
| 637 |
}else if(actual>remaining){ |
| 638 |
tooLong++; |
| 639 |
tooLongReadBases+=actual; |
| 640 |
tooLongBases+=(actual-remaining); |
| 641 |
adapterBasesRemaining+=(actual-remaining); |
| 642 |
validBasesCounted+=remaining; |
| 643 |
falseNeg++; |
| 644 |
adapterReadsRemaining++; |
| 645 |
} |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
public boolean errorState=false; |
| 652 |
|
| 653 |
private String in1=null; |
| 654 |
private String in2=null; |
| 655 |
|
| 656 |
private String out1=null; |
| 657 |
private String out2=null; |
| 658 |
|
| 659 |
private String extin=null; |
| 660 |
private String extout=null; |
| 661 |
|
| 662 |
private String adapterFile=null; |
| 663 |
private String[] literals=null; |
| 664 |
|
| 665 |
private boolean overwrite=true; |
| 666 |
private boolean append=false; |
| 667 |
|
| 668 |
|
| 669 |
private boolean addslash=true; |
| 670 |
|
| 671 |
private boolean changename=true; |
| 672 |
|
| 673 |
private boolean adderrors=true; |
| 674 |
|
| 675 |
|
| 676 |
private boolean addPaired=true; |
| 677 |
|
| 678 |
private boolean addRC=false; |
| 679 |
|
| 680 |
private boolean right=true; |
| 681 |
|
| 682 |
private long maxReads=-1; |
| 683 |
private int minlen=1; |
| 684 |
|
| 685 |
private boolean writeMode=true; |
| 686 |
private float adapterProb=0.5f; |
| 687 |
|
| 688 |
private long readsProcessed=0; |
| 689 |
private long basesProcessed=0; |
| 690 |
private long adaptersAdded=0; |
| 691 |
private long adapterBasesAdded=0; |
| 692 |
private long randomBasesAdded=0; |
| 693 |
private long validReads=0; |
| 694 |
private long validBases=0; |
| 695 |
|
| 696 |
private long truePos=0; |
| 697 |
private long trueNeg=0; |
| 698 |
private long falsePos=0; |
| 699 |
private long falseNeg=0; |
| 700 |
private long broken=0; |
| 701 |
private long mispaired=0; |
| 702 |
|
| 703 |
private long tooShort=0; |
| 704 |
private long tooLong=0; |
| 705 |
private long correct=0; |
| 706 |
private long fullyRemoved=0; |
| 707 |
|
| 708 |
private long tooShortBases=0; |
| 709 |
private long tooLongBases=0; |
| 710 |
private long tooShortReadBases=0; |
| 711 |
private long tooLongReadBases=0; |
| 712 |
private long correctBases=0; |
| 713 |
|
| 714 |
private long validBasesCounted=0; |
| 715 |
private long validBasesExpected=0; |
| 716 |
|
| 717 |
|
| 718 |
private long adapterBasesTotal=0; |
| 719 |
private long adapterReadsTotal=0; |
| 720 |
private long adapterReadsRemaining=0; |
| 721 |
private long adapterBasesRemaining=0; |
| 722 |
|
| 723 |
private final FileFormat ffin1; |
| 724 |
private final FileFormat ffin2; |
| 725 |
|
| 726 |
private final FileFormat ffout1; |
| 727 |
private final FileFormat ffout2; |
| 728 |
|
| 729 |
private final FileFormat ffa; |
| 730 |
|
| 731 |
private final ArrayList<byte[]> adapters; |
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
private PrintStream outstream=System.err; |
| 736 |
public static boolean verbose=false; |
| 737 |
|
| 738 |
private java.util.Random randy; |
| 739 |
|
| 740 |
} |