Hi everyone,

I have used the "findOverlaps" function in R to find which positions of my two datasets overlap. I have also used "countOverlaps" to see how many overlaps I have.

What I want to do now, is to find the exact coordinates which overlapped. I was looking how to do it using findOverlaps but it has no such options and by doing a google search, I couldn't find much help.

A little help on this would be greatly appreciated.

Thank you
I have done this with dummy data.

Lets say we have we have regions A (groupA) and regions B (groupB).

    groupA <- data.table(
        chr = rep("chr1", 5),
        start = seq(0, 1000, 100),
        end = seq(50, 1050, 100))
    setkey(groupA, chr, start, end)
    
    groupB <- data.table(
        chr = rep("chr1", 5),
        start = seq(25, 1025, 100),
        end = seq(75, 1075, 100))
    setkey(groupB, chr, start, end)

Check:

 1. If your datasets are data.table `class(groupA)`, if not do `setDT(groupA)`
 2. If keys are chr start end, if not do `setkey(groupA, chr, start, end)`

</ol>

    # Find overlaps
    over <- foverlaps(groupA, groupB, nomatch = 0)
    # Extract exact regions
    over2 <- data.table(
        chr = over$chr,
        start = over[, ifelse(start > i.start, start, i.start)],
        end = over[, ifelse(end < i.end, end, i.end)])