samtools-enumerator (empty) → 0.1
raw patch · 4 files changed
+117/−0 lines, 4 filesdep +basedep +bytestringdep +enumeratorsetup-changed
Dependencies added: base, bytestring, enumerator, haskell98, samtools, transformers
Files
- LICENSE +21/−0
- Setup.hs +2/−0
- samtools-enumerator.cabal +18/−0
- src/Bio/SamTools/Enumerator.hs +76/−0
+ LICENSE view
@@ -0,0 +1,21 @@+The MIT License++Copyright (c) 2011 Nicholas Ingolia++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in+all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN+THE SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ samtools-enumerator.cabal view
@@ -0,0 +1,18 @@+Name: samtools-enumerator+Version: 0.1+Synopsis: Enumerator interface to SamTools library+Description: Enumerator interface to SamTools library+License: MIT+License-file: LICENSE+Author: Nicholas Ingolia+Maintainer: nick@ingolia.org+Category: Bio++Build-type: Simple+Cabal-version: >=1.4++Library+ Exposed-modules: Bio.SamTools.Enumerator+ Build-depends: base >= 4.3 && < 5, bytestring, haskell98, transformers, enumerator >= 0.4.7 && < 0.5, samtools >= 0.1.1 && < 0.2+ Hs-Source-Dirs: src+
+ src/Bio/SamTools/Enumerator.hs view
@@ -0,0 +1,76 @@+module Bio.SamTools.Enumerator + (+ -- | BAM/SAM enumeration+ enumInHandle+ , enumTam, enumTamWithIndex, enumBam+ -- | Indexed BAM region enumeration+ , enumQuery, enumIndexRegion, enumBamRegion+ -- | BAM/SAM iteration+ , iterHandle+ )+ where++import Control.Exception (bracket, bracket_, finally)+import Control.Monad+import Control.Monad.IO.Class+import qualified Data.ByteString.Char8 as BS+import Data.Int (Int64)+import Data.List+import Data.Maybe++import qualified Bio.SamTools.Bam as Bam+import qualified Bio.SamTools.BamIndex as BamIndex+import qualified Data.Enumerator as E++-- | Enumerate over the contents of a BAM/SAM alignment input handle+enumInHandle :: (MonadIO m) => Bam.InHandle -> E.Enumerator Bam.Bam1 m a+enumInHandle inh = loop+ where loop (E.Continue k) = (liftIO $ Bam.get1 inh) >>= maybe eof next+ where eof = E.returnI (E.Continue k)+ next b = k (E.Chunks [b]) E.>>== loop+ loop step = E.returnI step++-- | Enumerate over the contents of a TAM (tab-delimited text) alignment file+enumTam :: FilePath -> E.Enumerator Bam.Bam1 IO a+enumTam inname step = E.Iteratee $ liftIO $ bracket (Bam.openTamInFile inname) Bam.closeInHandle $ \h ->+ E.runIteratee $ enumInHandle h step++-- | Enumerate over the contents of a TAM file with a separate target sequence index+enumTamWithIndex :: FilePath -> FilePath -> E.Enumerator Bam.Bam1 IO a+enumTamWithIndex inname idxname step+ = E.Iteratee $ liftIO $ bracket (Bam.openTamInFileWithIndex inname idxname) Bam.closeInHandle $ \h ->+ E.runIteratee $ enumInHandle h step++-- | Enumerate over the contents of a BAM (binary) alignment file+enumBam :: FilePath -> E.Enumerator Bam.Bam1 IO a+enumBam inname step = E.Iteratee $ liftIO $ bracket (Bam.openBamInFile inname) Bam.closeInHandle $ \h ->+ E.runIteratee $ enumInHandle h step++-- | Enumerate over the results of a query into a sorted, indexed BAM file+enumQuery :: BamIndex.Query -> E.Enumerator Bam.Bam1 IO a+enumQuery q = loop+ where loop (E.Continue k) = (liftIO $ BamIndex.next q) >>= maybe end next+ where end = E.returnI (E.Continue k)+ next b = k (E.Chunks [b]) E.>>== loop+ loop step = E.returnI step+ +-- | Enumerate over the reads in a region from an indexed BAM file input handle+enumIndexRegion :: BamIndex.IdxHandle -> Int -> (Int64, Int64) -> E.Enumerator Bam.Bam1 IO a+enumIndexRegion h tid bnds step = E.Iteratee $ do + q <- liftIO $ BamIndex.query h tid bnds + E.runIteratee $ enumQuery q step++-- | Enumerate over the reads in a region from a sorted, indexed BAM file+enumBamRegion :: FilePath -> BS.ByteString -> (Int64, Int64) -> E.Enumerator Bam.Bam1 IO a+enumBamRegion inname seqname bnds step = E.Iteratee $ liftIO $ bracket (BamIndex.open inname) (BamIndex.close) $ \h -> do+ tid <- lookupTid $ BamIndex.idxHeader h+ E.runIteratee $ enumIndexRegion h tid bnds step+ where lookupTid = maybe noTid return . findIndex ((== seqname) . Bam.name) . Bam.targetSeqList+ noTid = ioError . userError $ "Target " ++ show seqname ++ " not found in " ++ show inname+ +iterHandle :: Bam.OutHandle -> E.Iteratee Bam.Bam1 IO ()+iterHandle h = E.continue step+ where step E.EOF = E.yield () E.EOF+ step (E.Chunks []) = E.continue step+ step (E.Chunks bs) = do liftIO $ forM_ bs $ Bam.put1 h+ E.continue step