Apply intersect operation between two tables

intersect_bed(
  x,
  y,
  mode = c("default", "exclude", "wa", "wb", "wo", "unique", "loj"),
  max_gap = -1L,
  min_overlap = 0L,
  min_overlap_type = c("bp", "frac1", "frac2")
)

Arguments

x

A GRanges object.

y

A GRanges object.

mode

Mode of the intersect operation. Can be one of the following:

default: Similar to bedtools intersect's default behavior.

exclude: Return features in x that do not overlap with y. Similar to bedtools intersect's -u argument.

wa: Write the original entry in x for each overlap. Similar to bedtools intersect's -wa argument.

wb: Write the original entry in y for each overlap. Useful for knowing what A overlaps. Similar to bedtools intersect's -wb argument.

wo: Write the original x and y entries plus the number of base pairs of overlap between the two features. Only A features with overlap are reported.

unique: Write original x entry once if any overlaps found in y. In other words, just report the fact at least one overlap was found in y. Similar to bedtools intersect's -u argument.

loj: Perform a “left outer join”. That is, for each feature in x report each overlap with j. If no overlaps are found, report a NULL feature for B.

max_gap

The largest gap for two intervals to be considered as overlapping. Default is -1 (no gap allowed, adjacent intervals not allowd).

min_overlap

The smallest overlapping region for two intervals to be considered as overlapping. Default is 0.

min_overlap_type

A character value indicating how min_overlap is interpreted. bp means min_overlap is the number of base pairs. frac1 means min_overlap is the minimum overlap required as a fraction of x. Similarly, frac2 means min_overlap is the minimum overlap required as a fraction of y. Similar to bedtools intersect's -f and -F arguments.

Value

A GRanges representing the intersection of x and y

References

Manual page of bedtools intersect: https://bedtools.readthedocs.io/en/latest/content/tools/intersect.html

See also

Examples

# Load BED tables
tbl_x <- read_bed(system.file("extdata", "example_merge.bed", package = "bedtorch"), use_gr = FALSE)
tbl_y <- read_bed(system.file("extdata", "example_intersect_y.bed", package = "bedtorch"), use_gr = FALSE)

# Basic usages
result <- intersect_bed(tbl_x, tbl_y)
head(result)
#>    chrom start end score
#> 1:    21    22  25     7
#> 2:    21    26  30     7
#> 3:    21    29  35     9
#> 4:    21    47  49     1
#> 5:    21    47  50     2
#> 6:    21    53  55     5
#> -------
#> genome: unspecified.

# Exclude regions defined by tbl_y from tbl_x
result <- intersect_bed(tbl_x, tbl_y, mode = "exclude")
head(result)
#>    chrom start end score
#> 1:    21     3   6     8
#> 2:    21    10  16     5
#> 3:    21    12  17     4
#> 4:    21    15  22     5
#> 5:    22    27  36     3
#> 6:    22    30  33    10
#> -------
#> genome: unspecified.

# For each overlap, return the original entries in tbl_x. For a interval in
# tbl_x, it is considered as overlapping only if 50% of it overlaps with an
# interval in tbl_y.
result <- intersect_bed(tbl_x, tbl_y, mode = "wa", min_overlap = 0.5, min_overlap_type = "frac1")
head(result)
#>    chrom start end score
#> 1:    21    22  25     7
#> 2:    21    26  30     7
#> 3:    21    29  35     9
#> 4:    21    45  50     2
#> 5:    21    53  56     5
#> 6:    22    42  44     9
#> -------
#> genome: unspecified.

# For each overlap, return the original entries in both tbl_x and tbl_y, plus
# the number of overlapping base pairs. The minimum range for two intervals to
# be considered as overlapping is 5bp
result <- intersect_bed(tbl_x, tbl_y, mode = "wa", min_overlap = 5, min_overlap_type = "bp")
head(result)
#>    chrom start end score
#> 1:    21    29  35     9
#> 2:    22    51  59     9
#> -------
#> genome: unspecified.