############################ 
#### generate averaged FDR 
############################ 
require(data.table)
require(ggplot2)

fdr.01 <- fread('~/Dropbox/TBC-2018-single-subject-methods-accuracies/Results/yeast/full_tabular_results0.01.csv')
fdr.05 <- fread('~/Dropbox/TBC-2018-single-subject-methods-accuracies/Results/yeast/full_tabular_results0.05.csv')
fdr.1 <- fread('~/Dropbox/TBC-2018-single-subject-methods-accuracies/Results/yeast/full_tabular_results0.1.csv')
fdr.2 <- fread('~/Dropbox/TBC-2018-single-subject-methods-accuracies/Results/yeast/full_tabular_results0.2.csv')


produce_confidence_plot <- function(fdr.05, cutoff=.05, data='yeast'){ 

means = fdr.05[, list(Av.Precision = mean(Precision), Sd.Precision = sd(Precision), Av.Recall = mean(Recall), Sd.Recall=sd(Recall)), by=method]
sds = fdr.05[, list(bot.Precision = mean(Precision)-sd(Precision),  Up.Precision = mean(Precision)+sd(Precision), 
                    bot.Recall = mean(Recall) - sd(Recall), Up.Recall=mean(Recall) + sd(Recall),
                    MeanRecall = mean(Recall), MeanPrecision=mean(Precision)), by=method]

sds$Up.Precision[sds$Up.Precision > 1 ] <- 1

pts <- data.table(cbind(round(rbind(cbind(sds$bot.Precision, sds$MeanRecall),
             cbind(sds$Up.Precision, sds$MeanRecall),
             cbind(sds$MeanPrecision, sds$bot.Recall),
             cbind(sds$MeanPrecision, sds$Up.Recall)),2),
             rep(sds$method, 4)))

colnames(pts) <- c('Precision','Recall','method')
pts <- pts[!is.na(pts$Precision),]
sds <- sds[!is.na(sds$bot.Precision)]

pts$Precision <- as.numeric(pts$Precision)
pts$Recall <- as.numeric(pts$Recall)
pts$Precision[pts$Precision>1] <- 1

p1 = ggplot(data=pts, aes(x= Recall, y=Precision, col=method, size=10, shape=method))+ 
    geom_point()+ theme_minimal() + 
  scale_shape_manual(values=seq(15,21))+ xlim(0,1) + ylim(0,1)+
  geom_segment(aes(x = MeanRecall, y = bot.Precision, xend = MeanRecall, yend = Up.Precision, colour = method), data = sds)+
  geom_segment(aes(x = Up.Recall, y = MeanPrecision, xend = bot.Recall, yend = MeanPrecision, colour = method), data = sds)

# p1


# theme(plot.title = element_text(size=rel(2)),
#                                                                                               axis.text = element_text(size=rel(1.3)),
#                                                                                               axis.title = element_text(size=rel(1.7)),
#                                                                                               legend.text=element_text(size=rel(2)))
p1 
pdf(paste('~/Dropbox/TBC-2018-single-subject-methods-accuracies/Figures/', data, cutoff, '.pdf', sep=''))
print(p1)
dev.off()
}


produce_confidence_plot(fdr.01, cutoff = .01, data='yeast')
produce_confidence_plot(fdr.05, cutoff = .05, data='yeast')
produce_confidence_plot(fdr.1, cutoff = .1, data='yeast')
produce_confidence_plot(fdr.2, cutoff = .2, data='yeast')


fdr.01 <- fread('~/Dropbox/TBC-2018-single-subject-methods-accuracies/Results/cell-line data/full_tabular_results0.01.csv')
fdr.05 <- fread('~/Dropbox/TBC-2018-single-subject-methods-accuracies/Results/cell-line data/full_tabular_results0.05.csv')
fdr.1 <- fread('~/Dropbox/TBC-2018-single-subject-methods-accuracies/Results/cell-line data/full_tabular_results0.1.csv')
fdr.2 <- fread('~/Dropbox/TBC-2018-single-subject-methods-accuracies/Results/cell-line data/full_tabular_results0.2.csv')

produce_confidence_plot(fdr.01, cutoff = .01, data='cell')
produce_confidence_plot(fdr.05, cutoff = .05, data='cell')
produce_confidence_plot(fdr.1, cutoff = .1, data='cell')
produce_confidence_plot(fdr.2, cutoff = .2, data='cell')

