FastxPipe (empty) → 0.2.0.0
raw patch · 4 files changed
+117/−0 lines, 4 filesdep +attoparsecdep +basedep +blaze-buildersetup-changed
Dependencies added: attoparsec, base, blaze-builder, bytestring, pipes, pipes-attoparsec, pipes-bytestring
Files
- FastxPipe.cabal +16/−0
- FastxPipe.hs +87/−0
- LICENSE +12/−0
- Setup.hs +2/−0
+ FastxPipe.cabal view
@@ -0,0 +1,16 @@+name: FastxPipe+version: 0.2.0.0+synopsis: Fasta and Fastq streaming+description: Optimized fasta and fastq parsing using Pipes+license: BSD3+license-file: LICENSE+author: Rob O'Callahan+maintainer: rcallahan@eurekagenomics.com+copyright: (c) 2014-2015 Rob O'Callahan+build-type: Simple+cabal-version: >=1.10++library+ exposed-modules: FastxPipe+ build-depends: base >=4.6 && <5, attoparsec >=0.11 && <0.12, bytestring >=0.10 && <0.11, blaze-builder >=0.3 && <0.4, pipes >=4.0, pipes-bytestring >=1.0 && <1.1, pipes-attoparsec >=0.3 && <0.4+ default-language: Haskell2010
+ FastxPipe.hs view
@@ -0,0 +1,87 @@+module FastxPipe where++import Data.Attoparsec.Char8+import qualified Data.ByteString as B+import qualified Data.ByteString.Char8 as B8+import Data.ByteString.Internal (ByteString(..), memcpy)+import Blaze.ByteString.Builder+import Control.Monad+import Control.Applicative+import Control.Exception+import System.IO+import Data.Monoid+import Pipes+import Pipes.Internal+import qualified Pipes.ByteString as Pb+import qualified Pipes.Prelude as P+import Foreign.Ptr+import Foreign.ForeignPtr (newForeignPtr, withForeignPtr)+import Foreign.Marshal.Alloc (allocaBytes, mallocBytes, reallocBytes, finalizerFree)+import Pipes.Attoparsec hiding (parse)++takeTill' c = takeTill (== c) <* char c+skipTill c = skipWhile (/= c) *> char c *> pure ()++parseFasta :: Parser (ByteString, ByteString)+parseFasta = (,) <$> (char '>' *> takeTill' '\n') <*> multiline where+ multiline = toByteString . mconcat <$> (many1 $ fromByteString <$> do+ Just c <- peekChar+ if c == '>' then fail "next" else takeTill' '\n')++hFastaProd :: Handle -> Producer (ByteString, ByteString) IO ()+hFastaProd h = first where+ start_size = 1024+ first = do+ l <- liftIO $ B.hGetLine h+ case B8.uncons l of+ Just ('>', rest) -> go rest+ _ -> return ()+ go header = do+ let mkPS p nt = do+ p' <- reallocBytes p nt+ fp <- newForeignPtr finalizerFree p'+ return $! (PS fp 0 nt)+ loop p cap nt = do+ eof <- liftIO $ hIsEOF h+ if eof+ then do sq <- liftIO $ mkPS p nt+ yield (header, sq)+ else do+ l@(PS fpl offl lenl) <- liftIO $ B.hGetLine h+ let cap2 = cap * 2+ case B8.uncons l of+ Just ('>', nexthead) -> do+ sq <- liftIO $ mkPS p nt+ yield (header, sq) >> go nexthead+ Just {} -> do+ let nt' = lenl + nt+ (cap', p') <- liftIO $ if nt' > cap then (,) cap2 <$> reallocBytes p cap2 else return (cap, p)+ liftIO $ withForeignPtr fpl $ \ps -> memcpy (p' `plusPtr` nt) (ps `plusPtr` offl) lenl+ loop p' cap' nt'+ Nothing -> return ()+ p <- liftIO $ mallocBytes start_size+ loop p start_size 0++hFastqProd :: Handle -> Producer (ByteString, ByteString, ByteString) IO ()+hFastqProd h = go where+ go = do+ let getline = B.hGetLine h+ doread = do+ line1 <- getline+ head <- case B8.uncons line1 of+ Just ('@', rest) -> return rest+ _ -> ioError $ userError "bad format"+ sq <- getline+ blank <- getline+ case B8.uncons blank of+ Just ('+', _) -> return ()+ _ -> ioError $ userError "bad format"+ qual <- getline+ when (B.length qual /= B.length sq) (ioError $ userError "bad format")+ return $ Just (head, sq, qual)+ catcher :: IOException -> IO (Maybe (ByteString, ByteString, ByteString))+ catcher _ = return Nothing+ mbread <- liftIO $ catch doread catcher+ case mbread of+ Just j -> yield j >> go+ Nothing -> return ()
+ 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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain