<p>Just wanted to follow up to a question asked at <a href="https://www.biostars.org/p/95062/" rel="nofollow noreferrer">Biostars</a>. My solution and suggestion when dealing with genomic ranges. </p>

<pre><code>library(GenomicRanges)
df1 = data.frame(chr = c("chr1",  "chr12"), start = c(10000,  10000), end = c(20000, 20000))
df2 = data.frame(chr = rep("chr1", 4), posn = c(100, 12000, 15000, 250000), x = rep(1, 4), y = rep(2, 4), z = rep(3, 4))

df1.ir = IRanges(start = df1<span class="math-container">$start, end = df1$</span>end, names = df1<span class="math-container">$chr)
df2.ir = IRanges(start = df2$</span>posn, end = df2<span class="math-container">$posn, names = df2$</span>chr) 

df1.it = NCList(df1.ir)

overlap_it = findOverlaps(df2.ir, df1.it)
print(overlap_it)

# define a genomic interval tree (git) for faster search 

df1.gr = GRanges (IRanges(start = df1<span class="math-container">$start, end = df1$</span>end), seqnames=df1<span class="math-container">$chr) 
df2.gr = GRanges(IRanges(start=df2$</span>posn, end = df2<span class="math-container">$posn), seqnames = df2$</span>chr) 

df1.git  = GNCList(df1.gr)
t1 = Sys.time ()
overlap_git = findOverlaps(df2.gr, df1.git)
t2 = Sys.time()
sub1 = difftime(t2, t1, tz, units = c("auto"))
print (sub1)
print (overlap_git)

t1 = Sys.time ()
overlap_intersect = intersect(df1.gr, df2.gr)
t2 = Sys.time ()
print(overlap_intersect)
sub2 = difftime(t2, t1, tz, units = c("auto"))
print(sub2)
</code></pre>

<ul>
<li>One should be informed about differences between IRanges and GRanges (for example chromosome context); for example, <code>findOverlaps</code> with <code>IRanges</code> returns intersections from both <code>chr1</code> and <code>chr12</code>, but with <code>GRanges</code> it doesn't </li>
<li>Interval Tree based search is way faster (almost 10x) than simple <code>intersect</code> function</li>
</ul>

CC BY-SA 4.0
2018-12-04T00:40:10.653Z
2018-12-04T00:40:10.653Z
satyanarayan rao
<p>I was trying to find a way to find overlaps between two genomic ranges. I found a post at <a href="https://www.biostars.org/p/95062/#95098" rel="nofollow noreferrer">Biostars</a> but it couldn't consider the chromosome information. For example:</p>

<pre><code>library(IRanges)
df1 = data.frame(chr = c("chr1", "chr12"), start = c(10000, 10000), end = c(20000, 20000))
df2 = data.frame(chr = rep("chr1", 4), posn = c(100, 12000, 15000, 250000), x = rep(1, 4), y = rep(2, 4), z = rep(3, 4))

df1.ir = IRanges(start = df1<span class="math-container">$start, end = df1$</span>end, names = df1<span class="math-container">$chr)
df2.ir = IRanges(start = df2$</span>posn, end = df2<span class="math-container">$posn, names = df2$</span>chr) 

df1.it = NCList(df1.ir)

overlap_it = findOverlaps(df2.ir, df1.it)
print(overlap_it) 
</code></pre>

<p>It returns the overlap from both chromosomes. I am wondering if there is a way to keep the chromosome information intact. </p>

<bioconductor><genomicranges>
How to find genomic overlaps faster?
# How to find genomic overlaps faster?

(Asked by: username_0 on 2018-12-03)

I was trying to find a way to find overlaps between two genomic ranges. I found a post at Biostars but it couldn't consider the chromosome information. For example:
library(IRanges)
df1 = data.frame(chr = c("chr1", "chr12"), start = c(10000, 10000), end = c(20000, 20000))
df2 = data.frame(chr = rep("chr1", 4), posn = c(100, 12000, 15000, 250000), x = rep(1, 4), y = rep(2, 4), z = rep(3, 4))

df1.ir = IRanges(start = df1$start, end = df1$end, names = df1$chr)
df2.ir = IRanges(start = df2$posn, end = df2$posn, names = df2$chr) 

df1.it = NCList(df1.ir)

overlap_it = findOverlaps(df2.ir, df1.it)
print(overlap_it) 

It returns the overlap from both chromosomes. I am wondering if there is a way to keep the chromosome information intact.

**Tags:** bioconductor, genomicranges

---

## 1 answers

### Answer by username_0 on 2018-12-04. Score: 3

Just wanted to follow up to a question asked at Biostars. My solution and suggestion when dealing with genomic ranges. 
library(GenomicRanges)
df1 = data.frame(chr = c("chr1",  "chr12"), start = c(10000,  10000), end = c(20000, 20000))
df2 = data.frame(chr = rep("chr1", 4), posn = c(100, 12000, 15000, 250000), x = rep(1, 4), y = rep(2, 4), z = rep(3, 4))

df1.ir = IRanges(start = df1$start, end = df1$end, names = df1$chr)
df2.ir = IRanges(start = df2$posn, end = df2$posn, names = df2$chr) 

df1.it = NCList(df1.ir)

overlap_it = findOverlaps(df2.ir, df1.it)
print(overlap_it)

# define a genomic interval tree (git) for faster search 

df1.gr = GRanges (IRanges(start = df1$start, end = df1$end), seqnames=df1$chr) 
df2.gr = GRanges(IRanges(start=df2$posn, end = df2$posn), seqnames = df2$chr) 

df1.git  = GNCList(df1.gr)
t1 = Sys.time ()
overlap_git = findOverlaps(df2.gr, df1.git)
t2 = Sys.time()
sub1 = difftime(t2, t1, tz, units = c("auto"))
print (sub1)
print (overlap_git)

t1 = Sys.time ()
overlap_intersect = intersect(df1.gr, df2.gr)
t2 = Sys.time ()
print(overlap_intersect)
sub2 = difftime(t2, t1, tz, units = c("auto"))
print(sub2)


One should be informed about differences between IRanges and GRanges (for example chromosome context); for example, findOverlaps with IRanges returns intersections from both chr1 and chr12, but with GRanges it doesn't 
Interval Tree based search is way faster (almost 10x) than simple intersect function


---

