Debian Codegraph @

bin/GWAS_manhattanplots

raw ◀ ▶
1 Debian patch touch this file
1 #!/usr/bin/env Rscript
2 #' @(#)GWAS_manhattanplots 2021-01-25 A.Douglas and A.J.Travis
3
4 suppressMessages(library(getopt))
5 suppressMessages(library(optparse))
6
7 #' debug in RStudio
8
9 # command-line arguments
10 opt1 <- make_option(c("-b", "--bh"), action = "store", default = 0)
11 opt2 <- make_option(c("-c", "--chromosome"), action = "store", default = "1:23")
12 opt3 <- make_option(c("-g", "--guide"), action = "store", default = 0)
13 opt4 <- make_option(c("-i", "--input"), action = "store", default = NULL)
14 opt5 <- make_option(c("-o", "--output"), action = "store", default = "manhattan")
15 opt6 <- make_option(c("-q", "--qqplot"), action = "store_true", default = FALSE)
16 opt7 <- make_option(c("-v", "--verbose"), action = "store_true", default = FALSE)
17 opt8 <- make_option(c("-y", "--ymax"), action = "store", default = "max")
18
19 #' debug in RStudio
20 #' args <- c('-b', '0.1', '4', '-q', '-y', '10', '-i', 'files.txt')
21 #' opt <- parse_args(OptionParser(option_list = list(opt1, opt2, opt3, opt4, opt5, opt6,
22 #' opt7, opt8)), args = args, positional_arguments = TRUE)
23
24 opt <- parse_args(OptionParser(option_list = list(opt1, opt2, opt3, opt4, opt5, opt6,
25 opt7, opt8)), positional_arguments = TRUE)
26
27 # limit number of chromosomes
28 chr.name <- parse(text = opt$options$chromosome)
29 chr.limit <- eval(parse(text = chr.name))
30 chr.class <- class(chr.limit)
31 if (chr.class == "numeric") {
32 chr.limit <- as.vector(chr.limit)
33 } else {
34 if (chr.class != "integer") {
35 stop("list of chromosomes required e.g. 1:23")
36 }
37 }
38
39 # PNG output file
40 output.file <- paste(sep = "", opt$options$output, ".png")
41
42 # Benjamini-Hochberg output file
43 bh.file <- paste(sep = "", opt$options$output, ".bh")
44
45 # check usage
46 if (is.null(opt$options$input)) {
47 nplots <- length(opt$args)
48 } else {
49 title <- read.table(opt$options$input, col.names = c("filename", "title"), colClasses = "character",
50 sep = "\t")
51 nplots <- length(title$filename)
52 }
53
54 if (nplots == 0) {
55 stop("one or more '.manh' files required")
56 }
57
58 # script location
59 script <- get_Rscript_filename()
60 basename <- dirname(script)
61
62 # source library
•63 lib.name <- "/usr/lib/R/site-library/pique/GWAS_manhattanplots.R"
64 source(lib.name)
65
66 # Required for Benjamini-Hochberg correction
67 suppressMessages(library(multtest)) # include check if installed
68
69 # sides
70 BOTTOM <- 1
71 LEFT <- 2
72 TOP <- 3
73 RIGHT <- 4
74
75 # outer margins (inches)
76 OM_BOTTOM <- 0
77 OM_LEFT <- 0.4
78 OM_TOP <- 0.2
79 OM_RIGHT <- 0.4
80
81 # inner margins (inches)
82 IM_BOTTOM <- 0
83 IM_LEFT <- 0.4
84 IM_TOP <- 0.2
85 IM_RIGHT <- 0
86 IM_LAST <- 0.8
87
88 # overall plot height (inches)
89 PLOTH <- 1.4
90
91 # Manhattan plot aspect ratio (width / height)
92 MPLOTR <- 5
93
94 # Manhattan plot dimensions (inches)
95 MPLOTH <- IM_TOP + PLOTH + IM_BOTTOM
96 MPLOTW <- IM_LEFT + PLOTH * MPLOTR + IM_RIGHT
97
98 # QQ plot dimensions (inches)
99 QPLOTH <- MPLOTH
100 QPLOTW <- IM_LEFT + PLOTH + IM_RIGHT
101
102 #' markers <- read.table('marker_pos.txt', header = TRUE)
103 #' blocks <- read.table('qtl_blocks.txt', header = TRUE)
104
105 # page layout
106 page.height <- OM_TOP + (MPLOTH * nplots) + IM_LAST + OM_BOTTOM
107 plot.height <- c(rep.int(1, nplots - 1), (MPLOTH + IM_LAST)/MPLOTH)
108 if (opt$options$qqplot) {
109 page.width <- OM_LEFT + MPLOTW + QPLOTW + OM_RIGHT
110 png(output.file, width = page.width, height = page.height, unit = "in", res = 300)
111 layout(mat = matrix(c(1:(nplots * 2)), nplots, 2, byrow = TRUE), widths = c(MPLOTW,
112 QPLOTW), heights = plot.height)
113 } else {
114 page.width <- OM_LEFT + MPLOTW + OM_RIGHT
115 png(output.file, width = page.width, height = page.height, unit = "in", res = 300)
116 layout(mat = matrix(c(1:(nplots)), nplots, 1, byrow = TRUE), heights = plot.height)
117 }
118
119 # outer margins: c(bottom, left, top, right)
120 par(omi = c(OM_BOTTOM, OM_LEFT, OM_TOP, OM_RIGHT), lend = "square")
121
122 # stack plots
123 for (i in 1:nplots) {
124
125 # only show axis on last stacked plot
126 stack <- i < nplots
127 if (stack) {
128 par(mai = c(IM_BOTTOM, IM_LEFT, IM_TOP, IM_RIGHT))
129 } else {
130 par(mai = c(IM_BOTTOM + IM_LAST, IM_LEFT, IM_TOP, IM_RIGHT))
131 }
132
133 # read SNPs from file
134 if (is.null(opt$options$input)) {
135 snp <- read.table(opt$args[i], header = TRUE)
136 } else {
137 snp <- read.table(title$filename[i], header = TRUE)
138 }
139
140 # exclude P <= 0 (error flag?)
141 snp <- snp[snp$P > 0, ]
142
143 # calculate BH (Benjamini-Hochberg) adjusted P values
144 mt <- mt.rawp2adjp(snp$P, proc = "BH")
145 ind <- mt$index
146 snp <- cbind(snp, BH = mt$adjp[order(ind), 2])
147
148 # write BH adjusted P values to file
149 write.table(snp, file = bh.file, sep = "\t", row.names = FALSE, col.names = TRUE)
150
151 # Manhattan plot
152 manhattan(snp, markers = NULL, blocks = NULL, limitchromosomes = chr.limit, ymax = opt$options$ymax,
153 suggestiveline = opt$options$guide, genomewideline = FALSE, highlight = opt$options$bh, stack = stack)
154
155 # sub-plot title (left-justified)
156 if (!is.null(opt$options$input)) {
157 text <- title$title[i]
158 } else {
159 text <- sub(".manh", "", basename(opt$args[i]))
160 }
161 mtext(text, side = TOP, line = -0.8, adj = 0.02, cex = 0.8)
162
163 # QQ plot
164 if (opt$options$qqplot) {
165 qq(snp, limitchromosomes = chr.limit, ymax = opt$options$ymax, highlight = opt$options$bh,
166 stack = stack)
167 }
168 }
169
170 # add axis labels in outer margins
171 mtext(expression(" " - log[10](italic(p))), outer = TRUE, side = LEFT, line = 0)
172
173 # check device was closed
174 if (dev.off() != 1) {
175 stop("error: Graphics device still open")
176 }