Debian Codegraph @

samtools/bam_color.c

raw symbols SCIP
1 /* bam_color.c -- color-space support.
2
3 Copyright (C) 2009, 2012 University of California - Los Angeles.
4
5 Author: Nils Homer <[email protected]>
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 DEALINGS IN THE SOFTWARE. */
24
25 #include <config.h>
26
27 #include <ctype.h>
28 #include <string.h>
29
30 #include "htslib/sam.h"
31
32 /*!
33 @abstract Get the color encoding the previous and current base
34 @param b pointer to an alignment
35 @param i The i-th position, 0-based
36 @return color
37
38 @discussion Returns 0 no color information is found.
39 */
40 char bam_aux_getCSi(bam1_t *b, int i)
41 {
42 uint8_t *c = bam_aux_get(b, "CS");
43 char *cs = NULL;
44
45 // return the base if the tag was not found
46 if(0 == c) return 0;
47
48 cs = bam_aux2Z(c);
49 // adjust for strandedness and leading adaptor
50 if(bam_is_rev(b)) {
51 i = strlen(cs) - 1 - i;
52 // adjust for leading hard clip
53 uint32_t cigar = bam_get_cigar(b)[0];
54 if((cigar & BAM_CIGAR_MASK) == BAM_CHARD_CLIP) {
55 i -= cigar >> BAM_CIGAR_SHIFT;
56 }
57 } else { i++; }
58 return cs[i];
59 }
60
61 /*!
62 @abstract Get the color quality of the color encoding the previous and current base
63 @param b pointer to an alignment
64 @param i The i-th position, 0-based
65 @return color quality
66
67 @discussion Returns 0 no color information is found.
68 */
69 char bam_aux_getCQi(bam1_t *b, int i)
70 {
71 uint8_t *c = bam_aux_get(b, "CQ");
72 char *cq = NULL;
73
74 // return the base if the tag was not found
75 if(0 == c) return 0;
76
77 cq = bam_aux2Z(c);
78 // adjust for strandedness
79 if(bam_is_rev(b)) {
80 i = strlen(cq) - 1 - i;
81 // adjust for leading hard clip
82 uint32_t cigar = bam_get_cigar(b)[0];
83 if((cigar & BAM_CIGAR_MASK) == BAM_CHARD_CLIP) {
84 i -= (cigar >> BAM_CIGAR_SHIFT);
85 }
86 }
87 return cq[i];
88 }
89
90 char bam_aux_nt2int(char a)
91 {
92 switch(toupper(a)) {
93 case 'A':
94 return 0;
95 break;
96 case 'C':
97 return 1;
98 break;
99 case 'G':
100 return 2;
101 break;
102 case 'T':
103 return 3;
104 break;
105 default:
106 return 4;
107 break;
108 }
109 }
110
111 char bam_aux_ntnt2cs(char a, char b)
112 {
113 a = bam_aux_nt2int(a);
114 b = bam_aux_nt2int(b);
115 if(4 == a || 4 == b) return '4';
116 return "0123"[(int)(a ^ b)];
117 }
118
119 /*!
120 @abstract Get the color error profile at the give position
121 @param b pointer to an alignment
122 @return the original color if the color was an error, '-' (dash) otherwise
123
124 @discussion Returns 0 no color information is found.
125 */
126 char bam_aux_getCEi(bam1_t *b, int i)
127 {
128 int cs_i;
129 uint8_t *c = bam_aux_get(b, "CS");
130 char *cs = NULL;
131 char prev_b, cur_b;
132 char cur_color, cor_color;
133
134 // return the base if the tag was not found
135 if(0 == c) return 0;
136
137 cs = bam_aux2Z(c);
138
139 // adjust for strandedness and leading adaptor
140 if(bam_is_rev(b)) { //reverse strand
141 cs_i = strlen(cs) - 1 - i;
142 // adjust for leading hard clip
143 uint32_t cigar = bam_get_cigar(b)[0];
144 if((cigar & BAM_CIGAR_MASK) == BAM_CHARD_CLIP) {
145 cs_i -= cigar >> BAM_CIGAR_SHIFT;
146 }
147 // get current color
148 cur_color = cs[cs_i];
149 // get previous base. Note: must rc adaptor
150 prev_b = (cs_i == 1) ? "TGCAN"[(int)bam_aux_nt2int(cs[0])] : seq_nt16_str[bam_seqi(bam_get_seq(b), i+1)];
151 // get current base
152 cur_b = seq_nt16_str[bam_seqi(bam_get_seq(b), i)];
153 }
154 else {
155 cs_i=i+1;
156 // get current color
157 cur_color = cs[cs_i];
158 // get previous base
159 prev_b = (0 == i) ? cs[0] : seq_nt16_str[bam_seqi(bam_get_seq(b), i-1)];
160 // get current base
161 cur_b = seq_nt16_str[bam_seqi(bam_get_seq(b), i)];
162 }
163
164 // corrected color
165 cor_color = bam_aux_ntnt2cs(prev_b, cur_b);
166
167 if(cur_color == cor_color) {
168 return '-';
169 }
170 else {
171 return cur_color;
172 }
173 }