Skip to content

Instantly share code, notes, and snippets.

@mikelove
Created May 10, 2024 12:12
Show Gist options
  • Save mikelove/cbbc84634b6854c865cb5eb08ad9b245 to your computer and use it in GitHub Desktop.
Save mikelove/cbbc84634b6854c865cb5eb08ad9b245 to your computer and use it in GitHub Desktop.
Example of producing simple segmentation
library(plyranges)
library(nullranges)
x <- data.frame(
seqnames=rep(c("1","2","3"), each=10),
start=rep(c(0:4, 10:14) * 1000 + 1, times=3),
width=100) |>
as_granges()
seqlengths(x) <- c("1"=20123, "2"=20123, "3"=20123)
isCircular(x) <- rep(FALSE,3)
genome(x) <- "abcd"
seqinfo(x)
# for real genomic data, tiles on the order of 1 Mb, and larger than block size
tiles <- tileGenome(seqlengths(x), tilewidth=5000, cut.last.tile.in.chrom=TRUE)
tiles <- tiles %>%
mutate(num_olaps = count_overlaps(., x))
cut <- exp(mean(log(tiles$num_olaps+1)))
library(dplyr)
seg <- tiles |>
mutate(state = case_when(
num_olaps > cut ~ 1,
num_olaps <= cut ~ 2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment