-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathqqplot.R
More file actions
executable file
·137 lines (107 loc) · 5.33 KB
/
Copy pathqqplot.R
File metadata and controls
executable file
·137 lines (107 loc) · 5.33 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env Rscript
packs <- c("qqman", "optparse", "data.table", "R.utils")
for (p in packs) {
if (!require(p, character.only = TRUE)) {
print(p)
install.packages(p, repos = c(CRAN = "http://cran.r-project.org"))
}
}
option_list <- list(
make_option(c("-f", "--file"), type = "character", default = NULL,
help = "dataset file name",
metavar = "character"),
make_option(c("-o", "--out"), type = "character",
help = "output file name [default= %default]",
metavar = "character"),
make_option(c("-c", "--chrcol"), type = "character", default = "CHR",
help = "chromosome column [default= %default]",
metavar = "character"),
make_option(c("-p", "--pval_col"), type = "character", default = "P",
help = "pvalue column [default= %default]. This can be a comma separated list and plots will be generated for each of these",
metavar = "character"),
make_option(c("-b", "--bp_col"), type = "character", default = "BP",
help = "bp column [default= %default]",
metavar = "character"),
make_option(c("-l", "--loglog_pval"), type = "integer", default = 10,
help = "-log10 p-val threshold for using log-log scale in manhattan plot [default= %default]",
metavar = "integer"),
make_option(c("-y", "--loglog_ylim"), type = "integer", default = 324,
help = "-log10 p-val limit for y-axis of log-log manhattan [default= %default]",
metavar = "integer"),
make_option(c("-m", "--minrep_col"), type = "character",
help = "if given then chr:bp:ref:alt identifier assumed and chr and bp are read from there [default= %default]",
metavar = "character")
)
opt_parser <- OptionParser(option_list = option_list)
opt <- parse_args(opt_parser, positional_arguments = 0)
options(bitmapType = "cairo")
print(str(opt))
bp_col <- opt$options$bp_col
chr_col <- opt$options$chrcol
pcols <- unlist(strsplit(opt$options$pval_col, ","))
file <- opt$options$file
print(paste("reading file:", file))
data <- fread(file, header = TRUE, select = c(pcols, c(bp_col, chr_col)))
print(summary(data))
print(summary(data[[chr_col]]))
#colnames(data) <- toupper( colnames(data) )
output_prefix <- file
if (!is.null(opt$options$out)) {
output_prefix <- opt$options$out
}
if (! is.null(opt$options$minrep_col)) {
print("getting BP and CHR from minrepid")
split <- strsplit(as.character(data[[opt$options$minrep_col]]), ":")
data[[bp_col]] <- unlist(lapply(split, function(x) as.numeric(x[2])))
data[[chr_col]] <- unlist(lapply(split, function(x) x[1]))
}
print(append(pcols, c(bp_col, chr_col)))
if (any(! append(pcols, c(bp_col, chr_col)) %in% colnames(data))) {
stop("All required columns do not exist in the data: ", paste(c(pcols, bp_col, chr_col), collapse = ", "))
}
print(summary(as.factor(data[[chr_col]])))
data[[chr_col]] <- gsub("chr", "", data[[chr_col]])
data[[chr_col]] <- gsub("X|chrX", "23", data[[chr_col]])
data[[chr_col]] <- gsub("Y|chrY", "24", data[[chr_col]])
data[[chr_col]] <- gsub("MT|chrMT|M|chrM", "25", data[[chr_col]])
data[[chr_col]] <- as.numeric(data[[chr_col]])
data <- data[!is.na(data[[chr_col]])]
## added dummy SNP column as updated manhattan function from qqman package fails without it.
snp_col <- "SNP" ## needs to be called "SNP" for manhattan() function of qqman
data[[snp_col]] <- paste0(data[[chr_col]], ":", data[[bp_col]])
quants <- c(0.7, 0.5, 0.1, 0.01, 0.001)
for (pcol in pcols) {
subdata <- data[! is.na(data[[pcol]]) & is.numeric(data[[pcol]])]
lambda <- round(quantile((qchisq(1-subdata[[pcol]], 1)), probs = quants) / qchisq(quants, 1), 3)
png(paste(output_prefix, pcol, "qqplot.png", sep = "_"))
qq(subdata[[pcol]])
title(c(file, paste("\n", "\nlambda ", quants, ": ", lambda, sep="")), line = -0.2)
dev.off()
sink(paste(output_prefix, pcol, "qquantiles.txt", sep = "_"))
cat(paste0(quants, ":", lambda))
sink()
print("subsetting p-vals < 0.01 for manhattan...")
subdata <- subdata[subdata[[pcol]] < 0.01 & subdata[[pcol]] > 0]
print(paste("Plotting manhattan with", nrow(subdata), "variants"))
print(summary(subdata[[pcol]]))
png(paste(output_prefix, pcol, "manhattan.png", sep="_"), width = 1000, height = 400)
logs <- -log10(subdata[[pcol]])
manhattan(data.table(subdata[, c(bp_col,pcol,chr_col,snp_col), with=F]) , chr = chr_col, bp = bp_col, snp = snp_col, p = pcol, ylim=c(2, max(logs)+1), main = file)
dev.off()
print("plotting log-log manhattan")
loglog_p <- opt$options$loglog_pval
logs <- ifelse(logs < loglog_p, logs, loglog_p * log10(logs) / log10(loglog_p))
loglog_ylim <- opt$options$loglog_ylim
if (loglog_ylim == 0) {
max_loglog <- max(logs)
} else {
max_loglog <- loglog_p * log10(loglog_ylim) / log10(loglog_p)
}
subdata[["p_scaled"]] <- 10^(-logs)
tick_pos <- round(seq(1, max_loglog, length.out = max_loglog))
tick_lab <- sapply(tick_pos, function(pos) { round(ifelse(pos < loglog_p, pos, loglog_p^(pos/loglog_p))) })
png(paste(output_prefix, pcol, "manhattan_loglog.png", sep="_"), width = 1000, height = 400)
manhattan(data.table(subdata[, c(bp_col, "p_scaled", chr_col, snp_col), with=F]), chr = chr_col, bp = bp_col, snp = snp_col, p = "p_scaled", ylim=c(2, max_loglog), yaxt = "n", main = file)
axis(2, at = tick_pos, labels = tick_lab, las = 2)
dev.off()
}