#!/usr/bin/python

#Python script to try and setup up am1cm2 to am1bcc charging calculations for our different ligands. Some of this will have to be done manually

import os


#Read data file containing calculations to do
file=open('calclist','r')
text=file.readlines()
file.close()
calclist=[]
#Strip lines beginning with comments and the comments from lines
for line in text:
  if line.find('#')!=0:
    cmt=line.find('#')
    calc=line[:cmt]
    if len(calc)>2:
      calclist.append(calc)


#Now, loop over calculations; set up directories and obtain the starting topology and coordinate files from the val111 directory
for calc in calclist:
  startdir=os.getcwd()
  dirs=calc.split('/')
  #Make sure directories exist
  if not os.path.exists(dirs[0]):
    os.mkdir(dirs[0])
  if not os.path.exists(os.path.join(dirs[0],dirs[1])):
    os.mkdir(os.path.join(dirs[0],dirs[1]))
  #Copy files there; only need coordinates as we will have to re-generate the topology file using our scripts.
  os.system('cp /dmobley1/val111_corr/%s/complex_umbrella.gro %s/complex.gro' % (calc,calc))

  #Make setup directory for this calc, if it does not exist
  calcnum=dirs[1].replace('ori','')
  setupdir='setup'+calcnum
  if not os.path.exists(os.path.join(dirs[0],setupdir)):
    os.mkdir(os.path.join(dirs[0],setupdir))
    #Prompt for ligand file
    print "Please enter full path to ligand .eel1 file for %s/ori%s:" % (dirs[0],calcnum)
    ligeel=raw_input()
    #Error check
    while not os.path.isfile(ligeel):
       print "Error: Invalid ligand .eel1 file path (file not found). Plese re-enter."
       ligeel=raw_input()       
    #Copy file here
    os.system('cp %s %s/lig.eel1' % (ligeel,os.path.join(dirs[0],setupdir)))
   
  #Now go ahead and do the setup for each ligand
  os.chdir(os.path.join(dirs[0],setupdir)) #Change directory to setup directory
  #Generate ligand partial charges and file
  os.system('ligsetup.py -c bcc -f lig.eel1 -o lig')
  #Now run gromacssetup using the presolvated protein structure
  os.system('gromacssetup_presolvated.py -l lig -p /dmobley/docking/gromacs_presolvated/L99A -o complex ')
  os.system('rm #*#')
  #Now I have complex_nochg.top and complex_restr.top; they will work with the complex.gro already here
 
  #Make directories in which to run
  if not os.path.exists('../complex_nochg%s' % calcnum):
     os.mkdir('../complex_nochg%s' % calcnum)
  os.system('cp complex_nochg.top ../complex_nochg%s/.' % calcnum)
  os.system('cp complex.gro ../complex_nochg%s/complex_nochg.gro' % calcnum)
  if not os.path.exists('../complex_restr%s' % calcnum):
     os.mkdir('../complex_restr%s' % calcnum)
  os.system('cp complex.top ../complex_restr%s/.' % calcnum)
  os.system('cp complex.gro ../complex_restr%s/complex_restr.gro' % calcnum)

  #Setup run input files
  os.chdir('../complex_nochg%s' % calcnum)
  os.system('setup_runinput.py -t chg -s prot -n complex_nochg')
  os.system('rm "#*#"')
  os.chdir('../complex_restr%s' % calcnum)
  os.system('setup_runinput.py -t rst -s prot -n complex_restr')
  os.system('rm "#*#"')

  #After this, NEED TO MANUALLY EDIT ALL TOPOLOGIES TO ADD RESTRAINTS 

  #Go back to starting directory
  os.chdir(startdir)
  
