From 9a70a25d6b7771df7ca53fcc34d61f582296e384 Mon Sep 17 00:00:00 2001
From: Justin Bedo <cu@cua0.org>
Date: Thu, 10 Jan 2019 12:20:51 +1100
Subject: facets: init

---
 default.nix           |  3 ++-
 test-tnpair.nix       |  2 ++
 tools/facets-app.nix  | 42 ++++++++++++++++++++++++++++++++++++++++++
 tools/facets-call.nix | 24 ++++++++++++++++++++++++
 tools/facets.nix      |  8 ++++++++
 5 files changed, 78 insertions(+), 1 deletion(-)
 create mode 100644 tools/facets-app.nix
 create mode 100644 tools/facets-call.nix
 create mode 100644 tools/facets.nix

diff --git a/default.nix b/default.nix
index 3ed2b50..0c2f9ad 100644
--- a/default.nix
+++ b/default.nix
@@ -18,6 +18,7 @@ let
     cnvkit = callBionix ./tools/cnvkit.nix {};
     compression = callBionix ./tools/compression.nix {};
     crumble = callBionix ./tools/crumble.nix {};
+    facets = callBionix ./tools/facets.nix {};
     fastqc = callBionix ./tools/fastqc.nix {};
     gridss = callBionix ./tools/gridss.nix {};
     infercnv = callBionix ./tools/infercnv.nix {};
@@ -25,10 +26,10 @@ let
     mosdepth = callBionix ./tools/mosdepth.nix {};
     mutect = callBionix ./tools/mutect.nix {};
     platypus = callBionix ./tools/platypus.nix {};
+    ref = callBionix ./lib/references.nix {};
     samtools = callBionix ./tools/samtools.nix {};
     snpeff = callBionix ./tools/snpeff.nix {};
     strelka = callBionix ./tools/strelka.nix {};
-    ref = callBionix ./lib/references.nix {};
 
     qsub = attrs: bionix.extend (self: super: with self; rec {
       qsubDefs = { ppn = 1; mem = 1; walltime = "24:00:00"; tmpDir = "/tmp"; sleepTime = 60; } // attrs;
diff --git a/test-tnpair.nix b/test-tnpair.nix
index 0851a74..c531a83 100644
--- a/test-tnpair.nix
+++ b/test-tnpair.nix
@@ -35,11 +35,13 @@ let
   processPair = { tumour, normal }: rec {
     alignments = mapAttrs (_: x: markdup (sort (fixmate (alignWithRG x.name x.files)))) { inherit normal tumour; };
     variants = callVariants alignments;
+    platypusVars = platypus.call {} (builtins.attrValues alignments);
   };
 
   tnpairResult = processPair tnpair;
 
   testNaming = linkDrv [
+    (ln (facets.callCNV {} {vcf = tnpairResult.platypusVars; bams = with tnpairResult.alignments; [ normal tumour ];}) "facets")
     (ln tnpairResult.variants "strelka")
     (ln (bowtie.align {inherit ref;} tnpair.normal.files) "alignments/bowtie-normal.bam")
     (ln (gridss.callVariants {} (with tnpairResult.alignments; [normal tumour])) "gridss")
diff --git a/tools/facets-app.nix b/tools/facets-app.nix
new file mode 100644
index 0000000..8c4a7b2
--- /dev/null
+++ b/tools/facets-app.nix
@@ -0,0 +1,42 @@
+{buildRPackage
+,fetchFromGitHub
+,R
+,htslib
+,zlib}:
+
+let
+  pctGCdata = buildRPackage rec {
+    name = "pctGCdata-${version}";
+    version = "0.2.0";
+    requireX = false;
+    src = fetchFromGitHub {
+      owner = "mskcc";
+      repo = "pctGCdata";
+      rev = "v${version}";
+      sha256 = "1qq0fmm3zwz6rv0ka82850ww0qj50621gln9i0gfs8k3wyqil4l8";
+    };
+    buildInputs = [ R ];
+  };
+
+in buildRPackage rec{
+  name = "facets-${version}";
+  version = "0.5.6";
+  requireX = false;
+  src = fetchFromGitHub {
+    owner = "mskcc";
+    repo = "facets";
+    rev = "v${version}";
+    sha256 = "0yqr23446y2h8xgbgj0r6sl4i778111drgsi06a2cqy530xcmbxs";
+  };
+  buildInputs = [ R htslib zlib ];
+  propagatedBuildInputs = [ pctGCdata ];
+  postBuild = ''
+    cd inst/extcode
+    g++ --std=c++11 snp-pileup.cpp -lhts -o snp-pileup
+    cd ../../
+  '';
+  postInstall = ''
+    mkdir -p $out/bin
+    cp inst/extcode/snp-pileup $out/bin
+  '';
+}
diff --git a/tools/facets-call.nix b/tools/facets-call.nix
new file mode 100644
index 0000000..021cacc
--- /dev/null
+++ b/tools/facets-call.nix
@@ -0,0 +1,24 @@
+{bionix}:
+
+{vcf, bams}:
+
+with bionix;
+with types;
+with lib;
+
+assert (matchFiletype "facets-call-vcf" { vcf = _: true; } vcf);
+assert (all (matchFiletype "facets-call-bam" { bam = _: true; }) bams);
+assert (all (matchFileSorting "facets-call-bam" { coord = _: true; }) bams);
+
+stage {
+  name = "facets";
+  buildInputs = [ facets.app ];
+  buildCommand = ''
+    # Facets requires lexical sorting on the VCF files
+    grep '^#' ${vcf} > input.vcf
+    grep -v '^#' ${vcf} | LC_ALL=C sort -t $'\t' -k1,1 -k2,2n >> input.vcf || true
+
+    # Now actually run facets
+    snp-pileup input.vcf $out ${concatStringsSep " " bams}
+  '';
+}
diff --git a/tools/facets.nix b/tools/facets.nix
new file mode 100644
index 0000000..ed10ffa
--- /dev/null
+++ b/tools/facets.nix
@@ -0,0 +1,8 @@
+{bionix}:
+
+with bionix;
+
+{
+  app = lib.callPackageWith (pkgs // pkgs.rPackages) ./facets-app.nix {};
+  callCNV = callBionix ./facets-call.nix;
+}
-- 
cgit v1.2.3