Debian Codegraph @

sim4dbutils/realignPolishes.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 "seqCache.H"
8 #include "sim4.H"
9
10 // This code takes basic sim4db format polishes and recomputes the
11 // alignments and scores. Required in the input polishes are the EST
12 // id, genomic id, exon coordinates and an orientation.
13
14 int
15 main(int argc, char **argv) {
16
17 // Load all the sequences. We really do need all the ESTs in core,
18 // since they probably aren't in a useful sorted order. You can
19 // probably figure out a way to get rid of the seqCache for the
20 // GEN. Doing so will reduce memory usage by about 50%.
21
22 seqCache *EST = 0L;
23 seqCache *GEN = 0L;
24 int mergeTolerancePerc = 0;
25 int mergeToleranceBase = 0;
26 int statsOnly = 0;
27 int warnOnChange = 0;
28
29 // Statistics on the exon merge
30
31 int mergedExons = 0;
32 int mergedMatches = 0;
33
34 int numcdnagaps = 0;
35 int nummatcheswithgaps = 0;
36
37 FILE *mergeLog = 0L;
38
39 int arg = 1;
40 while (arg < argc) {
41 if (strncmp(argv[arg], "-merge", 2) == 0) {
42 mergeTolerancePerc = atoi(argv[++arg]);
43 } else if (strncmp(argv[arg], "-b", 2) == 0) {
44 mergeToleranceBase = atoi(argv[++arg]);
45 } else if (strncmp(argv[arg], "-M", 2) == 0) {
46 mergeLog = fopen(argv[++arg], "w");
47 } else if (strncmp(argv[arg], "-e", 2) == 0) {
48 if (statsOnly) {
49 EST = new seqCache(argv[++arg], 1000, false); // debugging only!
50 } else {
51 EST = new seqCache(argv[++arg], 0, false);
52 EST->loadAllSequences();
53 }
54 } else if (strncmp(argv[arg], "-g", 2) == 0) {
55 GEN = new seqCache(argv[++arg], 0, false);
56 GEN->loadAllSequences();
57 } else if (strncmp(argv[arg], "-q", 2) == 0) {
58 statsOnly = 1;
59 } else if (strncmp(argv[arg], "-w", 2) == 0) {
60 warnOnChange = 1;
61 }
62 arg++;
63 }
64
65 if ((statsOnly == 0) && (!EST || !GEN)) {
66 fprintf(stderr, "usage: %s [-merge percent-tolerance] [-M merge-log] [-q] -e est.fasta -g genome.fasta < polishes > somewhere\n", argv[0]);
67 fprintf(stderr, "\n");
68 fprintf(stderr, " Polishes _MUST_ be sorted by genomic index.\n");
69 fprintf(stderr, " If not, performance will be worse than atrocious.\n");
70 fprintf(stderr, "\n");
71 fprintf(stderr, " percent-tolerance -- merge exons separated by gap if\n");
72 fprintf(stderr, " the cDNA and genomic gaps differ by less than p percent.\n");
73 fprintf(stderr, " A value of 5 means 5%%\n");
74 fprintf(stderr, "\n");
75 fprintf(stderr, " -q: Don't actually do the work, just count the statistics\n");
76 fprintf(stderr, "\n");
77 fprintf(stderr, "\n");
78 exit(1);
79 }
80
81
82 char *s1 = new char [16 * 1024 * 1024];
83 char *s2 = new char [16 * 1024 * 1024];
84 int l1 = 0;
85 int l2 = 0;
86
87 speedCounter *C = new speedCounter("%12.0f polishes -- %12.0f polishes/second\r",
88 1.0, 0xff, true);
89
90 sim4polishWriter *W = new sim4polishWriter("-", sim4polishS4DB);
91 sim4polishReader *R = new sim4polishReader("-");
92 sim4polish *p = 0L;
93
94 while (R->nextAlignment(p)) {
95
96 //fprintf(stdout, "BEFORE\n");
97 //p->s4p_printPolish(stdout);
98
99 // If we have a mergeTolerance, merge adjacent exons that are
100 // separated my approximately equal sized cDNA and genomic gaps.
101 //
102 // Possible a better way to do this is to check if the identity
103 // of the missing region is decent, too.
104
105 // Remember the id/cv of this guy for the log
106 //
107 double id = 0.0;
108 double cv = 0.0;
109 if (mergeLog) {
110 id = p->s4p_percentIdentityExact();
111 cv = p->s4p_percentCoverageExact();
112 }
113
114 int merged = 0;
115 int gapped = 0;
116
117 if ((mergeTolerancePerc > 0) || (mergeToleranceBase > 0)) {
118 for (uint32 i=1; i<p->_numExons; i++) {
119 int cgap = p->_exons[i]._estFrom - p->_exons[i-1]._estTo;
120 int ggap = p->_exons[i]._genFrom - p->_exons[i-1]._genTo;
121
122 bool mergeGap = false;
123
124 // New method -- check if the gaps are within 20bp of each other
125 //
126 int diff = cgap - ggap;
127 if (diff < 0)
128 diff = -diff;
129
130 if (diff < mergeToleranceBase)
131 mergeGap = true;
132
133
134 // Original method -- cehck if the gaps are within 10% of each other
135 //
136 int ctol = cgap * (100 + mergeTolerancePerc);
137 int gtol = ggap * (100 + mergeTolerancePerc);
138
139 cgap *= 100;
140 ggap *= 100;
141
142 if (((cgap < ggap) && (ctol > ggap)) ||
143 ((ggap < cgap) && (gtol > cgap)))
144 mergeGap = true;
145
146 if (cgap > 1) {
147 numcdnagaps++;
148 gapped++;
149 }
150
151 if ((cgap > 1) && (mergeGap)) {
152
153 // Merge i and i-1 if adding in the tolerance makes either
154 // the cgap or the ggap longer than the other gap. i.e., the
155 // cgap was shorter, but including the tolerance makes it
156 // longer, so they're about the same size.
157
158 if (mergeLog)
159 fprintf(mergeLog,
160 "MERGE: "uint32FMTW(4)"-"uint32FMTW(4)" (%6.2f,%6.2f) "uint32FMTW(4)"-"uint32FMTW(4)
161 " and "uint32FMTW(8)"-"uint32FMTW(8)" (%6.2f,%6.2f) "uint32FMTW(8)"-"uint32FMTW(8)"\n",
162 p->_exons[i-1]._estFrom, p->_exons[i-1]._estTo,
163 cgap / 100.0, ctol / 100.0,
164 p->_exons[i]._estFrom, p->_exons[i]._estTo,
165 p->_exons[i-1]._genFrom, p->_exons[i-1]._genTo,
166 ggap / 100.0, gtol / 100.0,
167 p->_exons[i]._genFrom, p->_exons[i]._genTo);
168
169 // merge exons
170 p->_exons[i-1]._estTo = p->_exons[i]._estTo;
171 p->_exons[i-1]._genTo = p->_exons[i]._genTo;
172
173 // delete this exon
174 p->s4p_deleteExon(i);
175
176 // Do it again!
177 i--;
178
179 merged++;
180 mergedExons++;
181 }
182 }
183
184 if (merged)
185 mergedMatches++;
186 if (gapped)
187 nummatcheswithgaps++;
188 }
189
190
191 // For each exon, generate an alignment
192
193
194 if (statsOnly == 0) {
195 p->_estLen = EST->getSequenceInCore(p->_estID)->sequenceLength();
196 p->_estPolyA = 0;
197 p->_estPolyT = 0;
198
199 for (uint32 i=0; i<p->_numExons; i++) {
200 l1 = p->_exons[i]._estTo - p->_exons[i]._estFrom + 1;
201 l2 = p->_exons[i]._genTo - p->_exons[i]._genFrom + 1;
202
203 strncpy(s1, EST->getSequenceInCore(p->_estID)->sequence() + p->_exons[i]._estFrom - 1, l1);
204 strncpy(s2, GEN->getSequenceInCore(p->_genID)->sequence() + p->_exons[i]._genFrom - 1, l2);
205
206 if (p->_matchOrientation == SIM4_MATCH_COMPLEMENT) {
207 strncpy(s1, EST->getSequenceInCore(p->_estID)->sequence() + p->_estLen - p->_exons[i]._estTo, l1);
208 reverseComplementSequence(s1, l1);
209 }
210
211 s1[l1] = 0;
212 s2[l2] = 0;
213
214 delete [] p->_exons[i]._estAlignment;
215 delete [] p->_exons[i]._genAlignment;
216
217 p->_exons[i]._estAlignment = new char [l1+l2+1];
218 p->_exons[i]._genAlignment = new char [l1+l2+1];
219
220 halign(s1, s2,
221 l1, l2,
222 p->_exons[i]._estAlignment,
223 p->_exons[i]._genAlignment);
224 }
225
226 // There isn't an intron after the last exon. Force it.
227 //
228 p->_exons[p->_numExons-1]._intronOrientation = SIM4_INTRON_NONE;
229
230 // Check that we didn't radically change things
231 uint32 nm = p->_numMatches;
232
233 p->s4p_updateAlignmentScores();
234
235 W->writeAlignment(p);
236
237 if (warnOnChange) {
238 uint32 diff = 0;
239 if (nm < p->_numMatches) diff = p->_numMatches - nm;
240 if (nm > p->_numMatches) diff = nm - p->_numMatches;
241
242 if (diff > p->_numMatches / 100)
243 fprintf(stdout, "WARNING: CHANGED! "uint32FMT" -> "uint32FMT"\n", nm, p->_numMatches);
244 }
245 }
246
247 if (merged) {
248 fprintf(mergeLog, "MERGED\tEST\t"uint32FMT"\tfrom\t%8.3f\t%8.3f\tto\t%8.3f\t%8.3f\n",
249 p->_estID, id, cv, p->s4p_percentIdentityExact(), p->s4p_percentCoverageExact());
250 }
251
252 C->tick();
253 }
254
255 if ((mergeTolerancePerc > 0) || (mergeToleranceBase > 0)) {
256 fprintf(stderr, "FOUND: %d gaps in %d matches.\n", numcdnagaps, nummatcheswithgaps);
257 fprintf(stderr, "MERGED: %d gaps in %d matches.\n", mergedExons, mergedMatches);
258 }
259
260 delete GEN;
261 delete EST;
262
263 return(0);
264 }