forked from MBrooks313/R_codes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
et_edgeR.R
70 lines (56 loc) · 2.14 KB
/
et_edgeR.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
et_edgeR <- function(dge, expt.nm, fc, fdr, res.path = "../data/processed/"){
#############################################
# Written by MJB on Sept 19th, 2019, last modified on
# dge = dge list object of data
# expt.nm = expt name to be used in return and exported tables
# fc = fold change to filter for significance
# fdr = false discovery rate to filter for significance
# res.path = path for writing results tables
#############################################
#############################################
# Change log
#
# ***Testing variables at bottom
#############################################
library(edgeR)
library(plyr); library(dplyr)
library(tibble)
# DGElist object for the samples needed
y <- dge
# Estimate dispersions needed for exact test
y <- estimateCommonDisp(y)
y <- estimateTagwiseDisp(y)
# Perform DE exact test
et <- exactTest(y)
et$table$FDR <- p.adjust(et$table$PValue, method = "fdr")
# Filter for significance
et.sig <- et$table %>%
rownames_to_column('gene') %>%
filter(FDR < fdr, abs(logFC) >= fc) %>%
column_to_rownames('gene')
# Merge results tables
et_results <- list(all = data.frame(merge(et$genes, et$table, by = 0), row.names = 1),
sig = data.frame(merge(et$genes[row.names(et.sig),], et.sig, by = 0), row.names = 1))
# Export results
write.table(et_results$all,
paste0(res.path, "/DE.et.", expt.nm, ".all.tsv"),
sep = "\t", quote = F, col.names = NA)
write.table(et_results$sig,
paste0(res.path, "/DE.et.", expt.nm, ".sig.tsv"),
sep = "\t", quote = F, col.names = NA)
# Generate results summaries
results <- decideTests(et, p.value=fdr, lfc = fc)
d <- summary(results)
# Print out results summaries
# print(paste0("FC: ", fc, " , FDR: ", fdr))
# print(d)
#Return results
return(assign(paste0("et_results_", expt.nm), et_results, envir = .GlobalEnv))
}
# Testing variables #################
# expt.nm <- "grp_test"
# dge <- gene.dge_in6
# fc <- 0.585
# fdr <- 0.1
# res.path = "../data/processed/"
####################################