/test-data/rgtestouts/rgManQQ/rgManQQtest1.html

https://bitbucket.org/cistrome/cistrome-harvard/ · HTML · 255 lines · 217 code · 38 blank · 0 comment · 0 complexity · 40a1212c2e931b506f9235a0beb0aa6c MD5 · raw file

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <meta name="generator" content="Galaxy rgManQQ.py tool output - see http://g2.trac.bx.psu.edu/" />
  7. <title></title>
  8. <link rel="stylesheet" href="/static/style/base.css" type="text/css" />
  9. </head>
  10. <body>
  11. <div class="document">
  12. <h1>rgManQQtest1</h1>
  13. <table>
  14. <tr><td><a href="Allelep_manhattan.pdf">Allelep_manhattan.pdf</a></td></tr>
  15. <tr><td><a href="Allelep_manhattan.pdf"><img src="Allelep_manhattan.png" title="Allelep_manhattan.png" hspace="10" width="800"></a></td></tr>
  16. <tr><td><a href="Allelep_qqplot.pdf">Allelep_qqplot.pdf</a></td></tr>
  17. <tr><td><a href="Allelep_qqplot.pdf"><img src="Allelep_qqplot.png" title="Allelep_qqplot.png" hspace="10" width="800"></a></td></tr>
  18. <tr><td><a href="rgManQQtest1.R">rgManQQtest1.R</a></td></tr>
  19. <tr><td><a href="rgManQQtest1.R.log">rgManQQtest1.R.log</a></td></tr>
  20. </table>
  21. <h3>R log follows below</h3><hr><pre>
  22. Loading required package: reshape
  23. Loading required package: plyr
  24. Attaching package: 'reshape'
  25. The following object(s) are masked from 'package:plyr':
  26. rename, round_any
  27. Loading required package: grid
  28. Loading required package: proto
  29. [1] "### 101 values read from /data/tmp/tmpNaxDwH/database/files/000/dataset_1.dat read - now running plots"
  30. [1] "## qqplot on Allelep done"
  31. [1] "## manhattan on Allelep starting 2 3 8"
  32. [1] "## manhattan plot on Allelep done"
  33. ## R script=
  34. # generalised so 3 core fields passed as parameters ross lazarus March 24 2010 for rgenetics
  35. # Originally created as qqman with the following
  36. # attribution:
  37. #--------------
  38. # Stephen Turner
  39. # http://StephenTurner.us/
  40. # http://GettingGeneticsDone.blogspot.com/
  41. # Last updated: 19 July 2011 by Ross Lazarus
  42. # R code for making manhattan plots and QQ plots from plink output files.
  43. # With GWAS data this can take a lot of memory. Recommended for use on
  44. # 64bit machines only, for now.
  45. #
  46. library(ggplot2)
  47. coloursTouse = c('firebrick','darkblue','goldenrod','darkgreen')
  48. # not too ugly but need a colour expert please...
  49. DrawManhattan = function(pvals=Null,chrom=Null,offset=Null,title=NULL, max.y="max",suggestiveline=0, genomewide=T, size.x.labels=9,
  50. size.y.labels=10, annotate=F, SNPlist=NULL,grey=0) {
  51. if (annotate & is.null(SNPlist)) stop("You requested annotation but provided no SNPlist!")
  52. genomewideline=NULL # was genomewideline=-log10(5e-8)
  53. n = length(pvals)
  54. if (genomewide) { # use bonferroni since might be only a small region?
  55. genomewideline = -log10(0.05/n) }
  56. offset = as.integer(offset)
  57. if (n > 1000000) { offset = offset/10000 }
  58. else if (n > 10000) { offset = offset/1000}
  59. chro = as.integer(chrom) # already dealt with X and friends?
  60. pvals = as.double(pvals)
  61. d=data.frame(CHR=chro,BP=offset,P=pvals)
  62. if ("CHR" %in% names(d) & "BP" %in% names(d) & "P" %in% names(d) ) {
  63. d=d[!is.na(d$P), ]
  64. d=d[!is.na(d$BP), ]
  65. d=d[!is.na(d$CHR), ]
  66. #limit to only chrs 1-22, x=23,y=24,Mt=25?
  67. d=d[d$CHR %in% 1:25, ]
  68. d=d[d$P>0 & d$P<=1, ]
  69. d$logp = as.double(-log10(d$P))
  70. dlen = length(d$P)
  71. d$pos=NA
  72. ticks=NULL
  73. lastbase=0
  74. chrlist = unique(d$CHR)
  75. chrlist = as.integer(chrlist)
  76. chrlist = sort(chrlist) # returns lexical ordering
  77. if (max.y=="max") { maxy = ceiling(max(d$logp)) }
  78. else { maxy = max.y }
  79. nchr = length(chrlist) # may be any number?
  80. maxy = max(maxy,1.1*genomewideline)
  81. if (nchr >= 2) {
  82. for (x in c(1:nchr)) {
  83. i = chrlist[x] # need the chrom number - may not == index
  84. if (x == 1) { # first time
  85. d[d$CHR==i, ]$pos = d[d$CHR==i, ]$BP # initialize to first BP of chr1
  86. dsub = subset(d,CHR==i)
  87. dlen = length(dsub$P)
  88. lastbase = max(dsub$pos) # last one
  89. tks = d[d$CHR==i, ]$pos[floor(length(d[d$CHR==i, ]$pos)/2)+1]
  90. lastchr = i
  91. } else {
  92. d[d$CHR==i, ]$pos = d[d$CHR==i, ]$BP+lastbase # one humongous contig
  93. if (sum(is.na(lastchr),is.na(lastbase),is.na(d[d$CHR==i, ]$pos))) {
  94. cat(paste('manhattan: For',title,'chrlistx=',i,'lastchr=',lastchr,'lastbase=',lastbase,'pos=',d[d$CHR==i,]$pos))
  95. }
  96. tks=c(tks, d[d$CHR==i, ]$pos[floor(length(d[d$CHR==i, ]$pos)/2)+1])
  97. lastchr = i
  98. dsub = subset(d,CHR==i)
  99. lastbase = max(dsub$pos) # last one
  100. }
  101. ticklim=c(min(d$pos),max(d$pos))
  102. xlabs = chrlist
  103. }
  104. } else { # nchr is 1
  105. nticks = 10
  106. last = max(d$BP)
  107. first = min(d$BP)
  108. tks = c(first)
  109. t = (last-first)/nticks # units per tick
  110. for (x in c(1:(nticks))) {
  111. tks = c(tks,round(x*t)+first) }
  112. ticklim = c(first,last)
  113. } # else
  114. if (grey) {mycols=rep(c("gray10","gray60"),max(d$CHR))
  115. } else {
  116. mycols=rep(coloursTouse,max(d$CHR))
  117. }
  118. dlen = length(d$P)
  119. d$pranks = rank(d$P)/dlen
  120. d$centiles = 100*d$pranks # small are interesting
  121. d$sizes = ifelse((d$centile < 1),2,1)
  122. if (annotate) d.annotate=d[as.numeric(substr(d$SNP,3,100)) %in% SNPlist, ]
  123. if (nchr >= 2) {
  124. manplot=qplot(pos,logp,data=d, ylab=expression(-log[10](italic(p))) , colour=factor(CHR),size=factor(sizes))
  125. manplot=manplot+scale_x_continuous(name="Chromosome", breaks=tks, labels=xlabs,limits=ticklim)
  126. manplot=manplot+scale_size_manual(values = c(0.5,1.5)) # requires discreet scale - eg factor
  127. #manplot=manplot+scale_size(values=c(0.5,2)) # requires continuous
  128. }
  129. else {
  130. manplot=qplot(BP,logp,data=d, ylab=expression(-log[10](italic(p))) , colour=factor(CHR))
  131. manplot=manplot+scale_x_continuous(name=paste("Chromosome",chrlist[1]), breaks=tks, labels=tks,limits=ticklim)
  132. }
  133. manplot=manplot+scale_y_continuous(limits=c(0,maxy), breaks=1:maxy, labels=1:maxy)
  134. manplot=manplot+scale_colour_manual(value=mycols)
  135. if (annotate) { manplot=manplot + geom_point(data=d.annotate, colour=I("green3")) }
  136. manplot=manplot + opts(legend.position = "none")
  137. manplot=manplot + opts(title=title)
  138. manplot=manplot+opts(
  139. panel.background=theme_blank(),
  140. axis.text.x=theme_text(size=size.x.labels, colour="grey50"),
  141. axis.text.y=theme_text(size=size.y.labels, colour="grey50"),
  142. axis.ticks=theme_segment(colour=NA)
  143. )
  144. if (suggestiveline) manplot=manplot+geom_hline(yintercept=suggestiveline,colour="blue", alpha=I(1/3))
  145. if (genomewideline) manplot=manplot+geom_hline(yintercept=genomewideline,colour="red")
  146. manplot
  147. } else {
  148. stop("Make sure your data frame contains columns CHR, BP, and P")
  149. }
  150. }
  151. qq = function(pvector, title=NULL, spartan=F) {
  152. # Thanks to Daniel Shriner at NHGRI for providing this code for creating expected and observed values
  153. o = -log10(sort(pvector,decreasing=F))
  154. e = -log10( 1:length(o)/length(o) )
  155. # you could use base graphics
  156. # plot(e,o,pch=19,cex=0.25, xlab=expression(Expected~~-log[10](italic(p))),
  157. # ylab=expression(Observed~~-log[10](italic(p))), xlim=c(0,max(e)), ylim=c(0,max(e)))
  158. # lines(e,e,col="red")
  159. #You'll need ggplot2 installed to do the rest
  160. qq=qplot(e,o, xlim=c(0,max(e)), ylim=c(0,max(o))) + stat_abline(intercept=0,slope=1, col="red")
  161. qq=qq+opts(title=title)
  162. qq=qq+scale_x_continuous(name=expression(Expected~~-log[10](italic(p))))
  163. qq=qq+scale_y_continuous(name=expression(Observed~~-log[10](italic(p))))
  164. if (spartan) plot=plot+opts(panel.background=theme_rect(col="grey50"), panel.grid.minor=theme_blank())
  165. qq
  166. }
  167. rgqqMan = function(infile="/data/tmp/tmpNaxDwH/database/files/000/dataset_1.dat",chromcolumn=2, offsetcolumn=3, pvalscolumns=c(8),
  168. title="rgManQQtest1",grey=0) {
  169. rawd = read.table(infile,head=T,sep='\t')
  170. dn = names(rawd)
  171. cc = dn[chromcolumn]
  172. oc = dn[offsetcolumn]
  173. rawd[,cc] = sub('chr','',rawd[,cc],ignore.case = T) # just in case
  174. rawd[,cc] = sub(':','',rawd[,cc],ignore.case = T) # ugh
  175. rawd[,cc] = sub('X',23,rawd[,cc],ignore.case = T)
  176. rawd[,cc] = sub('Y',24,rawd[,cc],ignore.case = T)
  177. rawd[,cc] = sub('Mt',25,rawd[,cc], ignore.case = T)
  178. nams = c(cc,oc) # for sorting
  179. plen = length(rawd[,1])
  180. print(paste('###',plen,'values read from',infile,'read - now running plots',sep=' '))
  181. rawd = rawd[do.call(order,rawd[nams]),]
  182. # mmmf - suggested by http://onertipaday.blogspot.com/2007/08/sortingordering-dataframe-according.html
  183. # in case not yet ordered
  184. if (plen > 0) {
  185. for (pvalscolumn in pvalscolumns) {
  186. if (pvalscolumn > 0)
  187. {
  188. cname = names(rawd)[pvalscolumn]
  189. mytitle = paste('p=',cname,', ',title,sep='')
  190. myfname = chartr(' ','_',cname)
  191. myqqplot = qq(rawd[,pvalscolumn],title=mytitle)
  192. ggsave(filename=paste(myfname,"qqplot.png",sep='_'),myqqplot,width=8,height=6,dpi=96)
  193. ggsave(filename=paste(myfname,"qqplot.pdf",sep='_'),myqqplot,width=8,height=6,dpi=96)
  194. print(paste('## qqplot on',cname,'done'))
  195. if ((chromcolumn > 0) & (offsetcolumn > 0)) {
  196. print(paste('## manhattan on',cname,'starting',chromcolumn,offsetcolumn,pvalscolumn))
  197. mymanplot= DrawManhattan(chrom=rawd[,chromcolumn],offset=rawd[,offsetcolumn],pvals=rawd[,pvalscolumn],title=mytitle,grey=grey)
  198. ggsave(filename=paste(myfname,"manhattan.png",sep='_'),mymanplot,width=8,height=6,dpi=96)
  199. ggsave(filename=paste(myfname,"manhattan.pdf",sep='_'),mymanplot,width=8,height=6,dpi=96)
  200. print(paste('## manhattan plot on',cname,'done'))
  201. }
  202. else {
  203. print(paste('chrom column =',chromcolumn,'offset column = ',offsetcolumn,
  204. 'so no Manhattan plot - supply both chromosome and offset as numerics for Manhattan plots if required'))
  205. }
  206. }
  207. else {
  208. print(paste('pvalue column =',pvalscolumn,'Cannot parse it so no plots possible'))
  209. }
  210. } # for pvalscolumn
  211. } else { print('## Problem - no values available to plot - was there really a chromosome and offset column?') }
  212. }
  213. rgqqMan()
  214. # execute with defaults as substituted
  215. </pre>
  216. <h3><a href="http://rgenetics.org">Rgenetics</a> tool rgManQQ.py run at 04/08/2011 11:46:22</h3>
  217. </div></body></html>