###Installetion des packages edgeR et DEseq2
#source("http://bioconductor.org/biocLite.R")
#biocLite("edgeR")

###########################
###edgeR --simple design###
###########################

library(edgeR)
m3_counts <- read.delim('merged_counts_m3.txt',header=TRUE,row.names='gene')
group <- factor(c(1,1,1,2,2,2))
m3 <- DGEList(counts=m3_counts,group=group,remove.zeros=TRUE)
#m3_counts = readDGE(“merged_counts_m3.txt”,header=TRUE)

##Normalisation##
d.TMM <- m3
d.TMM <- calcNormFactors(d.TMM,method="TMM")
d.RLE <- m3
d.RLE <- calcNormFactors(d.RLE,method="RLE")
d.upperquartile <- m3
d.upperquartile <- calcNormFactors(d.upperquartile,method="upperquartile")

##Estimation de la dispersion commune##
d.TMM <- estimateCommonDisp(d.TMM)
d.RLE <- estimateCommonDisp(d.RLE)
d.upperquartile <- estimateCommonDisp(d.upperquartile)

##Tests statistiques##
##On prepare les 2 conditions a comparer
condition_m3 <- m3$samples$group[1]
condition_m6 <- m3$samples$group[4]
condition_m3 <- as.character(condition_m3)
condition_m6 <- as.character(condition_m6)

##On effectue le test statistique et on écrit les résultats dans un tableau
de.TMM <- exactTest(d.TMM, pair=c(condition_m3,condition_m6), dispersion= "common")
de.RLE <- exactTest(d.RLE, pair=c(condition_m3,condition_m6), dispersion= "common")
de.upperquartile <- exactTest(d.upperquartile, pair=c(condition_m3,condition_m6), dispersion= "common")
write.table(file = "edgeR_TMM_ExactTest_condition_m3_vs_condition_m6.xls",x=de.TMM$table,sep="\t",quote=F,row.names=T,col.names=NA)
write.table(file = "edgeR_RLE_ExactTest_condition_m3_vs_condition_m6.xls",x=de.RLE$table,sep="\t",quote=F,row.names=T,col.names=NA)
write.table(file = "edgeR_upperquartile_ExactTest_condition_m3_vs_condition_m6.xls",x=de.upperquartile$table,sep="\t",quote=F,row.names=T,col.names=NA)


#J'écris le même tableau avec en plus la p-value ajustée, selon la methode BH
##Pour le n, je prends le nombre de genes totaux dans de.TMM (le nombre de lignes du tableau counts qui ne sont pas a 0)
TMM_table <- topTags(de.TMM, n=16415, adjust.method='BH')
write.table(file = "TMM_ExactTest_m3_vs_m6_adjust_method_BH.xls",x=TMM_table$table,sep="\t",quote=F,row.names=T,col.names=NA)
RLE_table <- topTags(de.RLE, n=16415, adjust.method='BH')
write.table(file = "RLE_ExactTest_m3_vs_m6_adjust_method_BH.xls",x=RLE_table$table,sep="\t",quote=F,row.names=T,col.names=NA)
upperquartile_table <- topTags(de.upperquartile, n=16415, adjust.method='BH')
write.table(file = "upperquartile_ExactTest_m3_vs_m6_adjust_method_BH.xls",x=upperquartile_table$table,sep="\t",quote=F,row.names=T,col.names=NA)


##On determine le seuil de la p-value
pv <- 0.05

##On selectionne la liste des gènes ayant une p-value ajustée < pv et on les écrit dans un tableau

sum.TMM <- summary(decideTestsDGE(de.TMM, p.value=pv))
ntop.TMM <- sum.TMM[1] + sum.TMM[3]
top_tgw.TMM <- topTags(de.TMM,n=ntop.TMM)
write.table(file = paste("edgeR_TMM_ExactTest_condition_m3_vs_condition_m6_p_val_",pv,".xls",sep=''),x=top_tgw.TMM$table,sep="\t",quote=F,row.names=T,col.names=NA)

sum.RLE <- summary(decideTestsDGE(de.RLE, p.value=pv))
ntop.RLE <- sum.RLE[1] + sum.RLE[3]
top_tgw.RLE <- topTags(de.RLE,n=ntop.RLE)
write.table(file = paste("edgeR_RLE_ExactTest_condition_m3_vs_condition_m6_p_val_",pv,".xls",sep=''),x=top_tgw.RLE$table,sep="\t",quote=F,row.names=T,col.names=NA)

sum.upperquartile <- summary(decideTestsDGE(de.upperquartile, p.value=pv))
ntop.upperquartile <- sum.upperquartile[1] + sum.upperquartile[3]
top_tgw.upperquartile <- topTags(de.upperquartile,n=ntop.upperquartile)
write.table(file = paste("edgeR_upperquartile_ExactTest_condition_m3_vs_condition_m6_p_val_",pv,".xls",sep=''),x=top_tgw.upperquartile$table,sep="\t",quote=F,row.names=T,col.names=NA)

## plotSmear: plot de la quantité du gène dans l'IP vs le log du fold-change entre les conditions
pdf(file = paste("edgeR_TMM_ExactTest_condition_m3_vs_condition_m6_p_val_",pv,".pdf",sep=''),height= 600, width= 600)
plotSmear(d.TMM,pair=c(condition_m3,condition_m6), de.tags= rownames(top_tgw.TMM$table), main = "FC plot using common dispersion m3 vs m6", ylab="logFC m6 - m3")
abline(h = c(-1,1), col= "dodgerblue")
dev.off()
pdf(file = paste("edgeR_RLE_ExactTest_condition_m3_vs_condition_m6_p_val_",pv,".pdf",sep=''),height= 600, width= 600)
plotSmear(d.RLE,pair=c(condition_m3,condition_m6), de.tags= rownames(top_tgw.RLE$table), main = "FC plot using common dispersion m3 vs m6", ylab="logFC m6 - m3")
abline(h = c(-1,1), col= "dodgerblue")
dev.off()
pdf(file = paste("edgeR_upperquartile_ExactTest_condition_m3_vs_condition_m6_p_val_",pv,".pdf",sep=''),height= 600, width= 600)
plotSmear(d.upperquartile,pair=c(condition_m3,condition_m6), de.tags= rownames(top_tgw.upperquartile$table), main = "FC plot using common dispersion m3 vs m6", ylab="logFC m6 - m3")
abline(h = c(-1,1), col= "dodgerblue")
dev.off()

## Scatterplot log2 des données normalisées deux à deux, pour comparer les replicats entre eux
#jpeg(filename="edgeR-TMM_m3_vs_m6_scatterplot.jpeg",width= 1300, height= 900, quality= 100, bg= "white", res= NA)
#pairs(log2(d.TMM$pseudo.counts[,1:3]),xlim=c(0,20),ylim=c(0,20),pch=46)
#dev.off()

