Skip to content

Commit

Permalink
Use hts_pos_t in tweak_overlap_quality() and related functions
Browse files Browse the repository at this point in the history
While not strictly necessary (the positions in question are
relative to that of read 'b') it makes data types consistent
and reduces the possibility of accidental overflow.

Also adds a check that the position in the sequence is valid
before trying to use it for array look-ups.
  • Loading branch information
daviesrob committed Sep 19, 2019
1 parent f44aac1 commit 9dc3992
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions sam.c
Original file line number Diff line number Diff line change
Expand Up @@ -4018,9 +4018,9 @@ void bam_plp_destructor(bam_plp_t plp,
* Returns BAM_CMATCH, -1 when there is no more cigar to process or the requested position is not covered,
* or -2 on error.
*/
static inline int cigar_iref2iseq_set(uint32_t **cigar, uint32_t *cigar_max, int *icig, int *iseq, hts_pos_t *iref)
static inline int cigar_iref2iseq_set(uint32_t **cigar, uint32_t *cigar_max, hts_pos_t *icig, hts_pos_t *iseq, hts_pos_t *iref)
{
int pos = *iref;
hts_pos_t pos = *iref;
if ( pos < 0 ) return -1;
*icig = 0;
*iseq = 0;
Expand Down Expand Up @@ -4053,7 +4053,7 @@ static inline int cigar_iref2iseq_set(uint32_t **cigar, uint32_t *cigar_max, int
*iseq = -1;
return -1;
}
static inline int cigar_iref2iseq_next(uint32_t **cigar, uint32_t *cigar_max, int *icig, int *iseq, hts_pos_t *iref)
static inline int cigar_iref2iseq_next(uint32_t **cigar, uint32_t *cigar_max, hts_pos_t *icig, hts_pos_t *iseq, hts_pos_t *iref)
{
while ( *cigar < cigar_max )
{
Expand Down Expand Up @@ -4082,8 +4082,8 @@ static int tweak_overlap_quality(bam1_t *a, bam1_t *b)
{
uint32_t *a_cigar = bam_get_cigar(a), *a_cigar_max = a_cigar + a->core.n_cigar;
uint32_t *b_cigar = bam_get_cigar(b), *b_cigar_max = b_cigar + b->core.n_cigar;
int a_icig = 0, a_iseq = 0;
int b_icig = 0, b_iseq = 0;
hts_pos_t a_icig = 0, a_iseq = 0;
hts_pos_t b_icig = 0, b_iseq = 0;
uint8_t *a_qual = bam_get_qual(a), *b_qual = bam_get_qual(b);
uint8_t *a_seq = bam_get_seq(a), *b_seq = bam_get_seq(b);

Expand Down Expand Up @@ -4117,6 +4117,9 @@ static int tweak_overlap_quality(bam1_t *a, bam1_t *b)
iref++;
if ( a_iref+a->core.pos != b_iref+b->core.pos ) continue; // only CMATCH positions, don't know what to do with indels

if (a_iseq > a->core.l_qseq || b_iseq > b->core.l_qseq)
return -1; // Fell off end of sequence, bad CIGAR?

if ( bam_seqi(a_seq,a_iseq) == bam_seqi(b_seq,b_iseq) )
{
#if DBG
Expand Down

0 comments on commit 9dc3992

Please sign in to comment.