#!/usr/bin/env python

import re
import os
import os.path
import subprocess
import sys

#VERSION  March 27, 2017

# Run Bachrest as a command
#Synopsis: bachrest.py infile refile outfile source proto prot3 blunt prot5 symm \
#            minsite maxsite fragleast fragmost fragprint
#
#BACHREST is an interactive program, so we first generate a file containing the 
#keystrokes the user would normally type, and then run the program using that file.
 
""" ensure that there are enough command line arguments to parse """
if len(sys.argv) < 15:
    print("Usage: bachrest.py  INFILE  REFILE  OUTFILE  SOURCE  PROTO  PROT3  BLUNT")
    print("         PROT5  SYMM  MINSITE  MAXSITE  FRAGLEAST  FRAGMOST  FRAGPRINT")
    sys.exit();

#Convert arguments to variables
INFILE    = sys.argv[1]
REFILE    = sys.argv[2]
OUTFILE   = sys.argv[3]
SOURCE    = sys.argv[4]
PROTO     = sys.argv[5]
PROT3     = sys.argv[6]
BLUNT     = sys.argv[7]
PROT5     = sys.argv[8]
SYMM      = sys.argv[9]
MINSITE   = int(sys.argv[10])
MAXSITE   = int(sys.argv[11])
FRAGLEAST = int(sys.argv[12])
FRAGMOST  = int(sys.argv[13])
FRAGPRINT = int(sys.argv[14])


TEMPFILE = INFILE + '.' + str(os.getpid())
comfile = open(TEMPFILE,'w')

# Abort if INFILE does not exist or is of zero length
if os.path.exists(INFILE) and os.path.getsize(INFILE) > 1 :

    # a bit of sanity checking
    if (FRAGLEAST > FRAGMOST):
        FRAGLEAST = FRAGMOST
    if (FRAGPRINT > FRAGMOST):
        FRAGPRINT = FRAGMOST
    if (FRAGPRINT < 1):
        FRAGPRINT = 1
    if (MAXSITE > 23):
        MAXSITE = 23
    if (MINSITE > MAXSITE):
        MINSITE = MAXSITE

    # ------------------- Generate a file of commands to be read by BACHREST ----------
    #initialize
    comfile.write(INFILE + '\n')     #input filename
    comfile.write('G\n')               #GenBank
    comfile.write(REFILE + '\n')       #Restriction site file
    comfile.write(OUTFILE + '\n')      #output file

    # Set parameters
    comfile.write('4\n')          #Choose parameter menu
   
    comfile.write('1\n')          #choose source
    comfile.write(SOURCE + '\n')
   
    comfile.write('2\n')          #choose prototype/all
    comfile.write(PROTO + '\n')
 
    comfile.write('3\n')          #set 3' protruding ends
    comfile.write(PROT3 + '\n')
    
    comfile.write('4\n')          #set blunt ends
    comfile.write(BLUNT + '\n')
   
    comfile.write('5\n')          #choose 5' protruding ends
    comfile.write(PROT5 + '\n')
 
    comfile.write('6\n')          #choose symmetric, asymmetric or both
    comfile.write(SYMM + '\n')
    
    comfile.write('7\n')          #choose minsite
    comfile.write(str(MINSITE) + '\n')
   
    comfile.write('8\n')          #choose maxsite
    comfile.write(str(MAXSITE) + '\n')
 
    comfile.write('9\n')          #set FRAGLEAST
    comfile.write(str(FRAGLEAST) + '\n')
    
    comfile.write('10\n')         #set FRAGMOST
    comfile.write(str(FRAGMOST) + '\n')
   
    comfile.write('11\n')         #set FRAGPRINT
    comfile.write(str(FRAGPRINT) + '\n')
      
    comfile.write('0\n')          #exit parameter menu

    comfile.write('5\n')          #Search for sites & write output to file

    comfile.write('0\n')          #exit program

    comfile.close()

    #---------------- Run BACHREST using the command file as input --------------------
    # Yes, I know that we could pipe these lines directly to stdin, but it turns out that if you
    # do that and run this command in the background, there are odd things happening in the background,
    # at least on MacOSX, that prevent the output from popping up in a text editor until the Java VM
    # calling this script is terminated. 
    comfile = open(TEMPFILE,'r')
    p = subprocess.Popen(['bachrest'], stdin=comfile)
    p.wait()
    comfile.close()
    os.remove(TEMPFILE)
    


