The easiest way to define presence/absence of vOTUs within a sample and then estimate their abundance, is by mapping the reads back to the assembly.

:pencil2: How can we use the BAM files? We can use automatic tools (like MetaPop) or … create an Anvi’o database!

There are loads of mappers/aligners available (bowtie2, minimap2, and bbmap to name a few), but we’re going to use BWA-MEM (the bwa mem command).

Whatever mapper you use, it’s very likely that you’ll use samtools to process the output.

Installation

Here you can create an environment with both bwa and samtools installed:

:bulb: When mapping against a subset of contigs, you might want to increase the stringency of the mapping. We won’t change the defaults in this tutorial.

mamba create -n mapping \
  -c conda-forge -c bioconda\
  bwa samtools coverm

Remember to activate the environment and invoke the help menus as we have done previously. This is a great way to test that you’re in the correct environment and your programmes have installed successfully.

Mapping

Before mapping, BWA needs to build an index of the reference. This only has to be done once per reference:

# DO NOT COPY THIS COMMAND, adapt it before
bwa index <MY_VOTUS.fa>

This creates a few extra files next to your fasta file (with extensions like .amb, .ann, .bwt, .pac, .sa).

A typical bwa mem command looks like this:

# DO NOT COPY THIS COMMAND, adapt it before
bwa mem -t 8 <MY_VOTUS.fa> <reads-R1.fq.gz> <reads-R2.fq.gz>  > output.sam 

Where:

  • mem is the BWA algorithm suited to Illumina reads (70bp and longer)
  • -t 8 means using 8 cores
  • the positional parameters are the reference (vOTUs), R1 and R2 reads
  • > redirects the output to a text file (SAM format)

To produce the sorted bam file, we can pipe the output directly to samtools:

# DO NOT COPY THIS COMMAND, adapt it before
bwa mem -t 8 <VOTUS> <R1.fq.gz> <R2.fq.gz> | samtools view -bS -F4 - | samtools sort --write-index -@ 8 -o "X.bam" -

There’s a lot going on in this command (because it’s actually three commands separated by pipes (|) :open_mouth:), be sure to look up what pipes are, and what the bwa mem parameters might be doing.

The alignments will be saved in BAM files. You should look up what BAM files are and what the difference is between BAM and SAM…

For the bam file to be usable in Anvi’o, you will need to index your bam file. You can use Samtools to do this:

# DO NOT COPY THIS COMMAND, adapt it before
samtools index -b <sample_X.bam> 

:pencil2: Do you want to try yourself?

  • Create a directory where to store the alignments (eg. ~/bams/)
  • Use the structure of the above command to map the reads from sample_01 to the vOTUs you created earlier.
  • Optionally, map all the other samples

EBAME commands

You can process the files manually:


mkdir -p ~/bams/
VOTUS=genomad-out/derep_votus.fasta

# Index the reference (only once)
bwa index $VOTUS

bwa mem -t 6 $VOTUS $VIROME/ERR6797443_{1,2}.fastq.gz | \
  samtools view -bS -F4 - | \
  samtools sort -@ 4 -o ~/bams/sample_1.bam -
samtools index ~/bams/sample_1.bam

bwa mem -t 6 $VOTUS $VIROME/ERR6797444_{1,2}.fastq.gz | \
  samtools view -bS -F4 - | \
  samtools sort -@ 4 -o ~/bams/sample_2.bam -
samtools index ~/bams/sample_2.bam

bwa mem -t 6 $VOTUS $VIROME/ERR6797445_{1,2}.fastq.gz | \
  samtools view -bS -F4 - | \
  samtools sort -@ 4 -o ~/bams/sample_3.bam -
samtools index ~/bams/sample_3.bam

Making a loop

See bash loops tutorial

As you see from many Anvi’o tutorials, when you repeat the same command over multiple files you can make a loop. Here we will make a loop using multiple temporary variables as we have paired end reads to deal with.

It is a good idea to draft the loop initially to only print the variables to see if everything is right:

for R1 in $VIROME/*_1.fastq.gz;
do
   R2=${R1/_1/_2}
   BASE=$(basename $R1 | cut -f 1 -d _)

   echo "Mapping sample: $BASE"
   #... complete this
done

Explanation:

  • for R1 in $VIROME/*_1.fastq.gz;: This line starts the loop, iterating over all files in the $VIROME directory that end with _1.fastq.gz (typically forward reads in paired-end sequencing).
  • R2=${R1/_1/_2}: This creates the filename for the reverse read by replacing _1 with _2 in the R1 filename.
  • BASE=$(basename $R1 | cut -f 1 -d _): This extracts the sample name from the filename.
    • basename $R1: Gets the filename without the path.
    • cut -f 1 -d _: Cuts the string at the first underscore, keeping only the part before it.
  • echo $BASE: Prints the sample name, useful for debugging.

we now have the paired files in $R1 and $R2 respectively, and the sample name extracted from the read file in $BASE.

OUTDIR=bams
# Put your fasta VOTU in REF!
REF=...
mkdir -p "$OUTDIR"
# Index the reference (only once)
bwa index "$REF"
for R1 in "$VIROME"/*_1.fastq.gz;
do
   R2=${R1/_1/_2}
   BASE=$(basename $R1 | cut -f 1 -d _)
   # Let's also check if R2 exists:
   if [[ ! -e "$R2" ]]; then
      echo "[$BASE] Processing $R1 (R1), R2 was not found: $R2"
      exit 1;
   fi
   bwa mem -t 6 "$REF" "$R1" "$R2" | samtools view -bS -F4 - | samtools sort -@ 4 -o "$OUTDIR"/"$BASE".bam -
   samtools index "$OUTDIR"/"$BASE".bam 
done

:floppy_disk: Full code for this mapping script is available here

curl -L "https://gist.githubusercontent.com/telatin/fcbd5008e860906fc19147833ef12a70/raw/dd3a1f4272e25eaa6ed3cd0572e3fce1315531f6/map.sh" > map.sh
bash map.sh

Explanation:

  • OUTDIR=bams: Sets the output directory for BAM files.
  • REF=...: You should set this to the path of your reference genome file.
  • mkdir -p "$OUTDIR": Creates the output directory if it doesn’t exist.
  • bwa index "$REF": Builds the BWA index of the reference (needed once, before mapping).
  • The loop structure is similar to the basic example, but now we’re processing the files:
    • bwa mem: Aligns the reads to the reference genome (SAM output is the default).
      • -t 6: Uses 6 threads.
    • samtools view -F4: Converts SAM to BAM and filters out unmapped reads.
    • samtools sort: Sorts the BAM file.
      • -@ 4: Uses 4 threads for sorting.
    • samtools index: Indexes the sorted BAM file for faster access.

This loop efficiently processes all paired-end read files in the $VIROME directory, aligning them to a reference genome, converting to sorted BAM files, and indexing the results.

Remember to replace the $REF variable with the actual path to your reference genome before running this script.

Exploring your data with Anvi’o

You can do the Anvi’o data processing yourself on your own computer using the Anvio install that you have used earlier in the week. We will provide the necessary files for you to download from Dropbox so you don’t have to worry about downloading your files from the VM.

:warning: The filenames in the dropbox download might be slightly different, but the content will be the same