#!/bin/bash
########################################################
# bash script for converting pdb 
# Stefan Henrich		
version="22.03.2005"
########################################################

        pdb="1gi5"                      #pdbcode
 ligandname="A13"                       #ligandname max. 3 letters

#############################################################
# reading arguments from command line
until [ -z "$1" ]; do
	case $1 in
		--help|-h )
			echo bash script for converting pdb
			echo options:
			echo "-h    help"
			echo "-vers version"
			echo "-d    directory"
			echo "-pdb  pdb code"
			echo "-n    ligandname (3 characters)"
			echo "-g    get pdb file from PDB"
			echo "-no   no nedit"
			exit 0
		;;
		--version|-vers )
			echo $version
			exit 0
		;;
		--environment|-e )
			# set to 1 for later testing
			shift
			echo "--environment option doesn't work"
			exit 0
		;;
		--directory|-d )
			shift
			directory=$1
			echo directory $directory
		;;
		--pdb|-pdb )
			shift
			pdb=$1
		;;
		--ligandname|-n )
			shift
			ligandname=$1
		;;
		--getpdb|-g )
			getpdb="locatepdb"
			locatepdb $pdb > $directory$pdb".pdb"
			echo
		;;
		--noedit|-no )
			noedit="yes"
		;;
		-* )
			echo "Unrecognized option: $1"
			exit 1
		;;
		* ) 
			echo no command is given
		;;
	esac 

	shift

	if [ "$#" = "0" ]; then
		break  
	fi
done 

# INPUT ################################################
    pdbfile=$pdb".pdb"    #pdbfile
	echo $pdbfile
# OUTPUT ###############################################
#directory=${directory:="./"}	#"./"$pdb"/"        #directory for output
    protein=$directory$pdb".modif.pdb"  #protein, Cl-, Ca2+
     ligand=$directory$pdb".ligand.pdb" #ligand, SO4
 convertlog=$directory$pdb".log.tmp"    #logfile 
      pymol=$directory$pdb".pml"        #pymol script 
	  directory="./"
##### Directory for Programs ###########################
     prgdir="/home/henricsn/Combine/tassfun/programs/" #directory for programs
      eddir="/scratch/mcm/henricsn/ED/" #directory for electron density
########################################################
       temp=""

################################################################

echo
echo Script for converting pdb-files
echo -------------------------------

#[ -d "$directory" ] || mkdir $directory 


################################################################
### check for reserved residue names
################################################################
if [ "$ligandname" = "ALA" -o "$ligandname" = "ARG" -o "$ligandname" = "ASN" -o "$ligandname" = "ASH" -o "$ligandname" = "CYS" ]; then 
	echo wrong ligandname
	exit 0
fi
if [ "$ligandname" = "CYX" -o "$ligandname" = "GLU" -o "$ligandname" = "GLH" -o "$ligandname" = "GLN" -o "$ligandname" = "GLY" ]; then 
	echo wrong ligandname
	exit 0
fi
if [ "$ligandname" = "HID" -o "$ligandname" = "HIE" -o "$ligandname" = "HIP" -o "$ligandname" = "ILE" -o "$ligandname" = "LEU" ]; then 
	echo wrong ligandname
	exit 0
fi
if [ "$ligandname" = "LYS" -o "$ligandname" = "MET" -o "$ligandname" = "PHE" -o "$ligandname" = "PRO" -o "$ligandname" = "SER" ]; then 
	echo wrong ligandname
	exit 0
fi
if [ "$ligandname" = "THR" -o "$ligandname" = "TRP" -o "$ligandname" = "TYR" -o "$ligandname" = "VAL" -o "$ligandname" = "CYM" ]; then 
	echo wrong ligandname
	exit 0
fi
if [ "$ligandname" = "ACE" -o "$ligandname" = "NHE" -o "$ligandname" = "NME" ]; then 
	echo wrong ligandname
	exit 0
fi

if [ "$pdb" = "$temp" ]; then 
	echo name is missing
	exit 0
else 
	echo pdb:       $pdb    
	echo pdbfile:   $pdbfile   
	echo protein:   $protein
	echo ligand:    $ligand
	echo log:       $convertlog
	echo ligandname:$ligandname
#	echo Datum: $(date +%d.%m.%Y)
################################################################
### start pdbconv
echo	$prgdir"pdbconv" $pdbfile $protein $ligand $convertlog $ligandname
	$prgdir"pdbconv" $pdbfile $protein $ligand $convertlog $ligandname
fi

################################################################
### open generated files in text editor
#mv $pdbfile $directory
if [ "$noedit" != "yes" ]; then 
	nedit $pdbfile $protein $ligand $convertlog&
fi #noedit

################################################################
### get residue number of ligand for pymol script
grep "C1  $ligandname" $ligand > grep.tmp
ligandnumb=$(awk '{ print $5}' grep.tmp) 
rm -f grep.tmp

################################################################
##### write script for pymol
echo load $pdbfile, $pdb > $pymol
echo load $eddir$pdb.ccp4,map1 >> $pymol
echo load $eddir$pdb"_diff".ccp4,map2 >> $pymol
echo isomesh msh0,map1,1.0,\(resid 1:999\),0.0,0 >> $pymol
echo color blue,msh0 >> $pymol
echo isomesh msh1,map2,2.5,\(resid 1:999\),0.0,0 >> $pymol
echo color green,msh1 >> $pymol
echo isomesh msh2,map2,-2.5,\(resid 1:999\),0.0,0 >> $pymol
echo color red,msh2 >> $pymol
echo cmd.disable\(\'msh0\'\) >> $pymol
echo cmd.disable\(\'msh1\'\) >> $pymol
echo cmd.disable\(\'msh2\'\) >> $pymol
echo sele cit, \(r\;CIT\) >> $pymol
echo sele wat, \(r\;HOH\) >> $pymol
echo stereo on >> $pymol
echo center resi $ligandnumb >> $pymol
#echo "cmd.label('''(name ca+C1*+C1' and (byres($pdb)))''','''"\""%s-%s"\""%(resn,resi)''')" >> $pymol 
################################################################

