code: def bed_to_interval(orig_bed, bam_file):
      
      with open(orig_bed) as in_handle:
          line = in_handle.readline()
      if line.startswith("@"):
          yield orig_bed
      else:
          with pysam.Samfile(bam_file, "rb") as bam_handle:
              header = bam_handle.text
          with tmpfile(dir=os.path.dirname(orig_bed), prefix="picardbed") as tmp_bed:
              with open(tmp_bed, "w") as out_handle:
                  out_handle.write(header)
                  with open(orig_bed) as in_handle:
                      for i, line in enumerate(in_handle):
                          parts = line.rstrip().split("\t")
                          if len(parts) == 4:
                              chrom, start, end, name = parts
                              strand = "+"
                          elif len(parts) >= 3:
                              chrom, start, end = parts[:3]
                              strand = "+"
                              name = "r%s" % i
                          out = [chrom, start, end, strand, name]
                          out_handle.write("\t".join(out) + "\n")
              yield tmp_bed
docs: Add header and format BED bait and target files for Picard if necessary.