-
Notifications
You must be signed in to change notification settings - Fork 8
/
heatmap.R
executable file
·177 lines (143 loc) · 6.33 KB
/
heatmap.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
#!/usr/bin/env Rscript
#########################
# heatmap output module #
#########################
args=commandArgs(TRUE)
library("getopt")
spec <- matrix(c(
'query_gene' , 'g', 1, "character","gene",
'cutoff_pos' , 'p', 1, "numeric"," positive correlation cutoff, -p 0.5 ",
'cutoff_neg' , 'n', 1, "numeric"," inverse correlation cutoff, -n -0.5 ",
'cutoff_reg' , 'r', 1, "character"," interval for excleude(ex) or include(in), -r ex, -c in "
),ncol=5,byrow=T)
opt = getopt(spec);
if (is.null(opt$query_gene )) {
cat(paste(getopt(spec, usage=T),"\nExample
./heatmap.R -p 0.5 -n -0.5 -r ex -g chrX_47755339_47705503_fwd
./heatmap.R -p 0.5 -n -0.5 -r ex -g ELFN1-AS1
./heatmap.R -p 0.5 -n -0.5 -r ex -g LINC00346 \n"));
q();
}
library(pheatmap)
library(ggplot2)
library(data.table)
linc_coexp_pairs<-readRDS(paste0("output/linc_coexp_pairs.rds"))
df_opt<-readRDS(paste0("output/opt.rds"))
circ_gene_merged<-readRDS("output/circ_gene_merged.rds")
circ_gene_merged_t<-t(circ_gene_merged[,2:ncol(circ_gene_merged)])
# factor_list<-fread(df_opt["factorlist","V1"],header = F)
factor_list<-fread(paste0(getwd(),"/",df_opt["factorlist","V1"]),header = F)
factor_list[,V2:=as.factor(V2)]
query_gene<-opt$query_gene[1]
cutoff_pos<-opt$cutoff_pos[1]
cutoff_neg<-opt$cutoff_neg[1]
cutoff_reg<-opt$cutoff_reg[1]
################################# filtering #####################################
rna_idx<-ifelse( length(which("lncRNA" %in% colnames(linc_coexp_pairs))) == 1,which("lncRNA" %in% colnames(linc_coexp_pairs)),
ifelse(length(which("circRNA" %in% colnames(linc_coexp_pairs))) == 1,which("circRNA" %in% colnames(linc_coexp_pairs)),"NA"))
if (rna_idx =="NA"){q();}
rnacolumn<-linc_coexp_pairs[,colnames(linc_coexp_pairs)[rna_idx]]
linc_coexp_pairs_filtered<-linc_coexp_pairs[ rnacolumn %in% query_gene , ]
linc_coexp_pairs_filtered<-linc_coexp_pairs_filtered[!is.na(linc_coexp_pairs_filtered$cor),]
if( cutoff_reg == "ex") {
linc_coexp_pairs_filtered<-linc_coexp_pairs_filtered[which(linc_coexp_pairs_filtered$cor > cutoff_pos | linc_coexp_pairs_filtered$cor < cutoff_neg), ]
}
if(cutoff_reg == "in" ) {
linc_coexp_pairs_filtered<-linc_coexp_pairs_filtered[which(!(linc_coexp_pairs_filtered$cor > cutoff_pos & linc_coexp_pairs_filtered$cor < cutoff_neg)), ]
}
#filter list if gene l to large
gene_limit<-500
if (nrow(linc_coexp_pairs_filtered) > gene_limit ) {
rank<-rank(abs(linc_coexp_pairs_filtered$cor))
linc_coexp_pairs_filtered<-linc_coexp_pairs_filtered[which(rank> (nrow(linc_coexp_pairs_filtered)-gene_limit)),]
}
print(nrow(linc_coexp_pairs_filtered
))
####################### filtering END #########################################
# prep read count table
circ_gene_merged_t<-as.data.table(melt(circ_gene_merged_t))
circ_gene_merged_t<-as.data.frame(dcast(circ_gene_merged_t,Var1~Var2,sum))
row.names(circ_gene_merged_t)<-circ_gene_merged_t$Var1
circ_gene_merged_t$Var1<-NULL
# order correlation by cor
coexpgene_sym<-unique(linc_coexp_pairs_filtered[order(linc_coexp_pairs_filtered[,"cor"],decreasing = T),]$co_exp_gene)
heatmaprow<-c(query_gene, setdiff(coexpgene_sym, query_gene))
tmp<-circ_gene_merged_t[heatmaprow,]
sortedtab<-tmp
sortedtab$samples<-rownames(sortedtab)
sortedtab<-subset(sortedtab,select = samples)
for ( factor_levels in 1:length(levels(factor_list[[2]]))){
tmp2<-as.data.frame(tmp[heatmaprow,])
tmp2<-tmp2[,colnames(tmp2) %in% as.character(factor_list[factor_list$V2 %in% levels(factor_list$V2)[factor_levels],1][[1]])]
#for (revorder in length(rownames(tmp2)):1){
# tmp2<-tmp2[,order(tmp2[revorder,],decreasing = T)]
#tmp2<-tmp2[,order(colnames(tmp2),decreasing = F)]
# }
sortedtab<-cbind(sortedtab,tmp2)
}
heatmapann<-as.data.frame(factor_list)
rownames(heatmapann)<-heatmapann$V1
heatmapann$V1<-NULL
colnames(heatmapann)<-c("type")
sortedtab$samples<-NULL
# coloumn annotation
#
# factor_list$V2<- factor(factor_list$V2, levels = c(as.character(factor_list[[2]][1]),
# as.character(factor_list$V2[-grep(as.character(factor_list[[2]][1]),factor_list$V2)][1])))
# color_type = c( N="blue",T="red")
# ann_colors = list(type= color_type)
pdfwidth<-length(colnames(sortedtab))*0.05+5
pdfwidth<-ifelse( pdfwidth > 45,45, pdfwidth)
pdfheight <-(length(rownames(sortedtab))/length(colnames(sortedtab)))* pdfwidth*1
pdfheight<-ifelse( pdfheight > 45,45, pdfheight)
xx<-max(abs(min(scale(log2(t(sortedtab))),na.rm=T)),abs(max(scale(log2(t(sortedtab))),na.rm=T)))+0.5
xx2<-t(scale((t(log2(sortedtab)))))
qq<-max(abs(quantile(xx2, probs = 0.001,na.rm = T)),abs(quantile(xx2, probs = 0.999,na.rm = T)))
maxx<-qq
minn<--qq
xx2[xx2 < minn] <- minn # replace
xx2[xx2 > maxx] <- maxx
xx<-max(abs(min(xx2,na.rm=T)),abs(max(xx2,na.rm=T)))
breaksList = seq(-xx-1, xx+1, by = 0.1)
xx2<-melt(xx2)
ggplot(xx2, aes( Var2, Var1)) +
geom_raster(aes(fill = value)) +
scale_fill_gradientn(colours=c('blue', 'black', 'gold'),
name="Z-score")+
theme_bw(base_size = 8)+
coord_fixed(ratio=1)+
theme(
axis.text.x = element_text(angle =90, vjust = 0.5,
hjust = 1),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid.major = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
axis.ticks = element_blank()
)
par(mar=c(0,0,0,0))
ggsave(paste("output/heatmap",query_gene,cutoff_pos,cutoff_neg,cutoff_reg,".png",sep="_"),
width =pdfwidth,
height = pdfheight)
# output rds for plotly use
p<-ggplot(xx2, aes( Var2, Var1)) +
geom_raster(aes(fill = value)) +
scale_fill_gradientn(colours=c('blue', 'black', 'gold'),
name="Z-score")+
theme_bw(base_size = 8)+
#coord_fixed(ratio=1)+
theme (
axis.text.x = element_text(
angle = 90,
vjust = 0.5,
hjust = 1
) ,
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid.major = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
axis.ticks = element_blank()
)
saveRDS(p,paste("output/heatmap",query_gene,cutoff_pos,cutoff_neg,cutoff_reg,".rds",sep="_"))