let index
    ~reference_build
    ~(run_with : Machine.t) =
  let open KEDSL in
  let reference_fasta =
    Machine.get_reference_genome run_with reference_build
    |> Reference_genome.fasta in
  let mosaik_tool = Machine.get_tool run_with Machine.Tool.Default.mosaik in
  let name =
    sprintf "mosaik-build-%s" (Filename.basename reference_fasta#product#path) in
  let index_result = sprintf "%s.mosaik.dat" reference_fasta#product#path in
  let jump_file_result = sprintf "%s.mosaik-index" reference_fasta#product#path in
  let mosaik_tmp_dir = (Filename.dirname reference_fasta#product#path) // "mosaik-tmp" in
  workflow_node ~name
    (single_file ~host:(Machine.(as_host run_with)) index_result)
    ~edges:[
      on_failure_activate (Remove.file ~run_with jump_file_result);
      on_failure_activate (Remove.file ~run_with index_result);
      depends_on reference_fasta;
      depends_on Machine.Tool.(ensure mosaik_tool);
    ]
    ~tags:[Target_tags.aligner]
    ~make:(Machine.run_big_program run_with ~name
             ~self_ids:["mosaik""index"]
             Program.(
               Machine.Tool.(init mosaik_tool)
               && shf "mkdir -p %s" mosaik_tmp_dir
               && shf "export MOSAIK_TMP=%s" mosaik_tmp_dir 
               (* Command to build basic MOSAIK reference file *)
               && shf "MosaikBuild -fr %s -oa %s"
                 reference_fasta#product#path
                 index_result
               (* Command to build MOSAIK index file *)
               && shf "MosaikJump -ia %s -hs 15 -out %s"
                 index_result
                 jump_file_result
             ))