| 1 |
#include <stdio.h> |
| 2 |
#include <stdlib.h> |
| 3 |
#include <unistd.h> |
| 4 |
#include <string.h> |
| 5 |
#include <errno.h> |
| 6 |
|
| 7 |
#include "sim4reader.h" |
| 8 |
|
| 9 |
#define SHOWTRIMMING |
| 10 |
|
| 11 |
char const *usage = |
| 12 |
"usage: %s [-save splitFile] [-threshold t]\n" |
| 13 |
" -threshold Introns bigger than this are split into two matches (default = 150000).\n" |
| 14 |
" -savesplits Saves a before/after of each split match.\n" |
| 15 |
" All matches are printed to stdout (untrimmed and trimmed).\n" |
| 16 |
"\n"; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
bool |
| 21 |
lowComplexityExon(char *s) { |
| 22 |
int cnt[5][5] = {0}; |
| 23 |
int map[256] = {0}; |
| 24 |
int i, j, len = 0; |
| 25 |
int a=0, b=0, c=0; |
| 26 |
double qual = 0.0; |
| 27 |
|
| 28 |
if (s == 0L) |
| 29 |
return(false); |
| 30 |
|
| 31 |
map['A'] = map['a'] = 1; |
| 32 |
map['C'] = map['c'] = 2; |
| 33 |
map['G'] = map['g'] = 3; |
| 34 |
map['T'] = map['t'] = 4; |
| 35 |
|
| 36 |
for (i=0; i<5; i++) |
| 37 |
for (j=0; j<5; j++) |
| 38 |
cnt[i][j] = 0; |
| 39 |
|
| 40 |
for (i=0, j=1; s[j]; i++, j++) { |
| 41 |
cnt[map[s[i]]][map[s[j]]]++; |
| 42 |
len++; |
| 43 |
} |
| 44 |
|
| 45 |
for (i=0; i<5; i++) { |
| 46 |
for (j=0; j<5; j++) { |
| 47 |
if (a < cnt[i][j]) { |
| 48 |
c = b; |
| 49 |
b = a; |
| 50 |
a = cnt[i][j]; |
| 51 |
} else if (b < cnt[i][j]) { |
| 52 |
c = b; |
| 53 |
b = cnt[i][j]; |
| 54 |
} else if (c < cnt[i][j]) { |
| 55 |
c = cnt[i][j]; |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
qual = (double)(a+b+c) / (double)(len); |
| 61 |
|
| 62 |
if (len > 50) |
| 63 |
qual = 0.0; |
| 64 |
|
| 65 |
//if (qual > 0.75) |
| 66 |
//fprintf(stdout, "%8.5f:\t%s\n", qual, s); |
| 67 |
|
| 68 |
return(qual > 0.75); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
int |
| 75 |
main(int argc, char ** argv) { |
| 76 |
int arg = 1; |
| 77 |
FILE *splitFile = 0L; |
| 78 |
int intronLimit = 150000; |
| 79 |
sim4polish *p; |
| 80 |
|
| 81 |
#if 0 |
| 82 |
if (isatty(fileno(stdin)) || isatty(fileno(stdout))) { |
| 83 |
fprintf(stderr, usage, argv[0]); |
| 84 |
|
| 85 |
if (isatty(fileno(stdin))) |
| 86 |
fprintf(stderr, "error: I cannot read polishes from the terminal!\n\n"); |
| 87 |
|
| 88 |
if (isatty(fileno(stdout))) |
| 89 |
fprintf(stderr, "error: Please redirect the polishes to a file.\n (They are on stdout)\n\n"); |
| 90 |
|
| 91 |
exit(1); |
| 92 |
} |
| 93 |
#endif |
| 94 |
|
| 95 |
arg = 1; |
| 96 |
while (arg < argc) { |
| 97 |
if (strncmp(argv[arg], "-savesplits", 2) == 0) { |
| 98 |
arg++; |
| 99 |
errno=0; |
| 100 |
splitFile = fopen(argv[arg], "w"); |
| 101 |
if (errno) { |
| 102 |
fprintf(stderr, "Can't open '%s' for writing\n%s\n", argv[arg], strerror(errno)); |
| 103 |
exit(1); |
| 104 |
} |
| 105 |
} else if (strncmp(argv[arg], "-threshold", 2) == 0) { |
| 106 |
intronLimit = atoi(argv[++arg]); |
| 107 |
} |
| 108 |
|
| 109 |
arg++; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
// Statistics on the splitting quality / frequency |
| 114 |
int totMatches = 0; |
| 115 |
int oneExon = 0; |
| 116 |
int smaIntron = 0; |
| 117 |
int junkFirst = 0; |
| 118 |
int junkLast = 0; |
| 119 |
int junkBoth = 0; |
| 120 |
int splitOnGap = 0; |
| 121 |
int goodQual = 0; |
| 122 |
int flanking = 0; |
| 123 |
|
| 124 |
|
| 125 |
FILE *junkF = fopen("spl.junkfirst", "w"); |
| 126 |
FILE *junkL = fopen("spl.junklast", "w"); |
| 127 |
FILE *junkB = fopen("spl.junkboth", "w"); |
| 128 |
FILE *splGap = fopen("spl.splitGap", "w"); |
| 129 |
FILE *good = fopen("spl.good", "w"); |
| 130 |
FILE *flank = fopen("spl.flanking", "w"); |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
while ((p = readPolish(stdin)) != 0L) { |
| 135 |
int exA; |
| 136 |
int exB; |
| 137 |
|
| 138 |
|
| 139 |
if (p->numExons == 1) { |
| 140 |
oneExon++; |
| 141 |
} else { |
| 142 |
|
| 143 |
// Find the big intron. We assume there is only one big intron. |
| 144 |
// |
| 145 |
int biggestIntron = 0; |
| 146 |
int intronSplit = 0; |
| 147 |
int intronOri = 0; |
| 148 |
|
| 149 |
for (exA=0, exB=1; exB < p->numExons; exA++, exB++) { |
| 150 |
int dist = p->exons[exB].genFrom - p->exons[exA].genTo + 1; |
| 151 |
if (dist > biggestIntron) { |
| 152 |
biggestIntron = dist; |
| 153 |
intronSplit = exB; |
| 154 |
intronOri = p->exons[exA].intronOrientation; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
if (intronOri == 0) { |
| 159 |
fprintf(stderr, "didn't find the largest intron? (got zero)?\n"); |
| 160 |
exit(1); |
| 161 |
} |
| 162 |
|
| 163 |
if (intronOri == INTRON_NONE) { |
| 164 |
fprintf(stderr, "biggest intron isn't an intron? (got none)?\n"); |
| 165 |
exit(1); |
| 166 |
} |
| 167 |
|
| 168 |
if (biggestIntron < 100000) { |
| 169 |
smaIntron++; |
| 170 |
} else { |
| 171 |
|
| 172 |
|
| 173 |
// Declare the split obvious if all exons on either side are |
| 174 |
// below 30bp, difficult otherwise. |
| 175 |
// |
| 176 |
bool killFirst = true; |
| 177 |
bool killLast = true; |
| 178 |
|
| 179 |
for (int i=0; i<intronSplit; i++) |
| 180 |
if ((p->exons[i].estTo - p->exons[i].estFrom + 1 >= 50) && |
| 181 |
(p->exons[i].percentIdentity >= 88) && |
| 182 |
(lowComplexityExon(p->exons[i].estAlignment) == false)) |
| 183 |
killFirst = false; |
| 184 |
|
| 185 |
for (int i=intronSplit; i<p->numExons; i++) |
| 186 |
if ((p->exons[i].estTo - p->exons[i].estFrom + 1 >= 50) && |
| 187 |
(p->exons[i].percentIdentity >= 88) && |
| 188 |
(lowComplexityExon(p->exons[i].estAlignment) == false)) |
| 189 |
killLast = false; |
| 190 |
|
| 191 |
|
| 192 |
// We shouldn't ever want to kill both sides. |
| 193 |
// |
| 194 |
if ((killFirst == true) && (killLast == true)) { |
| 195 |
junkBoth++; |
| 196 |
fprintf(junkB, "==============================JUNK FIRST AND LAST?\n"); |
| 197 |
printPolish(junkB, p); |
| 198 |
} |
| 199 |
|
| 200 |
if ((killFirst == true) && (killLast == false)) { |
| 201 |
junkFirst++; |
| 202 |
printPolish(junkF, p); |
| 203 |
fprintf(junkF, "==============================\n"); |
| 204 |
} |
| 205 |
|
| 206 |
if ((killFirst == false) && (killLast == true)) { |
| 207 |
junkLast++; |
| 208 |
printPolish(junkL, p); |
| 209 |
fprintf(junkL, "==============================\n"); |
| 210 |
} |
| 211 |
|
| 212 |
if ((killFirst == false) && (killLast == false)) { |
| 213 |
if (intronOri == INTRON_GAP) { |
| 214 |
splitOnGap++; |
| 215 |
printPolish(splGap, p); |
| 216 |
fprintf(splGap, "==============================\n"); |
| 217 |
} else { |
| 218 |
|
| 219 |
// If there is a valid strand prediction and |
| 220 |
// a) all exons >= 90% |
| 221 |
// b) all exons >= 95% |
| 222 |
// c) all exons >= 95%, except first and last, which can be >= 90% |
| 223 |
// save the match as is. |
| 224 |
// |
| 225 |
bool validStrand = false; |
| 226 |
if ((p->strandOrientation == STRAND_POSITIVE) || |
| 227 |
(p->strandOrientation == STRAND_NEGATIVE)) |
| 228 |
validStrand = true; |
| 229 |
|
| 230 |
#if 0 |
| 231 |
bool qualIsA = true; |
| 232 |
for (exA=0; exA < p->numExons; exA++) |
| 233 |
if (p->exons[exA].percentIdentity < 90) |
| 234 |
qualIsA = false; |
| 235 |
|
| 236 |
bool qualIsB = true; |
| 237 |
for (exA=0; exA < p->numExons; exA++) |
| 238 |
if (p->exons[exA].percentIdentity < 95) |
| 239 |
qualIsB = false; |
| 240 |
#endif |
| 241 |
|
| 242 |
bool qualIsC = true; |
| 243 |
if (p->exons[0].percentIdentity < 90) |
| 244 |
qualIsC = false; |
| 245 |
if (p->exons[p->numExons-1].percentIdentity < 90) |
| 246 |
qualIsC = false; |
| 247 |
for (exA=1; exA < p->numExons-1; exA++) |
| 248 |
if (p->exons[exA].percentIdentity < 95) |
| 249 |
qualIsC = false; |
| 250 |
|
| 251 |
// If the match looks good, but just has a large intron, keep it. |
| 252 |
// |
| 253 |
if (validStrand && qualIsC) { |
| 254 |
printPolish(good, p); |
| 255 |
fprintf(good, "==============================\n"); |
| 256 |
goodQual++; |
| 257 |
} else { |
| 258 |
flanking++; |
| 259 |
printPolish(flank, p); |
| 260 |
fprintf(flank, "==============================\n"); |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
} // Has a big intron |
| 266 |
} // More than one exon |
| 267 |
|
| 268 |
totMatches++; |
| 269 |
if ((totMatches % 3759) == 0) { |
| 270 |
fprintf(stderr, "tot: %7d ", totMatches); |
| 271 |
fprintf(stderr, "one: %7d ", oneExon); |
| 272 |
fprintf(stderr, "sma: %7d ", smaIntron); |
| 273 |
fprintf(stderr, "jnkF: %7d ", junkFirst); |
| 274 |
fprintf(stderr, "jnkL: %7d ", junkLast); |
| 275 |
fprintf(stderr, "jnkB: %7d ", junkBoth); |
| 276 |
fprintf(stderr, "onGap: %7d ", splitOnGap); |
| 277 |
fprintf(stderr, "good: %7d ", goodQual); |
| 278 |
fprintf(stderr, "flank: %7d\r", flanking); |
| 279 |
} |
| 280 |
|
| 281 |
destroyPolish(p); |
| 282 |
} |
| 283 |
|
| 284 |
fclose(junkF); |
| 285 |
fclose(junkL); |
| 286 |
fclose(junkB); |
| 287 |
fclose(splGap); |
| 288 |
fclose(good); |
| 289 |
fclose(flank); |
| 290 |
|
| 291 |
fprintf(stderr, "tot: %7d ", totMatches); |
| 292 |
fprintf(stderr, "one: %7d ", oneExon); |
| 293 |
fprintf(stderr, "sma: %7d ", smaIntron); |
| 294 |
fprintf(stderr, "jnkF: %7d ", junkFirst); |
| 295 |
fprintf(stderr, "jnkL: %7d ", junkLast); |
| 296 |
fprintf(stderr, "jnkB: %7d ", junkBoth); |
| 297 |
fprintf(stderr, "onGap: %7d ", splitOnGap); |
| 298 |
fprintf(stderr, "good: %7d ", goodQual); |
| 299 |
fprintf(stderr, "flank: %7d\n", flanking); |
| 300 |
|
| 301 |
return(0); |
| 302 |
} |