Sam/Bam Manipulation


bamToBed [OPTIONS] -i <BAM>


This tool is part of the bedtools suite.


  • -i : input BAM file


  • -bedpe: Write BAM alignments in BEDPE format. Only one alignment from paired-end reads will be reported. Specifically, it each mate is aligned to the same chromosome, the BAM alignment reported will be the one where the BAM insert size is greater than zero. When the mate alignments are interchromosomal, the lexicographically lower chromosome will be reported first. Lastly, when an end is unmapped, the chromosome and strand will be set to "." and the start and end coordinates will be set to -1. By default, this is disabled and the output will be reported in BED format. This option requires the input BAM file is sorted/grouped by the read name (see samtools sort for more information).
  • -mate1: When writing BEDPE (-bedpe) format, always report mate one as the first BEDPE "block".
  • -bed12: Write "blocked" BED (a.k.a. BED12) format. This will convert "spliced" BAM alignments (denoted by the "N" CIGAR operation) to BED12. Forces -split.
  • -split: Report each portion of a "split" BAM (i.e., having an "N" CIGAR operation) alignment as a distinct BED intervals. This can be useful if you are working with RNA-seq libraries.
  • -splitD: Report each portion of a "split" BAM while obeying noth "N" CIGAR and "D" operation. Forces -split.
  • -ed: Use the "edit distance" tag (NM) for the BED score field. Default for BED is to use mapping quality. Default for BEDPE is to use the minimum of the two mapping qualities for the pair. When -ed is used with -bedpe, the total edit distance from the two mates is reported.
  • -tag: Use other numeric BAM alignment tag for BED score. Default for BED is to use mapping quality. Disallowed with BEDPE output.
  • -color: An R,G,B string for the color used with BED12 format. Default is (255,0,0).
  • -cigar: Add the CIGAR string to the BED entry as a 7th column.


By default, each alignment in the BAM file is converted to a 6 column BED. The BED “name” field is comprised of the RNAME field in the BAM alignment. If mate information is available, the mate (e.g., “/1” or “/2”) field will be appended to the name.

$ bedtools bamtobed -i reads.bam | head -3
chr7   118970079   118970129   TUPAC_0001:3:1:0:1452#0/1   37   -
chr7   118965072   118965122   TUPAC_0001:3:1:0:1452#0/2   37   +
chr11  46769934    46769984    TUPAC_0001:3:1:0:1472#0/1   37   -
Set the score field based on BAM tags

One can override the choice of the BAM MAPQ as the result BED record’s score field by using the -tag option. In the example below, we use the -tag option to select the BAM edit distance (the NM tag) as the score column in the resulting BED records.

$ bedtools bamtobed -i reads.bam -tag NM | head -3
chr7   118970079   118970129   TUPAC_0001:3:1:0:1452#0/1   1    -
chr7   118965072   118965122   TUPAC_0001:3:1:0:1452#0/2   3    +
chr11  46769934    46769984    TUPAC_0001:3:1:0:1472#0/1   1    -
Creating BED12 features from “spliced” BAM entries

bedtools bamtobed will, by default, create a BED6 feature that represents the entire span of a spliced/split BAM alignment. However, when using the -split command, a BED12 feature is reported where BED blocks will be created for each aligned portion of the sequencing read.

Chromosome  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Exons       ***************                                    **********

BED/BAM A      ^^^^^^^^^^^^....................................^^^^

Result      ===============                                    ====


Share your experience or ask a question