Subset genomic intervals by other intervals
Hello,
I have a bed-like dataframe in R and want to subset it by another bed-like file e.g.:
Bed file 1:
Chr Start End
1 1 10
Bed file2:
Chr Start End
1 5 13
Final bed file:
Chr Start End
1 5 10
So the final bed file should only consist of regions, which are present in both files.
Any suggestions how to do this in R?
Best,
Andreas
• 1,058 views
•
link
1 answer
Use the GenomicRanges library in R. This stuff is what it's all about:
library(GenomicRanges)
a <- data.frame(Chr=1, Start=1, End=10)
b <- data.frame(Chr=1, Start=5, End=13)
# convert to GRanges objects
a <- makeGRangesFromDataFrame(a)
b <- makeGRangesFromDataFrame(b)
# get the common overlapping bits
theIntersection <- intersect(a,b)
# check your results in a genome browser
library(rtracklayer)
export(theIntersection, "intersected_features.bed")
FYI, the result:
> intersect(a,b)
GRanges object with 1 range and 0 metadata columns:
seqnames ranges strand
<Rle> <IRanges> <Rle>
[1] 1 5-10 *
-------
seqinfo: 1 sequence from an unspecified genome; no seqlengths
• 0 views
•
link
Log in to answer this question.