#!/bin/csh -f
#Script to remove alternate conformations and hydrogens from PDB file

#Get all ATOM records and remove acetylated N-terminus
grep ^ATOM $1 | grep -v "ACE" | grep -v "NME" > tmp0

#Remove hydrogens
awk '((substr($0,14,1))!="H")&&((substr($0,13,1))!="H")' tmp0 > tmp00
awk '((substr($0,14,1))!="D")&&((substr($0,13,1))!="D")' tmp00 > tmp1

#Remove alternate conformations (some have U and L for un-liganded and liganded,
#  we will take the unliganded form here)
#Some put it in column 27 and start with "A" for the first alternate
awk '((substr($0,17,1))!="B")&&((substr($0,17,1))!="2")&&((substr($0,17,1))!="L")&& \
      ((substr($0,27,1))!="A")&&((substr($0,27,1))!="B")&&((substr($0,27,1))!="C")&& \
       ((substr($0,27,1))!="D")&&((substr($0,27,1))!="E")&&((substr($0,27,1))!="F")&& \
      ((substr($0,27,1))!="G")&&((substr($0,27,1))!="H")&&((substr($0,27,1))!="I")' tmp1 > tmp2

#Print records from first alternate or all records if no alternates
awk '\
  ((substr($0,17,1))=="A")||((substr($0,17,1))=="1")||((substr($0,17,1))=="U") {print substr($0,1,16),substr($0,18,63)} \
  ((substr($0,17,1))!="A")&&((substr($0,17,1))!="1")&&((substr($0,17,1))!="U") {print} \
' tmp2 > tmp3

#In case the following were in ATOM records rather than HETATM records
grep -v HOH tmp3 | grep -v PMS | grep -v FOR | grep -v ALK | grep -v ANI | grep -v OXT | grep -v CIO > tmp4

#Change AE1 and AE2 to OE1 and OE2

sed s/AE1/OE1/ tmp4 | sed s/AE2/NE2/ | sed s/AD1/OD1/ | sed s/AD2/ND2/ | sed 's/CD  ILE/CD1 ILE/' > tmp5
#Add END to the end of the PDB file
echo END >> tmp5

#Call the file input_argument.cln
mv tmp5 $1:r.cln 

#Clean up
rm tmp*
