let sam_to_bam ~(run_with : Machine.t) ~reference_build file_t =
let open KEDSL in
let samtools = Machine.get_tool run_with Machine.Tool.Default.samtools in
let src = file_t#product#path in
let dest = sprintf "%s.%s" (Filename.chop_suffix src ".sam") "bam" in
let program =
Program.(Machine.Tool.(init samtools)
&& exec ["samtools"; "view"; "-b"; "-o"; dest; src])
in
let name = sprintf "sam-to-bam-%s" (Filename.chop_suffix src ".sam") in
let make = Machine.run_program ~name run_with program in
let host = Machine.(as_host run_with) in
workflow_node ~name
(bam_file ~reference_build dest ~host)
~make
~edges:[
depends_on file_t;
depends_on Machine.Tool.(ensure samtools);
on_failure_activate (Remove.file ~run_with dest);
on_success_activate (Remove.file ~run_with src);
]