-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmhtplot.v072011.R
More file actions
68 lines (65 loc) · 2.8 KB
/
mhtplot.v072011.R
File metadata and controls
68 lines (65 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Making a Manhattan Plot of a data file
# Need columns with these names "CHR","BP","p","MAF","HWEP"
# Will plot to the standard output. Need to embed this function in pdf(), png(), etc.
# The plot does not have title, but can be added by running title("YOUR TITLE") after running manhattan.plot()
mht.plot <- function(P,CHR,POSITION,gwas.cutoff=5e-8,signal.from=5,plotcolor=c("blue","cyan"),signalcolor="red",limy,chr,...) {
# Recommended plot size: width=1280,height=.5*768
is.missing <- (is.na(CHR) | is.na(P) | is.na(POSITION))
CHR <- CHR[!is.missing]
P <- P[!is.missing]
POSITION <- POSITION[!is.missing]
print(paste(sum(is.missing), "SNPs with missing chromosomal position or P-values will not be plotted"))
if (gwas.cutoff < 1) {
GWAS.cutoff=-log(gwas.cutoff,10)
}
else {
GWAS.cutoff=gwas.cutoff
}
chrlist <- sort(unique(CHR))
chr.table <- data.frame("CHR"=chrlist,"chrMax"=as.numeric(tapply(POSITION,CHR,max)),
"chrMin"=as.numeric(tapply(POSITION,CHR,min)))
chr.table$range <- chr.table$chrMax - chr.table$chrMin
chr.table$start <- 1
offscale <- 20000000
for ( i in 2:nrow(chr.table)) {
chr.table[i,"start"] <- chr.table[i-1,"start"]+chr.table[i-1,"range"]+offscale
}
newscale <- POSITION+chr.table[match(CHR,chr.table$CHR),"start"]-chr.table[match(CHR,chr.table$CHR),"chrMin"]
chr.table$tick <- as.numeric(tapply(newscale,CHR,mean))
nlogp <- -log10(P)
##cat(dim(filtered.result)[1],"\n")
### MAKING PLOT FRAME
if (missing(limy)) limy=ceiling(max(nlogp))
plot(newscale,nlogp,type="n",xlab="Chromosome",ylab="-log10(p-value)",xaxt="n",xlim=c(0,max(newscale)),ylim=c(0,limy)) # ,log="y"
abline(h=GWAS.cutoff,lty="dashed",col="red")
### MAKING PLOT
mycolor <- plotcolor
odd<-chr.table$CHR[as.logical(chr.table$CHR %%2)]
even<-chr.table$CHR[!as.logical(chr.table$CHR %%2)]
cat("Plotting chromosome",odd,"\n")
points(newscale[CHR%in%odd],nlogp[CHR%in%odd],pch=1,cex=.5,lwd=.5,col=mycolor[1])
cat("Plotting chromosome",even,"\n")
points(newscale[CHR%in%even],nlogp[CHR%in%even],pch=1,cex=.5,lwd=.5,col=mycolor[2])
chrlab <- c(1:22,"X","XY","Y","MT")
chr <- length(unique(CHR))
axis(1,at=chr.table$tick[1:chr],label=chrlab[1:chr],las=3)
abline(h=signal.from,lty="dashed",col="darkblue")
myline<-signal.from
}
### Running Interactive Manhattan Plots
INFILE="PHE1.txt"
OUTFILE="MHTPlot_PHE1.pdf"
batch::parseCommandArgs()
if (INFILE == "") {
message("Missing INFILE")
stop('Example: R --vanilla --args INFILE "PHE1.txt" OUTFILE "MHTPlot_PHE1.pdf" < ~/script/R/mhtplot.v072011.R ')
}
result <- read.table(INFILE,header=TRUE,as.is=TRUE)
names(result) <- c("CHR","SNP","POSITION","P")
if ( OUTFILE == "") {
message("Saving plot to MHTPlot.pdf")
OUTFILE="MHTPlot.pdf"
}
png(file=OUTFILE,width=11,height=4,units="in",res=150)
mht.plot(result$P,result$CHR,result$POSITION)
dev.off()