Skip to content

Instantly share code, notes, and snippets.

View Zepeng-Mu's full-sized avatar
🖥️
Away

Zepeng (Phoenix) Mu Zepeng-Mu

🖥️
Away
View GitHub Profile
@Zepeng-Mu
Zepeng-Mu / ggQQplot.R
Created September 27, 2022 16:23
Plot QQ-plot using ggplot2
ggQQplot <- function(pList, colVec = c("black", "orange", "red", "purple"),
legendLabel = NULL, title = "", openRange = F, sampling = 1) {
if (is.null(legendLabel)) {
legendLabel = colVec
}
if (length(pList) > length(colVec)) {
stop("pList needs to be shorter than colVec!!!")
}
@Zepeng-Mu
Zepeng-Mu / getFDR.R
Created May 4, 2022 15:34
Calculate FDR using test statistics and null statistics, similar to `empPvals()` from qvalue package
getFDR <- function(Stat, Stat0) {
m <- length(Stat)
n <- length(Stat0)
v <- c(rep(T, m), rep(F, n))
v <- v[order(c(Stat, Stat0), decreasing = T)]
vSumT <- (m - cumsum(v == T)[v == T] + 1) / m
vSumF <- (n - cumsum(v == F)[v == T]) / n
fdr <- cummin(pmin(1, vSumF / vSumT))
fdr <- fdr[rank(-Stat)]
return(fdr)
@Zepeng-Mu
Zepeng-Mu / find_loci.R
Created April 21, 2022 20:22
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")