| 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 trimmedFile]\n" |
| 13 |
" -savetrimming Saves a before/after of each trimmed match.\n" |
| 14 |
" All matches are printed to stdout (untrimmed and trimmed).\n" |
| 15 |
"\n"; |
| 16 |
|
| 17 |
int |
| 18 |
main(int argc, char ** argv) { |
| 19 |
int arg = 1; |
| 20 |
FILE *trimmedFile = 0L; |
| 21 |
int beVerbose = 0; |
| 22 |
sim4polish *p; |
| 23 |
int polishesProcessed = 0; |
| 24 |
int polishesTrimmed = 0; |
| 25 |
|
| 26 |
if (isatty(fileno(stdin)) || isatty(fileno(stdout))) { |
| 27 |
fprintf(stderr, usage, argv[0]); |
| 28 |
|
| 29 |
if (isatty(fileno(stdin))) |
| 30 |
fprintf(stderr, "error: I cannot read polishes from the terminal!\n\n"); |
| 31 |
|
| 32 |
if (isatty(fileno(stdout))) |
| 33 |
fprintf(stderr, "error: Please redirect the polishes to a file.\n (They are on stdout)\n\n"); |
| 34 |
|
| 35 |
exit(1); |
| 36 |
} |
| 37 |
|
| 38 |
arg = 1; |
| 39 |
while (arg < argc) { |
| 40 |
if (strncmp(argv[arg], "-savetrimming", 2) == 0) { |
| 41 |
arg++; |
| 42 |
errno=0; |
| 43 |
trimmedFile = fopen(argv[arg], "w"); |
| 44 |
if (errno) { |
| 45 |
fprintf(stderr, "Can't open '%s' for writing\n%s\n", argv[arg], strerror(errno)); |
| 46 |
exit(1); |
| 47 |
} |
| 48 |
} else if (strncmp(argv[arg], "-verbose", 2) == 0) { |
| 49 |
beVerbose = 1; |
| 50 |
} |
| 51 |
|
| 52 |
arg++; |
| 53 |
} |
| 54 |
|
| 55 |
while ((p = readPolish(stdin)) != 0L) { |
| 56 |
int trimFirst = 0; |
| 57 |
int trimLast = 0; |
| 58 |
|
| 59 |
/* Decide if we need to trim anything |
| 60 |
*/ |
| 61 |
if (p->numExons > 1) { |
| 62 |
int exA; |
| 63 |
int exB; |
| 64 |
int dist; |
| 65 |
int qual; |
| 66 |
int size; |
| 67 |
|
| 68 |
exA = 0; // First exon |
| 69 |
exB = 1; // Second exon |
| 70 |
dist = p->exons[exB].genFrom - p->exons[exA].genTo + 1; |
| 71 |
qual = p->exons[exA].percentIdentity; |
| 72 |
size = p->exons[exA].estTo - p->exons[exA].estFrom + 1; |
| 73 |
|
| 74 |
trimFirst = 1; |
| 75 |
|
| 76 |
if (dist < 100000) |
| 77 |
trimFirst = 0; |
| 78 |
|
| 79 |
if (size >= 50) |
| 80 |
trimFirst = 0; |
| 81 |
|
| 82 |
if (size >= 25 + (int)((dist - 100000) * 25.0 / 900000.0)) |
| 83 |
trimFirst = 0; |
| 84 |
|
| 85 |
if ((qual >= 98) && |
| 86 |
(size >= 25 + (int)((dist - 100000) * 25.0 / 1400000.0))) |
| 87 |
trimFirst = 0; |
| 88 |
|
| 89 |
// Reverse our decision if the first exon is of low quality. |
| 90 |
// |
| 91 |
if ((qual < 85) && (dist >= 10000)) { |
| 92 |
if (trimFirst == 0) |
| 93 |
fprintf(trimmedFile, "Trimming frist exon based only on percent ID\n"); |
| 94 |
trimFirst = 1; |
| 95 |
} |
| 96 |
|
| 97 |
exA = p->numExons - 1; // Last exon |
| 98 |
exB = p->numExons - 2; // Second to last |
| 99 |
dist = p->exons[exA].genFrom - p->exons[exB].genTo + 1; |
| 100 |
qual = p->exons[exA].percentIdentity; |
| 101 |
size = p->exons[exA].estTo - p->exons[exA].estFrom + 1; |
| 102 |
|
| 103 |
trimLast = 1; |
| 104 |
|
| 105 |
if (dist < 100000) |
| 106 |
trimLast = 0; |
| 107 |
|
| 108 |
if (size >= 50) |
| 109 |
trimLast = 0; |
| 110 |
|
| 111 |
if (size >= 25 + (int)((dist - 100000) * 25.0 / 900000.0)) |
| 112 |
trimLast = 0; |
| 113 |
|
| 114 |
if ((qual >= 98) && |
| 115 |
(size >= 25 + (int)((dist - 100000) * 25.0 / 1400000.0))) |
| 116 |
trimLast = 0; |
| 117 |
|
| 118 |
// Reverse our decision if the first exon is of low quality. |
| 119 |
// |
| 120 |
if ((qual < 85) && (dist >= 10000)) { |
| 121 |
if (trimLast == 0) |
| 122 |
fprintf(trimmedFile, "Trimming last exon based only on percent ID\n"); |
| 123 |
trimLast = 1; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
if (trimmedFile && (trimFirst || trimLast)) { |
| 128 |
fprintf(trimmedFile, "------------------------------------------------------------BEFORE\n"); |
| 129 |
printPolish(trimmedFile, p); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
if (beVerbose) { |
| 134 |
polishesProcessed++; |
| 135 |
if (trimFirst || trimLast) |
| 136 |
polishesTrimmed++; |
| 137 |
if ((polishesProcessed % 10000) == 0) { |
| 138 |
fprintf(stderr, " %d processed, %d trimmed (%8.5f%%)\r", |
| 139 |
polishesProcessed, polishesTrimmed, |
| 140 |
100.0 * (double)polishesTrimmed / (double)polishesProcessed); |
| 141 |
fflush(stderr); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
// If there is one intron, and we've been asked to remove |
| 147 |
// either the first or the last (it should say to remove |
| 148 |
// both), then remove the shorter of the two. |
| 149 |
// |
| 150 |
if ((trimFirst || trimLast) && (p->numExons == 2)) { |
| 151 |
trimFirst = 0; |
| 152 |
trimLast = 0; |
| 153 |
|
| 154 |
if ((p->exons[0].estTo - p->exons[0].estFrom) > (p->exons[1].estTo - p->exons[1].estFrom)) |
| 155 |
trimLast = 1; |
| 156 |
else |
| 157 |
trimFirst = 1; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
// Remove the first exon, by circularly shifting the list of |
| 163 |
// exons. The exon trimmed from the start is moved to the end of |
| 164 |
// the exon list. |
| 165 |
// |
| 166 |
if (trimFirst) { |
| 167 |
int i; |
| 168 |
sim4polishExon save; |
| 169 |
|
| 170 |
memcpy(&save, p->exons+0, sizeof(sim4polishExon)); |
| 171 |
|
| 172 |
for (i=1; i<p->numExons; i++) |
| 173 |
memcpy(p->exons+i-1, p->exons+i, sizeof(sim4polishExon)); |
| 174 |
|
| 175 |
memcpy(p->exons+p->numExons-1, &save, sizeof(sim4polishExon)); |
| 176 |
|
| 177 |
p->numExons--; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
// Trimming the last exon is easy; just decrement the size of the |
| 182 |
// list. |
| 183 |
// |
| 184 |
if (trimLast) { |
| 185 |
p->numExons--; |
| 186 |
|
| 187 |
// We also need to clear the intron orientation flag in the new |
| 188 |
// last exon |
| 189 |
// |
| 190 |
p->exons[p->numExons-1].intronOrientation = INTRON_NONE; |
| 191 |
} |
| 192 |
|
| 193 |
if (trimmedFile && (trimFirst || trimLast)) { |
| 194 |
fprintf(trimmedFile, "------------------------------------------------------------AFTER\n"); |
| 195 |
printPolish(trimmedFile, p); |
| 196 |
fprintf(trimmedFile, "============================================================EOP\n"); |
| 197 |
} |
| 198 |
|
| 199 |
printPolish(stdout, p); |
| 200 |
|
| 201 |
// Insert the exons back in, so they will be destroyed properly. |
| 202 |
// |
| 203 |
if (trimFirst) p->numExons++; |
| 204 |
if (trimLast) p->numExons++; |
| 205 |
|
| 206 |
destroyPolish(p); |
| 207 |
} |
| 208 |
|
| 209 |
return(0); |
| 210 |
} |