Debian Codegraph @

sim4dbutils/pickBestPolish.C

raw
1 #include <stdlib.h>
2 #include <unistd.h>
3 #include <string.h>
4
5 #include "bio.h"
6 #include "sim4.H"
7
8 // Picks the best polish (or set of polishes that are all of the same
9 // best quality) for each cDNA.
10 //
11 // Validate mode will print out ALL input matches, in the following
12 // format
13 //
14 // estid gaid nummatches percentid (genFr genTo %) () ()
15 //
16 // With a * somewhere to denote the best ones. Separate ESTs with
17 // a dashed line.
18
19
20 #define EPS_X 1
21 #define EPS_N_ESTS 10
22 #define EPS_N_MRNA 15
23 #define EPS_I 3
24
25
26 uint32 EPS_N = EPS_N_ESTS;
27 uint32 doValidate = 0;
28
29 sim4polishWriter *W = 0L;
30
31 static
32 void
33 printPolishValidate(FILE *O, sim4polish *p, uint32 isBest) {
34
35 fprintf(O, uint32FMTW(8)" "uint32FMTW(8)" "uint32FMTW(4)" "uint32FMTW(4),
36 p->_estID, p->_genID, p->_percentIdentity, p->_numMatches);
37
38 for (uint32 i=0; i<p->_numExons; i++)
39 fprintf(O, " ("uint32FMTW(6)"/"uint32FMTW(6)" "uint32FMTW(6)"/"uint32FMTW(6)" "uint32FMTW(3)")",
40 p->_exons[i]._estFrom, p->_exons[i]._genFrom,
41 p->_exons[i]._estTo, p->_exons[i]._genTo,
42 p->_exons[i]._percentIdentity);
43
44 if (isBest)
45 fprintf(O, " *");
46
47 fprintf(O, "\n");
48 }
49
50
51 static
52 void
53 pickBestSlave(sim4polish **p, uint32 pNum) {
54 uint32 identitym = 0, nmatchesm = 0; // Best score for the mList
55 uint32 identityi = 0, nmatchesi = 0; // Best score the the iList
56 uint32 numExons = 0, numExonsi = 0, numExonsm = 0;
57 uint32 tmp_nmatches = 0;
58 double alpha;
59
60 // Difficult choice here....
61 //
62 if (pNum == 1) {
63 if (doValidate == 0)
64 W->writeAlignment(p[0]);
65 return;
66 }
67
68 if ((p[0]->_estID % 1287) == 0) {
69 fprintf(stderr, "Picking Best for estID="uint32FMT" with %5d choices.\r", p[0]->_estID, pNum);
70 fflush(stderr);
71 }
72
73 // Find the best percentIdentity and best numberOfMatches.
74 //
75 // identityi is the best percent identity of all the matches for this EST, and
76 // nmatchesi is the number of matches for the longest best identity match(es).
77 //
78 // nmatchesm is the best numMatches of all the matches for this EST, and
79 // identitym is the highest percent identity for the best numMatches match(es).
80
81 for (uint32 i=0; i<pNum; i++) {
82
83 if ((p[i]->_percentIdentity > identityi) ||
84 (p[i]->_percentIdentity == identityi && p[i]->_numMatches > nmatchesi)) {
85 identityi = p[i]->_percentIdentity;
86 nmatchesi = p[i]->_numMatches;
87 }
88
89 if ((p[i]->_numMatches > nmatchesm) ||
90 (p[i]->_numMatches == nmatchesm && p[i]->_percentIdentity > identitym)) {
91 nmatchesm = p[i]->_numMatches;
92 identitym = p[i]->_percentIdentity;
93 }
94
95 }
96
97
98 // Otherwise, if the best scores on both lists are the same, pick
99 // the matches with the largest number of exons
100 //
101 if ((identityi == identitym) &&
102 (nmatchesi == nmatchesm)) {
103
104 // Find the largest number of exons, allowing some margin in numMatches
105 //
106 numExonsi = 0;
107 for (uint32 i=0; i<pNum; i++)
108 if ((p[i]->_percentIdentity == identityi) &&
109 (p[i]->_numMatches >= nmatchesi) &&
110 (numExonsi < p[i]->_numExons))
111 numExonsi = p[i]->_numExons;
112
113 numExons = numExonsi;
114 tmp_nmatches = nmatchesi;
115 for (uint32 i=0; i<pNum; i++)
116 if ((p[i]->_percentIdentity == identityi) &&
117 (p[i]->_numMatches >= nmatchesi - EPS_N) &&
118 (numExons < p[i]->_numExons - EPS_X)) {
119 tmp_nmatches = p[i]->_numMatches;
120 numExons = p[i]->_numExons;
121 }
122
123 // Scan the entire list, printing the best stuff. We cannot just
124 // scan both the mList and iList, as those probably contain
125 // duplicates.
126
127 if (doValidate) {
128 if (tmp_nmatches == nmatchesi)
129 fprintf(stdout, "--------------------1 (Clear Winner)\n");
130 else
131 fprintf(stdout, "--------------------2 (Exon Clear Winner)\n");
132 for (uint32 i=0; i<pNum; i++)
133 printPolishValidate(stdout, p[i], ((p[i]->_percentIdentity == identityi) &&
134 (p[i]->_numMatches == tmp_nmatches) &&
135 (p[i]->_numExons == numExons)));
136 } else {
137 for (uint32 i=0; i<pNum; i++)
138 if ((p[i]->_percentIdentity == identityi) &&
139 (p[i]->_numMatches == tmp_nmatches) &&
140 (p[i]->_numExons == numExons))
141 W->writeAlignment(p[i]);
142 }
143
144 return;
145 }
146
147 // Start over. Find the best two percentIdentities. Break ties
148 // with numMatches.
149 //
150 // i will be the best,
151 // m will be the second best
152 //
153 identityi = identitym = 0;
154 nmatchesi = nmatchesm = 0;
155
156 for (uint32 i=0; i<pNum; i++) {
157
158 // Pick the two matches with the highest (different) percent
159 // identities; for each, pick the highest number of matches.
160 //
161 // First block: Have we found a new best percent identity?
162 // If so, save it, and shift former best to second best.
163 //
164 // Second and third blocks: make sure that we save the
165 // best numMatches for each.
166 //
167 if (p[i]->_percentIdentity > identityi) {
168 identitym = identityi;
169 nmatchesm = nmatchesi;
170
171 identityi = p[i]->_percentIdentity;
172 nmatchesi = p[i]->_numMatches;
173 } else if ((p[i]->_percentIdentity == identityi) &&
174 (p[i]->_numMatches > nmatchesi)) {
175 nmatchesi = p[i]->_numMatches;
176 } else if ((p[i]->_percentIdentity < identityi) &&
177 ((p[i]->_percentIdentity > identitym) ||
178 ((p[i]->_percentIdentity == identitym) &&
179 (p[i]->_numMatches > nmatchesm)))) {
180 nmatchesm = p[i]->_numMatches;
181 identitym = p[i]->_percentIdentity;
182 }
183 }
184
185 // Now, 'i' is the highest percent identity, 'm' is the second
186 // highest. By definition, numMatches for 'i' is less than
187 // numMatches for 'm'.
188
189 // If the number of matches is different, output everything with the
190 // top score.
191 //
192 // We are guaranteed that the identities are the same. (I think)
193
194 if (nmatchesi >= nmatchesm) {
195
196 // Find the match(es) with the largest number of exons
197
198 numExonsi = 0;
199 for (uint32 i=0; i<pNum; i++)
200 if ((p[i]->_percentIdentity == identityi) &&
201 (p[i]->_numMatches >= nmatchesi) &&
202 (numExonsi < p[i]->_numExons))
203 numExonsi = p[i]->_numExons;
204
205
206 numExons = numExonsi;
207 tmp_nmatches = nmatchesi;
208 for (uint32 i=0; i<pNum; i++)
209 if ((p[i]->_percentIdentity == identityi) &&
210 (p[i]->_numMatches >= nmatchesi - EPS_N) &&
211 (numExons < p[i]->_numExons - EPS_X)) {
212 numExons = p[i]->_numExons;
213 tmp_nmatches = p[i]->_numMatches;
214 }
215
216 if (doValidate) {
217 if (tmp_nmatches == nmatchesi)
218 fprintf(stdout, "--------------------3 (?)\n");
219 else
220 fprintf(stdout, "--------------------4 (Exon ?)\n");
221 for (uint32 i=0; i<pNum; i++)
222 printPolishValidate(stdout, p[i], ((p[i]->_percentIdentity == identityi) &&
223 (p[i]->_numMatches == tmp_nmatches) &&
224 (p[i]->_numExons == numExons)));
225 } else {
226 for (uint32 i=0; i<pNum; i++)
227 if ((p[i]->_percentIdentity == identityi) &&
228 (p[i]->_numMatches == tmp_nmatches) &&
229 (p[i]->_numExons == numExons))
230 W->writeAlignment(p[i]);
231 }
232
233 return;
234 }
235
236 // Otherwise, compute alpha
237
238 alpha = ((nmatchesm - nmatchesi) /
239 ((nmatchesm / (double)identitym) -
240 (nmatchesi / (double)identityi)))/100;
241
242 // If alpha below a magic threshold, pick the shorter match.
243 //
244 if (alpha < 0.8) {
245
246 // Find the match(es) with the largest number of exons
247
248 numExons = tmp_nmatches = 0;
249 for (uint32 i=0; i<pNum; i++)
250 if ((p[i]->_percentIdentity == identityi) &&
251 (p[i]->_numMatches >= nmatchesi) &&
252 (numExons < p[i]->_numExons))
253 numExons = p[i]->_numExons;
254
255 if (doValidate) {
256 fprintf(stdout, "--------------------5 (alpha < 0.8)\n");
257 for (uint32 i=0; i<pNum; i++)
258 printPolishValidate(stdout, p[i], ((p[i]->_percentIdentity == identityi) &&
259 (p[i]->_numMatches == nmatchesi) &&
260 (p[i]->_numExons == numExons)));
261 } else {
262 for (uint32 i=0; i<pNum; i++)
263 if ((p[i]->_percentIdentity == identityi) &&
264 (p[i]->_numMatches == nmatchesi) &&
265 (p[i]->_numExons == numExons))
266 W->writeAlignment(p[i]);
267 }
268
269 return;
270 }
271
272 // Otherwise, pick the longer one.
273
274 // XXX: We can still check:
275 // if an internal gap is in N's
276 // the number of exons
277 // etc, etc.
278
279
280
281 // See if the smaller one has an internal gap that corresponds to
282 // N's in the genome. If so, assume that the exon mapped to the
283 // N's and pick the smaller.
284 //
285 // Need code to process genome, finding N's larger than some threshold.
286 // Output as 'genID beg end'
287
288
289 // Find the largest number of exons for each of the contenders
290
291 numExonsi = numExonsm = 0;
292 for (uint32 i=0; i<pNum; i++) {
293 if ((p[i]->_percentIdentity == identitym) &&
294 (p[i]->_numMatches == nmatchesm) &&
295 (numExonsm < p[i]->_numExons))
296 numExonsm = p[i]->_numExons;
297 else if ((p[i]->_percentIdentity == identityi) &&
298 (p[i]->_numMatches == nmatchesi) &&
299 (numExonsi < p[i]->_numExons))
300 numExonsi = p[i]->_numExons;
301 }
302
303 if ((numExonsi > numExonsm + EPS_X) || (identityi > identitym + EPS_I)) {
304
305 if (doValidate) {
306 if (numExonsi > numExonsm + EPS_X)
307 fprintf(stdout, "--------------------6 (Exon Plus alpha > 0.8)\n");
308 else
309 fprintf(stdout, "--------------------7 (Pctid Plus alpha > 0.8)\n");
310
311 for (uint32 i=0; i<pNum; i++)
312 printPolishValidate(stdout, p[i], ((p[i]->_percentIdentity == identityi) &&
313 (p[i]->_numMatches == nmatchesi) &&
314 (p[i]->_numExons == numExonsi)));
315 } else {
316 for (uint32 i=0; i<pNum; i++)
317 if ((p[i]->_percentIdentity == identityi) &&
318 (p[i]->_numMatches == nmatchesi) &&
319 (p[i]->_numExons == numExonsi))
320 W->writeAlignment(p[i]);
321 }
322 } else {
323 numExons = numExonsm;
324 tmp_nmatches = nmatchesm;
325 for (uint32 i=0; i<pNum; i++)
326 if ((p[i]->_percentIdentity == identitym) &&
327 (p[i]->_numMatches >= nmatchesm - EPS_N) &&
328 (numExons < p[i]->_numExons - EPS_X)) {
329 tmp_nmatches = p[i]->_numMatches;
330 numExons = p[i]->_numExons;
331 }
332
333 if (doValidate) {
334 if (numExons == numExonsm)
335 fprintf(stdout, "--------------------8 (alpha > 0.8)\n");
336 else
337 fprintf(stdout, "--------------------9 (Exon alpha > 0.8)\n");
338 for (uint32 i=0; i<pNum; i++)
339 printPolishValidate(stdout, p[i], ((p[i]->_percentIdentity == identitym) &&
340 (p[i]->_numMatches == tmp_nmatches) &&
341 (p[i]->_numExons == numExons)));
342 } else {
343 for (uint32 i=0; i<pNum; i++)
344 if ((p[i]->_percentIdentity == identitym) &&
345 (p[i]->_numMatches == tmp_nmatches) &&
346 (p[i]->_numExons == numExons))
347 W->writeAlignment(p[i]);
348 }
349 }
350 }
351
352
353 // Just a wrapper around the real best picker, so that we can easily
354 // destroy polishes when we're done.
355 //
356 static
357 void
358 pickBest(sim4polish **p, uint32 pNum) {
359
360 pickBestSlave(p, pNum);
361
362 for (uint32 i=0; i<pNum; i++)
363 delete p[i];
364 }
365
366
367 int
368 main(int argc, char **argv) {
369 uint32 pNum = 0;
370 uint32 pAlloc = 8388608;
371 uint32 estID = ~uint32ZERO;
372
373 sim4polishStyle style = sim4polishStyleDefault;
374
375 int arg = 1;
376 while (arg < argc) {
377 if (strncmp(argv[arg], "-n", 2) == 0) {
378 pAlloc = atoi(argv[++arg]);
379 } else if (strncmp(argv[arg], "-mrna", 2) == 0) {
380 EPS_N = EPS_N_MRNA;
381 } else if (strncmp(argv[arg], "-ests", 2) == 0) {
382 EPS_N = EPS_N_ESTS;
383 } else if (strncmp(argv[arg], "-validate", 2) == 0) {
384 doValidate = 1;
385 } else if (strcmp(argv[arg], "-gff3") == 0) {
386 style = sim4polishGFF3;
387 } else {
388 fprintf(stderr, "unknown option: %s\n", argv[arg]);
389 }
390 arg++;
391 }
392
393 if (isatty(fileno(stdin))) {
394 fprintf(stderr, "usage: %s [-mrna|-ests] [-validate] [-gff3] < file > file\n", argv[0]);
395
396 if (isatty(fileno(stdin)))
397 fprintf(stderr, "error: I cannot read polishes from the terminal!\n\n");
398
399 exit(1);
400 }
401
402 // Read polishes, picking the best when we see a change in the
403 // estID.
404
405 sim4polishReader *R = new sim4polishReader("-");
406 sim4polish **p = new sim4polish * [pAlloc];
407 sim4polish *q = 0L;
408
409 W = new sim4polishWriter("-", style);
410
411 if (R->getsim4polishStyle() != style)
412 fprintf(stderr, "warning: input format and output format differ.\n");
413
414 while (R->nextAlignment(q)) {
415 if ((q->_estID != estID) && (pNum > 0)) {
416 pickBest(p, pNum);
417 pNum = 0;
418 }
419
420 if (pNum >= pAlloc) {
421 sim4polish **P = new sim4polish * [pAlloc * 2];
422 memcpy(p, P, sizeof(sim4polish *) * pAlloc);
423 delete [] p;
424 p = P;
425 pAlloc *= 2;
426 }
427
428 p[pNum++] = q;
429 estID = q->_estID;
430
431 q = 0L; // Otherwise we delete the alignment we just saved!
432 }
433
434 if (pNum > 0)
435 pickBest(p, pNum);
436
437 delete [] p;
438
439 delete R;
440 delete W;
441
442 return(0);
443 }
444