xxxxxxxxxx
# Part 2 RNAseq analysis workflow using DESeq2
​
We firts load the libraries, DeSeq 2 and scales

Part 2 RNAseq analysis workflow using DESeq2

We firts load the libraries, DeSeq 2 and scales

In [76]:
library("DESeq2")
library(scales)
options(repr.plot.width=4, repr.plot.height=3)
xxxxxxxxxx
We have to make sure we are in the right directory

We have to make sure we are in the right directory

In [77]:
setwd(path.expand("~/data/work/"))
xxxxxxxxxx
 
We define the file containing the raw counts.

We define the file containing the raw counts.

In [114]:
matrixFile = path.expand("~/data/work/all.gene.counts")
In [115]:
countData <- as.matrix(read.csv(matrixFile, sep="\t", row.names="Geneid"))
head(countData)
KO.bamKO2.bamWT.bamWT2.bam
MIR1302-110000
FAM138A0000
OR4F50000
RP11-34P13.72412
RP11-34P13.80000
AL627309.10010
xxxxxxxxxx
We change the name of the columns

We change the name of the columns

In [116]:
colnames(countData) <- c("KO1", "KO2", "WT1", "WT2")
head(countData)
KO1KO2WT1WT2
MIR1302-110000
FAM138A0000
OR4F50000
RP11-34P13.72412
RP11-34P13.80000
AL627309.10010
xxxxxxxxxx
Now we define the experimental conditions.

Now we define the experimental conditions.

In [117]:
condition <- factor(c("KO", "KO", "WT", "WT"))
print(condition)
[1] KO KO WT WT
Levels: KO WT
xxxxxxxxxx
 
We prepare a dataframe with the names and conditions.

We prepare a dataframe with the names and conditions.

In [118]:
colData <- data.frame(sampleName = colnames(countData),
                      condition = condition)
colData
sampleNamecondition
KO1KO
KO2KO
WT1WT
WT2WT
 
Here we create a DESeq `DESeqDataSet` object for subsequent analysis:

Here we create a DESeq DESeqDataSet object for subsequent analysis:

In [119]:
 
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 information

By printing dds to screen we can see some information

In [120]:
 
dds
class: DESeqDataSet 
dim: 41863 4 
exptData(0):
assays(1): counts
rownames(41863): MIR1302-11 FAM138A ... AC007965.1 RNU6-1314P
rowRanges metadata column names(0):
colnames(4): KO1 KO2 WT1 WT2
colData names(2): sampleName condition
xxxxxxxxxx
By comparing the `condition` factor in the `dds` object to the column names, we can confirm that the order is still correct

By comparing the condition factor in the dds object to the column names, we can confirm that the order is still correct

In [121]:
 
dds$condition
  1. KO
  2. KO
  3. WT
  4. WT
xxxxxxxxxx
## Differential expression analysis:
​
The dds function within `DESeq2` runs all steps required

Differential expression analysis:

The dds function within DESeq2 runs all steps required

In [122]:
 
dds <- DESeq(dds)
estimating size factors
estimating dispersions
gene-wise dispersion estimates
mean-dispersion relationship
final dispersion estimates
fitting model and testing
xxxxxxxxxx
## Quality control

Quality control

 
DESeq2 estimates a factor to correct for the library size:

DESeq2 estimates a factor to correct for the library size:

In [123]:
 
options(repr.plot.width=4, repr.plot.height=3)
barplot(sizeFactors(dds))
In [124]:
 
barplot(colSums(countData))
xxxxxxxxxx
We 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.

In [125]:
 
options(repr.plot.width=5, repr.plot.height=5)
plot(data.frame(log10(counts(dds))), cex=0.1, col=alpha("black", 0.4))
xxxxxxxxxx
We 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.

xxxxxxxxxx
In 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.

In [126]:
 
rld <- rlogTransformation(dds, blind=TRUE)
In [127]:
 
options(repr.plot.width=5, repr.plot.height=5)
plot(data.frame(assay(rld)), cex=0.1, col=alpha("black", 0.4))
xxxxxxxxxx
The PCA plot confirms the results obtained in the scatterplot.

The PCA plot confirms the results obtained in the scatterplot.

In [128]:
 
plotPCA(rld, intgroup=c("condition"))
xxxxxxxxxx
#### MA plot
​
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, 

MA plot

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,

In [136]:
 
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.

In [130]:
res <- results(dds)
res <- res[order(res$padj),]
head(res)
log2 fold change (MAP): condition WT vs KO 
Wald test p-value: condition WT vs KO 
DataFrame with 6 rows and 6 columns
         baseMean log2FoldChange     lfcSE      stat       pvalue         padj
        <numeric>      <numeric> <numeric> <numeric>    <numeric>    <numeric>
PAX8    2444.2788      1.3969672 0.1281551 10.900597 1.145029e-27 1.274189e-23
PSAP   26784.4300      1.0274915 0.1213602  8.466463 2.529579e-17 9.383053e-14
PPME1   1759.8764      1.0872897 0.1282896  8.475278 2.345198e-17 9.383053e-14
EPRS    5972.9283      1.0914711 0.1312396  8.316628 9.050160e-17 2.517755e-13
H2AFY2   761.1696      1.1657607 0.1420510  8.206634 2.274762e-16 5.062710e-13
ANXA2  45817.0779      0.9720065 0.1202290  8.084629 6.235393e-16 1.156458e-12
xxxxxxxxxx
 
The number of genes differentially expressed is 450.

The number of genes differentially expressed is 450.

In [131]:
res.sign <- res[(!is.na(res$padj)) & (res$padj < 0.05),]
dim(res.sign)
  1. 137
  2. 6
xxxxxxxxxx
 
We save the file to the disk.

We save the file to the disk.

In [132]:
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 expressed
genes.

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.

In [133]:
barplot(assay(rld)["PAX8",])