This is a test version of Biostars. For the public version, visit https://www.biostars.org.
rtracklayer import.bed adds one to start range

Hello, I have a weird problem while trying to import a bed file as a GRanges object. So my bed file is:

chr1    6780000 7440000 .   0   .
chr1    8430000 8910000 .   0   .
chr1    10560000    10950000    .   0   .
chr1    26910000    27150000    .   0   .
chr1    27600000    27930000    .   0   .
chr1    32040000    32850000    .   0   .
chr1    38460000    39330000    .   0   .
chr1    44670000    45750000    .   0   .

And when I use rtracklayers function import.bed, the GRanges object has the starting coordinate increased by one:

library(rtracklayer)
library(GenomicRanges)

test.bed.gr <- import.bed("Analysis/random/test.bed")
test.bed.gr

GRanges object with 8 ranges and 2 metadata columns:
      seqnames            ranges strand |        name     score
         <Rle>         <IRanges>  <Rle> | <character> <numeric>
  [1]     chr1   6780001-7440000      * |        <NA>         0
  [2]     chr1   8430001-8910000      * |        <NA>         0
  [3]     chr1 10560001-10950000      * |        <NA>         0
  [4]     chr1 26910001-27150000      * |        <NA>         0
  [5]     chr1 27600001-27930000      * |        <NA>         0
  [6]     chr1 32040001-32850000      * |        <NA>         0
  [7]     chr1 38460001-39330000      * |        <NA>         0
  [8]     chr1 44670001-45750000      * |        <NA>         0
rtracklayer import.bed start

1 answer

That is because BED files are 0-based and GRanges objects are 1-based. So it is normal and expected. If you use a dedicated function to write back GRanges to BED on disk then it (the function) should take care of the subtraction of start minus 1 internally. If you use a custom function you have to code that up manually. Off-by-1 errors are super common, so be sure to not fall into that trap.

See: Cheat Sheet For One-Based Vs Zero-Based Coordinate Systems

Thank you! This is really important to know!

Log in to answer this question.