Debian Codegraph @

sim4dbutils/comparePolishes.C

raw
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <errno.h>
4 #include <string.h>
5
6 #include "bio++.H"
7 #include "sim4.H"
8 #include "s4p_overlap.H"
9
10 // Matches two sets of polishes to each other using a simple overlap
11 // heuristic.
12 //
13 // Arguments (are horrible, whatcha gonna do about it?):
14 //
15 // -i min percent id (default 95)
16 // -c min percent coverage (default 50)
17 // -a polishes input file 1
18 // -b polishes input file 2
19 // -gff3 write output as GFF3
20 //
21 // Output is on standard out, and is tab-delimited. It reports
22 // stuff about the 'same' matches:
23 //
24 // ESTiid ESTlen overlap A%id A%cov #cdnagaps #exons B%id B%cov #cdnagaps #exons
25
26 // Try to analyze cDNA gaps.
27 //
28 // For cDNA gaps larger than GAP_MINIMUM, count it as a gap only if
29 // the genomic gap is within GAP_DIFFERENCE of the cDNA gap.
30 //
31 // XXX This needs some tweaking!
32 //
33 #define GAP_MINIMUM 10
34 #define GAP_DIFFERENCE 4
35
36
37 sim4polishWriter *
38 openOutput(const char *prefix, const char *suffix, sim4polishStyle style) {
39 char name[FILENAME_MAX];
40 sprintf(name, "%s.%s", prefix, suffix);
41 return(new sim4polishWriter(name, style));
42 }
43
44
45
46 int
47 main(int argc, char **argv) {
48 uint32 minI = 95;
49 uint32 minC = 50;
50 const char *prefix = "comparePolishes";
51 sim4polishFile *Afile = 0L;
52 sim4polishFile *Bfile = 0L;
53
54 // goodOverlap -- match in A maps uniquely to B and likewise.
55 //
56 // novelInA -- a match in A has no counterpart in B.
57 // novelInB -- similar for B.
58 //
59 // multipleInA -- a match in B maps to multiple things in A.
60 // multipleInB -- similar for A.
61 //
62 // multipleInA requires that the matches in A map only to the single
63 // match in B.
64 //
65 // hairyOverlap -- multiple matches in both.
66 //
67 uint32 goodOverlap = 0; // the number of lines in the output
68 uint32 novelInA = 0;
69 uint32 novelInB = 0;
70 uint32 multipleInA = 0;
71 uint32 multipleInB = 0;
72 uint32 hairyOverlap = 0;
73
74 bool doGFF3;
75
76 sim4polishStyle Astyle = sim4polishStyleDefault;
77 sim4polishStyle Bstyle = sim4polishStyleDefault;
78
79 sim4polishStyle style = sim4polishStyleDefault;
80
81
82 int arg=1;
83 while(arg < argc) {
84 if (strcmp(argv[arg], "-i") == 0) {
85 minI = atoi(argv[++arg]);
86 } else if (strcmp(argv[arg], "-c") == 0) {
87 minC = atoi(argv[++arg]);
88 } else if (strcmp(argv[arg], "-a") == 0) {
89 // Ugly hack to obtain the style of the input files, but can be fixed later
90
91 sim4polishReader *AR = new sim4polishReader(argv[++arg]);
92 Astyle = AR->getsim4polishStyle();
93 delete AR;
94
95 Afile = new sim4polishFile(argv[arg], Astyle);
96 } else if (strcmp(argv[arg], "-b") == 0) {
97 // Ugly hack to obtain the style of the input files, but can be fixed later
98
99 sim4polishReader *BR = new sim4polishReader(argv[++arg]);
100 Bstyle = BR->getsim4polishStyle();
101 delete BR;
102
103 Bfile = new sim4polishFile(argv[arg], Bstyle);
104 } else if (strcmp(argv[arg], "-p") == 0) {
105 prefix = argv[++arg];
106 } else if (strcmp(argv[arg], "-gff3") == 0) {
107 doGFF3 = true;
108 style = sim4polishGFF3;
109 }
110 arg++;
111 }
112
113 if ((Afile == 0L) || (Bfile == 0L)) {
114 fprintf(stderr, "usage: %s [-i percent-identity] [-c percent-coverage] -a input-set-a -b input-set-b [-p output-prefix] [-gff3]\n", argv[0]);
115 fprintf(stderr, "only -a and -b are mandatory, but you should give all anyway\n");
116 exit(1);
117 }
118
119 // Open the output files
120 //
121 sim4polishWriter *fasame = openOutput(prefix, "a-same", style);
122 sim4polishWriter *fbsame = openOutput(prefix, "b-same", style);
123 sim4polishWriter *fanovel = openOutput(prefix, "a-novel", style);
124 sim4polishWriter *fbnovel = openOutput(prefix, "b-novel", style);
125 sim4polishWriter *famulti = openOutput(prefix, "a-multi", style);
126 sim4polishWriter *fbmulti = openOutput(prefix, "b-multi", style);
127 sim4polishWriter *fhairy = openOutput(prefix, "hairy", style);
128
129 // Force index builds
130 //
131 Afile->setPosition(0);
132 Bfile->setPosition(0);
133
134
135 // Find the largest IID
136 //
137 uint32 largestIID = Afile->maxIID();
138 if (largestIID < Bfile->maxIID())
139 largestIID = Bfile->maxIID();
140
141
142 // Iterate over all the ESTs.
143
144 for (uint32 iid=0; iid<largestIID; iid++) {
145 sim4polishList *A = Afile->getEST(iid);
146 sim4polishList *B = Bfile->getEST(iid);
147 sim4polishList *Ta = 0L;
148 sim4polishList *Tb = 0L;
149
150 // Filter by quality.
151 A->filterByQuality(minI, minC);
152 B->filterByQuality(minI, minC);
153
154 // fill out the overlap matrix
155
156 olap_t **overlap = new olap_t* [A->length()];
157 overlap[0] = new olap_t [A->length() * B->length()];
158 for (uint32 i=1; i<A->length(); i++)
159 overlap[i] = overlap[i-1] + B->length();
160
161 for (uint32 a=0; a<A->length(); a++)
162 for (uint32 b=0; b<B->length(); b++)
163 overlap[a][b] = findOverlap((*A)[a], (*B)[b]);
164
165
166
167 // Find and remove those matches that are unique to either set.
168 // Removing is a big pain, because we either have to know
169 // something about the removal process, or we need to rebuild the
170 // overlap matrix after each removal. Instead, we build a new set.
171
172 bool *removeA = new bool [A->length()];
173 bool *removeB = new bool [B->length()];
174
175 for (uint32 a=0; a<A->length(); a++)
176 removeA[a] = false;
177
178 for (uint32 b=0; b<B->length(); b++)
179 removeB[b] = false;
180
181
182 for (uint32 a=0; a<A->length(); a++) {
183 uint32 ovl = 0;
184
185 for (uint32 b=0; b<B->length(); b++)
186 if (overlap[a][b])
187 ovl++;
188
189 if (ovl == 0) {
190 removeA[a] = true;
191 novelInA++;
192
193 if (fanovel)
194 fanovel->writeAlignment((*A)[a]);
195 }
196 }
197
198 for (uint32 b=0; b<B->length(); b++) {
199 uint32 ovl = 0;
200
201 for (uint32 a=0; a<A->length(); a++)
202 if (overlap[a][b])
203 ovl++;
204
205 if (ovl == 0) {
206 removeB[b] = true;
207 novelInB++;
208
209 if (fbnovel)
210 fbnovel->writeAlignment((*B)[b]);
211 }
212 }
213
214 //
215 // Now find all those that are perfect matches. Yeah, yeah, we
216 // could ignore those that we already marked for removal.
217 //
218
219 for (uint32 a=0; a<A->length(); a++) {
220 uint32 Boverlaps = 0;
221 uint32 theBovl = 0;
222
223 // Count the number of things we overlap in B.
224 for (uint32 b=0; b<B->length(); b++) {
225 if (overlap[a][b]) {
226 Boverlaps++;
227 theBovl = b;
228 }
229 }
230
231 // If exactly one overlap, we just need to check if the guy in B
232 // also has one overlap with anybody in A.
233
234 if (Boverlaps == 1) {
235
236 // Count the number of overlaps the guy in B has with A. If
237 // 1, it's a goodOverlap, else it's a multipleInA.
238
239 uint32 b = theBovl;
240
241 uint32 Aoverlaps = 0;
242 for (uint32 x=0; x<A->length(); x++)
243 if (overlap[x][b])
244 Aoverlaps++;
245
246 if (Aoverlaps == 1) {
247 removeA[a] = true;
248 removeB[b] = true;
249 goodOverlap++;
250
251 // ESTiid ESTlen overlap A%id A%cov AgenLen #exons #cdnagaps B%id B%cov BgenLen #exons #cdnagaps
252
253 uint32 AgenLen = 0, BgenLen = 0;
254 uint32 Agaps = 0, Bgaps = 0;
255
256 for (uint32 x=0; x < (*A)[a]->_numExons; x++)
257 AgenLen += (*A)[a]->_exons[x]._genTo - (*A)[a]->_exons[x]._genFrom + 1;
258
259 for (uint32 x=0; x < (*B)[b]->_numExons; x++)
260 BgenLen += (*B)[b]->_exons[x]._genTo - (*B)[b]->_exons[x]._genFrom + 1;
261
262 #ifdef GAP_MINIMUM
263 for (uint32 x=1; x < (*A)[a]->_numExons; x++) {
264 int egap = (*A)[a]->_exons[x]._estFrom - (*A)[a]->_exons[x-1]._estTo;
265 int ggap = (*A)[a]->_exons[x]._genFrom - (*A)[a]->_exons[x-1]._genTo;
266 int dgap = 0;
267
268 if (egap > ggap)
269 dgap = egap - ggap;
270 else
271 dgap = ggap - egap;
272
273 if ((egap > GAP_MINIMUM) &&
274 (dgap < GAP_DIFFERENCE))
275 Agaps++;
276 }
277
278 for (uint32 x=1; x < (*B)[b]->_numExons; x++) {
279 int egap = (*B)[b]->_exons[x]._estFrom - (*B)[b]->_exons[x-1]._estTo;
280 int ggap = (*B)[b]->_exons[x]._genFrom - (*B)[b]->_exons[x-1]._genTo;
281 int dgap = 0;
282
283 if (egap > ggap)
284 dgap = egap - ggap;
285 else
286 dgap = ggap - egap;
287
288 if ((egap > GAP_MINIMUM) &&
289 (dgap < GAP_DIFFERENCE))
290 Bgaps++;
291 }
292 #else
293 for (uint32 x=1; x < (*A)[a]->_numExons; x++)
294 if ( (*A)[a]->_exons[x]._estFrom - (*A)[a]->_exons[x-1]._estTo != 1 )
295 Agaps++;
296
297 for (uint32 x=1; x < (*B)[b]->_numExons; x++)
298 if ( (*B)[b]->_exons[x]._estFrom - (*B)[b]->_exons[x-1]._estTo != 1 )
299 Bgaps++;
300 #endif
301
302 double score = 0;
303 if (AgenLen > BgenLen)
304 score = (double)overlap[a][b] / (double)BgenLen;
305 else
306 score = (double)overlap[a][b] / (double)AgenLen;
307
308 fprintf(stdout, uint32FMT"\t"uint32FMT"\t"OLAPTFMT"\t%f\t%8.3f\t%8.3f\t"uint32FMT"\t"uint32FMT"\t"uint32FMT"\t%8.3f\t%8.3f\t"uint32FMT"\t"uint32FMT"\t"uint32FMT"\n",
309 iid,
310 (*A)[a]->_estLen,
311 overlap[a][b],
312 score,
313 (*A)[a]->s4p_percentIdentityExact(),
314 (*A)[a]->s4p_percentCoverageExact(),
315 AgenLen, (*A)[a]->_numExons, Agaps,
316 (*B)[b]->s4p_percentIdentityExact(),
317 (*B)[b]->s4p_percentCoverageExact(),
318 BgenLen, (*B)[b]->_numExons, Bgaps);
319
320 if (fasame)
321 fasame->writeAlignment((*A)[a]);
322 if (fbsame)
323 fbsame->writeAlignment((*B)[b]);
324 }
325 }
326 }
327
328 //
329 // Rebuild
330 //
331
332 Ta = new sim4polishList;
333 Tb = new sim4polishList;
334
335 for (uint32 a=0; a<A->length(); a++)
336 if (removeA[a] == false)
337 Ta->push(new sim4polish((*A)[a]));
338
339 for (uint32 b=0; b<B->length(); b++)
340 if (removeB[b] == false)
341 Tb->push(new sim4polish((*B)[b]));
342
343 delete A;
344 delete B;
345 A = Ta;
346 B = Tb;
347 Ta = Tb = 0L;
348
349 // Rebuild overlaps
350 //
351 for (uint32 a=0; a<A->length(); a++)
352 for (uint32 b=0; b<B->length(); b++)
353 overlap[a][b] = findOverlap((*A)[a], (*B)[b]);
354
355
356 //
357 // And now all we're left with is a bunch of intersecting crud.
358 //
359
360
361 // Grab the first match in A. Find all the overlaps with things
362 // in B. For each of those, find the overlaps in A. Repeat
363 // until nothing changes. Generate a report. Remove all those
364 // matches. Do it all again until there are no more matches.
365
366 while (A->length()) {
367 for (uint32 a=0; a<A->length(); a++)
368 removeA[a] = false;
369
370 for (uint32 b=0; b<B->length(); b++)
371 removeB[b] = false;
372
373 removeA[0] = true;
374
375 bool keepGoing = true;
376
377 while (keepGoing) {
378 keepGoing = false;
379
380 // For all of A, if we have something marked for removal, see if we
381 // overlap with anything in B. If that b is not marked for removal,
382 // mark it, and keep going.
383 //
384 for (uint32 a=0; a<A->length(); a++) {
385 if (removeA[a]) {
386 for (uint32 b=0; b<B->length(); b++) {
387 if ((overlap[a][b]) && (removeB[b] == false)) {
388 removeB[b] = true;
389 keepGoing = true;
390 }
391 }
392 }
393 }
394
395 // Same thing, but for B.
396 //
397 for (uint32 b=0; b<B->length(); b++) {
398 if (removeB[b]) {
399 for (uint32 a=0; a<A->length(); a++) {
400 if ((overlap[a][b]) && (removeA[a] == false)) {
401 removeA[a] = true;
402 keepGoing = true;
403 }
404 }
405 }
406 }
407 }
408
409 // Found a component. Output it.
410
411 uint32 inA = 0;
412 uint32 inB = 0;
413
414 for (uint32 a=0; a<A->length(); a++)
415 if (removeA[a])
416 inA++;
417 for (uint32 b=0; b<B->length(); b++)
418 if (removeB[b])
419 inB++;
420
421 if ((inA > 1) && (inB > 1)) {
422 hairyOverlap++;
423
424 //fprintf(fhairy, "EST="uint32FMT" "uint32FMT" "uint32FMT"\n", (*A)[0]->_estID, inA, inB);
425 for (uint32 a=0; a<A->length(); a++)
426 if (removeA[a])
427 fhairy->writeAlignment((*A)[a]);
428 for (uint32 b=0; b<B->length(); b++)
429 if (removeB[b])
430 fhairy->writeAlignment((*B)[b]);
431 } else if ((inA == 1) && (inB > 1)) {
432 multipleInB++;
433
434 //fprintf(fbmulti, "EST="uint32FMT" "uint32FMT" "uint32FMT"\n", (*A)[0]->_estID, inA, inB);
435 for (uint32 a=0; a<A->length(); a++)
436 if (removeA[a])
437 fbmulti->writeAlignment((*A)[a]);
438 for (uint32 b=0; b<B->length(); b++)
439 if (removeB[b])
440 fbmulti->writeAlignment((*B)[b]);
441 } else if ((inA > 1) && (inB == 1)) {
442 multipleInA++;
443
444 //fprintf(famulti, "EST="uint32FMT" "uint32FMT" "uint32FMT"\n", (*A)[0]->_estID, inA, inB);
445 for (uint32 a=0; a<A->length(); a++)
446 if (removeA[a])
447 famulti->writeAlignment((*A)[a]);
448 for (uint32 b=0; b<B->length(); b++)
449 if (removeB[b])
450 famulti->writeAlignment((*B)[b]);
451 } else {
452 fprintf(stderr, "ERROR! inA="uint32FMT" inB="uint32FMT"\n", inA, inB);
453 }
454
455 //
456 // Rebuild
457 //
458
459 Ta = new sim4polishList;
460 Tb = new sim4polishList;
461
462 for (uint32 a=0; a<A->length(); a++)
463 if (removeA[a] == false)
464 Ta->push(new sim4polish((*A)[a]));
465
466 for (uint32 b=0; b<B->length(); b++)
467 if (removeB[b] == false)
468 Tb->push(new sim4polish((*B)[b]));
469
470 delete A;
471 delete B;
472 A = Ta;
473 B = Tb;
474 Ta = Tb = 0L;
475
476 // Rebuild overlaps
477 //
478 for (uint32 a=0; a<A->length(); a++)
479 for (uint32 b=0; b<B->length(); b++)
480 overlap[a][b] = findOverlap((*A)[a], (*B)[b]);
481 }
482
483 if ((iid % 100) == 0) {
484 fprintf(stderr, "IID:"uint32FMTW(8)" good:"uint32FMTW(4)" Anovel:"uint32FMTW(4)" Amulti:"uint32FMTW(4)" Bnovel:"uint32FMTW(4)" Bmulti:"uint32FMTW(4)" hairy:"uint32FMTW(4)"\r",
485 iid,
486 goodOverlap, novelInA, multipleInA, novelInB, multipleInB, hairyOverlap);
487 fflush(stderr);
488 }
489
490 #if 0
491 if ((iid % 1234) == 0) {
492 fprintf(stderr, "IID:"uint32FMTW(8)" good:"uint32FMTW(4)" Anovel:"uint32FMTW(4)" Amulti:"uint32FMTW(4)" Bnovel:"uint32FMTW(4)" Bmulti:"uint32FMTW(4)" hairy:"uint32FMTW(4)"\r",
493 iid,
494 goodOverlap, novelInA, multipleInA, novelInB, multipleInB, hairyOverlap);
495 fflush(stderr);
496 }
497 #endif
498
499 delete [] overlap[0];
500 delete [] overlap;
501
502 delete [] removeA;
503 delete [] removeB;
504
505 delete A;
506 delete B;
507 }
508
509 delete fasame;
510 delete fbsame;
511 delete fanovel;
512 delete fbnovel;
513 delete famulti;
514 delete fbmulti;
515 delete fhairy;
516
517 delete Afile;
518 delete Bfile;
519
520 fprintf(stderr, "\ngood:"uint32FMTW(4)" Anovel:"uint32FMTW(4)" Amulti:"uint32FMTW(4)" Bnovel:"uint32FMTW(4)" Bmulti:"uint32FMTW(4)" hairy:"uint32FMTW(4)"\n",
521 goodOverlap, novelInA, multipleInA, novelInB, multipleInB, hairyOverlap);
522
523 exit(0);
524 }