#!/bin/sh

##############################################################################################
# Purpose: Generate node feature of a weighted dynamic network
##############################################################################################

## Load modules########################
# PATH to R
export PATH="/scratch/knewaz/knewaz/R-3.2.3/bin:$PATH"

## Intialization ######################
# for scaling weights # put any negative value for no normalization
epsilon=0.01

# higher-order relationship
hon=1 # constant as 1

# PATH to dynamic networks folder
netwalkdynDir=input-network
dynDir=$netwalkdynDir-processed

outdynDir=$netwalkdynDir-norm-diff

outdynDirLeda=$outdynDir-leda

outDir=$outdynDir-output
outDir1=$outdynDir-feat-runtime
outDir3=$outdynDir-feat-concat-runtime
outDir4=$outdynDir-feat-concat-vect-runtime
output=$outdynDir-final-output

if [ -f $output ]; then
	rm $output
fi
mkdir $output

bb=outputFeature
if [ -f $output/$bb.txt ]; then
	rm $output/$bb.txt
fi
touch $output/$bb.txt


if [ -d $dynDir ]; then
	rm -r $dynDir
fi
mkdir $dynDir

if [ -d $outdynDir ]; then
	rm -r $outdynDir
fi
mkdir $outdynDir

if [ -d $outdynDirLeda ]; then
	rm -r $outdynDirLeda
fi
mkdir $outdynDirLeda

if [ -d $outDir ]; then
	rm -r $outDir
fi
mkdir $outDir

if [ -d $outDir1 ]; then
	rm -r $outDir1
fi
mkdir $outDir1

if [ -d $outDir3 ]; then
	rm -r $outDir3
fi
mkdir $outDir3

if [ -d $outDir4 ]; then
	rm -r $outDir4
fi
mkdir $outDir4



# Different components of the program #

# Preprocess NetWalk output
Rscript script/preprocess-NetWalk-output.r $netwalkdynDir $dynDir


# Given N network snapshots, create differential networks for each HOR
for f in `seq 1 $hon`; do

	start=$( find $outdynDir -type f | wc -l )

	Rscript script/create-diff-networks_norm.r $dynDir $outdynDir $epsilon $f $start

done


# differential network to leda format
for f in $outdynDir/*.txt; do

	b=$(basename $f | cut -d"." -f1)

	./script/list2leda.sh $f > $outdynDirLeda/$b.gw

done


# For each differential network snapshot, generate node level features
for f in $outdynDirLeda/*.gw; do

	b=$(basename $f | cut -d"." -f1)

	if [ ! -d $outDir/$b ]; then
		mkdir $outDir/$b
	fi

	./bin/wtdyn $f $outDir/$b/$b

done


if [ ! -d $outDir1/onehop ]; then
	mkdir $outDir1/onehop
fi

if [ ! -d $outDir1/one_onehop ]; then
	mkdir $outDir1/one_onehop
fi

if [ ! -d $outDir1/twohop ]; then
	mkdir $outDir1/twohop
fi

if [ ! -d $outDir1/two_twohop ]; then
	mkdir $outDir1/two_twohop
fi

# single feature for a node
Rscript script/node-feature-runtime.r $outDir $outDir1/onehop $outDir1/one_onehop $outDir1/twohop $outDir1/two_twohop


for f in $outDir1/one_onehop/*.txt; do

  b=$(basename $f)
  cat $outDir1/one_onehop/$b > $outDir3/$b.tmp
  cut -d " " -f 2- $outDir3/$b.tmp > $outDir3/$b
  rm $outDir3/$b.tmp

done


#vectorize binned features
Rscript script/node-feature-bin-concat-vect-runtime.r $outDir3 $outDir4


# make a single file of features
for f in $outDir4/*.txt; do

  while read line
	do
        echo $line >> "$output/$bb.txt"

	done < "$f"

done


# eliminate zero variation features
Rscript script/eliminate-cols.r $output/$bb.txt $output/$bb.elim

