#!/usr/bin/env bash
[[ $# -ge 1 ]] || {
    echo "usage: $0 <odb version>"
    echo
    echo "This will create a orthomapper_levels.sh file for the given OrthoDB version."
    exit 255
}
DIRCMD="$(cd -P "$(dirname "${BASH_SOURCE[0]}")"/ && pwd)"
[[ -f $DIRCMD/get_orthodb_tree.py ]] || { echo "error: missing get_orthodb_tree.py in $DIRCMD"; exit 255; }

ODBVER="$1"
APIURL="https://data.orthodb.org/$ODBVER"

# get actual version string
if [[ $ODBVER == "current" ]]; then
    ODBVER=$(curl -H "Accept: text/plain" --silent $APIURL/orthodb_release_id 2>/dev/null)
    if [[ $ODBVER == "v11.0" ]]; then
        ODBVER="v11"
    elif [[ $ODBVER == "v12.0" ]]; then
        ODBVER="v12"
    else
        echo "error: retrieved an unknown version string: $ODBVER"
        exit 255
    fi
    APIURL="https://data.orthodb.org/$ODBVER"
fi

DIRETC="${DIRCMD}/../etc/$ODBVER"
[[ -d $DIRETC ]] || { echo "error: missing etc dir at $DIRETC"; exit 255; }

# output file
ODBLEVELS=$DIRETC/orthomapper_levels.sh
#
if [[ -f $ODBLEVELS ]]; then
    echo "skip: orthomapper_levels.sh file already exists for version $ODBVER"
    echo "      $ODBLEVELS"
    exit 0
fi

declare -A ALL_EUK
declare -A ALL_MET

export _RESULT_=
get_nodes() {
    local n="$1"
    echo "Getting nodes for $n"
    _RESULT_="$($DIRCMD/get_orthodb_tree.py -n --odbver $ODBVER -r $n -N)"
    [[ -n $_RESULT_ ]] || return 255
    return 0
}

echo "---------------------------------------------"
echo "Create odb levels file"
echo "API version: $ODBVER"
echo "Target file: $ODBLEVELS"
echo "---------------------------------------------"

# eukaryota
get_nodes 7742  || { echo "error: no vertebrata found in tree!"; exit 255; }
V=( $_RESULT_ )
VR=7742
for x in ${V[@]}; do ALL_EUK[$x]=1; ALL_MET[$x]=1; done
                     
get_nodes 6656  || { echo "error: no arthropoda found in tree!"; exit 255; }
A=( $_RESULT_ )
AR=6656
for x in ${A[@]}; do ALL_EUK[$x]=1; ALL_MET[$x]=1; done

get_nodes 33090 || { echo "error: no plants found in tree!"; exit 255; }
P=( $_RESULT_ )
PR=33090
for x in ${P[@]}; do ALL_EUK[$x]=1; done

get_nodes 4751  || { echo "error: no fungi found in tree!"; exit 255; }
F=( $_RESULT_ )
FR=4751
for x in ${F[@]}; do ALL_EUK[$x]=1; done

get_nodes 33208 || { echo "error: no metazoa found in tree!"; exit 255; }
_M=( $_RESULT_ )
M=()
for x in ${_M[@]}; do
    [[ -z ${ALL_MET[$x]} ]] || {
        continue
    }
    M+=($x)
done
MR=33208

for x in ${M[@]}; do ALL_EUK[$x]=1; done

# eukaryota, unique for 2759
get_nodes 2759  || { echo "error: no vertebrata root found in tree!"; exit 255; }
_K=( $_RESULT_ )
K=()
for x in ${_K[@]}; do
    [[ -z ${ALL_EUK[$x]} ]] || continue
    K+=($x)
done
KR=2759

#----------------------------------------------------------------
# protozoa
get_nodes 33630  || { echo "error: no protozoa root found in tree!"; exit 255; }
Z=( $_RESULT_ )
ZR=33630

# bacteria
get_nodes 2  || { echo "error: no bacteria root found in tree!"; exit 255; }
B=( $_RESULT_ )
BR=2

# archaea
get_nodes 2157  || { echo "error: no archaea root found in tree!"; exit 255; }
R=( $_RESULT_ )
RR=2157

# viridae
E=()
ER=10239
get_nodes 10239  || { echo "error: no viridae root found in tree!"; exit 255; }
E=( $_RESULT_ )





# keep only those unique to Metazoa in M
###M=( $(printf "%s\n" "${A[@]}" "${V[@]}" "${M[@]}" | sort -n | uniq -u) )

# print output
cat > $ODBLEVELS <<EOF
#--------------------------------------------------------------------
# Static data specific for OrthoDB $ODBVER - do not modify
# API url   : $APIURL
# Created   : $(date)
# By        : $(uname)
#--------------------------------------------------------------------

if [[ "\${LOADED_ORTHOMAPPER_LEVELS_SH+1}" == "" ]]; then
    export LOADED_ORTHOMAPPER_LEVELS_SH=\${LOADED_ORTHOMAPPER_LEVELS_SH:=255}

    # all clades
    declare -xr MAP_ROOT_CLADES=( VERTEBRATA ARTHROPODA METAZOA FUNGI PLANTS EUKARYOTA BACTERIA ARCHAEA VIRIDAE PROTOZOA )

    # all vertebrate nodes
    declare -xr MAP_NODE_ROOT_VERTEBRATA=$VR
    declare -xr MAP_NODES_VERTEBRATA=( ${V[*]} )

    # all arthopoda nodes
    declare -xr MAP_NODE_ROOT_ARTHROPODA=$AR
    declare -xr MAP_NODES_ARTHROPODA=( ${A[*]} )

    # fungi
    declare -xr MAP_NODE_ROOT_FUNGI=$FR
    declare -xr MAP_NODES_FUNGI=( ${F[*]} )

    # all plant nodes
    declare -xr MAP_NODE_ROOT_PLANTS=$PR
    declare -xr MAP_NODES_PLANTS=( ${P[*]} )

    # protozoa
    declare -xr MAP_NODE_ROOT_PROTOZOA=$ZR
    declare -xr MAP_NODES_PROTOZOA=( ${Z[*]} )

    # metazoa
    declare -xr MAP_NODE_ROOT_METAZOA=$MR
    declare -xr MAP_NODES_METAZOA=( ${M[*]} )

    # eukaryota (only the root and any not in Arthropoda, Vertebrata or Metazoa)
    declare -xr MAP_NODE_ROOT_EUKARYOTA=2759
    declare -xr MAP_NODES_EUKARYOTA=( ${K[*]} )

    # all bacteria nodes
    declare -xr MAP_NODE_ROOT_BACTERIA=$BR
    declare -xr MAP_NODES_BACTERIA=( ${B[*]} )

    # archaea
    declare -xr MAP_NODE_ROOT_ARCHAEA=$RR
    declare -xr MAP_NODES_ARCHAEA=( ${R[*]} )

    # viridae
    declare -xr MAP_NODE_ROOT_VIRIDAE=$ER
    declare -xr MAP_NODES_VIRIDAE=( ${E[*]} )

    # define directories
    declare -A MAP_DIRS
    MAP_DIRS['VERTEBRATA']=eukaryota
    MAP_DIRS['ARTHROPODA']=eukaryota
    MAP_DIRS['METAZOA']=eukaryota
    MAP_DIRS['FUNGI']=eukaryota
    MAP_DIRS['PLANTS']=eukaryota
    MAP_DIRS['EUKARYOTA']=eukaryota
    MAP_DIRS['PROTOZOA']=eukaryota

    MAP_DIRS['BACTERIA']=bacteria
    MAP_DIRS['ARCHAEA']=archaea
    MAP_DIRS['VIRIDAE']=viridae

    LOADED_ORTHOMAPPER_LEVELS_SH=0
fi

#--- end of file ---
EOF

echo "DONE"
