| 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 | |
| 10 | extern uint32 uniqThresh; |
| 11 | extern uint32 reptThresh; |
| 12 | extern FILE *logFile; |
| 13 | extern bool decided; |
| 14 | extern const char *label; |
| 15 | extern uint32 hitsToSave; |
| 16 | extern double qualToSave; |
| 17 | |
| 18 | double difference = 0.1; |
| 19 | |
| 20 | void |
| 21 | complicatedFilter_1_unique(hitReader &HR) { |
| 22 | |
| 23 | if (HR.numHits() <= uniqThresh) { |
| 24 | decided = true; |
| 25 | label = "unique"; |
| 26 | hitsToSave = HR.numHits(); |
| 27 | qualToSave = 0.0; |
| 28 | |
| 29 | // Try being a little more aggressive. Search for the last |
| 30 | // point where the score difference across 10 hits is more than |
| 31 | // difference and use that for a limit. |
| 32 | |
| 33 | |
| 34 | // On the 100k test set #1 (ESTmapper paper, 1 Oct 2004) this modification |
| 35 | // results in: |
| 36 | // tp=106564 fp=1255487 fn=56705 tn=52816595 |
| 37 | // |
| 38 | // compared to saving all hits: |
| 39 | // tp=106579 fp=1914659 fn=56690 tn=52157423 |
| 40 | // |
| 41 | // That is, we lost 15 true matches and didn't polish 660,000 |
| 42 | // matches -- 1.21% of the total, but 50% of what we actually |
| 43 | // need to polish. |
| 44 | // |
| 45 | uint32 i = HR.numHits() - 1; |
| 46 | while ((i >= 10) && ((HR[i-10].coverage - HR[i].coverage) < difference)) |
| 47 | i--; |
| 48 | |
| 49 | hitsToSave = HR.numHits(); |
| 50 | qualToSave = HR[i].coverage; |
| 51 | |
| 52 | #if 0 |
| 53 | // Take the middle hit, not the end. This doesn't hurt too much |
| 54 | // (20 matches out of 100,000 ESTs, and we missed one EST |
| 55 | // completely) but only gains us 0.08% additional filtering. |
| 56 | if (i >= 15) |
| 57 | qualToSave = HR[i-5].coverage; |
| 58 | #endif |
| 59 | |
| 60 | // Save all hits with this coverage score! This isn't really needed, but it |
| 61 | // makes the log message correct. |
| 62 | // |
| 63 | while ((i < HR.numHits()) && (qualToSave == HR[i].coverage)) |
| 64 | i++; |
| 65 | |
| 66 | if (logFile) |
| 67 | fprintf(logFile, uint32FMT"] unique: aggressively filtered to "uint32FMT" hits out of "uint32FMT" hits.\n", |
| 68 | HR.iid(), i, HR.numHits()); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | void |
| 77 | complicatedFilter_2_knee(hitReader &HR) { |
| 78 | |
| 79 | decided = false; |
| 80 | hitsToSave = 0; |
| 81 | qualToSave = 0.0; |
| 82 | |
| 83 | #if 0 |
| 84 | decided = true; |
| 85 | hitsToSave = 0; |
| 86 | qualToSave = 1.1; |
| 87 | return; |
| 88 | #endif |
| 89 | |
| 90 | // Apply the same filter as used in #1 (the aggressive part), and accept |
| 91 | // it if the number of hits saved is below some threshold. |
| 92 | |
| 93 | uint32 i = HR.numHits() - 1; |
| 94 | while ((i >= 10) && ((HR[i-10].coverage - HR[i].coverage) < difference)) |
| 95 | i--; |
| 96 | |
| 97 | // If i==9, then we failed to find a knee, and we fail this filter |
| 98 | // |
| 99 | if (i < 10) |
| 100 | return; |
| 101 | |
| 102 | hitsToSave = HR.numHits(); |
| 103 | qualToSave = HR[i].coverage; |
| 104 | |
| 105 | // Save all hits with this coverage score! |
| 106 | // |
| 107 | while ((i < HR.numHits()) && (qualToSave == HR[i].coverage)) |
| 108 | i++; |
| 109 | |
| 110 | if (i <= uniqThresh) { |
| 111 | decided = true; |
| 112 | label = "knee"; |
| 113 | |
| 114 | if (logFile) |
| 115 | fprintf(logFile, uint32FMT"] knee: filtered "uint32FMT" hits down to "uint32FMT" hits using threshold %f\n", |
| 116 | HR.iid(), HR.numHits(), i, qualToSave); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | |
| 121 | |
| 122 | // If all scores are about the same, it's either a repeat or a |
| 123 | // lot of spurious matches, depending on the level of signal. |
| 124 | // |
| 125 | void |
| 126 | complicatedFilter_3_uniform(hitReader &HR) { |
| 127 | |
| 128 | decided = false; |
| 129 | hitsToSave = 0; |
| 130 | qualToSave = 0.0; |
| 131 | |
| 132 | if ((HR.bestScore() - HR.worstScore()) < difference) { |
| 133 | decided = true; |
| 134 | label = "uniform"; |
| 135 | hitsToSave = reptThresh; |
| 136 | qualToSave = 0.0; |
| 137 | |
| 138 | if (logFile) { |
| 139 | fprintf(logFile, uint32FMT"] uniform: uniform signal strength, saving the first "uint32FMT" hits out of "uint32FMT" hits, best=%f, worst=%f\n", |
| 140 | HR.iid(), hitsToSave, HR.numHits(), HR.bestScore(), HR.worstScore()); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | // If we're not decided here, the EST had too many "good" hits to |
| 149 | // be filtered by the threshold method. Try a more sophisticated |
| 150 | // (confusing) method. |
| 151 | // |
| 152 | void |
| 153 | complicatedFilter_4_largestdifference(hitReader &HR) { |
| 154 | |
| 155 | decided = false; |
| 156 | hitsToSave = 0; |
| 157 | qualToSave = 0.0; |
| 158 | |
| 159 | double largestDifference = 0.0; |
| 160 | for (uint32 i=1; i < HR.numHits(); i++) |
| 161 | if (largestDifference < (HR[i-1].coverage - HR[i].coverage)) |
| 162 | largestDifference = HR[i-1].coverage - HR[i].coverage; |
| 163 | |
| 164 | // If the largest difference is below 10% coverage, then it's not |
| 165 | // clear how to pick a threshold and we just save a bunch of hits. |
| 166 | // |
| 167 | if (largestDifference < difference) { |
| 168 | decided = true; |
| 169 | label = "diff"; |
| 170 | hitsToSave = reptThresh; |
| 171 | qualToSave = 0.0; |
| 172 | |
| 173 | if (logFile) |
| 174 | fprintf(logFile, uint32FMT"] diff: has no clear signal knee, saving the first "uint32FMT" hits out of "uint32FMT" hits, best=%f, worst=%f, largestdiff=%f\n", |
| 175 | HR.iid(), hitsToSave, HR.numHits(), HR.bestScore(), HR.worstScore(), largestDifference); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | |
| 180 | |
| 181 | // Identify any spike near the start. If we see a spike, |
| 182 | // save the first uniqThresh hits. |
| 183 | // |
| 184 | // If the largest difference (which we guarantee to be >= 10% |
| 185 | // coverage here) is in the first uniqThresh hits, then we |
| 186 | // have a spike and we output uniqThresh hits. |
| 187 | // |
| 188 | // To narrow the range more, we find the last spot where the |
| 189 | // difference in scores over 10 hits is > difference. This is a |
| 190 | // generous heuristic. |
| 191 | // |
| 192 | void |
| 193 | complicatedFilter_5_spikes(hitReader &HR) { |
| 194 | |
| 195 | decided = false; |
| 196 | hitsToSave = 0; |
| 197 | qualToSave = 0.0; |
| 198 | |
| 199 | uint32 spikeFound = 0; |
| 200 | for (uint32 i=1; i < uniqThresh; i++) |
| 201 | if ((HR[i-1].coverage - HR[i].coverage) > difference) |
| 202 | spikeFound = i; |
| 203 | |
| 204 | // If we have found a spike, start at hit[uniqThresh], search |
| 205 | // backwards for the first point where the difference in |
| 206 | // scores across 10 hits is larger than difference |
| 207 | // |
| 208 | // Seems like a NOP, but it loosens things up a bit. Consider a |
| 209 | // spike between hits 3 and 4, but 1=2=3 and 4=5=6=7=8=9=10=11. We |
| 210 | // find a spike, then find a nice place to cut it. If we never |
| 211 | // find a nice place, we save the top uniqThresh hits. |
| 212 | |
| 213 | if (spikeFound) { |
| 214 | decided = true; |
| 215 | label = "spike"; |
| 216 | hitsToSave = uniqThresh; |
| 217 | qualToSave = 0.0; |
| 218 | |
| 219 | for (uint32 i=uniqThresh-1; i > 9; i--) |
| 220 | if ((HR[i-10].coverage - HR[i].coverage) > difference) { |
| 221 | hitsToSave = i + 1; |
| 222 | break; |
| 223 | } |
| 224 | |
| 225 | qualToSave = HR[hitsToSave].coverage; |
| 226 | |
| 227 | if (logFile) |
| 228 | fprintf(logFile, uint32FMT"] spike: at "uint32FMT", "uint32FMT" hits saved: thresh=%f, "uint32FMT" hits, best=%f, worst=%f\n", |
| 229 | HR.iid(), spikeFound, hitsToSave, qualToSave, HR.numHits(), HR.bestScore(), HR.worstScore()); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | void |
| 237 | complicatedFilter(hitReader &HR) { |
| 238 | decided = false; |
| 239 | label = "NOLABELERROR"; |
| 240 | qualToSave = 1.0; |
| 241 | hitsToSave = 0; |
| 242 | |
| 243 | complicatedFilter_1_unique(HR); |
| 244 | |
| 245 | if (decided) |
| 246 | return; |
| 247 | |
| 248 | complicatedFilter_2_knee(HR); |
| 249 | |
| 250 | if (decided) |
| 251 | return; |
| 252 | |
| 253 | complicatedFilter_3_uniform(HR); |
| 254 | |
| 255 | if (decided) |
| 256 | return; |
| 257 | |
| 258 | complicatedFilter_4_largestdifference(HR); |
| 259 | |
| 260 | if (decided) |
| 261 | return; |
| 262 | |
| 263 | complicatedFilter_5_spikes(HR); |
| 264 | |
| 265 | if (decided) |
| 266 | return; |
| 267 | |
| 268 | decided = true; |
| 269 | label = "unknown"; |
| 270 | hitsToSave = reptThresh; |
| 271 | qualToSave = 0.0; |
| 272 | |
| 273 | if (hitsToSave > HR.numHits()) |
| 274 | hitsToSave = HR.numHits(); |
| 275 | |
| 276 | if (logFile) |
| 277 | fprintf(logFile, uint32FMT"] is an unclassified signal, "uint32FMT" hits saved out of "uint32FMT" hits, best=%f, worst=%f\n", |
| 278 | HR.iid(), hitsToSave, HR.numHits(), HR.bestScore(), HR.worstScore()); |
| 279 | } |