-
Notifications
You must be signed in to change notification settings - Fork 3
/
alleleFreqw.R
187 lines (169 loc) · 7.84 KB
/
alleleFreqw.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# A function to perform allele frequency analysis using SNP data in a specified genomic region.
# Change to the directory of ECOGEMS using the setwd function of R.
# Usage: type the next two lines in R Console without the leading #
# source("Global.R")
# allele.plot <- alleleFreq(snpSite = c("0133024709", "1403584545", "1403584761"), accGroup = c("Landrace", "Glycine soja", "Improved cultivar"), pieCols = c("cornflowerblue", "forestgreen", "red"))
# Then the result plot would be displayed in a plotting device.
# For more info, please check the AlleleFreq menu of the ECOGEMS database.
alleleFreqw <- function(snpSite = c("0300000762", "2015000614","2015001173"),
accGroup = c("Glycine soja", "Landraces and elites"),
pieCols = c("cornflowerblue", "forestgreen") ) {
if ( exists("snpInfow") ){
}else{
source("snpInfow.R")
}
if (exists("snpw.lst")){
}else{
snpw.lst <- read.table("./data/w82Rdata/w82.snp.RData.lst", head=T, as.is=T, sep="\t")
}
myChr <- paste0("chr", as.numeric(substr(snpSite, 1, 2)))
myPos <- as.numeric(substr(snpSite, 3, 10))
snpSite <- snpSite[!is.na(myPos)]
myChr <- myChr[!is.na(myPos)]
myPos <- myPos[!is.na(myPos)]
myChr <- myChr[myChr %in% paste0("chr", 1:20)]
site.geno <- lapply(1:length(myChr), function(i) {
return(snpInfow(myChr[i], myPos[i], myPos[i]))
})
acc.group <- accGroup
site.allele.freq <- lapply(site.geno, function(x){
if (nrow(x[[2]]) == 0) {
return(NULL)
} else {
maj.allele <- x[[2]][1, 2]
min.allele <- x[[2]][1, 3]
heterozygote <- x[[2]][1, 4]
acc.grp.tab <- lapply(acc.group, function(i) {
i <- gsub(" ", "_", i)
i.acc <- readLines(paste0("./data/w82Rdata/", i, ".soya_82.txt"))
x.i.allele.freq <- table(x[[1]][[1]][, colnames(x[[1]][[1]]) %in% i.acc])
x.i.allele.freq <- x.i.allele.freq[c(maj.allele, min.allele, heterozygote)]
if (is.na(x.i.allele.freq[2])) {
x.i.allele.freq[2] <- 0
names(x.i.allele.freq)[2] <- min.allele
}
if (is.na(x.i.allele.freq[3])) {
x.i.allele.freq[3] <- 0
names(x.i.allele.freq)[3] <- heterozygote
}
return(x.i.allele.freq)
})
acc.grp.tab.df <- do.call(rbind, acc.grp.tab)
acc.grp.tab.df[, 1] <- acc.grp.tab.df[, 1] + acc.grp.tab.df[, 3]
acc.grp.tab.df[, 2] <- acc.grp.tab.df[, 2] + acc.grp.tab.df[, 3]
if (length(acc.group) == 1){
acc.grp.tab.df <- t(as.matrix(acc.grp.tab.df[, 1:2]))
}else{
acc.grp.tab.df <- acc.grp.tab.df[, 1:2]
}
rownames(acc.grp.tab.df) <- acc.group
return(acc.grp.tab.df)
}
})
snpSite <- snpSite[!sapply(site.allele.freq, is.null)]
site.allele.freq[sapply(site.allele.freq, is.null)] <- NULL
yrowname <- substr(snpSite, 1, 10)
freqsall <- rbind(data.frame(site.allele.freq, check.names = F), rep(yrowname, each = 2))
freqsall <- rbind(freqsall, colnames(freqsall))
# rownames(freqsall)[4] <- "Position"
# rownames(freqsall)[5] <- "Nucleotide"
nrowfreqsall <- nrow(freqsall)
rownames(freqsall)[nrowfreqsall - 1] <- "Position"
rownames(freqsall)[nrowfreqsall] <- "Nucleotide"
freqsall <- freqsall[c(nrowfreqsall - 1, nrowfreqsall, 1:(nrowfreqsall-2) ), ]
freqsall <- data.frame(t(freqsall), row.names = NULL, stringsAsFactors = F)
freqsalls <- plyr::ddply(freqsall, plyr::.(Position), function(x){
d <- paste0(x[1,], ":", x[2,])
})
freqsalls <- freqsalls[, -2]
colnames(freqsalls) <- c("ID", "Allele", acc.group)
freqsallwrite82 <<- freqsalls
if(length(site.allele.freq) == 0){
NULL
}else{
pie.cols <- pieCols
if ( length(accGroup) == 1){
# op <- par(mfrow=c(3,2),
# oma = c(0, 0, 0, 0),
# mar = c(0, 2, 1, 0),
# mgp = c(0, 0, 0),
# xpd = NA)
op <- par(mfrow=c(length(site.allele.freq), length(acc.group) + 1 ),
oma = c(0, 0, 0, 0),
mar = c(0, 2, 1, 0),
mgp = c(0, 0, 0),
xpd = NA)
for (i in 1:length(site.allele.freq)) {
for (j in 1:nrow(site.allele.freq[[i]])) {
if (i == 1) {
if (j == 1) {
pie(site.allele.freq[[i]][j, ], col=pie.cols, label=NA, radius=1) +
title(acc.group[j], line = -0.5, cex.main = 2) + title(ylab = yrowname[i], line = -0.4, cex.lab = 2)
plot(0, type = "n", axes = F, xlab="", ylab="")
legend("center", legend = names(site.allele.freq[[i]][j, ]),
fill = pie.cols, border = pie.cols, bty="n", cex = 2)
} else if (j == length(acc.group)) {
pie(site.allele.freq[[i]][j, ], col=pie.cols, label=NA, radius=1) + title(acc.group[j], line = -0.5, cex.main = 2)
plot(0, type = "n", axes = F, xlab="", ylab="")
legend("center", legend = names(site.allele.freq[[i]][j, ]),
fill = pie.cols, border = pie.cols, bty="n", cex = 2)
} else {
pie(site.allele.freq[[i]][j, ], col=pie.cols, label=NA, radius=1) + title(acc.group[j], line = -0.5, cex.main = 2)
}
} else {
if (j == 1) {
pie(site.allele.freq[[i]][j, ], col=pie.cols,
label=NA, radius=1) + title(ylab = yrowname[i], line = -0.4, cex.lab = 2)
plot(0, type = "n", axes = F, xlab="", ylab="")
legend("center", legend = names(site.allele.freq[[i]][j, ]),
fill = pie.cols, border = pie.cols, bty="n", cex = 2)
} else if (j == length(acc.group)) {
pie(site.allele.freq[[i]][j, ], col=pie.cols, label=NA, radius=1)
plot(0, type = "n", axes = F, xlab="", ylab="")
legend("center", legend = names(site.allele.freq[[i]][j, ]),
fill = pie.cols, border = pie.cols, bty="n", cex = 2)
} else {
pie(site.allele.freq[[i]][j, ], col=pie.cols, label=NA, radius=1)
}
}
}
}
}else{
op <- par(mfrow=c(length(site.allele.freq), length(acc.group) + 1 ),
oma = c(0, 0, 0, 0),
mar = c(0, 2, 1, 0),
mgp = c(0, 0, 0),
xpd = NA)
for (i in 1:length(site.allele.freq)) {
for (j in 1:nrow(site.allele.freq[[i]])) {
if (i == 1) {
if (j == 1) {
pie(site.allele.freq[[i]][j, ], col=pie.cols, label=NA, radius=1) +
title(acc.group[j], line = -0.3, cex.main = 2) + title(ylab = yrowname[i], line = -0.4, cex.lab = 2)
} else if (j == length(acc.group)) {
pie(site.allele.freq[[i]][j, ], col=pie.cols, label=NA, radius=1) + title(acc.group[j], line = -0.3, cex.main = 2)
plot(0, type = "n", axes = F, xlab="", ylab="")
legend("center", legend = names(site.allele.freq[[i]][j, ]),
fill = pie.cols, border = pie.cols, bty="n", cex = 2)
} else {
pie(site.allele.freq[[i]][j, ], col=pie.cols, label=NA, radius=1) + title(acc.group[j], line = -0.3, cex.main = 2)
}
} else {
if (j == 1) {
pie(site.allele.freq[[i]][j, ], col=pie.cols,
label=NA, radius=1) + title(ylab = yrowname[i], line = -0.4, cex.lab = 2)
} else if (j == length(acc.group)) {
pie(site.allele.freq[[i]][j, ], col=pie.cols, label=NA, radius=1)
plot(0, type = "n", axes = F, xlab="", ylab="")
legend("center", legend = names(site.allele.freq[[i]][j, ]),
fill = pie.cols, border = pie.cols, bty="n", cex = 2)
} else {
pie(site.allele.freq[[i]][j, ], col=pie.cols, label=NA, radius=1)
}
}
}
}
}
par(op)
}
}