Skip to content

Instantly share code, notes, and snippets.

@tbrittoborges
Forked from mikelove/E-P_pairs.R
Created July 31, 2023 08:09
Show Gist options
  • Save tbrittoborges/b68f5d961daf29fae2f4a539a79b6f07 to your computer and use it in GitHub Desktop.
Save tbrittoborges/b68f5d961daf29fae2f4a539a79b6f07 to your computer and use it in GitHub Desktop.
New approach to computing correlations between pairs of overlapping features in terms of data matrices
library(plyranges)
set.seed(1)
x <- data.frame(seqnames=1, start=0:9 * 100 + 1,
width=20, id=1:10) %>%
as_granges()
y <- data.frame(seqnames=1, start=round(runif(4,100,900)),
width=10, id=letters[1:4]) %>%
as_granges() %>%
sort()
tile <- data.frame(seqnames=1, start=c(1,401,801),
end=c(400,800,1000), tile_id=1:3) %>%
as_granges()
set.seed(1)
dat_x <- matrix(rnorm(10*100),nrow=10,dimnames=list(1:10,1:100))
dat_y <- matrix(rnorm(4*100),nrow=4,dimnames=list(letters[1:4],1:100))
x <- x %>% join_overlap_left(tile)
y <- y %>% join_overlap_left(tile)
x_overlaps <- x %>%
join_overlap_inner(y, maxgap=100) %>%
filter(tile_id.x == tile_id.y) %>%
select(tile_id = tile_id.x, id.x, id.y)
library(purrr)
x_overlaps %>%
mutate(
rho = map2_dbl(id.x, id.y, \(.x,.y) {
cor(dat_x[.x,], dat_y[.y,])
}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment