comparison PsiCLASS-1.0.2/samtools-0.1.19/bam_mate.c @ 0:903fc43d6227 draft default tip

Uploaded
author lsong10
date Fri, 26 Mar 2021 16:52:45 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:903fc43d6227
1 #include <stdlib.h>
2 #include <string.h>
3 #include <unistd.h>
4 #include "kstring.h"
5 #include "bam.h"
6
7 void bam_template_cigar(bam1_t *b1, bam1_t *b2, kstring_t *str)
8 {
9 bam1_t *swap;
10 int i, end;
11 uint32_t *cigar;
12 str->l = 0;
13 if (b1->core.tid != b2->core.tid || b1->core.tid < 0) return; // coordinateless or not on the same chr; skip
14 if (b1->core.pos > b2->core.pos) swap = b1, b1 = b2, b2 = swap; // make sure b1 has a smaller coordinate
15 kputc((b1->core.flag & BAM_FREAD1)? '1' : '2', str); // segment index
16 kputc((b1->core.flag & BAM_FREVERSE)? 'R' : 'F', str); // strand
17 for (i = 0, cigar = bam1_cigar(b1); i < b1->core.n_cigar; ++i) {
18 kputw(bam_cigar_oplen(cigar[i]), str);
19 kputc(bam_cigar_opchr(cigar[i]), str);
20 }
21 end = bam_calend(&b1->core, cigar);
22 kputw(b2->core.pos - end, str);
23 kputc('T', str);
24 kputc((b2->core.flag & BAM_FREAD1)? '1' : '2', str); // segment index
25 kputc((b2->core.flag & BAM_FREVERSE)? 'R' : 'F', str); // strand
26 for (i = 0, cigar = bam1_cigar(b2); i < b2->core.n_cigar; ++i) {
27 kputw(bam_cigar_oplen(cigar[i]), str);
28 kputc(bam_cigar_opchr(cigar[i]), str);
29 }
30 bam_aux_append(b1, "CT", 'Z', str->l+1, (uint8_t*)str->s);
31 }
32
33 // currently, this function ONLY works if each read has one hit
34 void bam_mating_core(bamFile in, bamFile out, int remove_reads)
35 {
36 bam_header_t *header;
37 bam1_t *b[2];
38 int curr, has_prev, pre_end = 0, cur_end;
39 kstring_t str;
40
41 str.l = str.m = 0; str.s = 0;
42 header = bam_header_read(in);
43 bam_header_write(out, header);
44
45 b[0] = bam_init1();
46 b[1] = bam_init1();
47 curr = 0; has_prev = 0;
48 while (bam_read1(in, b[curr]) >= 0) {
49 bam1_t *cur = b[curr], *pre = b[1-curr];
50 if (cur->core.tid < 0)
51 {
52 if ( !remove_reads ) bam_write1(out, cur);
53 continue;
54 }
55 cur_end = bam_calend(&cur->core, bam1_cigar(cur));
56 if (cur_end > (int)header->target_len[cur->core.tid]) cur->core.flag |= BAM_FUNMAP;
57 if (cur->core.flag & BAM_FSECONDARY)
58 {
59 if ( !remove_reads ) bam_write1(out, cur);
60 continue; // skip secondary alignments
61 }
62 if (has_prev) {
63 if (strcmp(bam1_qname(cur), bam1_qname(pre)) == 0) { // identical pair name
64 cur->core.mtid = pre->core.tid; cur->core.mpos = pre->core.pos;
65 pre->core.mtid = cur->core.tid; pre->core.mpos = cur->core.pos;
66 if (pre->core.tid == cur->core.tid && !(cur->core.flag&(BAM_FUNMAP|BAM_FMUNMAP))
67 && !(pre->core.flag&(BAM_FUNMAP|BAM_FMUNMAP))) // set TLEN/ISIZE
68 {
69 uint32_t cur5, pre5;
70 cur5 = (cur->core.flag&BAM_FREVERSE)? cur_end : cur->core.pos;
71 pre5 = (pre->core.flag&BAM_FREVERSE)? pre_end : pre->core.pos;
72 cur->core.isize = pre5 - cur5; pre->core.isize = cur5 - pre5;
73 } else cur->core.isize = pre->core.isize = 0;
74 if (pre->core.flag&BAM_FREVERSE) cur->core.flag |= BAM_FMREVERSE;
75 else cur->core.flag &= ~BAM_FMREVERSE;
76 if (cur->core.flag&BAM_FREVERSE) pre->core.flag |= BAM_FMREVERSE;
77 else pre->core.flag &= ~BAM_FMREVERSE;
78 if (cur->core.flag & BAM_FUNMAP) { pre->core.flag |= BAM_FMUNMAP; pre->core.flag &= ~BAM_FPROPER_PAIR; }
79 if (pre->core.flag & BAM_FUNMAP) { cur->core.flag |= BAM_FMUNMAP; cur->core.flag &= ~BAM_FPROPER_PAIR; }
80 bam_template_cigar(pre, cur, &str);
81 bam_write1(out, pre);
82 bam_write1(out, cur);
83 has_prev = 0;
84 } else { // unpaired or singleton
85 pre->core.mtid = -1; pre->core.mpos = -1; pre->core.isize = 0;
86 if (pre->core.flag & BAM_FPAIRED) {
87 pre->core.flag |= BAM_FMUNMAP;
88 pre->core.flag &= ~BAM_FMREVERSE & ~BAM_FPROPER_PAIR;
89 }
90 bam_write1(out, pre);
91 }
92 } else has_prev = 1;
93 curr = 1 - curr;
94 pre_end = cur_end;
95 }
96 if (has_prev) bam_write1(out, b[1-curr]);
97 bam_header_destroy(header);
98 bam_destroy1(b[0]);
99 bam_destroy1(b[1]);
100 free(str.s);
101 }
102
103 void usage()
104 {
105 fprintf(stderr,"Usage: samtools fixmate <in.nameSrt.bam> <out.nameSrt.bam>\n");
106 fprintf(stderr,"Options:\n");
107 fprintf(stderr," -r remove unmapped reads and secondary alignments\n");
108 exit(1);
109 }
110
111 int bam_mating(int argc, char *argv[])
112 {
113 bamFile in, out;
114 int c, remove_reads=0;
115 while ((c = getopt(argc, argv, "r")) >= 0) {
116 switch (c) {
117 case 'r': remove_reads=1; break;
118 }
119 }
120 if (optind+1 >= argc) usage();
121 in = (strcmp(argv[optind], "-") == 0)? bam_dopen(fileno(stdin), "r") : bam_open(argv[optind], "r");
122 out = (strcmp(argv[optind+1], "-") == 0)? bam_dopen(fileno(stdout), "w") : bam_open(argv[optind+1], "w");
123 bam_mating_core(in, out, remove_reads);
124 bam_close(in); bam_close(out);
125 return 0;
126 }
127
128