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

Uploaded
author lsong10
date Fri, 26 Mar 2021 16:52:45 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1 #include <ctype.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
2 #include "bam.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
3
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
4 /*!
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
5 @abstract Get the color encoding the previous and current base
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
6 @param b pointer to an alignment
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
7 @param i The i-th position, 0-based
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
8 @return color
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
9
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
10 @discussion Returns 0 no color information is found.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
11 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
12 char bam_aux_getCSi(bam1_t *b, int i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
13 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
14 uint8_t *c = bam_aux_get(b, "CS");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
15 char *cs = NULL;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
16
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
17 // return the base if the tag was not found
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
18 if(0 == c) return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
19
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
20 cs = bam_aux2Z(c);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
21 // adjust for strandedness and leading adaptor
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
22 if(bam1_strand(b)) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
23 i = strlen(cs) - 1 - i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
24 // adjust for leading hard clip
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
25 uint32_t cigar = bam1_cigar(b)[0];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
26 if((cigar & BAM_CIGAR_MASK) == BAM_CHARD_CLIP) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
27 i -= cigar >> BAM_CIGAR_SHIFT;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
28 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
29 } else { i++; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
30 return cs[i];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
31 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
32
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
33 /*!
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
34 @abstract Get the color quality of the color encoding the previous and current base
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
35 @param b pointer to an alignment
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
36 @param i The i-th position, 0-based
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
37 @return color quality
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
38
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
39 @discussion Returns 0 no color information is found.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
40 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
41 char bam_aux_getCQi(bam1_t *b, int i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
42 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
43 uint8_t *c = bam_aux_get(b, "CQ");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
44 char *cq = NULL;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
45
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
46 // return the base if the tag was not found
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
47 if(0 == c) return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
48
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
49 cq = bam_aux2Z(c);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
50 // adjust for strandedness
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
51 if(bam1_strand(b)) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
52 i = strlen(cq) - 1 - i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
53 // adjust for leading hard clip
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
54 uint32_t cigar = bam1_cigar(b)[0];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
55 if((cigar & BAM_CIGAR_MASK) == BAM_CHARD_CLIP) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
56 i -= (cigar >> BAM_CIGAR_SHIFT);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
57 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
58 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
59 return cq[i];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
60 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
61
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
62 char bam_aux_nt2int(char a)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
63 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
64 switch(toupper(a)) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
65 case 'A':
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
66 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
67 break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
68 case 'C':
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
69 return 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
70 break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
71 case 'G':
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
72 return 2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
73 break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
74 case 'T':
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
75 return 3;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
76 break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
77 default:
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
78 return 4;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
79 break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
80 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
81 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
82
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
83 char bam_aux_ntnt2cs(char a, char b)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
84 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
85 a = bam_aux_nt2int(a);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
86 b = bam_aux_nt2int(b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
87 if(4 == a || 4 == b) return '4';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
88 return "0123"[(int)(a ^ b)];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
89 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
90
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
91 /*!
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
92 @abstract Get the color error profile at the give position
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
93 @param b pointer to an alignment
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
94 @return the original color if the color was an error, '-' (dash) otherwise
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
95
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
96 @discussion Returns 0 no color information is found.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
97 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
98 char bam_aux_getCEi(bam1_t *b, int i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
99 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
100 int cs_i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
101 uint8_t *c = bam_aux_get(b, "CS");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
102 char *cs = NULL;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
103 char prev_b, cur_b;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
104 char cur_color, cor_color;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
105
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
106 // return the base if the tag was not found
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
107 if(0 == c) return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
108
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
109 cs = bam_aux2Z(c);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
110
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
111 // adjust for strandedness and leading adaptor
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
112 if(bam1_strand(b)) { //reverse strand
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
113 cs_i = strlen(cs) - 1 - i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
114 // adjust for leading hard clip
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
115 uint32_t cigar = bam1_cigar(b)[0];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
116 if((cigar & BAM_CIGAR_MASK) == BAM_CHARD_CLIP) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
117 cs_i -= cigar >> BAM_CIGAR_SHIFT;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
118 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
119 // get current color
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
120 cur_color = cs[cs_i];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
121 // get previous base. Note: must rc adaptor
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
122 prev_b = (cs_i == 1) ? "TGCAN"[(int)bam_aux_nt2int(cs[0])] : bam_nt16_rev_table[bam1_seqi(bam1_seq(b), i+1)];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
123 // get current base
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
124 cur_b = bam_nt16_rev_table[bam1_seqi(bam1_seq(b), i)];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
125 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
126 else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
127 cs_i=i+1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
128 // get current color
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
129 cur_color = cs[cs_i];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
130 // get previous base
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
131 prev_b = (0 == i) ? cs[0] : bam_nt16_rev_table[bam1_seqi(bam1_seq(b), i-1)];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
132 // get current base
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
133 cur_b = bam_nt16_rev_table[bam1_seqi(bam1_seq(b), i)];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
134 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
135
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
136 // corrected color
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
137 cor_color = bam_aux_ntnt2cs(prev_b, cur_b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
138
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
139 if(cur_color == cor_color) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
140 return '-';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
141 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
142 else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
143 return cur_color;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
144 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
145 }