Debian Codegraph @

sim4dbutils/convertToAtac.C

raw
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5
6 #include "sim4.H"
7
8 // Writes polished from stdin as atac-format matches. Splits polishes on any indel to generate gapless
9 // atac matches (type 'u').
10 //
11 // Does no cleanup.
12
13
14 void
15 indelRedo(char *a, char *b) {
16 uint32 orig = 0;
17 uint32 copy = 0;
18
19 while (a[orig] && b[orig]) {
20 if ((a[orig] != '-') ||
21 (b[orig] != '-')) {
22 if (orig != copy) {
23 a[copy] = a[orig];
24 b[copy] = b[orig];
25 }
26 copy++;
27 }
28 orig++;
29 }
30
31 a[copy] = 0;
32 b[copy] = 0;
33 }
34
35
36 uint32
37 indelFixAlignment(char *a, char *b) {
38 bool redo = false;
39 uint32 len = strlen(a) - 1;
40 uint32 fixed = 0;
41
42 //fprintf(stdout, "fixIndel\n");
43 //fprintf(stdout, "%s\n%s\n", a, b);
44
45 for (uint32 i=2; i<len; i++) {
46
47 // -Ac
48 // cA- two gaps -> two mismatches
49 if ((a[i-2] == '-') && (b[i] == '-')) {
50 a[i-2] = toUpper[a[i-1]]; a[i-1] = toUpper[a[i]]; a[i] = '-';
51 b[i-2] = toUpper[b[i-2]]; b[i-1] = toUpper[b[i-1]]; b[i] = '-';
52 fixed++;
53 redo = true;
54 }
55
56 if ((a[i] == '-') && (b[i-2] == '-')) {
57 a[i-2] = toUpper[a[i-2]]; a[i-1] = toUpper[a[i-1]]; a[i] = '-';
58 b[i-2] = toUpper[b[i-1]]; b[i-1] = toUpper[b[i]]; b[i] = '-';
59 fixed++;
60 redo = true;
61 }
62 }
63
64 if (redo) {
65 //fprintf(stdout, "%s\n%s\n", a, b);
66 //fprintf(stdout, "Fixed "uint32FMT" 1 base wide indel\n", fixed);
67 indelRedo(a, b);
68 }
69
70 redo = false;
71 len = strlen(a) - 1;
72
73 for (uint32 i=3; i<len; i++) {
74
75 // cAt-
76 // -Agg two gaps, one mismatch -> three mismatches
77 // we also would do two gaps -> three mismatches
78 if ((a[i] == '-') && (b[i-3] == '-')) {
79 a[i-3] = toUpper[a[i-3]]; a[i-2] = toUpper[a[i-2]]; a[i-1] = toUpper[a[i-1]]; a[i] = '-';
80 b[i-3] = toUpper[b[i-2]]; b[i-2] = toUpper[b[i-1]]; b[i-1] = toUpper[b[i]]; b[i] = '-';
81 fixed++;
82 redo = true;
83 }
84
85 if ((a[i-3] == '-') && (b[i] == '-')) {
86 a[i-3] = toUpper[a[i-2]]; a[i-2] = toUpper[a[i-1]]; a[i-1] = toUpper[a[i]]; a[i] = '-';
87 b[i-3] = toUpper[b[i-3]]; b[i-2] = toUpper[b[i-2]]; b[i-1] = toUpper[b[i-1]]; b[i] = '-';
88 fixed++;
89 redo = true;
90 }
91 }
92
93 if (redo) {
94 //fprintf(stdout, "%s\n%s\n", a, b);
95 //fprintf(stdout, "Fixed "uint32FMT" 2 base wide indel\n", fixed);
96 indelRedo(a, b);
97 }
98
99 return(fixed);
100 }
101
102
103
104 int
105 main(int argc, char **argv) {
106 char *nickname1 = 0L, *asmfile1 = 0L;
107 char *nickname2 = 0L, *asmfile2 = 0L;
108 bool flip = false;
109
110 int arg = 1;
111 while (arg < argc) {
112 if (strncmp(argv[arg], "-1", 2) == 0) {
113 nickname1 = argv[++arg];
114 asmfile1 = argv[++arg];
115 } else if (strncmp(argv[arg], "-2", 2) == 0) {
116 nickname2 = argv[++arg];
117 asmfile2 = argv[++arg];
118 } else if (strncmp(argv[arg], "-f", 2) == 0) {
119 flip = true;
120 } else {
121 fprintf(stderr, "Unknown arg '%s'\n", argv[arg]);
122 }
123 arg++;
124 }
125
126 if ((nickname1 == 0L) || (nickname2 == 0L)) {
127 fprintf(stderr, "usage: %s [-f] -1 nickname1 asmfile1 -2 nickname2 asmfile2 < matches.sim4db > matches.atac\n", argv[0]);
128 exit(1);
129 }
130
131 if (flip == false) {
132 fprintf(stdout, "!format atac 1.0\n");
133 fprintf(stdout, "/assemblyFile1=%s\n", asmfile1);
134 fprintf(stdout, "/assemblyFile2=%s\n", asmfile2);
135 fprintf(stdout, "/assemblyId1=%s\n", nickname1);
136 fprintf(stdout, "/assemblyId2=%s\n", nickname2);
137 } else {
138 fprintf(stdout, "!format atac 1.0\n");
139 fprintf(stdout, "/assemblyFile1=%s\n", asmfile2);
140 fprintf(stdout, "/assemblyFile2=%s\n", asmfile1);
141 fprintf(stdout, "/assemblyId1=%s\n", nickname2);
142 fprintf(stdout, "/assemblyId2=%s\n", nickname1);
143 }
144
145 uint32 dupRecordIID = 0;
146 uint32 dupParentIID = 0;
147
148 uint32 totalFixed = 0;
149
150 sim4polishReader *R = new sim4polishReader("-");
151 sim4polish *p = 0L;
152 while (R->nextAlignment(p)) {
153
154 // Parse the defline to find the genomic region our 'est'
155 // (unfortunate sim4db term) is from. Search for our
156 // information in the defline
157 //
158 // extracted from iid (\d+) pos (\d+) (\d+)
159
160 splitToWords W(p->_estDefLine);
161
162 uint32 i=0;
163 while ((i < W.numWords()) && (strcmp(W[i], "iid") != 0))
164 i++;
165 if ((i == 0) || (i == W.numWords()))
166 fprintf(stderr, "Failed to match est defline '%s'\n", p->_estDefLine), exit(1);
167
168 uint32 qSeqIID = strtouint32(W[i+1], 0L);
169 uint32 qSeqBeg = strtouint32(W[i+3], 0L);
170 uint32 qSeqEnd = strtouint32(W[i+4], 0L); // Not used
171
172
173 W.split(p->_genDefLine);
174
175 i=0;
176 while ((i<W.numWords()) && (strcmp(W[i], "iid") != 0))
177 i++;
178 if ((i == 0) || (i == W.numWords()))
179 fprintf(stderr, "Failed to match gen defline '%s'\n", p->_genDefLine), exit(1);
180
181 uint32 gSeqIID = strtouint32(W[i+1], 0L);
182 uint32 gSeqBeg = strtouint32(W[i+3], 0L);
183 //uint32 gSeqEnd = strtouint32(W[i+4], 0L); // Not used
184
185 bool fwd = (p->_matchOrientation == SIM4_MATCH_FORWARD);
186
187
188 // Fix the coords
189 //
190 if (fwd) {
191 // Forward is easy! Just add.
192
193 for (uint32 exon=0; exon<p->_numExons; exon++) {
194 sim4polishExon *e = p->_exons + exon;
195
196 e->_estFrom += qSeqBeg;
197 e->_estTo += qSeqBeg;
198 e->_genFrom += gSeqBeg;
199 e->_genTo += gSeqBeg;
200 }
201 } else {
202 // Reverse is not easy. Need to reverse complement the query positions.
203
204 for (uint32 exon=0; exon<p->_numExons; exon++) {
205 sim4polishExon *e = p->_exons + exon;
206
207 // First, reverse the query relative to our extracted piece
208 //
209 uint32 f = (qSeqEnd - qSeqBeg) - e->_estTo + 2; // Extra +1 to offset -1 when we set qBeg
210 uint32 t = (qSeqEnd - qSeqBeg) - e->_estFrom + 2;
211
212 // Now we can just offset stuff.
213 e->_estFrom = qSeqBeg + t; // Really the end!
214 e->_estTo = qSeqBeg + f; // Really the begin!
215 e->_genFrom += gSeqBeg;
216 e->_genTo += gSeqBeg;
217 }
218 }
219
220
221
222 for (uint32 exon=0; exon<p->_numExons; exon++) {
223 sim4polishExon *e = p->_exons + exon;
224
225 // Parse the alignment to find ungapped blocks
226
227 uint32 aPos = 0;
228
229 uint32 qBeg = e->_estFrom - 1;
230 uint32 gBeg = e->_genFrom - 1;
231
232 uint32 mLen = 0;
233
234 totalFixed += indelFixAlignment(e->_estAlignment, e->_genAlignment);
235
236 // Skip mismatches/gaps at the start of this sequence
237 //
238 while ((e->_estAlignment[aPos] == '-') ||
239 (e->_genAlignment[aPos] == '-') ||
240 (e->_estAlignment[aPos] != e->_genAlignment[aPos])) {
241 if (e->_estAlignment[aPos] != '-')
242 if (fwd) qBeg++;
243 else qBeg--;
244 if (e->_genAlignment[aPos] != '-')
245 gBeg++;
246 //fprintf(stderr, "SKIP BEGIN %c %c\n", e->_estAlignment[aPos], e->_genAlignment[aPos]);
247 aPos++;
248 }
249
250
251 bool notDone = true; // There should be a way to get rid of this stupid variable....
252 while (notDone) {
253 notDone = ((e->_estAlignment[aPos] != 0) &&
254 (e->_genAlignment[aPos] != 0));
255
256 // If we find the end of a gapless block, emit a match
257
258 if ((e->_estAlignment[aPos] == '-') || (e->_estAlignment[aPos] == 0) ||
259 (e->_genAlignment[aPos] == '-') || (e->_genAlignment[aPos] == 0)) {
260
261 // Trim off any mismatches at the end of this block.
262 //
263 uint32 mismatch = 0;
264 while ((aPos > mismatch) &&
265 (e->_estAlignment[aPos - mismatch - 1] != e->_genAlignment[aPos - mismatch - 1])) {
266 //fprintf(stderr, "SKIP MIDDLE %c %c\n", e->_estAlignment[aPos-mismatch], e->_genAlignment[aPos-mismatch]);
267 mismatch++;
268 }
269
270 // If there is an indel at the start (which probably
271 // shouldn't happen anyway!), or possibly at the end,
272 // then our length is zero, and we should not emit
273 // anything.
274 //
275 if (mLen > mismatch) {
276 mLen -= mismatch;
277
278 if (flip == false) {
279 fprintf(stdout, "M u dupr"uint32FMT" dupp"uint32FMT" %s:"uint32FMT" "uint32FMT" "uint32FMT" 1 %s:"uint32FMT" "uint32FMT" "uint32FMT" %s\n",
280 dupRecordIID,
281 dupParentIID,
282 nickname1, qSeqIID, (fwd) ? qBeg : qBeg - mLen, mLen,
283 nickname2, gSeqIID, gBeg, mLen,
284 (fwd) ? "1" : "-1");
285 } else {
286 fprintf(stdout, "M u dupr"uint32FMT" dupp"uint32FMT" %s:"uint32FMT" "uint32FMT" "uint32FMT" 1 %s:"uint32FMT" "uint32FMT" "uint32FMT" %s\n",
287 dupRecordIID,
288 dupParentIID,
289 nickname2, gSeqIID, gBeg, mLen,
290 nickname1, qSeqIID, (fwd) ? qBeg : qBeg - mLen, mLen,
291 (fwd) ? "1" : "-1");
292 }
293 dupRecordIID++;
294
295 mLen += mismatch;
296
297 // Adjust our begin and end positions to the end of this record
298 if (fwd) qBeg += mLen;
299 else qBeg -= mLen;
300 gBeg += mLen;
301
302 mLen = 0;
303 }
304
305 // Skip whatever caused us to emit a gapless block, also skip any mismatches here
306 //
307 while ((e->_estAlignment[aPos] == '-') ||
308 (e->_genAlignment[aPos] == '-') ||
309 (e->_estAlignment[aPos] != e->_genAlignment[aPos])) {
310 if (e->_estAlignment[aPos] != '-')
311 if (fwd) qBeg++;
312 else qBeg--;
313 if (e->_genAlignment[aPos] != '-')
314 gBeg++;
315 //fprintf(stderr, "SKIP END %c %c\n", e->_estAlignment[aPos], e->_genAlignment[aPos]);
316 aPos++;
317 }
318 } else {
319 // Not the end of a gapless block, extend this match by one
320 mLen++;
321 aPos++;
322 }
323
324 } // over all positions in the alignemnt
325 } // over all exons
326
327 dupParentIID++;
328 }
329
330 fprintf(stderr, "Fixed "uint32FMT" indel/mismatches.\n", totalFixed);
331
332 return(0);
333 }
334