#-------------------------------------------------------------------------------
# Name:        module1
# Purpose:
#
# Author:      oeffner
#
# Created:     26/06/2012
# Copyright:   (c) oeffner 2012
# Licence:     <your licence>
#-------------------------------------------------------------------------------
#!/usr/bin/env python

from libtbx import easy_run
import libtbx.load_env
import os, sys, zipfile
import shutil


# each element in the list is a phil input with corresponding files in the compressed find_alt_orig_sym_mate_data folder
# each element is converted to a text file that is then used as input for phenix.find_alt_orig_sym_mate
tests = [\
r"""
AltOrigSymMates.moving_pdb="2z0d.pdb"
AltOrigSymMates.fixed_pdb="Strider{3ECI_A0,2P82_A0}3ECI_A0,2P82_A0.1.pdb"
AltOrigSymMates.verbose = True
AltOrigSymMates.debug = True
"""
,
r"""
AltOrigSymMates.fixed.mrsolution
{
  solfname = "Strider{3ECI_A0,2P82_A0}3ECI_A0,2P82_A0.sol"
  ensembles
  {
    name = "MR_2P82_A0"
    xyzfname = "sculpt_2P82_A0.pdb"
  }
  ensembles
  {
    name = "MR_3ECI_A0"
    xyzfname = "sculpt_3ECI_A0.pdb"
  }
}

AltOrigSymMates.moving_pdb="2z0d.pdb"
AltOrigSymMates.verbose = True
"""
,
r"""
AltOrigSymMates.fixed.pickle_solution
{
  philfname = "phaser_mr_13.eff"
  pklfname = "phaser_mr_13.pkl"
}

AltOrigSymMates.moving.pickle_solution
{
  philfname = "phaser_mr_11.eff"
  pklfname = "phaser_mr_11.pkl"
}

#AltOrigSymMates.debug = True
AltOrigSymMates.verbose = True
"""
,
r"""
AltOrigSymMates.fixed.mrsolution
{
  solfname = "Strider{3ECI_A0,2P82_A0}3ECI_A0,2P82_A0.sol"
  ensembles
  {
    name = "MR_2P82_A0"
    xyzfname = "sculpt_2P82_A0.pdb"
  }
  ensembles
  {
    name = "MR_3ECI_A0"
    xyzfname = "sculpt_3ECI_A0.pdb"
  }
}

AltOrigSymMates.moving.mrsolution
{
  solfname = "Strider{2ZPN_A0,2P82_A0}2ZPN_A0,2P82_A0.sol"
  ensembles
  {
    name = "MR_2P82_A0"
    xyzfname = "sculpt_2P82_A0.pdb"
  }
  ensembles
  {
    name = "MR_2ZPN_A0"
    xyzfname = "sculpt_2ZPN_A0.pdb"
  }
}

AltOrigSymMates.spacegroupfname = "2Z0D.mtz"
AltOrigSymMates.verbose = True
"""
,
r"""
AltOrigSymMates.fixed.pickle_solution
{
  philfname = "phaser_mr_13.eff"
  pklfname = "phaser_mr_13.pkl"
}

AltOrigSymMates.moving_pdb = "MR.1.pdb"
AltOrigSymMates.verbose = True
"""
,
r"""
AltOrigSymMates.moving_pdb="P05_MR.1.pdb"
AltOrigSymMates.fixed_pdb="3sv2.pdb"
AltOrigSymMates.verbose = True
"""
,
r"""
AltOrigSymMates.moving_pdb="1se8.pdb"
AltOrigSymMates.fixed_pdb="Strider{1Z9F_A}1Z9F_A.1.pdb"
AltOrigSymMates.verbose = True
"""
,
r"""
AltOrigSymMates.moving_pdb="1tfx.pdb"
AltOrigSymMates.fixed_pdb="3RP2_A_1DTX_A.1.pdb"
AltOrigSymMates.movehetatms = True
"""
,
r"""
AltOrigSymMates.moving_pdb="1tfx.pdb"
AltOrigSymMates.fixed_pdb="Ellie1BBR_H_3D65_I.pdb"
AltOrigSymMates.verbose = True
"""
,
r"""
AltOrigSymMates.moving_pdb="1g9v.pdb"
AltOrigSymMates.fixed_pdb="RQ3_MR.1.pdb"
AltOrigSymMates.movehetatms = True
"""
,
r"""
AltOrigSymMates.moving_pdb="moving.pdb"
AltOrigSymMates.fixed_pdb="hetatm_among_atoms.pdb"
AltOrigSymMates.movehetatms = True
"""
]


def run_command(cmd):
    print "*"*len(cmd)
    print cmd
    print "*"*len(cmd)
    sys.stdout.flush()
    ret =  easy_run.call(command=cmd)
    return ret


if __name__ == '__main__':
    print " To retain output files use the command line argument 'dirty'"
    cleanup = True
    if len(sys.argv) > 1 and sys.argv[1] == "dirty":
        cleanup = False

    testdir = "find_alt_orig_sym_mate_data"
    zip_file = os.path.join(libtbx.env.under_dist("phenix_regression", "phaser"),
      "find_alt_orig_sym_mate_data.zip")
    datazip = zipfile.ZipFile(zip_file, "r")
    datazip.extractall()
    os.chdir(testdir)

    for i,test in enumerate(tests):
# run each of the phil input from the list
        fname = "testphil%d.txt" %i
        fh = open(fname,"w")
        fh.write(test)
        fh.close()
        assert(run_command("phenix.find_alt_orig_sym_mate %s" %fname) == 0)

    os.chdir("..")
# now clean up
    if cleanup:
        shutil.rmtree(testdir)

