#!/bin/bash

PPFILE=$1
THRESHOLD=$2
ALPHAVALUE=$3
BETAVALUE=$4

PPFILEEXT=`echo $PPFILE | awk -F . '{print $NF}'`
PPNAME=`echo $PPFILE | awk -F / '{print $NF}' | awk -F .$PPFILEEXT '{print $1}'`


echo "Phylo-Profile: "$PPNAME"; Threshold: "$THRESHOLD"; Alpha Value: "$ALPHAVALUE"; Beta Value: "$BETAVALUE




JAVA="/usr/java/jdk1.6.0_04/bin/java"

BIN="bin"
TEMP="tmp"
DATA="data"
OUTPUT="output"


SPECIESCOGMAPFILE=$DATA"/string/species.mappings.v8.1.txt"
COGLINKFILE=$DATA"/string/COG.links.v8.1.txt"
COGNAMEMAPFILE=$DATA"/cogs/COG-Name.map"




EXPDATADIR=$DATA"/experimental/"$PPNAME
EXPOUTDIR=$OUTPUT"/"$PPNAME"/"$THRESHOLD

TDGDIR=$EXPDATADIR"/typed-divided-graphs"
VMDIR=$EXPDATADIR"/vertex-maps"
DMDIR=$EXPDATADIR"/division-maps"


CLQDIR=$EXPOUTDIR"/ab-cliques"
POUTDIR=$EXPOUTDIR"/pdfs-output"
RCMDIR=$EXPOUTDIR"/raw-cog-modules"
OCMDIR=$EXPOUTDIR"/organized-cog-modules"
CMSDIR=$EXPOUTDIR"/cog-module-summaries"


if [ ! -d $EXPDATADIR ]; then
	if [ ! -d $DATA"/experimental" ]; then
		mkdir $DATA"/experimental"
	fi

	mkdir $EXPDATADIR
	mkdir $TDGDIR
	mkdir $VMDIR
	mkdir $DMDIR
fi


if [ ! -d $EXPOUTDIR ]; then
	if [ ! -d $OUTPUT/$PPNAME ]; then
		mkdir $OUTPUT/$PPNAME
	fi

	mkdir $EXPOUTDIR
	mkdir $CLQDIR
	mkdir $POUTDIR
	mkdir $RCMDIR
	mkdir $OCMDIR
	mkdir $CMSDIR
fi




DATASTEM=$PPNAME"_"$THRESHOLD
OUTSTEM=$PPNAME"_"$THRESHOLD"_"$ALPHAVALUE"_"$BETAVALUE

GRAPHFILE=$TDGDIR"/"$DATASTEM".tdg"
VERTMAPFILE=$VMDIR"/"$DATASTEM".vert.map"
DIVMAPFILE=$DMDIR"/"$DATASTEM".div.map"


CLIQUEFILESTEM=$CLQDIR"/"$OUTSTEM
CLIQUEFILE=$CLQDIR"/"$OUTSTEM".0000-000"
PDFSOUTPUTFILE=$POUTDIR"/"$OUTSTEM".pdfs.out"

RAWCOGOUTPUTFILE=$RCMDIR"/"$OUTSTEM".mods.raw.list"
ORGANIZEDCOGOUTPUTFILE=$OCMDIR"/"$OUTSTEM".mods.orgnz.tab"
SUMMARYOUTPUTFILE=$CMSDIR"/"$OUTSTEM".mods.summ.tab"


TEMPHEADERFILE=$TEMP"/"$DATASTEM".head.list"
TEMPTYPEDIVMAPFILE=$TEMP"/"$DATASTEM".div.list"
TEMPDIVVERTMAPFILE=$TEMP"/"$DATASTEM".vert.list"
TEMPEDGEFILE=$TEMP"/"$DATASTEM".edge.list"


if [ -f $RAWCOGOUTPUTFILE ];
then
	echo "COG Modules exist for these parameters.  No new data generated."
	exit
fi

if [ ! -f $GRAPHFILE -o ! -f $VERTMAPFILE ];
then
	echo -n "Building graph file..."
	$JAVA -jar $BIN/COGThresholdGraphBuilder.jar -p $PPFILE -t $THRESHOLD -m $SPECIESCOGMAPFILE -l $COGLINKFILE -h $TEMPHEADERFILE -d $TEMPTYPEDIVMAPFILE -v $TEMPDIVVERTMAPFILE -e $TEMPEDGEFILE -dm $DIVMAPFILE -vm $VERTMAPFILE

	cat $TEMPHEADERFILE $TEMPTYPEDIVMAPFILE $TEMPDIVVERTMAPFILE $TEMPEDGEFILE > $GRAPHFILE
	rm $TEMPHEADERFILE $TEMPTYPEDIVMAPFILE $TEMPDIVVERTMAPFILE $TEMPEDGEFILE
	echo "DONE!"
else
	echo "Using previously built graph file "$GRAPHFILE
fi

if [ ! -f $CLIQUEFILE ];
then
	echo -n "Enumerating alpha-beta cliques..."
	$BIN/AB-Clique -f $GRAPHFILE -o $CLIQUEFILESTEM -a $ALPHAVALUE -b $BETAVALUE > $PDFSOUTPUTFILE
	echo "DONE!"
else
	echo "Using previously enumerated alpha-beta cliques in "$CLIQUEFILE
fi

echo -n "Parsing pDFS output..."
$JAVA -jar $BIN/ABCliqueOutputConverter.jar -p $PPFILE -m $SPECIESCOGMAPFILE -n $COGNAMEMAPFILE -c $CLIQUEFILE -v $VERTMAPFILE -r $RAWCOGOUTPUTFILE -o $ORGANIZEDCOGOUTPUTFILE -s $SUMMARYOUTPUTFILE
echo "DONE!"
