| 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <unistd.h> |
| 4 | #include <string.h> |
| 5 | |
| 6 | #include "sim4.H" |
| 7 | |
| 8 | |
| 9 | void |
| 10 | pickBest(sim4polishWriter *W, sim4polish **p, int pNum, int uniq) { |
| 11 | int i; |
| 12 | |
| 13 | if (pNum == 1) { |
| 14 | if (uniq) |
| 15 | W->writeAlignment(p[0]); |
| 16 | } else { |
| 17 | if (!uniq) |
| 18 | for (i=0; i<pNum; i++) |
| 19 | W->writeAlignment(p[0]); |
| 20 | } |
| 21 | |
| 22 | for (i=0; i<pNum; i++) |
| 23 | delete p[i]; |
| 24 | } |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | int |
| 30 | main(int argc, char **argv) { |
| 31 | uint32 pNum = 0; |
| 32 | uint32 pAlloc = 8388608; |
| 33 | uint32 estID = ~uint32ZERO; |
| 34 | |
| 35 | uint32 uniq = 1; |
| 36 | |
| 37 | sim4polishStyle style = sim4polishStyleDefault; |
| 38 | |
| 39 | int arg = 1; |
| 40 | while (arg < argc) { |
| 41 | if (strncmp(argv[arg], "-uniq", 2) == 0) { |
| 42 | uniq = 1; |
| 43 | } else if (strncmp(argv[arg], "-dupl", 2) == 0) { |
| 44 | uniq = 0; |
| 45 | } else if (strcmp(argv[arg], "-gff3") == 0) { |
| 46 | style = sim4polishGFF3; |
| 47 | } else { |
| 48 | fprintf(stderr, "unknown option: %s\n", argv[arg]); |
| 49 | } |
| 50 | arg++; |
| 51 | } |
| 52 | |
| 53 | if (isatty(fileno(stdin))) { |
| 54 | fprintf(stderr, "usage: %s [-uniq | -dupl] [-gff3] < file > file\n", argv[0]); |
| 55 | |
| 56 | if (isatty(fileno(stdin))) |
| 57 | fprintf(stderr, "error: I cannot read polishes from the terminal!\n\n"); |
| 58 | |
| 59 | exit(1); |
| 60 | } |
| 61 | |
| 62 | // Read polishes, picking the best when we see a change in |
| 63 | // the estID. |
| 64 | |
| 65 | sim4polishWriter *W = new sim4polishWriter("-", style); |
| 66 | sim4polishReader *R = new sim4polishReader("-"); |
| 67 | sim4polish **p = new sim4polish * [pAlloc]; |
| 68 | sim4polish *q = 0L; |
| 69 | |
| 70 | if (R->getsim4polishStyle() != style) |
| 71 | fprintf(stderr, "warning: input format and output format differ.\n"); |
| 72 | |
| 73 | while (R->nextAlignment(q)) { |
| 74 | if ((q->_estID != estID) && (pNum > 0)) { |
| 75 | pickBest(W, p, pNum, uniq); |
| 76 | pNum = 0; |
| 77 | } |
| 78 | |
| 79 | if (pNum >= pAlloc) { |
| 80 | sim4polish **P = new sim4polish * [pAlloc * 2]; |
| 81 | memcpy(p, P, sizeof(sim4polish *) * pAlloc); |
| 82 | delete [] p; |
| 83 | p = P; |
| 84 | pAlloc *= 2; |
| 85 | } |
| 86 | |
| 87 | p[pNum++] = q; |
| 88 | estID = q->_estID; |
| 89 | |
| 90 | q = 0L; // Else we'll delete the polish we just saved! |
| 91 | } |
| 92 | |
| 93 | if (pNum > 0) |
| 94 | pickBest(W, p, pNum, uniq); |
| 95 | |
| 96 | delete [] p; |
| 97 | delete R; |
| 98 | delete W; |
| 99 | |
| 100 | return(0); |
| 101 | } |
| 102 |