#
#
#     Copyright (C) 2014 Ronan Keegan
#
#     This code is distributed under the terms and conditions of the
#     CCP4 Program Suite Licence Agreement as a CCP4 Application.
#     A copy of the CCP4 licence can be obtained by writing to the
#     CCP4 Secretary, Daresbury Laboratory, Warrington WA4 4AD, UK.
#
#
# ======================================================================
# mrSHELXE.tcl --
#
# CCP4Interface 
#
# =======================================================================

############################################################################
proc mrSHELXE_prereq { } {
############################################################################
# Check that Shelx executables are available
  # Shelxe
  if { ![file exists [FindExecutable shelxe]] } {
    return 0
  }
  return 1
}

############################################################################
proc mrSHELXE_setup { typedefVar arrayname } {
############################################################################
  upvar #0 $typedefVar typedef
  upvar #0 $arrayname array
  global env

  return 1
}

############################################################################
proc set_shelxe_flag { arrayname } {
############################################################################
  upvar #0 $arrayname array

  # Check the max resolution of the target data.
  if { $array(AUTOTRACE_AVAILABLE) == 1 && $array(RESOLUTION_MAX) <= 2.4 } {
     set array(SHELXE) 1
     WarningMessage "Data resolution is better than 2.4 Angstroms - SHELXE auto-tracing has been turned on"
  } else {
     set array(SHELXE) 0
  }

  return 1
}

############################################################################
proc autotrace_available_setup {} {
############################################################################
  catch { exec [BinPath shelxe] } shelxe_stdoe
  set shelxe_info [split $shelxe_stdoe]
  if { [set k [lsearch -exact $shelxe_info Version]] > -1 } {
    set shelxe_version [split [lindex $shelxe_info [expr $k + 1]] /]
    if { [llength $shelxe_version] == 2 } {
      set shelxe_year [lindex $shelxe_version 0]
      set shelxe_month [lindex $shelxe_version 1]
      if { [string is integer $shelxe_year] &&  [string is integer $shelxe_year] } {
        if { $shelxe_year > 2009 } {
          return 1
        }
      }
    }
  }
  return 0
}

############################################################################
proc mrSHELXE_run { arrayname } {
############################################################################
  upvar #0 $arrayname array
 
  set array(INPUT_FILES) "HKLIN XYZIN"
  set array(OUTPUT_FILES) "HKLOUT XYZOUT"

  if { [FindExecutable [ string tolower "shelxe" ] ] == "" } {
            # Not found
            WarningMessage "Input Error: SHELXE program not found in system path."
            return 0
     }

  if { $array(SOLVENT) == "" } {
            WarningMessage "Input Error: You must provide a value for the solvent content."
            return 0
     }

  #if { $array(SPACE_GROUP) != $array(SPACE_GROUP_PDB) } {
  #          WarningMessage "Input Error: MTZ ($array(SPACE_GROUP)) and PDB ($array(SPACE_GROUP_PDB)) space group mismatch. Are you sure that the PDB has been used in molecular replacement against this data?"
  #          return 0
  #} else {
  #          return 1
  #}

  if { ($array(CELL_1) == $array(CELL_PDB_1)) && ($array(CELL_2) == $array(CELL_PDB_2)) && ($array(CELL_3) == $array(CELL_PDB_3)) && ($array(CELL_4) == $array(CELL_PDB_4)) && ($array(CELL_5) == $array(CELL_PDB_5)) && ($array(CELL_6) == $array(CELL_PDB_6)) } {
            return 1
  } else {
            WarningMessage "Input Error: MTZ and PDB cell dimensions mismatch. Are you sure that the PDB has been used in molecular replacement against this data?"
            return 0
  }

  return 1
}

############################################################################
proc mrSHELXE_task_window { arrayname } {
############################################################################
  global configure

  upvar #0 $arrayname array

  if { [CreateTaskWindow $arrayname  \
        "C-alpha tracing and density modification from an MR solution using SHELXE" "mrSHELXE" \
               {} ] == 0 } return

  SetProgramHelpFile "mrSHELXE"

  #if { [FindExecutable [ string tolower "fasta34" ] ] == "" } {
  #       if { [FindExecutable [ string tolower "fasta35" ] ] == "" } {
  #          if { [FindExecutable [ string tolower "fasta36" ] ] == "" } {
  #             # Not found
  #             WriteCredits [list "Fasta program not found (fasta34, 35 or 36) "] \
  #               -label "Dependency Warning!" \
  #               -link "Download from: http://www.ebi.ac.uk/fasta/" \
  #                     "http://www.ebi.ac.uk/fasta/"
  #          }
  #       }
  # }

  # Check if autotrace is available in SHELXE
  set array(AUTOTRACE_AVAILABLE) [autotrace_available_setup]

#=PROTOCOL==============================================================

  OpenFolder protocol 

  CreateTitleLine line TITLE 

#=FILES================================================================ 

  OpenFolder file

  OpenSubFrame frame \
      -toggle_display MRBUMP_MODE open [list FULLMR]

  # Input MTZ file
  CreateInputFileLine line \
     "Enter name of the target MTZ file" \
     "MTZ in  " \
     HKLIN DIR_HKLIN \
      -fileout HKLOUT DIR_HKLOUT "_mrSHELXE_soln" \
      -setfileparam space_group_name SPACE_GROUP \
      -setfileparam cell CELL \
      -setfileparam resolution_max RESOLUTION_MAX 

  #CreateLine line \
    message "Space group (default as in MTZ file) (SYMMETRY)" \
    help "symmetry" \
    label "Space group" \
    widget SPACE_GROUP

  #CreateLine line \
    message "Cell dimensions (default from MTZ file) (CELL)" \
    help "cell" \
    label "Cell a" \
    widget CELL_1 -width 8 \
    label "b" \
    widget CELL_2 -width 8 \
    label "c" \
    widget CELL_3 -width 8 \
    label "alpha" \
    widget CELL_4 -width 8 \
    label "beta" \
    widget CELL_5 -width 8 \
    label "gamma" \
    widget CELL_6 -width 8

  # Input PDB file
  CreateInputFileLine line \
     "Enter name of the PDB file (molecular replacement solution)" \
     "PDB in  " \
     XYZIN DIR_XYZIN \
      -fileout XYZOUT DIR_XYZOUT "_mrSHELXE_soln" \
       -setfileparam space_group_name SPACE_GROUP_PDB \
       -setfileparam cell CELL_PDB

  #CreateLine line \
    message "Space group (default as in MTZ file) (SYMMETRY)" \
    help "symmetry" \
    label "Space group" \
    widget SPACE_GROUP_PDB

  #CreateLine line \
    message "Cell dimensions (default from MTZ file) (CELL)" \
    help "cell" \
    label "Cell a" \
    widget CELL_PDB_1 -width 8 \
    label "b" \
    widget CELL_PDB_2 -width 8 \
    label "c" \
    widget CELL_PDB_3 -width 8 \
    label "alpha" \
    widget CELL_PDB_4 -width 8 \
    label "beta" \
    widget CELL_PDB_5 -width 8 \
    label "gamma" \
    widget CELL_PDB_6 -width 8

#  CreateLabinLine line \
#	"Observed amplitude (FP) and obligatory sigma (SIGFP)" \
#	HKLIN "F    " F  [list F F.F_sigF.F] \
#        -sigma "Sigma  " SIGF  [list F F.F_sigF.sigF ]
#
#  CreateLabinLine line \
#      "Choose Free-R flag" \
#      HKLIN "Free-R" FreeR_flag  [list FreeR_flag]

  CreateOutputFileLine line \
      "Output MTZ File for solution" \
      "MTZ out" \
      HKLOUT DIR_HKLOUT

  CreateOutputFileLine line \
      "Output PDB File for solution" \
      "PDB out" \
      XYZOUT DIR_XYZOUT

  CreateLine line \
      message "Solvent content" \
      help solvent \
      label "Solvent content of asu:" \
      widget SOLVENT -width 5 -oblig

  CreateLine line \
    message "Number of SHELXE main-chain autotracing cycles" \
    label "Perform" \
    widget TRACE_NCYCLES \
    label "cycles of main-chain tracing each including" \
    message "Number of density modification cycles in each tracing cycle" \
    widget NCYCLES \
    label "cycles of density modification" \

  CloseSubFrame

}
