File size: 3,999 Bytes
90f526e |
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 |
#
# Differential expression analysis with the DESeq2 package.
#
# https://bioconductor.org/packages/release/bioc/html/DESeq2.html
#
# Load the library.
suppressPackageStartupMessages(library(DESeq2))
# The name of the file that contains the counts.
counts_file = "counts.csv"
# The sample file is in CSV format and must have the headers "sample" and "condition".
design_file = "design.csv"
# The final result file.
output_file = "results.csv"
# Read the sample file.
colData <- read.csv(design_file, stringsAsFactors=F)
# Turn conditions into factors.
colData$condition = factor(colData$condition)
# The first level should correspond to the first entry in the file!
# Required later when building a model.
colData$condition = relevel(colData$condition, toString(colData$condition[1]))
# Isolate the sample names.
sample_names <- colData$sample
# Read the data from the standard input.
df = read.csv(counts_file, header=TRUE, row.names=1 )
# Created rounded integers for the count data
countData = round(df[, sample_names])
# Other columns in the dataframe that are not sample information.
otherCols = df[!(names(df) %in% sample_names)]
#
# Running DESeq2
#
# Create DESEq2 dataset.
dds = DESeqDataSetFromMatrix(countData=countData, colData=colData, design = ~condition)
# Run deseq
dse = DESeq(dds)
# Format the results.
res = results(dse)
#
# The rest of the code is about formatting the output dataframe.
#
# Turn the DESeq2 results into a data frame.
data = cbind(otherCols, data.frame(res))
# Create the foldChange column.
data$foldChange = 2 ^ data$log2FoldChange
# Rename columns to better reflect reality.
names(data)[names(data)=="pvalue"] <-"PValue"
names(data)[names(data)=="padj"] <- "FDR"
# Create a real adjusted pvalue
data$PAdj = p.adjust(data$PValue, method="hochberg")
# Sort the data by PValue to compute false discovery counts.
data = data[with(data, order(PValue, -foldChange)), ]
# Compute the false discovery counts on the sorted table.
data$falsePos = 1:nrow(data) * data$FDR
# Create the additional columns that we wish to present.
data$baseMeanA = 1
data$baseMeanB = 1
# Get the normalized counts.
normed = counts(dse, normalized=TRUE)
# Round normalized counts to a single digit.
normed = round(normed, 1)
# Merge the two datasets by row names.
total <- merge(data, normed, by=0)
# Sort again for output.
total = total[with(total, order(PValue, -foldChange)), ]
# Sample names for condition A
col_names_A = data.frame(split(colData, colData$condition)[1])[,1]
# Sample names for condition B
col_names_B = data.frame(split(colData, colData$condition)[2])[,1]
# Create the individual baseMean columns.
total$baseMeanA = rowMeans(total[, col_names_A])
total$baseMeanB = rowMeans(total[, col_names_B])
# Bringing some sanity to numbers. Round columns to fewer digits.
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)
# Reformat these columns as string.
total$PAdj = formatC(total$PAdj, format = "e", digits = 1)
total$PValue = formatC(total$PValue, format = "e", digits = 1)
# Rename the first column.
colnames(total)[1] <- "name"
# Reorganize columns names to make more sense.
new_cols = c("name", names(otherCols), "baseMean","baseMeanA","baseMeanB","foldChange",
"log2FoldChange","lfcSE","stat","PValue","PAdj", "FDR","falsePos",col_names_A, col_names_B)
# Slice the dataframe with new columns.
total = total[, new_cols]
# Write the results to the standard output.
write.csv(total, file=output_file, row.names=FALSE, quote=FALSE)
# Inform the user.
print("# Tool: DESeq2")
print(paste("# Design: ", design_file))
print(paste("# Input: ", counts_file))
print(paste("# Output: ", output_file))
|