| 1 |
package jgi; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.PrintStream; |
| 5 |
import java.util.ArrayList; |
| 6 |
import java.util.Arrays; |
| 7 |
|
| 8 |
import dna.AminoAcid; |
| 9 |
import fileIO.ByteFile; |
| 10 |
import fileIO.FileFormat; |
| 11 |
import fileIO.ReadWrite; |
| 12 |
import shared.KillSwitch; |
| 13 |
import shared.Parse; |
| 14 |
import shared.Parser; |
| 15 |
import shared.PreParser; |
| 16 |
import shared.Shared; |
| 17 |
import shared.Timer; |
| 18 |
import shared.Tools; |
| 19 |
import stream.ConcurrentReadInputStream; |
| 20 |
import stream.ConcurrentReadOutputStream; |
| 21 |
import stream.FASTQ; |
| 22 |
import stream.FastaReadInputStream; |
| 23 |
import stream.Read; |
| 24 |
import structures.ListNum; |
| 25 |
import tracker.ReadStats; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public class Consect { |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public static void main(String[] args){ |
| 45 |
Timer t=new Timer(); |
| 46 |
Consect x=new Consect(args); |
| 47 |
x.process(t); |
| 48 |
|
| 49 |
|
| 50 |
Shared.closeStream(x.outstream); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public Consect(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_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 68 |
ReadWrite.setZipThreads(Shared.threads()); |
| 69 |
FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false; |
| 70 |
|
| 71 |
|
| 72 |
Parser parser=new Parser(); |
| 73 |
|
| 74 |
|
| 75 |
for(int i=0; i<args.length; i++){ |
| 76 |
String arg=args[i]; |
| 77 |
|
| 78 |
|
| 79 |
String[] split=arg.split("="); |
| 80 |
String a=split[0].toLowerCase(); |
| 81 |
String b=split.length>1 ? split[1] : null; |
| 82 |
|
| 83 |
if(a.equals("in") || a.equals("in")){ |
| 84 |
assert(b!=null) : "Bad parameter: "+arg; |
| 85 |
in.clear(); |
| 86 |
String[] split2=b.split(","); |
| 87 |
for(String s : split2){in.add(s);} |
| 88 |
}else if(a.equals("cq") || a.equals("changequality")){ |
| 89 |
changeQuality=Parse.parseBoolean(b); |
| 90 |
}else if(parser.parse(arg, a, b)){ |
| 91 |
|
| 92 |
}else if(a.equals("verbose")){ |
| 93 |
verbose=Parse.parseBoolean(b); |
| 94 |
}else if(new File(arg).exists()){ |
| 95 |
in.add(arg); |
| 96 |
}else if(a.equals("parse_flag_goes_here")){ |
| 97 |
|
| 98 |
}else{ |
| 99 |
outstream.println("Unknown parameter "+args[i]); |
| 100 |
assert(false) : "Unknown parameter "+args[i]; |
| 101 |
|
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
{ |
| 106 |
Parser.processQuality(); |
| 107 |
|
| 108 |
maxReads=parser.maxReads; |
| 109 |
|
| 110 |
overwrite=ReadStats.overwrite=parser.overwrite; |
| 111 |
append=ReadStats.append=parser.append; |
| 112 |
|
| 113 |
out=parser.out1; |
| 114 |
|
| 115 |
extin=parser.extin; |
| 116 |
extout=parser.extout; |
| 117 |
} |
| 118 |
|
| 119 |
assert(FastaReadInputStream.settingsOK()); |
| 120 |
|
| 121 |
|
| 122 |
if(in==null || in.size()<3){throw new RuntimeException("\nError - at least three input files are required; one original and two error-corrected.");} |
| 123 |
|
| 124 |
|
| 125 |
if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){ |
| 126 |
ByteFile.FORCE_MODE_BF2=true; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
if(!Tools.testOutputFiles(overwrite, append, false, out)){ |
| 131 |
outstream.println((out==null)+", "+out); |
| 132 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output file "+out+"\n"); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
if(!Tools.testInputFiles(false, true, in.toArray(new String[0]))){ |
| 137 |
throw new RuntimeException("\nCan't read some input files.\n"); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
if(!Tools.testForDuplicateFiles(true, in.toArray(new String[0]))){ |
| 142 |
throw new RuntimeException("\nSome file names were specified multiple times.\n"); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
ffout=FileFormat.testOutput(out, FileFormat.FASTQ, extout, true, overwrite, append, false); |
| 147 |
|
| 148 |
|
| 149 |
ffin=new FileFormat[in.size()]; |
| 150 |
for(int i=0; i<in.size(); i++){ |
| 151 |
ffin[i]=FileFormat.testInput(in.get(i), FileFormat.FASTQ, extin, true, true); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
void process(Timer t){ |
| 161 |
|
| 162 |
|
| 163 |
final ConcurrentReadInputStream[] crisa=new ConcurrentReadInputStream[ffin.length]; |
| 164 |
for(int i=0; i<ffin.length; i++){ |
| 165 |
crisa[i]=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffin[i], null, null, null); |
| 166 |
crisa[i].start(); |
| 167 |
if(verbose){outstream.println("Started cris "+i);} |
| 168 |
assert(!crisa[i].paired()); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
final ConcurrentReadOutputStream ros; |
| 173 |
if(ffout!=null){ |
| 174 |
final int buff=4; |
| 175 |
|
| 176 |
ros=ConcurrentReadOutputStream.getStream(ffout, null, qfout, null, buff, null, false); |
| 177 |
ros.start(); |
| 178 |
}else{ros=null;} |
| 179 |
|
| 180 |
|
| 181 |
readsProcessed=0; |
| 182 |
basesProcessed=0; |
| 183 |
|
| 184 |
|
| 185 |
processInner(crisa, ros); |
| 186 |
|
| 187 |
if(verbose){outstream.println("Finished; closing streams.");} |
| 188 |
|
| 189 |
|
| 190 |
for(int i=0; i<crisa.length; i++){ |
| 191 |
errorState|=ReadWrite.closeStream(crisa[i]); |
| 192 |
} |
| 193 |
|
| 194 |
errorState|=ReadWrite.closeStream(ros); |
| 195 |
|
| 196 |
|
| 197 |
{ |
| 198 |
t.stop(); |
| 199 |
|
| 200 |
outstream.println("Errors Corrected: \t"+corrections); |
| 201 |
outstream.println("Disagreements: \t"+disagreements); |
| 202 |
outstream.println(); |
| 203 |
|
| 204 |
outstream.println("Reads With Corrections: \t"+readsWithCorrections); |
| 205 |
outstream.println("Reads With Disagreements: \t"+readsWithDisagreements); |
| 206 |
outstream.println(); |
| 207 |
|
| 208 |
outstream.println("Reads Fully Corrected: \t"+readsFullyCorrected); |
| 209 |
outstream.println("Reads Partly Corrected: \t"+readsPartlyCorrected); |
| 210 |
outstream.println("Reads Not Corrected: \t"+readsNotCorrected); |
| 211 |
outstream.println("Reads Error Free: \t"+readsErrorFree); |
| 212 |
outstream.println(); |
| 213 |
|
| 214 |
outstream.println(Tools.timeReadsBasesProcessed(t, readsProcessed, basesProcessed, 8)); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
if(errorState){ |
| 219 |
throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt."); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
void processInner(final ConcurrentReadInputStream[] crisa, final ConcurrentReadOutputStream ros){ |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
{ |
| 229 |
@SuppressWarnings("unchecked") |
| 230 |
ArrayList<Read>[] array=new ArrayList[crisa.length]; |
| 231 |
long id=0; |
| 232 |
for(int i=0; i<crisa.length; i++){ |
| 233 |
ListNum<Read> ln=crisa[i].nextList(); |
| 234 |
if(ln!=null){ |
| 235 |
if(verbose){outstream.println("Fetched "+(ln.list==null ? 0 : ln.list.size())+" reads.");} |
| 236 |
array[i]=ln.list; |
| 237 |
id=ln.id; |
| 238 |
}else{ |
| 239 |
array[i]=null; |
| 240 |
} |
| 241 |
} |
| 242 |
if(verbose){outstream.println("Finished fetching block.");} |
| 243 |
|
| 244 |
|
| 245 |
while(array[0]!=null && array[0].size()>0){ |
| 246 |
|
| 247 |
ArrayList<Read> readsOut=consensus(array); |
| 248 |
|
| 249 |
|
| 250 |
if(ros!=null){ros.add(readsOut, id);} |
| 251 |
|
| 252 |
|
| 253 |
for(ConcurrentReadInputStream cris : crisa){ |
| 254 |
cris.returnList(id, false); |
| 255 |
if(verbose){outstream.println("Returned a list.");} |
| 256 |
} |
| 257 |
|
| 258 |
for(int i=0; i<crisa.length; i++){ |
| 259 |
ListNum<Read> ln=crisa[i].nextList(); |
| 260 |
if(ln!=null){ |
| 261 |
if(verbose){outstream.println("Fetched "+(ln.list==null ? 0 : ln.list.size())+" reads.");} |
| 262 |
array[i]=ln.list; |
| 263 |
id=ln.id; |
| 264 |
}else{ |
| 265 |
array[i]=null; |
| 266 |
} |
| 267 |
} |
| 268 |
if(verbose){outstream.println("Finished fetching block.");} |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
for(ConcurrentReadInputStream cris : crisa){ |
| 273 |
cris.returnList(id, true); |
| 274 |
if(verbose){outstream.println("Returned final list.");} |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
private ArrayList<Read> consensus(ArrayList<Read>[] array){ |
| 288 |
Read[] row=new Read[array.length]; |
| 289 |
for(int i=0, max=array[0].size(); i<max; i++){ |
| 290 |
for(int j=0; j<array.length; j++){ |
| 291 |
row[j]=array[j].get(i); |
| 292 |
} |
| 293 |
consensus(row); |
| 294 |
} |
| 295 |
return array[0]; |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
private int consensus(Read[] reads){ |
| 300 |
Read original=reads[0]; |
| 301 |
byte[] obases=original.bases; |
| 302 |
byte[] oquals=changeQuality ? null : original.quality; |
| 303 |
|
| 304 |
for(Read r : reads){assert(r.id.equals(original.id)) : "\n"+r.id+"\n"+original.id;} |
| 305 |
|
| 306 |
readsProcessed++; |
| 307 |
basesProcessed+=obases.length; |
| 308 |
|
| 309 |
int[] counts=KillSwitch.allocInt1D(4); |
| 310 |
int localCorrections=0; |
| 311 |
int localDisagreements=0; |
| 312 |
for(int pos=0; pos<obases.length; pos++){ |
| 313 |
Arrays.fill(counts, 0); |
| 314 |
int sum=0, last=-1, tooShort=0; |
| 315 |
byte qmax=0; |
| 316 |
for(int col=1; col<reads.length; col++){ |
| 317 |
Read r=reads[col]; |
| 318 |
if(r.length()>pos){ |
| 319 |
byte q=(r.quality==null ? 0 : r.quality[pos]); |
| 320 |
byte b=r.bases[pos]; |
| 321 |
int x=AminoAcid.baseToNumber[b]; |
| 322 |
if(x>=0){ |
| 323 |
sum++; |
| 324 |
counts[x]++; |
| 325 |
last=x; |
| 326 |
qmax=Tools.max(q, qmax); |
| 327 |
} |
| 328 |
}else{tooShort++;} |
| 329 |
} |
| 330 |
if(sum>1){ |
| 331 |
int maxIndex=Tools.maxIndex(counts); |
| 332 |
int max=counts[maxIndex]; |
| 333 |
int x0=AminoAcid.baseToNumber[obases[pos]]; |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
if(max==sum && tooShort==0){ |
| 340 |
if(maxIndex!=x0){localCorrections++;} |
| 341 |
obases[pos]=AminoAcid.numberToBase[maxIndex]; |
| 342 |
if(oquals!=null){ |
| 343 |
oquals[pos]=qmax; |
| 344 |
} |
| 345 |
}else{ |
| 346 |
localDisagreements++; |
| 347 |
} |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
disagreements+=localDisagreements; |
| 352 |
corrections+=localCorrections; |
| 353 |
|
| 354 |
if(localDisagreements+localCorrections>0){ |
| 355 |
|
| 356 |
if(localCorrections>0){ |
| 357 |
readsWithCorrections++; |
| 358 |
} |
| 359 |
|
| 360 |
if(localDisagreements>0){ |
| 361 |
readsWithDisagreements++; |
| 362 |
if(localCorrections>0){ |
| 363 |
readsPartlyCorrected++; |
| 364 |
}else{ |
| 365 |
readsNotCorrected++; |
| 366 |
} |
| 367 |
}else{ |
| 368 |
readsFullyCorrected++; |
| 369 |
} |
| 370 |
}else{ |
| 371 |
readsErrorFree++; |
| 372 |
} |
| 373 |
|
| 374 |
return localCorrections; |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
private ArrayList<String> in=new ArrayList<String>(); |
| 383 |
|
| 384 |
|
| 385 |
private String out=null; |
| 386 |
|
| 387 |
private String qfout=null; |
| 388 |
|
| 389 |
|
| 390 |
private String extin=null; |
| 391 |
|
| 392 |
private String extout=null; |
| 393 |
|
| 394 |
private boolean changeQuality=false; |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
protected long readsProcessed=0; |
| 400 |
|
| 401 |
protected long basesProcessed=0; |
| 402 |
|
| 403 |
|
| 404 |
private long maxReads=-1; |
| 405 |
|
| 406 |
private long readsFullyCorrected=0; |
| 407 |
private long readsPartlyCorrected=0; |
| 408 |
private long readsNotCorrected=0; |
| 409 |
private long readsErrorFree=0; |
| 410 |
private long readsWithDisagreements=0; |
| 411 |
private long readsWithCorrections=0; |
| 412 |
|
| 413 |
private long disagreements=0; |
| 414 |
private long corrections=0; |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
private final FileFormat[] ffin; |
| 422 |
|
| 423 |
|
| 424 |
private final FileFormat ffout; |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
private PrintStream outstream=System.err; |
| 432 |
|
| 433 |
public static boolean verbose=false; |
| 434 |
|
| 435 |
public boolean errorState=false; |
| 436 |
|
| 437 |
private boolean overwrite=true; |
| 438 |
|
| 439 |
private boolean append=false; |
| 440 |
|
| 441 |
private final boolean ordered=false; |
| 442 |
|
| 443 |
} |