-
Notifications
You must be signed in to change notification settings - Fork 1
/
step2-DESeq2.R
233 lines (207 loc) · 8.1 KB
/
step2-DESeq2.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
options(stringsAsFactors = F)
rm(list = ls())
if (any(!c("colorspace", "DESeq2", "tools") %in% installed.packages())) {
if (!"devtools" %in% installed.packages()) {
install.packages("devtools", repos = "https://stat.ethz.ch/CRAN/")
}
devtools::install_github("lldelisle/usefulLDfunctions")
library(usefulLDfunctions)
safelyLoadAPackageInCRANorBioconductor("colorspace")
safelyLoadAPackageInCRANorBioconductor("DESeq2")
safelyLoadAPackageInCRANorBioconductor("tools")
} else {
library(colorspace)
library(DESeq2)
library(tools)
}
if (length(commandArgs(TRUE)) > 0) {
f <- commandArgs(TRUE)[1]
} else {
cat("Select the config file.\n")
# Ask for the config file
f <- file.choose()
}
### Check the necessary options###
if (!file.exists(f)) {
stop("This file does not exist.")
}
source(f)
if (!exists("samplesPlan")) {
stop("The config file do not have samplesPlan definition.")
}
if (!file.exists(samplesPlan)) {
stop("The file specified as samplesPlan does not exist:", samplesPlan)
}
samplesPlanDF <- read.delim(samplesPlan, check.names = FALSE)
if (!("sample" %in% colnames(samplesPlanDF))) {
stop("The samplesPlan table do not contain a column called \"sample\".")
}
setwd(dirname(samplesPlan))
if (!exists("tableWithCounts")) {
stop("The config file do not have tableWithCounts definition.")
}
if (!file.exists(tableWithCounts)) {
stop("The file specified as tableWithCounts:", tableWithCounts, "does not exists.")
}
htseqCounts <- read.delim(tableWithCounts, check.names = FALSE)
if (!"Ens_ID" %in% colnames(htseqCounts)) {
if (!exists("geneIDColCounts")) {
stop("Ens_ID is not part of the column names and geneIDColCounts is not specified.")
} else {
if (!geneIDColCounts %in% colnames(htseqCounts)) {
stop("Ens_ID is not part of the column names and geneIDColCounts:", geneIDColCounts,
"neither.")
} else {
colnames(htseqCounts)[colnames(htseqCounts) == geneIDColCounts] <- "Ens_ID"
}
}
}
# The samplesplan may contain more values than the htseqCounts
sampleNamesWithValues <- intersect(colnames(htseqCounts), samplesPlanDF$sample)
if (length(sampleNamesWithValues) < 2) {
cat("Samples in samplesplan are \n")
cat(samplesPlanDF$sample)
cat("\n")
cat("The column names of the file are \n")
cat(colnames(htseqCounts))
cat("\n")
stop("The samplesplan and the HTSeqcount table are not compatible")
}
if (!exists("RNAseqFunctionPath")) {
stop("The RNAseqFunctionPath is not provided.")
} else {
if (!file.exists(RNAseqFunctionPath)) {
stop("The file provided in RNAseqFunctionPath:", RNAseqFunctionPath, " does not exists.")
}
}
source(RNAseqFunctionPath)
# The htseqCounts dataframe have geneID as rownames
rownames(htseqCounts) <- htseqCounts$Ens_ID
htseqCounts[, "Ens_ID"] <- NULL
simplifiedSP <- simplifyDF(samplesPlanDF, sampleNamesWithValues)
possibleFactors <- setdiff(colnames(simplifiedSP), c("sample"))
if (length(possibleFactors) == 0) {
cat("Will use sample as factor.\n")
factor <- "sample"
} else if (length(possibleFactors) == 1) {
cat("Will use ", possibleFactors, " as factor.\n")
factor <- possibleFactors
} else if (!exists("factor")) {
stop("To do a DESeq2 analysis, a factor (a group definition for samples) is required. Here there are multiple possibilities.")
} else {
if (!factor %in% colnames(samplesPlanDF)) {
stop("The factor specified in the config file:", factor, " is not part of the column names of the samplesPlan table.")
} else if (!factor %in% colnames(simplifiedSP)) {
stop("There is only one value for the factor ", factor, " in the samples present in the count table.")
}
}
### DESEQ2 ANALYSIS ###
if (sum(is.na(htseqCounts)) > 0) {
cat("There are NAs in the count table. They will be replaced by 0.\n")
htseqCounts[is.na(htseqCounts)] <- 0
}
cmd <- paste("dds <- DESeqDataSetFromMatrix(countData = htseqCounts[,match(rownames(simplifiedSP),colnames(htseqCounts))], colData = simplifiedSP, design = ~ ",
factor, ")", sep = "")
eval(parse(text = cmd))
cat("Genes that are never expressed are removed\n")
dds <- dds[rowSums(counts(dds)) > 1, ]
if (exists("changeTest")) {
if (!is.logical(changeTest)) {
cat("The value provided in changeTest is not logical. The default test (Wald will be used.\n)")
changeTest <- F
}
} else {
changeTest <- F
}
if (changeTest) {
dds <- DESeq(dds, test = "LRT", reduced = ~1)
} else {
dds <- DESeq(dds)
}
res <- results(dds)
resOrdered <- res[order(res$padj), ]
### Annotation addition ###
if (exists("tableWithAnnotations")) {
if (file.exists(tableWithAnnotations)) {
ann <- read.delim(tableWithAnnotations, check.names = FALSE)
if (exists("geneIDColInAnnotations")) {
if (!geneIDColInAnnotations %in% colnames(ann)) {
cat("The geneIDColInAnnotations provided: ", geneIDColInAnnotations,
" is not part of the column names of the annotation file. Unable to match the annotation file with the results of DESeq2. The annotation file will not be added.\n")
geneIDColInAnnotations <- NA
}
} else if ("gene_id" %in% colnames(ann)) {
geneIDColInAnnotations <- "gene_id"
} else if ("Ens_ID" %in% colnames(ann)) {
geneIDColInAnnotations <- "Ens_ID"
} else {
cat("The geneIDColInAnnotations is not defined in the config file. Unable to match the annotation file with the results of DESeq2. The annotation file will not be added.\n")
geneIDColInAnnotations <- NA
}
if (!is.na(geneIDColInAnnotations)) {
annot.df <- ann[match(rownames(resOrdered), ann[, geneIDColInAnnotations]),
]
}
} else {
cat("The annotation file specified:", tableWithAnnotations, " does not exists. It will not be added.\n")
}
}
if (exists("gtfFile")) {
if (file.exists(gtfFile)) {
if (!"rtracklayer" %in% installed.packages()) {
if (!"devtools" %in% installed.packages()) {
install.packages("devtools", repos = "https://stat.ethz.ch/CRAN/")
}
devtools::install_github("lldelisle/usefulLDfunctions")
library(usefulLDfunctions)
safelyLoadAPackageInCRANorBioconductor("rtracklayer")
} else {
library(rtracklayer)
}
cat("Reading gtf file...")
gtf <- readGFF(gtfFile)
cat("Done\n")
start <- aggregate(gtf$start - 1, by = list(gene_id = gtf$gene_id), FUN = min)
end <- aggregate(gtf$end, by = list(gene_id = gtf$gene_id), FUN = max)
gtf <- unique(gtf[, c("seqid", "strand", "gene_id", "gene_name", "gene_biotype")])
annot.gtf <- gtf[match(rownames(resOrdered), gtf$gene_id), ]
annot.gtf$start <- start$x[match(rownames(resOrdered), start$gene_id)]
annot.gtf$end <- end$x[match(rownames(resOrdered), end$gene_id)]
annot.gtf <- annot.gtf[, c("gene_id", "seqid", "start", "end", "strand",
"gene_name", "gene_biotype")]
if (exists("annot.df")) {
annot.df <- cbind(annot.gtf, annot.df[, setdiff(colnames(annot.df), geneIDColInAnnotations)])
} else {
annot.df <- annot.gtf
}
} else {
cat("The gtf file specified:", gtfFile, " does not exists. It will not be added.\n")
}
}
if (exists("annot.df")) {
resToExport <- cbind(annot.df, counts(dds, normalized = TRUE)[rownames(resOrdered),
], resOrdered)
} else {
resToExport <- data.frame(Ens_ID = rownames(resOrdered), counts(dds, normalized = TRUE)[rownames(resOrdered),
], resOrdered, check.names = FALSE)
}
if (exists("outputDESeqTable")) {
dir.create(dirname(outputDESeqTable), recursive = T, showWarnings = F)
} else {
outputDESeqTable <- paste0(dirname(samplesPlan), "/DESeq2AnalysisForFactor",
factor, ".txt")
}
write.table(resToExport, outputDESeqTable, sep = "\t", row.names = F, quote = F)
cat("The result table is written in ", outputDESeqTable, "\n")
if (exists("outputSignificantTable")) {
if (is.logical(outputSignificantTable)) {
if (outputSignificantTable) {
outputSignificantTableFN <- paste0(dirname(outputDESeqTable), "/Significant_",
basename(outputDESeqTable))
write.table(subset(resToExport, padj < 0.05 & abs(log2FoldChange) > 1.5),
outputSignificantTableFN, sep = "\t", row.names = F, quote = F)
cat("The result table with significants is written in ", outputSignificantTableFN,
"\n")
}
}
}