import os
import subprocess
import numpy as np
import Bio
import random
import string
import argparse


def get_args(): 
    parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument("ProgramPath", type=str, help="Path of the executable MAINMAST program")
    parser.add_argument("MAP", type=str, help="Input EM map")
    parser.add_argument('--Contour', type=float, dest='Cont', default='1.0', help='Map Contour Level')
    args = parser.parse_args()
    return args


def MakeSub(pg,map,d,f,e,r,i):
	outfile='SUB_t{:f}_f{:f}_d{:f}_r{:f}_N{:d}.sub'.format(d,f,e,r,i)
	lines='''
#!/bin/sh -l
#SBATCH --nodes=1
#SBATCH --time=24:00:00

{:s} -m {:s} -t {:f} -filter {:f} -Dkeep {:f} -Rlocal {:f} -Nround {:d} > OUT_t{:f}_f{:f}_d{:f}_r{:f}_N{:d}.pdb\n
'''.format(pg,map,d,f,e,r,i,d,f,e,r,i)
	with open(outfile,"w") as f:
		f.write(lines)
		


def main():
    args = get_args()
    MAP=args.MAP
    PG=args.ProgramPath
    
    if not os.path.isfile(MAP):
        print('##Missing MAP file:',MAP)
        return 0
    if not os.path.isfile(PG):
        print('##Missing Executable MAINMAST:',PG)
        return 0

    #cmd=['mkdir','-p',OutPath]
    #res=subprocess.run(cmd,stdout=subprocess.PIPE,encoding='utf-8')
	#Parameters
    
    density_th=[0.25*args.Cont,0.5*args.Cont,args.Cont]
    filter_th=[0.0,0.1,0.2,0.3]
    edge_th=[0.5,1.0,1.5]
    radius_th=[5.0,7.5,10.0]
    Iter_th=[5000]
    for dens in density_th:
    	for filter in filter_th:
    		for edge in edge_th:
    		    for rad in radius_th:
    			    for iter in Iter_th:
    				    MakeSub(PG,MAP,dens,filter,edge,rad,iter)

    
if __name__ == '__main__':
    main()