From 86b1b192501eecc8a4e566795327772c32b1633d Mon Sep 17 00:00:00 2001
From: Justin Bedo <cu@cua0.org>
Date: Wed, 5 Dec 2018 10:48:05 +1100
Subject: bowtie: init

---
 default.nix            |  1 +
 test-tnpair.nix        | 11 +++++++----
 tools/bowtie-align.nix | 36 ++++++++++++++++++++++++++++++++++++
 tools/bowtie-index.nix | 22 ++++++++++++++++++++++
 tools/bowtie.nix       |  8 ++++++++
 5 files changed, 74 insertions(+), 4 deletions(-)
 create mode 100644 tools/bowtie-align.nix
 create mode 100644 tools/bowtie-index.nix
 create mode 100644 tools/bowtie.nix

diff --git a/default.nix b/default.nix
index 50b85a3..d607aa2 100644
--- a/default.nix
+++ b/default.nix
@@ -12,6 +12,7 @@ let
     types = callBionix ./lib/types.nix {};
 
     bwa = callBionix ./tools/bwa.nix {};
+    bowtie = callBionix ./tools/bowtie.nix {};
     compression = callBionix ./tools/compression.nix {};
     crumble = callBionix ./tools/crumble.nix {};
     fastqc = callBionix ./tools/fastqc.nix {};
diff --git a/test-tnpair.nix b/test-tnpair.nix
index a6ea163..b01d61e 100644
--- a/test-tnpair.nix
+++ b/test-tnpair.nix
@@ -21,7 +21,9 @@ let
   fetchfq = attrs: types.tagFiletype (types.filetype.fq {}) (fetchlocal attrs);
   fetchfa = attrs: types.tagFiletype (types.filetype.fa {}) (fetchlocal attrs);
 
-  alignWithRG = rg: bwa.align { ref = fetchfa ./example/ref.fa; flags = "-R'@RG\\tID:${rg}\\tSM:${rg}'";};
+  ref = fetchfa ./example/ref.fa;
+
+  alignWithRG = rg: bwa.align { inherit ref; flags = "-R'@RG\\tID:${rg}\\tSM:${rg}'";};
   sort = samtools.sort {};
   flagstat = samtools.flagstat {};
   check = fastqc.check {};
@@ -35,7 +37,7 @@ let
     };
     normal = {name = "mysample2"; files = {
         input1 = fetchfq ./example/sample2-1.fq;
-        input2 = fetchfq ./example/sample2-1.fq;
+        input2 = fetchfq ./example/sample2-2.fq;
       };
     };
   };
@@ -53,8 +55,9 @@ let
       mkdir $out
       ln -s ${tnpairResult.variants} $out/strelka
       mkdir $out/alignments
-      ln -s ${gridss.callVariants {} (with tnpairResult.alignments; [tumour])} $out/gridss
-      ln -s ${gridss.call (with tnpairResult.alignments; [tumour])} $out/gridss2
+      ln -s ${bowtie.align {inherit ref;} tnpair.normal.files} $out/alignments/bowtie-normal.bam
+      ln -s ${gridss.callVariants {} (with tnpairResult.alignments; [normal tumour])} $out/gridss
+      ln -s ${gridss.call (with tnpairResult.alignments; [normal tumour])} $out/gridss2
       ln -s ${samtools.merge {} [tnpairResult.alignments.tumour tnpairResult.alignments.normal]} $out/alignments/merged.bam
       ln -s ${samtools.view { outfmt = types.toCram; } (tnpairResult.alignments.tumour)} $out/alignments/${tnpair.tumour.name}.cram
       ln -s ${samtools.view { outfmt = types.toCram; } (tnpairResult.alignments.normal)} $out/alignments/${tnpair.normal.name}.cram
diff --git a/tools/bowtie-align.nix b/tools/bowtie-align.nix
new file mode 100644
index 0000000..456747a
--- /dev/null
+++ b/tools/bowtie-align.nix
@@ -0,0 +1,36 @@
+{ bionix
+, nixpkgs
+, ref
+, bamOutput ? true
+, flags ? null
+, indexAttrs ? {}
+}:
+
+{ input1
+, input2 ? null
+}:
+
+with nixpkgs;
+with lib;
+with bionix.types;
+with bionix.compression;
+
+let
+  fa = f: matchFiletype "bowtie2-ref" { fa = _: f; } f;
+  fq = f: matchFiletype "bowtie2-input" { fq = _: f; gz = matchFiletype' "bowtie2-input" { fq = _: f; }; } f;
+
+in stdenv.mkDerivation {
+  name = "bowtie2-align";
+  buildInputs = [ bowtie2 bc ] ++ optional bamOutput samtools;
+  buildCommand = ''
+    cores=$(echo $NIX_BUILD_CORES ${optionalString bamOutput "- 1"} | bc)
+    if [[ $cores -lt 1 ]] ; then
+      cores=1
+    fi
+    bowtie2 -x ${bionix.bowtie.index indexAttrs ref}/ref ${optionalString (flags != null) flags} --threads $cores \
+      ${if input2 != null then "-1 " + fq input1 + " -2 " + fq input2 else "-U " + fq input1} \
+      ${optionalString bamOutput "| samtools view -b"} \
+      > $out
+  '';
+  passthru.filetype = if bamOutput then filetype.bam {ref = ref; sorting = sort.name {};} else filetype.sam {ref = ref; sorting = sort.name {};};
+}
diff --git a/tools/bowtie-index.nix b/tools/bowtie-index.nix
new file mode 100644
index 0000000..98e529f
--- /dev/null
+++ b/tools/bowtie-index.nix
@@ -0,0 +1,22 @@
+{ bionix
+, nixpkgs
+, flags ? null
+, seed ? 42
+}:
+
+ref:
+
+with nixpkgs;
+with lib;
+with bionix.types;
+
+assert (matchFiletype "bowtie-index" { fa = _: true; } ref);
+
+stdenv.mkDerivation {
+  name = "bowtie-index";
+  buildInputs = [ bowtie2 ];
+  buildCommand = ''
+    mkdir $out
+    bowtie2-build --seed ${toString seed} --threads $NIX_BUILD_CORES ${optionalString (flags != null) flags} ${ref} $out/ref
+  '';
+}
diff --git a/tools/bowtie.nix b/tools/bowtie.nix
new file mode 100644
index 0000000..a9fa2e8
--- /dev/null
+++ b/tools/bowtie.nix
@@ -0,0 +1,8 @@
+{ bionix, nixpkgs }:
+
+with bionix;
+
+{
+  align = callBionix ./bowtie-align.nix;
+  index = callBionix ./bowtie-index.nix;
+}
-- 
cgit v1.2.3