aboutsummaryrefslogtreecommitdiff
path: root/tools/samtools-view.nix
diff options
context:
space:
mode:
Diffstat (limited to 'tools/samtools-view.nix')
-rw-r--r--tools/samtools-view.nix80
1 files changed, 58 insertions, 22 deletions
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;
+ }