From 77f2374cecf52aeaea309eda148bef13ecce0872 Mon Sep 17 00:00:00 2001 From: Hao Hou Date: Thu, 7 Nov 2019 12:17:57 -0700 Subject: [PATCH] Fix issue #788 and add test cases --- src/utils/BamTools/include/BamAlignment.hpp | 6 ++--- test/bedtobam/chrsize.tmp | 1 + test/bedtobam/test-bedtobam.sh | 26 +++++++++++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 test/bedtobam/chrsize.tmp create mode 100644 test/bedtobam/test-bedtobam.sh diff --git a/src/utils/BamTools/include/BamAlignment.hpp b/src/utils/BamTools/include/BamAlignment.hpp index 96e8ebbef..c25cd008f 100644 --- a/src/utils/BamTools/include/BamAlignment.hpp +++ b/src/utils/BamTools/include/BamAlignment.hpp @@ -187,10 +187,10 @@ namespace BamTools { bool SyncExtraData() const { #define BAM_DATA_OFFSET(what) ((size_t)(((uint8_t*)bam_get_##what(&_bam)) - ((uint8_t*)_bam.data))) - void* qname_buf = _ensure_data_chunk((bam1_t*)&_bam, BAM_DATA_OFFSET(qname), _bam.core.l_qname, Name.size()); + void* qname_buf = _ensure_data_chunk((bam1_t*)&_bam, BAM_DATA_OFFSET(qname), _bam.core.l_qname, Name.size() + 1); if(NULL == qname_buf) return false; - memcpy(qname_buf, Name.c_str(), Name.size()); - ((bam1_t*)&_bam)->core.l_qname = Name.size(); + memcpy(qname_buf, Name.c_str(), Name.size() + 1); + ((bam1_t*)&_bam)->core.l_qname = Name.size() + 1; uint32_t* cigar_buf = (uint32_t*)_ensure_data_chunk((bam1_t*)&_bam, BAM_DATA_OFFSET(cigar), _bam.core.n_cigar * sizeof(uint32_t), sizeof(uint32_t) * CigarData.size()); if(NULL == cigar_buf) return false; diff --git a/test/bedtobam/chrsize.tmp b/test/bedtobam/chrsize.tmp new file mode 100644 index 000000000..d2e5cdd95 --- /dev/null +++ b/test/bedtobam/chrsize.tmp @@ -0,0 +1 @@ +1 3000 diff --git a/test/bedtobam/test-bedtobam.sh b/test/bedtobam/test-bedtobam.sh new file mode 100644 index 000000000..1003ff307 --- /dev/null +++ b/test/bedtobam/test-bedtobam.sh @@ -0,0 +1,26 @@ +set -e; +BT=${BT-../../bin/bedtools} + +FAILURES=0; + +check() +{ + if diff $1 $2; then + echo ok + else + FAILURES=$(expr $FAILURES + 1); + echo fail + fi +} + +################################################################## +# Test one block without -split +################################################################## +echo -e " bedtobam.t1...\c" +echo \ +"read_name 0 1 1001 255 1000M * 0 0 * *">exp +echo -e "1\t1000\t2000\tread_name\t255\t+" | $BT bedtobam -i - -g chrsize.tmp| samtools view > obs +check obs exp +rm obs exp + +[[ $FAILURES -eq 0 ]] || exit 1;