Skip to content

Commit 519239d

Browse files
src/bam_record_utils.cpp: fixing some issues from cppcheck including failing to throw an exception that was constructed
1 parent 0f896af commit 519239d

File tree

1 file changed

+34
-32
lines changed

1 file changed

+34
-32
lines changed

src/bam_record_utils.cpp

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,8 @@ get_full_and_partial_ops(const uint32_t *cig_in, const uint32_t in_ops,
431431
* output of "A-" is "-T", and the output of "C-" is "-G", and so
432432
* forth. The user must handle this case separately.
433433
*/
434-
const uint8_t byte_revcom_table[] = {
434+
const uint8_t byte_revcomp_table[] = {
435+
// clang-format off
435436
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 136,
436437
72, 0, 40, 0, 0, 0, 24, 0, 0, 0, 0, 0, 0, 248, 4, 132, 68, 0,
437438
36, 0, 0, 0, 20, 0, 0, 0, 0, 0, 0, 244, 0, 0, 0, 0, 0, 0,
@@ -446,29 +447,31 @@ const uint8_t byte_revcom_table[] = {
446447
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
447448
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
448449
0, 0, 0, 0, 0, 0, 15, 143, 79, 0, 47, 0, 0, 0, 31, 0, 0, 0,
449-
0, 0, 0, 255};
450+
0, 0, 0, 255
451+
// clang-format on
452+
};
450453

451454
static inline void
452-
revcom_byte_then_reverse(unsigned char *a, unsigned char *b) {
453-
unsigned char *p1, *p2;
454-
for (p1 = a, p2 = b - 1; p2 > p1; ++p1, --p2) {
455-
*p1 = byte_revcom_table[*p1];
456-
*p2 = byte_revcom_table[*p2];
455+
revcomp_byte_then_reverse(unsigned char *const a, unsigned char *const b) {
456+
unsigned char *p1{a}, *p2{b};
457+
for (p2 -= 1; p2 > p1; ++p1, --p2) {
458+
*p1 = byte_revcomp_table[*p1];
459+
*p2 = byte_revcomp_table[*p2];
457460
*p1 ^= *p2;
458461
*p2 ^= *p1;
459462
*p1 ^= *p2;
460463
}
461464
if (p1 == p2)
462-
*p1 = byte_revcom_table[*p1];
465+
*p1 = byte_revcomp_table[*p1];
463466
}
464467

465468
static inline void
466-
revcomp_seq_by_byte(bam1_t *aln) {
469+
revcomp_seq_by_byte(bam1_t *const aln) {
467470
const size_t l_qseq = get_l_qseq(aln);
468471
auto seq = bam_get_seq(aln);
469-
const size_t num_bytes = ceil(l_qseq / 2.0);
472+
const size_t num_bytes = (l_qseq + 1) / 2; // integer ceil / 2
470473
auto seq_end = seq + num_bytes;
471-
revcom_byte_then_reverse(seq, seq_end);
474+
revcomp_byte_then_reverse(seq, seq_end);
472475
if (l_qseq % 2 == 1) { // for odd-length sequences
473476
for (size_t i = 0; i < num_bytes - 1; i++) {
474477
// swap 4-bit chunks within consecutive bytes like this:
@@ -486,7 +489,7 @@ revcomp_seq_by_byte(bam1_t *aln) {
486489
// has been set to ceil((a_used_len + b_seq_len) / 2.0) where
487490
// a_used_len = c_seq_len - b_seq_len
488491
static inline void
489-
merge_by_byte(const bam1_t *a, const bam1_t *b, bam1_t *c) {
492+
merge_by_byte(bam1_t const *const a, bam1_t const *const b, bam1_t *const c) {
490493
// ADS: (todo) need some functions for int_ceil and is_odd
491494
const size_t b_seq_len = get_l_qseq(b);
492495
const size_t c_seq_len = get_l_qseq(c);
@@ -496,8 +499,8 @@ merge_by_byte(const bam1_t *a, const bam1_t *b, bam1_t *c) {
496499
const bool is_b_odd = b_seq_len % 2 == 1;
497500
const bool is_c_odd = c_seq_len % 2 == 1;
498501

499-
const size_t a_num_bytes = ceil(a_used_len / 2.0);
500-
const size_t b_num_bytes = ceil(b_seq_len / 2.0);
502+
const size_t a_num_bytes = (a_used_len + 1) / 2; // integer ceil / 2
503+
const size_t b_num_bytes = (b_seq_len + 1) / 2; // integer ceil / 2
501504

502505
const size_t b_offset = is_a_odd && is_b_odd;
503506

@@ -511,25 +514,25 @@ merge_by_byte(const bam1_t *a, const bam1_t *b, bam1_t *c) {
511514
// or [ aa aa aa a- ]
512515
c_seq[a_num_bytes - 1] &= 0xf0;
513516
c_seq[a_num_bytes - 1] |=
514-
is_b_odd ? byte_revcom_table[b_seq[b_num_bytes - 1]]
515-
: byte_revcom_table[b_seq[b_num_bytes - 1]] >> 4;
517+
is_b_odd ? byte_revcomp_table[b_seq[b_num_bytes - 1]]
518+
: byte_revcomp_table[b_seq[b_num_bytes - 1]] >> 4;
516519
}
517520
if (is_c_odd) {
518521
// c_seq looks like either [ aa aa aa aa ]
519522
// or [ aa aa aa ab ]
520523
for (size_t i = 0; i < b_num_bytes - 1; i++) {
521524
c_seq[a_num_bytes + i] =
522-
(byte_revcom_table[b_seq[b_num_bytes - i - 1]] << 4) |
523-
(byte_revcom_table[b_seq[b_num_bytes - i - 2]] >> 4);
525+
(byte_revcomp_table[b_seq[b_num_bytes - i - 1]] << 4) |
526+
(byte_revcomp_table[b_seq[b_num_bytes - i - 2]] >> 4);
524527
}
525-
c_seq[a_num_bytes + b_num_bytes - 1] = byte_revcom_table[b_seq[0]] << 4;
528+
c_seq[a_num_bytes + b_num_bytes - 1] = byte_revcomp_table[b_seq[0]] << 4;
526529
// Here, c_seq is either [ aa aa aa aa bb bb bb b- ] (a even; b odd)
527530
// or [ aa aa aa ab bb bb bb b- ] (a odd; b odd)
528531
}
529532
else {
530533
for (size_t i = 0; i < b_num_bytes - b_offset; i++) {
531534
c_seq[a_num_bytes + i] =
532-
byte_revcom_table[b_seq[b_num_bytes - i - 1 - b_offset]];
535+
byte_revcomp_table[b_seq[b_num_bytes - i - 1 - b_offset]];
533536
}
534537
// Here, c_seq is either [ aa aa aa aa bb bb bb bb ] (a even and b even)
535538
// or [ aa aa aa ab bb bb bb ] (a odd and b odd)
@@ -554,16 +557,15 @@ flip_conversion(bam_rec &aln) {
554557
flip_conversion(aln.b);
555558
}
556559

557-
// static inline bool
558-
// are_mates(const bam1_t *one, const bam1_t *two) {
559-
// return one->core.mtid == two->core.tid && one->core.mpos == two->core.pos
560-
// &&
561-
// (one->core.flag & BAM_FREVERSE) != (one->core.flag & BAM_FREVERSE);
562-
// // below is a consistency check and should not be necessary
563-
// /* &&
564-
// two->core.mtid == one->core.tid &&
565-
// two->core.mpos == one->core.pos; */
566-
// }
560+
[[maybe_unused]] static inline bool
561+
are_mates(const bam1_t *one, const bam1_t *two) {
562+
return one->core.mtid == two->core.tid && one->core.mpos == two->core.pos &&
563+
(one->core.flag & BAM_FREVERSE) != (one->core.flag & BAM_FREVERSE);
564+
// below is a consistency check and should not be necessary
565+
/* &&
566+
two->core.mtid == one->core.tid &&
567+
two->core.mpos == one->core.pos; */
568+
}
567569

568570
static inline int
569571
truncate_overlap(const bam1_t *a, const uint32_t overlap, bam1_t *c) {
@@ -830,7 +832,7 @@ keep_better_end(const bam_rec &a, const bam_rec &b, bam_rec &c) {
830832
// ADS: will move to using this function once it is written
831833
static inline void
832834
standardize_format(const string &input_format, bam1_t *aln) {
833-
int err_code = 0;
835+
int err_code{};
834836

835837
if (input_format == "abismal" || input_format == "walt")
836838
return;
@@ -977,7 +979,7 @@ to_string(const bam_header &hdr, const bam_rec &aln) {
977979
kstring_t ks = {0, 0, nullptr};
978980
int ret = sam_format1(hdr.h, aln.b, &ks);
979981
if (ret < 0) {
980-
runtime_error("Can't format record: " + to_string(hdr, aln));
982+
throw runtime_error("Can't format record: " + to_string(hdr, aln));
981983
}
982984
if (ks.s != nullptr)
983985
free(ks.s);

0 commit comments

Comments
 (0)