0% found this document useful (0 votes)
4 views

Manhattan plot of GWAS results_codes

The document provides R code for generating Manhattan and QQ plots for Genome-Wide Association Study (GWAS) results using the 'qqman' and 'lattice' libraries. It includes data manipulation steps such as merging datasets and performing statistical corrections for population stratification. The code also includes error checking and customization options for the plots.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Manhattan plot of GWAS results_codes

The document provides R code for generating Manhattan and QQ plots for Genome-Wide Association Study (GWAS) results using the 'qqman' and 'lattice' libraries. It includes data manipulation steps such as merging datasets and performing statistical corrections for population stratification. The code also includes error checking and customization options for the plots.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

# Manhattan plot of GWAS results

View(per)
str(per)
as.data.frame(per)

install.packages("qqman")
library(qqman)

manhattan(per, chr = "CHR", bp = "BP", p = "P", snp = "SNP", col = c("chartreuse",


"blue4"), chrlabs = NULL, suggestiveline = -log10(2.59e-06), genomewideline = -
log10(1.29e-06), highlight = NULL, logp = TRUE, ylim = c(0, 16))

# qqplot with confidence interval

library(lattice)
qqunif.plot<-function(pvalues,
should.thin=T, thin.obs.places=2, thin.exp.places=2,
xlab=expression(paste("Expected (",-log[10], " p-value)")),
ylab=expression(paste("Observed (",-log[10], " p-value)")),
draw.conf=TRUE, conf.points=1000, conf.col="lightgray", conf.alpha=.05,
already.transformed=FALSE, pch=20, aspect="iso",
prepanel=prepanel.qqunif,
par.settings=list(superpose.symbol=list(pch=pch)), ...) {

#error checking
if (length(pvalues)==0) stop("pvalue vector is empty, can't draw plot")
if(!(class(pvalues)=="numeric" ||
(class(pvalues)=="list" && all(sapply(pvalues,
class)=="numeric"))))
stop("pvalue vector is not numeric, can't draw plot")
if (any(is.na(unlist(pvalues)))) stop("pvalue vector contains NA
values, can't draw plot")
if (already.transformed==FALSE) {
if (any(unlist(pvalues)==0)) stop("pvalue vector contains
zeros, can't draw plot")
} else {
if (any(unlist(pvalues)<0)) stop("-log10 pvalue vector contains
negative values, can't draw plot")
}

grp<-NULL
n<-1
exp.x<-c()
if(is.list(pvalues)) {
nn<-sapply(pvalues, length)
rs<-cumsum(nn)
re<-rs-nn+1
n<-min(nn)
if (!is.null(names(pvalues))) {
grp=factor(rep(names(pvalues), nn),
levels=names(pvalues))
names(pvalues)<-NULL
} else {
grp=factor(rep(1:length(pvalues), nn))
}
pvo<-pvalues
pvalues<-numeric(sum(nn))
exp.x<-numeric(sum(nn))
for(i in 1:length(pvo)) {
if (!already.transformed) {
pvalues[rs[i]:re[i]] <- -log10(pvo[[i]])
exp.x[rs[i]:re[i]] <- -log10((rank(pvo[[i]],
ties.method="first")-.5)/nn[i])
} else {
pvalues[rs[i]:re[i]] <- pvo[[i]]
exp.x[rs[i]:re[i]] <- -log10((nn[i]+1-
rank(pvo[[i]], ties.method="first")-.5)/(nn[i]+1))
}
}
} else {
n <- length(pvalues)+1
if (!already.transformed) {
exp.x <- -log10((rank(pvalues,
ties.method="first")-.5)/n)
pvalues <- -log10(pvalues)
} else {
exp.x <- -log10((n-rank(pvalues,
ties.method="first")-.5)/n)
}
}

#this is a helper function to draw the confidence interval


panel.qqconf<-function(n, conf.points=1000, conf.col="gray",
conf.alpha=.05, ...) {
require(grid)
conf.points = min(conf.points, n-1);
mpts<-matrix(nrow=conf.points*2, ncol=2)
for(i in seq(from=1, to=conf.points)) {
mpts[i,1]<- -log10((i-.5)/n)
mpts[i,2]<- -log10(qbeta(1-conf.alpha/2, i, n-i))
mpts[conf.points*2+1-i,1]<- -log10((i-.5)/n)
mpts[conf.points*2+1-i,2]<- -log10(qbeta(conf.alpha/2,
i, n-i))
}
grid.polygon(x=mpts[,1],y=mpts[,2], gp=gpar(fill=conf.col, lty=0),
default.units="native")
}
#reduce number of points to plot
if (should.thin==T) {
if (!is.null(grp)) {
thin <- unique(data.frame(pvalues = round(pvalues,
thin.obs.places),
exp.x = round(exp.x, thin.exp.places),
grp=grp))
grp = thin$grp
} else {
thin <- unique(data.frame(pvalues = round(pvalues,
thin.obs.places),
exp.x = round(exp.x, thin.exp.places)))
}
pvalues <- thin$pvalues
exp.x <- thin$exp.x
}
gc()

prepanel.qqunif= function(x,y,...) {
A = list()
A$xlim = range(x, y)*1.02
A$xlim[1]=0
A$ylim = A$xlim
return(A)
}

#draw the plot


xyplot(pvalues~exp.x, groups=grp, xlab=xlab, ylab=ylab, aspect=aspect,
prepanel=prepanel, scales=list(axs="i"), pch=pch,
panel = function(x, y, ...) {
if (draw.conf) {
panel.qqconf(n, conf.points=conf.points,
conf.col=conf.col,
conf.alpha=conf.alpha)
};
panel.xyplot(x,y, ...);
panel.abline(0,1);
}, par.settings=par.settings, ...
)
}

qqunif.plot(per$P)

# Association after correction for stratification of population

> library(readxl)
> perl <- read_excel("perl.xlsx")
> View(perl)
> library(readxl)
> perl1 <- read_excel("perl1.xlsx")
> View(perl1)
> b = merge(perl1, perl, by = "SNP")
> head(b)
SNP CHR.x BP A1 TEST NMISS BETA STAT P
1 NC_037545.1:100014751[M96] 1 100014751 1 ADD 95 0.20710 1.3240 0.1887
2 NC_037545.1:100014751[M96] 1 100014751 1 COV1 95 -0.16930 -1.0830 0.2817
3 NC_037545.1:100014857[M96] 1 100014857 1 ADD 95 -0.10730 -0.8586 0.3928
4 NC_037545.1:100014857[M96] 1 100014857 1 COV1 95 -0.07356 -0.5885 0.5576
5 NC_037545.1:100105376[M96] 1 100105376 1 ADD 96 -0.01618 -0.1290 0.8976
6 NC_037545.1:100105376[M96] 1 100105376 1 COV1 96 -0.02800 -0.2232 0.8238
CHR.y UNADJ GC BONF HOLM SIDAK_SS SIDAK_SD FDR_BH FDR_BY
1 1 0.1887 0.1956 1 1 1 1 0.9837 1
2 1 0.1887 0.1956 1 1 1 1 0.9837 1
3 1 0.3928 0.4002 1 1 1 1 0.9862 1
4 1 0.3928 0.4002 1 1 1 1 0.9862 1
5 1 0.8976 0.8992 1 1 1 1 0.9978 1
6 1 0.8976 0.8992 1 1 1 1 0.9978 1
> manhattan(b, chr = "CHR.x", bp = "BP", p = "GC", snp = "SNP",
+ col = c("chartreuse", "blue4"), chrlabs = NULL,
+ suggestiveline = -log10(2.59e-06), genomewideline = -log10(1.29e-06),
+ highlight = NULL, logp = TRUE, ylim = c(0, 16))
>
> qqunif.plot(b$GC)

You might also like