From 9be41e572e5a983286c1520e713397dfa481df4f Mon Sep 17 00:00:00 2001
From: zhangxianting <zhangxianting@uniontech.com>
Date: Wed, 10 Apr 2024 14:38:30 +0800
Subject: [PATCH 2/2] fix build error of sam.h
	
	error: reference to 'byte' is ambiguous
		/usr/include/c++/12/cstddef:69:14: note: candidates are: 'enum class std::byte'
	error: 'samread' was not declared in this scope; did you mean 'sam_read1'?
	error: 'bam_header_t' was not declared in this scope; did you mean 'bam_hdr_t'?
	error: 'bam1_qname' was not declared in this scope; did you mean 'basename'?
	error: 'bam1_cigar' was not declared in this scope; did you mean 'bam_cigar_op'?
	error: 'samopen' was not declared in this scope; did you mean 'sam_open'?
	error: 'samclose' was not declared in this scope; did you mean 'sam_close'?
	error: '_hit_file' was not declared in this scope; did you mean 'htsFile'?

	src/GBase.h:
			rename byte to gbase_byte
	src/hits.cpp:
			sam_read1 instead of samread
			bam_hdr_t instead of bam_header_t
			bam_get_qname instead of bam1_qname
			bam_get_cigar instead of bam1_cigar
	src/hits.h:
			sam_open instead of samopen
			sam_close instead of samclose
			samFile instead of samfile_t
			fp.bgzf instead of x.bam

---
 src/GBase.h    |  2 +-
 src/codons.cpp |  8 ++++----
 src/gdna.cpp   | 12 ++++++------
 src/gdna.h     |  4 ++--
 src/gff.cpp    |  2 +-
 src/gff.h      | 11 +++++------
 src/hits.cpp   | 24 ++++++++++++------------
 src/hits.h     | 21 +++++++++++----------
 8 files changed, 42 insertions(+), 42 deletions(-)

diff --git a/src/GBase.h b/src/GBase.h
index fc3c5ba..1d31e9d 100644
--- a/src/GBase.h
+++ b/src/GBase.h
@@ -72,7 +72,7 @@ typedef int16_t int16;
 typedef uint16_t uint16;
 
 typedef unsigned char uchar;
-typedef unsigned char byte;
+typedef unsigned char gbase_byte;
 
 #ifndef MAXUINT
 #define MAXUINT ((unsigned int)-1)
diff --git a/src/codons.cpp b/src/codons.cpp
index a459250..4efe055 100644
--- a/src/codons.cpp
+++ b/src/codons.cpp
@@ -48,9 +48,9 @@ static bool isCodonTableReady=codonTableInit();
 
 unsigned short packCodon(char n1, char n2, char n3) {
  //assumes they are uppercase already!
- byte b1=n1-'A';
- byte b2=n2-'A';
- byte b3=n3-'A';
+ gbase_byte b1=n1-'A';
+ gbase_byte b2=n2-'A';
+ gbase_byte b3=n3-'A';
  b1 |= (b2 << 5); 
  b2 = (b2 >> 3) | (b3 << 2);
  return ( ((unsigned short)b2) << 8) + b1;
@@ -68,7 +68,7 @@ bool codonTableInit() {
 
 
 char Codon::translate() {
- for (byte i=0;i<3;i++) nuc[i]=toupper(nuc[i]);
+ for (gbase_byte i=0;i<3;i++) nuc[i]=toupper(nuc[i]);
  unsigned short aacode=packCodon(nuc[0], nuc[1], nuc[2]);
  return codonTable[aacode];
  }
diff --git a/src/gdna.cpp b/src/gdna.cpp
index 4d8a4b0..f7873be 100644
--- a/src/gdna.cpp
+++ b/src/gdna.cpp
@@ -11,8 +11,8 @@ const char* IUPAC_COMP  ="TtGgAaCcAaKkYyWwSsRrMmBbDdHhVvNnXx-*";
 #define G_2BIT 2 // 10
 #define T_2BIT 3 // 11
 
-static byte ntCompTable[256];
-static byte nt2bit[256]; //maps any character to a 2bit base value (with N = A)
+static gbase_byte ntCompTable[256];
+static gbase_byte nt2bit[256]; //maps any character to a 2bit base value (with N = A)
 static char v_2bit2nt[4] = {'A','C','G','T'};
 
 //----------------------
@@ -21,9 +21,9 @@ static bool gdna_Ready=gDnaInit();
 
 //----------------------
 
-byte gdna2bit(char* &nt, int n) {
-// Pack n bases into a byte (n can be 1..4)
-byte out = 0;
+gbase_byte gdna2bit(char* &nt, int n) {
+// Pack n bases into a gbase_byte (n can be 1..4)
+gbase_byte out = 0;
 while (n && *nt) {
     n--;
     out <<= 2;
@@ -43,7 +43,7 @@ char ntComplement(char c) {
  return ntCompTable[(int)c];
  }
 
-char g2bit2base(byte v2bit) {
+char g2bit2base(gbase_byte v2bit) {
  return v_2bit2nt[v2bit & 0x03 ];
 }
 
diff --git a/src/gdna.h b/src/gdna.h
index 1f923ed..6871a34 100644
--- a/src/gdna.h
+++ b/src/gdna.h
@@ -9,7 +9,7 @@ char* reverseComplement(char* seq, int slen=0);
 
 bool gDnaInit();
 
-byte gdna2bit(char* &nt, int n=4); //pack n bases into a byte (n can be 1..4)
-char g2bit2base(byte v2bit); //convert the 2-bit value into 'A', 'C', 'G' or 'T'
+gbase_byte gdna2bit(char* &nt, int n=4); //pack n bases into a gbase_byte (n can be 1..4)
+char g2bit2base(gbase_byte v2bit); //convert the 2-bit value into 'A', 'C', 'G' or 'T'
 
 #endif
diff --git a/src/gff.cpp b/src/gff.cpp
index 17103ff..1b7fa5b 100644
--- a/src/gff.cpp
+++ b/src/gff.cpp
@@ -21,7 +21,7 @@ const uint gfo_flag_BY_EXON          = 0x00000020; //created by subfeature (exon
 const uint gfo_flag_DISCARDED        = 0x00000100;
 const uint gfo_flag_LST_KEEP         = 0x00000200;
 const uint gfo_flag_LEVEL_MSK        = 0x00FF0000;
-const byte gfo_flagShift_LEVEL           = 16;
+const gbase_byte gfo_flagShift_LEVEL           = 16;
 
 void gffnames_ref(GffNames* &n) {
   if (n==NULL) n=new GffNames();
diff --git a/src/gff.h b/src/gff.h
index db2f87e..483b9d0 100644
--- a/src/gff.h
+++ b/src/gff.h
@@ -22,7 +22,6 @@ const byte exMskMinSpliceL = 0x04;
 const byte exMskMinSpliceR = 0x08;
 const byte exMskTag = 0x80;
 */
-
 //reserved Gffnames::feats entries -- basic feature types
 extern const int gff_fid_mRNA; // "mRNA" feature name
 extern const int gff_fid_transcript; // *RNA, *transcript feature name
@@ -43,7 +42,7 @@ extern const uint gfo_flag_DISCARDED; //should not be printed under the "transcr
 extern const uint gfo_flag_LST_KEEP; //GffObj from GffReader::gflst is to be kept (not deallocated)
                                      //when GffReader is destroyed
 extern const uint gfo_flag_LEVEL_MSK; //hierarchical level: 0 = no parent
-extern const byte gfo_flagShift_LEVEL;
+extern const gbase_byte gfo_flagShift_LEVEL;
 
 extern bool gff_show_warnings;
 
@@ -502,18 +501,18 @@ public:
     if (v) flags |= gfo_flag_CHILDREN_PROMOTED;
       else flags &= ~gfo_flag_CHILDREN_PROMOTED;
      }
-  void setLevel(byte v) {
+  void setLevel(gbase_byte v) {
     if (v==0) flags &= ~gfo_flag_LEVEL_MSK;
          else flags &= ~(((uint)v) << gfo_flagShift_LEVEL);
     }
-  byte incLevel() {
+  gbase_byte incLevel() {
     uint v=((flags & gfo_flag_LEVEL_MSK) >> gfo_flagShift_LEVEL);
     v++;
     flags &= ~(v << gfo_flagShift_LEVEL);
     return v;
     }
-  byte getLevel() {
-    return ((byte)((flags & gfo_flag_LEVEL_MSK) >> gfo_flagShift_LEVEL));
+  gbase_byte getLevel() {
+    return ((gbase_byte)((flags & gfo_flag_LEVEL_MSK) >> gfo_flagShift_LEVEL));
     }
 
   bool isValidTranscript() { 
diff --git a/src/hits.cpp b/src/hits.cpp
index 4cac1f1..068bb67 100644
--- a/src/hits.cpp
+++ b/src/hits.cpp
@@ -398,7 +398,7 @@ bool BAMHitFactory::next_record(const char*& buf, size_t& buf_size)
     
     memset(&_next_hit, 0, sizeof(_next_hit));
     
-	int bytes_read = samread(_hit_file, &_next_hit);
+	int bytes_read = sam_read1(_hit_file, _hit_file->bam_header, &_next_hit);
 	if (bytes_read < 0)
     {
         _eof_encountered = true;
@@ -470,7 +470,7 @@ bool BAMHitFactory::get_hit_from_buf(const char* orig_bwt_buf,
 	if (sam_flag & 0x4 || target_id < 0)
 	{
 		//assert(cigar.size() == 1 && cigar[0].opcode == MATCH);
-		bh = create_hit(bam1_qname(hit_buf),
+		bh = create_hit(bam_get_qname(hit_buf),
 						"*",
 						0, // SAM files are 1-indexed
 						0,
@@ -483,27 +483,27 @@ bool BAMHitFactory::get_hit_from_buf(const char* orig_bwt_buf,
                         sam_flag);
 		return true;
 	}
-	if (target_id >= _hit_file->header->n_targets)
+	if (target_id >= _hit_file->bam_header->n_targets)
     {
-        fprintf (stderr, "BAM error: file contains hits to sequences not in header SQ records (%s)\n", bam1_qname(hit_buf));
+        fprintf (stderr, "BAM error: file contains hits to sequences not in header SQ records (%s)\n", bam_get_qname(hit_buf));
         return false;
     }
     
-	string text_name = _hit_file->header->target_name[target_id];
+	string text_name = _hit_file->bam_header->target_name[target_id];
 	
 	for (int i = 0; i < hit_buf->core.n_cigar; ++i) 
 	{
 		//char* t;
 
-		int length = bam1_cigar(hit_buf)[i] >> BAM_CIGAR_SHIFT;
+		int length = bam_get_cigar(hit_buf)[i] >> BAM_CIGAR_SHIFT;
 		if (length <= 0)
 		{
-		  fprintf (stderr, "BAM error: CIGAR op has zero length (%s)\n", bam1_qname(hit_buf));
+		  fprintf (stderr, "BAM error: CIGAR op has zero length (%s)\n", bam_get_qname(hit_buf));
 			return false;
 		}
 		
 		CigarOpCode opcode;
-		switch(bam1_cigar(hit_buf)[i] & BAM_CIGAR_MASK)
+		switch(bam_get_cigar(hit_buf)[i] & BAM_CIGAR_MASK)
 		{
 			case BAM_CMATCH: opcode  = MATCH; break; 
 			case BAM_CINS: opcode  = INS; break;
@@ -533,7 +533,7 @@ bool BAMHitFactory::get_hit_from_buf(const char* orig_bwt_buf,
 	{
 		if (mate_target_id == target_id)
 		{
-			mrnm = _hit_file->header->target_name[mate_target_id];
+			mrnm = _hit_file->bam_header->target_name[mate_target_id];
 //			if (abs((int)text_mate_pos - (int)text_offset) > (int)max_intron_length)
 //			{
 //				//fprintf (stderr, "Mates are too distant, skipping\n");
@@ -593,7 +593,7 @@ bool BAMHitFactory::get_hit_from_buf(const char* orig_bwt_buf,
 		//assert(_rg_props.strandedness() == STRANDED_PROTOCOL || source_strand == CUFF_STRAND_UNKNOWN);
 
 		//assert(cigar.size() == 1 && cigar[0].opcode == MATCH);
-		bh = create_hit(bam1_qname(hit_buf),
+		bh = create_hit(bam_get_qname(hit_buf),
 						text_name,
 						text_offset,  // BAM files are 0-indexed
 						cigar,
@@ -614,7 +614,7 @@ bool BAMHitFactory::get_hit_from_buf(const char* orig_bwt_buf,
 			fprintf(stderr, "BAM record error: found spliced alignment without XS attribute\n");
 		}
 		
-		bh = create_hit(bam1_qname(hit_buf),
+		bh = create_hit(bam_get_qname(hit_buf),
 						text_name,
 						text_offset,  // BAM files are 0-indexed
 						cigar,
@@ -739,7 +739,7 @@ static const unsigned MAX_HEADER_LEN = 64 * 1024 * 1024; // 4 MB
 
 bool BAMHitFactory::inspect_header()
 {
-    bam_header_t* header = _hit_file->header;
+    bam_hdr_t* header = _hit_file->bam_header;
     
     if (header == NULL)
     {
diff --git a/src/hits.h b/src/hits.h
index 4072389..e63f9c1 100644
--- a/src/hits.h
+++ b/src/hits.h
@@ -18,7 +18,8 @@
 
 #include <boost/shared_ptr.hpp>
 
-#include <bam/sam.h>
+#include <htslib/sam.h>
+#include <htslib/bgzf.h>
 
 #include "common.h"
 #include "multireads.h"
@@ -724,16 +725,16 @@ public:
 				  RefSequenceTable& reference_table) : 
 		HitFactory(insert_table, reference_table) 
 	{
-		_hit_file = samopen(hit_file_name.c_str(), "rb", 0);
+		_hit_file = sam_open(hit_file_name.c_str(), "rb");
         
         memset(&_next_hit, 0, sizeof(_next_hit));
         
-		if (_hit_file == NULL || _hit_file->header == NULL) 
+		if (_hit_file == NULL || _hit_file->bam_header == NULL)
 		{
 			throw std::runtime_error("Fail to open BAM file");
 		}
 		
-		_beginning = bgzf_tell(_hit_file->x.bam);
+		_beginning = bgzf_tell(_hit_file->fp.bgzf);
         _eof_encountered = false;
         
         if (inspect_header() == false)
@@ -753,19 +754,19 @@ public:
 	{
 		if (_hit_file)
 		{
-			samclose(_hit_file);
+			sam_close(_hit_file);
 		}
 	}
 	
 	void mark_curr_pos() 
 	{ 
-		_curr_pos = bgzf_tell(_hit_file->x.bam);
+		_curr_pos = bgzf_tell(_hit_file->fp.bgzf);
 	}
 
 	
 	void undo_hit() 
 	{ 
-		bgzf_seek(_hit_file->x.bam, _curr_pos, SEEK_SET);
+		bgzf_seek(_hit_file->fp.bgzf, _curr_pos, SEEK_SET);
 		//--_line_num;
 	}
     
@@ -776,9 +777,9 @@ public:
 	
 	void reset() 
 	{ 
-		if (_hit_file && _hit_file->x.bam)
+		if (_hit_file && _hit_file->fp.bgzf)
 		{
-			bgzf_seek(_hit_file->x.bam, _beginning, SEEK_SET);
+			bgzf_seek(_hit_file->fp.bgzf, _beginning, SEEK_SET);
             _eof_encountered = false;
 		}
 	}
@@ -795,7 +796,7 @@ public:
     bool inspect_header();
 	
 private:
-	samfile_t* _hit_file; 
+	samFile* _hit_file;
 	int64_t _curr_pos;
 	int64_t _beginning;
 	
-- 
2.33.0

