|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
suppressPackageStartupMessages(library(DESeq2)) |
|
|
|
|
|
|
|
|
counts_file = "counts.csv" |
|
|
|
|
|
|
|
|
design_file = "design.csv" |
|
|
|
|
|
|
|
|
output_file = "results.csv" |
|
|
|
|
|
|
|
|
colData <- read.csv(design_file, stringsAsFactors=F) |
|
|
|
|
|
|
|
|
colData$condition = factor(colData$condition) |
|
|
|
|
|
|
|
|
|
|
|
colData$condition = relevel(colData$condition, toString(colData$condition[1])) |
|
|
|
|
|
|
|
|
sample_names <- colData$sample |
|
|
|
|
|
|
|
|
df = read.csv(counts_file, header=TRUE, row.names=1 ) |
|
|
|
|
|
|
|
|
countData = round(df[, sample_names]) |
|
|
|
|
|
|
|
|
otherCols = df[!(names(df) %in% sample_names)] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dds = DESeqDataSetFromMatrix(countData=countData, colData=colData, design = ~condition) |
|
|
|
|
|
|
|
|
dse = DESeq(dds) |
|
|
|
|
|
|
|
|
res = results(dse) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data = cbind(otherCols, data.frame(res)) |
|
|
|
|
|
|
|
|
data$foldChange = 2 ^ data$log2FoldChange |
|
|
|
|
|
|
|
|
names(data)[names(data)=="pvalue"] <-"PValue" |
|
|
names(data)[names(data)=="padj"] <- "FDR" |
|
|
|
|
|
|
|
|
data$PAdj = p.adjust(data$PValue, method="hochberg") |
|
|
|
|
|
|
|
|
data = data[with(data, order(PValue, -foldChange)), ] |
|
|
|
|
|
|
|
|
data$falsePos = 1:nrow(data) * data$FDR |
|
|
|
|
|
|
|
|
data$baseMeanA = 1 |
|
|
data$baseMeanB = 1 |
|
|
|
|
|
|
|
|
normed = counts(dse, normalized=TRUE) |
|
|
|
|
|
|
|
|
normed = round(normed, 1) |
|
|
|
|
|
|
|
|
total <- merge(data, normed, by=0) |
|
|
|
|
|
|
|
|
total = total[with(total, order(PValue, -foldChange)), ] |
|
|
|
|
|
|
|
|
col_names_A = data.frame(split(colData, colData$condition)[1])[,1] |
|
|
|
|
|
|
|
|
col_names_B = data.frame(split(colData, colData$condition)[2])[,1] |
|
|
|
|
|
|
|
|
total$baseMeanA = rowMeans(total[, col_names_A]) |
|
|
total$baseMeanB = rowMeans(total[, col_names_B]) |
|
|
|
|
|
|
|
|
total$foldChange = round(total$foldChange, 3) |
|
|
total$log2FoldChange = round(total$log2FoldChange, 1) |
|
|
total$baseMean = round(total$baseMean, 1) |
|
|
total$baseMeanA = round(total$baseMeanA, 1) |
|
|
total$baseMeanB = round(total$baseMeanB, 1) |
|
|
total$lfcSE = round(total$lfcSE, 2) |
|
|
total$stat = round(total$stat, 2) |
|
|
total$FDR = round(total$FDR, 4) |
|
|
total$falsePos = round(total$falsePos, 0) |
|
|
|
|
|
|
|
|
total$PAdj = formatC(total$PAdj, format = "e", digits = 1) |
|
|
total$PValue = formatC(total$PValue, format = "e", digits = 1) |
|
|
|
|
|
|
|
|
colnames(total)[1] <- "name" |
|
|
|
|
|
|
|
|
new_cols = c("name", names(otherCols), "baseMean","baseMeanA","baseMeanB","foldChange", |
|
|
"log2FoldChange","lfcSE","stat","PValue","PAdj", "FDR","falsePos",col_names_A, col_names_B) |
|
|
|
|
|
|
|
|
total = total[, new_cols] |
|
|
|
|
|
|
|
|
write.csv(total, file=output_file, row.names=FALSE, quote=FALSE) |
|
|
|
|
|
|
|
|
print("# Tool: DESeq2") |
|
|
print(paste("# Design: ", design_file)) |
|
|
print(paste("# Input: ", counts_file)) |
|
|
print(paste("# Output: ", output_file)) |
|
|
|
|
|
|
|
|
|