-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGWAS2_totals.Rmd
287 lines (239 loc) · 13 KB
/
GWAS2_totals.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
---
output: pdf_document
geometry: margin=1in
mainfont: Times New Roman
---
```{r,fig.width=7.5, fig.height=8,warning=F, message=F, echo=F,comment="K",results='asis', dev='png'}
library(pander)
library(cegwas)
library(genetics)
library(ggplot2)
library(data.table)
library(grid)
library(stringr)
library(gridExtra)
library(knitr)
library(tidyr)
library(scales)
library(gtable)
library(dplyr)
panderOptions('knitr.auto.asis', TRUE)
setwd("/Users/kristen/Documents/transposon_figure_data/data")
load("Processed_Transposon_Mappings_2.Rda")
#load("Processed_Transposon_Mappings_SUBSET2.Rda")
load("count_QTL.Rda")
#plot_peak_ld
plot_peak_ld <- function(plot_df, trait = NULL){
if (is.null(trait)) {
snp_df <- plot_df %>% na.omit()
}
else {
snp_df <- dplyr::filter(plot_df, trait == trait) %>%
na.omit()
}
ld_snps <- dplyr::filter(snps, CHROM %in% snp_df$CHROM, POS %in%
snp_df$POS)
ld_snps <- data.frame(snp_id = paste(ld_snps$CHROM, ld_snps$POS,
sep = "_"), data.frame(ld_snps)[, 5:ncol(ld_snps)])
sn <- list()
for (i in 1:nrow(ld_snps)) {
sn[[i]] <- genetics::genotype(as.character(gsub(1, "T/T",
gsub(-1, "A/A", ld_snps[i, 4:ncol(ld_snps)]))))
}
test <- data.frame(sn)
colnames(test) <- (ld_snps$snp_id)
if (ncol(test) == 1) {
print("Only one significant SNP, not calculating LD")
}
else {
ldcalc <- t(genetics::LD(test)[[3]])
diag(ldcalc) <- 1
LDs <- tbl_df(data.frame(ldcalc) %>%
dplyr::add_rownames(var = "SNP1")) %>%
tidyr::gather(SNP2, Dprime, -SNP1) %>%
dplyr::arrange(SNP1) %>%
tidyr::separate(SNP1, sep = "_", into = c("CHROM1", "POS1"), remove = F) %>%
dplyr::arrange(CHROM1, as.numeric(POS1))
ldplot <- ggplot2::ggplot(LDs)+
ggplot2::aes(x = factor(SNP1, levels = SNP1, ordered = T), y = factor(SNP2, levels = SNP1, ordered = T)) +
ggplot2::geom_tile(ggplot2::aes(fill = Dprime)) +
ggplot2::geom_text(ggplot2::aes(label = signif(Dprime,2)), size = 4)+ #, fontface = "bold"
ggplot2::theme(axis.text.x = ggplot2::element_text(size=11, face="bold", color="black",angle=90,hjust=.5),
axis.text.y = ggplot2::element_text(size=11, face="bold", color="black"),
axis.title.x = ggplot2::element_text(size=0, face="bold", color="black", vjust=-.3),
axis.title.y = ggplot2::element_text(size=0, face="bold", color="black"),
plot.margin=unit(c(.5,5,.5,5), "cm"),
axis.line.y = element_line(colour = "black"),
axis.line.x = element_line(colour = "black"),
legend.position="none") +
scale_x_discrete(labels = function(x) { gsub("_", ":", x)}, expand = c(0,0)) +
scale_y_discrete(labels = function(x) { gsub("_", ":", x)}, expand = c(0,0)) +
scale_fill_continuous(high = "#FF6666", low = "white", na.value = "white")
#ldplot <- cowplot::ggdraw(cowplot::switch_axis_position(ldplot, 'y'))
# rgb.palette <- grDevices::colorRampPalette(rev(c("blue",
# "orange", "red")), space = "rgb")
# ld_outs <- LDheatmap::LDheatmap(test, LDmeasure = "r",
# SNP.name = colnames(test), color = rgb.palette(18))
# LD.grob1 <- grid::editGrob(ld_outs$LDheatmapGrob, gPath("heatMap",
# "title"), gp = gpar(cex = 1.25, col = "black"))
# LD.grob2 <- grid::editGrob(LD.grob1, gPath("geneMap",
# "title"), gp = gpar(cex = 0, col = "orange"))
# LD.grob3 <- grid::editGrob(LD.grob2, gPath("Key", "title"),
# gp = gpar(cex = 1.25, col = "black"))
# grid::grid.newpage()
# grid::grid.draw(LD.grob3)
return(ldplot)
}
}
#PxG function
hm<-processed_mapping_df
hm<-distinct(processed_mapping_df, trait,strain,peak_id,.keep_all=TRUE)
hm$allele <- factor(hm$allele,
levels = c(-1,1),
labels = c("REF", "ALT"))
gwasPxG <- function(trt,specific_peak){
#load("~/Dropbox/AndersenLab/RCode/Stefan/good_gwasMappingsINlinkage_phenotypes.Rda")
hm %>%
filter(trait==trt,peak_id==specific_peak,!is.na(allele))%>%
ggplot(.)+
aes(x=allele,y = value,fill=as.factor(allele))+
geom_boxplot(outlier.shape=NA,size =.5,color="gray52")+
geom_point(size = 1, alpha = .8,position=position_jitter(w=.4, h=.025),na.rm=TRUE)+
theme_bw()+
theme(axis.text.x = element_text(size=9, , color="black"),
axis.text.y = element_text(size=9, color="black"),
axis.title.x = element_text(size=9, color="black"),
axis.title.y = element_text(size=9, color="black",vjust=1),
strip.text.x = element_text(size=9, color="black"),
strip.text.y = element_text(size=9, color="black"),
plot.title = element_text(size=9, vjust=1,hjust=.5),
legend.title = element_text(size=9),
panel.border = element_rect(size=1, colour = "black"),
plot.margin = unit(c(.05,.05,.05,.05), "cm"),
legend.position = "none")+
scale_y_continuous(breaks= pretty_breaks())+
labs( x = "Genotype",y="Value")+
scale_fill_manual( values = c("darkgray", "burlywood2", "darkolivegreen","black"))
}
# pull unique combos, remove strain column(don't need specific strain info at this point)
processed_mapping_df<- processed_mapping_df %>% distinct(trait,marker,strain,.keep_all=TRUE)
#create family and method columns
processed_mapping_df$family <- stringr::str_split_fixed(processed_mapping_df$trait, "_TRANS_",2)[,2]
processed_mapping_df$method <- stringr::str_split_fixed(processed_mapping_df$trait, "_TRANS_",2)[,1]
#read in position data and create family column
positions <- read.table("CtCp_all_nonredundant.txt")
names(positions)<-c("CHROM","start","end","TE","orientation","method","strain","class")
positions$family<- stringr::str_split_fixed(positions$TE, regex("_(non-)?reference"),2)[,1]
positions$family<- paste(stringr::str_split_fixed(positions$family, "_",4)[,3],stringr::str_split_fixed(positions$family, "_",4)[,4],sep="_")
positions$family <- gsub("_$" ,"",positions$family)
positions$family <- gsub("_non-reference(.*)$" ,"",positions$family)
#select traits above BF.....this step not needed, double checking everything is above BF
selection<-filter(processed_mapping_df, log10p > BF)
#extract the count base traits
base_traits <-selection[(selection$method=="cumulative"|selection$method=="absent"| selection$method=="new" |selection$method=="reference"|selection$method=="ZERO_new"|selection$method=="ONE_new"), ]
counts<-subset(base_traits, grepl("_C$", base_traits$family))
counts$family <- gsub("_C$" ,"",counts$family)
processed_mapping_df <- distinct(dplyr::select(processed_mapping_df, -strain,-allele,-value),.keep_all=TRUE)
processed_mapping_df<- processed_mapping_df %>% distinct(trait,marker,.keep_all=TRUE)
#pull out only position traits from mappings dataframe
position_traits<-subset(selection,
grepl('^I', selection$trait) |
grepl('^V', selection$trait) |
grepl('^X', selection$trait))
#create family column
position_traits$family <- paste(stringr::str_split_fixed(position_traits$trait, "_",4)[,3],stringr::str_split_fixed(position_traits$trait, "_",4)[,4],sep="_")
position_traits$family <- gsub("_$" ,"",position_traits$family)
position_traits$family <- gsub("_non-reference(.*)$" ,"",position_traits$family)
# add position TRAIT_col family info to processed_mapping_df
processed_mapping_df<-processed_mapping_df %>%mutate(family = ifelse(processed_mapping_df$trait %in% position_traits$trait, (paste(stringr::str_split_fixed(processed_mapping_df$trait, "_",4)[,3],stringr::str_split_fixed(processed_mapping_df$trait, "_",4)[,4],sep="_")), processed_mapping_df$family))
#bind count and position traits option...choose one of below three
#selection<-rbind(counts,position_traits)
selection<-counts
#selection<-position_traits
#strip count marker and remnant marks from dataframes
selection$trait <- gsub("_C$" ,"",selection$trait)
hm$trait <- gsub("_C$" ,"",hm$trait)
processed_mapping_df$trait <- gsub("_C$" ,"",processed_mapping_df$trait)
processed_mapping_df$family <- gsub("_C$" ,"",processed_mapping_df$family)
processed_mapping_df$family <- gsub("_$" ,"",processed_mapping_df$family)
processed_mapping_df$family <- gsub("_non-reference(.*)$" ,"",processed_mapping_df$family)
processed_mapping_df<-mutate(processed_mapping_df,ID=paste(trait,peak_id,sep="_"))
copy<-processed_mapping_df
processed_mapping_df<-mutate(processed_mapping_df,SNP_col=ifelse(is.na(peak_id), "PASS","PASS"))
count_QTL<-mutate(count_QTL, trait2=gsub("_\\d+$","",trait))
processed_mapping_df<-filter(processed_mapping_df,CHROM != "MtDNA")
selection<-filter(selection,grepl('total',family))
selection<-filter(selection,method!="reference")
class_subset<- positions %>% distinct(class,family,.keep_all=TRUE) %>% dplyr::select(class,family)
selection<-arrange(selection,family,method)
label<-expression(bold(-log["10"](p)))
i="ONE_new_TRANS_total"
for (i in unique(selection$trait)){
specific_trait<- processed_mapping_df[processed_mapping_df$trait == i, ]
empty <-specific_trait[specific_trait$method==NA,]
#specific_trait_mx <- max(specific_trait$log10p)
class_TE<-unique(filter(selection,trait==i)$class)
pvalues<-filter(specific_trait,log10p !="Inf") #
specific_trait_mx <- max(pvalues$log10p) #
TE<-specific_trait$family[1]
rect_data<-filter(specific_trait,SNP_col==ifelse(is.na(peak_id), "NO", "PASS"))
plot_method<-unique(filter(selection,trait==i)$method)
plot_title<-gsub(".*_TRANS_","",i)
plot_title<-gsub("_CE$","",plot_title)
plot_title<-gsub("WBTransposon","WBT",plot_title)
plot_title<-gsub("total","Total",plot_title)
plot_title<-gsub("Total$","Total Transposons",plot_title)
plot_title<-gsub("_"," ",plot_title)
plot_title<-gsub("retrotransposon","Retrotransposons",plot_title)
plot_title<-gsub("dnatransposon","DNA Transposons",plot_title)
plot_title<-gsub("unknown","Unknown",plot_title)
plot_title<-paste(plot_title,ifelse(plot_method=="ZERO_new","(ins)",ifelse(plot_method=="absent","(abs)",ifelse(plot_method=="ONE_new", "(ins)",ifelse(plot_method=="new", "(ins)", ifelse(plot_method=="cumulative"," (all)"," (ref)"))))),sep=" ")
plot_title<-gsub("(abs)","AR",plot_title)
##check for NAs
#sapply(Mappings, function(x)all(is.na(x)))
A<- processed_mapping_df %>%
filter(trait == i)%>%
.[order(.$peak_id,na.last=FALSE),]%>%
ggplot(.)+
aes(x=POS/1e6,y=log10p)+
geom_rect(data=rect_data,mapping=aes(xmin=startPOS/1e6, xmax=endPOS/1e6, ymin=0, ymax= Inf),fill="thistle1", alpha=1)+
geom_point(aes( color=ifelse(log10p> BF & SNP_col=="PASS", 'red', 'black')),size=1)+
facet_grid(.~CHROM,scale="free_x",space = "free_x")+scale_color_identity() +
ggtitle(plot_title)+
geom_hline(aes(yintercept=BF),color="grey60",linetype="dashed")+
theme(strip.background = element_rect(fill = "white"),
strip.text.x = element_text(size = 9, colour = "black",face="bold"),
plot.title = element_text(hjust = 0.5),
panel.background = element_rect(fill = "white"),
panel.border = element_rect(color="black", size=0.5, linetype="solid", fill=NA),
panel.spacing = unit(.6, "lines"),
axis.ticks =element_line(colour = "black"),
axis.text.y = element_text(colour = "black"),
axis.text.x = element_text(colour = "black"),
axis.title=element_text(size=9,face="bold"),
plot.margin=unit(c(.1,.1,.1,.1), "cm"),
# plot.title = element_text(colour=ifelse(class_TE=="dnatransposon","navy",ifelse(class_TE=="retrotransposon","brown3","darkgoldenrod2"))),
legend.position=('none'))+
labs(x="Chromosome Position (Mb)",y=label)+
scale_y_continuous(expand=c(0,0),limits=c(0,specific_trait_mx+.075*specific_trait_mx),labels = function(x) format(x,width = 4),breaks= pretty_breaks())
#plot(A)
df <- data.frame(1,2)
blank_plot<-ggplot(df,aes(x=1,y=1)) + geom_point(color="white") +
theme(axis.text =element_blank(),axis.ticks =element_blank(),axis.title =element_blank(),panel.background = element_blank(),panel.grid = element_blank())
plist<-vector()
for (p in rect_data$peak_id){
plist<-c(plist,p)
}
plist<-sort(plist)
box_list <- lapply(c(plist),FUN=function(x){gwasPxG(i,x)})
sns <- dplyr::filter(specific_trait, aboveBF == 1 ,!is.na(peak_id))
sns <- dplyr::distinct(sns, peak_id,.keep_all=TRUE)
if (nrow(sns)>1){
crs <- plot_peak_ld(specific_trait)
#crs<-blank_plot
} else {crs<-blank_plot} #single peak, no need to check for LD, blank plot
grid.draw(grid.arrange(A,do.call("grid.arrange", c(box_list, ncol=length(plist))),crs, heights=c(.40,.30,.30)))
#(do.call("grid.arrange", c(box_list, ncol=length(plist))))
cat("\n\n\\pagebreak\n")
}
```