|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
library(edgeR) |
|
|
|
|
|
|
|
|
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 ) |
|
|
|
|
|
|
|
|
counts = round(df[, sample_names]) |
|
|
|
|
|
|
|
|
otherCols = df[!(names(df) %in% sample_names)] |
|
|
|
|
|
|
|
|
group <- colData$condition |
|
|
|
|
|
|
|
|
dge <- DGEList(counts=counts, group=group) |
|
|
|
|
|
|
|
|
dis <- estimateCommonDisp(dge) |
|
|
|
|
|
|
|
|
tag <- estimateTagwiseDisp(dis) |
|
|
|
|
|
|
|
|
etx <- exactTest(tag) |
|
|
|
|
|
|
|
|
etp <- topTags(etx, n=nrow(counts)) |
|
|
|
|
|
|
|
|
scale = dge$samples$lib.size * dge$samples$norm.factors |
|
|
|
|
|
|
|
|
normed = round(t(t(counts)/scale) * mean(scale)) |
|
|
|
|
|
|
|
|
data = merge(otherCols, etp$table, by="row.names") |
|
|
|
|
|
|
|
|
data$baseMean = 1 |
|
|
data$baseMeanA = 1 |
|
|
data$baseMeanB = 1 |
|
|
data$foldChange = 2 ^ data$logFC |
|
|
data$falsePos = 1 |
|
|
|
|
|
|
|
|
names(data)[names(data)=="logFC"] <-"log2FoldChange" |
|
|
|
|
|
|
|
|
data$PAdj = p.adjust(data$PValue, method="hochberg") |
|
|
|
|
|
|
|
|
colnames(data)[1] <- "name" |
|
|
|
|
|
|
|
|
total <- merge(data, normed, by.x='name', by.y="row.names") |
|
|
|
|
|
|
|
|
total = total[with(total, order(PValue, -foldChange)), ] |
|
|
|
|
|
|
|
|
data$falsePos = 1:nrow(data) * data$FDR |
|
|
|
|
|
|
|
|
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$baseMean = total$baseMeanA + total$baseMeanB |
|
|
|
|
|
|
|
|
total$foldChange = round(total$foldChange, 3) |
|
|
total$FDR = round(total$FDR, 4) |
|
|
total$PAdj = round(total$PAdj, 4) |
|
|
total$logCPM = round(total$logCPM, 1) |
|
|
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$falsePos = round(total$falsePos, 0) |
|
|
|
|
|
|
|
|
total$PAdj = formatC(total$PAdj, format = "e", digits = 1) |
|
|
total$PValue = formatC(total$PValue, format = "e", digits = 1) |
|
|
|
|
|
|
|
|
new_cols = c("name", names(otherCols), "baseMean","baseMeanA","baseMeanB","foldChange", |
|
|
"log2FoldChange","logCPM","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: edgeR") |
|
|
print(paste("# Design: ", design_file)) |
|
|
print(paste("# Input: ", counts_file)) |
|
|
print(paste("# Output: ", output_file)) |
|
|
|
|
|
|