# RNAseq analysis workflow using DESeq2:# Get the DEseq2 and scales packages out of the library (needs to be in R): library("DESeq2")library(scales)options(repr.plot.width=4, repr.plot.height=3)# Ensure we are in the correct directory: # setwd(): returns an absolute filepath representing the current working directory of the R process # path.expand(): expand a path name, for example by replacing a leading tilde by the user's home directory. setwd(path.expand("~/data/RNAseqgroepswerk//"))# Now we define the the file we created, containing the raw gene counts. matrixFile = path.expand("~/data/RNAseqgroepswerk//all.geneCML.counts2.tsv")# Put the data into a matrix: # as.matrix(): attempts to turn its argument into a matrix # read.csv(): Reads a file in table format and creates a data frame from it, with cases corresponidng to lines # and variables to fields in the file. # sep= : the field separator character. Values on each line of the file are separated by this character (\t) # row.names= : a vector of row names. This can be a vector giving the actual row names, or a single number # giving the comun of the table which contains the row names, or character string giving the name of the # table column containing the row names.CMLcountData2 <- as.matrix(read.csv(matrixFile, sep="\t", row.names="Geneid"))head(CMLcountData2)# The column names don't need the .bam suffix, so we'll make it a bit more appealing: # c(): is a generic function which combines its arguments, all arguments are coerced to a common type which is the # type of the returned value, and all attributes except names are removed. # colnames(): Take the names of the columns of the matrix (countData) and on this we will use the c() function.colnames(CMLcountData2) <- c("CML15NT", "CML15T","CML17NT", "CML17T")head(CMLcountData2)# Now we'll define the experimental conditions of the samples (treated (T)) or not treated (NT)): # factor(c) : The function 'factor' is used to encode a vector as a factor.CMLcondition2 <- factor(c("NT", "T", "NT", "T"))print(CMLcondition2)# The order of the levels is important. Because T is first we will be comparing: T/NT. # So positive log fold change values are up in the non-treated samples.?data.frame#Now we prepare a dataframe which has the names and conditions. This will be fed to DESeq2. (!order has to be the same!)CMLcolData2 <- data.frame(sampleName = colnames(CMLcountData2), condition = CMLcondition2)CMLcolData2# Here we create a DESeq DESeqDataSet object for subsequent analysis: # DESeqDataSetFromMatrix: The DESeqDataSet is a subclass of SummarizedExperiment, used to store the input values, # intermediate calculations and results of an analysis of differential expression.CMLdds2 <- DESeqDataSetFromMatrix(countData = CMLcountData2, colData = CMLcolData2, design = ~ condition)#we need to do this to ensure the column names are correct.colnames(CMLdds2) <- colnames(CMLcountData2)CMLdds2# By comparing the condition factor in the dds object to the column names, we can confirm that the order is still correctCMLdds2$condition# Run the differential expression:# one function within `DESeq2` runs all steps required: # DESeq : Differential expression analysis based on negative binomial distribution: This function performs a default # analysis through the steps: 1) estimation of size factors 2) estimation of dispersion 3) negative ninomial GLM # fitting and Wald statistics.CMLdds2 <- DESeq(CMLdds2)# Quality Control:# DESeq2 estimates a factor to correct for the library size:options(repr.plot.width=9, repr.plot.height=5)barplot(sizeFactors(CMLdds2))# Gene expression upregulation found in samples from non-treated CML15 were different from those found in CML17.barplot(colSums(CMLcountData2))options(repr.plot.width=7, repr.plot.height=7)plot(data.frame(log10(counts(CMLdds2))), cex=0.1, col=alpha("black", 0.4))rld2 <- rlogTransformation(CMLdds2, blind=TRUE)options(repr.plot.width=7, repr.plot.height=7)plot(data.frame(assay(rld2)), cex=0.1, col=alpha("black", 0.4))plotPCA(rld2, intgroup=c("condition"))options(repr.plot.width=5, repr.plot.height=4)plotMA(CMLdds2,ylim=c(-3,3))CMLres2 <- results(CMLdds2)CMLres2 <- CMLres2[order(CMLres2$padj),]head(CMLres2)# $ means 'give me the column' CMLres2.sign <- CMLres2[(!is.na(CMLres2$padj)) & (CMLres2$padj < 0.05),]dim(CMLres2.sign)write.table(CMLres2, "deseq2.CMLresults.tsv", sep="\t", col.names=NA, quote=FALSE)options(repr.plot.width=9, repr.plot.height=5)barplot(assay(rld2)["OLFM4",])# GPR110 is strongly upregulated# log scale!# if the difference between S1 and S2 is large you better look at another gene, because you cannot be sure which one is right./# normally NS1 and NS2 should be the same and S1 and S2 as well. options(repr.plot.width=9, repr.plot.height=5)barplot(assay(rld2)["SMAD7",])options(repr.plot.width=9, repr.plot.height=5)barplot(assay(rld2)["ALOX15B",])# Upregulated genes mentioned in the article (OLFM4, ALOX15B and SMAD7) were also upregulated in our differential# gene expression analysis.