Skip to content

Instantly share code, notes, and snippets.

@npjc
Created April 22, 2015 21:42
Show Gist options
  • Save npjc/89094d38d2b58c96dd6d to your computer and use it in GitHub Desktop.
Save npjc/89094d38d2b58c96dd6d to your computer and use it in GitHub Desktop.
overlap stats
with combined 3-colbed + 4th col. id
```r
library(data.table)
y = data.table(chr=c("Chr1", "Chr1", "Chr1"), start=c(1, 15,1),
end=c(4, 18, 55), id=letters[1:3])
setkey(y, chr, start, end)
dt <- foverlaps(y, y, type="any")
```
then get overlap stats
```r
comn_df <- . %>%
as.data.frame() %>%
filter(id != i.id) %>%
rowwise() %>%
mutate(len_l = end - start,
len_r = i.end - i.start,
len_i = min(end, i.end) - max(start, i.start),
frac_i_l = len_i / len_l,
frac_i_r = len_i / len_r) %>%
arrange(chr,start,end)
```
@npjc
Copy link
Author

npjc commented Apr 22, 2015

quick visual parse

library(dplyr)
library(ggvis)

y = data.table(chr=c("Chr1", "Chr1", "Chr1"), start=c(1, 15,1), 
               end=c(4, 18, 55), id=letters[1:3])

as.data.frame(y) %>% 
    tbl_df() %>% 
    ggvis() %>% 
    layer_rects(y=~id,x=~start,x2=~end,height=band())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment