#!/bin/sh
#!   Watertidy a) moves your water molecules so that they are in the
#!   same asymmetric unit as your molecule.
#!             b) endeavours to give a rational name to each water.
#!    The convention is: 
#!  For each protein chain assign a "first shell" water chain ID.
#!  Waters are renamed to have the residue NUMBER of the neighbouring O or N
#!  and the CHAIN ID assigned in the command file.
#!  ( " Neighbouring" is decided on the DISTANCE only of the water from the
#!      protein atom - the program DISTANG outputs a table.)
#!   An atom may occur more than once if it is near enough several O or N.
#!   The occupany for the nearest contact is equal to the input occupancy;
#!  all other occupancies are set o a very small value.
#   You may search for second andthird shell waters in a similar way.

#!    Run the distang-parts separately; the log-file is needed for a
#!  watertidy-run. 
#!
#!
#!    To sort waters: edit the water atoms from the end of the output pdb
#!   run the UNIX sort utility,  then put them back in some sensible order.
#!
#!   sort +4 -5 +5 -6 +2.1 -3 waterbit.pdb > waterbit_sort.pdb 
#!  will sort file first on chain id, then on residue number
#!              and finally on atom name
#!

set -e

################### Label Chains in toxd.pdb ##############
#			For watertidy (and others)
pdbset xyzin $CEXAM/toxd/toxd.pdb xyzout $CCP4_SCR/toxd_chnid.pdb << eof-1
spacegroup P212121
renumber 1 60 to 64 
eof-1

#!First H2O shell..
#!DISTANG - run distang first...
#!   Use pdbset.com to put chain labels in .....

distang				\
XYZIN $CCP4_SCR/toxd_chnid.pdb \
<< END-distang | tee $CCP4_SCR/watertidy-3shells.log1
SYMM 19
dist vdw INTRA
RADI   C 1.6 CA 1.6 N 1.6 O 1.6 OW 1.6 S 1.6 P 1.6
FROM ATOM 1 TO 200000     !  See documentation for alternatives
TO ATOM 500 to 200000     !  See documentation for alternatives
END  
END-distang

#  Output file $CCP4_SCR/toxd_chnid.pdb will contain protein and Sulphate
#  atoms followed by waters: Those close to protein will have been renamed;
#  remainder will still have original name and chain ID.

watertidy                          \
XYZIN $CCP4_SCR/toxd_chnid.pdb \
XYZOUT $CCP4_SCR/toxd_wtidy1.pdb \
DISTOUT $CCP4_SCR/watertidy-3shells.log1 \
<< END-watertidy
TITL Watertidy run on original pdb-file
SYMM 19
CHNID  A WATOUTID J RANGE 1 59
CHNID  S WATOUTID K RANGE 1 5
WATID  W
occw 0.01
SHELL 1
end
END-watertidy


#!Second H2O shell..

#  Now check if some of the remaining waters are bonded to relabelled J and K; 
#  ie they are forming a second water shell.
#!Run DISTANG again.

distang XYZIN $CCP4_SCR/toxd_wtidy1.pdb \
   << END-distang | tee $CCP4_SCR/watertidy-3shells.log2
SYMM 19
dist vdw INTRA
RADI   C 1.7 CA 2.2 N 1.7 O 1.7 OW 1.7 S 1.6 P 1.6
FROM ATOM 1 TO 200000     !  See documentation for alternatives
TO ATOM 500 to 200000     !  See documentation for alternatives
END  
END-distang

# Now do this:

#!  only interested now in contacts between H2Os in chains J and K 
#   and H2Os in chain W.
#   watertidy ignores all other contacts in log file.

watertidy                          \
XYZIN $CCP4_SCR/toxd_wtidy1.pdb \
XYZOUT $CCP4_SCR/toxd_wtidy2.pdb \
DISTOUT $CCP4_SCR/watertidy-3shells.log2 \
<< END-watertidy
TITL Pdb-file including protein and reorganised waters; 2nd shell
SYMM 19
CHNID  J WATOUTID L RANGE 1 59
CHNID  K WATOUTID M RANGE 1 5
WATID  W
occw 0.01
SHELL 2
end
END-watertidy


#!Third H2O shell..

#  Now check if some of the remaining waters are bonded to relabelled L and M; 
#  ie they are forming a third water shell.
#!Run DISTANG again.

distang XYZIN $CCP4_SCR/toxd_wtidy2.pdb \
  << END-distang | tee $CCP4_SCR/watertidy-3shells.log3 
SYMM 19
dist vdw INTRA
RADI   C 1.7 CA 2.2 N 1.7 O 1.7 OW 1.7 S 1.6 P 1.6
FROM ATOM 1 TO 200000     !  See documentation for alternatives
TO ATOM 500 to 200000     !  See documentation for alternatives
END  
END-distang

# Now do this:

watertidy                          \
XYZIN $CCP4_SCR/toxd_wtidy2.pdb \
XYZOUT $CCP4_SCR/toxd_wtidy3.pdb \
DISTOUT $CCP4_SCR/watertidy-3shells.log3 \
<< END-watertidy
TITL Pdb-file with protein and twice reorganised waters; 3rd shell
SYMM 19
CHNID  L WATOUTID N RANGE 1 59
CHNID  M WATOUTID O RANGE 1 5
WATID  W
occw 0.01
SHELL 3
end
END-watertidy
#
