<p>The GenomicRanges package is a great place to start, something like this:</p>

<pre><code>library(GenomicRanges)

# make a GenomicRanges set corresponding to the genes
genes.gr = makeGRangesFromDataFrame(df = genes, start.field = "start", end.field = "stop", seqnames.field = "chrom")

# turn your windows into a data frame and make a GenomicRanges set from that
windows = ...
windows.gr = makeGRangesFromDataFrame(df = windows ...

# find overlaps between the two sets
olaps = findOverlaps(genes.gr, windows.gr)

# output relevant fields
cat(paste(genes[queryHits(olaps),"gene_id"], windows[subjectHits(olaps),"window_id"]))
</code></pre>

CC BY-SA 4.0
2019-06-17T19:04:33.640Z
2019-07-03T09:51:50.267Z
2019-07-03T09:51:50.267Z
Jay Moore
<p>I am trying to run some frequency based stats to identify selective sweep in my study system using Rpackage "PopGenome".</p>

<p>To proceed with it I have split my genome data (whole genome sequencing) into 5MB chunks</p>

<pre><code>cregions &lt;- character(31)
cregions[1] &lt;- "1-5000000"
cregions[2] &lt;- "5000001-10000000"
cregions[3] &lt;- "10000001-15000000"
cregions[4] &lt;- "15000001-20000000"
cregions[5] &lt;- "20000001-25000000"
cregions[6] &lt;- "25000001-30000000"
cregions[7] &lt;- "30000001-35000000"
....
</code></pre>

<p>I have a bunch of genes and I want to know each of my genes falls within the range of which intervals in the above list.</p>

<p>these are my genes:</p>

<pre><code>chrom start stop gene_id
Chr01 107310787 107323633 gene2748
Chr01 113789595 113794851 gene2894
Chr01 159041389 159068535 gene4806
Chr02 2742629 2758060 gene4846 
</code></pre>

<p>I'd like to get an out put like this</p>

<pre><code>gene2748  window_2
gene2894  window_3
...
</code></pre>

<p>Any idea how I can do it in R?</p>

<r><genomicranges><interval>
Find intervals that genes fall within their range
# Find intervals that genes fall within their range

(Asked by: username_0 on 2019-06-17)

I am trying to run some frequency based stats to identify selective sweep in my study system using Rpackage "PopGenome".
To proceed with it I have split my genome data (whole genome sequencing) into 5MB chunks
cregions <- character(31)
cregions[1] <- "1-5000000"
cregions[2] <- "5000001-10000000"
cregions[3] <- "10000001-15000000"
cregions[4] <- "15000001-20000000"
cregions[5] <- "20000001-25000000"
cregions[6] <- "25000001-30000000"
cregions[7] <- "30000001-35000000"
....

I have a bunch of genes and I want to know each of my genes falls within the range of which intervals in the above list.
these are my genes:
chrom start stop gene_id
Chr01 107310787 107323633 gene2748
Chr01 113789595 113794851 gene2894
Chr01 159041389 159068535 gene4806
Chr02 2742629 2758060 gene4846 

I'd like to get an out put like this
gene2748  window_2
gene2894  window_3
...

Any idea how I can do it in R?

**Tags:** r, genomicranges, interval

---

## 1 answers

### Answer by username_1 on 2019-06-17. Score: 8 | Accepted answer

The GenomicRanges package is a great place to start, something like this:
library(GenomicRanges)

# make a GenomicRanges set corresponding to the genes
genes.gr = makeGRangesFromDataFrame(df = genes, start.field = "start", end.field = "stop", seqnames.field = "chrom")

# turn your windows into a data frame and make a GenomicRanges set from that
windows = ...
windows.gr = makeGRangesFromDataFrame(df = windows ...

# find overlaps between the two sets
olaps = findOverlaps(genes.gr, windows.gr)

# output relevant fields
cat(paste(genes[queryHits(olaps),"gene_id"], windows[subjectHits(olaps),"window_id"]))


---

