Skip to content

Instantly share code, notes, and snippets.

@Zepeng-Mu
Created April 21, 2022 20:22
Show Gist options
  • Save Zepeng-Mu/2dd7bc093566a07bf9523acbb8400df4 to your computer and use it in GitHub Desktop.
Save Zepeng-Mu/2dd7bc093566a07bf9523acbb8400df4 to your computer and use it in GitHub Desktop.
Getting non-overlapping loci from GWAS for colocalization analysis
disjoinGr <- function(df, chr = "CHR", bp = "BP", pval = "Pval", snp = "SNP", size = 5e5) {
inGr <- GRanges(df[[chr]], IRanges(df[[bp]] - size, df[[bp]] + size - 1),
SNPID = df[[snp]], BP = df[[bp]], Pval = df[[pval]])
disjointGr <- disjoin(inGr)
ov <- findOverlaps(resize(inGr, 1, "center"), disjointGr)
hitGr <- disjointGr[subjectHits(ov)]
ovGr <- setdiff(disjointGr, hitGr)
firstHalf <- resize(ovGr, width(ovGr) / 2 - 1, "start")
secondHalf <- resize(ovGr, width(ovGr) / 2, "end")
cmbGr <- c(hitGr, firstHalf, secondHalf)
cmbGr <- reduce(cmbGr)
ov1 <- findOverlaps(resize(inGr, 1, "center"), cmbGr)
inGr <- inGr[queryHits(ov1)]
outGr <- cmbGr[subjectHits(ov1)]
start(inGr) <- start(outGr)
end(inGr) <- end(outGr)
outDf <- as.data.frame(inGr) %>%
dplyr::rename(CHR = seqnames) %>%
mutate(start = ifelse(start > 0, start, 0))
return(outDf)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment