#!/bin/bash

# $1 = parameter file to calculate average network in parallel
# $2 = average network file name
# $3 = no. of processes to run in parallel  

RANDOM=$$
#run bayesnetty $3 times for X bootstraps each
#all processes are ran simultaneously in the background 
for i in $(seq 1 $3);
do

./bayesnetty $1 -so -seed $i0$RANDOM -average-networks-file $2$i-i.dat -average-networks-igraph-file-prefix $2-graph&

done

#wait for all processes to finish
wait

#collate the results into a final average file
R --vanilla --args $2 $3 < collate-average-nets.R

#delete individual average network files
rm $2*-i.dat 

#plot the final network
#set threshold
t=$(cat "$2-threshold.dat")
sed -i "s/threshold<-/threshold<-$t #/g" $2-graph.R

#set final average file name
sed -i "s/aveGraph<-/aveGraph<-read.table(\"$2.dat\", header=TRUE, stringsAsFactors=FALSE) #/g" $2-graph.R

#plot average network
R --vanilla < $2-graph.R
