1 Kallisto

Kallisto is an “alignment-free” RNA-Seq quantification method that runs very fast with a small memory footprint, so that it can be run on most laptops. It is a command-line program that can be downloaded as binary executables for Linux or Mac, or in source code format. For a first insight into the program, read here and for the published article, see here.

Kallisto is geared towards quantification on the transcript (isoform) level, rather than the gene-level (although the latter can also be done by post-processing Kallisto output.) However, read assignment to transcript isoforms cannot (in general) be done unambiguously, so there is an intrinsic “quantification noise” or variability in this process. Kallisto can thus be run either in a single step (which is very fast) or in “bootstrap” mode (which takes longer, but can be done on several processors in parallel) in order to get uncertainty estimates for the expression levels - a kind of error bars for the quantification process. Running with bootstraps is mandatory if you want to perform differential expression analysis of isoforms with Sleuth (see below).

Kallisto is primarily meant for quantification of an existing set of FASTA sequences, that is, it does not perform transcript assembly and it cannot quantify the expression of novel transcripts that are not in the transcript index that you provide to it. With that said, you can of course use contigs from an assembly that you have produced in some other program in your Kallisto index. It would also be possible to use the software for eg: metagenomics or metatranscriptomics quantification.

1.1 Build index

Download the cDNA reference and create a index file to run on (This has already been done!). But if you want to do it yourself you can uncomment the code and run it yourselves.

sh
#cd ~/RNAseq/
#mkdir rawData/references/ref_kallisto
   
#cd rawData/references/ref_kallisto

#wget ftp://ftp.ensembl.org/pub/release-101/fasta/mus_musculus/cdna/Mus_musculus.GRCm38.cdna.all.fa.gz



#kallisto index \
#  --index kallisto_mm10_transcriptome_index.idx \
#  Mus_musculus.GRCm38.cdna.all.fa.gz


#cd ~/RNAseq/

It takes less than 10 minutes.

1.2 Pseudoalign reads

Now you can pseudoalign all your reads to the Kallisto index using Kallisto quantification

sh

cd ~/RNAseq/

for sample_name in $(ls rawData/fastqFiles/*.clean.fq.gz.1M_reads.fq.gz); do

  file=${sample_name#rawData/fastqFiles/}
  sample=${file%.clean.fq.gz.1M_reads.fq.gz}
   
  echo $sample

  mkdir results/05-kallisto/$sample

  kallisto quant \
    --index rawData/references/ref_kallisto/kallisto_mm10_transcriptome_index.idx \
    --output-dir results/05-kallisto/$sample \
    --threads 3 \
    --single \
    --fragment-length 200 \
    --sd 20 \
    $sample_name

done

2 Quantification

2.1 Get gene quantifications

Since Kallisto uses transcripts estimates and many programs, like DEseq and EdgeR, works better with gene counts there are programs to get gene counts from transcript estimates in our case we will use txImport. TxImport can import estimates from a lot of different sources. You can read more about it in the txImport vignette

For the program to work, it needs a two-column-file that assigns each transcript to a gene. This can found using ensembl database or other public databases. In our case the relationship between the transcripts and gene is found in the sequencenames in the cDNA file that we downloaded. By using awk we can extract the transcript gene dependency and write it to a file.

sh
# Example of a sequence name in file 
#  >ENSMUST00000177564.1 cdna chromosome:GRCm38:14:54122226:54122241:1 gene:ENSMUSG00000096176.1 gene_biotype:TR_D_gene transcript_biotype:TR_D_gene gene_symbol:Trdd2 description:T cell receptor delta diversity 2 [Source:MGI Symbol;Acc:MGI:4439546]


# Extract all transcriptnames (1st) and genenames (4th) from  sequence names and write to a file. If "gzcat" does not work, you can try replacing it by "gunzip -c"  
gzcat rawData/references/ref_kallisto/Mus_musculus.GRCm38.cdna.all.fa.gz| \
grep '>' |
awk '{FS= " "}BEGIN{ print "TXNAME,GENEID"};{print substr($1,2) "," substr($4,6)};' \
>rawData/references/ref_kallisto/tx2gene.mm.GRCm38.cdna.csv 

2.2 TxImport

The next steps are carried out in R so make sure you start R in the conda environment.

R
library(dplyr) # data wrangling
library(ggplot2) # plotting
library(DESeq2) # rna-seq
library(edgeR) # rna-seq
library(tximport) # importing kalisto transcript counts to geneLevels
library(readr) # Fast readr of files.
library(rhdf5) # read/convert kalisto output files.  
If tximport and rhdf5 are not installed you can install them now.
R
#if (!requireNamespace("BiocManager", quietly = TRUE))
#  install.packages("BiocManager")

#BiocManager::install("tximport")
#BiocManager::install("rhdf5")
Load the metadata for the samples.
R
# source download function
source("https://raw.githubusercontent.com/NBISweden/workshop-RNAseq/master/assets/scripts.R")

# download metadata

download_data("data/metadata_raw.csv")
mr <- read.csv("data/metadata_raw.csv",header=TRUE,stringsAsFactors=F,row.names=1)

mr

2.2.1 Convert to gene counts

Read in the Kallisto transcript abundances and convert the TxImport.

TxImport is created to

R
setwd("~/RNAseq")
files <- paste("results/05-kallisto" ,  
               list.files(path = "results/05-kallisto",
                          pattern = "abundance.tsv", 
                          recursive = TRUE),
               sep = "/")
names(files) <- mr$SampleName


tx2gene <- read_csv("rawData/references/ref_kallisto/tx2gene.mm.GRCm38.cdna.csv")


txi.kallisto.tsv <- tximport(files, 
                             type = "kallisto", 
                             tx2gene = tx2gene, 
                             ignoreAfterBar = TRUE)

2.2.2 Convert to DEseq2 object for analysis

R
mr = mr %>% mutate(Day = as.factor(Day))
dds <- DESeqDataSetFromTximport(txi.kallisto.tsv, mr, ~Day)


test = DESeq(dds)

and now you can go on with the differential gene expression in a similar way as the one you did with the feature count table.

2.2.3 Convert to a edgeR object for analysis

R
cts <- txi.kallisto.tsv$counts
normMat <- txi.kallisto.tsv$length
normMat <- normMat/exp(rowMeans(log(normMat)))
library(edgeR)
o <- log(calcNormFactors(cts/normMat)) + log(colSums(cts/normMat))
y <- DGEList(cts)
y$offset <- t(t(log(normMat)) + o)

# y is now ready for estimate dispersion functions see edgeR User's Guide

3 Session info

## R version 4.2.3 (2023-03-15)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 22.04.2 LTS
## 
## Matrix products: default
## BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
## LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] stats4    stats     graphics  grDevices utils     datasets  methods  
## [8] base     
## 
## other attached packages:
##  [1] fgsea_1.24.0                enrichR_3.2                
##  [3] rafalib_1.0.0               biomaRt_2.54.1             
##  [5] pvclust_2.2-0               pheatmap_1.0.12            
##  [7] edgeR_3.40.2                limma_3.54.2               
##  [9] DESeq2_1.38.3               SummarizedExperiment_1.28.0
## [11] Biobase_2.58.0              MatrixGenerics_1.10.0      
## [13] matrixStats_0.63.0          GenomicRanges_1.50.2       
## [15] GenomeInfoDb_1.34.9         IRanges_2.32.0             
## [17] S4Vectors_0.36.2            BiocGenerics_0.44.0        
## [19] ggplot2_3.4.3               formattable_0.2.1          
## [21] kableExtra_1.3.4            dplyr_1.1.3                
## [23] lubridate_1.9.3             yaml_2.3.7                 
## [25] fontawesome_0.5.2           bookdown_0.35              
## [27] knitr_1.44                 
## 
## loaded via a namespace (and not attached):
##  [1] nlme_3.1-162           bitops_1.0-7           bit64_4.0.5           
##  [4] filelock_1.0.2         progress_1.2.2         webshot_0.5.4         
##  [7] RColorBrewer_1.1-3     httr_1.4.5             tools_4.2.3           
## [10] bslib_0.4.2            utf8_1.2.3             R6_2.5.1              
## [13] DBI_1.1.3              mgcv_1.8-42            colorspace_2.1-0      
## [16] withr_2.5.0            prettyunits_1.1.1      tidyselect_1.2.0      
## [19] curl_5.0.0             bit_4.0.5              compiler_4.2.3        
## [22] cli_3.6.1              rvest_1.0.3            xml2_1.3.3            
## [25] DelayedArray_0.24.0    labeling_0.4.2         sass_0.4.5            
## [28] scales_1.2.1           rappdirs_0.3.3         systemfonts_1.0.4     
## [31] stringr_1.5.0          digest_0.6.31          rmarkdown_2.21        
## [34] svglite_2.1.1          XVector_0.38.0         pkgconfig_2.0.3       
## [37] htmltools_0.5.5        WriteXLS_6.4.0         dbplyr_2.3.2          
## [40] fastmap_1.1.1          htmlwidgets_1.6.2      rlang_1.1.0           
## [43] rstudioapi_0.14        RSQLite_2.3.1          farver_2.1.1          
## [46] jquerylib_0.1.4        generics_0.1.3         jsonlite_1.8.4        
## [49] BiocParallel_1.32.6    RCurl_1.98-1.12        magrittr_2.0.3        
## [52] GenomeInfoDbData_1.2.9 Matrix_1.5-3           Rcpp_1.0.10           
## [55] munsell_0.5.0          fansi_1.0.4            lifecycle_1.0.3       
## [58] stringi_1.7.12         zlibbioc_1.44.0        BiocFileCache_2.6.1   
## [61] grid_4.2.3             blob_1.2.4             parallel_4.2.3        
## [64] crayon_1.5.2           lattice_0.20-45        cowplot_1.1.1         
## [67] Biostrings_2.66.0      splines_4.2.3          annotate_1.76.0       
## [70] hms_1.1.3              KEGGREST_1.38.0        locfit_1.5-9.8        
## [73] pillar_1.9.0           rjson_0.2.21           geneplotter_1.76.0    
## [76] codetools_0.2-19       fastmatch_1.1-3        XML_3.99-0.14         
## [79] glue_1.6.2             evaluate_0.20          data.table_1.14.8     
## [82] vctrs_0.6.2            png_0.1-8              gtable_0.3.3          
## [85] purrr_1.0.1            tidyr_1.3.0            cachem_1.0.7          
## [88] xfun_0.40              xtable_1.8-4           viridisLite_0.4.1     
## [91] tibble_3.2.1           AnnotationDbi_1.60.2   memoise_2.0.1         
## [94] timechange_0.2.0