#!/bin/bash
#
# Code by  venkentom@gmail.com
#
# Licenced by the GPL General Public License
# Have a look at:
# http://www.gnu.org/copyleft/gpl.html
# For more information about the GPL License
#
# I am not responsible for what this code does to your hw/sw/mind
# Use at your own risk
#
# Usage: sh occupancy.sh mindistnum_CHOL_all.xvg > mindistnum_CHOL_all_occ.xvg
#


echo "Residue Avg Stdev"

file=$1 #pass file as input!
VALUE=0
#loop over all residues
for k in {0..18}; do
let COUNTER=0 #counts the number of instances above threshold
let STEP=1 #counter for the ns
let c=1 #counter for the intervals
let INT=500 #size of each interval (in ns)
ARRAY=()
while read -r -a line ; do
    VALUE=${line[$k]} 
#    echo $VALUE
    if (( $VALUE > 20 )); then #define threshold
	let COUNTER=COUNTER+1 #count this
#        echo $COUNTER
    fi
#    echo $VALUE " " $COUNTER
    let STEP=STEP+1 #go to next step
    let PS=$c*$INT
    if [[ $STEP = $PS ]]; then #each interval
    ARRAY=(${ARRAY[*]} $((COUNTER * 100 / INT))) #put value in array
#    echo "${ARRAY[*]}"
    let COUNTER=0 #and reset
    let c=$c+1 #update interval counter
    fi
done < $file #mindistnum_CHOL_all.xvg
#echo "${ARRAY[*]}" #printout counts in each interval
printf $((k+1))
echo ${ARRAY[@]} | awk '{for(i=1;i<=NF;i++){sum+=$i}; printf " " sum/NF}' #printout average of intervals
echo ${ARRAY[@]} | awk 'NF {sum=0;ssq=0;for (i=1;i<=NF;i++){sum+=$i;ssq+=$i**2}; printf " " (ssq/NF-(sum/NF)**2)**0.5 "\n"}' #printout standard deviation
#echo ${ARRAY[@]} | awk '{for(i=1;i<=NF;i++){sum+=$i};printf "Avg = " sum/NF}' #printout average of intervals
#echo ${ARRAY[@]} | awk 'NF {sum=0;ssq=0;for (i=1;i<=NF;i++){sum+=$i;ssq+=$i**2}; printf "Std Dev = " (ssq/NF-(sum/NF)**2)**0.5 "\n"}' #printout standard deviation
done


