Debian Codegraph @

atac-driver/gapShifter/correctGaps.C

raw symbols SCIP
1 // This file is part of A2Amapper.
2 // Copyright (c) 2006 J. Craig Venter Institute
3 // Author: Brian Walenz
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received (LICENSE.txt) a copy of the GNU General Public
16 // License along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22
23 #include "bio++.H"
24 #include "atac.H"
25
26
27 void
28 usage(char *name) {
29 fprintf(stderr, "usage: %s [] -m matches -l log\n", name);
30 fprintf(stderr, " When it works, fill this in...\n");
31 }
32
33
34 int
35 main(int argc, char *argv[]) {
36 char *matchesFile = 0L;
37 FILE *logFile = 0L;
38
39 int arg=1;
40 while (arg < argc) {
41 if (strcmp(argv[arg], "-m") == 0) {
42 matchesFile = argv[++arg];
43 } else if (strcmp(argv[arg], "-l") == 0) {
44 errno = 0;
45 logFile = fopen(argv[++arg], "w");
46 if (errno)
47 fprintf(stderr, "Failed to open logfile '%s': %s\n", strerror(errno), argv[arg]), exit(1);
48 } else {
49 usage(argv[0]);
50 exit(1);
51 }
52 arg++;
53 }
54
55 if (matchesFile == 0L)
56 usage(argv[0]), exit(1);
57 if (logFile == 0L)
58 usage(argv[0]), exit(1);
59
60 atacFile AF(matchesFile);
61 atacMatchList &ML = *AF.matches();
62 atacMatchOrder MO(ML);
63
64 // Sort by either axis.
65 MO.sortA();
66
67 // We need to compute the identity of the gap; our metric (thanks to Nelson) is
68 // if ("long" and "not low identity") or ("short"), close the gap
69
70 // We could use the seqCache, but with only a handful of gaps, we
71 // just let the OS cache stuff.
72
73 seqCache *C1 = new seqCache(AF.assemblyFileA(), 2, false);
74 seqCache *C2 = new seqCache(AF.assemblyFileB(), 1024, false);
75
76 seqInCore *S1 = 0L;
77 seqInCore *S2 = 0L;
78
79 for (uint32 iter=0; iter<10; iter++) {
80 uint32 gapsize = 1000;
81 uint32 fgaps = 0;
82 uint32 rgaps = 0;
83
84 int mergeuid = 1;
85
86 for (uint32 i=1; i<MO.numMatches(); i++) {
87 atacMatch *l = MO[i-1];
88 atacMatch *r = MO[i];
89
90 bool joinMatches = false;
91
92 uint32 gap1 = 0;
93 uint32 gap2 = 0;
94
95 if ((l->iid1 == r->iid1) && // Matches are between the same sequences
96 (l->iid2 == r->iid2) &&
97 (l->fwd2 == r->fwd2)) { // Matches are the same orientation
98
99
100 if (l->fwd2 == true) {
101 if ((l->pos1 + l->len1 <= r->pos1) && // Matches are ordered correctly (should be, from the sort)
102 (l->pos2 + l->len2 <= r->pos2)) {
103
104 gap1 = r->pos1 - (l->pos1 + l->len1);
105 gap2 = r->pos2 - (l->pos2 + l->len2);
106
107 if ((gap1 == gap2) &&
108 (gap1 <= gapsize)) {
109 S1 = C1->getSequenceInCore(l->iid1);
110 S2 = C2->getSequenceInCore(l->iid2);
111
112 char *s1 = S1->sequence() + l->pos1 + l->len1;
113 char *s2 = S2->sequence() + l->pos2 + l->len1;
114
115 uint32 identities = 0;
116 uint32 n1 = 0;
117 uint32 n2 = 0;
118 for (uint32 p=0; p<gap1; p++) {
119 if (toUpper[(int)s1[p]] == toUpper[(int)s2[p]])
120 identities++;
121 if (toUpper[(int)s1[p]] == 'N')
122 n1++;
123 if (toUpper[(int)s2[p]] == 'N')
124 n2++;
125 }
126
127 if ((100*n1 < 20*gap1) && // gap is not N and
128 (100*n2 < 20*gap1) && // gap is not N and
129 ((100*identities < 80*gap1) || // (gap is high identity
130 ((gap1 < 11) && (2*gap1 <= l->len1) && (2*gap1 <= r->len1)) || // (gap is short, and the flanks are big
131 ((gap1 < 11) && (100*identities < 90*gap1)))) { // (gap is short and high quality
132
133 // ALSO need to check that the gap is not actually
134 // mapped in sequence 2. Not really, just make sure
135 // these two matches are in the same run.
136 //
137 if (strcmp(l->parentuid, r->parentuid) != 0) {
138 fprintf(logFile, "HEY! F gap of size "uint32FMT" not in a run?\n", gap1);
139 l->print(logFile, AF.labelA(), AF.labelB());
140 r->print(logFile, AF.labelA(), AF.labelB());
141 } else {
142 fgaps++;
143
144 joinMatches = true;
145
146 //fprintf(logFile, "potential f gap of size L "uint32FMTW(4)" (n1="uint32FMTW(4)" n2="uint32FMTW(4)" ident="uint32FMTW(4)"/"uint32FMTW(4)")!\n",
147 // gap1, n1, n2, identities, gap1);
148 //l->print(logFile, AF.labelA(), AF.labelB());
149 //r->print(logFile, AF.labelA(), AF.labelB());
150 }
151 }
152 }
153 }
154 } // was a forward match
155
156
157
158 if (l->fwd2 == false) {
159 if ((l->pos1 + l->len1 <= r->pos1) && // Matches are ordered correctly (should be, from the sort)
160 (r->pos2 + r->len2 <= l->pos2)) {
161
162 gap1 = r->pos1 - (l->pos1 + l->len1);
163 gap2 = l->pos2 - (r->pos2 + r->len2);
164
165 if ((gap1 == gap2) &&
166 (gap1 <= gapsize)) {
167
168 S1 = C1->getSequenceInCore(l->iid1);
169 S2 = C2->getSequenceInCore(l->iid2);
170
171 char *s1 = S1->sequence() + l->pos1 + l->len1;
172 char *s2 = S2->sequence() + r->pos2 + r->len2;
173
174 uint32 identities = 0;
175 uint32 n1 = 0;
176 uint32 n2 = 0;
177 for (uint32 p=0, q=gap1-1; p<gap1; p++, q--) {
178 if (toUpper[(int)s1[p]] == toUpper[complementSymbol[(int)s2[q]]])
179 identities++;
180 if (toUpper[(int)s1[p]] == 'N')
181 n1++;
182 if (toUpper[(int)s2[q]] == 'N')
183 n2++;
184 }
185
186 // Gap is short, flanks are big
187 // Gap is short, flanks are short and gap is good
188
189 if ((100*n1 < 20*gap1) && // gap is not N and
190 (100*n2 < 20*gap1) && // gap is not N and
191 ((100*identities < 80*gap1) || // (gap is high identity
192 ((gap1 < 11) && (2*gap1 <= l->len1) && (2*gap1 <= r->len1)) || // (gap is short, and the flanks are big
193 ((gap1 < 11) && (100*identities < 90*gap1)))) { // (gap is short and high quality
194
195 // ALSO need to check that the gap is not actually
196 // mapped in sequence 2. Not really, just make sure
197 // these two matches are in the same run.
198 //
199 if (strcmp(l->parentuid, r->parentuid) != 0) {
200 fprintf(logFile, "HEY! R gap of size "uint32FMT" not in a run?\n", gap1);
201 l->print(logFile, AF.labelA(), AF.labelB());
202 r->print(logFile, AF.labelA(), AF.labelB());
203 } else {
204 rgaps++;
205
206 joinMatches = true;
207
208 //fprintf(logFile, "potential r gap of size L "uint32FMTW(4)" (n1="uint32FMTW(4)" n2="uint32FMTW(4)" ident="uint32FMTW(4)"/"uint32FMTW(4)")!\n",
209 // gap1, n1, n2, identities, gap1);
210 //l->print(logFile, AF.labelA(), AF.labelB());
211 //r->print(logFile, AF.labelA(), AF.labelB());
212 }
213 }
214
215
216 }
217 }
218 }
219 }
220
221 if (joinMatches) {
222 fprintf(logFile, "CLOSE "uint32FMT"----------------------------------------\n", gap1);
223 l->print(logFile, AF.labelA(), AF.labelB());
224 r->print(logFile, AF.labelA(), AF.labelB());
225
226 MO.mergeMatches(l, r, mergeuid);
227
228 l->print(logFile, AF.labelA(), AF.labelB());
229
230 mergeuid++;
231 i--;
232 }
233 }
234
235 fprintf(logFile, "At gapSize="uint32FMT" closed "uint32FMT" f-gaps and "uint32FMT" r-gaps.\n", gapsize, fgaps, rgaps);
236
237 if (fgaps + rgaps == 0)
238 iter = 10;
239 }
240
241
242 #if 0
243 // This analyzes an atac mapping, looking for a signature that indicates a bad
244 // alignment. If we have an alignment of:
245 // XXXXXXaC-YYYYYY
246 // XXXXXX-CtYYYYYY
247 // this will generate three matches, instead of one match with mismatches in it.
248 // We scan the FORWARD matches for this pattern, and report any we find.
249 //
250 // We only found 3 on huref4 vs b35. Further development here was stopped.
251
252 for (uint32 i=2; i<ML.numMatches(); i++) {
253 atacMatch *l = ML[i-2];
254 atacMatch *m = ML[i-1];
255 atacMatch *r = ML[i];
256
257 if (m->len1 < 3) { // The match in the middle is small
258
259 if ((l->iid1 == r->iid1) && // Matches are between the same sequences
260 (l->iid2 == r->iid2) &&
261 (l->fwd2 == r->fwd2)) { // Matches are the same orientation
262
263 if (l->fwd2 == true) {
264 if ((l->pos1 + l->len1 <= r->pos1) && // Matches are ordered correctly (should be, from the sort)
265 (l->pos2 + l->len2 <= r->pos2)) {
266
267 uint32 gapl1 = m->pos1 - (l->pos1 + l->len1);
268 uint32 gapl2 = m->pos2 - (l->pos2 + l->len2);
269 uint32 gapr1 = r->pos1 - (m->pos1 + m->len1);
270 uint32 gapr2 = r->pos2 - (m->pos2 + m->len2);
271
272 if ((gapl1 + gapr1 == gapl2 + gapr2) && (gapl1 + gapr1 < 5)) {
273 fprintf(logFile, "potential f fix of size L "uint32FMT" "uint32FMT" and R "uint32FMT" "uint32FMT"!\n",
274 gapl1, gapl2, gapr1, gapr2);
275 l->print(logFile, "A", "B");
276 m->print(logFile, "A", "B");
277 r->print(logFile, "A", "B");
278 }
279 } else {
280 fprintf(logFile, "sort is forward broken.\n");
281 }
282 } // was a forward match
283 }
284 }
285 }
286 #endif
287
288
289 // Write the new output to stdout -- we preserve runs here, but
290 // discard everything else.
291 //
292 AF.writeHeader(stdout);
293
294 for (uint32 i=0; i<MO.numMatches(); i++)
295 MO[i]->print(stdout, AF.labelA(), AF.labelB());
296
297 for (uint32 i=0; i<AF.runs()->numberOfMatches(); i++)
298 AF.runs()->getMatch(i)->print(stdout, AF.labelA(), AF.labelB());
299
300 return(0);
301 }