#!/bin/sh -e

##########################################################################
#   Description:
#       Convenient wrapper around ms-stack.py, placing species in
#       evolutionary order for easier viewing.
#
#   Arguments:
#       gene-name (e.g. jun, gap43)
#       
#   History:
#   Date        Name        Modification
#   2022-02-12  Jason Bacon Begin
##########################################################################

usage()
{
    cat << EOM

Usage: $0 [ms-stack.py flags] adjacent-genes max-nt orthologs-file \\
	gene-name [gene-name ...]

Example: $0 4 100000 de-tf-ortho.txt jun

will display a stack of Regions/*-jun-*-4-1000000.gff3

EOM
    ms-stack.py --help
    exit 1
}


##########################################################################
#   Main
##########################################################################

while [ 0$(echo $1 | cut -c 1,1) = 0'-' ]; do
    flags="$flags $1"
    shift
done

if [ $# -lt 4 ]; then
    usage
fi
adjacent_genes=$1
max_nt=$2
ortho_file=$3
shift
shift
shift
printf "\n$*\n\n"

for species in Danio_rerio Oryzias_latipes Takifugu_rubripes \
    Mus_musculus Rattus_norvegicus Homo_sapiens; do
    for gene in $@; do
	ortho_list=$(awk -F '|' -v gene=$gene '$1 == gene' $ortho_file | tr '|' ' ')
	for ortho in $ortho_list; do
	    gene_files="Regions/$species-$ortho-*-$adjacent_genes-$max_nt*.gff3"
	    for file in $gene_files; do
		if [ -e $file ]; then
		    files="$files $file"
		fi
	    done
	done
    done
done
if [ -n "$files" ]; then
    ms-stack.py $flags $files
else
    printf "No files for adjacent_genes=$adjacent_genes, max_nt=$max_nt, gene=$gene\n"
fi
