| 1 |
#include <stdio.h> |
| 2 |
#include <stdlib.h> |
| 3 |
#include <unistd.h> |
| 4 |
#include <string.h> |
| 5 |
#include <errno.h> |
| 6 |
|
| 7 |
#include "bio++.H" |
| 8 |
#include "sim4.H" |
| 9 |
|
| 10 |
//#define MIN_EXON_LENGTH 50 |
| 11 |
//#define MIN_PERCENT_IDENTITY 88 |
| 12 |
|
| 13 |
#define MIN_EXON_LENGTH 20 |
| 14 |
#define MIN_PERCENT_IDENTITY 90 |
| 15 |
|
| 16 |
|
| 17 |
bool |
| 18 |
lowComplexityExon(char *s) { |
| 19 |
int cnt[5][5] = { {0,0,0,0,0}, {0,0,0,0,0}, {0,0,0,0,0}, {0,0,0,0,0}, {0,0,0,0,0}}; |
| 20 |
int map[256] = {0}; |
| 21 |
int i, j, l = 0; |
| 22 |
int a=0, b=0, c=0; |
| 23 |
double qual = 0.0; |
| 24 |
|
| 25 |
if (s == 0L) |
| 26 |
return(false); |
| 27 |
|
| 28 |
map['A'] = map['a'] = 1; |
| 29 |
map['C'] = map['c'] = 2; |
| 30 |
map['G'] = map['g'] = 3; |
| 31 |
map['T'] = map['t'] = 4; |
| 32 |
|
| 33 |
for (i=0, j=1, l=0; s[j]; i++, j++, l++) |
| 34 |
cnt[map[s[i]]][map[s[j]]]++; |
| 35 |
|
| 36 |
if (l > MIN_EXON_LENGTH) |
| 37 |
return(false); |
| 38 |
|
| 39 |
for (i=0; i<5; i++) { |
| 40 |
for (j=0; j<5; j++) { |
| 41 |
if (a < cnt[i][j]) { |
| 42 |
c = b; |
| 43 |
b = a; |
| 44 |
a = cnt[i][j]; |
| 45 |
} else if (b < cnt[i][j]) { |
| 46 |
c = b; |
| 47 |
b = cnt[i][j]; |
| 48 |
} else if (c < cnt[i][j]) { |
| 49 |
c = cnt[i][j]; |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
qual = (double)(a+b+c) / (double)(l); |
| 55 |
|
| 56 |
return(qual > 0.75); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
// Delete exons before/after a specific intron. |
| 61 |
// |
| 62 |
void |
| 63 |
trimExonsBefore(int intronSplit, sim4polish *p) { |
| 64 |
for (int i=0; i<intronSplit; i++) |
| 65 |
p->s4p_deleteExon(0); |
| 66 |
} |
| 67 |
|
| 68 |
void |
| 69 |
trimExonsAfter(int intronSplit, sim4polish *p) { |
| 70 |
for (int i=p->_numExons-1; i>=intronSplit; i--) |
| 71 |
p->s4p_deleteExon(i); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
int |
| 77 |
main(int argc, char ** argv) { |
| 78 |
int totMatches = 0; |
| 79 |
int oneExon = 0; |
| 80 |
int smaIntron = 0; |
| 81 |
int junkFirst = 0; |
| 82 |
int junkLast = 0; |
| 83 |
int junkBoth = 0; |
| 84 |
int splitOnGap = 0; |
| 85 |
int goodQual = 0; |
| 86 |
int probGood = 0; |
| 87 |
|
| 88 |
bool filter = true; |
| 89 |
bool saveJunk = false; |
| 90 |
uint32 intronLimit = 100000; |
| 91 |
|
| 92 |
// Before / after files |
| 93 |
// |
| 94 |
bool beforeafter = false; |
| 95 |
#if 0 |
| 96 |
sim4polishWriter *splGood = 0L; |
| 97 |
sim4polishWriter *splProbGood = 0L; |
| 98 |
#endif |
| 99 |
sim4polishWriter *splJunkLeft = 0L; |
| 100 |
sim4polishWriter *splJunkRight = 0L; |
| 101 |
sim4polishWriter *splJunkBoth = 0L; |
| 102 |
sim4polishWriter *splIntronGap = 0L; |
| 103 |
|
| 104 |
// Segregation files |
| 105 |
// |
| 106 |
bool segregate = false; |
| 107 |
#if 0 |
| 108 |
sim4polishWriter *filtOne = 0L; |
| 109 |
sim4polishWriter *filtAllSmall = 0L; |
| 110 |
#endif |
| 111 |
sim4polishWriter *filtGood = 0L; |
| 112 |
sim4polishWriter *filtProbGood = 0L; |
| 113 |
sim4polishWriter *filtJunkLeft = 0L; |
| 114 |
sim4polishWriter *filtJunkRight = 0L; |
| 115 |
sim4polishWriter *filtJunkBoth = 0L; |
| 116 |
sim4polishWriter *filtIntronGap = 0L; |
| 117 |
|
| 118 |
sim4polishStyle style = sim4polishStyleDefault; |
| 119 |
|
| 120 |
bool hasBeenWarned = false; |
| 121 |
|
| 122 |
bool beVerbose = false; |
| 123 |
|
| 124 |
int arg = 1; |
| 125 |
int err = 0; |
| 126 |
while (arg < argc) { |
| 127 |
if (strncmp(argv[arg], "-threshold", 2) == 0) { |
| 128 |
intronLimit = atoi(argv[++arg]); |
| 129 |
|
| 130 |
} else if (strncmp(argv[arg], "-quiet", 2) == 0) { |
| 131 |
fprintf(stderr, "QUIET MODE ENABLED -- non-modified matches not output!\n"); |
| 132 |
filter = false; |
| 133 |
|
| 134 |
} else if (strncmp(argv[arg], "-beforeafter", 2) == 0) { |
| 135 |
fprintf(stderr, "DEBUG MODE ENABLED -- many 'spl.*' files created!\n"); |
| 136 |
beforeafter = true; |
| 137 |
|
| 138 |
} else if (strncmp(argv[arg], "-segregate", 3) == 0) { |
| 139 |
fprintf(stderr, "SEGREGATION MODE ENABLED -- many 'filt.*' files created!\n"); |
| 140 |
segregate = true; |
| 141 |
|
| 142 |
} else if (strncmp(argv[arg], "-gff3", 5) == 0) { |
| 143 |
style = sim4polishGFF3; |
| 144 |
|
| 145 |
} else if (strncmp(argv[arg], "-savejunk", 3) == 0) { |
| 146 |
saveJunk = true; |
| 147 |
|
| 148 |
} else if (strncmp(argv[arg], "-verbose", 2) == 0) { |
| 149 |
beVerbose = true; |
| 150 |
|
| 151 |
} else { |
| 152 |
err++; |
| 153 |
} |
| 154 |
|
| 155 |
arg++; |
| 156 |
} |
| 157 |
if ((err) || |
| 158 |
(isatty(fileno(stdin))) || |
| 159 |
(isatty(fileno(stdout)) && filter)) { |
| 160 |
fprintf(stderr, "usage: %s [-threshold t] [-savejunk] [-gff3] [-quiet] [-debug]\n", argv[0]); |
| 161 |
fprintf(stderr, " -threshold Introns bigger than this are candidates for trimming (default = 100000).\n"); |
| 162 |
fprintf(stderr, " -quiet Don't print unmodified matches\n"); |
| 163 |
fprintf(stderr, " -beforeafter Save (in separate files) the before/after of each modified match\n"); |
| 164 |
fprintf(stderr, " -segregate Save (in separate files) the after of each modified match\n"); |
| 165 |
fprintf(stderr, " -gff3 Write output in GFF3 format\n"); |
| 166 |
fprintf(stderr, " -savejunk Also print the trimmed pieces (as separate matches)\n"); |
| 167 |
|
| 168 |
if (isatty(fileno(stdin))) |
| 169 |
fprintf(stderr, "error: I cannot read polishes from the terminal!\n"); |
| 170 |
|
| 171 |
if (isatty(fileno(stdout)) && filter) |
| 172 |
fprintf(stderr, "error: Please redirect the polishes (stdout) to a file.\n"); |
| 173 |
|
| 174 |
exit(1); |
| 175 |
} |
| 176 |
|
| 177 |
if (beVerbose) |
| 178 |
fprintf(stderr, "A big intron is one that is at least "uint32FMT"bp long.\n", intronLimit); |
| 179 |
|
| 180 |
if (beforeafter) { |
| 181 |
#if 0 |
| 182 |
splGood = new sim4polishWriter("spl.good", style); |
| 183 |
splProbGood = new sim4polishWriter("spl.probGood", style); |
| 184 |
#endif |
| 185 |
splJunkLeft = new sim4polishWriter("spl.junkLeft", style); |
| 186 |
splJunkRight = new sim4polishWriter("spl.junkRight", style); |
| 187 |
splJunkBoth = new sim4polishWriter("spl.junkBoth", style); |
| 188 |
splIntronGap = new sim4polishWriter("spl.intronGap", style); |
| 189 |
} |
| 190 |
|
| 191 |
if (segregate) { |
| 192 |
#if 0 |
| 193 |
filtOne = new sim4polishWriter("filt.filtOne", style); |
| 194 |
filtAllSmall = new sim4polishWriter("filt.allSmall", style); |
| 195 |
#endif |
| 196 |
filtGood = new sim4polishWriter("filt.good", style); |
| 197 |
filtProbGood = new sim4polishWriter("filt.probGood", style); |
| 198 |
filtJunkLeft = new sim4polishWriter("filt.junkLeft", style); |
| 199 |
filtJunkRight = new sim4polishWriter("filt.junkRight", style); |
| 200 |
filtJunkBoth = new sim4polishWriter("filt.junkBoth", style); |
| 201 |
filtIntronGap = new sim4polishWriter("filt.intronGap", style); |
| 202 |
} |
| 203 |
|
| 204 |
sim4polishWriter *W = new sim4polishWriter("-", style); |
| 205 |
sim4polishReader *R = new sim4polishReader("-"); |
| 206 |
sim4polish *p = 0L; |
| 207 |
|
| 208 |
if (R->getsim4polishStyle() != style) |
| 209 |
fprintf(stderr, "warning: input format and output format differ.\n"); |
| 210 |
|
| 211 |
while (R->nextAlignment(p)) { |
| 212 |
uint32 exA; |
| 213 |
uint32 exB; |
| 214 |
|
| 215 |
if (p->_numExons == 1) { |
| 216 |
oneExon++; |
| 217 |
if (filter) |
| 218 |
W->writeAlignment(p); |
| 219 |
#if 0 |
| 220 |
if (segregate) |
| 221 |
filtOneExon->writeAlignment(p); |
| 222 |
#endif |
| 223 |
} else { |
| 224 |
|
| 225 |
// Find the big intron. We assume there is only one big intron. |
| 226 |
// |
| 227 |
uint32 biggestIntron = 0; |
| 228 |
uint32 intronSplit = 0; |
| 229 |
uint32 intronOri = 0; |
| 230 |
|
| 231 |
for (exA=0, exB=1; exB < p->_numExons; exA++, exB++) { |
| 232 |
uint32 dist = p->_exons[exB]._genFrom - p->_exons[exA]._genTo + 1; |
| 233 |
if (dist > biggestIntron) { |
| 234 |
biggestIntron = dist; |
| 235 |
intronSplit = exB; |
| 236 |
intronOri = p->_exons[exA]._intronOrientation; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
if (intronOri == 0) { |
| 241 |
fprintf(stderr, "didn't find the largest intron? (got zero)?\n"); |
| 242 |
exit(1); |
| 243 |
} |
| 244 |
|
| 245 |
if (intronOri == SIM4_INTRON_NONE) { |
| 246 |
fprintf(stderr, "biggest intron isn't an intron? (got none)?\n"); |
| 247 |
exit(1); |
| 248 |
} |
| 249 |
|
| 250 |
if (biggestIntron < intronLimit) { |
| 251 |
smaIntron++; |
| 252 |
if (filter) |
| 253 |
W->writeAlignment(p); |
| 254 |
#if 0 |
| 255 |
if (segregate) |
| 256 |
filtAllSmall->writeAlignment(p); |
| 257 |
#endif |
| 258 |
} else { |
| 259 |
|
| 260 |
// Declare the split obvious if all exons on either side are |
| 261 |
// below MIN_EXON_LENGTH, difficult otherwise. |
| 262 |
// |
| 263 |
bool killFirst = true; |
| 264 |
bool killLast = true; |
| 265 |
|
| 266 |
for (uint32 i=0; i<intronSplit; i++) |
| 267 |
if ((p->_exons[i]._estTo - p->_exons[i]._estFrom + 1 >= MIN_EXON_LENGTH) && |
| 268 |
(p->_exons[i]._percentIdentity >= MIN_PERCENT_IDENTITY) && |
| 269 |
(lowComplexityExon(p->_exons[i]._estAlignment) == false)) |
| 270 |
killFirst = false; |
| 271 |
|
| 272 |
for (uint32 i=intronSplit; i<p->_numExons; i++) |
| 273 |
if ((p->_exons[i]._estTo - p->_exons[i]._estFrom + 1 >= MIN_EXON_LENGTH) && |
| 274 |
(p->_exons[i]._percentIdentity >= MIN_PERCENT_IDENTITY) && |
| 275 |
(lowComplexityExon(p->_exons[i]._estAlignment) == false)) |
| 276 |
killLast = false; |
| 277 |
|
| 278 |
|
| 279 |
// Sometimes, all exons look crappy. If they have a large |
| 280 |
// intron too, just kill the match. |
| 281 |
// |
| 282 |
if ((killFirst == true) && (killLast == true)) { |
| 283 |
junkBoth++; |
| 284 |
|
| 285 |
if ((hasBeenWarned == false) && |
| 286 |
((p->_exons[0]._estAlignment == 0L) || (p->_exons[0]._genAlignment == 0L))) { |
| 287 |
hasBeenWarned = true; |
| 288 |
fprintf(stderr, "cleanPolishes: Need alignments to recompute scores correctly!\n"); |
| 289 |
} |
| 290 |
|
| 291 |
sim4polish *a = new sim4polish(p); |
| 292 |
sim4polish *b = new sim4polish(p); |
| 293 |
trimExonsAfter(intronSplit, a); |
| 294 |
trimExonsBefore(intronSplit, b); |
| 295 |
|
| 296 |
if (filter && saveJunk) { |
| 297 |
W->writeAlignment(a); |
| 298 |
W->writeAlignment(b); |
| 299 |
} |
| 300 |
|
| 301 |
if (beforeafter) { |
| 302 |
//fprintf(splJunkBoth, "====================\n"); |
| 303 |
splJunkBoth->writeAlignment(p); |
| 304 |
splJunkBoth->writeAlignment(a); |
| 305 |
splJunkBoth->writeAlignment(b); |
| 306 |
} |
| 307 |
|
| 308 |
if (segregate) { |
| 309 |
filtJunkBoth->writeAlignment(a); |
| 310 |
filtJunkBoth->writeAlignment(b); |
| 311 |
} |
| 312 |
|
| 313 |
delete a; |
| 314 |
delete b; |
| 315 |
} |
| 316 |
|
| 317 |
// If the first half (before the big intron) is crappy, delete |
| 318 |
// those exons. |
| 319 |
// |
| 320 |
if ((killFirst == true) && (killLast == false)) { |
| 321 |
junkFirst++; |
| 322 |
|
| 323 |
sim4polish *a = new sim4polish(p); |
| 324 |
sim4polish *b = new sim4polish(p); |
| 325 |
trimExonsAfter(intronSplit, a); |
| 326 |
trimExonsBefore(intronSplit, b); |
| 327 |
|
| 328 |
if (filter) { |
| 329 |
if (saveJunk) |
| 330 |
W->writeAlignment(a); |
| 331 |
W->writeAlignment(b); |
| 332 |
} |
| 333 |
|
| 334 |
if (beforeafter) { |
| 335 |
//fprintf(splJunkLeft, "====================\n"); |
| 336 |
splJunkLeft->writeAlignment(p); |
| 337 |
splJunkLeft->writeAlignment(a); |
| 338 |
splJunkLeft->writeAlignment(b); |
| 339 |
} |
| 340 |
|
| 341 |
if (segregate) { |
| 342 |
filtJunkLeft->writeAlignment(a); |
| 343 |
filtJunkLeft->writeAlignment(b); |
| 344 |
} |
| 345 |
|
| 346 |
delete a; |
| 347 |
delete b; |
| 348 |
} |
| 349 |
|
| 350 |
if ((killFirst == false) && (killLast == true)) { |
| 351 |
junkLast++; |
| 352 |
|
| 353 |
sim4polish *a = new sim4polish(p); |
| 354 |
sim4polish *b = new sim4polish(p); |
| 355 |
trimExonsAfter(intronSplit, a); |
| 356 |
trimExonsBefore(intronSplit, b); |
| 357 |
|
| 358 |
if (filter) { |
| 359 |
W->writeAlignment(a); |
| 360 |
if (saveJunk) |
| 361 |
W->writeAlignment(b); |
| 362 |
} |
| 363 |
|
| 364 |
if (beforeafter) { |
| 365 |
//fprintf(splJunkRight, "====================\n"); |
| 366 |
splJunkRight->writeAlignment(p); |
| 367 |
splJunkRight->writeAlignment(a); |
| 368 |
splJunkRight->writeAlignment(b); |
| 369 |
} |
| 370 |
|
| 371 |
if (segregate) { |
| 372 |
filtJunkRight->writeAlignment(a); |
| 373 |
filtJunkRight->writeAlignment(b); |
| 374 |
} |
| 375 |
|
| 376 |
delete a; |
| 377 |
delete b; |
| 378 |
} |
| 379 |
|
| 380 |
if ((killFirst == false) && (killLast == false)) { |
| 381 |
if (intronOri == SIM4_INTRON_GAP) { |
| 382 |
splitOnGap++; |
| 383 |
|
| 384 |
// Break the polish into two pieces, one before and one |
| 385 |
// after the large intron. This is done by copying the |
| 386 |
// entire polish, then deleting one half from each. |
| 387 |
// |
| 388 |
// XXX If we want to update the strand prediction of the |
| 389 |
// split pieces, we should |
| 390 |
// |
| 391 |
// a) make sure that all the intron signals agree |
| 392 |
// b) make sure that the percent identites of each exon are > 90% |
| 393 |
// |
| 394 |
// For now, we don't. |
| 395 |
|
| 396 |
sim4polish *a = new sim4polish(p); |
| 397 |
sim4polish *b = new sim4polish(p); |
| 398 |
trimExonsBefore(intronSplit, a); |
| 399 |
trimExonsAfter(intronSplit, b); |
| 400 |
|
| 401 |
if (filter) { |
| 402 |
W->writeAlignment(a); |
| 403 |
W->writeAlignment(b); |
| 404 |
} |
| 405 |
|
| 406 |
if (beforeafter) { |
| 407 |
//fprintf(splIntronGap, "====================\n"); |
| 408 |
splIntronGap->writeAlignment(p); |
| 409 |
splIntronGap->writeAlignment(a); |
| 410 |
splIntronGap->writeAlignment(b); |
| 411 |
} |
| 412 |
|
| 413 |
if (segregate) { |
| 414 |
filtIntronGap->writeAlignment(a); |
| 415 |
filtIntronGap->writeAlignment(b); |
| 416 |
} |
| 417 |
|
| 418 |
delete a; |
| 419 |
delete b; |
| 420 |
} else { |
| 421 |
|
| 422 |
// If there is a valid strand prediction and |
| 423 |
// a) all exons >= 90% |
| 424 |
// b) all exons >= 95% |
| 425 |
// c) all exons >= 95%, except first and last, which can be >= 90% |
| 426 |
// save the match as is. |
| 427 |
// |
| 428 |
|
| 429 |
bool qualIsC = ((p->_exons[0]._percentIdentity >= 90) && |
| 430 |
(p->_exons[p->_numExons-1]._percentIdentity >= 90)); |
| 431 |
|
| 432 |
for (exA=1; exA < p->_numExons-1; exA++) |
| 433 |
if (p->_exons[exA]._percentIdentity < 95) |
| 434 |
qualIsC = false; |
| 435 |
|
| 436 |
// If the match looks good, but just has a large intron, keep it. |
| 437 |
// |
| 438 |
if (qualIsC && |
| 439 |
((p->_strandOrientation == SIM4_STRAND_POSITIVE) || |
| 440 |
(p->_strandOrientation == SIM4_STRAND_NEGATIVE))) { |
| 441 |
goodQual++; |
| 442 |
if (filter) |
| 443 |
W->writeAlignment(p); |
| 444 |
if (segregate) |
| 445 |
filtGood->writeAlignment(p); |
| 446 |
} else { |
| 447 |
probGood++; |
| 448 |
if (filter) |
| 449 |
W->writeAlignment(p); |
| 450 |
if (segregate) |
| 451 |
filtProbGood->writeAlignment(p); |
| 452 |
} |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
} // Has a big intron |
| 457 |
} // More than one exon |
| 458 |
|
| 459 |
totMatches++; |
| 460 |
} |
| 461 |
|
| 462 |
delete R; |
| 463 |
delete W; |
| 464 |
|
| 465 |
if (beforeafter) { |
| 466 |
#if 0 |
| 467 |
delete splGood; |
| 468 |
delete splProbGood; |
| 469 |
#endif |
| 470 |
delete splJunkLeft; |
| 471 |
delete splJunkRight; |
| 472 |
delete splJunkBoth; |
| 473 |
delete splIntronGap; |
| 474 |
} |
| 475 |
|
| 476 |
if (segregate) { |
| 477 |
#if 0 |
| 478 |
delete filtOne; |
| 479 |
delete filtAllSmall; |
| 480 |
#endif |
| 481 |
delete filtGood; |
| 482 |
delete filtProbGood; |
| 483 |
delete filtJunkLeft; |
| 484 |
delete filtJunkRight; |
| 485 |
delete filtJunkBoth; |
| 486 |
delete filtIntronGap; |
| 487 |
} |
| 488 |
|
| 489 |
if (beVerbose) { |
| 490 |
fprintf(stderr, "\n"); |
| 491 |
fprintf(stderr, "oneExon: %7d\n", oneExon); |
| 492 |
fprintf(stderr, "allSmallIntrons: %7d\n", smaIntron); |
| 493 |
fprintf(stderr, "good: %7d\n", goodQual); |
| 494 |
fprintf(stderr, "probably good: %7d\n", probGood); |
| 495 |
fprintf(stderr, "junkExonsLeft: %7d\n", junkFirst); |
| 496 |
fprintf(stderr, "junkExonsRight: %7d\n", junkLast); |
| 497 |
fprintf(stderr, "junkExonsBoth: %7d\n", junkBoth); |
| 498 |
fprintf(stderr, "intronOnGap: %7d\n", splitOnGap); |
| 499 |
fprintf(stderr, "total: %7d\n", totMatches); |
| 500 |
} |
| 501 |
|
| 502 |
return(0); |
| 503 |
} |