#!/bin/sh
# scala-complete.exam
# 
# A run-all script which runs:
# pointless, reindex, sortmtz, scala, truncate & freerflag
# using some real cubic insulin data (see bug # 3129) with the intention of 
# making use of a wider list of keywords for these programs. The resulting
# reflection file may also be used in a refmac RB refinement example.
#
# Data used comes from SRS 14.2 measured by John Cowan.
#

set -e

# bug # 3192 - run-all examples produce harvest files - well to counteract 
# this here set HARVESTHOME to somewhere in $CCP4_SCR

HARVESTHOME=$CCP4_SCR
export HARVESTHOME

# run pointless - though we know the data are in the correct pg

pointless \
  hklin ${CEXAM}/data/insulin_unmerged.mtz \
  hklout ${CCP4_SCR}/sc-exam-in.mtz << eof
systematicabsences off
eof

if [ "$?" -ne "0" ]; then
  echo "pointless failed"
  exit 1
fi

# run reindex to put the correct spacegroup for insulin - I213 - into 
# the header

reindex \
  hklin ${CCP4_SCR}/sc-exam-in.mtz \
  hklout ${CCP4_SCR}/sc-exam-i213.mtz << eof
symmetry I213
eof

if [ "$?" -ne "0" ]; then
  echo "reindex failed"
  exit 1
fi

# now sort the data 

sortmtz \
  hklin ${CCP4_SCR}/sc-exam-i213.mtz \
  hklout ${CCP4_SCR}/sc-exam-sort.mtz << eof
H K L M/ISYM BATCH
eof

if [ "$?" -ne "0" ]; then
  echo "sortmtz failed"
  exit 1
fi

# and run scala

scala \
  hklin ${CCP4_SCR}/sc-exam-sort.mtz \
  hklout ${CCP4_SCR}/sc-exam-scale.mtz \
  scales ${CCP4_SCR}/sc-exam.scales \
  rogues ${CCP4_SCR}/sc-exam.rogues \
  plot ${CCP4_SCR}/sc-exam.plot \
  rogueplot ${CCP4_SCR}/sc-exam.rogueplot \
  correlplot ${CCP4_SCR}/sc-exam.correlplot \
  normplot ${CCP4_SCR}/sc-exam.normplot << eof
run 1 batch 1 to 45
name run 1 project RUNALL crystal INSULIN dataset NATIVE
scales rotation spacing 5.000000 secondary 6.000000 bfactor on tails
cycles 100
anomalous off
eof

if [ "$?" -ne "0" ]; then
  echo "scala failed"
  exit 1
fi

# run scala again for output unmerged - this is to provide test data for a
# new chef example

scala \
  hklin ${CCP4_SCR}/sc-exam-sort.mtz \
  hklout ${CCP4_SCR}/sc-exam-scale-unmerged.mtz \
  scales ${CCP4_SCR}/sc-exam-unmerged.scales \
  rogues ${CCP4_SCR}/sc-exam.rogues \
  plot ${CCP4_SCR}/sc-exam.plot \
  rogueplot ${CCP4_SCR}/sc-exam.rogueplot \
  correlplot ${CCP4_SCR}/sc-exam.correlplot \
  normplot ${CCP4_SCR}/sc-exam.normplot << eof
run 1 batch 1 to 45
name run 1 project RUNALL crystal INSULIN dataset NATIVE
scales rotation spacing 5.000000 secondary 6.000000 bfactor on tails
cycles 100
anomalous off
output unmerged
onlymerge
restore ${CCP4_SCR}/sc-exam.scales
eof

if [ "$?" -ne "0" ]; then
  echo "scala failed"
  exit 1
fi

# and then truncate

truncate \
  hklin ${CCP4_SCR}/sc-exam-scale.mtz \
  hklout ${CCP4_SCR}/sc-exam-trunc.mtz << eof
eof

if [ "$?" -ne "0" ]; then
  echo "truncate failed"
  exit 1
fi

# and finally add the FreeR flag

freerflag \
  hklin ${CCP4_SCR}/sc-exam-trunc.mtz \
  hklout ${CCP4_SCR}/sc-exam-free.mtz << eof
eof

if [ "$?" -ne "0" ]; then
  echo "freerflag failed"
  exit 1
fi