GRanges operations (add, aubtract, multiply, divide)

GRoperator(
  A,
  B,
  col = "score",
  operator = c("+", "-", "*", "/", "^", "%%"),
  ignore.strand = TRUE
)

Arguments

A

an object of GRanges

B

an object of GRanges

col

colname of A and B to be calculated

operator

operator, "+" means A + B, and so on. User-defined function also could be used.

ignore.strand

When set to TRUE, the strand information is ignored in the overlap calculations.

Value

an object of GRanges

Examples

gr2 <- GRanges(seqnames=c("chr1", "chr1"),
ranges=IRanges(c(7,13), width=3),
strand=c("-", "-"), score=3:4)
gr3 <- GRanges(seqnames=c("chr1", "chr1"),
               ranges=IRanges(c(1, 4), c(3, 9)),
               strand=c("-", "-"), score=c(6L, 2L))
GRoperator(gr2, gr3, col="score", operator="+")
#> GRanges object with 4 ranges and 5 metadata columns:
#>       seqnames    ranges strand |     score       gr2    gr2_id       gr3
#>          <Rle> <IRanges>  <Rle> | <numeric> <logical> <numeric> <logical>
#>   [1]     chr1       1-3      * |         6     FALSE        NA      TRUE
#>   [2]     chr1       4-6      * |         2     FALSE        NA      TRUE
#>   [3]     chr1       7-9      * |         5      TRUE         1      TRUE
#>   [4]     chr1     13-15      * |         4      TRUE         2     FALSE
#>          gr3_id
#>       <numeric>
#>   [1]         1
#>   [2]         2
#>   [3]         2
#>   [4]        NA
#>   -------
#>   seqinfo: 1 sequence from an unspecified genome; no seqlengths
GRoperator(gr2, gr3, col="score", operator="-")
#> GRanges object with 4 ranges and 5 metadata columns:
#>       seqnames    ranges strand |     score       gr2    gr2_id       gr3
#>          <Rle> <IRanges>  <Rle> | <numeric> <logical> <numeric> <logical>
#>   [1]     chr1       1-3      * |        -6     FALSE        NA      TRUE
#>   [2]     chr1       4-6      * |        -2     FALSE        NA      TRUE
#>   [3]     chr1       7-9      * |         1      TRUE         1      TRUE
#>   [4]     chr1     13-15      * |         4      TRUE         2     FALSE
#>          gr3_id
#>       <numeric>
#>   [1]         1
#>   [2]         2
#>   [3]         2
#>   [4]        NA
#>   -------
#>   seqinfo: 1 sequence from an unspecified genome; no seqlengths
GRoperator(gr2, gr3, col="score", operator="*")
#> GRanges object with 4 ranges and 5 metadata columns:
#>       seqnames    ranges strand |     score       gr2    gr2_id       gr3
#>          <Rle> <IRanges>  <Rle> | <numeric> <logical> <numeric> <logical>
#>   [1]     chr1       1-3      * |         0     FALSE        NA      TRUE
#>   [2]     chr1       4-6      * |         0     FALSE        NA      TRUE
#>   [3]     chr1       7-9      * |         6      TRUE         1      TRUE
#>   [4]     chr1     13-15      * |         0      TRUE         2     FALSE
#>          gr3_id
#>       <numeric>
#>   [1]         1
#>   [2]         2
#>   [3]         2
#>   [4]        NA
#>   -------
#>   seqinfo: 1 sequence from an unspecified genome; no seqlengths
GRoperator(gr2, gr3, col="score", operator="/")
#> GRanges object with 4 ranges and 5 metadata columns:
#>       seqnames    ranges strand |     score       gr2    gr2_id       gr3
#>          <Rle> <IRanges>  <Rle> | <numeric> <logical> <numeric> <logical>
#>   [1]     chr1       1-3      * |       0.0     FALSE        NA      TRUE
#>   [2]     chr1       4-6      * |       0.0     FALSE        NA      TRUE
#>   [3]     chr1       7-9      * |       1.5      TRUE         1      TRUE
#>   [4]     chr1     13-15      * |       Inf      TRUE         2     FALSE
#>          gr3_id
#>       <numeric>
#>   [1]         1
#>   [2]         2
#>   [3]         2
#>   [4]        NA
#>   -------
#>   seqinfo: 1 sequence from an unspecified genome; no seqlengths
GRoperator(gr2, gr3, col="score", operator=mean)
#> GRanges object with 4 ranges and 5 metadata columns:
#>       seqnames    ranges strand |     score       gr2    gr2_id       gr3
#>          <Rle> <IRanges>  <Rle> | <numeric> <logical> <numeric> <logical>
#>   [1]     chr1       1-3      * |         0     FALSE        NA      TRUE
#>   [2]     chr1       4-6      * |         0     FALSE        NA      TRUE
#>   [3]     chr1       7-9      * |         3      TRUE         1      TRUE
#>   [4]     chr1     13-15      * |         4      TRUE         2     FALSE
#>          gr3_id
#>       <numeric>
#>   [1]         1
#>   [2]         2
#>   [3]         2
#>   [4]        NA
#>   -------
#>   seqinfo: 1 sequence from an unspecified genome; no seqlengths