From 36a0a03eda2ec8cf9b1f5fea8c437b68638d1de5 Mon Sep 17 00:00:00 2001 From: Justin Bedo Date: Mon, 26 Sep 2022 11:51:52 +1000 Subject: samtools.view,mosdepth.depth: add targets parameter --- test-tnpair.nix | 159 +++++++++++++++++++++++++++++------------------ tools/mosdepth-depth.nix | 51 +++++++++------ tools/samtools-view.nix | 80 +++++++++++++++++------- 3 files changed, 190 insertions(+), 100 deletions(-) diff --git a/test-tnpair.nix b/test-tnpair.nix index 195ac27..ac8d0bb 100644 --- a/test-tnpair.nix +++ b/test-tnpair.nix @@ -1,25 +1,32 @@ -with import { }; -with lib; - -let - fetchlocal = path: pkgs.stdenv.mkDerivation { - name = baseNameOf path; - buildCommand = "ln -s ${path} $out"; - }; - fetchfq = attrs: types.tagFiletype (types.filetype.fq { }) (fetchlocal attrs); - fetchfa = attrs: types.tagFiletype (types.filetype.fa { }) (fetchlocal attrs); +with import {}; +with lib; let + fetchlocal = path: + pkgs.stdenv.mkDerivation { + name = baseNameOf path; + buildCommand = "ln -s ${path} $out"; + }; + fetchfq = attrs: types.tagFiletype (types.filetype.fq {}) (fetchlocal attrs); + fetchfa = attrs: types.tagFiletype (types.filetype.fa {}) (fetchlocal attrs); ref = fetchfa ./examples/ref.fa; - alignWithRG = aligner: rg: x: nameSort (aligner { inherit ref; RG = { ID = rg; SM = rg; }; } x); - sort = sambamba.sort { }; - nameSort = sambamba.sort { nameSort = true; }; - flagstat = samtools.flagstat { }; - check-fastqc = fastqc.check { }; - check-fastp = fastp.check { }; - callVariants = strelka.callSomatic { }; - markdup = samtools.markdup { }; - fixmate = samtools.fixmate { }; + alignWithRG = aligner: rg: x: + nameSort (aligner { + inherit ref; + RG = { + ID = rg; + SM = rg; + }; + } + x); + sort = sambamba.sort {}; + nameSort = sambamba.sort {nameSort = true;}; + flagstat = samtools.flagstat {}; + check-fastqc = fastqc.check {}; + check-fastp = fastp.check {}; + callVariants = strelka.callSomatic {}; + markdup = samtools.markdup {}; + fixmate = samtools.fixmate {}; tnpair = { tumour = { @@ -38,42 +45,56 @@ let }; }; - processPair = { tumour, normal }: rec { - alignments = mapAttrs (_: x: markdup (sort (fixmate (alignWithRG bwa.align x.name x.files)))) { inherit normal tumour; }; - bowtie-alignments = mapAttrs (_: x: markdup (sort (fixmate (alignWithRG bowtie.align x.name x.files)))) { inherit normal tumour; }; - bwa2-alignments = mapAttrs (_: x: markdup (sort (fixmate (alignWithRG bwa.mem2 x.name x.files)))) { inherit normal tumour; }; + processPair = { + tumour, + normal, + }: rec { + alignments = mapAttrs (_: x: markdup (sort (fixmate (alignWithRG bwa.align x.name x.files)))) {inherit normal tumour;}; + bowtie-alignments = mapAttrs (_: x: markdup (sort (fixmate (alignWithRG bowtie.align x.name x.files)))) {inherit normal tumour;}; + bwa2-alignments = mapAttrs (_: x: markdup (sort (fixmate (alignWithRG bwa.mem2 x.name x.files)))) {inherit normal tumour;}; variants = callVariants alignments; - octopusSomatic = octopus.callSomatic { } { inherit (alignments) normal; tumours = [ alignments.tumour ]; }; - glvariants = strelka.call { } (builtins.attrValues alignments); - platypusVars = platypus.call { } (builtins.attrValues alignments); - octopusVars = octopus.call { } (builtins.attrValues alignments); - shards = map (x: nameSort (bwa.align { inherit ref; } x)) (shard.fastQPair 2 normal.files); + octopusSomatic = octopus.callSomatic {} { + inherit (alignments) normal; + tumours = [alignments.tumour]; + }; + glvariants = strelka.call {} (builtins.attrValues alignments); + platypusVars = platypus.call {} (builtins.attrValues alignments); + octopusVars = octopus.call {} (builtins.attrValues alignments); + shards = map (x: nameSort (bwa.align {inherit ref;} x)) (shard.fastQPair 2 normal.files); }; tnpairResult = processPair tnpair; cnvkitResults = rec { - cnvs = cnvkit.callCNV { } (with tnpairResult.alignments; { normals = [ normal ]; tumours = [ tumour ]; }); - plot = cnvkit.scatterPlot { } cnvs.out1; + cnvs = cnvkit.callCNV {} (with tnpairResult.alignments; { + normals = [normal]; + tumours = [tumour]; + }); + plot = cnvkit.scatterPlot {} cnvs.out1; }; alignments = { - "bowtie-normal.bam" = bowtie.align { inherit ref; } tnpair.normal.files; - "subread.bam" = subread.align { inherit ref; } tnpair.normal.files; - "last.maf" = lastal.align { inherit ref; } tnpair.normal.files.input1; - "whisper.bam" = whisper.align { inherit ref; } tnpair.normal.files; - "bwa-mem.bam" = bwa.mem { inherit ref; } tnpair.normal.files; - "bwa-mem2.bam" = bwa.mem2 { inherit ref; } tnpair.normal.files; - "minimap2-normal.bam" = minimap2.align { inherit ref; preset = "sr"; } tnpair.normal.files; - "snap-normal.bam" = snap.align { inherit ref; } tnpair.normal.files; + "bowtie-normal.bam" = bowtie.align {inherit ref;} tnpair.normal.files; + "subread.bam" = subread.align {inherit ref;} tnpair.normal.files; + "last.maf" = lastal.align {inherit ref;} tnpair.normal.files.input1; + "whisper.bam" = whisper.align {inherit ref;} tnpair.normal.files; + "bwa-mem.bam" = bwa.mem {inherit ref;} tnpair.normal.files; + "bwa-mem2.bam" = bwa.mem2 {inherit ref;} tnpair.normal.files; + "minimap2-normal.bam" = + minimap2.align { + inherit ref; + preset = "sr"; + } + tnpair.normal.files; + "snap-normal.bam" = snap.align {inherit ref;} tnpair.normal.files; "${tnpair.tumour.name}.flagstat" = flagstat tnpairResult.alignments.tumour; - "hisat2-normal.bam" = hisat2.align { inherit ref; } tnpair.normal.files; + "hisat2-normal.bam" = hisat2.align {inherit ref;} tnpair.normal.files; }; testNaming = linkOutputs { - crai = samtools.index { } (samtools.view { outfmt = types.toCram; } tnpairResult.alignments.tumour); - kallisto = kallisto.quant { inherit ref; } (attrValues tnpair.tumour.files); - facets = facets.callCNV { vcf = tnpairResult.platypusVars; } (with tnpairResult.alignments; [ normal tumour ]); + crai = samtools.index {} (samtools.view {outfmt = types.toCram;} tnpairResult.alignments.tumour); + kallisto = kallisto.quant {inherit ref;} (attrValues tnpair.tumour.files); + facets = facets.callCNV {vcf = tnpairResult.platypusVars;} (with tnpairResult.alignments; [normal tumour]); cnvkit = cnvkitResults.cnvs; "cnvkit.pdf" = cnvkitResults.plot; "octopus.vcf" = tnpairResult.octopusVars; @@ -81,27 +102,45 @@ let strelka-indels = tnpairResult.variants.indels; "strelka.snvs.vcf" = tnpairResult.variants.snvs; "strelka.gl.vcf" = tnpairResult.glvariants; - delly = delly.call { } (with tnpairResult.alignments; [ normal tumour ]); - manta = manta.call { } (with tnpairResult.alignments; { normals = [ normal tumour ]; }); - mantaTN = manta.call { } (with tnpairResult.alignments; { normals = [ normal ]; inherit tumour; }); - mantaT = manta.call { } (with tnpairResult.alignments; { inherit tumour; }); - gridss = gridss.callVariants { } (with tnpairResult.alignments; [ normal tumour ]); - gridss2 = gridss.call (with tnpairResult.alignments; [ normal tumour ]); - gridss3 = gridss.callAndAssemble (with tnpairResult.alignments; [ normal tumour ]); - gridssSomatic = gridss.filterSomatic { normalName = "mysample2"; } (gridss.call (with tnpairResult.alignments; [ normal tumour ])); - "merged-shards.bam" = samtools.merge { } tnpairResult.shards; - "merged.bam" = samtools.merge { } [ tnpairResult.alignments.tumour tnpairResult.alignments.normal ]; - "merged-namesorted.bam" = samtools.merge { } [ (nameSort tnpairResult.alignments.tumour) (nameSort tnpairResult.alignments.normal) ]; - "${tnpair.tumour.name}.cram" = samtools.view { outfmt = types.toCram; } tnpairResult.alignments.tumour; + delly = delly.call {} (with tnpairResult.alignments; [normal tumour]); + manta = manta.call {} (with tnpairResult.alignments; {normals = [normal tumour];}); + mantaTN = manta.call {} (with tnpairResult.alignments; { + normals = [normal]; + inherit tumour; + }); + mantaT = manta.call {} (with tnpairResult.alignments; {inherit tumour;}); + gridss = gridss.callVariants {} (with tnpairResult.alignments; [normal tumour]); + gridss2 = gridss.call (with tnpairResult.alignments; [normal tumour]); + gridss3 = gridss.callAndAssemble (with tnpairResult.alignments; [normal tumour]); + gridssSomatic = gridss.filterSomatic {normalName = "mysample2";} (gridss.call (with tnpairResult.alignments; [normal tumour])); + "merged-shards.bam" = samtools.merge {} tnpairResult.shards; + "merged.bam" = samtools.merge {} [tnpairResult.alignments.tumour tnpairResult.alignments.normal]; + "merged-namesorted.bam" = samtools.merge {} [(nameSort tnpairResult.alignments.tumour) (nameSort tnpairResult.alignments.normal)]; + "${tnpair.tumour.name}.cram" = samtools.view {outfmt = types.toCram;} tnpairResult.alignments.tumour; + "${tnpair.tumour.name}-targeted.cram" = + samtools.view { + outfmt = types.toCram; + targets = "abcd\t10\t100"; + } + tnpairResult.alignments.tumour; "${tnpair.tumour.name}.fastqc.1" = check-fastqc tnpair.tumour.files.input1; "${tnpair.tumour.name}.fastp" = check-fastp tnpair.tumour.files; - snver = snver.call { ploidy = 1; } tnpairResult.alignments; - mosdepth = mosdepth.plot { } { inputs = mapAttrsToList (_: mosdepth.depth { }) tnpairResult.alignments; names = [ "seq1" "seq2" ]; }; - xenomapper = xenomapper.allocate { } { primary = nameSort tnpairResult.alignments.tumour; secondary = nameSort tnpairResult.alignments.tumour; }; + snver = snver.call {ploidy = 1;} tnpairResult.alignments; + mosdepth = mosdepth.plot {} { + inputs = mapAttrsToList (_: mosdepth.depth {}) tnpairResult.alignments; + names = ["seq1" "seq2"]; + }; + mosdepth-targeted = mosdepth.plot {} { + inputs = mapAttrsToList (_: mosdepth.depth {targets = ["abc\t10\t100"];}) tnpairResult.alignments; + names = ["seq1" "seq2"]; + }; + xenomapper = xenomapper.allocate {} { + primary = nameSort tnpairResult.alignments.tumour; + secondary = nameSort tnpairResult.alignments.tumour; + }; quip = with quip; unquip {} (quip {} tnpairResult.alignments.tumour); - genmap = genmap.calcmap { } ref; + genmap = genmap.calcmap {} ref; inherit alignments; }; - in -testNaming + testNaming diff --git a/tools/mosdepth-depth.nix b/tools/mosdepth-depth.nix index 2a72989..4627071 100644 --- a/tools/mosdepth-depth.nix +++ b/tools/mosdepth-depth.nix @@ -1,21 +1,36 @@ -{ bionix -, indexAttrs ? { } -, flags ? null +{ + bionix, + indexAttrs ? {}, + targets ? null, + flags ? null, }: - with bionix; with lib; - -input: - -stage { - name = "mosdepth-depth"; - buildInputs = with pkgs; [ mosdepth ]; - buildCommand = '' - mkdir $out - ln -s ${input} input.bam - ln -s ${bionix.samtools.index indexAttrs input} input.bam.bai - mosdepth -t $NIX_BUILD_CORES ${optionalString (flags != null) flags} $out/out input.bam - ''; - passthru.multicore = true; -} + input: let + handleTarget = x: let + type = builtins.typeOf x; + handler = handlers."${type}" or (builtins.throw "mosdepth-depth:unhandled target type:${type}"); + handlers = { + string = handleTarget [x]; + list = let + file = pkgs.writeText "target.bed" (concatStringsSep "\n" x); + in "-b ${file}"; + path = "-b ${x}"; + set = "-b ${x}"; + }; + in + handler; + in + stage { + name = "mosdepth-depth"; + buildInputs = with pkgs; [mosdepth]; + buildCommand = '' + mkdir $out + ln -s ${input} input.bam + ln -s ${bionix.samtools.index indexAttrs input} input.bam.bai + mosdepth -t $NIX_BUILD_CORES \ + ${optionalString (targets != null) (handleTarget targets)} \ + ${optionalString (flags != null) flags} $out/out input.bam + ''; + passthru.multicore = true; + } diff --git a/tools/samtools-view.nix b/tools/samtools-view.nix index 4d2bdc7..4966b13 100644 --- a/tools/samtools-view.nix +++ b/tools/samtools-view.nix @@ -1,27 +1,63 @@ -{ bionix -, nameSort ? false -, flags ? null -, outfmt ? null -}: - -input: - +{ + bionix, + nameSort ? false, + flags ? null, + outfmt ? null, + targets ? null, +}: input: with bionix; with lib; with types; +assert (matchFiletype "samtools-view" { + bam = _: true; + sam = _: true; + cram = _: true; + } + input); let + handleTarget = x: let + type = builtins.typeOf x; + handler = handlers."${type}" or (builtins.throw "samtools-view:unhandled target type:${type}"); + handlers = { + string = handleTarget [x]; + list = let + file = pkgs.writeText "target.bed" (concatStringsSep "\n" x); + in "--target-file ${file}"; + path = "--target-file ${x}"; + set = "--target-file ${x}"; + }; + in + handler; -assert (matchFiletype "samtools-view" { bam = _: true; sam = _: true; cram = _: true; } input); - -let - outfmtR = if outfmt != null then (if builtins.typeOf outfmt == "string" then { "bam" = toBam; "cram" = toCram; "sam" = toSam; }."${outfmt}" else outfmt) input else input.filetype; - fa = ref: matchFiletype "samtools-view-ref" { fa = _: ref; } ref; - outfmtFlags = matchFiletype "samtools-view-outfmt" { bam = _: "-O BAM"; sam = _: "-O SAM"; cram = x: "-O CRAM -T ${fa x.ref}"; } { filetype = outfmtR; }; + outfmtR = + if outfmt != null + then + ( + if builtins.typeOf outfmt == "string" + then + { + "bam" = toBam; + "cram" = toCram; + "sam" = toSam; + } + ."${outfmt}" + else outfmt + ) + input + else input.filetype; + fa = ref: matchFiletype "samtools-view-ref" {fa = _: ref;} ref; + outfmtFlags = matchFiletype "samtools-view-outfmt" { + bam = _: "-O BAM"; + sam = _: "-O SAM"; + cram = x: "-O CRAM -T ${fa x.ref}"; + } {filetype = outfmtR;}; in -stage { - name = "samtools-view"; - buildInputs = with pkgs; [ samtools ]; - buildCommand = '' - samtools view ${outfmtFlags} ${optionalString (flags != null) flags} ${input} > $out - ''; - passthru.filetype = outfmtR; -} + stage { + name = "samtools-view"; + buildInputs = with pkgs; [samtools]; + buildCommand = '' + samtools view \ + ${optionalString (targets != null) (handleTarget targets)} \ + ${outfmtFlags} ${optionalString (flags != null) flags} ${input} > $out + ''; + passthru.filetype = outfmtR; + } -- cgit v1.2.3