forked from elijahedmondson/NASA-GRSD-HS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlotting QTL.R
65 lines (48 loc) · 2.27 KB
/
Plotting QTL.R
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
xlim = c(0, 10641881)
ylim = c(0, max(sapply(qtl, function(z) { max(z$p.value) })))
plot <- loop.hs.qtl(qtl = qtl, title = "su")
loop.hs.qtl = function(qtl, title, bin.width = 1000, ...) {
library(GenomicRanges)
library(BSgenome.Mmusculus.UCSC.mm10)
new.qtl = NULL
for(chr in 1:length(qtl)) {
print(chr)
# Create SNP bins with given bin.width
brks = cut(x = 1:length(qtl[[chr]]), breaks = length(qtl[[chr]]) / bin.width)
# Split up the SNP positions and get the mean.
pos = split(start(qtl[[chr]]), brks)
pos = sapply(pos, mean)
# Split up the p-values and get the max.
pv = split(mcols(qtl[[chr]])$p.value, brks)
pv = sapply(pv, min)
# Make a single new GRanges object to return.
gr = GRanges(seqnames = seqnames(qtl[[chr]])[1],
ranges = IRanges(start = pos, width = 1), p.value = pv)
if(chr == 1) {
new.qtl = gr
} else {
new.qtl = c(new.qtl, gr)
} # else
} # for(chr)
# Get the chromosome lengths.
chrlen = seqlengths(BSgenome.Mmusculus.UCSC.mm10)
names(chrlen) = sub("^chr", "", names(chrlen))
chrlen = chrlen[seqlevels(new.qtl)] * 1e-6
# Add the chr lengths to the chromosomes for plotting.
# Switch positions to genome Mb.
gmb = start(new.qtl) * 1e-6
for(chr in 2:length(chrlen)) {
wh = which(seqnames(new.qtl) == names(chrlen)[chr])
gmb[wh] = gmb[wh] + sum(chrlen[1:(chr - 1)])
} # for(chr)
# Get chromosome mid-points for plotting the Chr name.
chrmid = (chrlen / 2) + cumsum(c(1, chrlen[-length(chrlen)]))
# Make the plot.
col = rep(rgb(0,0,0), length(new.qtl))
even.chr = which(seqnames(new.qtl) %in% (1:10 * 2))
col[even.chr] = rgb(0.7,0.7,0.7)
plot(gmb, -log10(new.qtl$p.value), pch = 20, xaxt = "n",
col = col, las = 1, xlab = "", ylab = "-log10(p-value)", main = "title")
mtext(side = 1, line = 0.5, at = chrmid, text = names(chrlen), cex = 1.2)
return(new.qtl)
}