| 1 |
#include <stdio.h> |
| 2 |
#include <stdlib.h> |
| 3 |
#include <unistd.h> |
| 4 |
#include <string.h> |
| 5 |
|
| 6 |
#include "bio.h" |
| 7 |
#include "sim4.H" |
| 8 |
|
| 9 |
// Derived from pickBestPolish.c. We report only the single best |
| 10 |
// match, when it is obvious that there is EXACTLY one best match. |
| 11 |
// |
| 12 |
// Example: we have ten matches, but one is 3%id better than everyone |
| 13 |
// else -- that is an obviously unique match. The rest are noise. |
| 14 |
// |
| 15 |
// Example: ten matches, but they're all about the same quality -- within |
| 16 |
// a few percent id, and about the same length. We pick no match, and |
| 17 |
// silently discard all. |
| 18 |
// |
| 19 |
|
| 20 |
uint32 statOneMatch = 0; |
| 21 |
uint32 statConsistent = 0; |
| 22 |
uint32 statInconsistent = 0; |
| 23 |
uint32 statUnique = 0; |
| 24 |
uint32 statLost = 0; |
| 25 |
|
| 26 |
uint32 consistentTie = 0; |
| 27 |
uint32 consistentMatches = 0; |
| 28 |
uint32 consistentIdentity = 0; |
| 29 |
uint32 consistentTooShort = 0; |
| 30 |
uint32 consistentNot = 0; |
| 31 |
|
| 32 |
uint32 totLQ = 0; |
| 33 |
uint32 totMQ = 0; |
| 34 |
uint32 totRQ = 0; |
| 35 |
|
| 36 |
uint32 qualityDifference = 5; |
| 37 |
uint32 minQuality = 95; |
| 38 |
|
| 39 |
sim4polishWriter *W = 0L; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
void |
| 44 |
pickUniqueSlave(sim4polish **p, uint32 pNum) { |
| 45 |
uint32 identitym = 0, nmatchesm = 0; // Best score for the mList |
| 46 |
uint32 identityi = 0, nmatchesi = 0; // Best score the the iList |
| 47 |
uint32 matchi = 0, matchm = 0; |
| 48 |
|
| 49 |
// Difficult choice here.... |
| 50 |
// |
| 51 |
if (pNum == 1) { |
| 52 |
statOneMatch++; |
| 53 |
statUnique++; |
| 54 |
W->writeAlignment(p[0]); |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
// Find the best percentIdentity and best numberOfMatches. |
| 59 |
// |
| 60 |
// identityi is the best percent identity of all the matches for this EST, and |
| 61 |
// nmatchesi is the number of matches for the longest best identity match(es). |
| 62 |
// matchi is the match index |
| 63 |
// |
| 64 |
// nmatchesm is the best numMatches of all the matches for this EST, and |
| 65 |
// identitym is the highest percent identity for the best numMatches match(es). |
| 66 |
// matchm is the match index |
| 67 |
|
| 68 |
for (uint32 i=0; i<pNum; i++) { |
| 69 |
if ((p[i]->_percentIdentity > identityi) || |
| 70 |
(p[i]->_percentIdentity == identityi && p[i]->_numMatches > nmatchesi)) { |
| 71 |
identityi = p[i]->_percentIdentity; |
| 72 |
nmatchesi = p[i]->_numMatches; |
| 73 |
matchi = i; |
| 74 |
} |
| 75 |
|
| 76 |
if ((p[i]->_numMatches > nmatchesm) || |
| 77 |
(p[i]->_numMatches == nmatchesm && p[i]->_percentIdentity > identitym)) { |
| 78 |
nmatchesm = p[i]->_numMatches; |
| 79 |
identitym = p[i]->_percentIdentity; |
| 80 |
matchm = i; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
bool matchIsOK = false; |
| 85 |
|
| 86 |
// If we are in agreement on what the best quality match is, |
| 87 |
// see if the best match is obviously unique. |
| 88 |
// |
| 89 |
if ((identityi == identitym) || |
| 90 |
(nmatchesi == nmatchesm)) { |
| 91 |
statConsistent++; |
| 92 |
|
| 93 |
// It's clear what the quality values of the best match is, but we |
| 94 |
// don't know if those values are shared by more than one match. |
| 95 |
// Count the number of matches with exactly those scores. If |
| 96 |
// there is more than one, then we cannot pick out a single best. |
| 97 |
// |
| 98 |
uint32 numBest = 0; |
| 99 |
for (uint32 i=0; i<pNum; i++) |
| 100 |
if ((p[i]->_percentIdentity == identityi) && (p[i]->_numMatches == nmatchesi)) |
| 101 |
numBest++; |
| 102 |
|
| 103 |
if (numBest > 1) { |
| 104 |
|
| 105 |
// Dang, we mapped this guy more than once, exactly the same! |
| 106 |
// |
| 107 |
consistentTie++; |
| 108 |
|
| 109 |
} else { |
| 110 |
|
| 111 |
// We claim to have a single best match. See if any other |
| 112 |
// matches are close to the quality of that one. |
| 113 |
// |
| 114 |
// This says if (p[i]/ii >= 1.0 - Q), then we're close. |
| 115 |
|
| 116 |
uint32 closeQuality = 0; |
| 117 |
for (uint32 i=0; i<pNum; i++) |
| 118 |
if (((p[i]->_percentIdentity * 100) >= (identityi * (100 - qualityDifference))) || |
| 119 |
((p[i]->_numMatches * 100) >= (nmatchesi * (100 - qualityDifference)))) |
| 120 |
closeQuality++; |
| 121 |
|
| 122 |
// If only one match has close quality (the one we want to save!), |
| 123 |
// save it. Otherwise, label this query as multiple. |
| 124 |
|
| 125 |
uint32 length = p[matchi]->_exons[0]._estFrom - p[matchi]->_exons[0]._estTo; |
| 126 |
|
| 127 |
if (closeQuality == 1) { |
| 128 |
matchIsOK = true; |
| 129 |
consistentMatches++; |
| 130 |
} else if ((length > 100) && |
| 131 |
(length / p[matchi]->_estLen < 0.5)) { |
| 132 |
consistentTooShort++; |
| 133 |
} else { |
| 134 |
consistentNot++; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
} else { |
| 139 |
|
| 140 |
// Otherwise, we disagree on what the best match is. |
| 141 |
// |
| 142 |
// That is, the match with the highest identity is not the match |
| 143 |
// with the highest number of matches -- a longer match exists, but |
| 144 |
// at lower overall percent identity. |
| 145 |
|
| 146 |
statInconsistent++; |
| 147 |
|
| 148 |
// Estimate the identity of the extended part, assuming the piece |
| 149 |
// matched in common is matched at about the same identity. Or |
| 150 |
// just give up and say it's mapped to multiple places! |
| 151 |
|
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
if (matchIsOK) { |
| 156 |
statUnique++; |
| 157 |
assert(matchi == matchm); |
| 158 |
W->writeAlignment(p[matchi]); |
| 159 |
} else { |
| 160 |
statLost++; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
// Delete all matches that are spanned, report everything else. |
| 169 |
// Matches that are close ties in span, but are clearly lower quality are deleted. |
| 170 |
// |
| 171 |
void |
| 172 |
pickCoveringSlave(sim4polish **p, uint32 pNum, char doCovering) { |
| 173 |
uint32 *bgn = new uint32 [pNum]; |
| 174 |
uint32 *end = new uint32 [pNum]; |
| 175 |
|
| 176 |
for (uint32 i=0; i<pNum; i++) { |
| 177 |
if (doCovering == 'q') { |
| 178 |
if (p[i]->_matchOrientation == SIM4_MATCH_FORWARD) { |
| 179 |
bgn[i] = p[i]->_exons[0]._estFrom - 1; |
| 180 |
end[i] = p[i]->_exons[0]._estTo; |
| 181 |
} else { |
| 182 |
bgn[i] = p[i]->_estLen - p[i]->_exons[0]._estTo; |
| 183 |
end[i] = p[i]->_estLen - p[i]->_exons[0]._estFrom + 1; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
if (doCovering == 'g') { |
| 188 |
bgn[i] = p[i]->_exons[0]._genFrom - 1; |
| 189 |
end[i] = p[i]->_exons[0]._genTo; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
for (uint32 i=0; i<pNum; i++) { |
| 195 |
if (p[i] == NULL) |
| 196 |
continue; |
| 197 |
|
| 198 |
assert(p[i]->_numExons == 1); |
| 199 |
|
| 200 |
for (uint32 j=i+1; j<pNum; j++) { |
| 201 |
if (p[j] == NULL) |
| 202 |
continue; |
| 203 |
|
| 204 |
// i contained in j |
| 205 |
// ---- |
| 206 |
// --------- |
| 207 |
if ((bgn[j] <= bgn[i]) && (end[i] <= end[j])) { |
| 208 |
delete p[i]; p[i] = NULL; |
| 209 |
break; // This i is finished. |
| 210 |
} |
| 211 |
|
| 212 |
// j contained in i |
| 213 |
// --------- |
| 214 |
// ---- |
| 215 |
if ((bgn[i] <= bgn[j]) && (end[j] <= end[i])) { |
| 216 |
delete p[j]; p[j] = NULL; |
| 217 |
continue; // This j is finished. |
| 218 |
} |
| 219 |
|
| 220 |
// i almost contained in j |
| 221 |
// ---- ---- |
| 222 |
// --------- OR ---------- |
| 223 |
if (((bgn[j] <= bgn[i] + 5) && (end[i] <= end[j])) || |
| 224 |
((bgn[j] <= bgn[i]) && (end[i] <= end[j] + 5))) { |
| 225 |
delete p[i]; p[i] = NULL; |
| 226 |
break; // This i is finished. |
| 227 |
} |
| 228 |
|
| 229 |
// j almost contained in i |
| 230 |
// --------- OR ---------- |
| 231 |
// ---- ---- |
| 232 |
if (((bgn[i] <= bgn[j] + 5) && (end[j] <= end[i])) || |
| 233 |
((bgn[i] <= bgn[j]) && (end[j] <= end[i] + 5))) { |
| 234 |
delete p[j]; p[j] = NULL; |
| 235 |
continue; // This j is finished. |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
for (uint32 i=0; i<pNum; i++) { |
| 241 |
if (p[i] == NULL) |
| 242 |
continue; |
| 243 |
|
| 244 |
W->writeAlignment(p[i]); |
| 245 |
} |
| 246 |
|
| 247 |
delete [] bgn; |
| 248 |
delete [] end; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
// Just a wrapper around the real best picker, so that we can easily |
| 257 |
// destroy polishes when we're done. |
| 258 |
// |
| 259 |
void |
| 260 |
pickUnique(sim4polish **p, uint32 pNum, char doCovering) { |
| 261 |
|
| 262 |
if (doCovering != 0) |
| 263 |
pickCoveringSlave(p, pNum, doCovering); |
| 264 |
else |
| 265 |
pickUniqueSlave(p, pNum); |
| 266 |
|
| 267 |
for (uint32 i=0; i<pNum; i++) |
| 268 |
delete p[i]; |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
int |
| 275 |
main(int argc, char **argv) { |
| 276 |
char doCovering = 0; |
| 277 |
uint32 pNum = 0; |
| 278 |
uint32 pAlloc = 1048576; |
| 279 |
uint32 lastID = ~uint32ZERO; |
| 280 |
|
| 281 |
sim4polishStyle style = sim4polishStyleDefault; |
| 282 |
|
| 283 |
int arg = 1; |
| 284 |
while (arg < argc) { |
| 285 |
if (strcmp(argv[arg], "-cq") == 0) { |
| 286 |
doCovering = 'q'; |
| 287 |
|
| 288 |
} else if (strcmp(argv[arg], "-cg") == 0) { |
| 289 |
doCovering = 'g'; |
| 290 |
|
| 291 |
} else if (strcmp(argv[arg], "-q") == 0) { |
| 292 |
qualityDifference = strtouint32(argv[++arg], 0L); |
| 293 |
|
| 294 |
} else if (strcmp(argv[arg], "-gff3") == 0) { |
| 295 |
style = sim4polishGFF3; |
| 296 |
|
| 297 |
} else { |
| 298 |
fprintf(stderr, "unknown option: %s\n", argv[arg]); |
| 299 |
} |
| 300 |
arg++; |
| 301 |
} |
| 302 |
|
| 303 |
if (isatty(fileno(stdin))) { |
| 304 |
fprintf(stderr, "usage: %s [-q qualDiff] [-c] [-1] [-gff3] < file > file\n", argv[0]); |
| 305 |
fprintf(stderr, " -q qualDiff Only report alignments where the best is qualDiff better\n"); |
| 306 |
fprintf(stderr, " in percent identity and coverage\n"); |
| 307 |
fprintf(stderr, "\n"); |
| 308 |
fprintf(stderr, " -cq Only report alignments that are not contained in some\n"); |
| 309 |
fprintf(stderr, " other alignment in the QUERY SEQUENCE.\n"); |
| 310 |
fprintf(stderr, "\n"); |
| 311 |
fprintf(stderr, " -cg Only report alignments that are not contained in some\n"); |
| 312 |
fprintf(stderr, " other alignment in the GENOMIC SEQUENCE.\n"); |
| 313 |
fprintf(stderr, "\n"); |
| 314 |
|
| 315 |
if (isatty(fileno(stdin))) |
| 316 |
fprintf(stderr, "error: I cannot read polishes from the terminal!\n\n"); |
| 317 |
|
| 318 |
exit(1); |
| 319 |
} |
| 320 |
|
| 321 |
// Read polishes, picking the best when we see a change in the |
| 322 |
// estID. |
| 323 |
|
| 324 |
sim4polishReader *R = new sim4polishReader("-"); |
| 325 |
sim4polish **p = new sim4polish * [pAlloc]; |
| 326 |
sim4polish *q = 0L; |
| 327 |
|
| 328 |
W = new sim4polishWriter("-", style); |
| 329 |
|
| 330 |
if (R->getsim4polishStyle() != style) |
| 331 |
fprintf(stderr, "warning: input format and output format differ.\n"); |
| 332 |
|
| 333 |
while (R->nextAlignment(q)) { |
| 334 |
bool doPick = false; |
| 335 |
|
| 336 |
if ((doCovering == 'q') && (q->_estID != lastID)) |
| 337 |
doPick = true; |
| 338 |
|
| 339 |
if ((doCovering == 'g') && (q->_genID != lastID)) |
| 340 |
doPick = true; |
| 341 |
|
| 342 |
if ((doCovering == 0) && (q->_estID != lastID)) |
| 343 |
doPick = true; |
| 344 |
|
| 345 |
if ((doPick == true) && (pNum > 0)) { |
| 346 |
pickUnique(p, pNum, doCovering); |
| 347 |
pNum = 0; |
| 348 |
} |
| 349 |
|
| 350 |
if (pNum >= pAlloc) { |
| 351 |
sim4polish **P = new sim4polish * [pAlloc * 2]; |
| 352 |
memcpy(p, P, sizeof(sim4polish *) * pAlloc); |
| 353 |
delete [] p; |
| 354 |
p = P; |
| 355 |
pAlloc *= 2; |
| 356 |
} |
| 357 |
|
| 358 |
p[pNum++] = q; |
| 359 |
lastID = (doCovering == 'g') ? q->_genID : q->_estID; |
| 360 |
|
| 361 |
q = 0L; // Otherwise we delete the alignment we just saved! |
| 362 |
} |
| 363 |
|
| 364 |
if (pNum > 0) |
| 365 |
pickUnique(p, pNum, doCovering); |
| 366 |
|
| 367 |
#if 0 |
| 368 |
fprintf(stderr, "Uni:"uint32FMTW(8)" Con:"uint32FMTW(8)" (T:"uint32FMTW(8)" M:"uint32FMTW(8)" I:"uint32FMTW(8)" N:"uint32FMTW(8)") Inc:"uint32FMTW(8)" -- Save:"uint32FMTW(8)" Lost:"uint32FMTW(8)"\n", |
| 369 |
statOneMatch, |
| 370 |
statConsistent, consistentTie, consistentMatches, consistentIdentity, consistentNot, |
| 371 |
statInconsistent, |
| 372 |
statUnique, statLost); |
| 373 |
#endif |
| 374 |
|
| 375 |
delete [] p; |
| 376 |
|
| 377 |
delete R; |
| 378 |
delete W; |
| 379 |
|
| 380 |
return(0); |
| 381 |
} |
| 382 |
|