| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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; |
| 39 |
int iseq; |
| 40 |
hts_pos_t ref_pos; |
| 41 |
|
| 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 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 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 ) |
| 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; |
| 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 |
|
| 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 |
|
| 118 |
if ( trim_left ) |
| 119 |
{ |
| 120 |
*pos_ptr = cs->bam->core.pos; |
| 121 |
return 0; |
| 122 |
} |
| 123 |
return -1; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 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 |