bgzf (empty) → 0.1.0.0
raw patch · 4 files changed
+110/−0 lines, 4 filesdep +basedep +bytestringdep +mtlsetup-changed
Dependencies added: base, bytestring, mtl, parallel, pipes, streaming-commons
Files
- LICENSE +12/−0
- Pipes/Bgzf.hs +73/−0
- Setup.hs +2/−0
- bgzf.cabal +23/−0
+ LICENSE view
@@ -0,0 +1,12 @@+Copyright (c) 2014-2015, Rob O'Callahan+All rights reserved.++Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:++1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.++2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.++3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Pipes/Bgzf.hs view
@@ -0,0 +1,73 @@+{-# LANGUAGE ForeignFunctionInterface, RankNTypes, OverloadedStrings #-}+module Pipes.Bgzf (+ inflateBlock,+ bgzfPipe,+ bgzfMultiPipe ) where++import Data.Streaming.Zlib.Lowlevel+import Data.Streaming.Zlib (ZlibException (..), WindowBits(..))+import Foreign.Ptr+import Foreign.Storable+import Foreign.ForeignPtr+import Foreign.C.Types+import Data.ByteString.Internal+import Control.Parallel.Strategies+import Control.Monad+import System.IO+import System.IO.Unsafe+import qualified Data.ByteString as B+import Data.ByteString.Unsafe+import Pipes++foreign import ccall unsafe "streaming_commons_free_z_stream_inflate"+ freeZstream :: ZStream' -> IO ()++bgzfBlockSize = 65536++inflateBlock :: ByteString -> ByteString+inflateBlock bs = unsafePerformIO $ do+ zstr <- zstreamNew+ inflateInit2 zstr (WindowBits (-15))+ fp <- mallocByteString bgzfBlockSize+ withForeignPtr fp $ \p -> do+ let buff = castPtr p+ c_set_avail_out zstr buff $ fromIntegral bgzfBlockSize+ unsafeUseAsCStringLen bs $ \(cstr, len) -> do+ c_set_avail_in zstr cstr $ fromIntegral len+ res <- c_call_inflate_noflush zstr+ avail <- c_get_avail_out zstr+ freeZstream zstr+ let size = bgzfBlockSize - fromIntegral avail+ size `seq` return $ PS fp 0 size++parseHeader :: ByteString -> Int+parseHeader bs = unsafePerformIO $ unsafeUseAsCString bs $ \p -> do+ let usptr :: Ptr CUShort+ usptr = castPtr (p `plusPtr` 16)+ blklen <- peek usptr+ return $ fromIntegral $ blklen + 1++bgzfPipe :: MonadIO m => Handle -> Producer' ByteString m ()+bgzfPipe hdl = go where+ go = do+ header <- liftIO $ B.hGet hdl 18+ case parseHeader header of+ 28 -> return ()+ blocklen -> do+ chunk <- liftIO $ B.hGet hdl (blocklen - 18)+ yield $ inflateBlock chunk+ go++bgzfMultiPipe :: MonadIO m => [Handle] -> Producer' [ByteString] m ()+bgzfMultiPipe hdls = go where+ go = do+ mbchunk <- forM hdls $ \h -> do+ header <- liftIO $ B.hGet h 18+ case parseHeader header of+ 28 -> return Nothing+ blocklen -> do+ chunk <- liftIO $ B.hGet h (blocklen - 18)+ return $ Just chunk+ case sequence (mbchunk `using` parList rpar) of+ Nothing -> return ()+ Just l -> yield (parMap rpar inflateBlock l) >> go
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ bgzf.cabal view
@@ -0,0 +1,23 @@+name: bgzf+version: 0.1.0.0+License: BSD3+License-File: LICENSE+synopsis: Blocked GZip+description: Bgzf consists of 64kb maximum sized gzip blocks, and can be inflated using normal zlib functions, but allows for random access+author: rcallahan+maintainer: rcallahan@eurekagenomics.com+copyright: (c) Rob O'Callahan 2014+category: Codec+build-type: Simple+cabal-version: >=1.10++source-repository head+ type: git+ location: https://github.com/rcallahan/bgzf.git++library+ exposed-modules: Pipes.Bgzf+ other-extensions: ForeignFunctionInterface+ build-depends: base >=4.6 && <5, streaming-commons >=0.1 && <0.2, bytestring >=0.10 && <0.11, pipes >= 4.1 && <4.2, mtl, parallel+ default-language: Haskell2010+ ghc-options: -Wall -O2