-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCattle_Genome_Functional_Annotation_ChromHMM.R
189 lines (142 loc) · 7.99 KB
/
Cattle_Genome_Functional_Annotation_ChromHMM.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
############################################################################################
############################################################################################
###15 chromatin states predicted using ChromHMM with 4 histone marks, ATAC, and CTCF########
##load required R packages
library(data.table)
if (!require("data.table")) {
install.packages("data.table", dependencies = TRUE)
library(data.table)
}
###heatplot
if (!require("gplots")) {
install.packages("gplots", dependencies = TRUE)
library(gplots)
}
if (!require("RColorBrewer")) {
install.packages("RColorBrewer", dependencies = TRUE)
library(RColorBrewer)
}
##1. we predicted the chromatin states using ChromHMM model with 15 states
###################################################################################
#set working dir
setwd("YourWdir")
Control_segments <- read.table("ChromHMM_results",header = F,sep="\t",stringsAsFactors = F)
head(Control_segments)
dim(Control_segments)
##reorder the original states
Original_Order <- paste("E",rev(c(10:8,6:5,11,7,4,2,3,1,15,14,13,12)),sep = "")
##give biological names for each state based on the biological functions
Biological_Names <- c("15 Quies", "14 ReprPCWk","13 ReprPC","12 BivFlnk","11 ReprWkCTCF",
"10 ATAC","9 EnhWkCTCFATAC","8 EnhPoisATAC","7 EnhPois","6 EnhWk",
"5 EnhAATAC","4 EnhA", "3 TxFlnk","2 TssAFlnk","1 TssA")
names(Biological_Names) <- Original_Order
ChromHMM_names <- Control_segments
##replace the original state names from ChromHMM with biologically interpretable names
##then reorder the states based on their biological functions, first active TSS, followed by enhancer, and then repressed regions
colnames(ChromHMM_names) <- c("Chr","Start","End","State")
ChromHMM_names$Biological_Names <- Biological_Names[ChromHMM_names$State]
head(ChromHMM_names)
##write out the ChromHMM results
write.table(ChromHMM_names[,c(1:3,5)],file = "ChromHMM_REPC.bed",append = F,quote = F,sep = "\t",row.names = F,col.names = T)
###################################################################################
###################################################################################
###plot the emissions for each chromatin state
##. input emissions
emissions <- read.table("emissions_15.txt",header = T,stringsAsFactors = F,sep="\t")
head(emissions)
emissions <- emissions[,c("H3K4me3","H3K4me1","H3K27Ac","ATAC","CTCF","H3K27me3")]
head(emissions)
emissions_order <- emissions[c(10:8,6:5,11,7,4,2,3,1,15,14,13,12),]
my_palette <- colorRampPalette(c("white","darkblue"))(n = 199)
col_breaks = c(seq(0,0.5,length=100),
seq(0.51,1,length=100))
tiff(file = "OutputDir/Emission.tiff",
res = 300, width = 800, height = 2400,compression = "lzw")
heatmap.2(as.matrix(emissions_order),
notecol="black", # change font color of cell labels to black
density.info="none", # turns off density plot inside color legend
trace="none", # turns off trace lines inside the heat map
margins =c(7,2),
key.par=list(mar=c(3.5,1,5,0)), # widens margins around plot
col=my_palette, # use on color palette defined earlier
breaks=col_breaks, # enable color transition at specified limits
dendrogram="none", # only draw a row dendrogram
labRow = NA,
#labRow=NA,
labCol = colnames(emissions),
cexRow=1.5,cexCol=1.5,
#adjRow=c(15,0),
key=T, keysize=1.2,key.title=NA,Colv=F,Rowv=F,
symm=T,symkey=F,symbreaks=T, scale="none",srtCol=90, adjCol = c(0.95,0),
key.xlab=expression(paste(italic(Emissions),sep="")))
dev.off()
##############################################################################################
##############################################################################################
###plot enrichment folds of chromatin states for genome annotations obtained by using ChromHMM
Overlaps <- read.table("cell1_15_overlap.txt",header = T,stringsAsFactors = F,sep="\t")
head(Overlaps)
Overlaps_order <- Overlaps[c(10:8,6:5,11,7,4,2,3,1,15,14,13,12),]
colnames(Overlaps_order) <- gsub(".bed.gz","",colnames(Overlaps_order))
colnames(Overlaps_order)
##plot genome coverage percentage for each chromatin state
Genome_coverage <- Overlaps_order[,c("state..Emission.order.","Genome..")]
my_palette <- colorRampPalette(c("white","darkblue"))(n = 199)
col_breaks = c(seq(0,2,length=100),
seq(2.1,4,length=100))
tiff(file = "OutputDir/Genome_coverage.tiff",
res = 300, width = 450, height = 2400,compression = "lzw")
heatmap.2(as.matrix(Genome_coverage[,c(2,2)]),
notecol="black", # change font color of cell labels to black
density.info="none", # turns off density plot inside color legend
trace="none", # turns off trace lines inside the heat map
margins =c(7,2),
key.par=list(mar=c(3.5,1,5,0)), # widens margins around plot
col=my_palette, # use on color palette defined earlier
breaks=col_breaks, # enable color transition at specified limits
dendrogram="none", # only draw a row dendrogram
labRow = NA,
#labRow=NA,
labCol = NA,
cexRow=1.5,cexCol=1.5,
#adjRow=c(15,0),
key=F, keysize=1.2,key.title=NA,Colv=F,Rowv=F,
symm=T,symkey=F,symbreaks=T, scale="none",srtCol=90, adjCol = c(0.95,0),
key.xlab=expression(paste(italic(Enrich_Fold),sep="")))
dev.off()
#################plot the enrichment folds of 15 chromatin states for multiple Genome annotations,
#################including Gene features, expressed/repressed genes tissue-sepcific genes and repeats
colnames(Overlaps_order)
Overlaps_order_gene <- Overlaps_order[,c("state..Emission.order.","G_CpG_island","G_Genic",
"G_CDS","G_Introns","G_FiveUTR","G_Promoter","G_TSS",
"G_TES","G_ThreeUTR","E_Expr","E_Expr_TSS","E_Expr_TES",
"E_Repr","E_Repr_TSS","E_Repr_TES","G_ZNF","C_TF","REPC_sg",
"R_LINE","R_SINE","R_Satellite","R_Simple_repeat","R_LTR")]
Mynames <- c("CpG_island","Genes","CDS","Introns","5'UTR","Promoter","TSS","TES",
"3'UTR","Expr_genes","Expr_TSS","Rxpr_TES","Repr_genes","Repr_TSS","Repr_TES",
"ZNF_genes","TF","REPC_SG","LINE","SINE","Satellite","Simple_repeat","LTR")
my_palette <- colorRampPalette(c("white","red"))(n = 199)
col_breaks = c(seq(0,5,length=100),
seq(5.1,10,length=100))
tiff(file = "OutputDir/Overlap_Genome_Annotation.tiff",
res = 300, width = 3800, height = 2300,compression = "lzw")
heatmap.2(as.matrix(Overlaps_order_gene[,-c(1)]),
cellnote = round(as.matrix(Overlaps_order_gene[,-c(1)]),digits = 2), # same data set for cell labels
notecol="black", # change font color of cell labels to black
density.info="none", # turns off density plot inside color legend
trace="none", # turns off trace lines inside the heat map
margins =c(10,2),
key.par=list(mar=c(3.5,1,5,0)), # widens margins around plot
col=my_palette, # use on color palette defined earlier
breaks=col_breaks, # enable color transition at specified limits
dendrogram="none", # only draw a row dendrogram
labRow = NA,
#labRow=NA,
labCol = Mynames,
cexRow=1.5,cexCol=1.5,
#adjRow=c(15,0),
key=F,
keysize=0.3,key.title=NA,Colv=F,Rowv=F,
symm=T,symkey=F,symbreaks=T,
scale="none",srtCol=90, adjCol = c(0.95,0),
key.xlab=expression(paste(italic(Overlap),sep="")))
dev.off()