-
Notifications
You must be signed in to change notification settings - Fork 2
/
deseq.R
executable file
·80 lines (68 loc) · 2.73 KB
/
deseq.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
#! /usr/bin/env Rscript
###############################################################################
#
# DESCRIPTION
#
# Abstract: This R script takes in gene counts table (output from python script
# format_counts_table.py) and performs differential expression
# analysis using DESeq2. The outputs are a results table with log2
# fold changes, p values and adjusted p values and MA plot as tab-
# separated and PDF file, respectively.
#
# Author: Akshay Paropkari
#
# -----------------------------------------------------------------------------
#
# USAGE and EXAMPLE
#
# Rscript --vanilla raw_gene_counts_file metadata_file results_file output_MA_file
# Rscript --vanilla gene_counts.txt metadata.txt deseq2_lfc.txt MA_plot.pdf
#
#-----------------------------------------------------------------------------
#
# DESeq2: http://bioconductor.org/packages/release/bioc/vignettes/DESeq2/inst/doc/DESeq2.html
#
###############################################################################
library("DESeq2", warn.conflicts = FALSE, quietly = T, verbose = F)
library("readxl", warn.conflicts = FALSE, quietly = T, verbose = F)
library("IHW", warn.conflicts = FALSE, quietly = T, verbose = F)
# Get all arguments
args <- commandArgs(trailingOnly=TRUE)
# test if there is at least one argument: if not, return an error
if (length(args)==0) {
stop("At least one argument must be supplied 'gene_raw_counts.txt'")
}
# Get metadata as input
coldata <- as.data.frame(read_excel(args[2]))
# Read in counts data
raw.counts <- as.matrix(read.delim(file = args[1], row.names = 1))
# Reorder samples (columns) to match samples (rows) in metadata file
raw.counts <- raw.counts[, coldata$Sample_ID]
# MUST BE TRUE
all(coldata$Sample_ID %in% colnames(raw.counts))
all(coldata$Sample_ID == colnames(raw.counts))
# Create a DGEList object
dds <- DESeqDataSetFromMatrix(countData = raw.counts,
colData = coldata,
design = ~ Condition)
dds$Condition <- factor(dds$Condition, levels = c("WT", "Mutant"))
print(resultsNames(dds))
# Prefiltering out genes that don't have count of at least 10
keep <- rowSums(counts(dds)) >= 10
dds <- dds[keep,]
# Run differential expression and get helpful analysis messages
dds <- DESeq(dds)
resIHW <- results(dds, filterFun = ihw, alpha = 0.05)
print(summary(resIHW))
print(sum(resIHW$padj < 0.05, na.rm = TRUE))
print(metadata(resIHW)$ihwResult)
# Obtain MA plot
resLFC <- lfcShrink(dds, coef = resultsNames(dds)[2], type="apeglm")
write.table(resLFC[order(resLFC$padj), ],
file = args[3],
quote = FALSE,
sep = "\t")
pdf(file = args[4])
plotMA(resLFC, ylim = c(-2,2))
abline(h = c(-1, 1), col = "dodgerblue", lwd = 2)
dev.off()