let index_to_bai ~(run_with:Machine.t) ?(check_sorted=true) input_bam =
  begin match input_bam#product#sorting with
  | (Some `Read_name | Nonewhen check_sorted ->
    failwithf "In function Samtools.index_to_bai the input bam %s is not declared as sorted-by-coordinate (samtools-index requires that)"
      input_bam#product#path
  | _ -> ()
  end;
  let product =
    KEDSL.single_file  ~host:(Machine.as_host run_with)
      (sprintf "%s.%s" input_bam#product#path "bai"in
  let make_command src des = ["index""-b"; src] in
  do_on_bam ~run_with input_bam ~product ~make_command
    ~name:(sprintf "Samtools-index %s"
             Filename.(basename input_bam#product#path))