aboutsummaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorJustin Bedo <cu@cua0.org>2022-09-26 11:51:52 +1000
committerJustin Bedo <cu@cua0.org>2022-10-24 13:37:57 +1100
commit36a0a03eda2ec8cf9b1f5fea8c437b68638d1de5 (patch)
tree0c87e6f4c815d4cc2d0878acc21b2fe7d968321d /tools
parent81cfa50e6b345942951b68eac0e184ea025f2ae4 (diff)
samtools.view,mosdepth.depth: add targets parameter
Diffstat (limited to 'tools')
-rw-r--r--tools/mosdepth-depth.nix51
-rw-r--r--tools/samtools-view.nix80
2 files changed, 91 insertions, 40 deletions
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;
+ }