Skip to content

Instantly share code, notes, and snippets.

@mikelove
Created June 16, 2023 06:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mikelove/2e899346d92908e6cbe3448705e4b5de to your computer and use it in GitHub Desktop.
Save mikelove/2e899346d92908e6cbe3448705e4b5de 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