Debian Codegraph @

atac-driver/gapShifter/extractUnmapped.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
28 //
29 // Tested to work on ../../atac/B36LCvsHUREF6A/B36LCvsHUREF6A.gapsFixed.atac
30 //
31
32
33
34 // Reads a set of matches and outputs two sequence files containing sequence that
35 // is not matched.
36
37
38 void
39 writeGaplessSequence(FILE *output,
40 seqInCore *S,
41 uint32 beg,
42 uint32 end,
43 uint32 extend,
44 atacMatch *l,
45 atacMatch *r) {
46 char *s = S->sequence();
47
48
49 // Skip any N's starting where we are currently
50 //
51 while ((beg < end) &&
52 (toUpper[(int)s[beg]] == 'N'))
53 beg++;
54
55 while ((beg < end) &&
56 (toUpper[(int)s[end-1]] == 'N'))
57 end--;
58
59 if (beg >= end)
60 return;
61
62 // Extend the ends up to 'extend' positions, as long as we don't
63 // hit a gap.
64 //
65 for (uint32 x=0; ((x < extend) &&
66 (beg > 0) &&
67 (toUpper[(int)s[beg-1]] != 'N')); x++)
68 beg--;
69
70 for (uint32 x=0; ((x < extend) &&
71 (end < S->sequenceLength()) &&
72 (toUpper[(int)s[end]] != 'N')); x++)
73 end++;
74
75 // Just make sure we're still in bounds!
76 if (end > S->sequenceLength())
77 end = S->sequenceLength();
78
79 // Over the whole sequence
80 //
81 while (beg < end) {
82
83 // Skip any N's starting where we are currently
84 //
85 while ((beg < end) &&
86 (toUpper[(int)s[beg]] == 'N'))
87 beg++;
88
89 // Move our current up to here
90 uint32 cur = beg;
91
92 // If we're at the end of the sequence, this block doesn't
93 // exist; it's solid N.
94 //
95 if (beg < end) {
96
97 // Move cur up to the next N
98 //
99 while ((cur < end) &&
100 (toUpper[(int)s[cur]] != 'N'))
101 cur++;
102
103 // And output whatever this block is
104 //
105 fprintf(output, "%s extracted from iid "uint32FMT" pos "uint32FMT" "uint32FMT" between match %s(%s) and %s(%s)\n",
106 S->header(), S->getIID(), beg, cur,
107 (l) ? l->matchuid : "none",
108 (l) ? l->parentuid : "none",
109 (r) ? r->matchuid : "none",
110 (r) ? r->parentuid : "none");
111
112 fwrite(S->sequence() + beg, sizeof(char), cur-beg, output);
113 fprintf(output, "\n");
114
115 }
116
117 // Move to the next block.
118 beg = cur;
119 }
120 }
121
122
123
124
125
126 // COPIED from libatac/matchList.C, but need to dereference the atacMatch again.
127 //
128 static
129 int
130 sort1_(const void *a, const void *b) {
131 const atacMatch *A = *(const atacMatch * const *)a;
132 const atacMatch *B = *(const atacMatch * const *)b;
133
134 if (A->iid1 < B->iid1) return(-1);
135 if (A->iid1 > B->iid1) return(1);
136 if (A->pos1 < B->pos1) return(-1);
137 if (A->pos1 > B->pos1) return(1);
138 if (A->len1 > B->len1) return(-1);
139 if (A->len1 < B->len1) return(1);
140 if (A->iid2 < B->iid2) return(-1);
141 if (A->iid2 > B->iid2) return(1);
142 if (A->pos2 < B->pos2) return(-1);
143 if (A->pos2 > B->pos2) return(1);
144 if (A->len2 > B->len2) return(-1);
145 if (A->len2 < B->len2) return(1);
146 return(0);
147 }
148
149 static
150 int
151 sort2_(const void *a, const void *b) {
152 const atacMatch *A = *(const atacMatch * const *)a;
153 const atacMatch *B = *(const atacMatch * const *)b;
154
155 if (A->iid2 < B->iid2) return(-1);
156 if (A->iid2 > B->iid2) return(1);
157 if (A->pos2 < B->pos2) return(-1);
158 if (A->pos2 > B->pos2) return(1);
159 if (A->len2 > B->len2) return(-1);
160 if (A->len2 < B->len2) return(1);
161 if (A->iid1 < B->iid1) return(-1);
162 if (A->iid1 > B->iid1) return(1);
163 if (A->pos1 < B->pos1) return(-1);
164 if (A->pos1 > B->pos1) return(1);
165 if (A->len1 > B->len1) return(-1);
166 if (A->len1 < B->len1) return(1);
167
168 return(0);
169 }
170
171
172
173 // New method, uses an intervalList to find the unmapped regions for
174 // each sequence.
175 //
176 class extractMatchList {
177
178 public:
179 extractMatchList() {
180 matchesLen = 0;
181 matchesMax = 16;
182 matches = new atacMatch * [matchesMax];
183 };
184 ~extractMatchList() {
185 delete [] matches;
186 };
187
188 atacMatch *operator[](uint32 idx) {
189 return(matches[idx]);
190 };
191
192 uint32 len(void) {
193 return(matchesLen);
194 };
195
196 void add(atacMatch *m) {
197 if (matchesLen >= matchesMax) {
198 matchesMax *= 2;
199 atacMatch **M = new atacMatch * [matchesMax];
200 memcpy(M, matches, sizeof(atacMatch *) * matchesLen);
201 delete [] matches;
202 matches = M;
203 }
204 matches[matchesLen++] = m;
205 };
206
207 void sort1(void) {
208 qsort(matches, matchesLen, sizeof(atacMatch*), sort1_);
209 };
210 void sort2(void) {
211 qsort(matches, matchesLen, sizeof(atacMatch*), sort2_);
212 };
213
214 private:
215 atacMatch **matches;
216 uint32 matchesLen;
217 uint32 matchesMax;
218 };
219
220
221
222
223 void
224 extractUnmapped(seqCache *A, seqCache *B,
225 FILE *Aoutput, FILE *Boutput,
226 uint32 extend,
227 atacFile &AF,
228 atacMatchList &ML) {
229 uint32 numSeqsA = AF.fastaA()->getNumberOfSequences();
230 uint32 numSeqsB = AF.fastaB()->getNumberOfSequences();
231
232 extractMatchList *coveredA = new extractMatchList [numSeqsA];
233 extractMatchList *coveredB = new extractMatchList [numSeqsB];
234
235 // Populate the intervals with the mapping
236 //
237 for (uint32 x=0; x<ML.numMatches(); x++) {
238 atacMatch *m = ML[x];
239
240 coveredA[m->iid1].add(m);
241 coveredB[m->iid2].add(m);
242 }
243
244 // Sort the intervals, manually invert the interval -- remembering
245 // what matches are where.
246 //
247 for (uint32 seq=0; seq<numSeqsA; seq++) {
248 coveredA[seq].sort1();
249
250 //ML.fastaA()->find(seq);
251 //seqInCore *S = ML.fastaA()->getSequenceInCore();
252
253 seqInCore *S = A->getSequenceInCore(seq);
254
255 if (coveredA[seq].len() == 0) {
256 // Hey! This sequence has NO matches at all!
257 //
258 writeGaplessSequence(Aoutput,
259 S,
260 0,
261 AF.fastaA()->getSequenceLength(seq),
262 extend,
263 0L, 0L);
264 } else {
265 if (0 < coveredA[seq][0]->pos1) {
266 writeGaplessSequence(Aoutput,
267 S,
268 0,
269 coveredA[seq][0]->pos1,
270 extend,
271 0L, coveredA[seq][0]);
272 }
273
274 for (uint32 i=1; i<coveredA[seq].len(); i++) {
275 if (coveredA[seq][i-1]->pos1 + coveredA[seq][i-1]->len1 < coveredA[seq][i]->pos1) {
276 writeGaplessSequence(Aoutput,
277 S,
278 coveredA[seq][i-1]->pos1 + coveredA[seq][i-1]->len1,
279 coveredA[seq][i]->pos1,
280 extend,
281 coveredA[seq][i-1], coveredA[seq][i]);
282 }
283 }
284
285 uint32 last = coveredA[seq].len()-1;
286 if (coveredA[seq][last]->pos1) {
287 writeGaplessSequence(Aoutput,
288 S,
289 coveredA[seq][last]->pos1 + coveredA[seq][last]->len1,
290 AF.fastaA()->getSequenceLength(seq),
291 extend,
292 coveredA[seq][0], 0L);
293 }
294 }
295 }
296
297
298
299 // DUPLICATION OF THE ABOVE! (Replace 1 with 2, A with B)
300
301
302 // Sort the intervals, manually invert the interval -- remembering
303 // what matches are where.
304 //
305 for (uint32 seq=0; seq<numSeqsB; seq++) {
306 coveredB[seq].sort2();
307
308 seqInCore *S = B->getSequenceInCore(seq);
309
310 if (coveredB[seq].len() == 0) {
311 // Hey! This sequence has NO matches at all!
312 //
313 writeGaplessSequence(Boutput,
314 S,
315 0,
316 AF.fastaB()->getSequenceLength(seq),
317 extend,
318 0L, 0L);
319 } else {
320 if (0 < coveredB[seq][0]->pos2) {
321 writeGaplessSequence(Boutput,
322 S,
323 0,
324 coveredB[seq][0]->pos2,
325 extend,
326 0L, coveredB[seq][0]);
327 }
328
329 for (uint32 i=1; i<coveredB[seq].len(); i++) {
330 if (coveredB[seq][i-1]->pos2 + coveredB[seq][i-1]->len2 < coveredB[seq][i]->pos2) {
331 writeGaplessSequence(Boutput,
332 S,
333 coveredB[seq][i-1]->pos2 + coveredB[seq][i-1]->len2,
334 coveredB[seq][i]->pos2,
335 extend,
336 coveredB[seq][i-1], coveredB[seq][i]);
337 }
338 }
339
340 uint32 last = coveredB[seq].len()-1;
341 if (coveredB[seq][last]->pos2) {
342 writeGaplessSequence(Boutput,
343 S,
344 coveredB[seq][last]->pos2 + coveredB[seq][last]->len2,
345 AF.fastaB()->getSequenceLength(seq),
346 extend,
347 coveredB[seq][0], 0L);
348 }
349 }
350 }
351
352
353
354
355
356
357
358
359
360 }
361
362
363 void
364 extractUnmappedRuns(seqCache *A, seqCache *B,
365 FILE *ARoutput, FILE *BRoutput,
366 uint32 extend,
367 atacMatchList &ML) {
368 seqInCore *S1 = 0L;
369 seqInCore *S2 = 0L;
370
371 atacMatchOrder MO(ML);
372 MO.sortA();
373
374 for (uint32 i=1; i<MO.numMatches(); i++) {
375 atacMatch *l = MO[i-1];
376 atacMatch *r = MO[i];
377
378 if (l->iid1 != r->iid1)
379 continue;
380 if (l->iid2 != r->iid2)
381 continue;
382
383 // Extract from (l->pos1 + l->len1) to (r->pos1), if it's longer than 20bp
384
385 bool lengthOK = true;
386 if (l->pos1 + l->len1 + 20 >= r->pos1)
387 lengthOK = false;
388 if ((l->fwd2 == true) && (l->pos2 + l->len2 + 20 >= r->pos2))
389 lengthOK = false;
390 if ((l->fwd2 == false) && (r->pos2 + r->len2 + 20 >= l->pos2))
391 lengthOK = false;
392
393 // Extract if our two matches are in the same run.
394 //
395 if ((lengthOK) &&
396 (strcmp(l->parentuid, r->parentuid) == 0)) {
397
398 #if 0
399 if (l->iid1 != S1->getIID()) {
400 delete S1;
401 W1->find(l->iid1);
402 S1 = W1->getSequenceInCore();
403 }
404
405 if (l->iid2 != S2->getIID()) {
406 delete S2;
407 W2->find(l->iid2);
408 S2 = W2->getSequenceInCore();
409 }
410 #else
411 S1 = A->getSequenceInCore(l->iid1);
412 S2 = B->getSequenceInCore(l->iid2);
413 #endif
414
415 writeGaplessSequence(ARoutput,
416 S1,
417 l->pos1 + l->len1,
418 r->pos1,
419 extend,
420 l, r);
421
422 // Need to deal with reverse matches here! In run matches
423 // should be the same orientation, but we'll still check.
424 //
425 if (l->fwd2 != r->fwd2) {
426 fprintf(stderr, "WOAH! Matches of different orientation in a run?!?\n");
427 exit(1);
428 }
429
430 if (l->fwd2) {
431 writeGaplessSequence(BRoutput,
432 S2,
433 l->pos2 + l->len2,
434 r->pos2,
435 extend,
436 l, r);
437 } else {
438 writeGaplessSequence(BRoutput,
439 S2,
440 r->pos2 + r->len2,
441 l->pos2,
442 extend,
443 l, r);
444 }
445 }
446 }
447 }
448
449
450
451
452
453
454
455
456 void
457 usage(char *name) {
458 fprintf(stderr, "usage: %s [-OP output.fasta] [-t trfile] -m matches\n", name);
459 fprintf(stderr, " OP\n");
460 fprintf(stderr, " -a extract all unmapped sequence in A\n");
461 fprintf(stderr, " -b extract all unmapped sequence in B\n");
462 fprintf(stderr, " -ar extract within run unmapped sequence in A\n");
463 fprintf(stderr, " -br extract within run unmapped sequence in B\n");
464 fprintf(stderr, " BOTH -ar and -br need to be specified!\n");
465 fprintf(stderr, "\n");
466 fprintf(stderr, " -t mask out tandem repeats listed in trfile\n");
467 }
468
469 FILE *
470 openOutputFile(char *name) {
471 errno = 0;
472 FILE *R = fopen(name, "w");
473 if (errno)
474 fprintf(stderr, "Failed to open '%s': %s\n", name, strerror(errno)), exit(1);
475 return(R);
476 }
477
478 int
479 main(int argc, char *argv[]) {
480 char *matchesFile = 0L;
481 FILE *Aoutput = 0L;
482 FILE *Boutput = 0L;
483 FILE *ARoutput = 0L;
484 FILE *BRoutput = 0L;
485 uint32 extend = 0;
486 char *trFile = 0L;
487
488 int arg=1;
489 while (arg < argc) {
490 if (strcmp(argv[arg], "-m") == 0) {
491 matchesFile = argv[++arg];
492 } else if (strcmp(argv[arg], "-a") == 0) {
493 Aoutput = openOutputFile(argv[++arg]);
494 } else if (strcmp(argv[arg], "-b") == 0) {
495 Boutput = openOutputFile(argv[++arg]);
496 } else if (strcmp(argv[arg], "-ar") == 0) {
497 ARoutput = openOutputFile(argv[++arg]);
498 } else if (strcmp(argv[arg], "-br") == 0) {
499 BRoutput = openOutputFile(argv[++arg]);
500 } else if (strcmp(argv[arg], "-e") == 0) {
501 extend = strtouint32(argv[++arg], 0L);
502 } else if (strcmp(argv[arg], "-t") == 0) {
503 trFile = argv[++arg];
504 } else {
505 usage(argv[0]);
506 exit(1);
507 }
508 arg++;
509 }
510
511 if (matchesFile == 0L)
512 usage(argv[0]), exit(1);
513
514 atacFile AF(matchesFile);
515 atacMatchList &ML = *AF.matches();
516
517 // Build caches for both sequences, then modify that sequence to
518 // mask out tandem repeats.
519 //
520 seqCache *A = new seqCache(AF.assemblyFileA(), 0, true);
521 seqCache *B = new seqCache(AF.assemblyFileB(), 0, true);
522
523 A->loadAllSequences();
524 B->loadAllSequences();
525
526 if (trFile) {
527 errno =0;
528 FILE *F = fopen(trFile, "r");
529 if (errno)
530 fprintf(stderr, "Error opening '%s': %s\n", trFile, strerror(errno));
531
532 char L[1024] = { 0 };
533 splitToWords W(L);
534
535 fprintf(stderr, "Masking repeats in '%s'\n", trFile);
536
537 uint32 statidx = 0;
538 uint32 stats[2] = { 0 };
539
540 while (!feof(F)) {
541 fgets(L, 1024, F);
542 W.split(L);
543
544 char source = W[0][0];
545 uint32 iid = strtouint32(W[1], 0L);
546 uint32 pos = strtouint32(W[2], 0L);
547 uint32 len = strtouint32(W[3], 0L);
548 bool fwd = (W[4][0] != '-');
549
550 seqInCore *S = 0L;
551 char *s = 0L;
552
553 if (source == 'B') {
554 S = A->getSequenceInCore(iid);
555 s = A->getSequenceInCore(iid)->sequence();
556 statidx = 0;
557 } else if (source == 'H') {
558 S = B->getSequenceInCore(iid);
559 s = B->getSequenceInCore(iid)->sequence();
560 statidx = 1;
561 } else {
562 fprintf(stderr, "Unknown source '%c'\n", source);
563 exit(1);
564 }
565
566 //fprintf(stderr, "Masking %c "uint32FMTW(8)" from "uint32FMTW(9)" to "uint32FMTW(9)" on strand %c\r",
567 // source, iid, pos, pos+len, (fwd) ? 'f' : 'r');
568
569 if (fwd) {
570 s += pos;
571 } else {
572 s += S->sequenceLength() - pos - len;
573 }
574
575 for (uint32 i=0; i<len; i++) {
576 if (toUpper[(int)s[i]] != 'N')
577 stats[statidx]++;
578 s[i] = 'N';
579 }
580 }
581 fclose(F);
582
583 fprintf(stderr, "Done masking. "uint32FMT" in A, "uint32FMT" in B.\n", stats[0], stats[1]);
584 }
585
586
587 if (Aoutput && Boutput) {
588 extractUnmapped(A, B, Aoutput, Boutput, extend, AF, ML);
589 fclose(Aoutput);
590 fclose(Boutput);
591 }
592
593 if (ARoutput && BRoutput) {
594 extractUnmappedRuns(A, B, ARoutput, BRoutput, extend, ML);
595 fclose(ARoutput);
596 fclose(BRoutput);
597 }
598
599 return(0);
600 }