Debian Codegraph @

bcftools/cigar_state.h

raw symbols SCIP
1 /* cigar_state.h -- API for efficient parsing of CIGAR strings
2
3 Copyright (C) 2022 Genome Research Ltd.
4
5 Author: pd3@sanger
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 #ifndef CIGAR_STATE_H
26 #define CIGAR_STATE_H
27
28 #include <stdint.h>
29 #include <htslib/hts.h>
30 #include <htslib/sam.h>
31
32 typedef struct
33 {
34 bam1_t *bam;
35 uint32_t *cigar;
36 uint8_t *seq;
37 int ncig;
38 int icig; // position in the cigar string
39 int iseq; // the cigar[icigar] operation refers to &seq[iseq]
40 hts_pos_t ref_pos; // reference coordinate, corresponds to iseq; points to
41 // the first base after the read when consumed
42 }
43 cigar_state_t;
44
45 static inline void cstate_init(cigar_state_t *cs, bam1_t *bam)
46 {
47 cs->bam = bam;
48 cs->cigar = bam_get_cigar(bam);
49 cs->seq = bam_get_seq(bam);
50 cs->ncig = bam->core.n_cigar;
51 cs->icig = 0;
52 cs->iseq = 0;
53 cs->ref_pos = bam->core.pos;
54 }
55
56 /**
57 * cstate_seek_fwd() - Move in the cigar forward to find query index that
58 * matches the reference position.
59 *
60 * When the position is not contained within the sequence, either because there
61 * is a deletion or there is no overlap, the behavior is controlled by the value
62 * of trim_left:
63 * - read starts after: qry_beg > pos && trim_left=1 .. returns 0 and sets pos to qry_beg
64 * - read starts after: qry_beg > pos && trim_left=0 .. returns -1
65 * - read ends before: qry_end < pos && trim_left=1 .. returns -2
66 * - read ends before: qry_end < pos && trim_left=0 .. returns qry_len-1 and sets pos to qry_end
67 * - pos inside a deletion && trim_left=1 .. returns position after the deletion
68 * - pos inside a deletion && trim_left=0 .. returns position before the deletion
69 */
70 static inline int cstate_seek_fwd(cigar_state_t *cs, hts_pos_t *pos_ptr, int trim_left)
71 {
72 hts_pos_t pos = *pos_ptr;
73 while ( cs->ref_pos <= pos )
74 {
75 if ( cs->icig >= cs->ncig ) // the read ends before pos
76 {
77 if ( trim_left ) return -2;
78 *pos_ptr = cs->ref_pos - 1;
79 return cs->iseq - 1;
80 }
81
82 int op = cs->cigar[cs->icig] & BAM_CIGAR_MASK;
83 int len = cs->cigar[cs->icig] >> BAM_CIGAR_SHIFT;
84 if ( op==BAM_CMATCH || op==BAM_CEQUAL || op==BAM_CDIFF )
85 {
86 if ( cs->ref_pos + len > pos ) return pos - cs->ref_pos + cs->iseq; // the cigar op overlaps pos
87 cs->ref_pos += len;
88 cs->iseq += len;
89 cs->icig++;
90 continue;
91 }
92 if ( op==BAM_CINS || op==BAM_CSOFT_CLIP )
93 {
94 cs->iseq += len;
95 cs->icig++;
96 continue;
97 }
98 if ( op==BAM_CDEL || op==BAM_CREF_SKIP )
99 {
100 if ( cs->ref_pos + len > pos )
101 {
102 // The deletion overlaps the position. NB: assuming del is never the first or last op
103 *pos_ptr = trim_left ? cs->ref_pos + len : cs->ref_pos - 1;
104 return trim_left ? cs->iseq : cs->iseq - 1;
105 }
106 cs->ref_pos += len;
107 cs->icig++;
108 continue;
109 }
110 if ( op==BAM_CHARD_CLIP || op==BAM_CPAD )
111 {
112 cs->icig++;
113 continue;
114 }
115 error("FIXME: not ready for CIGAR operator %d\n",op);
116 }
117 // the read starts after pos
118 if ( trim_left )
119 {
120 *pos_ptr = cs->bam->core.pos;
121 return 0;
122 }
123 return -1;
124 }
125
126
127 /**
128 * cstate_seek_op_fwd() - Move in the cigar forward to find query index that
129 * matches the seek operator and the reference position.
130 *
131 * In order to match a deletion, pass the position of the first deleted base.
132 * In order to match an insertion, pass the reference coordinate of the base
133 * after the inserted sequence.
134 *
135 * Returns the index to the query sequence cs->seq
136 * on success; -1 when there is no such matching position but the cigar
137 * is still not entirely consumed (e.g. a deletion or a soft-clip); -2
138 * when there is no overlap (i.e. the read ends before the position).
139 */
140 static inline int cstate_seek_op_fwd(cigar_state_t *cs, hts_pos_t pos, int seek_op, int *oplen)
141 {
142 while ( cs->ref_pos <= pos )
143 {
144 if ( cs->icig >= cs->ncig ) return -2;
145
146 int op = cs->cigar[cs->icig] & BAM_CIGAR_MASK;
147 int len = cs->cigar[cs->icig] >> BAM_CIGAR_SHIFT;
148 if ( op==BAM_CMATCH || op==BAM_CEQUAL || op==BAM_CDIFF )
149 {
150 if ( cs->ref_pos + len <= pos )
151 {
152 cs->ref_pos += len;
153 cs->iseq += len;
154 cs->icig++;
155 continue;
156 }
157 if ( seek_op==BAM_CMATCH ) return pos - cs->ref_pos + cs->iseq;
158 return -1;
159 }
160 if ( op==BAM_CINS || op==BAM_CSOFT_CLIP )
161 {
162 if ( cs->ref_pos == pos && seek_op==op )
163 {
164 if ( oplen ) *oplen = len;
165 return cs->iseq;
166 }
167 if ( cs->ref_pos >= pos ) return -1;
168 cs->iseq += len;
169 cs->icig++;
170 continue;
171 }
172 if ( op==BAM_CDEL || op==BAM_CREF_SKIP )
173 {
174 if ( cs->ref_pos == pos && seek_op==op )
175 {
176 if ( oplen ) *oplen = len;
177 return cs->iseq;
178 }
179 if ( cs->ref_pos >= pos ) return -1;
180 cs->ref_pos += len;
181 cs->icig++;
182 continue;
183 }
184 if ( op==BAM_CHARD_CLIP || op==BAM_CPAD )
185 {
186 cs->icig++;
187 continue;
188 }
189 error("FIXME: not ready for CIGAR operator %d\n",op);
190 }
191 return cs->icig < cs->ncig ? -1 : -2;
192 }
193
194 #endif