<p><a href="https://www.rdocumentation.org/packages/GenomicRanges/versions/1.24.1/topics/findOverlaps-methods" rel="noreferrer"><code>GenomicRanges::countOverlaps</code></a> seems to be what you’re after:</p>

<pre><code>position_range = GRanges(position$chrom, IRanges(position, position, width = 1))
ranges_at_position = countOverlaps(position_ranges, granges)
</code></pre>

CC BY-SA 3.0
2017-06-13T14:04:02.240Z
2017-06-13T14:04:02.240Z
Konrad Rudolph
<p>I have a set of genomic ranges that are potentially overlapping. I want to count the amount of ranges at certain positions using R. </p>

<p>I'm Pretty sure there are good solutions, but I seem to be unable to find them. </p>

<p>Solutions like <a href="https://stackoverflow.com/questions/26140151/binning-two-vectors-of-different-ranges-using-r?rq=1">cut</a> or <a href="https://stackoverflow.com/questions/11963508/generate-bins-from-a-data-frame">findIntervals</a> don't achieve what I want as they only count on one vector or accumulate by <code>all values &lt;= break</code>.</p>

<p>Also <code>countMatches {GenomicRanges}</code> doesn't seem to cover it.</p>

<p>Probably one could use Bedtools, but I don't want to leave R.</p>

<p>I could only come up with a hilariously slow solution</p>

<pre><code># generate test data
testdata &lt;- data.frame(chrom = rep(seq(1,10),10),
                       starts = abs(rnorm(100, mean = 1, sd = 1)) * 1000,
                       ends = abs(rnorm(100, mean = 2, sd = 1)) * 2000)

# make sure that all end coordinates are bigger than start
# this is a requirement of the original data
testdata &lt;- testdata[testdata$ends - testdata$starts &gt; 0,]

# count overlapping ranges on certain positions
count.data &lt;- lapply(unique(testdata$chrom), function(chromosome){
    tmp.inner &lt;- lapply(seq(1,10000, by = 120), function(i){
        sum(testdata$chrom == chromosome &amp; testdata$starts &lt;= i &amp; testdata$ends &gt;= i)
    })
    return(unlist(tmp.inner))
})

# generate a data.frame containing all data
df.count.data &lt;- ldply(count.data, rbind)

# ideally the chromosome will be columns and not rows
t(df.count.data)
</code></pre>

CC BY-SA 3.0
2017-06-13T13:58:26.227Z
So you only want overlaps where the start position is not contained within your interval range? What's the actual biological context to this?
Devon Ryan
CC BY-SA 3.0
2017-06-13T14:02:53.990Z
You could start by optimizing the code by using data.table (and probably using vapply instead of lapply). Also if one nucleotide overlaps between ranges then you count that too?
llrs
CC BY-SA 3.0
2017-06-13T14:55:14.907Z
@DevonRyan no what i want is that the respective counting position is with in the range, which is a genomic deletion (hence the biological context)
sargas
CC BY-SA 3.0
2017-06-13T14:56:54.760Z
@Llopis yes i want that counted as well. The list contains overlapping areas and i want to know how many of them are overlapping a certain position. Which is then used later for plotting.
sargas
CC BY-SA 3.0
2017-06-13T19:27:59.960Z
Hi sargas, thanks for your question and welcome to Bioinformatics Stack Exchange. If you have any additional story/context associated with your question (such as a desire to count genomic deletions), it can be helpful to include that in your question. This makes it easier to understand the question, and helps answerers to solve the problem you have rather than the question you're asking (see the [XY problem](http://xyproblem.info/) for more information).
gringer
<r>
Count genomic ranges
# Count genomic ranges

(Asked by: username_0 on 2017-06-13)

I have a set of genomic ranges that are potentially overlapping. I want to count the amount of ranges at certain positions using R. 
I'm Pretty sure there are good solutions, but I seem to be unable to find them. 
Solutions like cut or findIntervals don't achieve what I want as they only count on one vector or accumulate by all values <= break.
Also countMatches {GenomicRanges} doesn't seem to cover it.
Probably one could use Bedtools, but I don't want to leave R.
I could only come up with a hilariously slow solution
# generate test data
testdata <- data.frame(chrom = rep(seq(1,10),10),
                       starts = abs(rnorm(100, mean = 1, sd = 1)) * 1000,
                       ends = abs(rnorm(100, mean = 2, sd = 1)) * 2000)

# make sure that all end coordinates are bigger than start
# this is a requirement of the original data
testdata <- testdata[testdata$ends - testdata$starts > 0,]

# count overlapping ranges on certain positions
count.data <- lapply(unique(testdata$chrom), function(chromosome){
    tmp.inner <- lapply(seq(1,10000, by = 120), function(i){
        sum(testdata$chrom == chromosome & testdata$starts <= i & testdata$ends >= i)
    })
    return(unlist(tmp.inner))
})

# generate a data.frame containing all data
df.count.data <- ldply(count.data, rbind)

# ideally the chromosome will be columns and not rows
t(df.count.data)

**Tags:** r
> **Comments:**
> **username_1**: So you only want overlaps where the start position is not contained within your interval range? What's the actual biological context to this?  
> **username_2**: You could start by optimizing the code by using data.table (and probably using vapply instead of lapply). Also if one nucleotide overlaps between ranges then you count that too?  
> **username_0**: @DevonRyan no what i want is that the respective counting position is with in the range, which is a genomic deletion (hence the biological context)  
> **username_0**: @Llopis yes i want that counted as well. The list contains overlapping areas and i want to know how many of them are overlapping a certain position. Which is then used later for plotting.  
> **username_3**: Hi sargas, thanks for your question and welcome to Bioinformatics Stack Exchange. If you have any additional story/context associated with your question (such as a desire to count genomic deletions), it can be helpful to include that in your question. This makes it easier to understand the question, and helps answerers to solve the problem you have rather than the question you're asking (see the [XY problem](http://xyproblem.info/) for more information).  

---

## 1 answers

### Answer by username_4 on 2017-06-13. Score: 6 | Accepted answer

GenomicRanges::countOverlaps seems to be what you’re after:
position_range = GRanges(position$chrom, IRanges(position, position, width = 1))
ranges_at_position = countOverlaps(position_ranges, granges)


---

