-- GENERATED by C->Haskell Compiler, version 0.18.2 The shapeless maps, 31 Oct 2014 (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
                             , 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 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 = Ptr (TamFileInt)
{-# LINE 68 "src/Bio/SamTools/LowLevel.chs" #-}


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


bamOpen :: (String) -> (String) -> IO ((BamFilePtr))
bamOpen a1 a2 =
  withCString a1 $ \a1' -> 
  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 = Ptr (BamHeaderInt)
{-# LINE 81 "src/Bio/SamTools/LowLevel.chs" #-}


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


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


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


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


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


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


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

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


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


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


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


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


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


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


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


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


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


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


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


cigarIns :: BamCigar
cigarIns = BamCigar bam_cins
{-# LINE 144 "src/Bio/SamTools/LowLevel.chs" #-}


cigarDel :: BamCigar
cigarDel = BamCigar bam_cdel
{-# LINE 147 "src/Bio/SamTools/LowLevel.chs" #-}


cigarRefSkip :: BamCigar
cigarRefSkip = BamCigar bam_cref_skip
{-# LINE 150 "src/Bio/SamTools/LowLevel.chs" #-}


cigarSoftClip :: BamCigar
cigarSoftClip = BamCigar bam_csoft_clip
{-# LINE 153 "src/Bio/SamTools/LowLevel.chs" #-}


cigarHardClip :: BamCigar
cigarHardClip = BamCigar bam_chard_clip
{-# LINE 156 "src/Bio/SamTools/LowLevel.chs" #-}


cigarPad :: BamCigar
cigarPad = BamCigar bam_cpad
{-# LINE 159 "src/Bio/SamTools/LowLevel.chs" #-}


cigarOp :: CUInt -> BamCigar
cigarOp = BamCigar . bam_cigar_op
{-# LINE 162 "src/Bio/SamTools/LowLevel.chs" #-}


cigarLength :: CUInt -> CUInt
cigarLength = bam_cigar_length
{-# LINE 165 "src/Bio/SamTools/LowLevel.chs" #-}


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


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


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


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


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


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


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


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


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


bam1Strand :: (Bam1Ptr) -> (Bool)
bam1Strand a1 =
  let {a1' = id a1} in 
  let {res = bam1Strand'_ a1'} in
  let {res' = toBool res} in
  (res')

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


bam1MStrand :: (Bam1Ptr) -> (Bool)
bam1MStrand a1 =
  let {a1' = id a1} in 
  let {res = bam1MStrand'_ a1'} in
  let {res' = toBool res} in
  (res')

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


bam1Cigar :: (Bam1Ptr) -> (Ptr CUInt)
bam1Cigar a1 =
  let {a1' = id a1} in 
  let {res = bam1Cigar'_ a1'} in
  let {res' = id res} in
  (res')

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


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

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


bam1Seq :: (Bam1Ptr) -> (Ptr CUChar)
bam1Seq a1 =
  let {a1' = id a1} in 
  let {res = bam1Seq'_ a1'} in
  let {res' = id res} in
  (res')

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


bam1Qual :: (Bam1Ptr) -> (Ptr CUChar)
bam1Qual a1 =
  let {a1' = id a1} in 
  let {res = bam1Qual'_ a1'} in
  let {res' = id res} in
  (res')

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


bam1Seqi :: (Ptr CUChar) -> (CInt) -> (CUChar)
bam1Seqi a1 a2 =
  let {a1' = id a1} in 
  let {a2' = id a2} in 
  let {res = bam1Seqi'_ a1' a2'} in
  let {res' = id res} in
  (res')

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


-- Low-level SAM I/O

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

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


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

{-# LINE 222 "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 227 "src/Bio/SamTools/LowLevel.chs" #-}


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

{-# LINE 230 "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 233 "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 237 "src/Bio/SamTools/LowLevel.chs" #-}


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

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


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

{-# LINE 244 "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 249 "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 253 "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 256 "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 260 "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 264 "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 268 "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 271 "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 274 "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 277 "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 280 "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 283 "src/Bio/SamTools/LowLevel.chs" #-}


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

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


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

{-# LINE 289 "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 294 "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 297 "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 305 "src/Bio/SamTools/LowLevel.chs" #-}


-- BAM indexing

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


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

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


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

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


type BamFetchFPtr = FunPtr (((Bam1Ptr) -> ((Ptr ()) -> (IO CInt))))
{-# LINE 318 "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 326 "src/Bio/SamTools/LowLevel.chs" #-}


data BamIterInt
type BamIterPtr = Ptr (BamIterInt)
{-# LINE 329 "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 332 "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 335 "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 338 "src/Bio/SamTools/LowLevel.chs" #-}


-- Unified SAM/BAM I/O

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


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


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

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

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

{-# LINE 354 "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 358 "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 362 "src/Bio/SamTools/LowLevel.chs" #-}


-- FASTA Indexing

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


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

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


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

{-# LINE 373 "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 378 "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'_ :: ((Ptr CChar) -> ((Ptr CChar) -> (IO (BamFilePtr))))

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

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_fpaired"
  bam_fpaired :: CUInt

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_fproper_pair"
  bam_fproper_pair :: CUInt

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_funmap"
  bam_funmap :: CUInt

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_fmunmap"
  bam_fmunmap :: CUInt

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_freverse"
  bam_freverse :: CUInt

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_fmreverse"
  bam_fmreverse :: CUInt

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_fread1"
  bam_fread1 :: CUInt

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_fread2"
  bam_fread2 :: CUInt

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_fsecondary"
  bam_fsecondary :: CUInt

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_fqcfail"
  bam_fqcfail :: CUInt

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_fdup"
  bam_fdup :: CUInt

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_cmatch"
  bam_cmatch :: CUInt

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_cins"
  bam_cins :: CUInt

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_cdel"
  bam_cdel :: CUInt

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_cref_skip"
  bam_cref_skip :: CUInt

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_csoft_clip"
  bam_csoft_clip :: CUInt

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_chard_clip"
  bam_chard_clip :: CUInt

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_cpad"
  bam_cpad :: CUInt

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

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

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

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

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

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

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

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

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

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h sam_open"
  samOpen'_ :: ((Ptr 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 CInt))))

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h sam_header_read2"
  samHeaderRead2'_ :: ((Ptr 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) -> ((Ptr CChar) -> (IO 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 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 CInt)))

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

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

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

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

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

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

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_aux2A"
  bamAux2A'_ :: ((Ptr CUChar) -> (IO 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 (Ptr CChar))))

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

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_index_load"
  bamIndexLoad'_ :: ((Ptr 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) -> (CInt -> (CInt -> (CInt -> ((Ptr ()) -> ((BamFetchFPtr) -> (IO CInt))))))))

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

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h bam_iter_read"
  bamIterRead'_ :: ((BamFilePtr) -> ((BamIterPtr) -> ((Bam1Ptr) -> (IO 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'_ :: ((Ptr CChar) -> ((Ptr CChar) -> ((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 CInt)))

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

foreign import ccall unsafe "Bio/SamTools/LowLevel.chs.h fai_load"
  faiLoad'_ :: ((Ptr 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) -> ((Ptr CChar) -> (CInt -> (CInt -> ((Ptr CInt) -> (IO (Ptr CChar)))))))