| 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 | |
| 9 | #define MAX_ESTS (16 * 1024 * 1024) |
| 10 | #define MAX_HITS (18474961) // for 20-03-5000-0.4 |
| 11 | |
| 12 | //#define SHOW_ONE |
| 13 | |
| 14 | //////////////////////////////////////// |
| 15 | |
| 16 | struct aHit { |
| 17 | uint32 _forward; |
| 18 | uint32 _qsIdx; |
| 19 | uint32 _dsIdx; |
| 20 | uint32 _dsLo; |
| 21 | uint32 _dsHi; |
| 22 | uint32 _covered; |
| 23 | uint32 _matched; |
| 24 | uint32 _numMers; |
| 25 | uint32 _yesno; |
| 26 | uint32 _identity; |
| 27 | uint32 _coverage; |
| 28 | |
| 29 | float scoreCov; |
| 30 | float scoreMult; |
| 31 | }; |
| 32 | |
| 33 | |
| 34 | void ahit_writeBinary(aHit *a, FILE *F) { |
| 35 | fwrite(a, sizeof(aHit), 1, F); |
| 36 | } |
| 37 | |
| 38 | void ahit_readBinary(aHit *a, FILE *F) { |
| 39 | fread(a, sizeof(aHit), 1, F); |
| 40 | } |
| 41 | |
| 42 | void ahit_printASCII(aHit *a, FILE *F) { |
| 43 | #ifdef TRUE64BIT |
| 44 | fprintf(F, "-%c -e %u -D %u %u %u -M %u %u %u %s %u %u\n", |
| 45 | a->_forward ? 'f' : 'r', |
| 46 | a->_qsIdx, |
| 47 | a->_dsIdx, |
| 48 | a->_dsLo, |
| 49 | a->_dsHi, |
| 50 | a->_covered, |
| 51 | a->_matched, |
| 52 | a->_numMers, |
| 53 | a->_yesno ? "-Y" : "-N", |
| 54 | a->_identity, |
| 55 | a->_coverage); |
| 56 | #else |
| 57 | fprintf(F, "-%c -e %lu -D %lu %lu %lu -M %lu %lu %lu %s %lu %lu\n", |
| 58 | a->_forward ? 'f' : 'r', |
| 59 | a->_qsIdx, |
| 60 | a->_dsIdx, |
| 61 | a->_dsLo, |
| 62 | a->_dsHi, |
| 63 | a->_covered, |
| 64 | a->_matched, |
| 65 | a->_numMers, |
| 66 | a->_yesno ? "-Y" : "-N", |
| 67 | a->_identity, |
| 68 | a->_coverage); |
| 69 | #endif |
| 70 | } |
| 71 | |
| 72 | |
| 73 | void ahit_parseString(aHit *a, char *b) { |
| 74 | char *c = b+1; |
| 75 | |
| 76 | a->_forward = (*c == 'f'); |
| 77 | c += 1; |
| 78 | |
| 79 | if (c[2] != 'e') fprintf(stderr, "'%s' didn't get -e\n", b); |
| 80 | |
| 81 | c += 4; |
| 82 | a->_qsIdx = (uint32)strtoul(c, &c, 10); |
| 83 | |
| 84 | if (c[2] != 'D') fprintf(stderr, "'%s' didn't get -D\n", b); |
| 85 | |
| 86 | c += 4; |
| 87 | a->_dsIdx = (uint32)strtoul(c, &c, 10); |
| 88 | a->_dsLo = (uint32)strtoul(c, &c, 10); |
| 89 | a->_dsHi = (uint32)strtoul(c, &c, 10); |
| 90 | |
| 91 | if (c[2] == 'M') { |
| 92 | c += 4; |
| 93 | a->_covered = (uint32)strtoul(c, &c, 10); |
| 94 | a->_matched = (uint32)strtoul(c, &c, 10); |
| 95 | a->_numMers = (uint32)strtoul(c, &c, 10); |
| 96 | } else { |
| 97 | //fprintf(stderr, "'%s' didn't get -M\n", b); |
| 98 | a->_covered = 0; |
| 99 | a->_matched = 0; |
| 100 | a->_numMers = 0; |
| 101 | } |
| 102 | |
| 103 | a->_yesno = 0; |
| 104 | a->_identity = 0; |
| 105 | a->_coverage = 0; |
| 106 | |
| 107 | if (c[2] == 'Y') { |
| 108 | c += 4; |
| 109 | a->_yesno = 1; |
| 110 | a->_identity = (uint32)strtoul(c, &c, 10); |
| 111 | a->_coverage = (uint32)strtoul(c, &c, 10); |
| 112 | } |
| 113 | |
| 114 | #if 0 |
| 115 | if (c[2] == 'N') { |
| 116 | c += 4; |
| 117 | a->_yesno = 0; |
| 118 | a->_identity = (uint32)strtoul(c, &c, 10); |
| 119 | a->_coverage = (uint32)strtoul(c, &c, 10); |
| 120 | } |
| 121 | #endif |
| 122 | } |
| 123 | |
| 124 | //////////////////////////////////////// |
| 125 | |
| 126 | int |
| 127 | hitCompare(const void *a, const void *b) { |
| 128 | const aHit *A = (const aHit *)a; |
| 129 | const aHit *B = (const aHit *)b; |
| 130 | |
| 131 | if (A->scoreCov > B->scoreCov) |
| 132 | return(-1); |
| 133 | else |
| 134 | return(A->scoreCov < B->scoreCov); |
| 135 | } |
| 136 | |
| 137 | int |
| 138 | hitCompareID(const void *a, const void *b) { |
| 139 | const aHit *A = (const aHit *)a; |
| 140 | const aHit *B = (const aHit *)b; |
| 141 | |
| 142 | if (A->_qsIdx < B->_qsIdx) |
| 143 | return(-1); |
| 144 | if (A->_qsIdx > B->_qsIdx) |
| 145 | return(1); |
| 146 | return(0); |
| 147 | } |
| 148 | |
| 149 | |
| 150 | |
| 151 | int |
| 152 | main(int argc, char **argv) { |
| 153 | aHit *hits = new aHit [MAX_HITS]; |
| 154 | uint32 hitsLen = 0; |
| 155 | |
| 156 | |
| 157 | // read all the hits from stdin -- assumes ascii format |
| 158 | // |
| 159 | char hitLine[1025]; |
| 160 | |
| 161 | while (!feof(stdin)) { |
| 162 | fgets(hitLine, 1024, stdin); |
| 163 | if (!feof(stdin)) { |
| 164 | ahit_parseString(hits + hitsLen, hitLine); |
| 165 | |
| 166 | // These are the scores used by the filter |
| 167 | // |
| 168 | hits[hitsLen].scoreCov = (float)hits[hitsLen]._covered / (float)hits[hitsLen]._numMers; |
| 169 | hits[hitsLen].scoreMult = (float)hits[hitsLen]._matched / (float)hits[hitsLen]._covered; |
| 170 | |
| 171 | // aHit->_covered is in bases, but aHit->_numMers is the |
| 172 | // number of mers. Possible for coverage to be > 1.0. |
| 173 | // |
| 174 | if (hits[hitsLen].scoreCov > 1.0) |
| 175 | hits[hitsLen].scoreCov = 1.0; |
| 176 | |
| 177 | hitsLen++; |
| 178 | |
| 179 | if ((hitsLen & 0xff) == 0) { |
| 180 | fprintf(stderr, "reading hits "uint32FMT"\r", hitsLen); |
| 181 | fflush(stderr); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | fprintf(stderr, "reading hits "uint32FMT"\n", hitsLen); |
| 187 | |
| 188 | |
| 189 | // Sort the hits by estid |
| 190 | // |
| 191 | fprintf(stderr, "sorting hits by cDNA\n"); |
| 192 | qsort(hits, hitsLen, sizeof(aHit), hitCompareID); |
| 193 | |
| 194 | |
| 195 | // Sort the hits by score (scoreCov), in decreasing order. |
| 196 | // |
| 197 | fprintf(stderr, "sorting hits by score\n"); |
| 198 | for (uint32 currentHit = 0; currentHit < hitsLen; ) { |
| 199 | uint32 estOfInterest = hits[currentHit]._qsIdx; |
| 200 | uint32 numHits = 0; |
| 201 | for (uint32 t=currentHit; (t < hitsLen) && (hits[t]._qsIdx == estOfInterest); t++) |
| 202 | numHits++; |
| 203 | |
| 204 | qsort(hits + currentHit, numHits, sizeof(aHit), hitCompare); |
| 205 | |
| 206 | currentHit += numHits; |
| 207 | } |
| 208 | |
| 209 | fprintf(stderr, "filtering hits\n"); |
| 210 | |
| 211 | double L = 0.0; |
| 212 | double H = 0.0; |
| 213 | double V = 0.1; |
| 214 | double M = 1.0; |
| 215 | double MC = 0.0; |
| 216 | uint32 ML = 0; |
| 217 | |
| 218 | double minIdentity = 98.0; |
| 219 | double minCoverage = 96.0; |
| 220 | |
| 221 | for (uint32 Hcnt = 10; Hcnt <= 100; Hcnt += 10) { |
| 222 | for (uint32 Lcnt = 10; Lcnt < Hcnt && Lcnt < 60; Lcnt += 10) { |
| 223 | for (uint32 Vcnt = 10; Vcnt < 100; Vcnt += 10) { |
| 224 | #ifdef SHOW_ONE |
| 225 | Lcnt = 30; |
| 226 | Hcnt = 40; |
| 227 | Vcnt = 100; |
| 228 | #endif |
| 229 | L = Lcnt / 100.0; |
| 230 | H = Hcnt / 100.0; |
| 231 | V = Vcnt / 100.0; |
| 232 | |
| 233 | uint32 truepositive = 0; |
| 234 | uint32 falsepositive = 0; |
| 235 | uint32 truenegative = 0; |
| 236 | uint32 falsenegative = 0; |
| 237 | |
| 238 | for (uint32 currentHit = 0; currentHit < hitsLen; ) { |
| 239 | |
| 240 | // Find the number of hits for this ESTid |
| 241 | // |
| 242 | uint32 estOfInterest = hits[currentHit]._qsIdx; |
| 243 | uint32 numHits = 0; |
| 244 | for (uint32 t=currentHit; (t < hitsLen) && (hits[t]._qsIdx == estOfInterest); t++) |
| 245 | numHits++; |
| 246 | |
| 247 | double h = hits[currentHit].scoreCov - hits[currentHit + numHits - 1].scoreCov; |
| 248 | double p = 0.0; |
| 249 | |
| 250 | if (h <= L) p = 1.0; |
| 251 | if (h >= H) p = V; |
| 252 | if (p == 0.0) p = 1.0 - (1.0 - V) * (h - L) / (H - L); |
| 253 | |
| 254 | // check p; it should be between V and 1.0 |
| 255 | if (p > 1.0) { |
| 256 | fprintf(stderr, "error in p; p=%f h=%f (%f %f %f)\n", p, h, L, H, V); |
| 257 | p = 1.0; |
| 258 | } |
| 259 | |
| 260 | if (p < V) { |
| 261 | fprintf(stderr, "error in p; p=%f h=%f (%f %f %f)\n", p, h, L, H, V); |
| 262 | p = V; |
| 263 | } |
| 264 | |
| 265 | // Output the top p% hits, by score. |
| 266 | // |
| 267 | double cutL = hits[currentHit].scoreCov - p * h; |
| 268 | |
| 269 | if (cutL > M) |
| 270 | cutL = M; |
| 271 | |
| 272 | #ifdef SHOW_ONE |
| 273 | fprintf(stdout, "LHV = %f %f %f p=%f h=%f cutL=%f\n", L, H, V, p, h, cutL); |
| 274 | #endif |
| 275 | |
| 276 | |
| 277 | for (uint32 i=currentHit; i < currentHit + numHits; i++) { |
| 278 | if ((cutL <= hits[i].scoreCov) && |
| 279 | ((MC <= hits[i].scoreCov) || (ML <= hits[i]._covered))) { |
| 280 | #ifdef SHOW_ONE |
| 281 | fprintf(stdout, "POS: (%f)", hits[i].scoreCov); |
| 282 | ahit_printASCII(hits+i, stdout); |
| 283 | #endif |
| 284 | if ((hits[i]._yesno == 1) && (hits[i]._identity >= minIdentity) && (hits[i]._coverage >= minCoverage)) |
| 285 | truepositive++; |
| 286 | else |
| 287 | falsepositive++; |
| 288 | } else { |
| 289 | #ifdef SHOW_ONE |
| 290 | fprintf(stdout, "NEG: (%f)", hits[i].scoreCov); |
| 291 | ahit_printASCII(hits+i, stdout); |
| 292 | #endif |
| 293 | if ((hits[i]._yesno == 1) && (hits[i]._identity >= minIdentity) && (hits[i]._coverage >= minCoverage)) |
| 294 | falsenegative++; |
| 295 | else |
| 296 | truenegative++; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | #ifdef SHOW_ONE |
| 301 | fprintf(stdout, "----\n"); |
| 302 | #endif |
| 303 | currentHit += numHits; |
| 304 | } |
| 305 | |
| 306 | // Print L, H, V, sensitivity, specificity |
| 307 | // |
| 308 | fprintf(stdout, "%f %f %f %6.4f %6.4f "uint32FMT" "uint32FMT" "uint32FMT" "uint32FMT"\n", |
| 309 | L, H, V, |
| 310 | (double)truepositive / (truepositive + falsenegative), |
| 311 | (double)truenegative / (truenegative + falsepositive), |
| 312 | truepositive, falsepositive, |
| 313 | truenegative, falsenegative); |
| 314 | fflush(stdout); |
| 315 | |
| 316 | #ifdef SHOW_ONE |
| 317 | exit(0); |
| 318 | #endif |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | return(0); |
| 324 | } |
| 325 |