| 1 |
package jgi; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.util.ArrayList; |
| 5 |
import java.util.Arrays; |
| 6 |
import java.util.HashMap; |
| 7 |
import java.util.Random; |
| 8 |
|
| 9 |
import dna.Data; |
| 10 |
import fileIO.ByteStreamWriter; |
| 11 |
import fileIO.FileFormat; |
| 12 |
import fileIO.ReadWrite; |
| 13 |
import fileIO.TextFile; |
| 14 |
import gff.GffLine; |
| 15 |
import prok.CallGenes; |
| 16 |
import prok.GeneCaller; |
| 17 |
import prok.GeneModel; |
| 18 |
import prok.GeneModelParser; |
| 19 |
import prok.Orf; |
| 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 sketch.Sketch; |
| 27 |
import sketch.SketchMakerMini; |
| 28 |
import sketch.SketchObject; |
| 29 |
import sketch.SketchTool; |
| 30 |
import stream.ConcurrentReadInputStream; |
| 31 |
import stream.FASTQ; |
| 32 |
import stream.Read; |
| 33 |
import stream.ReadInputStream; |
| 34 |
import structures.DoubleList; |
| 35 |
import structures.Feature; |
| 36 |
import structures.ListNum; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public class CheckStrand { |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public static void main(String[] args){ |
| 52 |
|
| 53 |
Timer t=new Timer(); |
| 54 |
|
| 55 |
|
| 56 |
CheckStrand x=new CheckStrand(args); |
| 57 |
|
| 58 |
|
| 59 |
x.process(t); |
| 60 |
|
| 61 |
|
| 62 |
Shared.closeStream(x.outstream); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public CheckStrand(String[] args){ |
| 70 |
|
| 71 |
{ |
| 72 |
PreParser pp=new PreParser(args, getClass(), false); |
| 73 |
args=pp.args; |
| 74 |
outstream=pp.outstream; |
| 75 |
} |
| 76 |
|
| 77 |
Parser parser=new Parser(); |
| 78 |
parser.out1=out1; |
| 79 |
for(int i=0; i<args.length; i++){ |
| 80 |
String arg=args[i]; |
| 81 |
String[] split=arg.split("="); |
| 82 |
String a=split[0].toLowerCase(); |
| 83 |
String b=split.length>1 ? split[1] : null; |
| 84 |
if(b!=null && b.equalsIgnoreCase("null")){b=null;} |
| 85 |
|
| 86 |
if(a.equals("parse_flag_goes_here")){ |
| 87 |
|
| 88 |
}else if(a.equals("size") || a.equals("len") || a.equals("length") || a.equals("sketchsize")){ |
| 89 |
sketchSize=Parse.parseIntKMG(b); |
| 90 |
}else if(a.equals("samplerate")){ |
| 91 |
samplerate=Float.parseFloat(b); |
| 92 |
}else if(a.equals("sampleseed") || a.equals("seed")){ |
| 93 |
sampleseed=Long.parseLong(b); |
| 94 |
}else if(a.equals("normalize")){ |
| 95 |
normalize=Parse.parseBoolean(b); |
| 96 |
}else if(a.equals("ref") || a.equals("fna")){ |
| 97 |
fna=b; |
| 98 |
}else if(a.equals("gff")){ |
| 99 |
gff=b; |
| 100 |
}else if(parser.parse(arg, a, b)){ |
| 101 |
|
| 102 |
}else{ |
| 103 |
|
| 104 |
assert(false) : "Unknown parameter "+args[i]; |
| 105 |
outstream.println("Unknown parameter "+args[i]); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
{ |
| 110 |
Parser.processQuality(); |
| 111 |
|
| 112 |
maxReads=parser.maxReads; |
| 113 |
in1=parser.in1; |
| 114 |
out1=parser.out1; |
| 115 |
} |
| 116 |
|
| 117 |
ffout1=FileFormat.testOutput(out1, FileFormat.TXT, null, true, true, false, false); |
| 118 |
ffin1=FileFormat.testInput(in1, FileFormat.FASTQ, null, true, true); |
| 119 |
} |
| 120 |
|
| 121 |
void process(Timer t){ |
| 122 |
if(verbose){System.err.println("Setting sketch params.");} |
| 123 |
setSketchStatics(); |
| 124 |
|
| 125 |
SketchTool canonTool=new SketchTool(sketchSize, 0, true, true, true); |
| 126 |
SketchTool forwardTool=new SketchTool(sketchSize, 0, true, true, false); |
| 127 |
|
| 128 |
FASTQ.PAIR_READS=false; |
| 129 |
final int threads=Tools.min(Shared.threads(), 16); |
| 130 |
|
| 131 |
System.err.println("Making canonical sketch."); |
| 132 |
Sketch canonSketch=canonTool.processReadsMT(in1, threads, maxReads, SketchObject.ONE_SKETCH, |
| 133 |
samplerate, 0, 0, (byte)0, false); |
| 134 |
|
| 135 |
System.err.println("Making forward sketch."); |
| 136 |
Sketch fwdSketch=forwardTool.processReadsMT(in1, threads, maxReads, SketchObject.ONE_SKETCH, |
| 137 |
samplerate, 0, 0, (byte)0, false); |
| 138 |
|
| 139 |
final double[] refResults=calcPMRatioWithRef(canonTool, forwardTool, canonSketch, fwdSketch); |
| 140 |
|
| 141 |
FASTQ.PAIR_READS=true; |
| 142 |
if(verbose){outstream.println("Finished reading data.");} |
| 143 |
|
| 144 |
outstream.println(); |
| 145 |
double[] results=calcStrandedness(canonSketch, fwdSketch); |
| 146 |
if(normalize) {results=calcStrandednessNormalized(canonSketch, fwdSketch);} |
| 147 |
|
| 148 |
outputResults(results, refResults); |
| 149 |
|
| 150 |
outstream.println(); |
| 151 |
t.stop("Time:\t"); |
| 152 |
assert(!errorState) : "An error was encountered."; |
| 153 |
} |
| 154 |
|
| 155 |
private void setSketchStatics() { |
| 156 |
SketchObject.AUTOSIZE=false; |
| 157 |
SketchObject.k=32; |
| 158 |
SketchObject.k2=-1; |
| 159 |
SketchObject.setK=true; |
| 160 |
SketchObject.sampleseed=sampleseed; |
| 161 |
|
| 162 |
|
| 163 |
SketchObject.AUTOSIZE=false; |
| 164 |
SketchObject.AUTOSIZE_LINEAR=false; |
| 165 |
SketchObject.targetSketchSize=sketchSize; |
| 166 |
SketchObject.SET_TARGET_SIZE=true; |
| 167 |
SketchObject.processSSU=false; |
| 168 |
|
| 169 |
SketchObject.defaultParams.parse("trackcounts", "trackcounts", null); |
| 170 |
SketchObject.defaultParams.samplerate=samplerate; |
| 171 |
SketchObject.postParse(); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
private double[] calcPMRatioWithRef(SketchTool canonTool, SketchTool fwdTool, |
| 218 |
Sketch canonSketch, Sketch fwdSketch) { |
| 219 |
ArrayList<Read> genes=grabGenes(); |
| 220 |
if(genes==null) {return null;} |
| 221 |
outstream.println("Processing "+genes.size()+" genes."); |
| 222 |
Sketch[] geneSketches=sketchGenes(genes, canonTool, fwdTool); |
| 223 |
Sketch canonGeneSketch=geneSketches[0], plusSketch=geneSketches[1], minusSketch=geneSketches[2]; |
| 224 |
return calcPMRatioWithGeneSketches(canonSketch, fwdSketch, canonGeneSketch, plusSketch, minusSketch); |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
private ArrayList<Read> grabGenes(){ |
| 232 |
if(fna==null) {return null;} |
| 233 |
|
| 234 |
ArrayList<Read> genes=null; |
| 235 |
if(gff!=null) { |
| 236 |
ArrayList<GffLine> lines=getGffLines(gff, types); |
| 237 |
HashMap<String, Read> map=getSequenceMap(fna); |
| 238 |
genes=CheckStrand.grabGenes(lines, map); |
| 239 |
}else{ |
| 240 |
System.err.println("Calling genes."); |
| 241 |
genes=callGenes(fna); |
| 242 |
} |
| 243 |
return (genes==null || genes.isEmpty()) ? null : genes; |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
static double[] calcPMRatioWithGeneSketches(Sketch canonSketch, Sketch fwdSketch, |
| 256 |
Sketch canonGeneSketch, Sketch plusSketch, Sketch minusSketch) { |
| 257 |
double[] p=CheckStrand.countSharedSum(fwdSketch, plusSketch); |
| 258 |
double[] m=CheckStrand.countSharedSum(fwdSketch, minusSketch); |
| 259 |
|
| 260 |
double ratio=p[3]/(p[3]+m[3]); |
| 261 |
double[] abFractions=CheckStrand.calcCoverage(canonSketch, canonGeneSketch); |
| 262 |
|
| 263 |
double[] ret={ratio, abFractions[0], abFractions[1]}; |
| 264 |
return ret; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
static Sketch[] sketchGenes(ArrayList<Read> genes, SketchTool canonTool, SketchTool fwdTool) { |
| 275 |
SketchMakerMini smmPlus=new SketchMakerMini(fwdTool, SketchObject.ONE_SKETCH, SketchObject.defaultParams); |
| 276 |
SketchMakerMini smmMinus=new SketchMakerMini(fwdTool, SketchObject.ONE_SKETCH, SketchObject.defaultParams); |
| 277 |
SketchMakerMini smmCanon=new SketchMakerMini(canonTool, SketchObject.ONE_SKETCH, SketchObject.defaultParams); |
| 278 |
for(Read r : genes) { |
| 279 |
smmCanon.processRead(r); |
| 280 |
smmPlus.processRead(r); |
| 281 |
r.reverseComplement(); |
| 282 |
r.setStrand(0); |
| 283 |
smmMinus.processRead(r); |
| 284 |
} |
| 285 |
Sketch canonGeneSketch=smmCanon.toSketch(0); |
| 286 |
Sketch plusSketch=smmPlus.toSketch(0); |
| 287 |
Sketch minusSketch=smmMinus.toSketch(0); |
| 288 |
return new Sketch[] {canonGeneSketch, plusSketch, minusSketch}; |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
private void outputResults(double[] results, double[] refResults){ |
| 297 |
if(ffout1==null) {return;} |
| 298 |
ByteStreamWriter bsw=new ByteStreamWriter(ffout1); |
| 299 |
bsw.start(); |
| 300 |
|
| 301 |
double strandedness=results[4]; |
| 302 |
double depth=results[5]; |
| 303 |
|
| 304 |
|
| 305 |
bsw.println(String.format("Strandedness:\t%.2f%%", strandedness*100)); |
| 306 |
bsw.println(String.format("AvgKmerDepth:\t%.2f", depth)); |
| 307 |
if(refResults!=null) { |
| 308 |
double pmRatio=refResults[0]; |
| 309 |
double aFraction=refResults[1]; |
| 310 |
double bFraction=refResults[2]; |
| 311 |
bsw.println(String.format("P/(P+M)_Ratio:\t%.6f", pmRatio)); |
| 312 |
bsw.println("MajorStrand:\t"+((pmRatio>=0.5) ? "Plus" : "Minus")); |
| 313 |
bsw.println(String.format("GeneCoverage:\t%.4f", aFraction)); |
| 314 |
bsw.println(String.format("GenePrecision:\t%.4f", bFraction)); |
| 315 |
} |
| 316 |
|
| 317 |
errorState=bsw.poisonAndWait() | errorState; |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
static double[] calcStrandedness(Sketch saCanon, Sketch sbFwd) { |
| 331 |
final long[] a=saCanon.keys, b=sbFwd.keys; |
| 332 |
final int[] aCounts=saCanon.keyCounts, bCounts=sbFwd.keyCounts; |
| 333 |
|
| 334 |
int matches=0; |
| 335 |
int totalCount=0; |
| 336 |
long sharedSum=0; |
| 337 |
long nonUniqueCount=0; |
| 338 |
double totalSum=0; |
| 339 |
double minSum=0; |
| 340 |
double expectedMinSum=0; |
| 341 |
double maxPossibleMinSum=0; |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
for(int i=0, j=0; i<a.length && j<b.length; ){ |
| 346 |
final long ka=a[i], kb=b[j]; |
| 347 |
if(ka==kb){ |
| 348 |
matches++; |
| 349 |
final int ca=aCounts[i]; |
| 350 |
final int cb=bCounts[j]; |
| 351 |
final int cmin=Tools.min(cb, ca-cb); |
| 352 |
assert(cmin>=0) : ca+", "+cb; |
| 353 |
nonUniqueCount+=(ca>1 ? 1 : 0); |
| 354 |
totalSum+=ca; |
| 355 |
totalCount++; |
| 356 |
sharedSum+=ca; |
| 357 |
minSum+=cmin; |
| 358 |
maxPossibleMinSum+=(ca/2); |
| 359 |
expectedMinSum+=expectedMinorAlleleCount(ca); |
| 360 |
i++; |
| 361 |
j++; |
| 362 |
}else if(ka<kb){ |
| 363 |
final int ca=aCounts[i]; |
| 364 |
final int cb=0; |
| 365 |
final int cmin=Tools.min(cb, ca-cb); |
| 366 |
assert(cmin>=0); |
| 367 |
nonUniqueCount+=(ca>1 ? 1 : 0); |
| 368 |
totalSum+=ca; |
| 369 |
totalCount++; |
| 370 |
minSum+=cmin; |
| 371 |
maxPossibleMinSum+=(ca/2); |
| 372 |
expectedMinSum+=expectedMinorAlleleCount(ca); |
| 373 |
i++; |
| 374 |
}else{ |
| 375 |
j++; |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
double strandedness; |
| 386 |
if(minSum<=expectedMinSum) { |
| 387 |
|
| 388 |
strandedness=0.5+(1-(minSum/expectedMinSum))*0.5; |
| 389 |
}else{ |
| 390 |
assert(minSum<=maxPossibleMinSum) : minSum+", "+maxPossibleMinSum; |
| 391 |
double range=(maxPossibleMinSum-expectedMinSum); |
| 392 |
double delta=minSum-expectedMinSum; |
| 393 |
assert(delta>=0 && delta<=range); |
| 394 |
double x=0.5*(1-(delta/range)); |
| 395 |
strandedness=x; |
| 396 |
} |
| 397 |
double depth=totalSum/totalCount; |
| 398 |
double nonUniqueFraction=nonUniqueCount/(1.0*totalCount); |
| 399 |
return new double[] {totalSum, minSum, expectedMinSum, maxPossibleMinSum, |
| 400 |
strandedness, depth, matches, nonUniqueFraction}; |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
static double[] calcStrandednessNormalized(Sketch saCanon, Sketch sbFwd) { |
| 413 |
final long[] a=saCanon.keys, b=sbFwd.keys; |
| 414 |
final int[] aCounts=saCanon.keyCounts, bCounts=sbFwd.keyCounts; |
| 415 |
|
| 416 |
int matches=0; |
| 417 |
int totalCount=0; |
| 418 |
long sharedSum=0; |
| 419 |
long nonUniqueCount=0; |
| 420 |
double totalSum=0; |
| 421 |
double minSum=0; |
| 422 |
double expectedMinSum=0; |
| 423 |
double maxPossibleMinSum=0; |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
for(int i=0, j=0; i<a.length && j<b.length; ){ |
| 428 |
final long ka=a[i], kb=b[j]; |
| 429 |
if(ka==kb){ |
| 430 |
final int ca=aCounts[i]; |
| 431 |
final int cb=bCounts[j]; |
| 432 |
if(ca>1) { |
| 433 |
matches++; |
| 434 |
final int cmin=Tools.min(cb, ca-cb); |
| 435 |
assert(cmin>=0) : ca+", "+cb; |
| 436 |
nonUniqueCount+=(ca>1 ? 1 : 0); |
| 437 |
double expectedMinor=expectedMinorAlleleCount(ca); |
| 438 |
assert(expectedMinor>0); |
| 439 |
double ratio=cmin/(double)ca; |
| 440 |
totalSum+=ca; |
| 441 |
totalCount++; |
| 442 |
sharedSum+=ca; |
| 443 |
minSum+=ratio; |
| 444 |
maxPossibleMinSum+=((ca/2)/(double)ca); |
| 445 |
expectedMinSum+=(expectedMinor/ca); |
| 446 |
} |
| 447 |
i++; |
| 448 |
j++; |
| 449 |
}else if(ka<kb){ |
| 450 |
final int ca=aCounts[i]; |
| 451 |
final int cb=0; |
| 452 |
if(ca>1) { |
| 453 |
final int cmin=Tools.min(cb, ca-cb); |
| 454 |
assert(cmin>=0); |
| 455 |
nonUniqueCount+=(ca>1 ? 1 : 0); |
| 456 |
double expectedMinor=expectedMinorAlleleCount(ca); |
| 457 |
double ratio=cmin/(double)ca; |
| 458 |
totalSum+=ca; |
| 459 |
totalCount++; |
| 460 |
sharedSum+=ca; |
| 461 |
minSum+=ratio; |
| 462 |
maxPossibleMinSum+=((ca/2)/(double)ca); |
| 463 |
expectedMinSum+=(expectedMinor/ca); |
| 464 |
} |
| 465 |
i++; |
| 466 |
}else{ |
| 467 |
j++; |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
double strandedness; |
| 478 |
if(minSum<=expectedMinSum) { |
| 479 |
|
| 480 |
strandedness=0.5+(1-(minSum/expectedMinSum))*0.5; |
| 481 |
}else{ |
| 482 |
assert(minSum<=maxPossibleMinSum) : minSum+", "+maxPossibleMinSum; |
| 483 |
double range=(maxPossibleMinSum-expectedMinSum); |
| 484 |
double delta=minSum-expectedMinSum; |
| 485 |
assert(delta>=0 && delta<=range); |
| 486 |
double x=0.5*(1-(delta/range)); |
| 487 |
strandedness=x; |
| 488 |
} |
| 489 |
double depth=totalSum/totalCount; |
| 490 |
double nonUniqueFraction=nonUniqueCount/(1.0*totalCount); |
| 491 |
return new double[] {totalSum, minSum, expectedMinSum, maxPossibleMinSum, |
| 492 |
strandedness, depth, matches, nonUniqueFraction}; |
| 493 |
} |
| 494 |
|
| 495 |
static float strandedness(long plus, long minus) { |
| 496 |
long sum=plus+minus; |
| 497 |
long maxPossibleMinor=sum/2; |
| 498 |
float expectedMinor=(float)expectedMinorAlleleCount(sum); |
| 499 |
assert(expectedMinor>0); |
| 500 |
|
| 501 |
return strandedness(plus, minus, maxPossibleMinor, expectedMinor); |
| 502 |
} |
| 503 |
|
| 504 |
static float strandedness(long plus, long minus, long maxPossibleMinor, float expectedMinor) { |
| 505 |
long sum=plus+minus; |
| 506 |
long min=Math.min(plus, minus); |
| 507 |
assert(expectedMinor>0); |
| 508 |
|
| 509 |
if(min<=expectedMinor) { |
| 510 |
float strandedness=1-0.5f*(min/expectedMinor); |
| 511 |
assert(strandedness>=0 && strandedness<=1.01) : strandedness+ |
| 512 |
", "+plus+", "+minus+", "+sum+", "+min+", "+expectedMinor; |
| 513 |
return strandedness; |
| 514 |
}else{ |
| 515 |
float range=maxPossibleMinor-expectedMinor; |
| 516 |
float dif=min-expectedMinor; |
| 517 |
float strandedness=0.5f*(1-(dif/range)); |
| 518 |
assert(strandedness>=0 && strandedness<=1.01) : strandedness+", "+plus+", "+minus+ |
| 519 |
", "+sum+", "+min+", "+expectedMinor+", "+range+", "+dif; |
| 520 |
return strandedness; |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
static double[] countSharedSum(Sketch saFwd, Sketch sbTranscriptStrand) { |
| 531 |
final long[] a=saFwd.keys, b=sbTranscriptStrand.keys; |
| 532 |
final int[] aCounts=saFwd.keyCounts, bCounts=sbTranscriptStrand.keyCounts; |
| 533 |
|
| 534 |
int matches=0; |
| 535 |
int totalCount=0; |
| 536 |
long sharedSum=0; |
| 537 |
double totalSum=0; |
| 538 |
|
| 539 |
for(int i=0, j=0; i<a.length && j<b.length; ){ |
| 540 |
final long ka=a[i], kb=b[j]; |
| 541 |
if(ka==kb){ |
| 542 |
matches++; |
| 543 |
final int ca=aCounts[i]; |
| 544 |
totalSum+=ca; |
| 545 |
totalCount++; |
| 546 |
sharedSum+=ca; |
| 547 |
i++; |
| 548 |
j++; |
| 549 |
}else if(ka<kb){ |
| 550 |
final int ca=aCounts[i]; |
| 551 |
totalSum+=ca; |
| 552 |
totalCount++; |
| 553 |
i++; |
| 554 |
}else{ |
| 555 |
j++; |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
double depth=sharedSum/(double)Tools.max(matches, 1); |
| 560 |
return new double[] {totalSum, depth, matches, sharedSum}; |
| 561 |
} |
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
static double[] calcCoverage(Sketch sa, Sketch sb) { |
| 569 |
final long[] a=sa.keys, b=sb.keys; |
| 570 |
final int[] aCounts=sa.keyCounts, bCounts=sb.keyCounts; |
| 571 |
|
| 572 |
int matches=0; |
| 573 |
|
| 574 |
int i=0, j=0; |
| 575 |
for(; i<a.length && j<b.length; ){ |
| 576 |
final long ka=a[i], kb=b[j]; |
| 577 |
if(ka==kb){ |
| 578 |
matches++; |
| 579 |
i++; |
| 580 |
j++; |
| 581 |
}else if(ka<kb){ |
| 582 |
i++; |
| 583 |
}else{ |
| 584 |
j++; |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
double aFraction=matches/(double)j; |
| 589 |
double bFraction=matches/(double)i; |
| 590 |
|
| 591 |
return new double[] {aFraction, bFraction}; |
| 592 |
} |
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
public static final double expectedMinorAlleleFrequency(final long depth) { |
| 603 |
if(depth<expectedMinorAlleleFreq.length) {return depth>1 ? expectedMinorAlleleFreq[(int)depth] : 0;} |
| 604 |
long d=depth; |
| 605 |
double mult=1.0; |
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
while(d>=expectedMinorAlleleFreq.length) { |
| 613 |
d=d/100; |
| 614 |
mult*=0.1; |
| 615 |
} |
| 616 |
double maf=expectedMinorAlleleFreq[(int)d]; |
| 617 |
double dif=0.5-maf; |
| 618 |
return 0.5-(dif*mult); |
| 619 |
} |
| 620 |
|
| 621 |
public static final double expectedMinorAlleleCount(final long depth) { |
| 622 |
if(depth<expectedMinorAlleleCount.length) {return depth>1 ? expectedMinorAlleleCount[(int)depth] : 0;} |
| 623 |
return depth*expectedMinorAlleleFrequency(depth); |
| 624 |
} |
| 625 |
|
| 626 |
|
| 627 |
private static void printMinorAlleleCount() { |
| 628 |
System.out.println("#Expected minor allele count for N coin flips, starting at 0, 10m simulations."); |
| 629 |
for(int i=0; i<expectedMinorAlleleCount.length; i++) { |
| 630 |
double c=expectedMinorAlleleCount[i]; |
| 631 |
int decimals=Tools.max(1, 7-Integer.toString((int)c).length()); |
| 632 |
System.out.println(String.format("%."+decimals+"f", expectedMinorAlleleCount[i])); |
| 633 |
} |
| 634 |
} |
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
public static final double[] makeExpectedMinorAlleleArray(final int maxDepth, final int trials) { |
| 645 |
{ |
| 646 |
double[] d=loadExpectedMinorAlleleArray(); |
| 647 |
if(d!=null) {return d;} |
| 648 |
} |
| 649 |
final long[] minorSum=new long[maxDepth+1]; |
| 650 |
final Random randy=Shared.threadLocalRandom(); |
| 651 |
final int[] headsTails=new int[2]; |
| 652 |
for(int trial=0; trial<trials; trial++) { |
| 653 |
headsTails[0]=headsTails[1]=0; |
| 654 |
for(int i=1; i<maxDepth+1; i++) { |
| 655 |
int bit=randy.nextInt()&1; |
| 656 |
headsTails[bit]++; |
| 657 |
minorSum[i]+=Tools.min(headsTails[0], headsTails[1]); |
| 658 |
} |
| 659 |
} |
| 660 |
final double[] expected=new double[maxDepth+1]; |
| 661 |
final double mult=1.0/trials; |
| 662 |
for(int i=0; i<maxDepth+1; i++) { |
| 663 |
expected[i]=minorSum[i]*mult; |
| 664 |
} |
| 665 |
return expected; |
| 666 |
} |
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
public static double[] loadExpectedMinorAlleleArray() { |
| 673 |
String path=Data.findPath("?minorAlleleCount.txt", true); |
| 674 |
if(path==null) {return null;} |
| 675 |
File f=new File(path); |
| 676 |
if(!f.exists() || !f.canRead()) {return null;} |
| 677 |
|
| 678 |
DoubleList dl=new DoubleList(); |
| 679 |
String[] lines=TextFile.toStringLines(path); |
| 680 |
for(String s : lines) { |
| 681 |
if(!Tools.startsWith(s, '#')) { |
| 682 |
double d=Parse.parseDouble(s, 0, s.length()); |
| 683 |
dl.add(d); |
| 684 |
} |
| 685 |
} |
| 686 |
return dl.toArray(); |
| 687 |
} |
| 688 |
|
| 689 |
|
| 690 |
private static double[] makeExpectedMinorAlleleFreq(double[] counts) { |
| 691 |
double[] freq=new double[counts.length]; |
| 692 |
for(int i=1; i<counts.length; i++) { |
| 693 |
freq[i]=counts[i]/i; |
| 694 |
} |
| 695 |
return freq; |
| 696 |
} |
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
static ArrayList<GffLine> getGffLines(String gff, String types){ |
| 707 |
return GffLine.loadGffFile(gff, types, false); |
| 708 |
} |
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
static HashMap<String, Read> getSequenceMap(String fna){ |
| 716 |
ArrayList<Read> list=ReadInputStream.toReads(fna, FileFormat.FA, -1); |
| 717 |
return getSequenceMap(list); |
| 718 |
} |
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
static HashMap<String, Read> getSequenceMap(ArrayList<Read> list){ |
| 727 |
HashMap<String, Read> map=new HashMap<String, Read>(1+list.size()*3); |
| 728 |
|
| 729 |
for(Read r : list){ |
| 730 |
map.put(r.id, r); |
| 731 |
|
| 732 |
String id2=Tools.whitespacePlus.split(r.id)[0]; |
| 733 |
if(!id2.equals(r.id)) {map.put(id2, r);} |
| 734 |
} |
| 735 |
return map; |
| 736 |
} |
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
static <K extends Feature> ArrayList<Read> grabGenes(ArrayList<? extends K> lines, HashMap<String, Read> map){ |
| 747 |
ArrayList<Read> list=null; |
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
for(K gline : lines){ |
| 753 |
|
| 754 |
Read scaf=map.get(gline.seqid()); |
| 755 |
assert(scaf!=null) : "Can't find "+gline.seqid()+" in "+map.keySet(); |
| 756 |
|
| 757 |
final int start=gline.start(); |
| 758 |
final int stop=gline.stop(); |
| 759 |
|
| 760 |
|
| 761 |
if(start>=0 && stop<scaf.length()){ |
| 762 |
String id=gline.name(); |
| 763 |
Read r=new Read(Arrays.copyOfRange(scaf.bases, start, stop+1), null, id, 1); |
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
if(r!=null){ |
| 768 |
if(gline.strand()==1){r.reverseComplement();} |
| 769 |
if(list==null){list=new ArrayList<Read>(8);} |
| 770 |
list.add(r); |
| 771 |
} |
| 772 |
|
| 773 |
|
| 774 |
} |
| 775 |
} |
| 776 |
return list; |
| 777 |
} |
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
private ArrayList<Read> callGenes(String fna){ |
| 785 |
final ConcurrentReadInputStream cris=makeFastaCris(fna); |
| 786 |
|
| 787 |
if(pgmFile==null){ |
| 788 |
pgmFile=Data.findPath("?model.pgm"); |
| 789 |
} |
| 790 |
GeneModel pgm=GeneModelParser.loadModel(pgmFile); |
| 791 |
GeneCaller gCaller=CallGenes.makeGeneCaller(pgm); |
| 792 |
ArrayList<Read> genes=callGenes(cris, gCaller); |
| 793 |
|
| 794 |
|
| 795 |
errorState|=ReadWrite.closeStream(cris); |
| 796 |
return genes; |
| 797 |
} |
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
private ConcurrentReadInputStream makeFastaCris(String fname){ |
| 805 |
FileFormat ffin=FileFormat.testInput(fname, FileFormat.FA, null, true, true); |
| 806 |
ConcurrentReadInputStream cris=ConcurrentReadInputStream.getReadInputStream(maxReads, false, ffin, null); |
| 807 |
cris.start(); |
| 808 |
if(verbose){outstream.println("Started cris");} |
| 809 |
return cris; |
| 810 |
} |
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
static ArrayList<Read> callGenes(ConcurrentReadInputStream cris, GeneCaller gCaller){ |
| 819 |
ArrayList<Read> genes=new ArrayList<Read>(); |
| 820 |
|
| 821 |
|
| 822 |
ListNum<Read> ln=cris.nextList(); |
| 823 |
|
| 824 |
CallGenes.callCDS=true; |
| 825 |
CallGenes.calltRNA=CallGenes.call16S=CallGenes.call23S |
| 826 |
=CallGenes.call5S=CallGenes.call18S=true; |
| 827 |
|
| 828 |
|
| 829 |
while(ln!=null && ln.size()>0){ |
| 830 |
|
| 831 |
for(Read r : ln) { |
| 832 |
ArrayList<Orf> orfs=gCaller.callGenes(r); |
| 833 |
if(orfs!=null) { |
| 834 |
for(Orf orf : orfs) { |
| 835 |
Read gene=CallGenes.fetch(orf, r); |
| 836 |
genes.add(gene); |
| 837 |
} |
| 838 |
} |
| 839 |
|
| 840 |
} |
| 841 |
|
| 842 |
ln=cris.nextList(); |
| 843 |
} |
| 844 |
|
| 845 |
|
| 846 |
if(ln!=null){ |
| 847 |
cris.returnList(ln.id, ln.list==null || ln.list.isEmpty()); |
| 848 |
} |
| 849 |
return genes; |
| 850 |
} |
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
private int sketchSize=20000; |
| 855 |
|
| 856 |
private String in1=null; |
| 857 |
private String out1="stdout.txt"; |
| 858 |
String fna=null; |
| 859 |
String gff=null; |
| 860 |
String pgmFile=null; |
| 861 |
|
| 862 |
private final FileFormat ffin1; |
| 863 |
private final FileFormat ffout1; |
| 864 |
|
| 865 |
|
| 866 |
private String types="CDS,rRNA,tRNA,ncRNA,exon,5S,16S,23S"; |
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
private boolean normalize=false; |
| 871 |
private long maxReads=-1; |
| 872 |
private float samplerate=1; |
| 873 |
private long sampleseed=17; |
| 874 |
private boolean errorState=false; |
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
static final double[] expectedMinorAlleleCount= |
| 879 |
makeExpectedMinorAlleleArray(10000, 100000); |
| 880 |
static final double[] expectedMinorAlleleFreq= |
| 881 |
makeExpectedMinorAlleleFreq(expectedMinorAlleleCount); |
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
private java.io.PrintStream outstream=System.err; |
| 887 |
public static boolean verbose=false; |
| 888 |
|
| 889 |
} |