#!/bin/bash

#####
#####
##
##   Run me from IAM/ANALYSIS/PhiPsi_Bins
##   
##   comment out "./bin_phipsi S", below, for the peptide-only runs
##
#####
#####

#Here's how I figured out a good set of contours to use.

#0.  Generate the binned data

#From the directory above:
cd ../ 
./bin_phipsi P
./bin_phipsi S
cd PhiPsi_Bins

#1.  sort the XYZ data by the first Z column    

sort -n -k3 < P_CONT_1  > P_CONT_1_sorted
sort -n -k3 < P_CONT_2  > P_CONT_2_sorted
sort -n -k3 < P_CONT_3  > P_CONT_3_sorted
sort -n -k3 < P_CONT_4  > P_CONT_4_sorted
sort -n -k3 < S_CONT_1  > S_CONT_1_sorted
sort -n -k3 < S_CONT_2  > S_CONT_2_sorted
sort -n -k3 < S_CONT_3  > S_CONT_3_sorted
sort -n -k3 < S_CONT_4  > S_CONT_4_sorted
 
#2.  grep out the entries that are all zero (should only occur in Z)

grep -v " 0.00000" P_CONT_1_sorted > P_CONT_1_sorted_nozero
grep -v " 0.00000" P_CONT_2_sorted > P_CONT_2_sorted_nozero
grep -v " 0.00000" P_CONT_3_sorted > P_CONT_3_sorted_nozero
grep -v " 0.00000" P_CONT_4_sorted > P_CONT_4_sorted_nozero
grep -v " 0.00000" S_CONT_1_sorted > S_CONT_1_sorted_nozero
grep -v " 0.00000" S_CONT_2_sorted > S_CONT_2_sorted_nozero
grep -v " 0.00000" S_CONT_3_sorted > S_CONT_3_sorted_nozero
grep -v " 0.00000" S_CONT_4_sorted > S_CONT_4_sorted_nozero

#3.  get the top and bottom of each file

echo "Gathering contour range on $(date)" > RANGES
echo P_CONT_1 >> RANGES
head -n 1 P_CONT_1_sorted_nozero >> RANGES
tail -n 1 P_CONT_1_sorted_nozero >> RANGES
echo P_CONT_2 >> RANGES
head -n 1 P_CONT_2_sorted_nozero >> RANGES
tail -n 1 P_CONT_2_sorted_nozero >> RANGES
echo P_CONT_3 >> RANGES
head -n 1 P_CONT_3_sorted_nozero >> RANGES
tail -n 1 P_CONT_3_sorted_nozero >> RANGES
echo P_CONT_4 >> RANGES
head -n 1 P_CONT_4_sorted_nozero >> RANGES
tail -n 1 P_CONT_4_sorted_nozero >> RANGES
echo S_CONT_1 >> RANGES
head -n 1 S_CONT_1_sorted_nozero >> RANGES
tail -n 1 S_CONT_1_sorted_nozero >> RANGES
echo S_CONT_2 >> RANGES
head -n 1 S_CONT_2_sorted_nozero >> RANGES
tail -n 1 S_CONT_2_sorted_nozero >> RANGES
echo S_CONT_3 >> RANGES
head -n 1 S_CONT_3_sorted_nozero >> RANGES
tail -n 1 S_CONT_3_sorted_nozero >> RANGES
echo S_CONT_4 >> RANGES
head -n 1 S_CONT_4_sorted_nozero >> RANGES
tail -n 1 S_CONT_4_sorted_nozero >> RANGES

