b29f1fbf93c9df5aa65fed050d9f8132dc6a2c73
braney
  Tue Nov 24 14:20:10 2015 -0800
support htslib for sam/bam/cram/tabix support  #14717

diff --git src/inc/bamFile.h src/inc/bamFile.h
index 84b7b45..1bded69 100644
--- src/inc/bamFile.h
+++ src/inc/bamFile.h
@@ -1,31 +1,49 @@
 /* bamFile -- interface to binary alignment format files using Heng Li's samtools lib. */
 
 #ifndef BAMFILE_H
 #define BAMFILE_H
 
 #include "dnaseq.h"
 #include "dystring.h"
 
 #ifdef USE_BAM
 
 // bam.h is incomplete without _IOLIB set to 1, 2 or 3.  2 is used by Makefile.generic:
 #ifndef _IOLIB
 #define _IOLIB 2
 #endif
+#ifdef USE_HTS
+#include "htslib/sam.h"
+typedef samFile samfile_t;
+typedef hts_idx_t bam_index_t;
+typedef bam_hdr_t bam_header_t;
+typedef int (*bam_fetch_f)(const bam1_t *bam, void *data, bam_hdr_t *header) ;
+#define samopen(a,b,c) sam_open(a,b)
+#define samclose(a) sam_close(a)
+#define bam1_qname bam_get_qname
+#define bam1_qual bam_get_qual
+#define bam1_aux bam_get_aux
+#define bam1_cigar bam_get_cigar
+#define bam1_seq bam_get_seq
+#define bam1_seqi bam_seqi
+#define bam_nt16_rev_table seq_nt16_str
+#define data_len l_data
+#else
 #include "bam.h"
 #include "sam.h"
+#endif
 
 #else // no USE_BAM
 typedef struct { } bam1_t;
 typedef struct { } bam_index_t;
 typedef struct { } samfile_t;
 typedef int (*bam_fetch_f)(const bam1_t *b, void *data);
 
 #define COMPILE_WITH_SAMTOOLS "%s: in order to use this functionality you must " \
     "install the samtools library (<A HREF=\"http://samtools.sourceforge.net\" " \
     "TARGET=_BLANK>http://samtools.sourceforge.net</A>) and recompile kent/src with " \
     "USE_BAM=1 in your environment " \
     "(see <A HREF=\"http://genomewiki.ucsc.edu/index.php/Build_Environment_Variables\" " \
     "TARGET=_BLANK>http://genomewiki.ucsc.edu/index.php/Build_Environment_Variables</A>)."
 
 #endif // USE_BAM
@@ -46,31 +64,35 @@
 
 samfile_t *bamOpen(char *fileOrUrl, char **retBamFileName);
 /* Return an open bam file as well as the filename of the bam. */
 
 samfile_t *bamMustOpenLocal(char *fileName, char *mode, void *extraHeader);
 /* Open up sam or bam file or die trying.  The mode parameter is 
  *    "r" - open SAM to read
  *    "rb" - open BAM to read
  *    "w" - open SAM to write
  *    "wb" - open BAM to write
  * The extraHeader is generally NULL in the read case, and the write case
  * contains a pointer to a bam_header_t with information about the header.
  * The implementation is just a wrapper around samopen from the samtools library
  * that aborts with error message if there's a problem with the open. */
 
+#ifdef USE_HTS
+void bamFetchAlreadyOpen(samfile_t *samfile, bam_hdr_t *header,  bam_index_t *idx, char *bamFileName, 
+#else
 void bamFetchAlreadyOpen(samfile_t *samfile, bam_index_t *idx, char *bamFileName, 
+#endif
 			 char *position, bam_fetch_f callbackFunc, void *callbackData);
 /* With the open bam file, return items the same way with the callbacks as with bamFetch() */
 /* except in this case use an already-open bam file and index (use bam_index_load and free() for */
 /* the index). It seems a little strange to pass the filename in with the open bam, but */
 /* it's just used to report errors. */
 
 void bamFetch(char *fileOrUrl, char *position, bam_fetch_f callbackFunc, void *callbackData,
 	samfile_t **pSamFile);
 /* Open the .bam file, fetch items in the seq:start-end position range,
  * and call callbackFunc on each bam item retrieved from the file plus callbackData.
  * This handles BAM files with "chr"-less sequence names, e.g. from Ensembl. 
  * The pSamFile parameter is optional.  If non-NULL it will be filled in, just for
  * the benefit of the callback function, with the open samFile.  */
 
 void bamClose(samfile_t **pSamFile);