Debian Codegraph @

samtools/bam_color.c.pysam.c

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