| 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <unistd.h> |
| 4 | #include <string.h> |
| 5 | #include <errno.h> |
| 6 | #include "aHit.H" |
| 7 | #include "hitReader.H" |
| 8 | |
| 9 | // XXX: Thread the filter! Really cool! Pretty neat hack! Usual |
| 10 | // thing, a thread to read hits, n threads to do filtering, and a |
| 11 | // thread to write filtered hits. Not trivial, but maybe a win. |
| 12 | |
| 13 | // Global statistics |
| 14 | // |
| 15 | uint32 hitsSaved = 0; |
| 16 | uint32 hitsFiltered = 0; |
| 17 | uint32 hitsUnknown = 0; |
| 18 | uint32 hitsTotal = 0; |
| 19 | |
| 20 | uint32 seqsMapped = 0; // Sequences that we mapped |
| 21 | uint32 seqsPartial = 0; // Sequences that we mapped, but missed a few good matches |
| 22 | uint32 seqsMissed = 0; // Sequences that we failed to map, but should have |
| 23 | |
| 24 | uint32 filterTP = 0; |
| 25 | uint32 filterFP = 0; |
| 26 | uint32 filterFNfilt = 0; // false negatives from filtering |
| 27 | uint32 filterFNunk = 0; // false negatives from our failure to classify |
| 28 | uint32 filterTN = 0; |
| 29 | |
| 30 | uint32 goodPercentID = 94; |
| 31 | uint32 goodCoverage = 50; |
| 32 | |
| 33 | // Command line options |
| 34 | // |
| 35 | uint32 uniqThresh = 200; // Used to be 100 |
| 36 | uint32 reptThresh = 200; // Used to be 100 |
| 37 | |
| 38 | FILE *logFile = 0L; |
| 39 | |
| 40 | // Filter results -- thread unsafe! |
| 41 | // |
| 42 | // bool decided -- true if the filter could decide on how to filter the hits |
| 43 | // char label -- if decided, the name of the decider |
| 44 | // uint32 hitsToSave -- the number of hits to save |
| 45 | // double qualToSave -- the quality threshold to filter at |
| 46 | // |
| 47 | bool decided; |
| 48 | const char *label; |
| 49 | uint32 hitsToSave; |
| 50 | double qualToSave; |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | void |
| 56 | report(uint32 iid |
| 57 | #ifdef WITH_ANSWERS |
| 58 | , |
| 59 | uint32 filterTP, |
| 60 | uint32 filterFP, |
| 61 | uint32 filterFNfilt, |
| 62 | uint32 filterFNunk, |
| 63 | uint32 filterTN, |
| 64 | uint32 seqsMapped, |
| 65 | uint32 seqsPartial, |
| 66 | uint32 seqsMissed |
| 67 | #endif |
| 68 | ) { |
| 69 | |
| 70 | fprintf(stderr, |
| 71 | uint32FMTW(9)"]" |
| 72 | #ifdef WITH_ANSWERS |
| 73 | " tp="uint32FMTW(7)" fp="uint32FMTW(7)" fnfilt="uint32FMTW(7)" fnunkn="uint32FMTW(7)" tn="uint32FMTW(7) |
| 74 | " yea:"uint32FMTW(7)" may:"uint32FMTW(7)" nay:"uint32FMTW(7) |
| 75 | #endif |
| 76 | " hits saved:"uint32FMTW(8)"/"uint32FMTW(8)" = %6.3f%%\r", |
| 77 | iid, |
| 78 | #ifdef WITH_ANSWERS |
| 79 | filterTP, filterFP, filterFNfilt, filterFNunk, filterTN, |
| 80 | seqsMapped, seqsPartial, seqsMissed, |
| 81 | #endif |
| 82 | hitsSaved, hitsTotal, |
| 83 | 100.0 * hitsSaved / hitsTotal); |
| 84 | } |
| 85 | |
| 86 | |
| 87 | |
| 88 | void |
| 89 | complicatedFilter(hitReader &HR); |
| 90 | |
| 91 | |
| 92 | |
| 93 | // The simple filter just returns the top uniqThresh hits |
| 94 | // |
| 95 | void |
| 96 | simpleFilter(hitReader &HR) { |
| 97 | |
| 98 | decided = true; |
| 99 | label = "simple"; |
| 100 | qualToSave = 0.0; |
| 101 | hitsToSave = HR.numHits(); |
| 102 | |
| 103 | if (HR.numHits() > uniqThresh) |
| 104 | hitsToSave = uniqThresh; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | int |
| 111 | main(int argc, char **argv) { |
| 112 | |
| 113 | if (argc == 1) { |
| 114 | fprintf(stderr, "ESTmapper utility function -- not for human use.\n"); |
| 115 | exit(1); |
| 116 | } |
| 117 | |
| 118 | hitReader HR(argc); |
| 119 | |
| 120 | int arg = 1; |
| 121 | while (arg < argc) { |
| 122 | if (strncmp(argv[arg], "-uniquethreshold", 2) == 0) { |
| 123 | uniqThresh = atoi(argv[++arg]); |
| 124 | } else if (strncmp(argv[arg], "-repeatthreshold", 2) == 0) { |
| 125 | reptThresh = atoi(argv[++arg]); |
| 126 | } else if (strncmp(argv[arg], "-log", 2) == 0) { |
| 127 | ++arg; |
| 128 | errno = 0; |
| 129 | logFile = fopen(argv[arg], "w"); |
| 130 | if (errno) { |
| 131 | fprintf(stderr, "filterEST: ERROR: couldn't open logFile '%s' for writing.\n%s\n", argv[arg], strerror(errno)); |
| 132 | exit(1); |
| 133 | } |
| 134 | } else { |
| 135 | HR.addInputFile(argv[arg]); |
| 136 | } |
| 137 | |
| 138 | arg++; |
| 139 | } |
| 140 | |
| 141 | while (HR.loadHits()) { |
| 142 | |
| 143 | // Not every filter we can think of needs the hits sorted, but |
| 144 | // it's nice to guarantee they are sorted. |
| 145 | // |
| 146 | HR.sortByCoverage(); |
| 147 | |
| 148 | |
| 149 | //simpleFilter(HR); |
| 150 | complicatedFilter(HR); |
| 151 | |
| 152 | |
| 153 | #ifdef WITH_ANSWERS |
| 154 | int tp=0, tn=0, fn=0, fp=0; |
| 155 | #endif |
| 156 | |
| 157 | // If we still haven't figured out what to do, then the EST is |
| 158 | // labeled a repeat. Otherwise, write the (filtered) hits to the |
| 159 | // file. |
| 160 | // |
| 161 | if (!decided) { |
| 162 | hitsUnknown += HR.numHits(); |
| 163 | |
| 164 | #ifdef WITH_ANSWERS |
| 165 | // We've failed to classify all these hits, so anythig that looks good is a false negative |
| 166 | // |
| 167 | for (uint32 i=0; i < HR.numHits(); i++) |
| 168 | if ((HR[i].mappedCoverage >= goodCoverage) && (HR[i].mappedIdentity >= goodPercentID)) { |
| 169 | if (logFile) { |
| 170 | fprintf(logFile, "FAILUNKN hit="uint32FMTW(3)" id=%2d cv=%2d COV=%5.3f MUL=%5.3f: ", i, HR[i].mappedIdentity, HR[i].mappedCoverage, HR[i].coverage, HR[i].multiplicity); |
| 171 | ahit_printASCII(&HR[i].a, logFile); |
| 172 | } |
| 173 | filterFNunk++; |
| 174 | fn++; |
| 175 | } else { |
| 176 | tn++; |
| 177 | } |
| 178 | #endif |
| 179 | |
| 180 | } else { |
| 181 | for (uint32 i=0; i < HR.numHits(); i++) { |
| 182 | if ((i < hitsToSave) && (qualToSave <= HR[i].coverage)) { |
| 183 | hitsSaved++; |
| 184 | ahit_printASCII(&HR[i].a, stdout); |
| 185 | |
| 186 | #ifdef WITH_ANSWERS |
| 187 | if ((HR[i].mappedCoverage >= goodCoverage) && (HR[i].mappedIdentity >= goodPercentID)) { |
| 188 | tp++; |
| 189 | } else { |
| 190 | fp++; |
| 191 | } |
| 192 | #endif |
| 193 | } else if (HR[i].a._merged) { |
| 194 | // We merged this hit, so scores are incorrect. Give it |
| 195 | // the benefit of the doubt and report it. |
| 196 | // |
| 197 | hitsSaved++; |
| 198 | ahit_printASCII(&HR[i].a, stdout); |
| 199 | } else { |
| 200 | hitsFiltered++; |
| 201 | #ifdef WITH_ANSWERS |
| 202 | // Report hits that are false negatives |
| 203 | // |
| 204 | if ((HR[i].mappedCoverage >= goodCoverage) && (HR[i].mappedIdentity >= goodPercentID)) { |
| 205 | if (logFile) { |
| 206 | fprintf(logFile, "FAILFILT hit="uint32FMTW(3)" id=%2d cv=%2d COV=%5.3f MUL=%5.3f: ", i, HR[i].mappedIdentity, HR[i].mappedCoverage, HR[i].coverage, HR[i].multiplicity); |
| 207 | ahit_printASCII(&HR[i].a, logFile); |
| 208 | } |
| 209 | filterFNfilt++; |
| 210 | fn++; |
| 211 | } else { |
| 212 | tn++; |
| 213 | } |
| 214 | #endif |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | #ifdef WITH_ANSWERS |
| 220 | filterTP += tp; |
| 221 | filterTN += tn; |
| 222 | filterFP += fp; |
| 223 | |
| 224 | if (tp > 0) |
| 225 | seqsMapped++; |
| 226 | |
| 227 | if (fn > 0) |
| 228 | seqsPartial++; |
| 229 | |
| 230 | if ((tp == 0) && (fn > 0)) |
| 231 | seqsMissed++; |
| 232 | #endif |
| 233 | |
| 234 | hitsTotal += HR.numHits(); |
| 235 | |
| 236 | #ifdef WITH_ANSWERS |
| 237 | // Report if we saw falsenegatives (we should have printed FAIL into the log, too) |
| 238 | // |
| 239 | if (fn > 0) |
| 240 | if (logFile) |
| 241 | fprintf(logFile, uint32FMT"] %sFALSENEGATIVE %10.10s tp="uint32FMTW(7)" fp="uint32FMTW(7)" fn="uint32FMTW(7)" tn="uint32FMTW(7)"\n", |
| 242 | HR.iid(), |
| 243 | (tp > 0) ? "partial" : "fatal", |
| 244 | label, |
| 245 | tp, fp, fn, tn); |
| 246 | #endif |
| 247 | |
| 248 | |
| 249 | if ((HR.iid() % 500) == 0) { |
| 250 | report(HR.iid() |
| 251 | #ifdef WITH_ANSWERS |
| 252 | , |
| 253 | filterTP, filterFP, filterFNfilt, filterFNunk, filterTN, |
| 254 | seqsMapped, seqsPartial, seqsMissed |
| 255 | #endif |
| 256 | ); |
| 257 | fflush(stderr); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | if (logFile) |
| 262 | fclose(logFile); |
| 263 | |
| 264 | |
| 265 | report(HR.iid() |
| 266 | #ifdef WITH_ANSWERS |
| 267 | , |
| 268 | filterTP, filterFP, filterFNfilt, filterFNunk, filterTN, |
| 269 | seqsMapped, seqsPartial, seqsMissed |
| 270 | #endif |
| 271 | ); |
| 272 | fprintf(stderr, "\n"); |
| 273 | |
| 274 | return(0); |
| 275 | } |