Skip to content

Taxonomic Classification

Reconstruct rRNA using PhyloFlash-EMIRGE

Another method that we can use to obtain taxonomic composition of our metagenomic libraries is to extract/reconstruct the 16S and 18S ribosomal RNA gene. However, modern assemblers often struggle to produce contiguous 16S rRNA genes due to (1) taxa harbouring multiple copies of the gene and (2) repeat regions that short reads cannot span (see here for details). As such, software such as EMIRGE (Miller et al., 2011) and PhyloFlash (Gruber-Vodicka et al., 2020) were developed to reconstruct 16S and 18S rRNA genes. These methods leverage the expansive catalogue of 16S rRNA genes available in databases such as SILVA in order to subset reads and then reconstruct the full-length gene.

For this workshop, we will use PhyloFlash to obtain sequences and abundances of 16S rRNA sequences. The reason being that PhyloFlash can also run EMIRGE as part of its routine, compare those reconstructions with sequences generated by PhyloFlash's map-assemble, and produce a set of 16S rRNA gene sequences as well as estimate relative abundances via coverage estimation.

-lib

A quirk of PhyloFlash is that all outputs will be placed in the current running directory. The only punctuation allowed is "-" and "_". Make sure to name your "LIB" sensibly so you can track which library you're working with.

Create script named phyloflash.sl

nano phyloflash.sl

slurm script

#!/bin/bash -e
#SBATCH --account       nesi02659
#SBATCH --job-name      phyloflash
#SBATCH --partition     milan
#SBATCH --time          15:00
#SBATCH --mem           16G
#SBATCH --cpus-per-task 12
#SBATCH --error         %x.%j.err
#SBATCH --output        %x.%j.out

module purge
module load Miniconda3/23.10.0-1
module load USEARCH/11.0.667-i86linux32

# Navigate to working directory
cd /nesi/nobackup/nesi02659/MGSS_U/<YOUR FOLDER>/2.assembly_evaluation/

# Set conda variables
source $(conda info --base)/etc/profile.d/conda.sh
export PYTHONNOUSERSITE=1

# Activate environment
export CONDA_ENVS_PATH=/nesi/project/nesi02659/MGSS_2024/resources/tools
source activate phyloflash

# Run PhyloFlash per sample pair
for r1 in ../2.assembly/sample*_R1.fastq.gz; do
    phyloFlash.pl -lib $(basename $r1 _R1.fastq.gz) \
                  -read1 ${r1} -read2 ${r1/R1/R2} \
                  -everything \
                  -CPUs $SLURM_CPUS_PER_TASK
done

phyloFlash_compare.pl \
    --zip $(ls -1 sample*.phyloFlash.tar.gz | paste -sd ",") \
    --task heatmap,barplot,matrix,ntu_table \
    --out allsamples_compare

conda deactivate

A successful run of PhyloFlash (lines 24-29) would have generated the following per-sample outputs:

  • tar.gz archive containing relevant data files
  • An .html page summary
  • A timestamped .log detailing what was done

The reconstructed 16S rRNA sequences are stored in the per-sample archive. We will extract them to explore the data files inside.

Terminal

for pf in *phyloFlash.tar.gz; do
    outdir=$(basename ${pf} .tar.gz)
    mkdir -p ${outdir}
    tar -xvzf ${pf} -C ${outdir}
done

The reconstructed sequences based on the phyloFlash and EMIRGE workflows are stored in sample?.phyloFlash/sample?.all.final.fasta.

In lines 32-34 of our slurm script, we also ran a cross-sample comparison script. This generated 4 files:

  • allsamples_compare.barplot.pdf A relative abundance barplot of identified taxa
  • allsamples_compare.heatmap.pdf A heatmap of identified taxa
  • allsamples_compare.matrix.tsv A dissimilarity matrix (abundance-weighted, UniFrac-like)
  • allsamples_compare.ntu_table.tsv (long/tidy-format table of relative abundance per sample per taxa)

The allsamples_compare.ntu_table.tsv is especially useful if you intend to perform downstream diversity analyses in R.

Sequence taxonomic classification using Kraken2

Most, if not all, of the time, we never know the taxonomic composition of our metagenomic reads a priori. For some environments, we can make good guesses (e.g., Prochlorococcus in marine samples, members of the Actinobacteriota in soil samples, various Bacteroidota in the gut microbiome, etc.). Here, we can use Kraken2, a k-mer based taxonomic classifier to help us interrogate the taxonomic composition of our samples. This is helpful if there are targets we might be looking for (e.g. working hypotheses, well characterised microbiome). Furthermore, we can estimate the abundance of the classified taxa using Bracken.

Create script named kraken2_bracken.sl

nano kraken2_bracken.sl

slurm script

#!/bin/bash -e
#SBATCH --account       nesi02659
#SBATCH --job-name      kraken2_bracken
#SBATCH --partition     milan
#SBATCH --time          30:00
#SBATCH --mem           80G
#SBATCH --cpus-per-task 20
#SBATCH --error         %x.%j.err
#SBATCH --output        %x.%j.out

# Load modules
module purge
module load \
    Kraken2/2.1.3-GCC-11.3.0 \
    Bracken/2.7-GCC-11.3.0

# Navigate to working directory
cd /nesi/nobackup/nesi02659/MGSS_U/<YOUR FOLDER>/2.assembly_evaluation/

# Point to database
K2DB=/nesi/project/nesi02659/MGSS_2024/resources/databases/k2_standard_20240605

# Create output directory
mkdir -p read_classification/

# Run Kraken2 and Bracken
for r1 in ../2.assembly/sample?_R1.fastq.gz; do
  # Output basename
  outbase=read_classification/$(basename ${r1} _R1.fastq.gz)
  # Taxonomic classification of reads
  kraken2 --paired --threads $SLURM_CPUS_PER_TASK \
          --classified-out ${outbase}#.k2_classified.fq \
          --unclassified-out ${outbase}#.k2_unclassified.fq \
          --report ${outbase}.k2_report.txt \
          --output ${outbase}.k2_out \
          --db ${K2DB} \
          ${r1} ${r1/R1/R2}
  # Estimate taxa abundance
  bracken -d ${K2DB} -i ${outbase}.k2_report.txt -r 100 \
          -o ${outbase}.bracken
done

The main output is quite dense and verbose with columns indicated here. The report is more human-readable, with nicely spaced columns that indicate:

  1. Percentage reads mapped to taxon
  2. Number of reads mapped to taxon
  3. Number of reads directly assigned to taxon
  4. Rank of taxon
  5. NCBI taxonomy ID
  6. Scientific name of taxon

Remember to produce the --report as Bracken bases its estimation on the report

The code tells Kraken2 that the inputs ${r1} and ${r1/R1/R2} are paired-end reads. The # after ${base} will be replaced with the read orientation (i.e., forward _1 or reverse _2).

The Bracken outputs provides us with adjusted number (columns added_reads and new_est_reads) and fraction of reads that were assigned to each species identified by Kraken2 for downstream analyses.

Database construction for Kraken2 and Bracken

For this workshop (and most applications), the standard or other extensive pre-built Kraken2 databases are sufficient. However, if you require specific reference sequences to be present, the database building process (outlined here) can be quite time and resource intensive.

The Kraken2 database also comes with a few pre-built Bracken databases (filenames look like this: database100mers.kmer_distrib). Note that the "100mers" doesn't refer to k-mers (oligonucleotide frequency), but it is the length of the read the database was built for. In this workshop, we've used the 100bp database to estimate abundances for 126bp reads. According to the developers, this is acceptable. As long as the read length of the built database is \(\le\) than the length of reads to be classified, Bracken will provide good enough estimates.

If you want to build your own Bracken database based on your library minimum read lengths, you can follow the process here. Take note that you will require the initial sequence and taxonomy files used for building the Kraken2 database (not part of the files in the pre-built databases) and these (especially for NCBI nt and bacterial databses) will take a long time to download and lots of memory to build.