#!/usr/bin/env python

import os

template = """
#!/bin/bash
prep phenix
cd {path}
cp ../input.mtz .
cp ../input.pdb .
phenix.ensemble_refinement input.pdb input.mtz \\
      tls_group_selections="chain A" \\
      ptls={pTLS} \\
      tx={tx} \\
      wxray_coupled_tbath_offset={wxray} \\
>& log-{pTLS}-{wxray}-{tx}.log
"""

pTLS_all  = (0.6, 0.7, 0.8, 0.9)
wxray_all = (2.5, 5.0, 7.5)
tx_all    = (0.25, 0.75, 1.00)

out_all = "all.sh"
open(out_all, "w").write("#!/bin/bash\n")
os.system("chmod +x {}".format(out_all))

for pTLS in pTLS_all:
	for wxray in wxray_all:
		for tx in tx_all:
			output_dir = "TLS-{}-WX-{}-TX-{}".format(pTLS,wxray,tx)
			if not os.path.exists(output_dir):
				os.mkdir(output_dir)
			print output_dir
			output_file = os.path.join(output_dir,"cmd.sh")
			open(output_file, "w").write(template.format(pTLS=pTLS,wxray=wxray,tx=tx,path=os.path.abspath(output_dir)))
			os.system("chmod +x {}".format(output_file))
			open(out_all, "a").write("./{}\n".format(output_file))
