#!/bin/bash
# The purpose of this script is to check the tree characteristic of the clusters
# generated.
# The tree is:
#
# 7147
#   |-- 7148 - 7157 - 7164
#   |
#   |-- 7203 -------- 7393
#         |
#         |-- 7214 -- 7215
#
[[ -f common.sh ]] || { echo "error: missing common.sh"; exit 255; }
[[ -d Cluster   ]] || { echo "error: missing directory Cluster"; exit 255; }

evalthis=$(grep DIR_BRHCLUS common.sh)
[[ ${evalthis:0:11} == "DIR_BRHCLUS" ]] && eval $evalthis
[[ -d $DIR_BRHCLUS ]] || { echo "error: unable to evaluate DIR_BRHCLUS - is it defined in common.sh?"; exit 255; }

cmpclus=$DIR_BRHCLUS/cmpclus
[[ -x $cmpclus ]] || { echo "error: missing cmpclus tool in DIR_BRHCLUS=$DIR_BRHCLUS"; exit 255; }

check_for_subset() {
    [[ $# -eq 2 ]] || return 255
    local A="$1"
    local B="$2"
    local fA=Cluster/${A}.og_raw
    local fB=Cluster/${B}.og_raw
    $cmpclus --A $fA --B $fB mismatch >& cmp_${A}_${B}.log
    local n=0
    n=$(grep '^TBL:' cmp_${A}_${B}.log | grep -v SUBSET | wc -l)
    local res="FAIL"
    [[ $n == "1" ]] && res="OK"
    printf "%s %s        [%s]\n" "$A" "$B" "$res"
       
}

echo "# Test that clusters further down the tree are strict subsets of parents"
check_for_subset 7164 7157
check_for_subset 7157 7148
#check_for_subset 7148 7147

#check_for_subset 7393 7203
#check_for_subset 7203 7147

#check_for_subset 7215 7214
#check_for_subset 7214 7203
