| 1 |
package jgi; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.PrintStream; |
| 5 |
import java.util.ArrayList; |
| 6 |
import java.util.Collections; |
| 7 |
|
| 8 |
import dna.AminoAcid; |
| 9 |
import fileIO.ByteFile; |
| 10 |
import fileIO.ByteFile1; |
| 11 |
import fileIO.ByteFile2; |
| 12 |
import fileIO.ByteStreamWriter; |
| 13 |
import fileIO.FileFormat; |
| 14 |
import fileIO.ReadWrite; |
| 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 shared.TrimRead; |
| 22 |
import stream.ConcurrentGenericReadInputStream; |
| 23 |
import stream.ConcurrentReadInputStream; |
| 24 |
import stream.ConcurrentReadOutputStream; |
| 25 |
import stream.FASTQ; |
| 26 |
import stream.FastaReadInputStream; |
| 27 |
import stream.Read; |
| 28 |
import stream.SamLine; |
| 29 |
import structures.ByteBuilder; |
| 30 |
import structures.ListNum; |
| 31 |
import structures.LongPair; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class FindHiCJunctions { |
| 39 |
|
| 40 |
public static void main(String[] args){ |
| 41 |
|
| 42 |
Timer t=new Timer(); |
| 43 |
|
| 44 |
|
| 45 |
FindHiCJunctions x=new FindHiCJunctions(args); |
| 46 |
|
| 47 |
|
| 48 |
x.process(t); |
| 49 |
|
| 50 |
|
| 51 |
Shared.closeStream(x.outstream); |
| 52 |
} |
| 53 |
|
| 54 |
public FindHiCJunctions(String[] args){ |
| 55 |
|
| 56 |
{ |
| 57 |
PreParser pp=new PreParser(args, getClass(), false); |
| 58 |
args=pp.args; |
| 59 |
outstream=pp.outstream; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
Shared.capBuffers(4); |
| 64 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 65 |
ReadWrite.setZipThreads(Shared.threads()); |
| 66 |
FASTQ.TEST_INTERLEAVED=FASTQ.FORCE_INTERLEAVED=false; |
| 67 |
|
| 68 |
Parser parser=new Parser(); |
| 69 |
for(int i=0; i<args.length; i++){ |
| 70 |
String arg=args[i]; |
| 71 |
String[] split=arg.split("="); |
| 72 |
String a=split[0].toLowerCase(); |
| 73 |
String b=split.length>1 ? split[1] : null; |
| 74 |
|
| 75 |
if(a.equals("verbose")){ |
| 76 |
verbose=Parse.parseBoolean(b); |
| 77 |
ByteFile1.verbose=verbose; |
| 78 |
ByteFile2.verbose=verbose; |
| 79 |
stream.FastaReadInputStream.verbose=verbose; |
| 80 |
ConcurrentGenericReadInputStream.verbose=verbose; |
| 81 |
stream.FastqReadInputStream.verbose=verbose; |
| 82 |
ReadWrite.verbose=verbose; |
| 83 |
}else if(a.equals("minclip")){ |
| 84 |
minClipLength=Integer.parseInt(b); |
| 85 |
}else if(a.equals("printkmers") || a.equals("printcounts")){ |
| 86 |
printKmers=Parse.parseBoolean(b); |
| 87 |
}else if(a.equals("junctionfile") || a.equals("junctions") || a.equals("outk")){ |
| 88 |
junctionFile=b; |
| 89 |
assert(b==null || b.indexOf('%')>0); |
| 90 |
}else if(parser.parse(arg, a, b)){ |
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
else if(parser.in1==null && i==0 && Tools.looksLikeInputStream(arg)){ |
| 95 |
parser.in1=arg; |
| 96 |
}else if(parser.out1==null && i==1 && !arg.contains("=")){ |
| 97 |
parser.out1=arg; |
| 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=parser.overwrite; |
| 111 |
append=parser.append; |
| 112 |
|
| 113 |
in1=parser.in1; |
| 114 |
|
| 115 |
out1=parser.out1; |
| 116 |
|
| 117 |
extin=parser.extin; |
| 118 |
extout=parser.extout; |
| 119 |
} |
| 120 |
|
| 121 |
assert(FastaReadInputStream.settingsOK()); |
| 122 |
|
| 123 |
if(in1==null){throw new RuntimeException("Error - at least one input file is required.");} |
| 124 |
if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2){ |
| 125 |
ByteFile.FORCE_MODE_BF2=false; |
| 126 |
ByteFile.FORCE_MODE_BF1=true; |
| 127 |
} |
| 128 |
|
| 129 |
if(out1!=null && out1.equalsIgnoreCase("null")){out1=null;} |
| 130 |
|
| 131 |
if(!Tools.testOutputFiles(overwrite, append, false, out1)){ |
| 132 |
outstream.println((out1==null)+", "+out1); |
| 133 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out1+"\n"); |
| 134 |
} |
| 135 |
|
| 136 |
ffout1=FileFormat.testOutput(out1, FileFormat.FASTQ, extout, true, overwrite, append, false); |
| 137 |
|
| 138 |
ffin1=FileFormat.testInput(in1, FileFormat.FASTQ, extin, true, true); |
| 139 |
|
| 140 |
counts=new long[11][]; |
| 141 |
counts[10]=new long[1024*1024]; |
| 142 |
counts[8]=new long[256*256]; |
| 143 |
counts[6]=new long[4096]; |
| 144 |
counts[4]=new long[256]; |
| 145 |
|
| 146 |
leftCounts=new long[6][]; |
| 147 |
rightCounts=new long[6][]; |
| 148 |
leftCounts[5]=new long[1024]; |
| 149 |
leftCounts[4]=new long[256]; |
| 150 |
leftCounts[3]=new long[64]; |
| 151 |
leftCounts[2]=new long[16]; |
| 152 |
rightCounts[5]=new long[1024]; |
| 153 |
rightCounts[4]=new long[256]; |
| 154 |
rightCounts[3]=new long[64]; |
| 155 |
rightCounts[2]=new long[16]; |
| 156 |
} |
| 157 |
|
| 158 |
void process(Timer t){ |
| 159 |
|
| 160 |
final ConcurrentReadInputStream cris; |
| 161 |
{ |
| 162 |
cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffin1, null, null, null); |
| 163 |
cris.start(); |
| 164 |
if(verbose){outstream.println("Started cris");} |
| 165 |
} |
| 166 |
|
| 167 |
final ConcurrentReadOutputStream ros; |
| 168 |
if(out1!=null){ |
| 169 |
final int buff=4; |
| 170 |
|
| 171 |
assert(!out1.equalsIgnoreCase(in1) && !out1.equalsIgnoreCase(in1)) : "Input file and output file have same name."; |
| 172 |
|
| 173 |
ros=ConcurrentReadOutputStream.getStream(ffout1, null, null, null, buff, null, false); |
| 174 |
ros.start(); |
| 175 |
}else{ros=null;} |
| 176 |
|
| 177 |
long readsProcessed=0, readsOut=0; |
| 178 |
long basesProcessed=0, basesOut=0; |
| 179 |
|
| 180 |
{ |
| 181 |
|
| 182 |
ListNum<Read> ln=cris.nextList(); |
| 183 |
ArrayList<Read> reads=(ln!=null ? ln.list : null); |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
if(reads!=null && !reads.isEmpty()){ |
| 188 |
Read r=reads.get(0); |
| 189 |
assert((ffin1==null || ffin1.samOrBam()) || (r.mate!=null)==cris.paired()); |
| 190 |
} |
| 191 |
|
| 192 |
while(ln!=null && reads!=null && reads.size()>0){ |
| 193 |
|
| 194 |
final ArrayList<Read> listOut=new ArrayList<Read>(reads.size()); |
| 195 |
for(int idx=0; idx<reads.size(); idx++){ |
| 196 |
final Read r1=reads.get(idx); |
| 197 |
|
| 198 |
final int initialLength1=r1.length(); |
| 199 |
final boolean keep=process(r1, r1.samline); |
| 200 |
if(keep || true){ |
| 201 |
listOut.add(r1); |
| 202 |
readsOut++; |
| 203 |
basesOut+=r1.length(); |
| 204 |
} |
| 205 |
|
| 206 |
readsProcessed++; |
| 207 |
basesProcessed+=initialLength1; |
| 208 |
} |
| 209 |
|
| 210 |
if(ros!=null){ros.add(listOut, ln.id);} |
| 211 |
|
| 212 |
cris.returnList(ln); |
| 213 |
ln=cris.nextList(); |
| 214 |
reads=(ln!=null ? ln.list : null); |
| 215 |
} |
| 216 |
if(ln!=null){ |
| 217 |
cris.returnList(ln.id, ln.list==null || ln.list.isEmpty()); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
errorState|=ReadWrite.closeStreams(cris, ros); |
| 222 |
|
| 223 |
if(printKmers){ |
| 224 |
printKmers(10, counts[10], ""); |
| 225 |
printKmers(8, counts[8], ""); |
| 226 |
printKmers(6, counts[6], ""); |
| 227 |
printKmers(4, counts[4], ""); |
| 228 |
|
| 229 |
printKmers(5, leftCounts[5], "L"); |
| 230 |
printKmers(4, leftCounts[4], "L"); |
| 231 |
printKmers(3, leftCounts[3], "L"); |
| 232 |
|
| 233 |
printKmers(5, rightCounts[5], "R"); |
| 234 |
printKmers(4, rightCounts[4], "R"); |
| 235 |
printKmers(3, rightCounts[3], "R"); |
| 236 |
} |
| 237 |
|
| 238 |
t.stop(); |
| 239 |
outstream.println(Tools.timeReadsBasesProcessed(t, readsProcessed, basesProcessed, 8)); |
| 240 |
outstream.println(Tools.readsBasesOut(readsProcessed, basesProcessed, readsOut, basesOut, 8, false)); |
| 241 |
|
| 242 |
if(errorState){ |
| 243 |
throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt."); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
private void printKmers(int k, long[] array, String direction){ |
| 248 |
boolean tsv=junctionFile.endsWith(".tsv") || junctionFile.endsWith(".tsv.gz"); |
| 249 |
final String fname=junctionFile.replaceFirst("%", k+direction); |
| 250 |
final ByteStreamWriter bsw=new ByteStreamWriter(fname, true, false, false); |
| 251 |
bsw.start(); |
| 252 |
final long sum=shared.Vector.sum(array); |
| 253 |
final double mult=1.0/(Tools.max(1, sum)); |
| 254 |
final long thresh=Tools.max((2*sum)/array.length, (long)Math.ceil(Tools.max(minCount, minFraction*sum))); |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
ArrayList<LongPair> list=new ArrayList<LongPair>(); |
| 260 |
for(int kmer=0; kmer<array.length; kmer++){ |
| 261 |
final long count=array[kmer]; |
| 262 |
if(count>=thresh){ |
| 263 |
list.add(new LongPair(count, kmer)); |
| 264 |
} |
| 265 |
} |
| 266 |
Collections.sort(list); |
| 267 |
Collections.reverse(list); |
| 268 |
|
| 269 |
|
| 270 |
ByteBuilder bb=new ByteBuilder(4200); |
| 271 |
for(LongPair pair : list){ |
| 272 |
if(tsv){ |
| 273 |
bb.append(AminoAcid.kmerToString(pair.b, k)).append('\t'); |
| 274 |
bb.append(pair.a).append('\t').append(mult*pair.a, 5).append('\n'); |
| 275 |
}else{ |
| 276 |
bb.append('>').append(pair.a).append('\t').append(mult*pair.a, 5).append('\n'); |
| 277 |
bb.append(AminoAcid.kmerToString(pair.b, k)).append('\n'); |
| 278 |
} |
| 279 |
if(bb.length()>4096){ |
| 280 |
bsw.print(bb); |
| 281 |
bb.clear(); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
if(!bb.isEmpty()){ |
| 286 |
bsw.print(bb); |
| 287 |
} |
| 288 |
errorState|=bsw.poisonAndWait(); |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
private boolean process(Read r, SamLine sl){ |
| 294 |
|
| 295 |
if(sl==null || !sl.mapped() || !sl.primary() || sl.supplementary()|| r.match==null || !r.containsNonNM()){ |
| 296 |
return false; |
| 297 |
} |
| 298 |
|
| 299 |
boolean definiteJunction=(sl.nextMapped() && !sl.properPair()); |
| 300 |
|
| 301 |
|
| 302 |
final byte[] bases=r.bases; |
| 303 |
final boolean rcomp=(r.strand()==Shared.MINUS); |
| 304 |
final int left, right; |
| 305 |
{ |
| 306 |
if(r.shortmatch()){r.toLongMatchString(true);} |
| 307 |
int left0=SamLine.countLeadingClip(r.match); |
| 308 |
int right0=SamLine.countTrailingClip(r.match); |
| 309 |
if(left0==0 && right0==0){ |
| 310 |
byte[] smatch=softClipMatch(r.match, minClipLength, true); |
| 311 |
left0=SamLine.countLeadingClip(smatch); |
| 312 |
right0=SamLine.countTrailingClip(smatch); |
| 313 |
} |
| 314 |
left=left0; |
| 315 |
right=right0; |
| 316 |
} |
| 317 |
|
| 318 |
if((left>1 && right>1) || (left<minClipLength && right<minClipLength)){return false;} |
| 319 |
|
| 320 |
final int pos=(left>right ? left-1 : bases.length-right-1); |
| 321 |
if(printKmers && definiteJunction && junctionFile!=null){ |
| 322 |
if(rcomp){r.reverseComplement();} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
for(int k=10, half=5, start=pos-4; k>=4; k-=2, half--, start++){ |
| 328 |
int kmer=0; |
| 329 |
assert(start>=0) : left+", "+right+", "+pos+", "+start+", "+k+", "+bases.length+"\n"+new String(r.match); |
| 330 |
for(int i=start, len=0; len<k; i++, len++){ |
| 331 |
final byte b=bases[i]; |
| 332 |
final int num=AminoAcid.baseToNumber[b]; |
| 333 |
kmer=(kmer<<2)|num; |
| 334 |
} |
| 335 |
if(kmer<0){return false;} |
| 336 |
|
| 337 |
counts[k][kmer]++; |
| 338 |
leftCounts[half][kmer>>k]++; |
| 339 |
rightCounts[half][kmer&~((-1)<<k)]++; |
| 340 |
} |
| 341 |
|
| 342 |
if(rcomp){r.reverseComplement();} |
| 343 |
} |
| 344 |
|
| 345 |
if(trim){ |
| 346 |
int trimAmount=bases.length-pos-1; |
| 347 |
int remainingAmount=bases.length-trimAmount; |
| 348 |
if(remainingAmount>=minTrimLength){ |
| 349 |
if(rcomp){ |
| 350 |
TrimRead.trimByAmount(r, trimAmount, 0, 1, false); |
| 351 |
}else{ |
| 352 |
TrimRead.trimByAmount(r, 0, trimAmount, 1, false); |
| 353 |
} |
| 354 |
}else if(trimAmount>=minTrimLength){ |
| 355 |
if(rcomp){ |
| 356 |
TrimRead.trimByAmount(r, 0, remainingAmount, 1, false); |
| 357 |
}else{ |
| 358 |
TrimRead.trimByAmount(r, remainingAmount, 0, 1, false); |
| 359 |
} |
| 360 |
}else{ |
| 361 |
|
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
return true; |
| 366 |
} |
| 367 |
|
| 368 |
public static byte[] softClipMatch(byte[] match, int minClipLength, boolean allowMutation){ |
| 369 |
|
| 370 |
final int matchScore=100; |
| 371 |
final int subScore=-200; |
| 372 |
final int subScore2=-100; |
| 373 |
final int insScore=-200; |
| 374 |
final int delScore=-200; |
| 375 |
final int delScore2=-10; |
| 376 |
final int clipScore=-1; |
| 377 |
final int nScore=1; |
| 378 |
|
| 379 |
int insCount=0; |
| 380 |
int delCount=0; |
| 381 |
|
| 382 |
long score=0; |
| 383 |
long maxScore=0; |
| 384 |
int maxPos=-1; |
| 385 |
int maxStart=-1; |
| 386 |
int currentStart=-1; |
| 387 |
byte current='?'; |
| 388 |
|
| 389 |
for(int mpos=0; mpos<match.length; mpos++){ |
| 390 |
final byte m=match[mpos]; |
| 391 |
|
| 392 |
|
| 393 |
if(m=='m' || m=='N' || m=='R'){ |
| 394 |
if(score==0){currentStart=mpos;} |
| 395 |
|
| 396 |
score=score+(m=='m' ? matchScore : nScore); |
| 397 |
|
| 398 |
if(score>maxScore){ |
| 399 |
maxScore=score; |
| 400 |
maxPos=mpos; |
| 401 |
maxStart=currentStart; |
| 402 |
} |
| 403 |
}else{ |
| 404 |
if(m=='S' || m=='s'){ |
| 405 |
score=score+(m==current ? subScore2 : subScore); |
| 406 |
}else if(m=='D'){ |
| 407 |
score=score+(m==current ? delScore2 : delScore); |
| 408 |
delCount++; |
| 409 |
}else if(m=='I' || m=='X' || m=='Y'){ |
| 410 |
score=score+insScore; |
| 411 |
insCount++; |
| 412 |
}else if(m=='C'){ |
| 413 |
score=score+clipScore; |
| 414 |
} |
| 415 |
score=Tools.max(0, score); |
| 416 |
} |
| 417 |
current=m; |
| 418 |
} |
| 419 |
|
| 420 |
if(maxScore<1){return match;} |
| 421 |
final int leftClipM=maxStart; |
| 422 |
final int rightClipM=(match.length-maxPos-1); |
| 423 |
int leftClip=0, rightClip=0; |
| 424 |
for(int i=0; i<match.length; i++){ |
| 425 |
byte m=match[i]; |
| 426 |
if(i<maxStart){ |
| 427 |
leftClip+=(m=='D' ? 0 : 1); |
| 428 |
}else if(i>maxPos){ |
| 429 |
rightClip+=(m=='D' ? 0 : 1); |
| 430 |
} |
| 431 |
} |
| 432 |
if(leftClip<minClipLength && rightClip<minClipLength){return match;} |
| 433 |
if(delCount==0){ |
| 434 |
final byte[] array=allowMutation ? match : match.clone(); |
| 435 |
for(int i=0; i<leftClip; i++){array[i]='C';} |
| 436 |
for(int i=0, j=array.length-1; i<rightClip; i++, j--){array[j]='C';} |
| 437 |
return array; |
| 438 |
} |
| 439 |
|
| 440 |
ByteBuilder bb=new ByteBuilder(match.length); |
| 441 |
if(leftClip>=minClipLength){ |
| 442 |
for(int mpos=0, processed=0; mpos<match.length; mpos++){ |
| 443 |
byte m=match[mpos]; |
| 444 |
if(m=='D'){ |
| 445 |
if(mpos>=leftClipM){bb.append(m);} |
| 446 |
}else{ |
| 447 |
bb.append(mpos<leftClipM ? (byte)'C' : m); |
| 448 |
processed++; |
| 449 |
} |
| 450 |
} |
| 451 |
}else{ |
| 452 |
bb.append(match); |
| 453 |
} |
| 454 |
if(rightClip>=minClipLength){ |
| 455 |
bb.reverseInPlace(); |
| 456 |
byte[] temp=bb.toBytes(); |
| 457 |
bb.clear(); |
| 458 |
for(int mpos=0, processed=0; mpos<temp.length; mpos++){ |
| 459 |
byte m=temp[mpos]; |
| 460 |
if(m=='D'){ |
| 461 |
if(mpos>=rightClipM){bb.append(m);} |
| 462 |
}else{ |
| 463 |
bb.append(mpos<rightClipM ? (byte)'C' : m); |
| 464 |
processed++; |
| 465 |
} |
| 466 |
} |
| 467 |
bb.reverseInPlace(); |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
return bb.toBytes(); |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
private String in1=null; |
| 486 |
|
| 487 |
private String out1=null; |
| 488 |
|
| 489 |
private String extin=null; |
| 490 |
private String extout=null; |
| 491 |
|
| 492 |
private String junctionFile="junctions_k%.txt"; |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
private long maxReads=-1; |
| 497 |
private int minClipLength=8; |
| 498 |
private int minTrimLength=25; |
| 499 |
private int minCount=2; |
| 500 |
private float minFraction=0.0005f; |
| 501 |
|
| 502 |
boolean printKmers=true; |
| 503 |
boolean trim=true; |
| 504 |
private long[][] counts; |
| 505 |
private long[][] leftCounts; |
| 506 |
private long[][] rightCounts; |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
private final FileFormat ffin1; |
| 511 |
|
| 512 |
private final FileFormat ffout1; |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
private PrintStream outstream=System.err; |
| 518 |
public static boolean verbose=false; |
| 519 |
public boolean errorState=false; |
| 520 |
private boolean overwrite=true; |
| 521 |
private boolean append=false; |
| 522 |
|
| 523 |
} |