aboutsummaryrefslogtreecommitdiff
path: root/tools/bwa-mem.nix
blob: 84038d9fbb505acfc9ef01c6c884c7ecba5732d6 (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
39
40
41
42
{ bionix
, ref
, bamOutput ? true
, flags ? null
, indexAttrs ? { }
, RG ? { }
}:

{ input1
, input2 ? null
}:

with bionix;
with lib;
with types;
with compression;

let
  fa = f: matchFiletype "bwa-ref" { fa = _: f; } f;
  fq = f: matchFiletype "bwa-input" { fq = _: f; gz = matchFiletype' "bwa-input" { fq = _: f; }; } f;
in
stage {
  name = "bwa-mem";
  buildInputs = with pkgs; [ bwa bc ] ++ optional bamOutput samtools;
  buildCommand = ''
    ln -s ${fa ref} ref.fa
    for f in ${bionix.bwa.index indexAttrs ref}/* ; do
      ln -s $f
    done
    cores=$(echo $NIX_BUILD_CORES ${optionalString bamOutput "- 1"} | bc)
    if [[ $cores -lt 1 ]] ; then
      cores=1
    fi
    bwa mem ${optionalString (flags != null) flags} -t $cores ref.fa ${fq input1} \
      ${optionalString (input2 != null) (fq input2)} \
      ${optionalString (RG ? ID) "-R'@RG\\t${concatMapAttrsStringsSep "\\t" (k: v: "${k}:${v}") RG}'"} \
      ${optionalString bamOutput "| samtools view -b"} \
      > $out
  '';
  passthru.filetype = if bamOutput then filetype.bam { inherit ref; sorting = sort.none { }; } else filetype.sam { inherit ref; sorting = sort.name { }; };
  passthru.multicore = true;
}