Debian Codegraph @

sim4dbutils/removeDuplicate.C

raw
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <string.h>
5
6 #include "sim4.H"
7
8 // Input matches should be sorted by cDNA, and ran through pickBest.
9 // This code will remove all matches that have the same genomic span,
10 // and warn when two matches have nearly the same genomic span.
11
12 sim4polishWriter *W = 0L;
13
14 void
15 pickBest(sim4polish **p, int pNum) {
16 int i, j;
17
18 for (i=0; i<pNum; i++) {
19 for (j=i+1; j<pNum; j++) {
20 if ((p[i]) &&
21 (p[j]) &&
22 (p[i]->_numExons == p[j]->_numExons) &&
23 (p[i]->_genID == p[j]->_genID)) {
24 int a, b;
25 int sd = 666;
26 int ed = 666;
27
28 a = p[i]->_exons[0]._genFrom;
29 b = p[j]->_exons[0]._genFrom;
30 if (a < b)
31 sd = b - a;
32 else
33 sd = a - b;
34
35 a = p[i]->_exons[p[i]->_numExons-1]._genTo;
36 b = p[j]->_exons[p[j]->_numExons-1]._genTo;
37 if (a < b)
38 ed = b - a;
39 else
40 ed = a - b;
41
42 if ((sd == 0) && (ed == 0)) {
43 //fprintf(stderr, "%d and %d are exact; %d removed.\n", i, j, j);
44 delete p[j];
45 p[j] = 0L;
46 } else if ((sd < 10) && (ed < 10)) {
47 char *alignI = p[i]->s4p_polishToString(sim4polishS4DB);
48 char *alignJ = p[j]->s4p_polishToString(sim4polishS4DB);
49
50 fprintf(stderr, "----------------------------------------\n");
51 fprintf(stderr, "Warning: %d and %d are similar.\n", i, j);
52 fprintf(stderr, "%s\n", alignI);
53 fprintf(stderr, "%s\n", alignJ);
54 fprintf(stderr, "----------------------------------------\n");
55
56 delete [] alignI;
57 delete [] alignJ;
58 }
59 }
60 }
61 }
62
63 for (i=0; i<pNum; i++) {
64 if (p[i]) {
65 W->writeAlignment(p[i]);
66 delete p[i];
67 }
68 }
69 }
70
71 int
72 main(int argc, char **argv) {
73 uint32 pNum = 0;
74 uint32 pAlloc = 8388608;
75 uint32 estID = ~uint32ZERO;
76
77 sim4polishStyle style = sim4polishStyleDefault;
78
79 int arg = 1;
80
81 while (arg < argc) {
82 if (strcmp(argv[1], "-gff3") == 0)
83 style = sim4polishGFF3;
84 else
85 fprintf(stderr, "usage: %s [-gff3] < file > file\n", argv[0]);
86
87 arg++;
88 }
89
90
91 if (isatty(fileno(stdin))) {
92 fprintf(stderr, "usage: %s [-gff3] < file > file\n", argv[0]);
93
94 if (isatty(fileno(stdin)))
95 fprintf(stderr, "error: I cannot read polishes from the terminal!\n\n");
96
97 exit(1);
98 }
99
100 // Read polishes, picking the best when we see a change in
101 // the estID.
102
103 sim4polishReader *R = new sim4polishReader("-");
104 sim4polish **p = new sim4polish * [pAlloc];
105 sim4polish *q = 0L;
106
107 W = new sim4polishWriter("-", style);
108
109 if (R->getsim4polishStyle() != style)
110 fprintf(stderr, "warning: input format and output format differ.\n");
111
112 while (R->nextAlignment(q)) {
113 if ((q->_estID != estID) && (pNum > 0)) {
114 pickBest(p, pNum);
115 pNum = 0;
116 }
117
118 if (pNum >= pAlloc) {
119 sim4polish **P = new sim4polish * [pAlloc * 2];
120 memcpy(p, P, sizeof(sim4polish *) * pAlloc);
121 delete [] p;
122 p = P;
123 pAlloc *= 2;
124 }
125
126 p[pNum++] = q;
127 estID = q->_estID;
128
129 q = 0L; // Else we will delete the polish we just saved!
130 }
131
132 if (pNum > 0)
133 pickBest(p, pNum);
134
135 delete [] p;
136
137 delete R;
138 delete W;
139
140 return(0);
141 }
142