Team Assignment - ExpressionDE Answers
The solutions below are for team A. Other team solutions will be very similar but each for their own unique chromosome dataset.
Estimate expression levels
Use stringtie to estimate gene/transcript abundance levels
cd $RNA_HOME/team_exercise
mkdir expression
stringtie -p 4 -G references/chr11_Homo_sapiens.GRCh38.95.gtf -e -B -o expression/KO_sample1/transcripts.gtf -A expression/KO_sample1/gene_abundances.tsv alignments/SRR10045016.bam
stringtie -p 4 -G references/chr11_Homo_sapiens.GRCh38.95.gtf -e -B -o expression/KO_sample2/transcripts.gtf -A expression/KO_sample2/gene_abundances.tsv alignments/SRR10045017.bam
stringtie -p 4 -G references/chr11_Homo_sapiens.GRCh38.95.gtf -e -B -o expression/KO_sample3/transcripts.gtf -A expression/KO_sample3/gene_abundances.tsv alignments/SRR10045018.bam
stringtie -p 4 -G references/chr11_Homo_sapiens.GRCh38.95.gtf -e -B -o expression/Rescue_sample1/transcripts.gtf -A expression/Rescue_sample1/gene_abundances.tsv alignments/SRR10045019.bam
stringtie -p 4 -G references/chr11_Homo_sapiens.GRCh38.95.gtf -e -B -o expression/Rescue_sample2/transcripts.gtf -A expression/Rescue_sample2/gene_abundances.tsv alignments/SRR10045020.bam
stringtie -p 4 -G references/chr11_Homo_sapiens.GRCh38.95.gtf -e -B -o expression/Rescue_sample3/transcripts.gtf -A expression/Rescue_sample3/gene_abundances.tsv alignments/SRR10045021.bam
Q1. Based on your stringtie results, what are the top 5 genes with highest average expression levels across all knockout samples? What about in your rescue samples? (Hint: You can use R, command-line tools, or download files to your desktop for this analysis)
A1. (click to show/hide answer)
Use the Griffiths’ custom Perl script to collapse all of our just-created Stringtie results into a single matrix of FPKM values:
cd $RNA_HOME/team_exercise/expression
wget https://raw.githubusercontent.com/griffithlab/rnabio.org/master/assets/scripts/stringtie_expression_matrix.pl
chmod +x stringtie_expression_matrix.pl
./stringtie_expression_matrix.pl --expression_metric=TPM --result_dirs='KO_sample1,KO_sample2,KO_sample3,Rescue_sample1,Rescue_sample2,Rescue_sample3' --transcript_matrix_file=transcript_tpm_all_samples.tsv --gene_matrix_file=gene_tpm_all_samples.tsv
./stringtie_expression_matrix.pl --expression_metric=FPKM --result_dirs='KO_sample1,KO_sample2,KO_sample3,Rescue_sample1,Rescue_sample2,Rescue_sample3' --transcript_matrix_file=transcript_fpkm_all_samples.tsv --gene_matrix_file=gene_fpkm_all_samples.tsv
./stringtie_expression_matrix.pl --expression_metric=Coverage --result_dirs='KO_sample1,KO_sample2,KO_sample3,Rescue_sample1,Rescue_sample2,Rescue_sample3' --transcript_matrix_file=transcript_coverage_all_samples.tsv --gene_matrix_file=gene_coverage_all_samples.tsv
column -t transcript_tpm_all_samples.tsv | less -S
column -t gene_tpm_all_samples.tsv | less -S
Now that you have this matrix, you can take the average expression per condition (KO or rescue) for each gene in the matrix. You can then sort the genes from highest to lowest expression and look at the top 5. Here is an awk hairball to do that really efficiently:
awk -F'\t' 'NR>1 {printf "%s\t%.4f\n", $1, ($2+$3+$4)/3}' gene_fpkm_all_samples.tsv | sort -k2,2nr | head -n 5
And here is some R code to do it less efficiently, but in a way that makes a lot more sense:
R # open up R on our terminal
# Read in the table of FPKM values (tab-separated, with a header row)
fpkm = read.table("gene_fpkm_all_samples.tsv", header=TRUE, sep="\t")
# Calculate the mean FPKM for each gene across the KO samples and across the Rescue samples
fpkm$KO_mean = rowMeans(fpkm[, c("KO_sample1", "KO_sample2", "KO_sample3")])
fpkm$Rescue_mean = rowMeans(fpkm[, c("Rescue_sample1", "Rescue_sample2", "Rescue_sample3")])
# Sort the genes from highest to lowest KO mean and look at the top 5
KO_sorted = fpkm[order(fpkm$KO_mean, decreasing=TRUE), ]
head(KO_sorted[, c("Gene_ID", "KO_mean")], 5)
# You can also do the same for the Rescue samples, just for fun
Rescue_sorted = fpkm[order(fpkm$Rescue_mean, decreasing=TRUE), ]
head(Rescue_sorted[, c("Gene_ID", "Rescue_mean")], 5)
No matter what approach you use, the answers below should be correct for each team. Note that these answers were generated by working from the gene-level matrices, not the transcript-level matrices. If you worked from the transcript level, your answers will be slightly different.
Team A (click to show/hide answer)
| KO gene | KO avg FPKM | Rescue gene | Rescue avg FPKM |
|---|---|---|---|
| FTH1 | 71,384.5 | FTH1 | 66,656.9 |
| HSPA8 | 19,218.5 | HSPA8 | 19,360.0 |
| LDHA | 16,415.9 | LDHA | 14,113.3 |
| RPS3 | 12,712.7 | RPS3 | 12,416.2 |
| EEF1G | 10,741.9 | EEF1G | 9,956.6 |
Team B (click to show/hide answer)
| KO gene | KO avg FPKM | Rescue gene | Rescue avg FPKM |
|---|---|---|---|
| GAPDH | 44,118.2 | GAPDH | 42,795.7 |
| KRT8 | 17,796.6 | TUBA1B | 14,704.7 |
| KRT18 | 16,715.1 | RPLP0 | 12,871.9 |
| RPLP0 | 13,796.6 | RPL6 | 12,578.5 |
| TUBA1B | 13,721.6 | KRT8 | 11,843.4 |
Team C (click to show/hide answer)
| KO gene | KO avg FPKM | Rescue gene | Rescue avg FPKM |
|---|---|---|---|
| ACTG1 | 36,987.3 | ACTG1 | 34,251.2 |
| RPL19 | 15,288.8 | RPL19 | 14,887.7 |
| RPL26 | 12,706.6 | RPL26 | 12,943.9 |
| EIF5A | 9,496.4 | EIF4A1 | 10,403.4 |
| EIF4A1 | 9,381.3 | EIF5A | 10,097.4 |
Team D (click to show/hide answer)
| KO gene | KO avg FPKM | Rescue gene | Rescue avg FPKM |
|---|---|---|---|
| RPL13A | 27,221.8 | RPL13A | 25,262.9 |
| FTL | 16,744.7 | FTL | 16,461.4 |
| RPS16 | 15,201.2 | RPS16 | 15,758.3 |
| EEF2 | 14,463.7 | RPS11 | 14,232.4 |
| RPS15 | 13,587.0 | EEF2 | 13,333.2 |
Team E (click to show/hide answer)
| KO gene | KO avg FPKM | Rescue gene | Rescue avg FPKM |
|---|---|---|---|
| EEF1A1 | 47,100.0 | EEF1A1 | 45,882.0 |
| HSP90AB1 | 23,160.3 | HSP90AB1 | 25,849.6 |
| RPS10 | 16,630.3 | RPS10 | 15,921.0 |
| RPS18 | 13,602.4 | RPS12 | 11,579.6 |
| RPS12 | 12,770.2 | RPS18 | 10,705.6 |
Perform differential expression analysis
Use ballgown to identify differentially expressed genes between KO and Rescue samples
cd $RNA_HOME/team_exercise
mkdir de
cd de
First, start an R session:
R
Run the following R commands in your R session.
# load the required libraries
library(ballgown)
library(genefilter)
library(dplyr)
library(devtools)
# Create phenotype data needed for ballgown analysis.
ids=c("KO_sample1","KO_sample2","KO_sample3","Rescue_sample1","Rescue_sample2","Rescue_sample3")
type=c("KO","KO","KO","Rescue","Rescue","Rescue")
results="/home/ubuntu/workspace/rnaseq/team_exercise/expression/"
path=paste(results,ids,sep="")
pheno_data=data.frame(ids,type,path)
pheno_data
# Load ballgown data structure and save it to a variable "bg"
bg = ballgown(samples=as.vector(pheno_data$path), pData=pheno_data)
# Display a description of this object
bg
# Load all attributes including gene name
bg_table = texpr(bg, 'all')
bg_gene_names = unique(bg_table[, 9:10])
bg_transcript_names = unique(bg_table[,c(1,6)])
# Save the ballgown object to a file for later use
save(bg, file='bg.rda')
# Perform differential expression (DE) analysis with no filtering
results_transcripts = stattest(bg, feature="transcript", covariate="type", getFC=TRUE, meas="FPKM")
results_transcripts = merge(results_transcripts, bg_transcript_names, by.x=c("id"), by.y=c("t_id"))
results_genes = stattest(bg, feature="gene", covariate="type", getFC=TRUE, meas="FPKM")
results_genes = merge(results_genes, bg_gene_names, by.x=c("id"), by.y=c("gene_id"))
# Save a tab delimited file for both the transcript and gene results
write.table(results_transcripts, "KO_vs_Rescue_transcript_results.tsv", sep="\t", quote=FALSE, row.names = FALSE)
write.table(results_genes, "KO_vs_Rescue_gene_results.tsv", sep="\t", quote=FALSE, row.names = FALSE)
# Filter low-abundance genes. Here we remove all transcripts with a variance across the samples of less than one
bg_filt = subset (bg,"rowVars(texpr(bg)) > 1", genomesubset=TRUE)
# Load all attributes including gene name
bg_filt_table = texpr(bg_filt , 'all')
bg_filt_gene_names = unique(bg_filt_table[, 9:10])
bg_filt_transcript_names = unique(bg_filt_table[,c(1,6)])
# Perform differential expression (DE) analysis with filtering this time, at both gene and transcript level
results_transcripts = stattest(bg_filt, feature="transcript", covariate="type", getFC=TRUE, meas="FPKM")
results_transcripts = merge(results_transcripts, bg_filt_transcript_names, by.x=c("id"), by.y=c("t_id"))
results_genes = stattest(bg_filt, feature="gene", covariate="type", getFC=TRUE, meas="FPKM")
results_genes = merge(results_genes, bg_filt_gene_names, by.x=c("id"), by.y=c("gene_id"))
# Output the filtered list of genes and transcripts and save to tab delimited files
write.table(results_transcripts, "KO_vs_Rescue_transcript_results_filtered.tsv", sep="\t", quote=FALSE, row.names = FALSE)
write.table(results_genes, "KO_vs_Rescue_gene_results_filtered.tsv", sep="\t", quote=FALSE, row.names = FALSE)
# Identify the significant genes with p-value < 0.05
sig_transcripts = subset(results_transcripts, results_transcripts$pval<0.05)
sig_genes = subset(results_genes, results_genes$pval<0.05)
sig_transcripts_ordered = sig_transcripts[order(sig_transcripts$pval),]
sig_genes_ordered = sig_genes[order(sig_genes$pval),]
# Output the significant gene results to a pair of tab delimited files
write.table(sig_transcripts_ordered, "KO_vs_Rescue_transcript_results_sig.tsv", sep="\t", quote=FALSE, row.names = FALSE)
write.table(sig_genes_ordered, "KO_vs_Rescue_gene_results_sig.tsv", sep="\t", quote=FALSE, row.names = FALSE)
# Exit the R session
quit(save="no")
Q2. How many significant differentially expressed genes do you observe?
A2. (click to show/hide answer)
Now that we’ve saved our sig_genes_ordered R object to a file, we can easily see how many significant genes are in the file by counting the number of lines as follows:
cd $RNA_HOME/team_exercise/de
wc -l KO_vs_Rescue_gene_results_sig.tsv
Note that there is a header line in this file, so your number of significant genes should actually be the number you get from this command, minus one!
Team A: 241
Team B: 348
Team C: 264
Team D: 257
Team E: 262
Q3. By referring back to the supplementary tutorial in the DE Visualization Module, can you construct a volcano plot showcasing the significantly de genes?
A3. (click to show/hide answer)
Make sure we are in the directory with our DE results
cd $RNA_HOME/team_exercise/de
Restart an R session:
R
The following R commands create a volcano plot of the DE results from Ballgown
#Load libraries
library(ggplot2)
library(ballgown)
library(ggrepel)
#Import the ballgown object created during the differential expression analysis
load('bg.rda')
# Load gene names for lookup later in the tutorial
bg_table = texpr(bg, 'all')
bg_gene_names = unique(bg_table[, 9:10])
# Calculate the differential expression results including significance
results_genes = stattest(bg, feature="gene", covariate="type", getFC=TRUE, meas="FPKM")
results_genes = merge(results_genes,bg_gene_names,by.x=c("id"),by.y=c("gene_id"))
# Convert fold change to log2 fold change
## NOTE: THIS IS VERY IMPORTANT! If you miss this step, your volcano plot will look skinny and weird and will be off-center!!
## Always check whether the DE software you are using outputs log fold change or plain fold change!
## (Ballgown outputs plain fold change, so we need to log2-transform it)
results_genes[,"de"] = log2(results_genes[,"fc"])
# Open a PDF file where we will save the volcano plot
pdf(file="KO_vs_rescue_volcano.pdf")
# set default for all genes to "no change"
results_genes$diffexpressed <- "No"
# if log2Foldchange > 0.6 and pvalue < 0.05, set as "Up regulated"
results_genes$diffexpressed[results_genes$de > 0.6 & results_genes$pval < 0.05] <- "Up"
# if log2Foldchange < -0.6 and pvalue < 0.05, set as "Down regulated"
results_genes$diffexpressed[results_genes$de < -0.6 & results_genes$pval < 0.05] <- "Down"
# label the significantly up/downregulated genes by name (only these points are in the labelled layer of the plot)
ggplot(data=results_genes[results_genes$diffexpressed != "No",], aes(x=de, y=-log10(pval), label=gene_name, color = diffexpressed)) +
xlab("log2Foldchange") +
scale_color_manual(name = "Differentially expressed", values=c("blue", "red")) +
geom_point() +
theme_minimal() +
geom_text_repel() +
geom_vline(xintercept=c(-0.6, 0.6), col="red") +
geom_hline(yintercept=-log10(0.05), col="red") +
guides(colour = guide_legend(override.aes = list(size=5))) +
geom_point(data = results_genes[results_genes$diffexpressed == "No",], aes(x=de, y=-log10(pval)), colour = "black")
dev.off()
# Exit the R session
quit(save="no")
Optional: additional DE visualizations (MDS and expression scatter plot) (click to show/hide)
These plots are not required for the questions above, but they are useful for checking sample relationships and for viewing the significant genes in the context of overall expression levels.
Make sure we are in the directory with our DE results
cd $RNA_HOME/team_exercise/de
Start an R session:
R
The following R commands create an MDS plot and a KO vs Rescue expression scatter plot from the Ballgown results
#Load libraries
library(ballgown)
#Import expression and differential expression results from the HISAT2/StringTie/Ballgown pipeline
load('bg.rda')
# View a summary of the ballgown object
bg
# Load gene names for lookup later in the tutorial
bg_table = texpr(bg, 'all')
bg_gene_names = unique(bg_table[, 9:10])
# Pull the gene_expression data frame from the ballgown object
gene_expression = as.data.frame(gexpr(bg))
#Set min value to 1
min_nonzero=1
# Set the columns for finding FPKM and create shorter names for figures
data_columns=c(1:6)
short_names=c("KO1","KO2","KO3","R1","R2","R3")
#Calculate the FPKM sum for all 6 libraries
gene_expression[,"sum"]=apply(gene_expression[,data_columns], 1, sum)
#Identify genes where the sum of FPKM across all samples is above some arbitrary threshold
i = which(gene_expression[,"sum"] > 5)
#Calculate the correlation between all pairs of data
r=cor(gene_expression[i,data_columns], use="pairwise.complete.obs", method="pearson")
#Print out these correlation values
r
# Open a PDF file where we will save some plots.
# We will save all figures and then view the PDF at the end
pdf(file="KO_vs_rescue_figures.pdf")
data_colors=c("tomato1","tomato2","tomato3","royalblue1","royalblue2","royalblue3")
#Plot - Convert correlation to 'distance', and use 'multi-dimensional scaling' to display the relative differences between libraries
#This step calculates 2-dimensional coordinates to plot points for each library
#Libraries with similar expression patterns (highly correlated to each other) should group together
#note that the x and y display limits are calculated from the data (with a small margin) so that no points are clipped
d=1-r
mds=cmdscale(d, k=2, eig=TRUE)
par(mfrow=c(1,1))
plot(mds$points, type="n", xlab="", ylab="", main="MDS distance plot (genes with summed FPKM > 5)", xlim=extendrange(mds$points[,1]), ylim=extendrange(mds$points[,2]))
points(mds$points[,1], mds$points[,2], col="grey", cex=2, pch=16)
text(mds$points[,1], mds$points[,2], short_names, col=data_colors)
# Calculate the differential expression results including significance
results_genes = stattest(bg, feature="gene", covariate="type", getFC=TRUE, meas="FPKM")
results_genes = merge(results_genes,bg_gene_names,by.x=c("id"),by.y=c("gene_id"))
# Plot - Display the average expression values from KO and Rescue conditions and mark those that are significantly differentially expressed
sig=which(results_genes$pval<0.05)
results_genes[,"de"] = log2(results_genes[,"fc"])
gene_expression[,"KO"]=apply(gene_expression[,c(1:3)], 1, mean)
gene_expression[,"Rescue"]=apply(gene_expression[,c(4:6)], 1, mean)
x=log2(gene_expression[,"KO"]+min_nonzero)
y=log2(gene_expression[,"Rescue"]+min_nonzero)
plot(x=x, y=y, pch=16, cex=0.25, xlab="KO FPKM (log2)", ylab="Rescue FPKM (log2)", main="Rescue vs KO FPKMs")
abline(a=0, b=1)
xsig=x[sig]
ysig=y[sig]
points(x=xsig, y=ysig, col="magenta", pch=16, cex=0.5)
legend("topleft", "Significant", col="magenta", pch=16)
#Get the gene symbols for the top N (according to corrected p-value) and display them on the plot
#Note: order(...) returns positions within the 'sig' subset, so map them back to row indices of results_genes via sig[...]
topn = sig[order(results_genes[sig,"qval"])[1:min(25, length(sig))]]
text(x[topn], y[topn], results_genes[topn,"gene_name"], col="black", cex=0.75, srt=45)
dev.off()
# Exit the R session
quit(save="no")