This repository has been archived by the owner on Jul 30, 2019. It is now read-only.
forked from shendurelab/MAP-C
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hi-C.R
315 lines (278 loc) · 14.3 KB
/
Hi-C.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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# HiC.R
# plotting of HiC data
# Seungsoo Kim
setwd("/Volumes/shendure-vol8/projects/mutagenesis.3C/nobackup/MAP-C")
# load libraries ----
library(ggplot2)
library(dplyr)
library(reshape)
library(RColorBrewer)
library(data.table)
library(grid)
library(gplots)
library(scales)
library(permute)
library(gridExtra)
#library(Cairo)
# output directory ----
dir.create("figures")
outdir <- "figures"
# color palette
brewercols <- brewer.pal(4,"Set1")
# plot themes ----
paper.fig.rot <- theme(panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.background=element_rect(fill="white",color="white"),
plot.background=element_rect(fill="white",color="white"),
text=element_text(color="black",size=7),
axis.text=element_text(color="black",size=7),
axis.text.y=element_text(angle=90,hjust=0.5),
axis.line=element_line(color="black"),
axis.ticks=element_line(color="black"),
legend.background=element_rect(fill="white"))
paperhm <- theme(axis.line = element_blank(),
axis.title = element_blank(),
legend.key.size = unit(0.5,"cm"),
legend.title = element_text(size=7),
legend.text = element_text(size=7))
plot_refs <- read.table("plotting_refs.txt",stringsAsFactors=FALSE,sep = "\t")
colnames(plot_refs) <- c("ref","g1","g2","g1p","g2p","rDNA1","rDNA2","bin1","bin2","HAS1TDA1g1","HAS1TDA1g2")
plotting <- read.table("matrices.txt",stringsAsFactors = FALSE)
colnames(plotting) <- c("expt.sample","ref","bsize","mask","filt")
plotting$matr <- paste("data/",plotting$expt.sample,".",plotting$ref,".",plotting$bsize,".matrix.txt",sep="")
plotting$rsum <- paste("data/",plotting$expt.sample,".",plotting$ref,".",plotting$bsize,".rowsums.txt",sep="")
loci <- data.frame("cer"=c(44+36,211+18,355+15,292,120+13,266+15),
"uva"=c(391+28,594+17,723+15,672,507+13,646+14),
"name"=c("HXT3","YKR075C","SKS1","TDA1","MIG1","ILV2"),
"cerchr"=c("Scer_4","Scer_11","Scer_16","Scer_13","Scer_7","Scer_13"),
"uvachr"=c("Sbay_2","Sbay_11","Sbay_16","Sbay_13","Sbay_7","Sbay_13"))
loci$name <- as.character(loci$name)
# make figures ----
js <- 1:nrow(plotting)
for (j in js) {
expt.sample <- plotting[j,]$expt.sample
print(expt.sample)
mask <- plotting[j,]$mask
ref <- plotting[j,]$ref
bsize <- plotting[j,]$bsize
filt <- plotting[j,]$filt
matr <- plotting[j,]$matr
rsum <- plotting[j,]$rsum
# get reference information
g1 <- plot_refs[plot_refs$ref==ref,]$g1
g2 <- plot_refs[plot_refs$ref==ref,]$g2
g1p <- plot_refs[plot_refs$ref==ref,]$g1p
g2p <- plot_refs[plot_refs$ref==ref,]$g2p
nbin1 <- plot_refs[plot_refs$ref==ref,]$bin1
nbin2 <- plot_refs[plot_refs$ref==ref,]$bin2
rDNA1 <- plot_refs[plot_refs$ref==ref,]$rDNA1
rDNA2 <- plot_refs[plot_refs$ref==ref,]$rDNA2
rDNA <- c(rDNA1,rDNA2)
# set path to save output
outdir <- "figures"
# load ref annotations
chrannot <- read.table(paste(ref,".",bsize,".chr_annotations.txt",sep=""))
binannot <- read.table(paste(ref,".",bsize,".bin_annotations.txt",sep=""))
# load data
raw <- read.table(matr)
# normalized matrix
normed.melt <- raw
if (any(is.na(normed.melt$V4))) {
normed.melt[is.na(normed.melt$V4),]$V4 <- NA
}
#add bin annotations
annotated <- merge(normed.melt,binannot,by.x="V1",by.y="V1")
colnames(annotated) <- c("bin1","bin2","raw","norm","chr1","cen1","arm1","chrn1")
annotated <- merge(annotated,binannot,by.x="bin2",by.y="V1")
colnames(annotated) <- c("bin2","bin1","raw","norm","chr1","cen1","arm1","chrn1","chr2","cen2","arm2","chrn2")
annotated <- annotated[order(annotated$bin1,annotated$bin2),]
annotated$bins <- paste(annotated$bin1,annotated$bin2,sep="-")
annotated$tel1 <- annotated$arm1-annotated$cen1
annotated$tel2 <- annotated$arm2-annotated$cen2
# load loci
loci.merged <- merge(loci,chrannot,by.x="cerchr",by.y="V1")
colnames(loci.merged) <- c("cerchr","cer","uva","name","uvachr","cerst","cerend","cercen","cerno")
loci.merged <- merge(loci.merged,chrannot,by.x="uvachr",by.y="V1")
colnames(loci.merged) <- c("uvachr","cerchr","cer","uva","name","cerst","cerend","cercen","cerno","uvast","uvaend","uvacen","uvano")
loci.merged$locino <- factor(loci.merged$name,levels=loci$name)
loci.merged <- loci.merged[order(loci.merged$locino),]
# loop through loci, making heatmaps centered at pairing between S. cer and S. uva copies of each locus
# for Figure 3--figure supplement 2
for (l in 1:nrow(loci.merged)) {
Lg1 <- loci.merged$cer[l]
Lg2 <- loci.merged$uva[l]
window <- 5
subbins1 <- seq(Lg1-window,Lg1+window)
subbins2 <- seq(Lg2-window,Lg2+window)
annotated.sub <- subset(annotated,bin1 %in% subbins1 & bin2 %in% subbins2)
pdf(paste(outdir,"/",loci.merged$name[l],"_",expt.sample,"_heatmap.pdf",sep=""),0.8,0.8)
p <- ggplot(annotated.sub) + geom_tile(aes(x=bin1,y=bin2,fill=norm)) +
geom_vline(aes(xintercept=loci.merged$cerst[l]-.5),size=0.25) +
geom_vline(aes(xintercept=loci.merged$cerend[l]-.5),size=0.25) +
geom_hline(aes(yintercept=loci.merged$uvast[l]-.5),size=0.25) +
geom_hline(aes(yintercept=loci.merged$uvaend[l]-.5),size=0.25) +
paperhm +
coord_fixed() +
scale_fill_gradientn(colors=c("white","orange","red","black"), limits=c(0,3), oob=squish, na.value="grey50", name = "O/E") +
theme(legend.position="none",text=element_text(size=6)) +
scale_x_continuous(#breaks=c(subbins1[1]-0.5,subbins1[length(subbins1)]+.5),
breaks=c(subbins1[1]+5),
labels=c(loci.merged$name[l]),
limits=c(subbins1[1]-.5,subbins1[1]+.5+2*window),
#labels=c(bsize/1000*(subbins1[1]-(chrannot[chrannot$V1==paste(g1,"_4",sep=""),]$V2)),bsize/1000*(subbins1[length(subbins1)]-(chrannot[chrannot$V1==paste(g1,"_4",sep=""),]$V2))),
expand=c(0,0)) +
scale_y_continuous(breaks=c(subbins2[1]+5),
labels=c(loci.merged$name[l]),
limits=c(subbins2[1]-.5,subbins2[1]+.5+2*window),
#breaks=c(subbins2[1]-0.5,subbins2[length(subbins2)]+.5),
#labels=c(bsize/1000*(subbins2[1]-(chrannot[chrannot$V1==paste(g2,"_2",sep=""),]$V2)),bsize/1000*(subbins2[length(subbins2)]-(chrannot[chrannot$V1==paste(g2,"_2",sep=""),]$V2))),
expand=c(0,0)) + theme(axis.text.y = element_text(hjust=0.5,angle=90))
grid.newpage()
gt <- ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name == "panel"] <- "off"
chart <- arrangeGrob(gt)
grid.draw(chart)
grid.force()
dev.off()
}
# heatmap of interactions among motif clusters
# for Figure 3--figure supplement 4
subbins <- c(loci.merged$cer,loci.merged$uva)
names <- c(loci.merged$name,loci.merged$name)
annotated.sub <- subset(annotated,bin1 %in% subbins & bin2 %in% subbins)
annotated.sub$bin1 <- factor(annotated.sub$bin1,levels=subbins)
annotated.sub$bin2 <- factor(annotated.sub$bin2,levels=subbins)
pdf(paste(outdir,"/all_loci_",expt.sample,"_heatmap.pdf",sep=""),2.2,2.2)
p <- ggplot(annotated.sub) + geom_tile(aes(x=bin1,y=bin2,fill=norm)) +
paperhm +
theme_classic() +
coord_fixed() +
scale_fill_gradientn(colors=c("white","orange","red","black"), limits=c(0,5), oob=squish, na.value="grey50", name = "O/E") +
theme(plot.margin=unit(c(0.2,0.2,0.2,0.2),"cm"),legend.position="none") +
geom_hline(aes(yintercept=nrow(loci.merged)+0.5)) +
geom_vline(aes(xintercept=nrow(loci.merged)+0.5)) +
scale_x_discrete(labels=names) +
scale_y_discrete(labels=names) + theme(axis.text.x = element_text(angle=90,hjust=1,vjust=0.5,size=8,color="black"),axis.text.y = element_text(size=8,color="black")) + xlab("") + ylab("")
grid.newpage()
gt <- ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name == "panel"] <- "off"
chart <- arrangeGrob(gt)
grid.draw(chart)
dev.off()
# heatmap of interactions among HAS1/TDA1 and HXT3
# for Figure 3--figure supplement 3
HAS1TDA1g1 <- 292
HAS1TDA1g2 <- 672
HXT3g1 <- 80
HXT3g2 <- 419
subbins <- c(HAS1TDA1g1,HAS1TDA1g2,HXT3g1,HXT3g2)
annotated.sub <- subset(annotated,bin1 %in% subbins & bin2 %in% subbins)
annotated.sub$bin1 <- factor(annotated.sub$bin1,levels=subbins)
annotated.sub$bin2 <- factor(annotated.sub$bin2,levels=subbins)
pdf(paste(outdir,"/HAS1-HXT3_",expt.sample,"_heatmap.pdf",sep=""),1.8,1.8)
p <- ggplot(annotated.sub) + geom_tile(aes(x=bin1,y=bin2,fill=norm)) +
paperhm +
theme_classic() +
coord_fixed() +
scale_fill_gradientn(colors=c("white","orange","red","black"), limits=c(0,5), oob=squish, na.value="grey50", name = "O/E") +
theme(plot.margin=unit(c(0.2,0.2,0.8,0.8),"cm"),legend.position="none") +
scale_x_discrete(expand=c(0,0),labels=c("Sc","Su","Sc","Su")) + scale_y_discrete(expand=c(0,0),labels=c("Sc","Su","Sc","Su")) + xlab("") + ylab("") +
annotation_custom(grob=textGrob(label="HXT3pr",hjust=1,rot=0,gp=gpar(fontsize=8,fontface="italic")),ymin=3.5,ymax=3.5,xmin=-0.6,xmax=-0.6) +
annotation_custom(grob=textGrob(label="HAS1pr-\nTDA1pr",hjust=1,rot=0,gp=gpar(fontsize=8,fontface="italic")),ymin=1.5,ymax=1.5,xmin=-0.6,xmax=-0.6) +
annotation_custom(grob=textGrob(label="HXT3pr",hjust=0.5,vjust=1,rot=0,gp=gpar(fontsize=8,fontface="italic")),ymin=-0.4,ymax=-0.4,xmin=3.5,xmax=3.5) +
annotation_custom(grob=textGrob(label="HAS1pr-\nTDA1pr",hjust=0.5,vjust=1,rot=0,gp=gpar(fontsize=8,fontface="italic")),ymin=-0.4,ymax=-0.4,xmin=1.5,xmax=1.5)
grid.newpage()
gt <- ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name == "panel"] <- "off"
chart <- arrangeGrob(gt)
grid.draw(chart)
dev.off()
# HXT3-centered heatmap, for Figure 3D
window <- 5
subbins1 <- seq(HXT3g1-window,HXT3g1+window)
subbins2 <- seq(HXT3g2-window,HXT3g2+window)
annotated.sub <- subset(annotated,bin1 %in% subbins1 & bin2 %in% subbins2)
png(paste(outdir,"/HXT3_",expt.sample,"_heatmap.png",sep=""),1.,1.,"in",res=1200)
p <- ggplot(annotated.sub) + geom_tile(aes(x=bin1,y=bin2,fill=norm)) +
paperhm +
coord_fixed() +
scale_fill_gradientn(colors=c("white","orange","red","black"), limits=c(0,3), oob=squish, na.value="grey50", name = "O/E") +
theme(legend.position="none",text=element_text(size=6)) +
scale_x_continuous(breaks=c(subbins1[1]-0.5,subbins1[length(subbins1)]+.5),
labels=c(bsize/1000*(subbins1[1]-(chrannot[chrannot$V1==paste(g1,"_4",sep=""),]$V2)),bsize/1000*(subbins1[length(subbins1)]-(chrannot[chrannot$V1==paste(g1,"_4",sep=""),]$V2)+1)),
expand=c(0,0)) +
scale_y_continuous(breaks=c(subbins2[1]-0.5,subbins2[length(subbins2)]+.5),
labels=c(bsize/1000*(subbins2[1]-(chrannot[chrannot$V1==paste(g2,"_2",sep=""),]$V2)),bsize/1000*(subbins2[length(subbins2)]-(chrannot[chrannot$V1==paste(g2,"_2",sep=""),]$V2)+1)),
expand=c(0,0))
grid.newpage()
gt <- ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name == "panel"] <- "off"
chart <- arrangeGrob(gt)
grid.draw(chart)
grid.force()
dev.off()
# save comparisons for violin plot
HXTcomp <- subset(annotated,
cen1 >= 15 & cen2 >= 15 &
tel1 > 1 & tel2 > 1 &
bin1 < chrannot[chrannot$V1==paste(g2,"_1",sep=""),2] &
bin2 >= chrannot[chrannot$V1==paste(g2,"_1",sep=""),2] &
bin1 != HAS1TDA1g1 & bin2 != HAS1TDA1g2 &
bin1 != HAS1TDA1g1-1 & bin2 != HAS1TDA1g2-1 &
bin1 != HAS1TDA1g1+1 & bin2 != HAS1TDA1g2+1 &
(chr1 != rDNA1 | chr2 != rDNA2))
HXTpair <- data.frame(subset(annotated,bin1==HXT3g1 & bin2==HXT3g2)$norm)
HXTcomp.df <- as.data.frame(HXTcomp$norm)
write.table(HXTcomp.df,paste(outdir,"/HXT3_",expt.sample,"_comp.txt",sep=""),row.names=FALSE,col.names=FALSE,quote=FALSE,sep=" ")
write.table(HXTpair,paste(outdir,"/HXT3_",expt.sample,"_pair.txt",sep=""),row.names=FALSE,col.names=FALSE,quote=FALSE,sep=" ")
}
# HXT3 violin plot
all_HXT3 <- data.frame()
filelist <- c(paste(outdir,"/HXT3_ILY456_exponential_Sau3AI",sep=""),
paste(outdir,"/HXT3_ILY456_exponential_rep2_Sau3AI",sep=""),
paste(outdir,"/HXT3_YMD3920_exponential_Sau3AI",sep=""),
paste(outdir,"/HXT3_ILY456_saturated_Sau3AI",sep=""),
paste(outdir,"/HXT3_YMD3920_saturated_Sau3AI",sep=""),
paste(outdir,"/HXT3_YMD3266_saturated_Sau3AI",sep=""),
paste(outdir,"/HXT3_YMD3267_saturated_Sau3AI",sep=""),
paste(outdir,"/HXT3_YMD3268_saturated_Sau3AI",sep=""),
paste(outdir,"/HXT3_YMD3269_saturated_Sau3AI",sep="")
)
samplist <- c("exp WT 1",
"exp WT 2",
"exp +GATC",
"sat WT",
"sat +GATC",
"sat ymr285-296",
"sat ymr290",
"sat ymr290coding",
"sat ymr290intergenic"
)
for (i in 1:length(filelist)) {
toadd <- read.table(paste(filelist[i],"_comp.txt",sep=""))
toadd$V2 <- samplist[i]
toadd$V3 <- "comp"
toadd_h <- read.table(paste(filelist[i],"_pair.txt",sep=""))
toadd_h$V2 <- samplist[i]
toadd_h$V3 <- "pair"
all_HXT3 <- rbind(all_HXT3,toadd)
all_HXT3 <- rbind(all_HXT3,toadd_h)
}
# for paper, Figure 3E
pdf(paste(outdir,"/HXT3_violin.pdf",sep=""),2.4,1.4)
ggplot(subset(all_HXT3,V3=="comp")) +
geom_violin(aes(x=factor(V2,levels=samplist),y=V1),fill="darkgrey") +
paper.fig.rot +
geom_point(data=subset(all_HXT3,V3=="pair"),aes(x=factor(V2,levels=samplist),y=V1),color="red",shape="-",size=7)+
xlab("") + ylab("") + theme(text=element_text(size=8),axis.text.y=element_text(size=8),axis.text.x=element_blank()) +
scale_x_discrete(labels=samplist)
dev.off()
# labeled
pdf(paste(outdir,"/HXT3_violin_labeled.pdf",sep=""),3.5,3)
ggplot(subset(all_HXT3,V3=="comp")) +
geom_violin(aes(x=factor(V2,levels=samplist),y=V1),fill="darkgrey") +
paper.fig.rot +
geom_point(data=subset(all_HXT3,V3=="pair"),aes(x=factor(V2,levels=samplist),y=V1),color="red",shape="-",size=7)+
xlab("") + ylab("Normalized interaction frequency") + theme(text=element_text(size=8),axis.text.y=element_text(size=8),axis.text.x=element_text(size=8,angle=90,hjust=0)) +
scale_x_discrete(labels=samplist)
dev.off()