-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbsaplot.r
165 lines (164 loc) · 4.75 KB
/
bsaplot.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
bsaplot <- function(
key,
chrom,
chrmax,
chrmin = 1,
linef = NULL,
linep = c(1, 2),
linev = c(3, 4),
linec = c("black", "blue", "red", "blue", "red"),
pointf = NULL,
pointp = c(1, 2),
pointv = 3,
pointc = c("#33A02C", "#FF7F00"),
hline = NULL,
hcolor = "grey",
hlty = 1,
ymin = -1,
ymax = 1,
xlab = "Chromosome",
ylab = expression(Delta(SNPindex)),
cexa = 1.5,
cexl = 2,
cexp = 0.4,
pch = 19,
lwd = 3,
las = 1,
bandc = "grey90",
alt = F,
width = 15,
height = 6,
res = 600
){
library(gtools)
multi.mixedorder <- function(..., na.last = TRUE, decreasing = FALSE){
do.call(order, c(
lapply(list(...), function(l){
if(is.character(l)){
factor(l, levels=mixedsort(unique(l)))
} else {
l
}
}),
list(na.last = na.last, decreasing = decreasing)
))
}
## global deal
chrom <- as.character(chrom)
mindf <- data.frame(chr = chrom, pos = chrmin, stringsAsFactors = F)
maxdf <- data.frame(chr = chrom, pos = chrmax, stringsAsFactors = F)
minmax <- rbind(mindf, maxdf)
minmax <- minmax[multi.mixedorder(minmax[,1], minmax[,2]), ]
chrom <- mixedsort(chrom)
mins <- NULL
maxs <- NULL
print("pass1: global")
## line deal
if(!is.null(linef)){
linet <- read.table(linef, header = T, stringsAsFactors = F, check.names = F)
linet <- linet[,c(linep, linev)]
linet[, 1] <- as.character(linet[, 1])
linet <- linet[linet[, 1] %in% chrom, ]
lminmax <- minmax
for(i in 3:ncol(linet)){
lminmax[,i] <- NA
}
names(lminmax) <- names(linet)
linet <- rbind(linet, lminmax)
linet <- linet[multi.mixedorder(linet[,1], linet[,2]),]
mins <- min(linet[, 3:ncol(linet)], na.rm = T)
maxs <- max(linet[, 3:ncol(linet)], na.rm = T)
class(linet) <- c("scanone", "data.frame")
}
print("pass2: line data")
## point deal
if(!is.null(pointf)){
pointc <- rep(pointc, ceiling(length(chrom)/length(pointc)))[1:length(chrom)]
chrcolor <- data.frame(chrom, pointc, stringsAsFactors = F)
pointt <- read.table(pointf, header = T, stringsAsFactors = F, check.names = F)
pointt <- pointt[,c(pointp, pointv)]
pointt[, 1] <- as.character(pointt[, 1])
pointt <- pointt[pointt[, 1] %in% chrom,]
pminmax <- minmax
for(i in 3:ncol(pointt)){
pminmax[,i] <- NA
}
names(pminmax) <- names(pointt)
names(pminmax) <- names(pointt)
pointt <- rbind(pointt, pminmax)
pointt <- pointt[multi.mixedorder(pointt[,1], pointt[,2]),]
if(is.null(mins)){
mins <- min(pointt[,3], na.rm = T)
}else{
mins <- min(min(pointt[,3], na.rm = T), mins)
}
if(is.null(maxs)){
maxs <- max(pointt[,3], na.rm = T)
}else{
maxs <- max(max(pointt[,3], na.rm = T), maxs)
}
class(pointt) <- c("scanone", "data.frame")
}
print("pass3: point data")
## plot
if(is.null(ymin)){
ymin <- mins
}
if(is.null(ymax)){
ymax <- maxs
}
minmax$lod <- NA
class(minmax) <- c("scanone", "data.frame")
# png
png(filename = paste0(key, ".plplot.png"), width = width, height = height, units = "in", res = res)
dev.control("enable")
# global parameters
chara_len <- max(nchar(chrom))
marx <- 5
if(chara_len > 6){
marx <- marx * chara_len/5
}
par(mgp = c(2.5, 0.8, 0), mai = c(0.8, 1, 0.2, 0.2),
mar = c(marx,5,0.8,0.8), cex.axis = cexa, cex.lab = cexl)
# empty plot
plot.scanone(x = minmax, type="n", ylim = c(ymin, ymax), bty = "n",
bandcol = bandc, incl.markers = F, yaxs="i", alternate.chrid = alt,
xlab = xlab, ylab = ylab, las = las)
box()
# point plot
if(!is.null(pointf)){
nchr <- length(chrcolor[,2])
if(nchr > 1){
for (i in 1:nchr) {
ptmp <- pointt
ptmp[ptmp[, 1] != chrcolor[i, 1], ][, 3] <- NA
plot.scanone(ptmp, add = T, lodcolumn = 1, col = chrcolor[i, 2], type = "p", pch = pch, cex = cexp)
}
}else{
ptmp <- pointt
plot.scanone(ptmp, add = T, lodcolumn = 1, col = chrcolor[,2], type = "p", pch = pch, cex = cexp)
}
print("pass4: point plot")
}
# hline
if(!is.null(hline)){
abline(h = hline, lty = hlty, col = hcolor, lwd = lwd)
}
# line plot
if(!is.null(linef)){
for (l in 1:(ncol(linet) - 2)) {
plot.scanone(linet, add=T, lodcolumn = l, col = linec[l], type = "l",
pch = pch, lwd = lwd, cex = cexp)
}
print("pass5: line plot")
}
# box
#box()
# copy to pdf
dev.copy(pdf, file = paste0(key, ".plplot.pdf"), width = width, height = height)
dev.off(which = dev.cur())
# close all image device
for (i in 1:length(dev.list())) {
dev.off()
}
}