| 1 |
package var; |
| 2 |
|
| 3 |
import java.io.OutputStream; |
| 4 |
import java.io.PrintWriter; |
| 5 |
import java.util.ArrayList; |
| 6 |
import java.util.HashMap; |
| 7 |
|
| 8 |
import align2.MultiStateAligner9PacBio; |
| 9 |
import align2.MultiStateAligner9ts; |
| 10 |
import align2.TranslateColorspaceRead; |
| 11 |
import dna.Data; |
| 12 |
import fileIO.ReadWrite; |
| 13 |
import fileIO.TextFile; |
| 14 |
import pacbio.SiteR; |
| 15 |
import shared.Parse; |
| 16 |
import shared.Parser; |
| 17 |
import shared.PreParser; |
| 18 |
import shared.Shared; |
| 19 |
import shared.Timer; |
| 20 |
import shared.Tools; |
| 21 |
import stream.ConcurrentLegacyReadInputStream; |
| 22 |
import stream.RTextInputStream; |
| 23 |
import stream.Read; |
| 24 |
import stream.SiteScore; |
| 25 |
import stream.SiteScoreR; |
| 26 |
import structures.CoverageArray; |
| 27 |
import structures.ListNum; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public class GenerateVarlets3 { |
| 32 |
|
| 33 |
|
| 34 |
public static void main(String[] args){ |
| 35 |
{ |
| 36 |
PreParser pp=new PreParser(args, new Object() { }.getClass().getEnclosingClass(), false); |
| 37 |
args=pp.args; |
| 38 |
|
| 39 |
} |
| 40 |
|
| 41 |
Data.GENOME_BUILD=-1; |
| 42 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 43 |
|
| 44 |
String reads1=args[0]; |
| 45 |
String reads2=args[1].equalsIgnoreCase("null") ? null : args[1]; |
| 46 |
String outname=args[2]; |
| 47 |
String pcovFile=null; |
| 48 |
String covFile=null; |
| 49 |
String sitesfile=null; |
| 50 |
int minChrom=-1; |
| 51 |
int maxChrom=-1; |
| 52 |
int distFromDefined=-1; |
| 53 |
|
| 54 |
for(int i=3; i<args.length; i++){ |
| 55 |
final String arg=args[i]; |
| 56 |
final String[] split=arg.split("="); |
| 57 |
String a=split[0].toLowerCase(); |
| 58 |
String b=split.length>1 ? split[1] : null; |
| 59 |
|
| 60 |
if(Parser.parseZip(arg, a, b)){ |
| 61 |
|
| 62 |
}else if(a.equals("condense")){ |
| 63 |
CONDENSE=Parse.parseBoolean(b); |
| 64 |
}else if(a.equals("condensesnps")){ |
| 65 |
CONDENSE_SNPS=Parse.parseBoolean(b); |
| 66 |
}else if(a.startsWith("splitsubs")){ |
| 67 |
SPLIT_SUBS=Parse.parseBoolean(b); |
| 68 |
}else if(a.startsWith("illumina")){ |
| 69 |
PAC_BIO_MODE=!Parse.parseBoolean(b); |
| 70 |
}else if(a.startsWith("pacbio")){ |
| 71 |
PAC_BIO_MODE=Parse.parseBoolean(b); |
| 72 |
}else if(a.equals("tosssolo1")){ |
| 73 |
TOSS_SOLO1=Parse.parseBoolean(b); |
| 74 |
}else if(a.equals("tosssolo2")){ |
| 75 |
TOSS_SOLO2=Parse.parseBoolean(b); |
| 76 |
}else if(a.startsWith("minchrom")){ |
| 77 |
minChrom=Integer.parseInt(b); |
| 78 |
}else if(a.startsWith("maxchrom")){ |
| 79 |
maxChrom=Integer.parseInt(b); |
| 80 |
}else if(a.startsWith("build") || a.startsWith("genomebuild") || a.startsWith("genome")){ |
| 81 |
Data.setGenome(Integer.parseInt(b)); |
| 82 |
System.out.println("Set GENOME_BUILD to "+Data.GENOME_BUILD); |
| 83 |
}else if(a.equals("threads") || a.equals("t")){ |
| 84 |
THREADS=(Integer.parseInt(b)); |
| 85 |
}else if(a.startsWith("buffer") || a.startsWith("writebuffer")){ |
| 86 |
WRITE_BUFFER=(Integer.parseInt(b)); |
| 87 |
}else if(a.startsWith("maxreads")){ |
| 88 |
MAX_READS=(Long.parseLong(b)); |
| 89 |
}else if(a.startsWith("minenddist")){ |
| 90 |
MIN_END_DIST=Integer.parseInt(b); |
| 91 |
}else if(a.startsWith("alignrow")){ |
| 92 |
ALIGN_ROWS=Integer.parseInt(b); |
| 93 |
}else if(a.startsWith("aligncol")){ |
| 94 |
ALIGN_COLUMNS=Integer.parseInt(b); |
| 95 |
}else if(a.startsWith("pcovtipdist")){ |
| 96 |
PCOV_TIP_DIST=Integer.parseInt(b); |
| 97 |
}else if(a.equals("blocksize")){ |
| 98 |
BLOCKSIZE=(Integer.parseInt(b)); |
| 99 |
}else if(a.equals("norefcap") || a.equals("distfromdefined") || a.equals("maxdistfromdefined")){ |
| 100 |
distFromDefined=(Integer.parseInt(b)); |
| 101 |
}else if(a.startsWith("sites") || a.startsWith("sitesfile")){ |
| 102 |
sitesfile=(b==null || b.equalsIgnoreCase("null") ? null : b); |
| 103 |
}else if(a.startsWith("pcov") || a.startsWith("perfectcov")){ |
| 104 |
pcovFile=(b==null || b.equalsIgnoreCase("null") ? null : b); |
| 105 |
}else if(a.equals("cov") || a.startsWith("coverage")){ |
| 106 |
covFile=(b==null || b.equalsIgnoreCase("null") ? null : b); |
| 107 |
}else{ |
| 108 |
throw new RuntimeException("Unknown parameter "+args[i]); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
if(Data.GENOME_BUILD<0){throw new RuntimeException("Please set genome number.");} |
| 113 |
if(minChrom<0){minChrom=1;} |
| 114 |
if(maxChrom<0){maxChrom=Data.numChroms;} |
| 115 |
|
| 116 |
assert(minChrom<=maxChrom && minChrom>=0); |
| 117 |
|
| 118 |
if(ReadWrite.ZIPLEVEL<2){ReadWrite.ZIPLEVEL=2;} |
| 119 |
GenerateVarlets3 gv=new GenerateVarlets3(reads1, reads2, outname, MAX_READS, sitesfile, pcovFile, distFromDefined); |
| 120 |
gv.process(); |
| 121 |
} |
| 122 |
|
| 123 |
public GenerateVarlets3(String fname1, String fname2, String outname_, long maxReads, String sitesfile_, String pcovFile, int distFromDefined_){ |
| 124 |
this(new RTextInputStream(fname1, fname2, maxReads), outname_, maxReads, sitesfile_, pcovFile, distFromDefined_); |
| 125 |
assert(fname2==null || !fname1.equals(fname2)) : "Error - input files have same name."; |
| 126 |
} |
| 127 |
|
| 128 |
public GenerateVarlets3(RTextInputStream stream_, String outname_, long maxReads, String sitesfile_, String pcovFile, int distFromDefined_){ |
| 129 |
sitesfile=sitesfile_; |
| 130 |
sitesTextFile=new TextFile(sitesfile, false); |
| 131 |
stream=stream_; |
| 132 |
outname=outname_; |
| 133 |
assert(outname==null || outname.contains("#")) : "Output file name must contain the character '#' to be used for key number."; |
| 134 |
makeKeyMap(); |
| 135 |
|
| 136 |
cris=(USE_CRIS ? new ConcurrentLegacyReadInputStream(stream, maxReads) : null); |
| 137 |
if(CONDENSE_SNPS){assert(!SPLIT_SUBS);} |
| 138 |
|
| 139 |
maxDistFromDefined=distFromDefined_; |
| 140 |
|
| 141 |
if(maxDistFromDefined>0){ |
| 142 |
|
| 143 |
nearestDefinedBase=new char[Data.numChroms+1][]; |
| 144 |
for(int i=1; i<=Data.numChroms; i++){ |
| 145 |
nearestDefinedBase[i]=Data.getChromosome(i).nearestDefinedBase(); |
| 146 |
} |
| 147 |
}else{ |
| 148 |
nearestDefinedBase=null; |
| 149 |
} |
| 150 |
|
| 151 |
if(pcovFile!=null){ |
| 152 |
assert(pcovFile.contains("#") || Data.numChroms<2); |
| 153 |
pcov=new CoverageArray[Data.numChroms+1]; |
| 154 |
for(int i=1; i<=Data.numChroms; i++){ |
| 155 |
String fname=pcovFile.replaceFirst("#", ""+i); |
| 156 |
pcov[i]=ReadWrite.read(CoverageArray.class, fname, true); |
| 157 |
} |
| 158 |
}else{ |
| 159 |
pcov=null; |
| 160 |
} |
| 161 |
|
| 162 |
} |
| 163 |
|
| 164 |
public void finish(){ |
| 165 |
|
| 166 |
ArrayList<Long> keys=new ArrayList<Long>(); |
| 167 |
keys.addAll(keymap.keySet()); |
| 168 |
Shared.sort(keys); |
| 169 |
for(long k : keys){ |
| 170 |
ArrayList<Varlet> vars=keymap.remove(k); |
| 171 |
if(!vars.isEmpty()){writeList(vars);} |
| 172 |
} |
| 173 |
|
| 174 |
if(cris!=null){ReadWrite.closeStream(cris);} |
| 175 |
else{stream.close();} |
| 176 |
|
| 177 |
} |
| 178 |
|
| 179 |
public void process(){ |
| 180 |
|
| 181 |
Timer t=new Timer(); |
| 182 |
|
| 183 |
if(sitesfile==null){ |
| 184 |
sitemap=null; |
| 185 |
} |
| 186 |
|
| 187 |
cris.start(); |
| 188 |
ProcessThread[] threadHandles=new ProcessThread[THREADS]; |
| 189 |
for(int i=0; i<THREADS; i++){ |
| 190 |
threadHandles[i]=new ProcessThread(); |
| 191 |
threadHandles[i].start(); |
| 192 |
} |
| 193 |
|
| 194 |
long varsMade=0; |
| 195 |
long norefsMade=0; |
| 196 |
long snpMade=0; |
| 197 |
long delMade=0; |
| 198 |
long subnMade=0; |
| 199 |
long subdMade=0; |
| 200 |
long subiMade=0; |
| 201 |
long insMade=0; |
| 202 |
long deltaLen=0; |
| 203 |
long sitesProcessed=0; |
| 204 |
long readsProcessed=0; |
| 205 |
|
| 206 |
for(int i=0; i<threadHandles.length; i++){ |
| 207 |
ProcessThread pt=threadHandles[i]; |
| 208 |
while(!pt.finished()){ |
| 209 |
synchronized(pt){ |
| 210 |
try { |
| 211 |
pt.wait(1000); |
| 212 |
} catch (InterruptedException e) { |
| 213 |
|
| 214 |
e.printStackTrace(); |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
varsMade+=pt.varsMade; |
| 219 |
norefsMade+=pt.norefsMade; |
| 220 |
snpMade+=pt.snpMade; |
| 221 |
delMade+=pt.delMade; |
| 222 |
subnMade+=pt.subnMade; |
| 223 |
subdMade+=pt.subdMade; |
| 224 |
subiMade+=pt.subiMade; |
| 225 |
insMade+=pt.insMade; |
| 226 |
deltaLen+=pt.deltaLen; |
| 227 |
sitesProcessed+=pt.sitesProcessed; |
| 228 |
readsProcessed+=pt.readsProcessed; |
| 229 |
} |
| 230 |
|
| 231 |
sitesTextFile.close(); |
| 232 |
assert(sitemap==null || sitemap.size()==0) : sitemap; |
| 233 |
|
| 234 |
finish(); |
| 235 |
|
| 236 |
t.stop(); |
| 237 |
|
| 238 |
System.out.println("\nOutput variations count"); |
| 239 |
System.out.println("Total (minus no-ref): \t"+(varsMade-norefsMade)); |
| 240 |
System.out.println("Deletions: \t"+(delMade)); |
| 241 |
System.out.println("D-type subs: \t"+(subdMade)); |
| 242 |
System.out.println("Insertions: \t"+(insMade)); |
| 243 |
System.out.println("I-type subs: \t"+(subiMade)); |
| 244 |
System.out.println("Snps: \t"+(snpMade)); |
| 245 |
System.out.println("N-type subs: \t"+(subnMade)); |
| 246 |
System.out.println("No-refs: \t"+(norefsMade)); |
| 247 |
System.out.println("Delta Length: \t"+(deltaLen)); |
| 248 |
System.out.println("Lines Loaded: \t"+(linesLoaded)); |
| 249 |
System.out.println("Lines Retained: \t"+(linesRetained)); |
| 250 |
System.out.println("Reads Processed: \t"+(readsProcessed)); |
| 251 |
System.out.println("Sites Loaded: \t"+(sitesLoaded)); |
| 252 |
System.out.println("Sites Retained: \t"+(sitesRetained)); |
| 253 |
System.out.println("Sites Processed: \t"+(sitesProcessed)); |
| 254 |
System.out.println(); |
| 255 |
System.out.println("Max Site Table Size: \t"+maxSiteTableSize); |
| 256 |
System.out.println(); |
| 257 |
System.out.println("Time:\t"+t); |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
private final long readSites(TextFile tf, long maxID) { |
| 266 |
long maxFound=-1; |
| 267 |
|
| 268 |
final boolean retainSemiperfect=maxDistFromDefined!=0; |
| 269 |
synchronized(sitemap){ |
| 270 |
|
| 271 |
if(maxID>=maxSiteRead && tf.isOpen()){ |
| 272 |
|
| 273 |
|
| 274 |
String s; |
| 275 |
for(s=tf.nextLine(); s!=null; s=tf.nextLine()){ |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
SiteR head=toImperfectSites(s, retainSemiperfect); |
| 285 |
if(head!=null){ |
| 286 |
sitemap.put(head.idPairnum, head); |
| 287 |
long id=head.numericID(); |
| 288 |
assert(id>=maxFound); |
| 289 |
maxFound=id; |
| 290 |
} |
| 291 |
if(maxFound>maxID){break;} |
| 292 |
} |
| 293 |
maxSiteRead=Tools.max(maxFound, maxSiteRead); |
| 294 |
if(s==null){ |
| 295 |
tf.close(); |
| 296 |
|
| 297 |
maxSiteRead=Long.MAX_VALUE; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
if(maxSiteRead<=maxID){assert(!tf.isOpen());} |
| 302 |
maxSiteTableSize=Tools.max(maxSiteTableSize, sitemap.size()); |
| 303 |
|
| 304 |
} |
| 305 |
|
| 306 |
return maxSiteRead; |
| 307 |
} |
| 308 |
|
| 309 |
public SiteR toImperfectSites(String s, boolean retainSemiperfect){ |
| 310 |
SiteR head=null; |
| 311 |
SiteR prev=null; |
| 312 |
String[] split=s.split("\t"); |
| 313 |
|
| 314 |
|
| 315 |
sitesLoaded+=split.length; |
| 316 |
linesLoaded++; |
| 317 |
|
| 318 |
for(int i=0; i<split.length; i++){ |
| 319 |
SiteScoreR ssr=SiteScoreR.fromText(split[i]); |
| 320 |
|
| 321 |
boolean retain=true; |
| 322 |
|
| 323 |
if(ssr.perfect || (ssr.semiperfect && !retainSemiperfect)){retain=false;} |
| 324 |
|
| 325 |
|
| 326 |
if(retain && !ssr.semiperfect && pcov!=null){ |
| 327 |
CoverageArray ca=pcov[ssr.chrom]; |
| 328 |
boolean toss=true; |
| 329 |
for(int j=ssr.start-PCOV_TIP_DIST; toss && j<=ssr.stop+PCOV_TIP_DIST; j++){ |
| 330 |
toss=ca.get(j)>=MIN_PCOV_DEPTH_TO_TOSS; |
| 331 |
} |
| 332 |
if(toss){retain=false;} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
} |
| 337 |
|
| 338 |
if(retain){ |
| 339 |
|
| 340 |
SiteR sr=new SiteR(ssr); |
| 341 |
if(head==null){ |
| 342 |
head=sr; |
| 343 |
prev=head; |
| 344 |
}else{ |
| 345 |
assert(sr.idPairnum==prev.idPairnum) : "Not sorted correctly."; |
| 346 |
prev.next=sr; |
| 347 |
prev=sr; |
| 348 |
} |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
if(head!=null){ |
| 354 |
sitesRetained+=head.listLength(); |
| 355 |
linesRetained++; |
| 356 |
} |
| 357 |
return head; |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
public static SiteR toImperfectSites2(String s){ |
| 362 |
SiteScoreR[] array=SiteScoreR.fromTextArray(s); |
| 363 |
if(array!=null && array.length>0){ |
| 364 |
SiteR[] a2=new SiteR[array.length]; |
| 365 |
for(int i=0; i<a2.length; i++){ |
| 366 |
a2[i]=new SiteR(array[i]); |
| 367 |
if(i>0){a2[i-1].next=a2[i];} |
| 368 |
} |
| 369 |
return a2[0]; |
| 370 |
} |
| 371 |
return null; |
| 372 |
} |
| 373 |
|
| 374 |
private void writeList(ArrayList<Varlet> list){ |
| 375 |
assert(list!=null && list.size()>0); |
| 376 |
long key=key(list.get(0).chromosome, list.get(0).beginLoc); |
| 377 |
String fname=fname(key, outname); |
| 378 |
boolean allowSubprocess=false; |
| 379 |
OutputStream os=ReadWrite.getOutputStream(fname, true, true, allowSubprocess); |
| 380 |
PrintWriter pw=new PrintWriter(os); |
| 381 |
|
| 382 |
|
| 383 |
for(Varlet v : list){ |
| 384 |
pw.println(v.toText()); |
| 385 |
} |
| 386 |
ReadWrite.finishWriting(pw, os, fname, allowSubprocess); |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
private final class ProcessThread extends Thread { |
| 391 |
|
| 392 |
public ProcessThread(){ |
| 393 |
} |
| 394 |
|
| 395 |
private void fixReadSites(ArrayList<Read> reads){ |
| 396 |
assert(sitemap!=null); |
| 397 |
if(reads==null || reads.size()==0){return;} |
| 398 |
long max=-2; |
| 399 |
for(Read r : reads){ |
| 400 |
max=Tools.max(max, r.numericID); |
| 401 |
} |
| 402 |
synchronized(sitemap){ |
| 403 |
if(max>=maxSiteRead){ |
| 404 |
readSites(sitesTextFile, max); |
| 405 |
} |
| 406 |
for(Read r : reads){ |
| 407 |
{ |
| 408 |
long key=r.numericID; |
| 409 |
if((r.pairnum()&1)==1){ |
| 410 |
key=-key; |
| 411 |
assert(key<0); |
| 412 |
} |
| 413 |
SiteR head=sitemap.get(key); |
| 414 |
|
| 415 |
ArrayList<SiteScore> old=r.sites; |
| 416 |
r.sites=null; |
| 417 |
if(head!=null){ |
| 418 |
r.sites=new ArrayList<SiteScore>(); |
| 419 |
sitemap.remove(key); |
| 420 |
while(head!=null){ |
| 421 |
SiteScore ss=find(head, old); |
| 422 |
assert(ss!=null) : "\nCan't find sr "+head+" in read\n"+r+"\nlist:\n"+old; |
| 423 |
r.sites.add(ss); |
| 424 |
head=head.next; |
| 425 |
} |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
Read r2=r.mate; |
| 430 |
if(r2!=null){ |
| 431 |
long key=r2.numericID; |
| 432 |
if((r2.pairnum()&1)==1){ |
| 433 |
key=-key; |
| 434 |
assert(key<0); |
| 435 |
} |
| 436 |
SiteR head=sitemap.get(key); |
| 437 |
|
| 438 |
ArrayList<SiteScore> old=r2.sites; |
| 439 |
r2.sites=null; |
| 440 |
if(head!=null){ |
| 441 |
r2.sites=new ArrayList<SiteScore>(); |
| 442 |
sitemap.remove(key); |
| 443 |
while(head!=null){ |
| 444 |
SiteScore ss=find(head, old); |
| 445 |
assert(ss!=null) : "\nCan't find sr "+head+" in read\n"+r2+"\nlist:\n"+old; |
| 446 |
r2.sites.add(ss); |
| 447 |
} |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
} |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
@Override |
| 456 |
public void run(){ |
| 457 |
|
| 458 |
final boolean processReads=true; |
| 459 |
if(!processReads){System.err.println("Warning: Skipping read processing.");} |
| 460 |
|
| 461 |
if(cris!=null){ |
| 462 |
ListNum<Read> ln=cris.nextList(); |
| 463 |
ArrayList<Read> reads=(ln!=null ? ln.list : null); |
| 464 |
|
| 465 |
while(!terminate && reads!=null && reads.size()>0){ |
| 466 |
if(processReads){processReads(reads);} |
| 467 |
cris.returnList(ln); |
| 468 |
ln=cris.nextList(); |
| 469 |
reads=(ln!=null ? ln.list : null); |
| 470 |
} |
| 471 |
cris.returnList(ln); |
| 472 |
}else{ |
| 473 |
ArrayList<Read> reads=stream.nextList(); |
| 474 |
while(!terminate && reads!=null && reads.size()>0){ |
| 475 |
if(processReads){processReads(reads);} |
| 476 |
reads=stream.nextList(); |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
finished=true; |
| 481 |
synchronized(this){this.notifyAll();} |
| 482 |
} |
| 483 |
|
| 484 |
private void processReads(ArrayList<Read> reads){ |
| 485 |
if(sitemap==null){ |
| 486 |
for(Read r : reads){ |
| 487 |
Read r2=r.mate; |
| 488 |
assert(r2==null || r.mate.mate==r); |
| 489 |
|
| 490 |
if(r2==null){ |
| 491 |
processRead(r); |
| 492 |
}else{ |
| 493 |
if(!TOSS_SOLO1 || r.paired()){processRead(r);} |
| 494 |
if(!TOSS_SOLO2 || r2.paired()){processRead(r2);} |
| 495 |
} |
| 496 |
} |
| 497 |
}else{ |
| 498 |
fixReadSites(reads); |
| 499 |
|
| 500 |
for(Read r : reads){ |
| 501 |
Read r2=r.mate; |
| 502 |
assert(r2==null || r.mate.mate==r); |
| 503 |
|
| 504 |
if(r2==null){ |
| 505 |
multiprocessRead(r); |
| 506 |
}else{ |
| 507 |
if(!TOSS_SOLO1 || r.paired()){multiprocessRead(r);} |
| 508 |
if(!TOSS_SOLO2 || r2.paired()){multiprocessRead(r2);} |
| 509 |
} |
| 510 |
} |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
private void multiprocessRead(Read r){ |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
if(r.numSites()==0){return;} |
| 519 |
|
| 520 |
readsProcessed++; |
| 521 |
for(SiteScore ss : r.sites){ |
| 522 |
r.clearSite(); |
| 523 |
r.setFromSite(ss); |
| 524 |
r.match=null; |
| 525 |
|
| 526 |
r.setPaired(ss.pairedScore>0); |
| 527 |
r.setPerfect(ss.perfect); |
| 528 |
r.setRescued(ss.rescued); |
| 529 |
|
| 530 |
processRead(r); |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
private SiteScore find(SiteScoreR ssr, ArrayList<SiteScore> list) { |
| 540 |
for(SiteScore ss : list){ |
| 541 |
if(ssr.equals(ss)){return ss;} |
| 542 |
} |
| 543 |
return null; |
| 544 |
} |
| 545 |
|
| 546 |
private SiteScore find(SiteR sr, ArrayList<SiteScore> list) { |
| 547 |
for(SiteScore ss : list){ |
| 548 |
if(sr.equals(ss)){return ss;} |
| 549 |
} |
| 550 |
return null; |
| 551 |
} |
| 552 |
|
| 553 |
|
| 554 |
private void processRead(Read r){ |
| 555 |
sitesProcessed++; |
| 556 |
|
| 557 |
assert(r.numericID<Integer.MAX_VALUE) : r.toText(false); |
| 558 |
|
| 559 |
boolean flag=false; |
| 560 |
if(false && ( r.numericID==42829556) ){ |
| 561 |
System.err.println("Processing read:"); |
| 562 |
System.err.println("\n"+r.toText(false)); |
| 563 |
System.err.println("\n"+r.strand()); |
| 564 |
System.err.println("\n"); |
| 565 |
System.err.println(new String(r.bases)); |
| 566 |
System.err.println(r.match==null ? "null" : new String(r.match)); |
| 567 |
System.err.println("\n"); |
| 568 |
tcr.verbose=true; |
| 569 |
flag=true; |
| 570 |
System.err.println("Mapped Length: "+(r.stop-r.start+1)); |
| 571 |
} |
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
assert((r.chrom>=1)==r.mapped()) : r.toText(false); |
| 582 |
if(!r.mapped()){ |
| 583 |
assert(r.sites==null || r.sites.isEmpty()) : r.toText(false); |
| 584 |
return; |
| 585 |
} |
| 586 |
if(r.invalid()){return;} |
| 587 |
|
| 588 |
if(r.match!=null){ |
| 589 |
if(r.perfect()){ |
| 590 |
assert(TranslateColorspaceRead.perfectMatch(r.match)); |
| 591 |
return; |
| 592 |
}else if(TranslateColorspaceRead.perfectMatch(r.match)){ |
| 593 |
return; |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
assert(r.numericID<Integer.MAX_VALUE) : r.toText(false); |
| 598 |
|
| 599 |
if(flag){ |
| 600 |
System.err.println("r.match = "+(r.match==null ? null : new String(r.match))); |
| 601 |
System.err.println("Mapped Length: "+(r.stop-r.start+1)); |
| 602 |
} |
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
if(r.match==null){ |
| 615 |
if(flag){ |
| 616 |
System.err.println("realigning match string"); |
| 617 |
System.err.println("Mapped Length: "+(r.stop-r.start+1)); |
| 618 |
} |
| 619 |
tcr.realign_new(r, 20, true, 0, false); |
| 620 |
if(TranslateColorspaceRead.perfectMatch(r.match)){return;} |
| 621 |
if(flag){ |
| 622 |
System.err.println("new match string:\n"+(r.match==null ? null : new String(r.match))); |
| 623 |
System.err.println("Mapped Length: "+(r.stop-r.start+1)); |
| 624 |
} |
| 625 |
} |
| 626 |
assert(r.numericID<Integer.MAX_VALUE) : r.toText(false); |
| 627 |
r.errors=r.estimateErrors(); |
| 628 |
assert(r.numericID<Integer.MAX_VALUE) : r.toText(false); |
| 629 |
|
| 630 |
if(r.match==null){ |
| 631 |
System.err.println("Could not align read "+r.numericID); |
| 632 |
return; |
| 633 |
}else if(r.match[0]=='X'){ |
| 634 |
System.err.println("Could not align read "+r.numericID+": "+new String(r.match)); |
| 635 |
return; |
| 636 |
} |
| 637 |
|
| 638 |
assert(r.numericID<Integer.MAX_VALUE) : r.toText(false); |
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
ArrayList<Varlet> vars=tcr.toVars(r, CONDENSE, CONDENSE_SNPS, SPLIT_SUBS); |
| 643 |
|
| 644 |
if(vars==null){return;} |
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
char[] nearest=(nearestDefinedBase == null ? null : nearestDefinedBase[r.chrom]); |
| 661 |
CoverageArray ca=(pcov==null ? null : pcov[r.chrom]); |
| 662 |
|
| 663 |
for(Varlet v : vars){ |
| 664 |
if(v.endDist>=MIN_END_DIST){ |
| 665 |
assert(v.numUniqueReads==1); |
| 666 |
assert(v.numSemiUniqueReads==1); |
| 667 |
assert(v.numPlusReads1+v.numMinusReads1+v.numPlusReads2+v.numMinusReads2==1); |
| 668 |
assert(v.numReads>=1); |
| 669 |
|
| 670 |
assert(v.numReads==r.copies); |
| 671 |
assert(v.readLen==r.length()); |
| 672 |
|
| 673 |
boolean retain=true; |
| 674 |
if(maxDistFromDefined>=0 && v.varType==Variation.NOREF){ |
| 675 |
char dist=(maxDistFromDefined==0 ? 1 : Tools.min(nearest[v.beginLoc], nearest[v.endLoc])); |
| 676 |
if(dist>maxDistFromDefined){retain=false;} |
| 677 |
} |
| 678 |
|
| 679 |
if(retain && v.varType!=Variation.NOREF && ca!=null){ |
| 680 |
boolean toss=true; |
| 681 |
assert(PCOV_TIP_DIST>0); |
| 682 |
for(int j=v.beginLoc-PCOV_TIP_DIST; toss && j<=v.endLoc+PCOV_TIP_DIST; j++){ |
| 683 |
toss=ca.get(j)>=MIN_PCOV_DEPTH_TO_TOSS; |
| 684 |
} |
| 685 |
if(toss){retain=false;} |
| 686 |
} |
| 687 |
|
| 688 |
if(retain){ |
| 689 |
varsMade++; |
| 690 |
if(v.varType==Variation.NOREF){norefsMade++;} |
| 691 |
else if(v.varType==Variation.SNP){snpMade++;} |
| 692 |
else if(v.varType==Variation.DEL){delMade++;} |
| 693 |
else if(v.varType==Variation.INS){insMade++;} |
| 694 |
else if(v.varType==Variation.DELINS){ |
| 695 |
int a=v.lengthRef(); |
| 696 |
int b=v.lengthVar(); |
| 697 |
if(a==b){subnMade++;} |
| 698 |
else if(a>b){subdMade++;} |
| 699 |
else{subiMade++;} |
| 700 |
} |
| 701 |
deltaLen+=v.lengthDif(); |
| 702 |
addVar(v); |
| 703 |
} |
| 704 |
|
| 705 |
} |
| 706 |
} |
| 707 |
|
| 708 |
} |
| 709 |
|
| 710 |
|
| 711 |
private void addVar(Varlet v){ |
| 712 |
long key=key(v.chromosome, v.beginLoc); |
| 713 |
ArrayList<Varlet> list=keymap.get(key); |
| 714 |
assert(list!=null) : "\nCan't find "+key+" in "+keymap.keySet()+"\n"; |
| 715 |
synchronized(list){ |
| 716 |
list.add(v); |
| 717 |
if(list.size()>=WRITE_BUFFER){ |
| 718 |
|
| 719 |
if(MERGE_EQUAL_VARLETS){ |
| 720 |
mergeEqualVarlets(list); |
| 721 |
}else{ |
| 722 |
Shared.sort(list); |
| 723 |
} |
| 724 |
|
| 725 |
writeList(list); |
| 726 |
list.clear(); |
| 727 |
} |
| 728 |
} |
| 729 |
} |
| 730 |
|
| 731 |
private void mergeEqualVarlets(ArrayList<Varlet> vars){ |
| 732 |
|
| 733 |
Shared.sort(vars); |
| 734 |
ArrayList<Varlet> list=new ArrayList<Varlet>(8); |
| 735 |
for(int i=0; i<vars.size(); i++){ |
| 736 |
Varlet a=vars.get(i); |
| 737 |
vars.set(i, null); |
| 738 |
Varlet b=(list.isEmpty() ? null : list.get(0)); |
| 739 |
if(b==null || a.equals(b)){ |
| 740 |
list.add(a); |
| 741 |
}else{ |
| 742 |
Varlet c=StackVariations.mergeEqualVarlets(list); |
| 743 |
vars.set(i-1, c); |
| 744 |
list.clear(); |
| 745 |
list.add(a); |
| 746 |
} |
| 747 |
} |
| 748 |
if(!list.isEmpty()){ |
| 749 |
Varlet c=StackVariations.mergeEqualVarlets(list); |
| 750 |
vars.set(list.size()-1, c); |
| 751 |
} |
| 752 |
Tools.condenseStrict(vars); |
| 753 |
} |
| 754 |
|
| 755 |
protected boolean finished(){return finished;} |
| 756 |
protected void terminate(){terminate=true;} |
| 757 |
|
| 758 |
private final TranslateColorspaceRead tcr=new TranslateColorspaceRead(PAC_BIO_MODE ? |
| 759 |
new MultiStateAligner9PacBio(ALIGN_ROWS, ALIGN_COLUMNS) : new MultiStateAligner9ts(ALIGN_ROWS, ALIGN_COLUMNS)); |
| 760 |
private boolean finished=false; |
| 761 |
private boolean terminate=false; |
| 762 |
private long varsMade=0; |
| 763 |
private long norefsMade=0; |
| 764 |
private long snpMade=0; |
| 765 |
private long delMade=0; |
| 766 |
private long subnMade=0; |
| 767 |
private long subdMade=0; |
| 768 |
private long subiMade=0; |
| 769 |
private long insMade=0; |
| 770 |
private long deltaLen=0; |
| 771 |
private long sitesProcessed=0; |
| 772 |
private long readsProcessed=0; |
| 773 |
|
| 774 |
|
| 775 |
} |
| 776 |
|
| 777 |
|
| 778 |
protected static final long key(int chrom, int start){ |
| 779 |
long k=((long)chrom<<32)+(Tools.max(start, 0))/BLOCKSIZE; |
| 780 |
return k; |
| 781 |
} |
| 782 |
|
| 783 |
protected static final long[] keys(final int chrom){ |
| 784 |
int lim=(Data.chromLengths[chrom]+1000)/BLOCKSIZE; |
| 785 |
long[] keys=new long[lim+1]; |
| 786 |
for(int i=0; i<=lim; i++){ |
| 787 |
long key=key(chrom, i*BLOCKSIZE); |
| 788 |
keys[i]=key; |
| 789 |
} |
| 790 |
return keys; |
| 791 |
} |
| 792 |
|
| 793 |
protected static final String fname(long key, String outname){ |
| 794 |
if(outname==null){outname="GV2TempFile_#.txt";} |
| 795 |
assert(outname.contains("#")) : outname; |
| 796 |
assert(!outname.endsWith(".gz") && !outname.endsWith(".zip") && !outname.endsWith(".bz2")) : outname; |
| 797 |
return outname.replace("#", "b"+Data.GENOME_BUILD+"_"+key); |
| 798 |
} |
| 799 |
|
| 800 |
private final void makeKeyMap(){ |
| 801 |
final String header=Varlet.textHeader()+"\n"; |
| 802 |
keymap=new HashMap<Long, ArrayList<Varlet>>(); |
| 803 |
for(int chrom=1; chrom<=Data.numChroms; chrom++){ |
| 804 |
long[] keys=keys(chrom); |
| 805 |
for(long key : keys){ |
| 806 |
keymap.put(key, new ArrayList<Varlet>(WRITE_BUFFER)); |
| 807 |
ReadWrite.writeString(header, fname(key, outname), false); |
| 808 |
} |
| 809 |
} |
| 810 |
} |
| 811 |
|
| 812 |
private HashMap<Long, ArrayList<Varlet>> keymap; |
| 813 |
private final char[][] nearestDefinedBase; |
| 814 |
private final int maxDistFromDefined; |
| 815 |
|
| 816 |
private final CoverageArray[] pcov; |
| 817 |
|
| 818 |
public final String outname; |
| 819 |
public final String sitesfile; |
| 820 |
private TextFile sitesTextFile; |
| 821 |
private static long maxSiteRead=-1; |
| 822 |
private static long maxSiteTableSize=-1; |
| 823 |
|
| 824 |
private static long sitesLoaded=0; |
| 825 |
private static long sitesRetained=0; |
| 826 |
private static long linesLoaded=0; |
| 827 |
private static long linesRetained=0; |
| 828 |
|
| 829 |
private HashMap<Long, SiteR> sitemap=new HashMap<Long, SiteR>(4096); |
| 830 |
private final RTextInputStream stream; |
| 831 |
private final ConcurrentLegacyReadInputStream cris; |
| 832 |
|
| 833 |
public static boolean USE_CRIS=true; |
| 834 |
|
| 835 |
public static int THREADS=Shared.LOGICAL_PROCESSORS; |
| 836 |
public static int WRITE_BUFFER=16000; |
| 837 |
|
| 838 |
public static boolean CONDENSE=true; |
| 839 |
public static boolean CONDENSE_SNPS=true; |
| 840 |
public static boolean SPLIT_SUBS=false; |
| 841 |
|
| 842 |
public static boolean TOSS_SOLO1=false; |
| 843 |
public static boolean TOSS_SOLO2=false; |
| 844 |
|
| 845 |
public static boolean MERGE_EQUAL_VARLETS=false; |
| 846 |
public static boolean PAC_BIO_MODE=true; |
| 847 |
public static int ALIGN_ROWS=2020; |
| 848 |
public static int ALIGN_COLUMNS=3000; |
| 849 |
|
| 850 |
public static long MAX_READS=-1; |
| 851 |
public static int MIN_END_DIST=4; |
| 852 |
public static int BLOCKSIZE=1000000; |
| 853 |
|
| 854 |
public static int MIN_PCOV_DEPTH_TO_TOSS=2; |
| 855 |
|
| 856 |
|
| 857 |
public static int PCOV_TIP_DIST=8; |
| 858 |
|
| 859 |
} |