xxxxxxxxxx# Part 2 RNAseq analysis workflow using DESeq2We firts load the libraries, DeSeq 2 and scalesWe firts load the libraries, DeSeq 2 and scales
library("DESeq2")library(scales)options(repr.plot.width=4, repr.plot.height=3)xxxxxxxxxxWe have to make sure we are in the right directoryWe have to make sure we are in the right directory
setwd(path.expand("~/data/work/"))xxxxxxxxxxWe define the file containing the raw counts.We define the file containing the raw counts.
matrixFile = path.expand("~/data/work/all.gene.counts")countData <- as.matrix(read.csv(matrixFile, sep="\t", row.names="Geneid"))head(countData)xxxxxxxxxxWe change the name of the columnsWe change the name of the columns
colnames(countData) <- c("KO1", "KO2", "WT1", "WT2")head(countData)xxxxxxxxxxNow we define the experimental conditions.Now we define the experimental conditions.
condition <- factor(c("KO", "KO", "WT", "WT"))print(condition)xxxxxxxxxxWe prepare a dataframe with the names and conditions.We prepare a dataframe with the names and conditions.
colData <- data.frame(sampleName = colnames(countData), condition = condition)colDataHere we create a DESeq `DESeqDataSet` object for subsequent analysis:Here we create a DESeq DESeqDataSet object for subsequent analysis:
dds <- DESeqDataSetFromMatrix(countData = countData, colData = colData, design = ~ condition)#we need to do this to ensure the column names are correct.colnames(dds) <- colnames(countData)By printing `dds` to screen we can see some informationBy printing dds to screen we can see some information
ddsxxxxxxxxxxBy comparing the `condition` factor in the `dds` object to the column names, we can confirm that the order is still correctBy comparing the condition factor in the dds object to the column names, we can confirm that the order is still correct
dds$conditionxxxxxxxxxx## Differential expression analysis:The dds function within `DESeq2` runs all steps requiredThe dds function within DESeq2 runs all steps required
dds <- DESeq(dds)DESeq2 estimates a factor to correct for the library size:DESeq2 estimates a factor to correct for the library size:
options(repr.plot.width=4, repr.plot.height=3)barplot(sizeFactors(dds))barplot(colSums(countData))xxxxxxxxxxWe create a scatter plot of the WT sampe versus the KO-PAX8.We create a scatter plot of the WT sampe versus the KO-PAX8.
options(repr.plot.width=5, repr.plot.height=5)plot(data.frame(log10(counts(dds))), cex=0.1, col=alpha("black", 0.4))xxxxxxxxxxWe observe that the different sample conditions (WT and KO) are more similar with each other than samples 1 and 2 with each other.We observe that the different sample conditions (WT and KO) are more similar with each other than samples 1 and 2 with each other.
xxxxxxxxxxIn an attempt to minimize the variation for the low expressing genes, we use the rld function.In an attempt to minimize the variation for the low expressing genes, we use the rld function.
rld <- rlogTransformation(dds, blind=TRUE)options(repr.plot.width=5, repr.plot.height=5)plot(data.frame(assay(rld)), cex=0.1, col=alpha("black", 0.4))xxxxxxxxxxThe PCA plot confirms the results obtained in the scatterplot.The PCA plot confirms the results obtained in the scatterplot.
plotPCA(rld, intgroup=c("condition"))xxxxxxxxxx#### MA plotWe created a MA plot to compare the mean expression with the log fold change. This allows us to observe the differentially expressed genes (in red). Those genes are classified as significant since their p-value is lower than 0.05, We created a MA plot to compare the mean expression with the log fold change. This allows us to observe the differentially expressed genes (in red). Those genes are classified as significant since their p-value is lower than 0.05,
DESeq2::plotMA(dds,ylim=c(-3,3))Now we export the results for a further analysis with GSEA. We can observe the top downregulated genes.Now we export the results for a further analysis with GSEA. We can observe the top downregulated genes.
res <- results(dds)res <- res[order(res$padj),]head(res)xxxxxxxxxxThe number of genes differentially expressed is 450.The number of genes differentially expressed is 450.
res.sign <- res[(!is.na(res$padj)) & (res$padj < 0.05),]dim(res.sign)xxxxxxxxxxWe save the file to the disk.We save the file to the disk.
write.table(res, "deseq.results.tsv", sep="\t", col.names=NA, quote=FALSE)We checked manually the results with a barplot. We checked the expression of `PAX8`. As expected, it shows lowers expression in the KO-PAX8 cells. It is alwasy important to manually check a number of differentially expressedgenes.We checked manually the results with a barplot. We checked the expression of PAX8. As expected, it shows lowers expression in the KO-PAX8 cells. It is alwasy important to manually check a number of differentially expressed
genes.
barplot(assay(rld)["PAX8",])