-- GENERATED by C->Haskell Compiler, version 0.28.1 Switcheroo, 1 April 2016 (Haskell)
-- Edit the ORIGNAL .chs file instead!


{-# LINE 1 "src/Bio/SamTools/LowLevel.chs" #-}
-- -*- haskell -*-
{-# LANGUAGE ForeignFunctionInterface, EmptyDataDecls, GeneralizedNewtypeDeriving #-}

module Bio.SamTools.LowLevel ( TamFilePtr
                             , samOpen, samClose
                             , BamFilePtr, BamFileInt
                             , bamOpen, bamClose
                             
                             , BamHeaderPtr, BamHeaderInt
                             , getNTargets, getTargetName, getTargetLen, bamGetTid
                             , bamHeaderInit, bamHeaderDestroy, bamHeaderDestroyPtr, bamInitHeaderHash
                             , setNTargets, setTargetName, setTargetLen
                             
                             , samHeaderRead, samHeaderRead2                             
                             , samRead1
                             
                             , bamHeaderRead, bamHeaderWrite
                             , bamRead1, bamWrite1
                             , BamCigar(..)                             
                             , cigarMatch, cigarIns, cigarDel, cigarRefSkip, cigarSoftClip, cigarHardClip, cigarPad
                             , cigarOp, cigarLength
                             , BamFlag(..)
                             , flagPaired, flagProperPair, flagUnmap, flagMUnmap, flagReverse, flagMReverse
                             , flagRead1, flagRead2, flagSecondary, flagQCFail, flagDup, flagSupplementary
                             , Bam1Ptr, Bam1Int
                             , getTID, getPos, getFlag, getNCigar, getLQSeq, getMTID, getMPos, getISize
                             , bam1Strand, bam1MStrand, bam1Cigar, bam1QName, bam1Seq, bam1Qual, bam1Seqi
                                                                                                                             
                             , bamAuxGet, bamAux2Z, bamAux2i, bamAux2f, bamAux2d, bamAux2A
                                                                                                 
                             , bamInit1, bamDestroy1, bamDestroy1Ptr, bamDup1, bamFormat1

                             , bamAuxAppend
                             
                             , BamIndexInt, BamIndexPtr
                             , bamIndexLoad, bamIndexDestroy
                             , BamIterInt, BamIterPtr
                             , bamIterQuery, bamIterRead, bamIterDestroy
                             , BamFetchFPtr, mkBamFetchFPtr, bamFetch
                             , SamFilePtr, SamFileInt
                             , sbamOpen, sbamClose, getSbamHeader, sbamRead, sbamWrite                             
                             , FaIdxPtr, FaIdxInt
                             , faiLoad, faiDestroy, faiFetchSeq
                             )
where
import qualified Data.Bits as C2HSImp
import qualified Foreign.C.String as C2HSImp
import qualified Foreign.C.Types as C2HSImp
import qualified Foreign.Marshal.Utils as C2HSImp
import qualified Foreign.Ptr as C2HSImp
import qualified Foreign.Storable as C2HSImp
import qualified System.IO.Unsafe as C2HSImp



import System.IO.Unsafe (unsafePerformIO)
import Foreign hiding (Word, unsafePerformIO)
import Foreign.C

import Control.Monad
import qualified Data.ByteString.Char8 as BS








data TamFileInt
type TamFilePtr = C2HSImp.Ptr (TamFileInt)
{-# LINE 68 "src/Bio/SamTools/LowLevel.chs" #-}


data BamFileInt
type BamFilePtr = C2HSImp.Ptr (BamFileInt)
{-# LINE 71 "src/Bio/SamTools/LowLevel.chs" #-}


bamOpen :: (String) -> (String) -> IO ((BamFilePtr))
bamOpen a1 a2 =
  C2HSImp.withCString a1 $ \a1' -> 
  C2HSImp.withCString a2 $ \a2' -> 
  bamOpen'_ a1' a2' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 75 "src/Bio/SamTools/LowLevel.chs" #-}


bamClose :: (BamFilePtr) -> IO ((CInt))
bamClose a1 =
  let {a1' = id a1} in 
  bamClose'_ a1' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 78 "src/Bio/SamTools/LowLevel.chs" #-}


data BamHeaderInt
type BamHeaderPtr = C2HSImp.Ptr (BamHeaderInt)
{-# LINE 81 "src/Bio/SamTools/LowLevel.chs" #-}


getNTargets :: BamHeaderPtr -> IO CInt
getNTargets = (\ptr -> do {C2HSImp.peekByteOff ptr 0 :: IO C2HSImp.CInt})
{-# LINE 84 "src/Bio/SamTools/LowLevel.chs" #-}


setNTargets :: BamHeaderPtr -> CInt -> IO ()
setNTargets = (\ptr val -> do {C2HSImp.pokeByteOff ptr 0 (val :: C2HSImp.CInt)})
{-# LINE 87 "src/Bio/SamTools/LowLevel.chs" #-}


getTargetName :: BamHeaderPtr -> IO (Ptr CString)
getTargetName = (\ptr -> do {C2HSImp.peekByteOff ptr 8 :: IO (C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar))})
{-# LINE 90 "src/Bio/SamTools/LowLevel.chs" #-}


setTargetName :: BamHeaderPtr -> Ptr CString -> IO ()
setTargetName = (\ptr val -> do {C2HSImp.pokeByteOff ptr 8 (val :: (C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)))})
{-# LINE 93 "src/Bio/SamTools/LowLevel.chs" #-}


getTargetLen :: BamHeaderPtr -> IO (Ptr CUInt)
getTargetLen = (\ptr -> do {C2HSImp.peekByteOff ptr 16 :: IO (C2HSImp.Ptr C2HSImp.CUInt)})
{-# LINE 96 "src/Bio/SamTools/LowLevel.chs" #-}


setTargetLen :: BamHeaderPtr -> Ptr CUInt -> IO ()
setTargetLen = (\ptr val -> do {C2HSImp.pokeByteOff ptr 16 (val :: (C2HSImp.Ptr C2HSImp.CUInt))})
{-# LINE 99 "src/Bio/SamTools/LowLevel.chs" #-}


newtype BamFlag = BamFlag { unBamFlag :: CUInt }
                deriving (Eq, Show, Ord, Num, Bits)

flagPaired :: BamFlag
flagPaired = BamFlag (C2HSImp.unsafePerformIO bam_fpaired)
{-# LINE 105 "src/Bio/SamTools/LowLevel.chs" #-}


flagProperPair :: BamFlag
flagProperPair = BamFlag (C2HSImp.unsafePerformIO bam_fproper_pair)
{-# LINE 108 "src/Bio/SamTools/LowLevel.chs" #-}


flagUnmap :: BamFlag
flagUnmap = BamFlag (C2HSImp.unsafePerformIO bam_funmap)
{-# LINE 111 "src/Bio/SamTools/LowLevel.chs" #-}


flagMUnmap :: BamFlag
flagMUnmap = BamFlag (C2HSImp.unsafePerformIO bam_fmunmap)
{-# LINE 114 "src/Bio/SamTools/LowLevel.chs" #-}


flagReverse :: BamFlag
flagReverse = BamFlag (C2HSImp.unsafePerformIO bam_freverse)
{-# LINE 117 "src/Bio/SamTools/LowLevel.chs" #-}


flagMReverse :: BamFlag
flagMReverse = BamFlag (C2HSImp.unsafePerformIO bam_fmreverse)
{-# LINE 120 "src/Bio/SamTools/LowLevel.chs" #-}


flagRead1 :: BamFlag
flagRead1 = BamFlag (C2HSImp.unsafePerformIO bam_fread1)
{-# LINE 123 "src/Bio/SamTools/LowLevel.chs" #-}


flagRead2 :: BamFlag
flagRead2 = BamFlag (C2HSImp.unsafePerformIO bam_fread2)
{-# LINE 126 "src/Bio/SamTools/LowLevel.chs" #-}


flagSecondary :: BamFlag
flagSecondary = BamFlag (C2HSImp.unsafePerformIO bam_fsecondary)
{-# LINE 129 "src/Bio/SamTools/LowLevel.chs" #-}


flagQCFail :: BamFlag
flagQCFail = BamFlag (C2HSImp.unsafePerformIO bam_fqcfail)
{-# LINE 132 "src/Bio/SamTools/LowLevel.chs" #-}


flagDup :: BamFlag
flagDup = BamFlag (C2HSImp.unsafePerformIO bam_fdup)
{-# LINE 135 "src/Bio/SamTools/LowLevel.chs" #-}


flagSupplementary :: BamFlag
flagSupplementary = BamFlag (C2HSImp.unsafePerformIO bam_fsupplementary)
{-# LINE 138 "src/Bio/SamTools/LowLevel.chs" #-}


newtype BamCigar = BamCigar { unBamCigar :: CUInt }
                   deriving (Eq, Show, Ord)
                            
cigarMatch :: BamCigar
cigarMatch = BamCigar (C2HSImp.unsafePerformIO bam_cmatch)
{-# LINE 144 "src/Bio/SamTools/LowLevel.chs" #-}


cigarIns :: BamCigar
cigarIns = BamCigar (C2HSImp.unsafePerformIO bam_cins)
{-# LINE 147 "src/Bio/SamTools/LowLevel.chs" #-}


cigarDel :: BamCigar
cigarDel = BamCigar (C2HSImp.unsafePerformIO bam_cdel)
{-# LINE 150 "src/Bio/SamTools/LowLevel.chs" #-}


cigarRefSkip :: BamCigar
cigarRefSkip = BamCigar (C2HSImp.unsafePerformIO bam_cref_skip)
{-# LINE 153 "src/Bio/SamTools/LowLevel.chs" #-}


cigarSoftClip :: BamCigar
cigarSoftClip = BamCigar (C2HSImp.unsafePerformIO bam_csoft_clip)
{-# LINE 156 "src/Bio/SamTools/LowLevel.chs" #-}


cigarHardClip :: BamCigar
cigarHardClip = BamCigar (C2HSImp.unsafePerformIO bam_chard_clip)
{-# LINE 159 "src/Bio/SamTools/LowLevel.chs" #-}


cigarPad :: BamCigar
cigarPad = BamCigar (C2HSImp.unsafePerformIO bam_cpad)
{-# LINE 162 "src/Bio/SamTools/LowLevel.chs" #-}


cigarOp :: CUInt -> BamCigar
cigarOp = BamCigar . (\ x1 -> C2HSImp.unsafePerformIO (bam_cigar_op x1))
{-# LINE 165 "src/Bio/SamTools/LowLevel.chs" #-}


cigarLength :: CUInt -> CUInt
cigarLength = (\ x1 -> C2HSImp.unsafePerformIO (bam_cigar_length x1))
{-# LINE 168 "src/Bio/SamTools/LowLevel.chs" #-}


data Bam1Int
type Bam1Ptr = C2HSImp.Ptr (Bam1Int)
{-# LINE 171 "src/Bio/SamTools/LowLevel.chs" #-}


getTID :: Bam1Ptr -> IO CInt
getTID = (\ptr -> do {C2HSImp.peekByteOff ptr 0 :: IO C2HSImp.CInt})
{-# LINE 174 "src/Bio/SamTools/LowLevel.chs" #-}


getPos :: Bam1Ptr -> IO CInt
getPos = (\ptr -> do {C2HSImp.peekByteOff ptr 4 :: IO C2HSImp.CInt})
{-# LINE 177 "src/Bio/SamTools/LowLevel.chs" #-}


getFlag :: Bam1Ptr -> IO BamFlag
getFlag = liftM BamFlag . (\ptr -> do {val <- C2HSImp.peekByteOff ptr 20 :: IO C2HSImp.CUInt{-:16-}; return $ (val `C2HSImp.shiftL` (32 - 16)) `C2HSImp.shiftR` (32 - 16)})
{-# LINE 180 "src/Bio/SamTools/LowLevel.chs" #-}


getNCigar :: Bam1Ptr -> IO Int
getNCigar = liftM fromIntegral . (\ptr -> do {val <- C2HSImp.peekByteOff ptr 24 :: IO C2HSImp.CUInt{-:16-}; return $ (val `C2HSImp.shiftL` (32 - 16)) `C2HSImp.shiftR` (32 - 16)})
{-# LINE 183 "src/Bio/SamTools/LowLevel.chs" #-}


getLQSeq :: Bam1Ptr -> IO CInt
getLQSeq = (\ptr -> do {C2HSImp.peekByteOff ptr 28 :: IO C2HSImp.CInt})
{-# LINE 186 "src/Bio/SamTools/LowLevel.chs" #-}


getMTID :: Bam1Ptr -> IO CInt
getMTID = (\ptr -> do {C2HSImp.peekByteOff ptr 32 :: IO C2HSImp.CInt})
{-# LINE 189 "src/Bio/SamTools/LowLevel.chs" #-}


getMPos :: Bam1Ptr -> IO CInt
getMPos =  (\ptr -> do {C2HSImp.peekByteOff ptr 36 :: IO C2HSImp.CInt})
{-# LINE 192 "src/Bio/SamTools/LowLevel.chs" #-}


getISize :: Bam1Ptr -> IO CInt
getISize = (\ptr -> do {C2HSImp.peekByteOff ptr 40 :: IO C2HSImp.CInt})
{-# LINE 195 "src/Bio/SamTools/LowLevel.chs" #-}


bam1Strand :: (Bam1Ptr) -> (Bool)
bam1Strand a1 =
  C2HSImp.unsafePerformIO $
  let {a1' = id a1} in 
  bam1Strand'_ a1' >>= \res ->
  let {res' = C2HSImp.toBool res} in
  return (res')

{-# LINE 198 "src/Bio/SamTools/LowLevel.chs" #-}


bam1MStrand :: (Bam1Ptr) -> (Bool)
bam1MStrand a1 =
  C2HSImp.unsafePerformIO $
  let {a1' = id a1} in 
  bam1MStrand'_ a1' >>= \res ->
  let {res' = C2HSImp.toBool res} in
  return (res')

{-# LINE 201 "src/Bio/SamTools/LowLevel.chs" #-}


bam1Cigar :: (Bam1Ptr) -> (Ptr CUInt)
bam1Cigar a1 =
  C2HSImp.unsafePerformIO $
  let {a1' = id a1} in 
  bam1Cigar'_ a1' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 204 "src/Bio/SamTools/LowLevel.chs" #-}


bam1QName :: (Bam1Ptr) -> (BS.ByteString)
bam1QName a1 =
  C2HSImp.unsafePerformIO $
  let {a1' = id a1} in 
  bam1QName'_ a1' >>= \res ->
  packCString res >>= \res' ->
  return (res')

{-# LINE 207 "src/Bio/SamTools/LowLevel.chs" #-}


bam1Seq :: (Bam1Ptr) -> (Ptr CUChar)
bam1Seq a1 =
  C2HSImp.unsafePerformIO $
  let {a1' = id a1} in 
  bam1Seq'_ a1' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 210 "src/Bio/SamTools/LowLevel.chs" #-}


bam1Qual :: (Bam1Ptr) -> (Ptr CUChar)
bam1Qual a1 =
  C2HSImp.unsafePerformIO $
  let {a1' = id a1} in 
  bam1Qual'_ a1' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 213 "src/Bio/SamTools/LowLevel.chs" #-}


bam1Seqi :: (Ptr CUChar) -> (CInt) -> (CUChar)
bam1Seqi a1 a2 =
  C2HSImp.unsafePerformIO $
  let {a1' = id a1} in 
  let {a2' = id a2} in 
  bam1Seqi'_ a1' a2' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 217 "src/Bio/SamTools/LowLevel.chs" #-}


-- Low-level SAM I/O

samOpen :: (String) -> IO ((TamFilePtr))
samOpen a1 =
  C2HSImp.withCString a1 $ \a1' -> 
  samOpen'_ a1' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 222 "src/Bio/SamTools/LowLevel.chs" #-}


samClose :: (TamFilePtr) -> IO ()
samClose a1 =
  let {a1' = id a1} in 
  samClose'_ a1' >>
  return ()

{-# LINE 225 "src/Bio/SamTools/LowLevel.chs" #-}


samRead1 :: (TamFilePtr) -> (BamHeaderPtr) -> (Bam1Ptr) -> IO ((Int))
samRead1 a1 a2 a3 =
  let {a1' = id a1} in 
  let {a2' = id a2} in 
  let {a3' = id a3} in 
  samRead1'_ a1' a2' a3' >>= \res ->
  let {res' = fromIntegral res} in
  return (res')

{-# LINE 230 "src/Bio/SamTools/LowLevel.chs" #-}


samHeaderRead2 :: (String) -> IO ((BamHeaderPtr))
samHeaderRead2 a1 =
  C2HSImp.withCString a1 $ \a1' -> 
  samHeaderRead2'_ a1' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 233 "src/Bio/SamTools/LowLevel.chs" #-}


samHeaderRead :: (TamFilePtr) -> IO ((BamHeaderPtr))
samHeaderRead a1 =
  let {a1' = id a1} in 
  samHeaderRead'_ a1' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 236 "src/Bio/SamTools/LowLevel.chs" #-}


bamGetTid :: (BamHeaderPtr) -> (BS.ByteString) -> IO ((CInt))
bamGetTid a1 a2 =
  let {a1' = id a1} in 
  useAsCString a2 $ \a2' -> 
  bamGetTid'_ a1' a2' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 240 "src/Bio/SamTools/LowLevel.chs" #-}


-- Low-level BAM I/O
bamHeaderInit :: IO ((BamHeaderPtr))
bamHeaderInit =
  bamHeaderInit'_ >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 244 "src/Bio/SamTools/LowLevel.chs" #-}


bamHeaderDestroy :: (BamHeaderPtr) -> IO ()
bamHeaderDestroy a1 =
  let {a1' = id a1} in 
  bamHeaderDestroy'_ a1' >>
  return ()

{-# LINE 247 "src/Bio/SamTools/LowLevel.chs" #-}


foreign import ccall unsafe "bam.h &bam_header_destroy" bamHeaderDestroyPtr :: FunPtr (Ptr BamHeaderInt -> IO ())

bamHeaderRead :: (BamFilePtr) -> IO ((BamHeaderPtr))
bamHeaderRead a1 =
  let {a1' = id a1} in 
  bamHeaderRead'_ a1' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 252 "src/Bio/SamTools/LowLevel.chs" #-}


bamHeaderWrite :: (BamFilePtr) -> (BamHeaderPtr) -> IO ((CInt))
bamHeaderWrite a1 a2 =
  let {a1' = id a1} in 
  let {a2' = id a2} in 
  bamHeaderWrite'_ a1' a2' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 256 "src/Bio/SamTools/LowLevel.chs" #-}


bamInitHeaderHash :: (BamHeaderPtr) -> IO ((()))
bamInitHeaderHash a1 =
  let {a1' = id a1} in 
  bamInitHeaderHash'_ a1' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 259 "src/Bio/SamTools/LowLevel.chs" #-}


bamRead1 :: (BamFilePtr) -> (Bam1Ptr) -> IO ((CInt))
bamRead1 a1 a2 =
  let {a1' = id a1} in 
  let {a2' = id a2} in 
  bamRead1'_ a1' a2' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 263 "src/Bio/SamTools/LowLevel.chs" #-}


bamWrite1 :: (BamFilePtr) -> (Bam1Ptr) -> IO ((CInt))
bamWrite1 a1 a2 =
  let {a1' = id a1} in 
  let {a2' = id a2} in 
  bamWrite1'_ a1' a2' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 267 "src/Bio/SamTools/LowLevel.chs" #-}


bamAuxGet :: (Bam1Ptr) -> (CString) -> IO ((Ptr CUChar))
bamAuxGet a1 a2 =
  let {a1' = id a1} in 
  let {a2' = id a2} in 
  bamAuxGet'_ a1' a2' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 271 "src/Bio/SamTools/LowLevel.chs" #-}


bamAux2Z :: (Ptr CUChar) -> IO ((CString))
bamAux2Z a1 =
  let {a1' = id a1} in 
  bamAux2Z'_ a1' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 274 "src/Bio/SamTools/LowLevel.chs" #-}


bamAux2i :: (Ptr CUChar) -> IO ((CInt))
bamAux2i a1 =
  let {a1' = id a1} in 
  bamAux2i'_ a1' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 277 "src/Bio/SamTools/LowLevel.chs" #-}


bamAux2f :: (Ptr CUChar) -> IO ((CFloat))
bamAux2f a1 =
  let {a1' = id a1} in 
  bamAux2f'_ a1' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 280 "src/Bio/SamTools/LowLevel.chs" #-}


bamAux2d :: (Ptr CUChar) -> IO ((CDouble))
bamAux2d a1 =
  let {a1' = id a1} in 
  bamAux2d'_ a1' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 283 "src/Bio/SamTools/LowLevel.chs" #-}


bamAux2A :: (Ptr CUChar) -> IO ((CChar))
bamAux2A a1 =
  let {a1' = id a1} in 
  bamAux2A'_ a1' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 286 "src/Bio/SamTools/LowLevel.chs" #-}


bamInit1 :: IO ((Bam1Ptr))
bamInit1 =
  bamInit1'_ >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 289 "src/Bio/SamTools/LowLevel.chs" #-}


bamDestroy1 :: (Bam1Ptr) -> IO ()
bamDestroy1 a1 =
  let {a1' = id a1} in 
  bamDestroy1'_ a1' >>
  return ()

{-# LINE 292 "src/Bio/SamTools/LowLevel.chs" #-}


foreign import ccall unsafe "samtools.h &bam_destroy1_" bamDestroy1Ptr :: FunPtr (Ptr Bam1Int -> IO ())

bamDup1 :: (Bam1Ptr) -> IO ((Bam1Ptr))
bamDup1 a1 =
  let {a1' = id a1} in 
  bamDup1'_ a1' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 297 "src/Bio/SamTools/LowLevel.chs" #-}


bamFormat1 :: (BamHeaderPtr) -> (Bam1Ptr) -> IO ((CString))
bamFormat1 a1 a2 =
  let {a1' = id a1} in 
  let {a2' = id a2} in 
  bamFormat1'_ a1' a2' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 300 "src/Bio/SamTools/LowLevel.chs" #-}


bamAuxAppend :: (Bam1Ptr) -> (Ptr CChar) -> (CChar) -> (CInt) -> (Ptr CUChar) -> IO ()
bamAuxAppend a1 a2 a3 a4 a5 =
  let {a1' = id a1} in 
  let {a2' = id a2} in 
  let {a3' = id a3} in 
  let {a4' = id a4} in 
  let {a5' = id a5} in 
  bamAuxAppend'_ a1' a2' a3' a4' a5' >>
  return ()

{-# LINE 308 "src/Bio/SamTools/LowLevel.chs" #-}


-- BAM indexing

data BamIndexInt
type BamIndexPtr = C2HSImp.Ptr (BamIndexInt)
{-# LINE 313 "src/Bio/SamTools/LowLevel.chs" #-}


bamIndexLoad :: (String) -> IO ((BamIndexPtr))
bamIndexLoad a1 =
  C2HSImp.withCString a1 $ \a1' -> 
  bamIndexLoad'_ a1' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 316 "src/Bio/SamTools/LowLevel.chs" #-}


bamIndexDestroy :: (BamIndexPtr) -> IO ()
bamIndexDestroy a1 =
  let {a1' = id a1} in 
  bamIndexDestroy'_ a1' >>
  return ()

{-# LINE 319 "src/Bio/SamTools/LowLevel.chs" #-}


type BamFetchFPtr = C2HSImp.FunPtr (((Bam1Ptr) -> ((C2HSImp.Ptr ()) -> (IO C2HSImp.CInt))))
{-# LINE 321 "src/Bio/SamTools/LowLevel.chs" #-}


foreign import ccall "wrapper"
  mkBamFetchFPtr :: (Bam1Ptr -> Ptr () -> IO CInt) -> IO (FunPtr (Bam1Ptr ->Ptr () -> IO CInt))

bamFetch :: (BamFilePtr) -> (BamIndexPtr) -> (CInt) -> (CInt) -> (CInt) -> (Ptr ()) -> (BamFetchFPtr) -> IO ((CInt))
bamFetch a1 a2 a3 a4 a5 a6 a7 =
  let {a1' = id a1} in 
  let {a2' = id a2} in 
  let {a3' = id a3} in 
  let {a4' = id a4} in 
  let {a5' = id a5} in 
  let {a6' = id a6} in 
  let {a7' = id a7} in 
  bamFetch'_ a1' a2' a3' a4' a5' a6' a7' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 329 "src/Bio/SamTools/LowLevel.chs" #-}


data BamIterInt
type BamIterPtr = C2HSImp.Ptr (BamIterInt)
{-# LINE 332 "src/Bio/SamTools/LowLevel.chs" #-}


bamIterQuery :: (BamIndexPtr) -> (CInt) -> (CInt) -> (CInt) -> IO ((BamIterPtr))
bamIterQuery a1 a2 a3 a4 =
  let {a1' = id a1} in 
  let {a2' = id a2} in 
  let {a3' = id a3} in 
  let {a4' = id a4} in 
  bamIterQuery'_ a1' a2' a3' a4' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 335 "src/Bio/SamTools/LowLevel.chs" #-}


bamIterRead :: (BamFilePtr) -> (BamIterPtr) -> (Bam1Ptr) -> IO ((CInt))
bamIterRead a1 a2 a3 =
  let {a1' = id a1} in 
  let {a2' = id a2} in 
  let {a3' = id a3} in 
  bamIterRead'_ a1' a2' a3' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 338 "src/Bio/SamTools/LowLevel.chs" #-}


bamIterDestroy :: (BamIterPtr) -> IO ((()))
bamIterDestroy a1 =
  let {a1' = id a1} in 
  bamIterDestroy'_ a1' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 341 "src/Bio/SamTools/LowLevel.chs" #-}


-- Unified SAM/BAM I/O

data SamFileInt
type SamFilePtr = C2HSImp.Ptr (SamFileInt)
{-# LINE 346 "src/Bio/SamTools/LowLevel.chs" #-}


getSbamHeader :: SamFilePtr -> IO BamHeaderPtr
getSbamHeader = (\ptr -> do {C2HSImp.peekByteOff ptr 16 :: IO (BamHeaderPtr)})
{-# LINE 349 "src/Bio/SamTools/LowLevel.chs" #-}


sbamOpen :: (String) -> (String) -> (Ptr ()) -> IO ((SamFilePtr))
sbamOpen a1 a2 a3 =
  C2HSImp.withCString a1 $ \a1' -> 
  C2HSImp.withCString a2 $ \a2' -> 
  let {a3' = id a3} in 
  sbamOpen'_ a1' a2' a3' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 354 "src/Bio/SamTools/LowLevel.chs" #-}

    
sbamClose :: (SamFilePtr) -> IO ()
sbamClose a1 =
  let {a1' = id a1} in 
  sbamClose'_ a1' >>
  return ()

{-# LINE 357 "src/Bio/SamTools/LowLevel.chs" #-}


sbamRead :: (SamFilePtr) -> (Bam1Ptr) -> IO ((CInt))
sbamRead a1 a2 =
  let {a1' = id a1} in 
  let {a2' = id a2} in 
  sbamRead'_ a1' a2' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 361 "src/Bio/SamTools/LowLevel.chs" #-}


sbamWrite :: (SamFilePtr) -> (Bam1Ptr) -> IO ((CInt))
sbamWrite a1 a2 =
  let {a1' = id a1} in 
  let {a2' = id a2} in 
  sbamWrite'_ a1' a2' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 365 "src/Bio/SamTools/LowLevel.chs" #-}


-- FASTA Indexing

data FaIdxInt
type FaIdxPtr = C2HSImp.Ptr (FaIdxInt)
{-# LINE 370 "src/Bio/SamTools/LowLevel.chs" #-}


faiLoad :: (String) -> IO ((FaIdxPtr))
faiLoad a1 =
  C2HSImp.withCString a1 $ \a1' -> 
  faiLoad'_ a1' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 373 "src/Bio/SamTools/LowLevel.chs" #-}


faiDestroy :: (FaIdxPtr) -> IO ()
faiDestroy a1 =
  let {a1' = id a1} in 
  faiDestroy'_ a1' >>
  return ()

{-# LINE 376 "src/Bio/SamTools/LowLevel.chs" #-}


faiFetchSeq :: (FaIdxPtr) -> (CString) -> (CInt) -> (CInt) -> (Ptr CInt) -> IO ((CString))
faiFetchSeq a1 a2 a3 a4 a5 =
  let {a1' = id a1} in 
  let {a2' = id a2} in 
  let {a3' = id a3} in 
  let {a4' = id a4} in 
  let {a5' = id a5} in 
  faiFetchSeq'_ a1' a2' a3' a4' a5' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 381 "src/Bio/SamTools/LowLevel.chs" #-}


-- Helpers

packCString :: CString -> IO BS.ByteString
packCString = BS.packCString

useAsCString :: BS.ByteString -> (CString -> IO a) -> IO a
useAsCString = BS.useAsCString


foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_open_"
  bamOpen'_ :: ((C2HSImp.Ptr C2HSImp.CChar) -> ((C2HSImp.Ptr C2HSImp.CChar) -> (IO (BamFilePtr))))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_close_"
  bamClose'_ :: ((BamFilePtr) -> (IO C2HSImp.CInt))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_fpaired"
  bam_fpaired :: (IO C2HSImp.CUInt)

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_fproper_pair"
  bam_fproper_pair :: (IO C2HSImp.CUInt)

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_funmap"
  bam_funmap :: (IO C2HSImp.CUInt)

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_fmunmap"
  bam_fmunmap :: (IO C2HSImp.CUInt)

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_freverse"
  bam_freverse :: (IO C2HSImp.CUInt)

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_fmreverse"
  bam_fmreverse :: (IO C2HSImp.CUInt)

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_fread1"
  bam_fread1 :: (IO C2HSImp.CUInt)

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_fread2"
  bam_fread2 :: (IO C2HSImp.CUInt)

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_fsecondary"
  bam_fsecondary :: (IO C2HSImp.CUInt)

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_fqcfail"
  bam_fqcfail :: (IO C2HSImp.CUInt)

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_fdup"
  bam_fdup :: (IO C2HSImp.CUInt)

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_fsupplementary"
  bam_fsupplementary :: (IO C2HSImp.CUInt)

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_cmatch"
  bam_cmatch :: (IO C2HSImp.CUInt)

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_cins"
  bam_cins :: (IO C2HSImp.CUInt)

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_cdel"
  bam_cdel :: (IO C2HSImp.CUInt)

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_cref_skip"
  bam_cref_skip :: (IO C2HSImp.CUInt)

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_csoft_clip"
  bam_csoft_clip :: (IO C2HSImp.CUInt)

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_chard_clip"
  bam_chard_clip :: (IO C2HSImp.CUInt)

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_cpad"
  bam_cpad :: (IO C2HSImp.CUInt)

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_cigar_op"
  bam_cigar_op :: (C2HSImp.CUInt -> (IO C2HSImp.CUInt))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_cigar_length"
  bam_cigar_length :: (C2HSImp.CUInt -> (IO C2HSImp.CUInt))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam1_strand_"
  bam1Strand'_ :: ((Bam1Ptr) -> (IO C2HSImp.CInt))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam1_mstrand_"
  bam1MStrand'_ :: ((Bam1Ptr) -> (IO C2HSImp.CInt))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam1_cigar_"
  bam1Cigar'_ :: ((Bam1Ptr) -> (IO (C2HSImp.Ptr C2HSImp.CUInt)))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam1_qname_"
  bam1QName'_ :: ((Bam1Ptr) -> (IO (C2HSImp.Ptr C2HSImp.CChar)))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam1_seq_"
  bam1Seq'_ :: ((Bam1Ptr) -> (IO (C2HSImp.Ptr C2HSImp.CUChar)))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam1_qual_"
  bam1Qual'_ :: ((Bam1Ptr) -> (IO (C2HSImp.Ptr C2HSImp.CUChar)))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam1_seqi_"
  bam1Seqi'_ :: ((C2HSImp.Ptr C2HSImp.CUChar) -> (C2HSImp.CInt -> (IO C2HSImp.CUChar)))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h sam_open"
  samOpen'_ :: ((C2HSImp.Ptr C2HSImp.CChar) -> (IO (TamFilePtr)))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h sam_close"
  samClose'_ :: ((TamFilePtr) -> (IO ()))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h sam_read1"
  samRead1'_ :: ((TamFilePtr) -> ((BamHeaderPtr) -> ((Bam1Ptr) -> (IO C2HSImp.CInt))))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h sam_header_read2"
  samHeaderRead2'_ :: ((C2HSImp.Ptr C2HSImp.CChar) -> (IO (BamHeaderPtr)))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h sam_header_read"
  samHeaderRead'_ :: ((TamFilePtr) -> (IO (BamHeaderPtr)))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_get_tid"
  bamGetTid'_ :: ((BamHeaderPtr) -> ((C2HSImp.Ptr C2HSImp.CChar) -> (IO C2HSImp.CInt)))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_header_init"
  bamHeaderInit'_ :: (IO (BamHeaderPtr))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_header_destroy"
  bamHeaderDestroy'_ :: ((BamHeaderPtr) -> (IO ()))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_header_read"
  bamHeaderRead'_ :: ((BamFilePtr) -> (IO (BamHeaderPtr)))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_header_write"
  bamHeaderWrite'_ :: ((BamFilePtr) -> ((BamHeaderPtr) -> (IO C2HSImp.CInt)))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_init_header_hash"
  bamInitHeaderHash'_ :: ((BamHeaderPtr) -> (IO ()))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_read1"
  bamRead1'_ :: ((BamFilePtr) -> ((Bam1Ptr) -> (IO C2HSImp.CInt)))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_write1"
  bamWrite1'_ :: ((BamFilePtr) -> ((Bam1Ptr) -> (IO C2HSImp.CInt)))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_aux_get"
  bamAuxGet'_ :: ((Bam1Ptr) -> ((C2HSImp.Ptr C2HSImp.CChar) -> (IO (C2HSImp.Ptr C2HSImp.CUChar))))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_aux2Z"
  bamAux2Z'_ :: ((C2HSImp.Ptr C2HSImp.CUChar) -> (IO (C2HSImp.Ptr C2HSImp.CChar)))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_aux2i"
  bamAux2i'_ :: ((C2HSImp.Ptr C2HSImp.CUChar) -> (IO C2HSImp.CInt))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_aux2f"
  bamAux2f'_ :: ((C2HSImp.Ptr C2HSImp.CUChar) -> (IO C2HSImp.CFloat))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_aux2d"
  bamAux2d'_ :: ((C2HSImp.Ptr C2HSImp.CUChar) -> (IO C2HSImp.CDouble))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_aux2A"
  bamAux2A'_ :: ((C2HSImp.Ptr C2HSImp.CUChar) -> (IO C2HSImp.CChar))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_init1_"
  bamInit1'_ :: (IO (Bam1Ptr))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_destroy1_"
  bamDestroy1'_ :: ((Bam1Ptr) -> (IO ()))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_dup1_"
  bamDup1'_ :: ((Bam1Ptr) -> (IO (Bam1Ptr)))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_format1"
  bamFormat1'_ :: ((BamHeaderPtr) -> ((Bam1Ptr) -> (IO (C2HSImp.Ptr C2HSImp.CChar))))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_aux_append"
  bamAuxAppend'_ :: ((Bam1Ptr) -> ((C2HSImp.Ptr C2HSImp.CChar) -> (C2HSImp.CChar -> (C2HSImp.CInt -> ((C2HSImp.Ptr C2HSImp.CUChar) -> (IO ()))))))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_index_load"
  bamIndexLoad'_ :: ((C2HSImp.Ptr C2HSImp.CChar) -> (IO (BamIndexPtr)))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_index_destroy"
  bamIndexDestroy'_ :: ((BamIndexPtr) -> (IO ()))

foreign import ccall safe "Bio/SamTools/LowLevel.chs.h bam_fetch"
  bamFetch'_ :: ((BamFilePtr) -> ((BamIndexPtr) -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> ((C2HSImp.Ptr ()) -> ((BamFetchFPtr) -> (IO C2HSImp.CInt))))))))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_iter_query"
  bamIterQuery'_ :: ((BamIndexPtr) -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> (IO (BamIterPtr))))))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_iter_read"
  bamIterRead'_ :: ((BamFilePtr) -> ((BamIterPtr) -> ((Bam1Ptr) -> (IO C2HSImp.CInt))))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_iter_destroy"
  bamIterDestroy'_ :: ((BamIterPtr) -> (IO ()))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h samopen"
  sbamOpen'_ :: ((C2HSImp.Ptr C2HSImp.CChar) -> ((C2HSImp.Ptr C2HSImp.CChar) -> ((C2HSImp.Ptr ()) -> (IO (SamFilePtr)))))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h samclose"
  sbamClose'_ :: ((SamFilePtr) -> (IO ()))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h samread"
  sbamRead'_ :: ((SamFilePtr) -> ((Bam1Ptr) -> (IO C2HSImp.CInt)))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h samwrite"
  sbamWrite'_ :: ((SamFilePtr) -> ((Bam1Ptr) -> (IO C2HSImp.CInt)))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h fai_load"
  faiLoad'_ :: ((C2HSImp.Ptr C2HSImp.CChar) -> (IO (FaIdxPtr)))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h fai_destroy"
  faiDestroy'_ :: ((FaIdxPtr) -> (IO ()))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h faidx_fetch_seq"
  faiFetchSeq'_ :: ((FaIdxPtr) -> ((C2HSImp.Ptr C2HSImp.CChar) -> (C2HSImp.CInt -> (C2HSImp.CInt -> ((C2HSImp.Ptr C2HSImp.CInt) -> (IO (C2HSImp.Ptr C2HSImp.CChar)))))))