library(ggplot2)
library(reshape)
library(scales)

data <- read.table(file="data_to_heatmap_translated.csv", sep = "\t", header=T)
data.m <- melt(data)

levelorder <- names(data)[order(gsub("HeLa.S3.DeltapinT.|HeLa.S3.WT.", "", names(data)[3:length(names(data))], ignore.case = FALSE, perl = FALSE))+2]

data.m$variable <- factor(data.m$variable, levels=levelorder)
data.m$group    <- factor(data.m$group, levels=c("bac","reg","hsa"))
data.m$time     <-gsub("HeLa.S3.DeltapinT.|HeLa.S3.WT.|.R.", "", data.m$variable, ignore.case = FALSE, perl = FALSE)
data.m$time     <- factor(data.m$time, levels=c("00_h", "02_h", "04_h", "08_h", "16_h"))

levels(data.m$group)[levels(data.m$group)=="reg"] <- "sseC"
levels(data.m$group)[levels(data.m$group)=="bac"] <- "bacterial gene"
levels(data.m$group)[levels(data.m$group)=="hsa"] <- "human gene"

levels(data.m$time)[levels(data.m$time)=="00_h"] <- "0 h >> 2 h p.i. "
levels(data.m$time)[levels(data.m$time)=="02_h"] <- "2 h >> 4 h p.i. "
levels(data.m$time)[levels(data.m$time)=="04_h"] <- "4 h >> 8 h p.i. "
levels(data.m$time)[levels(data.m$time)=="08_h"] <- "8 h >> 16 h p.i. "
levels(data.m$time)[levels(data.m$time)=="16_h"] <- "16 h >> 24 h p.i. "

pp <- ggplot(data.m, aes(variable, ID)) + geom_tile(aes(fill = value)) + scale_fill_gradient2(low = "darkblue", mid = "grey", midpoint= 0, high = "yellow") + facet_grid(group~time, scale="free", space = "free") + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + xlab("Sample") + ylab("") + theme_minimal() + theme(axis.text.x  = element_text(size=7, angle=90), axis.text.y  = element_text(size=4), axis.ticks = element_blank(), strip.text.y = element_text(size=7, angle=0), strip.text.x = element_text(size=7))
qq <- ggplot(data.m, aes(variable, ID)) + geom_tile(aes(fill = value)) + scale_fill_gradient2(low = "darkblue", mid = "grey", midpoint= 0, high = "yellow") + facet_grid(group~time, scale="free", space = "free") + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + xlab("Sample") + ylab("") + theme_minimal() + theme(axis.text.x  = element_blank(), axis.text.y  = element_text(size=4), axis.ticks = element_blank(), strip.text.y = element_text(size=7, angle=0), strip.text.x = element_text(size=7))

ggsave(file="heatmap_sseC_secreted_withlabels.pdf", pp, width = 6.5, height = 25)
ggsave(file="heatmap_sseC_secreted_withoutlabels.pdf", qq, width = 6.5, height = 25)
