| 1 |
package pacbio; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
|
| 5 |
import fileIO.TextFile; |
| 6 |
import fileIO.TextStreamWriter; |
| 7 |
import shared.Parse; |
| 8 |
import shared.PreParser; |
| 9 |
import shared.Shared; |
| 10 |
import shared.Timer; |
| 11 |
import shared.Tools; |
| 12 |
import stream.SiteScoreR; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
public class ProcessStackedSitesNormalized { |
| 20 |
|
| 21 |
public static void main(String[] args){ |
| 22 |
{ |
| 23 |
PreParser pp=new PreParser(args, new Object() { }.getClass().getEnclosingClass(), false); |
| 24 |
args=pp.args; |
| 25 |
|
| 26 |
} |
| 27 |
|
| 28 |
Timer t=new Timer(); |
| 29 |
|
| 30 |
String infile=args[0]; |
| 31 |
String outfile=args[1]; |
| 32 |
|
| 33 |
for(int i=2; i<args.length; i++){ |
| 34 |
String[] split=args[i].toLowerCase().split("="); |
| 35 |
String a=split[0]; |
| 36 |
String b=split.length>1 ? split[1] : null; |
| 37 |
|
| 38 |
if(a.equals("scorethresh")){ |
| 39 |
SCORE_THRESH=Float.parseFloat(b); |
| 40 |
}else if(a.equals("interval")){ |
| 41 |
INTERVAL=Integer.parseInt(b); |
| 42 |
}else if(a.equals("minsitestodiscard")){ |
| 43 |
MIN_SITES_TO_DISCARD=Integer.parseInt(b); |
| 44 |
}else if(a.equals("minlength")){ |
| 45 |
MIN_LENGTH_TO_RETAIN=Integer.parseInt(b); |
| 46 |
}else if(a.equals("retainall")){ |
| 47 |
RETAIN_ALL=Parse.parseBoolean(b); |
| 48 |
if(RETAIN_ALL){MIN_VOTES_TO_RETAIN=0;} |
| 49 |
}else if(a.equals("fractiontoretain1")){ |
| 50 |
FRACTION_TO_RETAIN1=Float.parseFloat(b); |
| 51 |
}else if(a.equals("fractiontoretain2")){ |
| 52 |
FRACTION_TO_RETAIN2=Float.parseFloat(b); |
| 53 |
}else if(a.equals("centerweight")){ |
| 54 |
CENTER_WEIGHT=Float.parseFloat(b); |
| 55 |
}else if(a.equals("sitestoretain1")){ |
| 56 |
SITES_TO_RETAIN1=Integer.parseInt(b); |
| 57 |
}else if(a.equals("sitestoretain2")){ |
| 58 |
SITES_TO_RETAIN2=Integer.parseInt(b); |
| 59 |
}else if(a.equals("minvotestoretain")){ |
| 60 |
MIN_VOTES_TO_RETAIN=Integer.parseInt(b); |
| 61 |
}else if(a.equals("mindistfromreadends")){ |
| 62 |
|
| 63 |
|
| 64 |
int x=Integer.parseInt(b); |
| 65 |
float f=x/((150-INTERVAL)*.5f); |
| 66 |
System.err.println("Warning - mindistfromreadends is deprecated. Setting minfractionfromreadends = "+Tools.format("%.3f",f)); |
| 67 |
MIN_FRACTION_FROM_READ_ENDS=f; |
| 68 |
}else if(a.equals("minfractionfromreadends")){ |
| 69 |
MIN_FRACTION_FROM_READ_ENDS=Float.parseFloat(b); |
| 70 |
}else{ |
| 71 |
assert(false) : "Unknown parameter "+a; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
process(infile, outfile); |
| 76 |
|
| 77 |
System.out.println("Sites In:\t"+sitesIn+" \t"+Tools.format("%.3f%% correct",correctIn*100d/sitesIn)); |
| 78 |
System.out.println("Sites Out:\t"+sitesOut+" \t"+Tools.format("%.3f%% correct",correctOut*100d/sitesOut)); |
| 79 |
t.stop(); |
| 80 |
System.out.println("Time: \t"+t); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public static void process(String infile, String outfile) { |
| 88 |
|
| 89 |
Buffer buffer=new Buffer(3, infile, outfile); |
| 90 |
|
| 91 |
int chrom=buffer.chrom; |
| 92 |
int start=buffer.min; |
| 93 |
int stop=buffer.min+INTERVAL-1; |
| 94 |
|
| 95 |
assert(buffer.array[0]!=null); |
| 96 |
while(buffer.array[0]!=null){ |
| 97 |
|
| 98 |
processInterval(buffer, chrom, start, stop); |
| 99 |
|
| 100 |
start+=INTERVAL; |
| 101 |
stop+=INTERVAL; |
| 102 |
boolean success=buffer.advanceToInterval(start, stop, chrom); |
| 103 |
if(!success){ |
| 104 |
chrom=buffer.chrom; |
| 105 |
start=buffer.min; |
| 106 |
stop=start+INTERVAL-1; |
| 107 |
} |
| 108 |
} |
| 109 |
buffer.close(); |
| 110 |
} |
| 111 |
|
| 112 |
private static void processInterval(Buffer buffer, int chrom, int start, int stop){ |
| 113 |
|
| 114 |
ArrayList<SiteScoreR> plus=new ArrayList<SiteScoreR>(); |
| 115 |
ArrayList<SiteScoreR> minus=new ArrayList<SiteScoreR>(); |
| 116 |
|
| 117 |
for(Ssra ssra : buffer.array){ |
| 118 |
|
| 119 |
if(Tools.isWithin(start, stop, ssra.min, ssra.max)){ |
| 120 |
for(SiteScoreR ssr : ssra.array){ |
| 121 |
|
| 122 |
int x=(int)((((ssr.stop-ssr.start+1)-INTERVAL)/2)*MIN_FRACTION_FROM_READ_ENDS); |
| 123 |
if(x<0){x=0;} |
| 124 |
|
| 125 |
if(ssr.readlen>=MIN_LENGTH_TO_RETAIN){ |
| 126 |
if(Tools.isWithin(start, stop, ssr.start+x, ssr.stop-x)){ |
| 127 |
ssr.normalizedScore=normalizedScore(ssr, Tools.min(start-ssr.start, ssr.stop-stop)); |
| 128 |
if(ssr.strand==Shared.PLUS){ |
| 129 |
plus.add(ssr); |
| 130 |
}else{ |
| 131 |
minus.add(ssr); |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
markRetain(plus); |
| 140 |
markRetain(minus); |
| 141 |
|
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
private static final int markRetain(ArrayList<SiteScoreR> list){ |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
int sites=list.size()-MIN_SITES_TO_DISCARD; |
| 192 |
|
| 193 |
int retain=(int)(sites*FRACTION_TO_RETAIN1); |
| 194 |
if(retain>SITES_TO_RETAIN1){ |
| 195 |
int temp=(int)((retain-SITES_TO_RETAIN1)*FRACTION_TO_RETAIN2); |
| 196 |
|
| 197 |
retain=SITES_TO_RETAIN1+temp; |
| 198 |
} |
| 199 |
retain=Tools.min(retain, SITES_TO_RETAIN2); |
| 200 |
|
| 201 |
if(RETAIN_ALL){retain=sites;} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
Shared.sort(list, SiteScoreR.NCOMP); |
| 209 |
|
| 210 |
final SiteScoreR best=(list!=null && list.size()>0 ? list.get(0) : null); |
| 211 |
for(int i=0; i<retain; i++){ |
| 212 |
final SiteScoreR b=list.get(i); |
| 213 |
if(i>0){ |
| 214 |
|
| 215 |
|
| 216 |
if(!RETAIN_ALL && best.score-b.score>best.score*SCORE_THRESH){break;} |
| 217 |
} |
| 218 |
|
| 219 |
if(i==0){ |
| 220 |
b.retainVotes+=5; |
| 221 |
}else if(i<4){ |
| 222 |
b.retainVotes+=3; |
| 223 |
}else if(i<8){ |
| 224 |
b.retainVotes+=2; |
| 225 |
}else{ |
| 226 |
b.retainVotes++; |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
return retain; |
| 231 |
} |
| 232 |
|
| 233 |
public static Ssra toSrar(String s){ |
| 234 |
String[] split=s.split("\t"); |
| 235 |
SiteScoreR[] scores=new SiteScoreR[split.length]; |
| 236 |
int min=Integer.MAX_VALUE; |
| 237 |
int max=Integer.MIN_VALUE; |
| 238 |
int worst=Integer.MAX_VALUE; |
| 239 |
int best=Integer.MIN_VALUE; |
| 240 |
int chrom=-1; |
| 241 |
|
| 242 |
for(int i=0; i<split.length; i++){ |
| 243 |
SiteScoreR ssr=scores[i]=SiteScoreR.fromText(split[i]); |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
min=Tools.min(min, ssr.start); |
| 253 |
max=Tools.max(max, ssr.stop); |
| 254 |
worst=Tools.min(worst, ssr.score); |
| 255 |
best=Tools.max(best, ssr.score); |
| 256 |
assert(chrom==-1 || chrom==ssr.chrom); |
| 257 |
chrom=ssr.chrom; |
| 258 |
} |
| 259 |
Ssra ssra=new Ssra(scores, chrom, min, max, best, worst); |
| 260 |
return ssra; |
| 261 |
} |
| 262 |
|
| 263 |
public static float normalizedScore(SiteScoreR ssr, int endDist){ |
| 264 |
final float lim1=0.008f; |
| 265 |
final float lim2=-lim1; |
| 266 |
|
| 267 |
|
| 268 |
int dif=ssr.readlen-ssr.reflen(); |
| 269 |
float modifier=dif/(float)(ssr.readlen*4); |
| 270 |
if(modifier<lim2){modifier=lim2;} |
| 271 |
if(modifier>lim1){modifier=lim1;} |
| 272 |
|
| 273 |
int maxEndDist=(ssr.reflen()-INTERVAL)/2; |
| 274 |
|
| 275 |
float modifier2=CENTER_WEIGHT*endDist/(float)maxEndDist; |
| 276 |
|
| 277 |
float f=ssr.score*(1+modifier+modifier2); |
| 278 |
return f; |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
public static int maxScore(Ssra ssra, final int min, final int max){ |
| 283 |
assert(Tools.overlap(min, max, ssra.min, ssra.max)); |
| 284 |
assert(Tools.isWithin(min, max, ssra.min, ssra.max)); |
| 285 |
|
| 286 |
int best=-1; |
| 287 |
for(SiteScoreR ssr : ssra.array){ |
| 288 |
if(ssr.start>min){break;} |
| 289 |
if(max>=ssr.stop){ |
| 290 |
best=Tools.max(best, ssr.score); |
| 291 |
if(best>=ssra.best){break;} |
| 292 |
} |
| 293 |
} |
| 294 |
return best; |
| 295 |
} |
| 296 |
|
| 297 |
public static class Ssra{ |
| 298 |
|
| 299 |
public Ssra(){} |
| 300 |
|
| 301 |
public Ssra(SiteScoreR[] array_, int chrom_, int min_, int max_, int best_, int worst_){ |
| 302 |
array=array_; |
| 303 |
chrom=chrom_; |
| 304 |
min=min_; |
| 305 |
max=max_; |
| 306 |
best=best_; |
| 307 |
worst=worst_; |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
SiteScoreR[] array; |
| 312 |
|
| 313 |
int chrom; |
| 314 |
|
| 315 |
int min; |
| 316 |
|
| 317 |
int max; |
| 318 |
|
| 319 |
int best; |
| 320 |
|
| 321 |
int worst; |
| 322 |
|
| 323 |
} |
| 324 |
|
| 325 |
public static class Buffer{ |
| 326 |
|
| 327 |
public Buffer(int size, String infname_, String outfname_){ |
| 328 |
assert(!infname_.equalsIgnoreCase(outfname_)) : infname_+" == "+outfname_; |
| 329 |
array=new Ssra[size]; |
| 330 |
infname=infname_; |
| 331 |
outfname=outfname_; |
| 332 |
tf=new TextFile(infname, true); |
| 333 |
tsw=new TextStreamWriter(outfname, true, false, true); |
| 334 |
tsw.start(); |
| 335 |
nextSsra=read(); |
| 336 |
fill(); |
| 337 |
|
| 338 |
} |
| 339 |
|
| 340 |
public Ssra read(){ |
| 341 |
String s=tf.nextLine(); |
| 342 |
if(s==null){ |
| 343 |
tf.close(); |
| 344 |
return null; |
| 345 |
} |
| 346 |
Ssra ssra=toSrar(s); |
| 347 |
sitesIn+=ssra.array.length; |
| 348 |
return ssra; |
| 349 |
} |
| 350 |
|
| 351 |
private boolean advance(){ |
| 352 |
if(nextSsra==null){return false;} |
| 353 |
|
| 354 |
Ssra old=add(nextSsra); |
| 355 |
nextSsra=read(); |
| 356 |
if(old!=null){write(old);} |
| 357 |
return true; |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
private boolean fill(){ |
| 362 |
assert(array[0]==null); |
| 363 |
if(nextSsra==null){return false;} |
| 364 |
int c=nextSsra.chrom; |
| 365 |
for(int i=0; i<array.length && nextSsra!=null && c==nextSsra.chrom; i++){ |
| 366 |
array[i]=nextSsra; |
| 367 |
nextSsra=read(); |
| 368 |
} |
| 369 |
setLimits(); |
| 370 |
return true; |
| 371 |
} |
| 372 |
|
| 373 |
public boolean advanceToInterval(final int a, final int b, final int c){ |
| 374 |
|
| 375 |
while(chrom<c || (chrom==c && max<a)){ |
| 376 |
purge(); |
| 377 |
boolean success=fill(); |
| 378 |
if(!success){return false;} |
| 379 |
} |
| 380 |
|
| 381 |
assert(array[0]!=null && chrom>=c); |
| 382 |
|
| 383 |
|
| 384 |
while(array[0].max<a && nextSsra!=null && nextSsra.chrom==c){ |
| 385 |
advance(); |
| 386 |
} |
| 387 |
|
| 388 |
return chrom==c && Tools.overlap(a, b, min, max); |
| 389 |
} |
| 390 |
|
| 391 |
private void purge() { |
| 392 |
for(int i=0; i<array.length; i++){ |
| 393 |
Ssra ssra=array[i]; |
| 394 |
if(ssra!=null){write(ssra);} |
| 395 |
array[i]=null; |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
private void write(Ssra ssra) { |
| 400 |
String tab=""; |
| 401 |
StringBuilder sb=new StringBuilder(ssra.array.length*48); |
| 402 |
|
| 403 |
final long sitesOut_0=sitesOut; |
| 404 |
for(SiteScoreR ssr : ssra.array){ |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
if(ssr.correct){correctIn++;} |
| 411 |
if(ssr.retainVotes>=MIN_VOTES_TO_RETAIN){ |
| 412 |
sitesOut++; |
| 413 |
if(ssr.correct){correctOut++;} |
| 414 |
sb.append(tab); |
| 415 |
sb.append(ssr.toText()); |
| 416 |
tab="\t"; |
| 417 |
} |
| 418 |
} |
| 419 |
if(sitesOut_0==sitesOut){return;} |
| 420 |
sb.append('\n'); |
| 421 |
tsw.print(sb); |
| 422 |
} |
| 423 |
|
| 424 |
public Ssra add(Ssra s){ |
| 425 |
|
| 426 |
assert(array[0]==null || array[0].chrom==s.chrom); |
| 427 |
|
| 428 |
Ssra r=null; |
| 429 |
if(array[array.length-1]==null){ |
| 430 |
|
| 431 |
for(int i=0; i<array.length; i++){ |
| 432 |
if(array[i]==null){ |
| 433 |
array[i]=s; |
| 434 |
break; |
| 435 |
} |
| 436 |
} |
| 437 |
}else{ |
| 438 |
r=array[0]; |
| 439 |
for(int i=1; i<array.length; i++){ |
| 440 |
array[i-1]=array[i]; |
| 441 |
} |
| 442 |
array[array.length-1]=s; |
| 443 |
} |
| 444 |
|
| 445 |
setLimits(); |
| 446 |
|
| 447 |
return r; |
| 448 |
} |
| 449 |
|
| 450 |
private void setLimits(){ |
| 451 |
max=Integer.MIN_VALUE; |
| 452 |
min=Integer.MAX_VALUE; |
| 453 |
chrom=array[0].chrom; |
| 454 |
for(int i=0; i<array.length; i++){ |
| 455 |
if(array[i]!=null){ |
| 456 |
min=Tools.min(min, array[i].min); |
| 457 |
max=Tools.max(max, array[i].max); |
| 458 |
} |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
public void close(){ |
| 463 |
purge(); |
| 464 |
while(fill()){purge();} |
| 465 |
tf.close(); |
| 466 |
tsw.poison(); |
| 467 |
} |
| 468 |
|
| 469 |
public int max=-1; |
| 470 |
public int min=-1; |
| 471 |
public int chrom=-1; |
| 472 |
|
| 473 |
public final Ssra[] array; |
| 474 |
private Ssra nextSsra; |
| 475 |
public final String infname; |
| 476 |
public final String outfname; |
| 477 |
private TextFile tf; |
| 478 |
private TextStreamWriter tsw; |
| 479 |
|
| 480 |
} |
| 481 |
|
| 482 |
public static int MIN_LENGTH_TO_RETAIN=0; |
| 483 |
public static boolean RETAIN_ALL=false; |
| 484 |
|
| 485 |
public static long sitesIn=0; |
| 486 |
public static long correctIn=0; |
| 487 |
public static long sitesOut=0; |
| 488 |
public static long correctOut=0; |
| 489 |
public static float FRACTION_TO_RETAIN1=0.75f; |
| 490 |
public static float FRACTION_TO_RETAIN2=0.3f; |
| 491 |
public static int MIN_SITES_TO_DISCARD=0; |
| 492 |
public static int SITES_TO_RETAIN1=8; |
| 493 |
public static int SITES_TO_RETAIN2=16; |
| 494 |
public static int MIN_VOTES_TO_RETAIN=5; |
| 495 |
|
| 496 |
public static float MIN_FRACTION_FROM_READ_ENDS=0.35f; |
| 497 |
public static float SCORE_THRESH=0.034f; |
| 498 |
public static float CENTER_WEIGHT=0.015f; |
| 499 |
public static int INTERVAL=12; |
| 500 |
|
| 501 |
} |