aboutsummaryrefslogtreecommitdiff
path: root/tools/samtools-merge.nix
blob: ff8ea00a4359c733d694fa9487747ae3ec828f04 (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
{ bionix
, flags ? null
, outfmt ? null
}:

inputs:

with bionix;
with lib;

let
  inherit (bionix.types) matchFiletype matchSorting;
  inputIsHomogenous = length (unique (map (matchFiletype "samtools-merge" { bam = x: x // { sorting = matchSorting "samtools-merge" { coord = _: "coord"; name = _: "name"; } x; }; }) inputs)) == 1;
  nameSorted = matchFiletype "samtools-merge" { bam = matchSorting "samtools-merge" { coord = _: false; name = _: true; }; } (lib.head inputs);
in

assert inputIsHomogenous;

if length inputs == 1 then head inputs else

stage {
  name = "samtools-merge";
  buildInputs = with pkgs; [ samtools ];
  buildCommand = ''
    samtools merge ${optionalString (flags != null) flags} \
      ${if nameSorted then "-n" else ""} \
      out.bam ${concatStringsSep " " inputs}

    # Merge is non-deterministic with PG lines; if files have clashing PG IDs then a random
    # suffix is appended to make it unique. PG lines are stripped in the following to
    # resolve the issue.
    samtools reheader <(samtools view -H out.bam | grep -v '@PG') out.bam > $out
  '';
  passthru.filetype = (builtins.elemAt inputs 0).filetype;
}