aboutsummaryrefslogtreecommitdiff
path: root/tools/samtools-sort.nix
blob: 5a02dab158c79b5d41ac4f4e2af42994fc8a9daf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
{ bionix
, nixpkgs
, nameSort ? false
, flags ? null
, outfmt ? null
}:

input:

with nixpkgs;
with lib;

let
  inherit (bionix.types) matchFiletype coordSort matchFileSorting;
in

assert (matchFiletype "samtools-sort" { bam = _: true; sam = _: true; cram = _: true; } input);

let
  outfmtR = if outfmt != null then outfmt input else input.filetype;
  outFmtFlags = matchFiletype "samtools-sort-outfmt" { bam = _: "-O BAM"; sam = _: "-O SAM"; cram = ref: "-O CRAM -T ${ref}"; } {filetype = outfmtR;};
  alreadySorted = matchFileSorting "samtools-sort" { name = _: nameSort; coord = _: !nameSort; } input;
in stdenv.mkDerivation {
  name = "samtools-sort";
  buildInputs = [ samtools ];
  buildCommand =
    if alreadySorted then
      "ln -s $out ${input}"
    else
      ''
        samtools sort -@ $NIX_BUILD_CORES \
          ${optionalString nameSort "-n"} \
          ${outFmtFlags} \
          ${optionalString (flags != null) flags} \
          ${input} > $out
      '';
  passthru.filetype = if nameSort then bionix.types.nameSort outfmtR else coordSort outfmtR;
}