Extract Number of Reads to a Table from Bowtie2 Output

After using Nextflow Taxprofiler pipeline, I’ve got a bunch of *bowtie2out.txt files bowtie2 the output of bowtie2. In microbiome research, we sometimes need the number of reads per sample to account for sample depth. This info is readily available in these bowtie2 outfiles.

To extract this info to a tsv table use this script:

#!/usr/bin/env bash

echo -e "filename\tnreads" >nreads_table.tsv

for f in *bowtie2out.txt; do
    nreads=$(awk '$1 == "#nreads" {print $2; exit}' "$f")
    echo -e "$(basename "$f")\t$nreads"
done >>nreads_table.tsv

This will create a tab-separate table nreads_table.tsv with two columns: one for the name of the file and the other for the number of reads in that sample.