library(ggplot2)

library(cowplot)
library(scales)
library(gridExtra)
library(grid)
library(ggplotify)
library(dplyr)
library(data.table)

dat<-fread('output/statsFiles/all.tmerge.GeneReadCoverage.stats.tsv', header=T, sep='\t')

dat <- subset(dat, seqTech=='ont-Cshl-Smarter')

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


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

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


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

dat$sampleRep <- gsub('HpreCap_', '', 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



dat <- arrange(dat, desc(readCount))
group_by(dat, seqTech, capDesign, sizeFrac, sampleRep) %>% mutate(rank=row_number()) -> dat
mutate(dat, rank=row_number()) -> dat
dat <- mutate(dat, cumSum=cumsum(readCount))
dat <- mutate(dat, cumProp=cumSum/sum(readCount))
filter(dat, rank==10)  -> cumPropTop10Genes


plotBase <- "p <- ggplot(dat, aes(x = rank, y = cumProp)) + geom_line(size=lineSize) + scale_x_log10() + ylim(0,1)+ geom_segment(data = cumPropTop10Genes, aes(x=10,xend=10,y=0,yend=cumProp), color='firebrick3',size=lineSize) + geom_segment(data = cumPropTop10Genes, aes(x=0,xend=10,y=cumProp,yend=cumProp,color='Contribution\nof top 10 genes'),size=lineSize) + labs(y='Proportion of total mapped reads', x='# genes (ranked by expression)', color='') + scale_color_manual(values = c('Contribution
of top 10 genes' = 'firebrick3')) + facet_grid( seqTech ~ capDesign + sampleRep) + geom_rect(data = cumPropTop10Genes, aes(xmin=0,xmax=10,ymin=0,ymax=cumProp),fill='firebrick3', alpha=0.3, size=0) +
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.text.x = element_text(angle = 45, hjust = 1)) + "




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/geneReadCoverage.stats/ont-Cshl-Smarter/HpreCap/ont-Cshl-Smarter_HpreCap_0+_Heart01Rep1.geneReadCoverage.stats.legendOnly.png', legendOnly, base_width=wLegendOnly, base_height=hLegendOnly)
save_plot('output/plots/geneReadCoverage.stats/ont-Cshl-Smarter/HpreCap/ont-Cshl-Smarter_HpreCap_0+_Heart01Rep1.geneReadCoverage.stats.xy.wLegend.png', pXy, base_width=wXyPlot, base_height=hXyPlot)

save_plot('output/plots/geneReadCoverage.stats/ont-Cshl-Smarter/HpreCap/ont-Cshl-Smarter_HpreCap_0+_Heart01Rep1.geneReadCoverage.stats.xy.woLegend.png', pXyNoLegend, base_width=wXyNoLegendPlot, base_height=hXyNoLegendPlot)
save_plot('output/plots/geneReadCoverage.stats/ont-Cshl-Smarter/HpreCap/ont-Cshl-Smarter_HpreCap_0+_Heart01Rep1.geneReadCoverage.stats.yx.wLegend.png', pYx, base_width=wYxPlot, base_height=hYxPlot)

save_plot('output/plots/geneReadCoverage.stats/ont-Cshl-Smarter/HpreCap/ont-Cshl-Smarter_HpreCap_0+_Heart01Rep1.geneReadCoverage.stats.yx.woLegend.png', pYxNoLegend, base_width=wYxNoLegendPlot, base_height=hYxNoLegendPlot)