| 1 |
package structures; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.concurrent.ArrayBlockingQueue; |
| 5 |
import java.util.concurrent.locks.ReadWriteLock; |
| 6 |
import java.util.concurrent.locks.ReentrantReadWriteLock; |
| 7 |
|
| 8 |
import dna.Scaffold; |
| 9 |
import shared.Timer; |
| 10 |
import shared.Tools; |
| 11 |
import template.Accumulator; |
| 12 |
import template.ThreadWaiter; |
| 13 |
|
| 14 |
public class StandardDeviator implements Accumulator<StandardDeviator.ProcessThread> { |
| 15 |
|
| 16 |
public StandardDeviator(boolean stranded_, int strand_) { |
| 17 |
stranded=stranded_; |
| 18 |
strand=strand_; |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public boolean calculateStuff(int threads0, ArrayList<Scaffold> scaffolds, |
| 72 |
boolean calcStd, boolean calcMedian, boolean calcHist, |
| 73 |
int minDepth, int histMax, double windowAvg, int window) { |
| 74 |
|
| 75 |
final int threads=Tools.mid(1, threads0, scaffolds.size()); |
| 76 |
Timer t=new Timer(); |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
{ |
| 81 |
totalSum=0; |
| 82 |
bins=0; |
| 83 |
|
| 84 |
ArrayList<ProcessThread> alpt=new ArrayList<ProcessThread>(threads); |
| 85 |
for(int i=0; i<threads; i++){ |
| 86 |
alpt.add(new ProcessThread(i, threads, scaffolds, false, false, false, minDepth, -1, -1, -1, -1)); |
| 87 |
} |
| 88 |
|
| 89 |
ThreadWaiter.startThreads(alpt); |
| 90 |
boolean success=ThreadWaiter.waitForThreadsToFinish(alpt, this); |
| 91 |
errorState&=!success; |
| 92 |
if(verbose) {t.stopAndStart("calcSumMT:");} |
| 93 |
} |
| 94 |
|
| 95 |
final double globalMean=totalSum/(double)bins; |
| 96 |
{ |
| 97 |
totalSum=0; |
| 98 |
bins=0; |
| 99 |
|
| 100 |
ArrayList<ProcessThread> alpt=new ArrayList<ProcessThread>(threads); |
| 101 |
for(int i=0; i<threads; i++){ |
| 102 |
alpt.add(new ProcessThread(i, threads, scaffolds, |
| 103 |
calcStd, calcMedian, calcHist, minDepth, histMax, globalMean, windowAvg, window)); |
| 104 |
} |
| 105 |
|
| 106 |
ThreadWaiter.startThreads(alpt); |
| 107 |
boolean success=ThreadWaiter.waitForThreadsToFinish(alpt, this); |
| 108 |
errorState&=!success; |
| 109 |
if(verbose) {t.stopAndStart("calcStdMT:");} |
| 110 |
} |
| 111 |
|
| 112 |
return errorState; |
| 113 |
} |
| 114 |
|
| 115 |
public double[] standardDeviation(ArrayList<Scaffold> scaffolds, int minscaf0){ |
| 116 |
totalSum=bins=0; |
| 117 |
mean=stdev=0; |
| 118 |
long scafsCounted=0; |
| 119 |
final int minscaf=Tools.max(minscaf0, 1); |
| 120 |
|
| 121 |
for(Scaffold scaf : scaffolds){ |
| 122 |
if(scaf.length>=minscaf){ |
| 123 |
final CoverageArray ca=(CoverageArray)(strand==1 ? scaf.obj1 : scaf.obj0); |
| 124 |
bins+=scaf.length; |
| 125 |
totalSum+=(ca==null ? 0 : ca.sum()); |
| 126 |
scafsCounted++; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
if(bins<1){return new double[] {mean, stdev};} |
| 131 |
mean=totalSum/(double)bins; |
| 132 |
|
| 133 |
double sumdev2=0; |
| 134 |
for(Scaffold scaf : scaffolds){ |
| 135 |
if(scaf.length>=minscaf){ |
| 136 |
final CoverageArray ca=(CoverageArray)(strand==1 ? scaf.obj1 : scaf.obj0); |
| 137 |
if(ca==null) { |
| 138 |
sumdev2+=(mean*mean*scaf.length); |
| 139 |
}else { |
| 140 |
sumdev2+=ca.devSum(mean); |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
stdev=Math.sqrt(sumdev2/bins); |
| 146 |
|
| 147 |
return new double[] {mean, stdev}; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
@Deprecated |
| 152 |
public double[] standardDeviationBinned(ArrayList<Scaffold> scaffolds, int binsize, int minscaf0){ |
| 153 |
totalSumBinned=binsBinned=0; |
| 154 |
meanBinned=stdevBinned=0; |
| 155 |
final int minscaf=Tools.max(minscaf0, 1); |
| 156 |
|
| 157 |
for(Scaffold scaf : scaffolds){ |
| 158 |
if(scaf.length>=minscaf){ |
| 159 |
CoverageArray ca=(CoverageArray)(strand==1 ? scaf.obj1 : scaf.obj0); |
| 160 |
int bins=(int)((scaf.length+(long)binsize-1L)/binsize); |
| 161 |
assert(bins>0) : scaf.length+", "+binsize+", "+minscaf; |
| 162 |
binsBinned+=bins; |
| 163 |
if(ca==null) { |
| 164 |
totalSumBinned+=0; |
| 165 |
}else { |
| 166 |
totalSumBinned+=ca.sum()/bins; |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
if(binsBinned<1){return new double[] {meanBinned, stdevBinned};} |
| 172 |
meanBinned=totalSumBinned/(double)binsBinned; |
| 173 |
double sumdev2=0; |
| 174 |
|
| 175 |
for(Scaffold scaf : scaffolds){ |
| 176 |
if(scaf.length>=minscaf){ |
| 177 |
CoverageArray ca=(CoverageArray)(strand==1 ? scaf.obj1 : scaf.obj0); |
| 178 |
int lastPos=-1, nextPos=binsize-1; |
| 179 |
long tempSum=0; |
| 180 |
final int lim=scaf.length-1; |
| 181 |
for(int i=0; i<scaf.length; i++){ |
| 182 |
int x=(ca==null ? 0 : ca.get(i)); |
| 183 |
tempSum+=x; |
| 184 |
if(i>=nextPos || i==lim){ |
| 185 |
int bin=(i-lastPos); |
| 186 |
double depth=(tempSum/(double)bin); |
| 187 |
double dev=meanBinned-depth; |
| 188 |
sumdev2+=(dev*dev); |
| 189 |
nextPos+=binsize; |
| 190 |
lastPos=i; |
| 191 |
tempSum=0; |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
stdevBinned=Math.sqrt(sumdev2/binsBinned); |
| 198 |
return new double[] {meanBinned, stdevBinned}; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
public double[] standardDeviationBinnedUnsafe(String fname, ArrayList<Scaffold> scaffolds, int binsize, int minscaf0){ |
| 203 |
final int minscaf=Tools.max(minscaf0, 1); |
| 204 |
|
| 205 |
LongList list=new LongList(); |
| 206 |
for(Scaffold scaf : scaffolds){ |
| 207 |
CoverageArray ca=(CoverageArray)(strand==1 ? scaf.obj1 : scaf.obj0); |
| 208 |
int lastPos=-1, nextPos=binsize-1; |
| 209 |
long sum=0; |
| 210 |
final int lim=scaf.length-1; |
| 211 |
for(int i=0; i<scaf.length; i++){ |
| 212 |
int x=(ca==null ? 0 : ca.get(i)); |
| 213 |
sum+=x; |
| 214 |
if(i>=nextPos || i==lim){ |
| 215 |
int bin=(i-lastPos); |
| 216 |
if(scaf.length>=minscaf){ |
| 217 |
list.add((int)(10*(sum/(double)bin))); |
| 218 |
} |
| 219 |
nextPos+=binsize; |
| 220 |
lastPos=i; |
| 221 |
sum=0; |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
list.sort(); |
| 226 |
|
| 227 |
mean=0.1*list.mean(); |
| 228 |
double median=0.1*list.median(); |
| 229 |
double mode=0.1*list.mode(); |
| 230 |
stdev=0.1*list.stdev(); |
| 231 |
return new double[] {mean, median, mode, stdev}; |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
class ProcessThread extends Thread { |
| 289 |
|
| 290 |
ProcessThread(int tid_, int threads_, ArrayList<Scaffold> list_, |
| 291 |
boolean calcStd_, boolean calcMedian_, boolean calcHist_, int minDepth_, int histMax_, |
| 292 |
double globalMean_, double windowAvg_, int window_){ |
| 293 |
|
| 294 |
tid=tid_; |
| 295 |
threads=threads_; |
| 296 |
list=list_; |
| 297 |
|
| 298 |
calcStd=calcStd_; |
| 299 |
calcMedian=calcMedian_; |
| 300 |
calcHist=calcHist_; |
| 301 |
minDepth=minDepth_; |
| 302 |
histMax=histMax_; |
| 303 |
globalMean=globalMean_; |
| 304 |
windowAvg=windowAvg_; |
| 305 |
window=window_; |
| 306 |
if(calcHist) { |
| 307 |
assert(histMax>0) : histMax; |
| 308 |
hist0=new LongList(Tools.min(histMax+1, Character.MAX_VALUE+1)); |
| 309 |
if(stranded) {hist1=new LongList(Tools.min(histMax+1, Character.MAX_VALUE+1));} |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
@Override |
| 314 |
public synchronized void run() { |
| 315 |
for(int i=tid; i<list.size(); i+=threads){ |
| 316 |
Scaffold scaf=list.get(i); |
| 317 |
synchronized(scaf) { |
| 318 |
binsT+=scaf.length; |
| 319 |
processCA((CoverageArray)scaf.obj0, 0, scaf.length, scaf.basehits); |
| 320 |
if(stranded) {processCA((CoverageArray)scaf.obj1, 1, scaf.length, scaf.basehits);} |
| 321 |
} |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
private void processCA(CoverageArray ca, int strand, int length, long mappedBases) { |
| 327 |
if(mappedBases>0) { |
| 328 |
if(calcHist) {addToHist(ca, strand, length);} |
| 329 |
if(ca==null) {return;} |
| 330 |
totalSumT+=ca.calcSumAndCovered(minDepth); |
| 331 |
if(calcStd) {ca.standardDeviation();} |
| 332 |
if(calcMedian) {ca.median();} |
| 333 |
if(globalMean>-1) {ca.devSum(globalMean);} |
| 334 |
if(windowAvg>0) {ca.basesUnderAverageCoverage(windowAvg, window);} |
| 335 |
}else { |
| 336 |
if(calcHist) {addToHist(null, strand, length);} |
| 337 |
if(ca==null) {return;} |
| 338 |
totalSumT+=0; |
| 339 |
ca.setSum(0); |
| 340 |
ca.setCovered(0); |
| 341 |
ca.setStdev(0); |
| 342 |
ca.setMedian(0); |
| 343 |
ca.setDevSum(mappedBases); |
| 344 |
if(globalMean>-1) {ca.setDevSum((globalMean*globalMean)*length);} |
| 345 |
if(windowAvg>0) {ca.setUnderWindowAverage(length);} |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
private void addToHist(CoverageArray ca, int strand, int length) { |
| 350 |
LongList hist=(strand==0 ? hist0 : hist1); |
| 351 |
assert(hist!=null) : stranded+", "+strand+", "+length; |
| 352 |
if(ca==null) { |
| 353 |
hist.increment(0, length); |
| 354 |
return; |
| 355 |
} |
| 356 |
assert(ca.length()==length) : ca.length()+", "+length+", "+ca.maxIndex; |
| 357 |
for(int i=0, len=ca.length(); i<len; i++) { |
| 358 |
int d=Tools.min(ca.get(i), histMax); |
| 359 |
hist.increment(d); |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
long totalSumT=0; |
| 364 |
long binsT=0; |
| 365 |
|
| 366 |
final int tid; |
| 367 |
final int threads; |
| 368 |
final ArrayList<Scaffold> list; |
| 369 |
LongList hist0; |
| 370 |
LongList hist1; |
| 371 |
|
| 372 |
final boolean calcStd; |
| 373 |
final boolean calcMedian; |
| 374 |
final boolean calcHist; |
| 375 |
final int minDepth; |
| 376 |
final int histMax; |
| 377 |
final double globalMean; |
| 378 |
final double windowAvg; |
| 379 |
final int window; |
| 380 |
} |
| 381 |
|
| 382 |
private static Scaffold getNext(ArrayBlockingQueue<Scaffold> queue) { |
| 383 |
Scaffold next=null; |
| 384 |
while(next==null) { |
| 385 |
try { |
| 386 |
next=queue.take(); |
| 387 |
} catch (InterruptedException e) { |
| 388 |
|
| 389 |
e.printStackTrace(); |
| 390 |
} |
| 391 |
} |
| 392 |
return next; |
| 393 |
} |
| 394 |
|
| 395 |
private static void addToQueue(ArrayBlockingQueue<Scaffold> queue, Scaffold s) { |
| 396 |
while(s!=null) { |
| 397 |
try { |
| 398 |
queue.put(s); |
| 399 |
s=null; |
| 400 |
} catch (InterruptedException e) { |
| 401 |
|
| 402 |
e.printStackTrace(); |
| 403 |
} |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
@Override |
| 408 |
public void accumulate(ProcessThread t) { |
| 409 |
synchronized(t) { |
| 410 |
totalSum+=t.totalSumT; |
| 411 |
bins+=t.binsT; |
| 412 |
if(hist0==null) {hist0=t.hist0;} |
| 413 |
else if(t.hist0!=null) {hist0.incrementBy(t.hist0);} |
| 414 |
if(hist1==null) {hist1=t.hist1;} |
| 415 |
else if(t.hist1!=null) {hist1.incrementBy(t.hist1);} |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
@Override |
| 420 |
public boolean success() { |
| 421 |
|
| 422 |
return false; |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
long totalSum; |
| 430 |
long bins; |
| 431 |
double mean; |
| 432 |
double stdev; |
| 433 |
|
| 434 |
long totalSumBinned; |
| 435 |
long binsBinned; |
| 436 |
double meanBinned; |
| 437 |
double stdevBinned; |
| 438 |
|
| 439 |
public LongList hist0; |
| 440 |
public LongList hist1; |
| 441 |
|
| 442 |
@Override |
| 443 |
public final ReadWriteLock rwlock() {return rwlock;} |
| 444 |
private final ReadWriteLock rwlock=new ReentrantReadWriteLock(); |
| 445 |
|
| 446 |
public final int strand; |
| 447 |
public final boolean stranded; |
| 448 |
public boolean errorState=false; |
| 449 |
|
| 450 |
private static final Scaffold POISON=new Scaffold("POISON", null, -1); |
| 451 |
public static boolean verbose=false; |
| 452 |
|
| 453 |
} |