Description: The bam_dup1 function desc is missing from pysam, hence manually add in a couple
 of functions
Author: Nilesh Patra <nilesh@debian.org>
Forwarded: not-needed
Last-Update: 2022-12-31
--- /dev/null
+++ b/pairtools/lib/bam_header.h
@@ -0,0 +1,37 @@
+#ifndef BAM_HEADER_H
+#define BAM_HEADER_H
+
+#include "htslib/sam.h"
+#include "htslib/vcf.h"
+#include "htslib/khash.h"
+
+bam1_t *bam_init1()
+{
+    return (bam1_t*)calloc(1, sizeof(bam1_t));
+}
+
+bam1_t *bam_copy1(bam1_t *bdst, const bam1_t *bsrc)
+{
+    uint8_t *data = bdst->data;
+    int m_data = bdst->m_data;   // backup data and m_data
+    if (m_data < bsrc->l_data) { // double the capacity
+        m_data = bsrc->l_data; kroundup32(m_data);
+        data = (uint8_t*)realloc(data, m_data);
+    }
+    memcpy(data, bsrc->data, bsrc->l_data); // copy var-len data
+    *bdst = *bsrc; // copy the rest
+    // restore the backup
+    bdst->m_data = m_data;
+    bdst->data = data;
+    return bdst;
+}
+
+bam1_t *bam_dup1(const bam1_t *bsrc)
+{
+    if (bsrc == NULL) return NULL;
+    bam1_t *bdst = bam_init1();
+    if (bdst == NULL) return NULL;
+    return bam_copy1(bdst, bsrc);
+}
+
+#endif
--- /dev/null
+++ b/pairtools/lib/parse_pysam.pxd
@@ -0,0 +1,27 @@
+from libc.stdint cimport int8_t, int16_t, int32_t, int64_t
+from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t
+
+cdef extern from "bam_header.h" nogil:
+    ctypedef struct bam1_core_t:
+        int32_t tid
+        int32_t pos
+        uint16_t bin
+        uint8_t qual
+        uint8_t l_qname
+        uint16_t flag
+        uint8_t unused1
+        uint8_t l_extranul
+        uint32_t n_cigar
+        int32_t l_qseq
+        int32_t mtid
+        int32_t mpos
+        int32_t isize
+
+    ctypedef struct bam1_t:
+        bam1_core_t core
+        int l_data
+        uint32_t m_data
+        uint8_t *data
+        uint64_t id
+
+    bam1_t *bam_dup1(const bam1_t *bsrc)
