library(ggplot2)
library(cowplot)
library(plyr)
library(scales)
library(gridExtra)
library(grid)
library(ggplotify)

dat <- read.table('output/statsFiles/all.spikeIns.mapping.stats.tsv', header=T, as.is=T, sep='\t')
dat <- subset(dat, seqTech=='pacBioSII-Cshl-CapTrap')

dat <- subset(dat, capDesign=='MpreCap')


dat <- subset(dat, sizeFrac=='0+')

dat <- subset(dat, sampleRep=='WBlood01Rep1')


dat$seqTech <- gsub('-', '\n', dat$seqTech)

dat$sampleRep <- gsub('MpreCap_', '', dat$sampleRep)



horizCats <- length(unique(dat$capDesign)) * length(unique(dat$sampleRep))
vertCats <- length(unique(dat$seqTech))

wXyPlot = (horizCats * 0.9) +1.7
hXyPlot = (vertCats * 0.6) + 1.7

geom_textSize=1.4 
themeSize = (14/5) * geom_textSize
# https://stackoverflow.com/questions/25061822/ggplot-geom-text-font-size-control/25062509
lineSize=geom_textSize/8
minorLineSize=lineSize/2



maxY <- max(dat$percent)
plotBase <- "p <- ggplot(data=dat, aes(x=1, y=percent, fill=category)) +
geom_bar(stat='identity', position=position_dodge()) +
geom_text(position = position_dodge(width = 0.9), size=geom_textSize, aes(y = 0, label = paste(sep='',percent(percent),'\n','(',comma(count),')')), hjust = 0, vjust = 0.5, angle=90) +
scale_fill_manual(values=c('ERCCs' = '#e4b5ff', 'SIRVs' = '#5edba9')) +
ylab('% reads mapped on\nspike-in sequences') +
xlab('') +
guides(fill = guide_legend(title='Spike-in set'))+
scale_y_continuous(labels=percent)+
expand_limits(y=c(0,maxY))+
theme(axis.ticks.x = element_blank(), axis.text.x = element_blank()) +
theme(axis.text= element_text(size=themeSize*1.8), axis.ticks = element_line(size=lineSize), axis.line = element_line(colour = '#595959', size=lineSize), axis.title=element_text(size = themeSize*2), panel.grid.major = element_line(colour='#d9d9d9', size=lineSize),panel.grid.minor = element_line(colour='#e6e6e6', size=minorLineSize),panel.border = element_blank(),panel.background = element_blank(), strip.background = element_rect(colour='#737373',fill='white'), legend.key.size=unit(0.5,'line'), legend.title=element_text(size=themeSize*1.2), legend.text=element_text(size=themeSize), strip.text = element_text(size = themeSize)) + 
theme(axis.ticks.x = element_blank(), axis.text.x = element_blank()) +
"



plotFacetXy <- parse(text =paste(plotBase, "facet_grid( seqTech ~ capDesign + sampleRep, scales='free_y')"))
plotFacetYx <- parse(text=paste(plotBase, "facet_grid( capDesign + sampleRep ~ seqTech, scales='free_y')"))

plotFacetXy <- parse(text =paste(plotFacetXy, " + theme(strip.text.x = element_blank(), strip.text.y = element_blank())"))
plotFacetYx <- parse(text=paste(plotFacetYx, " + theme(strip.text.x = element_blank(), strip.text.y = element_blank())"))

pXy <- eval(plotFacetXy)
pYx <- eval(plotFacetYx)

legend <- get_legend(pXy)

pXyNoLegend <- pXy + theme(legend.position='none')
pYxNoLegend <- pYx + theme(legend.position='none')

legendOnly <- grid.arrange(legend)
pXyGrob <- as.grob(pXy)
pYxGrob <- as.grob(pYx)
pXyNoLegendGrob <- as.grob(pXyNoLegend)
pYxNoLegendGrob <- as.grob(pYxNoLegend)


hLegendOnly <- convertUnit(sum(legend$heights), 'in', valueOnly=TRUE)
wLegendOnly <- convertUnit(sum(legend$widths), 'in', valueOnly=TRUE)


hYxPlot <- wXyPlot
wYxPlot <- hXyPlot 

hXyNoLegendPlot<- hXyPlot 
wXyNoLegendPlot<- wXyPlot - wLegendOnly

hYxNoLegendPlot<- hYxPlot
wYxNoLegendPlot<- wYxPlot - wLegendOnly


save_plot('output/plots/lrMapping.spikeIns.stats/pacBioSII-Cshl-CapTrap/MpreCap/pacBioSII-Cshl-CapTrap_MpreCap_0+_WBlood01Rep1.lrMapping.spikeIns.stats.legendOnly.png', legendOnly, base_width=wLegendOnly, base_height=hLegendOnly)
save_plot('output/plots/lrMapping.spikeIns.stats/pacBioSII-Cshl-CapTrap/MpreCap/pacBioSII-Cshl-CapTrap_MpreCap_0+_WBlood01Rep1.lrMapping.spikeIns.stats.xy.wLegend.png', pXy, base_width=wXyPlot, base_height=hXyPlot)

save_plot('output/plots/lrMapping.spikeIns.stats/pacBioSII-Cshl-CapTrap/MpreCap/pacBioSII-Cshl-CapTrap_MpreCap_0+_WBlood01Rep1.lrMapping.spikeIns.stats.xy.woLegend.png', pXyNoLegend, base_width=wXyNoLegendPlot, base_height=hXyNoLegendPlot)
save_plot('output/plots/lrMapping.spikeIns.stats/pacBioSII-Cshl-CapTrap/MpreCap/pacBioSII-Cshl-CapTrap_MpreCap_0+_WBlood01Rep1.lrMapping.spikeIns.stats.yx.wLegend.png', pYx, base_width=wYxPlot, base_height=hYxPlot)

save_plot('output/plots/lrMapping.spikeIns.stats/pacBioSII-Cshl-CapTrap/MpreCap/pacBioSII-Cshl-CapTrap_MpreCap_0+_WBlood01Rep1.lrMapping.spikeIns.stats.yx.woLegend.png', pYxNoLegend, base_width=wYxNoLegendPlot, base_height=hYxNoLegendPlot)