#!/bin/sh

# 1) Read in coordinates to calculate structure factors. 
#  Use sf_calc to produce $CCP4_SCR/toxd_sf1.mtz
# 2) Read in coordinates to calculate solvent density map.
#  Read in map to calculate "solvent" structure factors
#  in $CCP4_SCR/toxd_sf_solv
# 3) Use icoefl to scale SFs and solvent transform together.
#  No guarantee this will work sensibly; I dont know what to expect..
#  (in fact, the converged parameters don't look v. sensible,
#  but you get the idea).

set -e

#  Read in coordinates to calculate structure factors. 
sfall		\
HKLIN $CEXAM/toxd/toxd                        \
HKLOUT $CCP4_SCR/toxd_sf1		\
XYZIN $CEXAM/toxd/toxd.pdb                        \
<< END-sfall
TITLE Structure factors calculated for toxd.
GRID 152 96 64    !div CELL by these should give .=. 0.7 A
MODE SFCALC XYZIN HKLIN
RESOLUTION 37 2.1 
BINS  60
RSCB 8.0 2.1
LABIN FP=FTOXD3 SIGFP=SIGFTOXD3 -
      F0=FAU20 F1=SIGFAU20    -
      F2=FMM11 F3=SIGFMM11    -
      F4=FI100 F5=SIGFI100    

LABOUT FC=FC_sf PHIC=AC_sf
END 
END-sfall

#  Calculate solvent map from atom coordinates
#  and then generate structure factors from map
#

sfall		\
XYZIN $CEXAM/toxd/toxd.pdb                        \
MAPOUT $CCP4_SCR/toxd_solv \
<< END-sfall
TITLE Calculating "coordinate" map
GRID 152 96 64    !div CELL by these should give .=. 0.7 A
MODE ATMMAP SOLVMAP
RESOLUTION 37 2.1 
BINS  60
RSCB 8.0 2.1
SYMMETRY 19
END 
END-sfall

#  Read in map to calculate "solvent" structure factors. 
sfall		\
HKLIN  $CCP4_SCR/toxd_sf1		\
HKLOUT $CCP4_SCR/toxd_sf_solv		\
MAPIN $CCP4_SCR/toxd_solv \
<< END-sfall
TITLE  calculating structure factors from "Fc" map
GRID 152 96 64    !div CELL by these should give .=. 0.7 A
MODE SFCALC MAPIN HKLIN
RESOLUTION 37 2.1 
BINS  60
RSCB 8.0 2.1
LABIN FP=FTOXD3 SIGFP=SIGFTOXD3
LABOUT ALLIN FC=FC_solv PHIC=AC_solv
END 
END-sfall

#  Scale 

icoefl \
HKLIN $CCP4_SCR/toxd_sf_solv.mtz		\
       hklout $CCP4_SCR/toxd_sf2.mtz  \
       << eof
title Scale Fc(atom) and Fc(solvent) to Fobs
labin FP=FTOXD3 SIGFP=SIGFTOXD3 FC1=FC_sf PHIC1=AC_sf -
      FC2=FC_solv PHIC2=AC_solv
labout  FC=FCALL PHIC=PHICALL
scale FP 1.4
scale FC1 1.0 15.0
scale FC2 1.0 0.0       # default
cycles  20
eof
#
