From 7c29ef494f974b457bfef6a5af7d37fa03bb7952 Mon Sep 17 00:00:00 2001 From: Justin Bedo Date: Thu, 29 Jul 2021 16:19:30 +1000 Subject: initial import --- biohazard.patch | 13 +++++++++++++ default.nix | 21 +++++++++++++++++++++ shell.nix | 12 ++++++++++++ xenomapper.hs | 41 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+) create mode 100644 biohazard.patch create mode 100644 default.nix create mode 100644 shell.nix create mode 100644 xenomapper.hs diff --git a/biohazard.patch b/biohazard.patch new file mode 100644 index 0000000..b075312 --- /dev/null +++ b/biohazard.patch @@ -0,0 +1,13 @@ +diff --git a/Control/Monad/Log.hs b/Control/Monad/Log.hs +index 5b6598a..de4589f 100644 +--- a/Control/Monad/Log.hs ++++ b/Control/Monad/Log.hs +@@ -14,7 +14,7 @@ module Control.Monad.Log + , panic + ) where + +-import BasePrelude hiding ( try, catchIOError ) ++import BasePrelude hiding ( try, catchIOError, option ) + import Control.Monad.Base ( MonadBase(..) ) + import Control.Monad.Catch + import Control.Monad.Primitive diff --git a/default.nix b/default.nix new file mode 100644 index 0000000..3314d66 --- /dev/null +++ b/default.nix @@ -0,0 +1,21 @@ +let + pkgs = import { }; + + ghc = with pkgs.haskell.lib; + pkgs.ghc.withPackages (pkgs: + with pkgs; + [ + (doJailbreak (markUnbroken + (biohazard.overrideAttrs (_: { patches = [ ./biohazard.patch ]; })))) + ]); +in pkgs.stdenv.mkDerivation { + name = "xenomapper-hs"; + buildInputs = [ ghc ]; + src = ./.; + buildPhase = '' + ghc -O3 xenomapper.hs -o xenomapper + ''; + installPhase = '' + install -D xenomapper $out/bin/xenomapper + ''; +} diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..40ee189 --- /dev/null +++ b/shell.nix @@ -0,0 +1,12 @@ +let + pkgs = import { }; + + ghc = with pkgs.haskell.lib; + pkgs.ghc.withPackages (pkgs: + with pkgs; + [ + hlint + (doJailbreak (markUnbroken + (biohazard.overrideAttrs (_: { patches = [ ./biohazard.patch ]; })))) + ]); +in pkgs.mkShell { buildInputs = [ ghc ]; } diff --git a/xenomapper.hs b/xenomapper.hs new file mode 100644 index 0000000..123b1a5 --- /dev/null +++ b/xenomapper.hs @@ -0,0 +1,41 @@ +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE ViewPatterns #-} +module Main where + +import Bio.Prelude hiding ((.)) +import Bio.Bam +import Control.Monad +import Control.Monad.Log +import Control.Monad.IO.Class +import System.Environment +import qualified Streaming.Prelude as S +import Streaming.Prelude (Stream, Of) + +filterBams :: (MonadIO m, MonadLog m, MonadMask m) => Stream (Of BamRaw) m () -> Stream (Of BamRaw) m () -> Stream (Of BamRaw) m () +filterBams a b = do + str0 <- lift $ S.next a + str1 <- lift $ S.next b + loop str0 str1 + where + loop a@(Right (a', a'')) b@(Right (b', b'')) = + case compare (qname a') (qname b') of + EQ -> do + when (as a' > as b') $ S.yield a' + filterBams a'' b'' + LT -> do + n <- lift $ S.next a'' + loop n b + GT -> do + n <- lift $ S.next b'' + loop a n + + loop _ _ = return () + + qname = b_qname . unpackBam + as = extAsInt 0 "AS" . unpackBam + +main = do + args <- getArgs + + withLogging_ (LoggingConf Warning Error Error 10 True) $ do + decodeBamFiles args (\[(hdr, prim), (_, sec)] -> writeBamFile "primary.bam" hdr $ filterBams prim sec) -- cgit v1.2.3