-
Notifications
You must be signed in to change notification settings - Fork 5
/
DIscBIO-generic-DEGanalysis2clust.R
338 lines (330 loc) · 11 KB
/
DIscBIO-generic-DEGanalysis2clust.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#' @title Determining differentially expressed genes (DEGs) between two
#' particular clusters.
#' @description This function defines DEGs between particular clusters generated
#' by either K-means or model based clustering.
#' @param object \code{DISCBIO} class object.
#' @param Clustering Clustering has to be one of the following:
#' ["K-means","MB"]. Default is "K-means"
#' @param K A numeric value of the number of clusters.
#' @param fdr A numeric value of the false discovery rate. Default is 0.05.
#' @param name A string vector showing the name to be used to save the resulted
#' tables.
#' @param First A string vector showing the first target cluster. Default is
#' "CL1"
#' @param Second A string vector showing the second target cluster. Default is
#' "CL2"
#' @param export A logical vector that allows writing the final gene list in
#' excel file. Default is TRUE.
#' @param quiet if `TRUE`, suppresses intermediate text output
#' @param plot if `TRUE`, plots are generated
#' @param filename_deg Name of the exported DEG table
#' @param filename_sigdeg Name of the exported sigDEG table
#' @param ... additional parameters to be passed to samr()
#' @importFrom graphics title
#' @importFrom utils write.csv capture.output
#' @importFrom AnnotationDbi keys
#' @return A list containing two tables.
setGeneric(
"DEGanalysis2clust",
function(object, K, Clustering = "K-means", fdr = 0.05, name = "Name",
First = "CL1", Second = "CL2", export = FALSE, quiet = FALSE,
plot = TRUE, filename_deg = "DEGsTable", filename_sigdeg = "sigDEG",
...) {
standardGeneric("DEGanalysis2clust")
}
)
#' @export
#' @rdname DEGanalysis2clust
setMethod(
"DEGanalysis2clust",
signature = "DISCBIO",
definition = function(
object, K, Clustering, fdr, name, First, Second, export, quiet, plot,
filename_deg, filename_sigdeg, ...
) {
if (!(Clustering %in% c("K-means", "MB"))) {
stop("Clustering has to be either K-means or MB")
}
gene_list <- object@FinalGeneList
gene_names <- rownames(object@expdata)
idx_genes <- is.element(gene_names, gene_list)
gene_names2 <- gene_names[idx_genes]
dataset <- object@expdata[gene_names2, ]
Nam <- colnames(dataset)
if (Clustering == "K-means") {
Cluster_ID <- object@cpart
if (length(object@cpart) < 1) {
stop("run Clustexp before running DEGanalysis2clust")
}
}
if (Clustering == "MB") {
Cluster_ID <- object@MBclusters$clusterid
if (length(object@MBclusters$clusterid) < 1) {
stop("run ExprmclustMB before running DEGanalysis2clust")
}
}
num <- seq_len(K)
num1 <- paste("CL", num, sep = "")
for (n in num) {
Nam <- ifelse((Cluster_ID == n), num1[n], Nam)
}
colnames(dataset) <- Nam
sg1 <- dataset[, which(colnames(dataset) == First)]
sg2 <- dataset[, which(colnames(dataset) == Second)]
sg <- cbind(sg1, sg2)
sg3 <- factor(
gsub(paste0("(", First, "|", Second, ").*"), "\\1", colnames(sg)),
levels = c(paste0(First), paste0(Second))
)
sg3 <- sg3[!is.na(sg3)]
colnames(sg) <- sg3
len <- c(
length(sg[, which(colnames(sg) == First)]),
length(sg[, which(colnames(sg) == Second)])
)
y <- c(rep(1:2, len))
L <- as.matrix(sg)
gname <- rownames(sg)
x <- L
data <- list(x = x, y = y, geneid = gname)
if (quiet) {
suppressMessages(
invisible(capture.output({
samr.obj <- sammy(
data,
resp.type = "Two class unpaired",
assay.type = "seq",
testStatistic = "wilcoxon",
random.seed = 15,
...
)
delta.table <- samr.compute.delta.table(samr.obj)
}))
)
} else {
samr.obj <- sammy(
data,
resp.type = "Two class unpaired",
assay.type = "seq",
testStatistic = "wilcoxon",
random.seed = 15,
...
)
delta.table <- samr.compute.delta.table(samr.obj)
}
DEGsTable <- data.frame()
DEGsE <- vector()
DEGsS <- vector()
wm <- which.min(delta.table[, 5])
if (delta.table[wm, 5] <= fdr) {
w <- which(delta.table[, 5] <= fdr)
if (is.null(w)) stop("No suitable deltas. Try a lower FDR value.")
delta <- delta.table[w[1], 1] - 0.001
if (plot) {
samr.plot(samr.obj, delta)
title(paste("DEGs in the", Second, "in", First, "VS", Second))
}
siggenes.table <- samr.compute.siggenes.table(
samr.obj, delta, data, delta.table
)
# ------------------------------------------------------------------
# Reformat siggenes.table as data.frame
# ------------------------------------------------------------------
siggenes.table$genes.lo <- reformatSiggenes(siggenes.table$genes.lo)
siggenes.table$genes.up <- reformatSiggenes(siggenes.table$genes.up)
FDRl <- as.numeric(siggenes.table$genes.lo[, 8]) / 100
FDRu <- as.numeric(siggenes.table$genes.up[, 8]) / 100
siggenes.table$genes.lo[, 8] <- FDRl
siggenes.table$genes.up[, 8] <- FDRu
DEGsTable[1, 1] <- paste0(First, " VS ", Second)
DEGsTable[1, 2] <- Second
DEGsTable[1, 3] <- length(FDRu)
DEGsTable[1, 4] <- paste0(
"Up-regulated-", name, Second, "in", First, "VS", Second,
".csv"
)
DEGsTable[1, 5] <- length(FDRl)
DEGsTable[1, 6] <- paste0(
"Low-regulated-", name, Second, "in", First, "VS", Second,
".csv"
)
DEGsTable[2, 1] <- paste0(First, " VS ", Second)
DEGsTable[2, 2] <- First
DEGsTable[2, 3] <- length(FDRu)
DEGsTable[2, 4] <- paste0(
"Low-regulated-", name, First, "in", First, "VS", Second,
".csv"
)
DEGsTable[2, 5] <- length(FDRl)
DEGsTable[2, 6] <- paste0(
"Up-regulated-", name, First, "in", First, "VS", Second,
".csv"
)
FinalDEGsL <- data.frame()
if (length(FDRl) > 0) {
genes <- siggenes.table$genes.lo[, 3]
if (quiet) {
suppressMessages(
geneList <- AnnotationDbi::select(
org.Hs.eg.db,
keys = keys(org.Hs.eg.db),
columns = c("SYMBOL", "ENSEMBL")
)
)
GL <- c(1, "MTRNR2", "ENSG00000210082")
GL1 <- c(1, "MTRNR1", "ENSG00000211459")
geneList <- rbind(geneList, GL, GL1)
} else {
geneList <- AnnotationDbi::select(
org.Hs.eg.db,
keys = keys(org.Hs.eg.db),
columns = c("SYMBOL", "ENSEMBL")
)
GL <- c(1, "MTRNR2", "ENSG00000210082")
GL1 <- c(1, "MTRNR1", "ENSG00000211459")
geneList <- rbind(geneList, GL, GL1)
}
FinalDEGsL <- cbind(genes, siggenes.table$genes.lo)
gene_list <- geneList[, 3]
idx_genes <- is.element(gene_list, genes)
genes2 <- geneList[idx_genes, ]
if (!is.null(FinalDEGsL)) {
FinalDEGsL <- merge(
FinalDEGsL,
genes2,
by.x = "genes",
by.y = "ENSEMBL",
all.x = TRUE
)
FinalDEGsL[, 3] <- FinalDEGsL[, 11]
FinalDEGsL <- FinalDEGsL[, c(-1, -10, -11)]
FinalDEGsL <- FinalDEGsL[order(FinalDEGsL[, 8]), ]
FinalDEGsL[is.na(FinalDEGsL[, 2]), c(2, 3)] <-
FinalDEGsL[is.na(FinalDEGsL[, 2]), 3]
}
if (export) {
message("The results of DEGs are saved in your directory")
message(
"Low-regulated genes in the ", Second, " in ",
First, " VS ", Second, "\n"
)
write.csv(
FinalDEGsL,
file = paste0(
"Low-regulated-", name, Second, "in", First,
"VS", Second, ".csv"
)
)
write.csv(
FinalDEGsL,
file = paste0(
"Up-regulated-", name, First, "in", First, "VS",
Second, ".csv"
)
)
}
DEGsS <- c(DEGsS, FinalDEGsL[, 2])
DEGsE <- c(DEGsE, as.character(FinalDEGsL[, 3]))
}
FinalDEGsU <- data.frame()
if (length(FDRu) > 0) {
genes <- siggenes.table$genes.up[, 3]
if (quiet) {
suppressMessages(
geneList <- AnnotationDbi::select(
org.Hs.eg.db,
keys = keys(org.Hs.eg.db),
columns = c("SYMBOL", "ENSEMBL")
)
)
GL <- c(1, "MTRNR2", "ENSG00000210082")
geneList <- rbind(geneList, GL)
} else {
geneList <- AnnotationDbi::select(
org.Hs.eg.db,
keys = keys(org.Hs.eg.db),
columns = c("SYMBOL", "ENSEMBL")
)
GL <- c(1, "MTRNR2", "ENSG00000210082")
geneList <- rbind(geneList, GL)
}
FinalDEGsU <- cbind(genes, siggenes.table$genes.up)
gene_list <- geneList[, 3]
idx_genes <- is.element(gene_list, genes)
genes2 <- geneList[idx_genes, ]
if (!is.null(FinalDEGsU)) {
FinalDEGsU <- merge(
FinalDEGsU,
genes2,
by.x = "genes",
by.y = "ENSEMBL",
all.x = TRUE
)
FinalDEGsU[, 3] <- FinalDEGsU[, 11]
FinalDEGsU <- FinalDEGsU[, c(-1, -10, -11)]
FinalDEGsU <- FinalDEGsU[order(FinalDEGsU[, 8]), ]
FinalDEGsU[is.na(FinalDEGsU[, 2]), c(2, 3)] <-
FinalDEGsU[is.na(FinalDEGsU[, 2]), 3]
}
if (export) {
message("The results of DEGs are saved in your directory")
message(
"Up-regulated genes in the ", Second, " in ", First,
" VS ", Second, "\n"
)
write.csv(
FinalDEGsU,
file = paste0(
"Up-regulated-", name, Second, "in", First, "VS",
Second, ".csv"
)
)
write.csv(
FinalDEGsU,
file = paste0(
"Low-regulated-", name, First, "in", First, "VS",
Second, ".csv"
)
)
}
DEGsS <- c(DEGsS, FinalDEGsU[, 2])
DEGsE <- c(DEGsE, as.character(FinalDEGsU[, 3]))
}
} else {
DEGsTable[1, 1] <- paste0(First, " VS ", Second)
DEGsTable[1, 2] <- Second
DEGsTable[1, 3] <- NA
DEGsTable[1, 4] <- NA
DEGsTable[1, 5] <- NA
DEGsTable[1, 6] <- NA
DEGsTable[2, 1] <- paste0(First, " VS ", Second)
DEGsTable[2, 2] <- First
DEGsTable[2, 3] <- NA
DEGsTable[2, 4] <- NA
DEGsTable[2, 5] <- NA
DEGsTable[2, 6] <- NA
}
colnames(DEGsTable) <- c(
"Comparisons",
"Target cluster",
"Gene number",
"File name",
"Gene number",
"File name"
)
if (!quiet) print(DEGsTable)
sigDEG <- cbind(DEGsE, DEGsS)
if (export) {
write.csv(DEGsTable, file = paste0(filename_deg, ".csv"))
write.csv(sigDEG, file = paste0(filename_sigdeg, ".csv"))
}
return(
list(
sigDEG = sigDEG,
DEGsTable = DEGsTable,
FinalDEGsL = FinalDEGsL,
FinalDEGsU = FinalDEGsU
)
)
}
)