#!/bin/bash

# Copyright 2016-2026, André Müller (github.com/muellan)
#
# This file is part of the MetaCache taxonomic sequence classification tool.
#
# MetaCache is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# MetaCache is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MetaCache.  If not, see <http://www.gnu.org/licenses/>.

set -u  # Protect against uninitialized vars.
set -e  # Stop on error


if [ $# -lt 1 ]; then
  echo "usage: $0 <database filename>"
  exit
fi

DB=$1


if [ -e $DB ]; then
  echo "collecting statistics for database $DB"

  ./metacache info $DB statistics | tail -n+1 > ${DB}_statistics.txt

  ./metacache info $DB lineages | tail -n+1 > ${DB}_lineages.txt

  RANKSTAT=${DB}_ranks.txt
  rm -f $RANKSTAT

  for r in sequence subspecies species genus family order class phylum kingdom domain; do
    RESFILE=${DB}_rank_${r}.txt

    ./metacache info $DB rank ${r}  | tail -n+2 > $RESFILE

    nr=$(cat $RESFILE | wc -l)
    nr=$(( $nr - 1))

    echo "$r: $nr" >> $RANKSTAT
  done

fi
