#!/bin/bash
# Build the relatedTracks table for each assembly
set -beEu -o pipefail

function usage() {
    printf "Usage: %s relatedTrack_name db1 db2 ...\n" "`basename $0`"
    printf "Build the relatedTrack tables for each assembly listed in relatedTracks.ra\n"
    printf "The first argument must be the name of the table to build, followed by all\nthe"
    printf "assemblies to update (similar to trackDb update process)\n"
}

if [ $# -lt 2 ] ; then
    echo "wrong # args" >&2
    usage >&2
    exit 1
fi

tbl="$1"; shift;

for db in $*; do
    set +e
    grep -wq "^$db" relatedTracks.ra &> /dev/null
    if [ $? -eq 0 ]
    then
        set -e
        # enfore two lines per relationship, skipping the '>' and '~' entries
        # that state their own direction:
        recip=$(grep -w "^$db" relatedTracks.ra | grep -v "^[^ ]* [>~]" || true)
        uniqTables=$(comm -3 <(echo "$recip" | cut -d' ' -f2 | sort) <(echo "$recip" | cut -d' ' -f3 | sort) | wc -l)
        if [ $uniqTables -gt 0 ]; then
            printf "ERROR: mismatched primary and secondary related tracks for '%s'\n" "$db"
            echo "Tracks:" $(comm -3 <(echo "$recip" | cut -d' ' -f2 | sort) <(echo "$recip" | cut -d' ' -f3 | sort))
            exit 255
        fi
        # '>track1' is one way, so just drop the marker.  '~track1' is bidirectional,
        # so emit the line as written and again with the two tracks swapped.  In that
        # second expression \1 is track1, \2 is track2 and \3 is the reason text.
        grep -w "^$db" relatedTracks.ra | cut -d' ' -f2- \
            | sed -e 's/^>//' \
                  -e 's/^~\([^ ]*\) \([^ ]*\) \(.*\)/\1 \2 \3\n\2 \1 \3/' \
            | sed -e 's/ /\t/' -e 's/ /\t/' | sort -k1 -k2 \
            | /cluster/bin/x86_64/hgLoadSqlTab "$db" "$tbl" ~/kent/src/hg/lib/relatedTrack.sql stdin
        printf "done building relatedTrack table for %s\n" "$db"
    fi
done
