Define helper function for flexibly shifting an anchor

shiftAnchor(a, p)

Arguments

a

GRanges object

p

Position within anchors to resize the bin. Can be a character or integer vector of length 1 or length(a) designating the position for each element in bedpe. Character options are "start", "end" and "center". Integers are referenced from the start position for '+' and '*' strands and from the end position for the '-' strand.

Value

GRanges object with a single position range that has been shifted appropriately.

Examples

library(GenomicRanges)

## Create example GRanges
gr1 <- GRanges(seqnames = "chr1",
               ranges = IRanges(start = rep(5000,3), end = rep(6000,3)),
               strand = c('+', '-', '*'))

gr2 <- gr1 |> promoters(upstream = 2000, downstream = 200)

## Shifting anchors by keyword
shiftAnchor(gr1, 'start')
#> Error in shiftAnchor(gr1, "start"): could not find function "shiftAnchor"
shiftAnchor(gr1, 'end')
#> Error in shiftAnchor(gr1, "end"): could not find function "shiftAnchor"
shiftAnchor(gr1, 'center')
#> Error in shiftAnchor(gr1, "center"): could not find function "shiftAnchor"
# shiftAnchor(gr1, 'blah') error

## Shifting anchors by position
shiftAnchor(gr1, 100)
#> Error in shiftAnchor(gr1, 100): could not find function "shiftAnchor"
shiftAnchor(gr1, c(100, 200, 300))
#> Error in shiftAnchor(gr1, c(100, 200, 300)): could not find function "shiftAnchor"
# shiftAnchor(gr1, c(100, 200, 300, 400)) error
# shiftAnchor(gr1, c(100, 200)) error

## Shifting back to TSS
shiftAnchor(gr2, 2000)
#> Error in shiftAnchor(gr2, 2000): could not find function "shiftAnchor"