| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
package jgi; |
| 7 |
|
| 8 |
import static jgi.Dedupe.maxNmer; |
| 9 |
import static jgi.Dedupe.nmerIndex; |
| 10 |
|
| 11 |
import java.util.ArrayList; |
| 12 |
import java.util.Arrays; |
| 13 |
import java.util.List; |
| 14 |
import java.util.concurrent.ArrayBlockingQueue; |
| 15 |
|
| 16 |
import dna.AminoAcid; |
| 17 |
import fileIO.ByteStreamWriter; |
| 18 |
import fileIO.FileFormat; |
| 19 |
import fileIO.ReadWrite; |
| 20 |
import shared.Parse; |
| 21 |
import shared.Parser; |
| 22 |
import shared.PreParser; |
| 23 |
import shared.Shared; |
| 24 |
import shared.Timer; |
| 25 |
import shared.Tools; |
| 26 |
import stream.ConcurrentReadInputStream; |
| 27 |
import stream.Read; |
| 28 |
import structures.ByteBuilder; |
| 29 |
import structures.ListNum; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public class TetramerFrequencies { |
| 37 |
public static void main(String[] args){ |
| 38 |
|
| 39 |
System.out.println("Start Tetramer Frequencies analysis ..."); |
| 40 |
|
| 41 |
final int oldThreads=Shared.threads(); |
| 42 |
Shared.capThreads(16); |
| 43 |
|
| 44 |
Timer t=new Timer(); |
| 45 |
|
| 46 |
|
| 47 |
TetramerFrequencies x=new TetramerFrequencies(args); |
| 48 |
|
| 49 |
|
| 50 |
x.process(t); |
| 51 |
|
| 52 |
|
| 53 |
Shared.closeStream(x.outstream); |
| 54 |
|
| 55 |
Shared.setThreads(oldThreads); |
| 56 |
} |
| 57 |
|
| 58 |
public TetramerFrequencies(String[] args){ |
| 59 |
|
| 60 |
{ |
| 61 |
PreParser pp=new PreParser(args, getClass(), false); |
| 62 |
args=pp.args; |
| 63 |
outstream=pp.outstream; |
| 64 |
} |
| 65 |
|
| 66 |
int k_=4; |
| 67 |
Parser parser=new Parser(); |
| 68 |
for (String arg : args) { |
| 69 |
String[] split=arg.split("="); |
| 70 |
String a=split[0].toLowerCase(); |
| 71 |
String b=split.length>1 ? split[1] : null; |
| 72 |
|
| 73 |
if (a.equals("s") || a.equals("step")){ |
| 74 |
step = Integer.parseInt(b); |
| 75 |
} else if (a.equals("w") || a.equals("window")){ |
| 76 |
winSize = Integer.parseInt(b); |
| 77 |
} else if (a.equals("out") || a.equals("freq")){ |
| 78 |
out1 = b; |
| 79 |
} else if (a.equals("dropshort")){ |
| 80 |
keepShort=!Parse.parseBoolean(b); |
| 81 |
} else if (a.equals("keepshort") || a.equals("short")){ |
| 82 |
keepShort=Parse.parseBoolean(b); |
| 83 |
} else if (a.equals("k")){ |
| 84 |
k_ = Integer.parseInt(b); |
| 85 |
} else if(parser.parse(arg, a, b)){ |
| 86 |
|
| 87 |
} else { |
| 88 |
throw new RuntimeException("Unknown argument "+arg); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
{ |
| 93 |
Parser.processQuality(); |
| 94 |
|
| 95 |
maxReads=parser.maxReads; |
| 96 |
in1=parser.in1; |
| 97 |
} |
| 98 |
|
| 99 |
k=k_; |
| 100 |
ffin1=FileFormat.testInput(in1, FileFormat.FASTQ, null, true, true); |
| 101 |
assert(ffin1!=null) : "No input file."; |
| 102 |
assert(ffin1.exists() && ffin1.canRead()) : "Cannot read input file "+in1+"."; |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
if (out1==null || out1.equals("")){ |
| 107 |
out1 = "stdout"; |
| 108 |
} |
| 109 |
|
| 110 |
threads=Tools.max(1, Shared.threads()); |
| 111 |
inq=new ArrayBlockingQueue<Line>(threads+1); |
| 112 |
|
| 113 |
FileFormat ff=FileFormat.testOutput(out1, FileFormat.TEXT, null, true, true, false, true); |
| 114 |
bsw = new ByteStreamWriter(ff); |
| 115 |
bsw.start(); |
| 116 |
|
| 117 |
|
| 118 |
List<String> head = new ArrayList<String>(); |
| 119 |
head.add("scaffold"); |
| 120 |
head.add("range"); |
| 121 |
|
| 122 |
|
| 123 |
tetramerGen2(head); |
| 124 |
bsw.add(new ByteBuilder(String.join("\t", head)).append('\n'), nextID); |
| 125 |
nextID++; |
| 126 |
} |
| 127 |
|
| 128 |
void process(Timer t){ |
| 129 |
|
| 130 |
ArrayList<PrintThread> alpt=spawnThreads(); |
| 131 |
|
| 132 |
final ConcurrentReadInputStream cris; |
| 133 |
{ |
| 134 |
cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffin1, null); |
| 135 |
cris.start(); |
| 136 |
} |
| 137 |
|
| 138 |
long readsProcessed=0; |
| 139 |
{ |
| 140 |
ListNum<Read> ln=cris.nextList(); |
| 141 |
ArrayList<Read> reads=(ln!=null ? ln.list : null); |
| 142 |
|
| 143 |
if(reads!=null && !reads.isEmpty()){ |
| 144 |
Read r=reads.get(0); |
| 145 |
assert((ffin1==null || ffin1.samOrBam()) || (r.mate!=null)==cris.paired()); |
| 146 |
} |
| 147 |
|
| 148 |
while(ln!=null && reads!=null && reads.size()>0){ |
| 149 |
if(verbose){outstream.println("Fetched "+reads.size()+" reads.");} |
| 150 |
|
| 151 |
for (Read r1 : reads){ |
| 152 |
windowedTetramerProfile(r1.bases, r1.id); |
| 153 |
readsProcessed++; |
| 154 |
} |
| 155 |
|
| 156 |
cris.returnList(ln); |
| 157 |
if(verbose){ |
| 158 |
outstream.println("Returned a list."); |
| 159 |
} |
| 160 |
ln=cris.nextList(); |
| 161 |
reads=(ln!=null ? ln.list : null); |
| 162 |
} |
| 163 |
|
| 164 |
if(ln!=null){ |
| 165 |
cris.returnList(ln.id, ln.list==null || ln.list.isEmpty()); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
putLine(POISON_LINE); |
| 170 |
waitForFinish(alpt); |
| 171 |
|
| 172 |
|
| 173 |
bsw.poisonAndWait(); |
| 174 |
|
| 175 |
ReadWrite.closeStream(cris); |
| 176 |
if(verbose){outstream.println("Finished.");} |
| 177 |
|
| 178 |
t.stop(); |
| 179 |
outstream.println("Time: \t"+t); |
| 180 |
outstream.println("Reads Processed: "+readsProcessed+" \t"+Tools.format("%.2fk reads/sec", (readsProcessed/(double)(t.elapsed))*1000000)); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
private void windowedTetramerProfileOpt(byte[] bases, String header){ |
| 185 |
int sidx = 0; |
| 186 |
int eidx = winSize; |
| 187 |
|
| 188 |
final int[] counts = new int[maxNmer+1]; |
| 189 |
|
| 190 |
int leadlen = 0; |
| 191 |
int leadtmer = -1; |
| 192 |
int endlen = 0; |
| 193 |
int endtmer = -1; |
| 194 |
|
| 195 |
int tmer = 0; |
| 196 |
int len = 0; |
| 197 |
|
| 198 |
for (int i=0; i<= eidx; i++){ |
| 199 |
int x = AminoAcid.baseToNumber[bases[i]]; |
| 200 |
if (x < 0){ |
| 201 |
tmer = 0; |
| 202 |
len = 0; |
| 203 |
} else { |
| 204 |
tmer = (leadtmer<<2)|x ; |
| 205 |
len++; |
| 206 |
if (len >= k){ |
| 207 |
int idx = tmerIndex(tmer); |
| 208 |
counts[idx]++; |
| 209 |
|
| 210 |
if( leadtmer==-1){ |
| 211 |
leadtmer = tmer; |
| 212 |
} else if(i == eidx){ |
| 213 |
|
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
len--; |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
} |
| 225 |
|
| 226 |
private void windowedTetramerProfile(byte[] bases, String header){ |
| 227 |
int sidx = 0; |
| 228 |
int eidx = winSize<1 ? bases.length : Tools.min(bases.length, winSize); |
| 229 |
|
| 230 |
while (eidx <= bases.length){ |
| 231 |
Line line=new Line(header, bases, sidx, eidx, nextID); |
| 232 |
putLine(line); |
| 233 |
sidx+=windowsPerLine*step; |
| 234 |
eidx+=windowsPerLine*step; |
| 235 |
nextID++; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
void append(Line line, int[] counts, StringBuilder sb){ |
| 241 |
sb.append(line.header); |
| 242 |
sb.append('\t'); |
| 243 |
sb.append(line.sidx+1); |
| 244 |
sb.append('-'); |
| 245 |
sb.append(line.eidx); |
| 246 |
float mult=1f/(line.length()-k+1); |
| 247 |
for (int cnt: counts){ |
| 248 |
sb.append('\t'); |
| 249 |
sb.append(Tools.format("%.4f", cnt*mult)); |
| 250 |
} |
| 251 |
sb.append('\n'); |
| 252 |
} |
| 253 |
|
| 254 |
void append(Line line, int[] counts, ByteBuilder bb){ |
| 255 |
bb.append(line.header); |
| 256 |
bb.tab(); |
| 257 |
bb.append(line.sidx+1); |
| 258 |
bb.append('-'); |
| 259 |
bb.append(line.eidx); |
| 260 |
for (int cnt: counts){ |
| 261 |
bb.tab(); |
| 262 |
bb.append(cnt); |
| 263 |
} |
| 264 |
bb.nl(); |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
public int[] tetramerCounter(byte[] bases, int startidx, int endidx, int[] counts){ |
| 269 |
if (counts == null){ |
| 270 |
counts = new int[maxNmer+1]; |
| 271 |
} |
| 272 |
|
| 273 |
for (int i=startidx; i<=endidx-k; i++){ |
| 274 |
int kmer = 0; |
| 275 |
for (int j=0; j<k; j++){ |
| 276 |
int x = AminoAcid.baseToNumber[bases[i+j]]; |
| 277 |
kmer= (kmer<<2)|x ; |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
if (kmer > -1){ |
| 282 |
int idx = tmerIndex(kmer); |
| 283 |
counts[idx]++; |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
return counts; |
| 288 |
} |
| 289 |
|
| 290 |
private int tmerIndex(int tmer){ |
| 291 |
int rtmer = AminoAcid.reverseComplementBinaryFast(tmer, k); |
| 292 |
int mtmer = Tools.min(tmer, rtmer); |
| 293 |
int idx = -1; |
| 294 |
try{ |
| 295 |
idx = nmerIndex[rtmer]; |
| 296 |
} catch (ArrayIndexOutOfBoundsException e){ |
| 297 |
System.out.printf("ArrayIndexOutOfBoundsException tmer=%d: rtmer=%d; \n", tmer, rtmer); |
| 298 |
System.exit(-1); |
| 299 |
} |
| 300 |
return idx; |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
public List<String> tetramerGen(List<String> tlist){ |
| 305 |
if (tlist == null){ |
| 306 |
tlist = new ArrayList<String>(); |
| 307 |
} |
| 308 |
|
| 309 |
int[] bases = {0, 1, 2, 3}; |
| 310 |
|
| 311 |
for (int b1 : bases){ |
| 312 |
for (int b2 : bases){ |
| 313 |
for (int b3 : bases){ |
| 314 |
for (int b4 : bases){ |
| 315 |
int tcode = b1; |
| 316 |
tcode = (tcode<<2)|b2; |
| 317 |
tcode = (tcode<<2)|b3; |
| 318 |
tcode = (tcode<<2)|b4; |
| 319 |
String tmer = AminoAcid.kmerToString(tcode, k).toLowerCase(); |
| 320 |
String rtmer = AminoAcid.kmerToString(AminoAcid.reverseComplementBinaryFast(tcode, k), k).toLowerCase(); |
| 321 |
|
| 322 |
if (!tlist.contains(rtmer)){ |
| 323 |
tlist.add(tmer); |
| 324 |
} |
| 325 |
} |
| 326 |
} |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
return tlist; |
| 331 |
} |
| 332 |
|
| 333 |
public List<String> tetramerGen2(List<String> tlist){ |
| 334 |
if (tlist == null){ |
| 335 |
tlist = new ArrayList<String>(); |
| 336 |
} |
| 337 |
|
| 338 |
final int max=(1<<(2*k))-1; |
| 339 |
|
| 340 |
for(int i=0; i<=max; i++){ |
| 341 |
final int a=i, b=AminoAcid.reverseComplementBinaryFast(i, k); |
| 342 |
int min=Tools.min(a, b); |
| 343 |
if(min==a){ |
| 344 |
tlist.add(AminoAcid.kmerToString(a, k).toLowerCase()); |
| 345 |
} |
| 346 |
} |
| 347 |
return tlist; |
| 348 |
} |
| 349 |
|
| 350 |
public void printTetramerFromCode(long code){ |
| 351 |
System.out.println(AminoAcid.kmerToString(code, k)); |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
public static void printHelp(){ |
| 379 |
List<String> helplist = new ArrayList<String>(); |
| 380 |
helplist.add("Program Name : TetramerFrequencies v1.1"); |
| 381 |
helplist.add("Usage : "); |
| 382 |
helplist.add(" -h : this page"); |
| 383 |
helplist.add(" -s : step [500]"); |
| 384 |
helplist.add(" -w : window size [2000]. If set to 0 the whole sequence is processed"); |
| 385 |
System.out.println(String.join("\n", helplist)); |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
final Line takeLine(){ |
| 391 |
Line line=null; |
| 392 |
while(line==null){ |
| 393 |
try { |
| 394 |
line=inq.take(); |
| 395 |
} catch (InterruptedException e) { |
| 396 |
|
| 397 |
e.printStackTrace(); |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
return line; |
| 402 |
} |
| 403 |
|
| 404 |
final void putLine(Line line){ |
| 405 |
|
| 406 |
while(line!=null){ |
| 407 |
try { |
| 408 |
inq.put(line); |
| 409 |
line=null; |
| 410 |
} catch (InterruptedException e) { |
| 411 |
|
| 412 |
e.printStackTrace(); |
| 413 |
} |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
private ArrayList<PrintThread> spawnThreads(){ |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
ArrayList<PrintThread> alpt=new ArrayList<PrintThread>(threads); |
| 424 |
for(int i=0; i<threads; i++){ |
| 425 |
alpt.add(new PrintThread()); |
| 426 |
} |
| 427 |
if(verbose){outstream.println("Spawned threads.");} |
| 428 |
|
| 429 |
|
| 430 |
for(PrintThread pt : alpt){ |
| 431 |
pt.start(); |
| 432 |
} |
| 433 |
if(verbose){outstream.println("Started threads.");} |
| 434 |
|
| 435 |
|
| 436 |
return alpt; |
| 437 |
} |
| 438 |
|
| 439 |
private void waitForFinish(ArrayList<PrintThread> alpt){ |
| 440 |
|
| 441 |
boolean allSuccess=true; |
| 442 |
for(PrintThread pt : alpt){ |
| 443 |
while(pt.getState()!=Thread.State.TERMINATED){ |
| 444 |
try { |
| 445 |
|
| 446 |
pt.join(); |
| 447 |
} catch (InterruptedException e) { |
| 448 |
|
| 449 |
e.printStackTrace(); |
| 450 |
} |
| 451 |
} |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
private class PrintThread extends Thread { |
| 456 |
|
| 457 |
PrintThread(){} |
| 458 |
|
| 459 |
@Override |
| 460 |
public void run(){ |
| 461 |
Line line=takeLine(); |
| 462 |
while(line!=null && line!=POISON_LINE){ |
| 463 |
processLine(line); |
| 464 |
line=takeLine(); |
| 465 |
} |
| 466 |
putLine(POISON_LINE); |
| 467 |
} |
| 468 |
|
| 469 |
private void processLine(Line line){ |
| 470 |
ByteBuilder bb=new ByteBuilder(512); |
| 471 |
for(int i=0; i<windowsPerLine && line.eidx<=line.bases.length; i++){ |
| 472 |
Arrays.fill(counts, 0); |
| 473 |
tetramerCounter(line.bases, line.sidx, line.eidx, counts); |
| 474 |
if(keepShort || line.length()>=winSize){ |
| 475 |
append(line, counts, bb); |
| 476 |
} |
| 477 |
line.sidx+=step; |
| 478 |
line.eidx+=step; |
| 479 |
} |
| 480 |
bsw.add(bb, line.id); |
| 481 |
} |
| 482 |
|
| 483 |
private final int[] counts=new int[maxNmer+1]; |
| 484 |
} |
| 485 |
|
| 486 |
private class Line { |
| 487 |
|
| 488 |
Line(String header_, byte[] bases_, int sidx_, int eidx_, long id_){ |
| 489 |
header=header_; |
| 490 |
bases=bases_; |
| 491 |
sidx=sidx_; |
| 492 |
eidx=eidx_; |
| 493 |
id=id_; |
| 494 |
} |
| 495 |
|
| 496 |
public int length() { |
| 497 |
return eidx-sidx+1; |
| 498 |
} |
| 499 |
|
| 500 |
final String header; |
| 501 |
final byte[] bases; |
| 502 |
int sidx; |
| 503 |
int eidx; |
| 504 |
final long id; |
| 505 |
|
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
final Line POISON_LINE=new Line(null, null, -1, -1, -1); |
| 511 |
private final ArrayBlockingQueue<Line> inq; |
| 512 |
private final int threads; |
| 513 |
private long nextID=0; |
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
private String in1 = null; |
| 518 |
private String out1 = null; |
| 519 |
private ByteStreamWriter bsw = null; |
| 520 |
|
| 521 |
private final FileFormat ffin1; |
| 522 |
|
| 523 |
private java.io.PrintStream outstream=System.err; |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
private long maxReads=-1; |
| 528 |
int step = 500; |
| 529 |
private int winSize = 2000; |
| 530 |
private final int k; |
| 531 |
private boolean keepShort=false; |
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
private final static int windowsPerLine=8; |
| 536 |
public static boolean verbose=false; |
| 537 |
} |