Skip to content

Instantly share code, notes, and snippets.

@rpetit3
Created March 11, 2026 17:50
Show Gist options
  • Select an option

  • Save rpetit3/d2c26c2828f2b83686d243aeab2e0d94 to your computer and use it in GitHub Desktop.

Select an option

Save rpetit3/d2c26c2828f2b83686d243aeab2e0d94 to your computer and use it in GitHub Desktop.
Build Kraken2 Host Database
# crete conda environment
conda create -n dehosting -y ncbi-datasets-cli kraken2 seqfu
# Create/upload host inpo
head hosts.tsv
name taxid accession
cow 9913 GCF_002263795.3
mouse 10090 GCF_000001635.27
sheep 9940 GCF_016772045.2
# get the accessions
cut -f 3 hosts.tsv | grep -c accession > accessions.txt
# Preview genomes using accessions file
datasets download genome accession --inputfile accessions.txt --preview
Collecting 18 genome records [================================================] 100% 18/18
{"resource_updated_on":"2026-03-11T15:21:00Z","record_count":18,"estimated_file_size_mb":12827,"included_data_files":{"all_genomic_fasta":{"file_count":18,"size_mb":12827.134}}}
# Download the genomes
datasets download genome accession --inputfile accessions.txt
Collecting 18 genome records [================================================] 100% 18/18
Downloading: ncbi_dataset.zip 13.5GB valid data package
Validating package files [================================================] 100% 22/22
# Extract the downloaded genomes
unzip ncbi_dataset.zip -d ncbi_dataset
# move genomes
cat hosts.tsv | \
grep -v name | \
awk '{print "./ncbi_dataset/data/"$3"/*.fna hosts/"$1"/"$3".fna"}' | \
xargs -I {} bash -c "mv {}"
# FASTA files
find -name "*.fna"
./hosts/pig/GCF_000003025.6.fna
./hosts/sheep/GCF_016772045.2.fna
...
./hosts/human-standard-ref/GCF_000001405.40.fna
./hosts/prarie-dog/GCA_052576455.1.fna
# Seqfu tax id
cat hosts.tsv | \
grep -v name | \
awk '{print "seqfu cat --append \"|kraken:taxid|"$2"\" hosts/"$1"/"$3".fna > hosts/"$1"/"$3".taxid.fna"}' > taxid.sh
# Add taxid to FASTA headers
bash -x ./taxid.sh
+ seqfu cat --append '|kraken:taxid|9913' hosts/cow/GCF_002263795.3.fna
+ seqfu cat --append '|kraken:taxid|10090' hosts/mouse/GCF_000001635.27.fna
...
+ seqfu cat --append '|kraken:taxid|45480' hosts/prarie-dog/GCA_052576455.1.fna
+ seqfu cat --append '|kraken:taxid|9986' hosts/rabbit/GCF_964237555.1.fna
# verify taxid in FASTA headers
head hosts/cow/GCF_002263795.3.taxid.fna | grep ">"
# Add genomes to kraken2 database
mkdir -p hostdb
find -name "*.taxid.fna" | xargs -I {} kraken2-build --add-to-library {} --db hostdb/ --threads 30
# Download taxonomy
kraken2-build --download-taxonomy --db hostdb/
# Build the kraken2 database
# NOTE: this will take a long time, and if will need to be repeated if you add
# more genomes to the database
kraken2-build --build --db hostdb/ --threads 30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment