aboutsummaryrefslogtreecommitdiff
path: root/tools/octopus-callSomatic.nix
blob: df415508835adf4d6f1a699a7482585dfbcb4333 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
{ bionix
, fast ? false
, very-fast ? false
, max-genotypes ? null
, targets ? null
, faidxAttrs ? { }
, indexAttrs ? { }
, flags ? ""
}:

assert !fast || !very-fast;
assert max-genotypes == null || max-genotypes > 0;

with bionix;
with lib;
with types;

{ normal, tumours }:

let
  smScript = pkgs.writeText "smScript.awk" ''
    /^@RG/{
      for(i = 1; i <= NF; i++) {
        n=split($i, fields, ":")
        if(n == 2 && fields[1] == "SM"){
          print fields[2]
          exit
        }
      }
    }
  '';

  inputs = [ normal ] ++ tumours;
  getref = matchFiletype "octopus-callSomatic" { bam = { ref, ... }: ref; cram = { ref, ... }: ref; };
  refs = map getref inputs;
  ref = head refs;

  handleTarget = x:
    let
      type = builtins.typeOf x;
      handler = handlers."${type}" or (builtins.throw "octopus-callSomatic:unhandled target type:${type}");
      handlers = {
        string = "-T '${x}'";
        list =
          let file = pkgs.writeText "regions.txt" (concatStringsSep "\n" x);
          in "-t ${file}";
        path = "-t ${x}";
      };
    in
    handler;

in

assert (length (unique refs) == 1);


stage {
  name = "octopus-callSomatic";
  buildInputs = with pkgs; [ octopus-caller samtools ];
  outputs = [ "out" "evidence" ];
  buildCommand = ''
    ln -s ${ref} ref.fa
    ln -s ${samtools.faidx faidxAttrs ref} ref.fai
    ${concatMapStringsSep "\n" (i: ''
      ln -s ${i} $(basename ${i}).bam
      ln -s ${samtools.index indexAttrs i} $(basename ${i}).bai
    '') inputs}
    normal=$(samtools view -H ${normal} | awk -f ${smScript})
    mkdir $evidence
    octopus -R ref.fa -I *.bam -o $out \
      --bamout $evidence \
      --threads=$NIX_BUILD_CORES \
      ${optionalString fast "--fast"} \
      ${optionalString very-fast "--very-fast"} \
      ${optionalString (max-genotypes != null) "--max-genotypes ${toString max-genotypes}"} \
      ${optionalString (targets != null) (handleTarget targets)} \
      -N $normal \
      ${flags}
  '';
  passthru.filetype = filetype.vcf { inherit ref; };
  passthru.multicore = true;
}