#!/bin/bash


## pre-step: calculates static gdv
## input parameters: static gdv, dynamic gdv
## step1: calculates static gdv-s, dynamic gdv-s
## step2: compare static gdv-s and dynamic gdv-s
## step3: plot the results of static-vs-dynamic-gdvs
## step4: principal component analysis
## step5: plot the results of the principal component analysis
## step6: Pearson correlation
## step7: Spearman's rank correlation

netName=$1
statGDV=$2
dynGDV=$3
ofolder=$4
tmpGraphletF=$5

if [ -e $ofolder ];then
	rm -r $ofolder
fi
mkdir $ofolder
## step1:
## calculate static gdv-s
statGDVS=$netName"-sgdvs.txt"
echo "calculating static gdv-s"
./gdvs3_4 $statGDV $statGDV $ofolder/$statGDVS
## calculate dynamic gdv-s
dynGDVS=$netName"-dgdvs.txt"
echo "calculating dynamic gdvs"
num=`wc -l $tmpGraphletF | cut -d' ' -f 1`
echo $num
./gdvs_dynamics $dynGDV $dynGDV $ofolder/$dynGDVS $tmpGraphletF $num

## step2: compare static gdv-s and dynamic gdv-s
echo "running static-vs-dynamic-gdvs now"
StaDynOutput=$netName"-StatDyn-output.txt"
python static-vs-dynamic-gdvs.py -s $ofolder/$statGDVS -d $ofolder/$dynGDVS -o $ofolder/$StaDynOutput

## step3: plot the results of static-vs-dynamic-gdvs
gnufile=$netName"-StatDyn.gnuplot"
gnuOutput=$netName"-StatDyn.eps"

sed 's/testtest/'"$ofolder\/$StaDynOutput"'/g; s/XYZ/'"$ofolder\/$gnuOutput"'/g; s/networkname/'"$netName"'/g' static-dynamic-gdvs.gnuplot > $ofolder/$gnufile
gnuplot $ofolder/$gnufile

## step4: principal component analysis
echo "running pca"
pcaF=$netName"-pca.txt"
pcaOutput=$netName"-pca.eps"
pcaGNU=$netName"-pca.gnuplot"
Rscript pca.r $statGDV $dynGDV > $ofolder/$pcaF

pcatmp=$pcaF"tmp"
## step5: plot the results of the principal component analysis
awk '{if(NR!=1) print $3 "\t" $4}' $ofolder/$pcaF > $ofolder/$pcaF"tmp"

sed 's/testtest/'"$ofolder\/$pcatmp"'/g; s/XYZ/'"$ofolder\/$pcaOutput"'/g; s/networkname/'"$netName"'/g' static-dynamic-pca.gnuplot > $ofolder/$pcaGNU
gnuplot $ofolder/$pcaGNU
rm $ofolder/$pcaF"tmp"

## step6: Pearson correlation
## step7: Spearman's rank correlation
echo "running correlation analysis"
corrF=$netName"-corrSummary.txt"
Rscript correlation.r $ofolder/$StaDynOutput > $ofolder/$corrF


