#!/bin/bash
#The script allows you to insert loops of dummy atoms into the contact map matrix. 
#Thanks to that ft_comar does not draw the  ends of consecutive protein chains together.

#USAE:
#cmap_insert_loop <loop length> <insertion_pt1> <insertion_pt2> ... 
#cmap_insert_loop -f <loop_file_path>


AWK_SCRIPT_PATH=/home/bob/Contacts2Struct/c2s_pipeline_src/cmap_insert_loop.awk # SET TO PROPER PATH !!!

EXPECTED_ARGS=3;
BAD_ARGS=1;
NOT_SUPPORTED=2;

if [ $# -lt ${EXPECTED_ARGS} -o "$1" == "help" ]
then
        echo "USAGE:"
	echo "cmap_insert loop <contact_map_file> <loop length> <insertion_pt1> <insertion_pt2> ..."
	echo "cmap_insert_loop <contact_map_file> -f <file_path>"
        exit $BAD_ARGS
fi

cmap_file=$1
tmp_file1="${cmap_file:0:$[${#cmap_file}-5]}_loop.cmap"
tmp_file2=${cmap_file}_tmp
cp $cmap_file $tmp_file1
counter=0;
if [ $2 == "-f" ]
then 
	echo "Reading loop file: $3"
	echo "File reading not supported yet"	
	exit $NOT_SUPPORTED
else
	loop_length=$2
	echo "Chosen loop length: $loop_length"
	for i in ${@:3:$#}
	do
		insert_pt=$[$i+${loop_length}*${counter}]
		echo "Inserting loop in point $i ( $insert_pt) "
		awk -v insert_pt=${insert_pt} -v loop_length=${loop_length} -f ${AWK_SCRIPT_PATH} ${tmp_file1} > ${tmp_file2}
		mv $tmp_file2 $tmp_file1
		
		counter=$[${counter}+1]
		#echo $counter
	done
fi
echo "Output file: $tmp_file1"


