let mpileup ~run_with ?adjust_mapq ~region input_bam =
let open KEDSL in
let samtools = Machine.get_tool run_with Machine.Tool.Default.samtools in
let src = input_bam#product#path in
let adjust_mapq_option =
match adjust_mapq with | None -> "" | Some n -> sprintf "-C%d" n in
let samtools_region_option = Region.to_samtools_option region in
let reference_genome =
Machine.get_reference_genome run_with input_bam#product#reference_build in
let fasta = Reference_genome.fasta reference_genome in
let pileup =
Filename.chop_suffix src ".bam" ^
sprintf "-%s%s.mpileup" (Region.to_filename region) adjust_mapq_option
in
let sorted_bam =
sort_bam_if_necessary ~run_with input_bam ~by:`Coordinate in
let program =
Program.(
Machine.Tool.(init samtools)
&& shf
"samtools mpileup %s %s -Bf %s %s > %s"
adjust_mapq_option samtools_region_option
fasta#product#path
sorted_bam#product#path
pileup
) in
let name =
sprintf "samtools-mpileup-%s" Filename.(basename pileup |> chop_extension)
in
let make = Machine.run_program ~name run_with program in
let host = Machine.(as_host run_with) in
let edges = [
depends_on Machine.Tool.(ensure samtools);
depends_on sorted_bam;
depends_on fasta;
index_to_bai ~run_with sorted_bam |> depends_on;
on_failure_activate (Remove.file ~run_with pileup);
] in
workflow_node ~name (single_file pileup ~host) ~make ~edges