deseq2.script.R 5.56 KB
Newer Older
1
2
3
4
#log <- file(snakemake@log[[1]], open="wt")
#sink(log)
#sink(log, type="message")
library(DESeq2)
5
library(edgeR)
6
7
8
9
library(apeglm)
library(BiocParallel)
library(tximport)
library(rhdf5)
10
library(GenomicFeatures)
mmassaviol's avatar
mmassaviol committed
11
library(ggplot2)
12

13
register(MulticoreParam(snakemake@threads))
14

15
16
samples = read.table(snakemake@config[["popmap_file"]],stringsAsFactors=F)$V1
conditions = read.table(snakemake@config[["popmap_file"]],stringsAsFactors=F)$V2
17
18
19
#samples = c("DRR016125","DRR016126","DRR016127","DRR016128","DRR016129","DRR016130","DRR016131","DRR016132")
#files = file.path("/home/mbb/Documents/results2/kallisto/quant",samples,"abundance.h5")
files = sort(snakemake@input[["counts"]])
20
names(files) = samples
21

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
if (snakemake@config[["quantification"]] == "htseq_count")
{
   print("Quantification was done with htseq-count !")
   sampleTable <- data.frame(
		sampleName = samples,
		fileName   = files,
		condition  = conditions
		)
    # create the DESeq data object
    dds <- DESeqDataSetFromHTSeqCount(
		sampleTable = sampleTable, 
		directory = "", #paste0(snakemake@config[["results_dir"]],"/",snakemake@config[["<step_name>__deseq2_output_dir"]],"/"), 
		design = ~ condition
		)

} else{
    if (snakemake@params[["tx2gene"]] == TRUE){
      # conversion transcrits vers gènes
      TxDb <- makeTxDbFromGFF(file = snakemake@params[["annotations"]])
      k <- keys(TxDb, keytype = "TXNAME")
      tx2gene <- select(TxDb, k, "GENEID", "TXNAME")
      txi <- tximport(files, type = snakemake@config[["quantification"]], tx2gene = tx2gene) 
    } else {
      txi <- tximport(files, type = snakemake@config[["quantification"]], txOut = TRUE) 
    }

    groups = as.factor(conditions)
    designMat <- model.matrix(~groups)
    colnames(designMat)[2] = "condition"
    #dds$condition <- factor(dds$condition, levels = unique(conditions))

    sampleTable <- data.frame(condition = factor(conditions))
    rownames(sampleTable) <- colnames(txi$counts)

    dds <- DESeqDataSetFromTximport(txi, sampleTable, designMat)
57
58
59
60
61
}

keep <- rowSums(counts(dds)) >= 10
dds <- dds[keep,]

mmassaviol's avatar
mmassaviol committed
62
dds <- DESeq(dds) #, fitType = snakemake@config[["deseq2_fitType"]], betaPrior = as.logical(snakemake@config[["deseq2_betaPrior"]]), test="Wald")
63
64
65
66
67
68

res <- results(dds)

save.image(snakemake@output[["r_data"]])
write.csv(res, snakemake@output[["de_table"]])

mmassaviol's avatar
mmassaviol committed
69
###### PREPARE REPORT ######
70
71
72
73
74
75
76
77
# normalized, variance-stabilized transformed counts for visualization
if (sum(keep) < 1000) {
  vsd = varianceStabilizingTransformation(dds)
  } else {
    vsd <- vst(dds, blind=FALSE)
    }

dat <- plotPCA(vsd, intgroup="condition",returnData=TRUE)
mmassaviol's avatar
mmassaviol committed
78
79


80
png(filename = snakemake@output[["PCA"]], height=800, width=800)
81
82
 p <- ggplot(dat,aes(x=PC1,y=PC2,col=group))
 p + geom_point() + ggtitle("PCA on normalized, variance-stabilized transformed counts")
mmassaviol's avatar
mmassaviol committed
83
84
dev.off()

85
86
87
88
89
90
91
# cpm_counts = cpm(txi$counts)
# pca = prcomp(t(cpm_counts))

# png(filename = snakemake@output[["PCA"]], height=800, width=800)
# ggplot(data.frame(pca$x),aes(x =pca$x[,1] , y = pca$x[,2],col = conditions)) + geom_point()
# dev.off()

mmassaviol's avatar
mmassaviol committed
92
93
94
95
96
97
98
options(digit=4)

resSig <- subset(res, padj < 0.1)
resSigdf = data.frame(resSig@listData,row.names = resSig@rownames)

#datatable(resSigdf,options = list(scrollX = '300px'))

mmassaviol's avatar
mmassaviol committed
99
100
101
102
103
topGenes = head(resSigdf[order(resSigdf$padj),], 40)

cat("# id: Top_genes
# section_name: 'Top 40 genes'
# description: 'Tableau des 40 top genes'
mmassaviol's avatar
mmassaviol committed
104
# format: 'tsv'
mmassaviol's avatar
mmassaviol committed
105
# plot_type: 'table'\ngene\t", file=snakemake@output[["Top_genes"]])
mmassaviol's avatar
mmassaviol committed
106

mmassaviol's avatar
mmassaviol committed
107
write.table(topGenes,snakemake@output[["Top_genes"]],append=TRUE,sep="\t")
mmassaviol's avatar
mmassaviol committed
108

109
png(filename = snakemake@output[["MA_plot"]], height=800, width=800)
mmassaviol's avatar
mmassaviol committed
110
DESeq2::plotMA(res, ylim=c(-5,5))
mmassaviol's avatar
mmassaviol committed
111
112
dev.off()

mmassaviol's avatar
mmassaviol committed
113
png(filename = snakemake@output[["Volcano_plot"]], height=800, width=800)
mmassaviol's avatar
mmassaviol committed
114
115
116
117
with(res, plot(log2FoldChange, -log10(pvalue), pch=20, main="Volcano plot", xlim=c(-2.5,2)))
with(subset(res, padj<.05 ), points(log2FoldChange, -log10(pvalue), pch=20, col="red"))
with(subset(res, abs(log2FoldChange)>1), points(log2FoldChange, -log10(pvalue), pch=20, col="orange"))
with(subset(res, padj<.05 & abs(log2FoldChange)>1), points(log2FoldChange, -log10(pvalue), pch=20, col="green"))
mmassaviol's avatar
mmassaviol committed
118
dev.off()
mmassaviol's avatar
mmassaviol committed
119
120


121
122
123
124
125
126
# Heatmap
library(pheatmap)
select <- order(rowMeans(counts(dds,normalized=TRUE)), decreasing=TRUE)[1:20]
df <- as.data.frame(colData(dds)[,c("condition")])
ntd <- normTransform(dds)
rownames(df) = samples
127
colnames(df) = "Conditions"
128
129
130
131
assayNTD = assay(ntd)

png(filename = snakemake@output[["Heatmap"]], height=800, width=800)
pheatmap(assayNTD[select,], cluster_rows=FALSE, show_rownames=FALSE, cluster_cols=FALSE, annotation_col=df)
132
133
134

#cpmTop = cpm_counts[rownames(resSigdf),]

135
#heatmap(cpmTop,main = "Heatmap",margins = c(10,1))
mmassaviol's avatar
mmassaviol committed
136
137
dev.off()

138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#resLFC <- lfcShrink(dds, coef="groups", type="apeglm")
#resOrdered <- res[order(res$pvalue),]
 
#plotMA(res, ylim=c(-2,2))
#plotMA(resLFC, ylim=c(-2,2))
# 
# ntd <- normTransform(dds)
# 
# library("vsn")
# meanSdPlot(assay(ntd))
# 
# library("pheatmap")
# select <- order(rowMeans(counts(dds,normalized=TRUE)), decreasing=TRUE)[1:20]
# df <- as.data.frame(colData(dds)[,c("condition")])
# pheatmap(assay(ntd)[select,], cluster_rows=FALSE, show_rownames=FALSE, cluster_cols=FALSE, annotation_col=df)
# 
# 
# sampleDists <- dist(t(assay(vsd)))
# 
# library("RColorBrewer")
# sampleDistMatrix <- as.matrix(sampleDists)
# rownames(sampleDistMatrix) <- paste(vsd$condition, vsd$type, sep="-")
# colnames(sampleDistMatrix) <- NULL
# colors <- colorRampPalette( rev(brewer.pal(9, "Blues")) )(255)
# pheatmap(sampleDistMatrix,
#          clustering_distance_rows=sampleDists,
#          clustering_distance_cols=sampleDists,
#          col=colors)
# 
# plotPCA(vsd, intgroup=c("condition"))
168
#sink(NULL, type = "message")