-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrj01_RL_pheno_qtl2_DO_11092020_69k.Rmd
272 lines (239 loc) · 11 KB
/
Prj01_RL_pheno_qtl2_DO_11092020_69k.Rmd
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
---
title: "Prj01_RL_pheno_qtl2_DO_11092020_69k"
author: "Hao He"
date: "`r Sys.Date()`"
output: workflowr::wflow_html
---
```{r knitr-opts, include=FALSE}
knitr::opts_chunk$set(comment = "#",collapse = TRUE)
```
**Last update:** `r Sys.Date()`
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
###loading libraries
```{r library, message=FALSE, warning=FALSE}
library(ggplot2)
library(gridExtra)
library(GGally)
library(parallel)
library(qtl2)
library(parallel)
library(survival)
library(regress)
library(abind)
library(openxlsx)
library(tidyverse)
rz.transform <- function(y) {
rankY=rank(y, ties.method="average", na.last="keep")
rzT=qnorm(rankY/(length(na.exclude(rankY))+1))
return(rzT)
}
setwd("/projects/csna/csna_workflow/")
```
###Read phenotype data
```{r Read phenotype data, message=FALSE, fig.height=8, fig.width=6}
#Prj01_RL_pheno_qttl2_DO_11092020
pheno <- readr::read_csv("data/pheno/Prj01_RL_pheno_qttl2_DO_11092020.csv", na = c("N/A", "NA"))
pheno <- pheno %>%
filter(apply(pheno, 1, function(x) sum(is.na(x))) < 5) %>% # remove rows with 5 NAs
mutate(
Acq.Anticipatory.Correct.Responses.nooutlier = case_when(
(Acq.Anticipatory.Correct.Responses > 5 | is.na(Acq.Anticipatory.Correct.Responses)) ~ NA_real_,
TRUE ~ Acq.Anticipatory.Correct.Responses),
Rev.Anticipatory.Incorrect.Responses.nooutlier = case_when(
(Rev.Anticipatory.Incorrect.Responses > 6) ~ NA_real_,
TRUE ~ Rev.Anticipatory.Incorrect.Responses
)) %>% #boxcoxtransformation
mutate(across(c(Rev.Anticipatory.Incorrect.Responses), ~ EnvStats::boxcoxTransform(.x, lambda = EnvStats::boxcox(.x, optimize = TRUE)$lambda),
.names ="{.col}.boxcox.trans")) %>% #omit values >6 for Acq.Anticipatory.Correct.Responses and Rev.Anticipatory.Incorrect.Responses variables.
mutate(across(Acq.Total.Trials:Rev.Anticipatory.Incorrect.Responses.nooutlier, ~ rz.transform(.x),
.names ="{.col}.rz"))
#load gm data
load("data/Jackson_Lab_12_batches/gm_DO3173_qc_newid.RData")
#subset
overlap.id <- intersect(pheno$mouseID, ind_ids(gm_after_qc))
load("data/Jackson_Lab_12_batches/qc_info.RData")
#merge pheno with covar
pheno <- pheno %>%
left_join(qc_info) %>%
filter(mouseID %in% overlap.id) %>%
filter(bad.sample == FALSE) %>%
mutate(sex = case_when(
sex == "M" ~ 1,
sex == "F" ~ 0
)) %>%
mutate_at(vars(sex,
ngen),
list(factor))
#pgmap
load("/projects/csna/csna_workflow/data/69k_grid_pgmap.RData")
query_variants <- create_variant_query_func("/projects/csna/csna_workflow/data/cc_variants.sqlite")
query_genes <- create_gene_query_func("/projects/csna/csna_workflow/data/mouse_genes_mgi.sqlite")
```
###Plot qtl mapping on 69k
Plot by using its output Prj01_RL_11092020_cutoff6.qtlout.69k.RData
```{r Plot qtl mapping on 69k, message=FALSE, warning=FALSE}
load("output/Prj01_RL_11092020_cutoff6.qtlout.69k.RData")
#genome-wide plot
for(i in names(m2.qtl.out)){
par(mar=c(5.1, 4.1, 1.1, 1.1))
ymx <- maxlod(m2.qtl.out[[i]]) # overall maximum LOD score
plot(m2.qtl.out[[i]], map=pmap, lodcolumn=1, col="slateblue", ylim=c(0, 12))
abline(h=summary(m2.permu[[i]], alpha=c(0.10, 0.05, 0.01))[[1]], col="red")
abline(h=summary(m2.permu[[i]], alpha=c(0.10, 0.05, 0.01))[[2]], col="red")
abline(h=summary(m2.permu[[i]], alpha=c(0.10, 0.05, 0.01))[[3]], col="red")
title(main = paste0(i))
}
pdf(file = paste0("output/RL_prj/Prj01_RL_11092020_cutoff6.qtlout.pdf"), width = 16, height =8)
#save genome-wide plo
for(i in names(m2.qtl.out)){
par(mar=c(5.1, 4.1, 1.1, 1.1))
ymx <- maxlod(m2.qtl.out[[i]]) # overall maximum LOD score
plot(m2.qtl.out[[i]], map=pmap, lodcolumn=1, col="slateblue", ylim=c(0, 10))
abline(h=summary(m2.permu[[i]], alpha=c(0.10, 0.05, 0.01))[[1]], col="red")
abline(h=summary(m2.permu[[i]], alpha=c(0.10, 0.05, 0.01))[[2]], col="red")
abline(h=summary(m2.permu[[i]], alpha=c(0.10, 0.05, 0.01))[[3]], col="red")
title(main = paste0(i))
}
dev.off()
#peaks coeff plot
for(i in names(m2.qtl.out)){
print(i)
peaks <- find_peaks(m2.qtl.out[[i]], map=pmap, threshold=6, drop=1.5)
if(nrow(peaks) > 0) {
print(peaks)
for(p in 1:dim(peaks)[1]) {
print(p)
chr <-peaks[p,3]
#coeff plot
par(mar=c(4.1, 4.1, 0.6, 0.6))
plot_coefCC(m2.sigqtl.coef[[i]][[p]], pmap[chr], scan1_output=m2.qtl.out[[i]], bgcolor="gray95", legend=NULL)
plot_coefCC(m2.sigqtl.blup[[i]][[p]], pmap[chr], scan1_output=m2.qtl.out[[i]], bgcolor="gray95", legend=NULL)
#plot(m2.sigqtl.snps[[i]][[p]]$lod, m2.sigqtl.snps[[i]][[p]]$snpinfo, drop_hilit=1.5, genes=m2.sigqtl.genes[[i]][[p]])
}
}
}
#save peaks coeff plot
for(i in names(m2.qtl.out)){
print(i)
peaks <- find_peaks(m2.qtl.out[[i]], map=pmap, threshold=6, drop=1.5)
if(nrow(peaks) > 0){
fname <- paste("output/RL_prj/Prj01_RL_11092020_cutoff6.qtlout_", str_replace_all(i, "[[:punct:]]", "") ,"_coefplot.pdf",sep="")
pdf(file = fname, width = 16, height =8)
for(p in 1:dim(peaks)[1]) {
print(p)
chr <-peaks[p,3]
#coeff plot
par(mar=c(4.1, 4.1, 0.6, 0.6))
plot_coefCC(m2.sigqtl.coef[[i]][[p]], pmap[chr], scan1_output=m2.qtl.out[[i]], bgcolor="gray95", legend=NULL)
plot_coefCC(m2.sigqtl.blup[[i]][[p]], pmap[chr], scan1_output=m2.qtl.out[[i]], bgcolor="gray95", legend=NULL)
#plot(m2.sigqtl.snps[[i]][[p]]$lod, m2.sigqtl.snps[[i]][[p]]$snpinfo, drop_hilit=1.5, genes=m2.sigqtl.genes[[i]][[p]])
}
dev.off()
}
}
#save peaks coeff blup plot
for(i in names(m2.qtl.out)){
print(i)
peaks <- find_peaks(m2.qtl.out[[i]], map=pmap, threshold=6, drop=1.5)
if(nrow(peaks) > 0) {
fname <- paste("output/RL_prj/Prj01_RL_11092020_cutoff6.qtlout_", str_replace_all(i, "[[:punct:]]", "") ,"_coefplot_blup.pdf",sep="")
pdf(file = fname, width = 16, height =8)
for(p in 1:dim(peaks)[1]) {
print(p)
chr <-peaks[p,3]
#coeff plot
par(mar=c(4.1, 4.1, 0.6, 0.6))
plot_coefCC(m2.sigqtl.blup[[i]][[p]], pmap[chr], scan1_output=m2.qtl.out[[i]], bgcolor="gray95", legend=NULL)
#plot(m2.sigqtl.snps[[i]][[p]]$lod, m2.sigqtl.snps[[i]][[p]]$snpinfo, drop_hilit=1.5, genes=m2.sigqtl.genes[[i]][[p]])
}
dev.off()
}
}
```
```{r heritability by GCTA, eval=FALSE}
#gcta id
gcta.id <- read.table("/projects/csna/csna_workflow/data/GCTA/12_batches.fam", header = F, sep = " ")
#overlap
pheno.overlap <- pheno[pheno$mouseID %in% intersect(gcta.id$V1, pheno$mouseID),]
pheno.overlap$sex <- as.numeric(pheno.overlap$sex)
pheno.overlap$sex[pheno.overlap$sex == 0] <- 2 #female
dim(pheno.overlap)
#subset id
idlist <- cbind(pheno.overlap$mouseID, pheno.overlap$mouseID)
write.table(idlist, file = "/projects/csna/csna_workflow/data/GCTA/Prj01_RL_pheno_qttl2_DO_11092020/update_id.txt", sep = "\t", col.names = FALSE, row.names = FALSE, quote = FALSE)
#update sex
sex <- data.frame(id1 = pheno.overlap$mouseID,
id2 = pheno.overlap$mouseID,
sex = as.character(pheno.overlap$sex))
write.table(sex, file = "/projects/csna/csna_workflow/data/GCTA/Prj01_RL_pheno_qttl2_DO_11092020/update_sex.txt",sep = "\t", col.names = FALSE, row.names = FALSE, quote = FALSE)
#updated covariate
covar <- data.frame(id1 = pheno.overlap$mouseID,
id2 = pheno.overlap$mouseID,
sex = as.character(pheno.overlap$sex)
)
write.table(covar, file = "/projects/csna/csna_workflow/data/GCTA/Prj01_RL_pheno_qttl2_DO_11092020/update_covar.txt",sep = "\t", col.names = FALSE, row.names = FALSE, quote = FALSE)
#update pheno
update.pheno <- cbind(data.frame(FID = pheno.overlap$mouseID,
IID = pheno.overlap$mouseID),
pheno.overlap[,4:17]
)
write.table(update.pheno, file = "/projects/csna/csna_workflow/data/GCTA/Prj01_RL_pheno_qttl2_DO_11092020/update.pheno.txt",sep = "\t", col.names = FALSE, row.names = FALSE, quote = FALSE)
#subset id and update sex
system(paste0("cd /projects/csna/csna_workflow/data/GCTA/Prj01_RL_pheno_qttl2_DO_11092020/; /projects/csna/csna_workflow/data/GCTA/plink -bfile ",
"/projects/csna/csna_workflow/data/GCTA/","12_batches ",
"--keep update_id.txt ",
"--update-sex update_sex.txt ",
"--make-bed --out ",
"Prj01_RL_pheno_qttl2_DO_11092020"))
# Estimate the GRM
system(paste0("cd /projects/csna/csna_workflow/data/GCTA/Prj01_RL_pheno_qttl2_DO_11092020/; /projects/csna/csna_workflow/data/GCTA/gcta64 --bfile ",
"Prj01_RL_pheno_qttl2_DO_11092020", " --make-grm --out ",
"Prj01_RL_pheno_qttl2_DO_11092020"))
for(i in 1:length(colnames(pheno.overlap)[4:17])){
system(
paste0("cd /projects/csna/csna_workflow/data/GCTA/Prj01_RL_pheno_qttl2_DO_11092020/; /projects/csna/csna_workflow/data/GCTA/gcta64 --reml --grm ",
"Prj01_RL_pheno_qttl2_DO_11092020", " --pheno update.pheno.txt --covar update_covar.txt --mpheno ",
i," --out ",colnames(pheno.overlap)[4:17][i],".out")
)
}
#plot heritability by GCTA
hsq.gcta <- list()
phe.name <- colnames(pheno.overlap)[4:17]
for (i in phe.name){
hsq.gcta[[i]] <- read.table(file = paste0("/projects/csna/csna_workflow/data/GCTA/Prj01_RL_pheno_qttl2_DO_11092020/",i,".out.hsq"),sep = "\t", header = FALSE,fill = TRUE, stringsAsFactors = FALSE)
}
h <- data.frame(
Phenotype = phe.name,
Heritability = as.numeric(as.vector(unlist(lapply(hsq.gcta, FUN = function(x){x[5,2]})))),
SE = as.numeric(as.vector(unlist(lapply(hsq.gcta, FUN = function(x){x[5,3]})))),
Sample_size = as.numeric(as.vector(unlist(lapply(hsq.gcta, FUN = function(x){x[11,2]})))),
# Domain = sub("\\..*", "", phe.name),
stringsAsFactors = FALSE)
write.csv(h, file = "/projects/csna/csna_workflow/data/GCTA/Prj01_RL_pheno_qttl2_DO_11092020/h2.csv", row.names = F, quote = F)
#histgram
h$Heritability <- round(h$Heritability,2)
pdf(file = paste0("/projects/csna/csna_workflow/data/GCTA/Prj01_RL_pheno_qttl2_DO_11092020/","Prj01_RL_pheno_qttl2_DO_11092020__heritability_by_GCTA.pdf"), height = 10, width = 10)
p<-ggplot(data=h, aes(x=Phenotype, y=Heritability)) + #, fill=Domain, color = Domain)) +
geom_bar(stat="identity", fill = "blue", color = "blue", show.legend = FALSE) +
scale_y_continuous(breaks=seq(0.0, 1.0, 0.1)) +
geom_text(aes(label = Heritability, y = Heritability + 0.005), position = position_dodge(0.9),vjust = 0) +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
p
dev.off()
#plot heritability by qtl2 array --------------
herit <- data.frame(Phenotype = names(unlist(qtl.hsq)),
Heritability = round(unlist(qtl.hsq),3))
herit <- herit %>%
arrange(desc(Heritability))
herit$Phenotype <- factor(herit$Phenotype, levels = herit$Phenotype)
#histgram
p2 <- ggplot(data=herit, aes(x=Phenotype, y=Heritability)) + #, fill=Domain, color = Domain)) +
geom_bar(stat="identity", fill = "blue", color = "blue", show.legend = FALSE) +
scale_y_continuous(breaks=seq(0.0, 1.0, 0.1)) +
geom_text(aes(label = Heritability, y = Heritability + 0.005), position = position_dodge(0.9),vjust = 0) +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
ggtitle("Heritability by qtl2 array")
p2
```