Debian Codegraph @

sim4dbutils/removeRedundant.C

raw
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <errno.h>
4
5 #include "bio++.H"
6 #include "sim4.H"
7 #include "s4p_overlap.H"
8
9 // Remove redundant polishes from an input set.
10 //
11 // Redundancy is defined as two polishes that overlap on the genome.
12 // Any amount of overlap is redundant.
13 //
14 // The longest of the overlapping matches is saved.
15
16 //#define DEBUGOUT
17
18 int
19 main(int argc, char **argv) {
20
21 if (argc < 2) {
22 fprintf(stderr, "usage: %s [-gff3] <polishes-file>\n", argv[0]);
23 fprintf(stderr, "(yes, you _must_ give it a file. stdin is not possible.)\n");
24 fprintf(stderr, "WARNING THIS IS PROTOTYPE BROKEN CODE!\n");
25 exit(1);
26 }
27
28 sim4polishStyle wstyle = sim4polishStyleDefault;
29 sim4polishStyle rstyle = sim4polishStyleDefault;
30
31 int arg = 1;
32 while (arg < argc) {
33 if (strcmp(argv[arg], "-gff3") == 0) {
34 wstyle = sim4polishGFF3;
35 }
36
37 arg++;
38 }
39
40
41 uint32 matchesWithNoOverlap = 0;
42 uint32 matchesWithOverlap = 0;
43 uint32 notPerfectClique = 0;
44
45 // Open a polishFile and force the index to build
46 // First find the input file type, with a hack
47
48 sim4polishReader *reader = new sim4polishReader(argv[argc-1]);
49 rstyle = reader->getsim4polishStyle();
50 delete reader;
51
52 sim4polishFile *Afile = new sim4polishFile(argv[argc-1], rstyle);
53 Afile->setPosition(0);
54
55 sim4polishWriter *writer = new sim4polishWriter("-", wstyle);
56
57 if (rstyle != wstyle)
58 fprintf(stderr, "warning: input format and output format differ.\n");
59
60
61 // Ask both for the largest EST iid seen, then iterate over those.
62 //
63 uint32 largestIID = Afile->maxIID();
64
65 for (uint32 iid=0; iid<largestIID; iid++) {
66 sim4polishList *A = Afile->getEST(iid);
67
68 if (A->length() > 0) {
69
70 // fill out the overlap matrix
71
72 olap_t **overlap = new olap_t* [A->length()];
73 overlap[0] = new olap_t [A->length() * A->length()];
74 for (uint32 i=1; i<A->length(); i++)
75 overlap[i] = overlap[i-1] + A->length();
76
77 for (uint32 a=0; a<A->length(); a++)
78 for (uint32 b=0; b<A->length(); b++)
79 if (a == b)
80 overlap[a][b] = 0;
81 else
82 overlap[a][b] = findOverlap((*A)[a], (*A)[b]);
83
84 // look for guys with no overlaps, print and remove them
85
86 sim4polishList *W = new sim4polishList;
87
88 for (uint32 a=0; a<A->length(); a++) {
89 bool nooverlaps = true;
90
91 for (uint32 b=0; b<A->length(); b++)
92 if (overlap[a][b])
93 nooverlaps = false;
94
95 if (nooverlaps) {
96 matchesWithNoOverlap++;
97
98 writer->writeAlignment((*A)[a]);
99 } else {
100 matchesWithOverlap++;
101 W->push(new sim4polish((*A)[a]));
102 }
103 }
104
105
106 #if 1
107 fprintf(stderr, "IID="uint32FMTW(8)" -- overlap:"uint32FMT" noOverlap:"uint32FMT"\r",
108 iid, matchesWithOverlap, matchesWithNoOverlap);
109 fflush(stderr);
110 #endif
111
112
113 // A is junk, W contains the matches that overlap.
114
115 delete A;
116 A = 0L;
117
118
119 // Report all the overlaps
120
121 #ifdef DEBUGOUT
122 for (uint32 a=0; a<W->length(); a++) {
123 sim4polish *p = (*W)[a];
124 fprintf(stderr, uint32FMTW(3)": "uint32FMTW(3)"--"uint32FMTW(3)"\n",
125 iid, p->exons[0].genFrom, p->exons[p->numExons-1].genTo);
126 }
127 #endif
128
129
130
131 // while we have matches in the set of overlapping matches,
132 // find a connected component, check that it is/is not a
133 // clique, and decide which match to keep.
134
135 uint32 *clique = new uint32 [W->length()];
136 uint32 cliqueSize = 0;
137 bool inserted = false;
138 uint32 *length = new uint32 [W->length()];
139
140 while (W->length() > 0) {
141
142 #ifdef DEBUGOUT
143 fprintf(stderr, "IID="uint32FMTW(8)" -- examine "uint32FMT" matches\n",
144 iid, W->length());
145 #endif
146
147 // Find the length of all the matches in this set
148
149 for (uint32 a=0; a<W->length(); a++) {
150 length[a] = 0;
151 for (uint32 i=0; i<(*W)[a]->_numExons; i++)
152 length[a] += (*W)[a]->_exons[i]._genTo - (*W)[a]->_exons[i]._genFrom + 1;
153 }
154
155 // reconstruct the overlap matrix -- hey, if you want to be
156 // efficient and recover this from the existing one, nobody is
157 // stopping you.
158
159 for (uint32 a=0; a<W->length(); a++)
160 for (uint32 b=0; b<W->length(); b++)
161 if (a == b)
162 overlap[a][b] = 0;
163 else
164 overlap[a][b] = findOverlap((*W)[a], (*W)[b]);
165
166 // OK, now find the clique/connected component
167
168 for (uint32 i=0; i<W->length(); i++)
169 clique[i] = 0;
170
171 clique[0] = 1;
172 cliqueSize = 1;
173 inserted = true;
174
175 while (inserted) {
176 inserted = false;
177
178 // If a is in the clique, add all it's overlaps
179
180 for (uint32 a=0; a<W->length(); a++) {
181 if (clique[a]) {
182 for (uint32 b=0; b<W->length(); b++) {
183 if ((overlap[a][b]) && (!clique[b])) {
184 clique[b] = 1;
185 cliqueSize++;
186 inserted = true;
187 }
188 }
189 }
190 }
191 }
192
193 #ifdef DEBUGOUT
194 fprintf(stderr, "IID="uint32FMTW(8)" -- examine "uint32FMT" matches, found "uint32FMT" overlapping\n",
195 iid, W->length(), cliqueSize);
196 #endif
197
198 // Check that it is a clique
199
200 if (cliqueSize > 2) {
201
202 uint32 num = 0;
203
204 for (uint32 a=0; a<W->length(); a++)
205 for (uint32 b=0; b<W->length(); b++)
206 if (clique[a] && clique[b] && overlap[a][b])
207 num++;
208
209 if (num != cliqueSize * (cliqueSize-1)) {
210 notPerfectClique++;
211
212 fprintf(stderr, "\nNOT A PERFECT CLIQUE! Found "uint32FMT" overlaps, wanted "uint32FMT" in the clique.\n",
213 num, cliqueSize * (cliqueSize-1));
214
215 //for (uint32 a=0; a<W->length(); a++)
216 // if (clique[a])
217 // writer->writeAlignment((*W)[a]);
218 }
219
220 }
221
222 // Find the longest member, output it
223
224 uint32 longest = 0;
225 while (clique[longest] == 0)
226 longest++;
227
228 for (uint32 i=0; i<W->length(); i++)
229 if ((clique[i]) && (length[longest] < length[i]))
230 longest = i;
231
232 writer->writeAlignment((*W)[longest]);
233
234 // Remove the clique from the set of overlaps
235
236 A = new sim4polishList;
237 for (uint32 i=0; i<W->length(); i++) {
238 if (clique[i] == 0)
239 A->push(new sim4polish((*W)[i]));
240 }
241
242 delete W;
243 W = A;
244 A = 0L;
245 }
246
247 delete [] clique;
248 delete W;
249
250 delete [] overlap[0];
251 delete [] overlap;
252 }
253
254 delete A;
255 }
256
257 delete writer;
258 delete Afile;
259
260 fprintf(stderr, "\nmatches withOvl:"uint32FMT" withoutOvl:"uint32FMT"\n",
261 matchesWithOverlap, matchesWithNoOverlap);
262 fprintf(stderr, "not perfect clique:"uint32FMT"\n", notPerfectClique);
263 }
264
265