#!/bin/bash
#The script reformats the output from FT-COMAR. FT-COMAR returns a pdb-like file with the coordinates of CA atoms. They are all marked as
#Alanin CA's. The script uses the PDB file to change the reside names into proper ones. It also inserts TER records where chains change.

#USAGE:
#./format_FT_COMAR <real-pdb> <FT-COMAR output> 



awk '$3~/CA\>/ {printf ("%s %s%4d\n",$4,$5,$6)}' $1 > tmp
sed -n -e 's/\(\<[A-Z]\)\(\([0-9]\+\|\>\)\)/\1 \2/p' $2 > tmp2
cat -b tmp > tmp1
cat -b tmp2 > tmp3
join -j1 1 -j2 1 tmp1 tmp3 | awk '{
num++;
if(num == 1)chain=$3;
if($3!=chain){
	printf ("TER\n");
	chain=$3;
	}
printf("ATOM %6d  CA  %s %s %3d    %8.3f %7.3f %7.3f\n",num,$2,$3,$4,$11,$12,$13)
}' 
echo "TER" 
