#!/usr/bin/env python3

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

#Version Jan. 11, 2020
# Run TESTCODE as a command
#Synopsis: testcode.py infile outfile start finish which format window skip

#Convert arguments to variables
INFILE  = sys.argv[1]
OUTFILE = sys.argv[2]
START   = int(sys.argv[3])
FINISH  = int(sys.argv[4])
WHICH   = sys.argv[5]
FORMAT  = sys.argv[6]
WINDOW  = int(sys.argv[7])
SKIP    = int(sys.argv[8])

PID = str(os.getpid()) #process id

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

    # Correct for bug in GDE2.2, which writes 'Circular' in wrong columns
    # in GenBank LOCUS line. This is done by determining whether seq is
    # linear or circular, converting to Pearson format, and appending the
    # '1' if linear or '2' if circular.
    file = open(INFILE, 'r')
    if (re.search('[Cc]ircular', file.readline())):
        TOPOLOGY = 1
    else:
        TOPOLOGY = 2
    file.close()

    TEMPFILE = INFILE + "." + PID

    subprocess.call(['readseq', '-i1', '-f8', '-o=' + TEMPFILE, INFILE])

    temphandle = open (TEMPFILE, 'a')
    temphandle.write(str(TOPOLOGY) + '\n')
    temphandle.close()
   
    # Determine the size of seq. ininfile and make sure that
    # start and finish values aren't greater than seq. length.
    #tail +2 $TEMPFILE > TEMP.$PID
    # We can no longer use tail +n because the syntax is no longer consistent
    # between Unix and Linux

    # Determine the size of seq. in infile and make sure that
    # start and finish values aren't greater than seq. length.
    temphandle = open (TEMPFILE, 'r')
    SEQLENGTH = 0
    for line in temphandle:
        if not line.startswith(">"):
            SEQLENGTH += len(line) - 1
    SEQLENGTH -= 1
    temphandle.close()

    if START > SEQLENGTH:
        START = SEQLENGTH
    if FINISH > SEQLENGTH:
        FINISH = SEQLENGTH
    MAXCODONS = (SEQLENGTH / 3)

    if WINDOW > MAXCODONS:
        WINDOW = MAXCODONS
    if SKIP > MAXCODONS:
        SKIP = MAXCODONS


    # Create a command file to be read as input by TESTCODE
    CFN = 'testcode.py.com' + '.' + PID
    comfile = open(CFN,'w')
    NL = '\n'
    
    #initial filenames
    comfile.write(TEMPFILE + NL)         #input filename
    comfile.write("f" + NL)                   #Pearson/Fasta format
    comfile.write(OUTFILE + NL)               #outfile

    # Set parameters
    comfile.write("4" + NL)                   #Choose parameter menu

    comfile.write("1" + NL)                   #choose start
    comfile.write(str(START)  + NL)

    comfile.write("2"  + NL)                  #choose finish
    comfile.write(str(FINISH) + NL)

    comfile.write("3" + NL)                   #choose which
    comfile.write(WHICH + NL)

    comfile.write("4" + NL)                   #choose format
    comfile.write(FORMAT + NL)

    comfile.write("5" + NL)                   #choose window
    comfile.write(str(WINDOW) + NL)

    comfile.write("6" + NL)                   #choose skip
    comfile.write(str(SKIP) + NL)

    comfile.write("0" + NL)                   #exit parameter menu

    comfile.write("6" + NL)                   #Print output to file
    comfile.write("" + NL)                    #dummy prompt line
    comfile.write("0" + NL)                   #exit program
    comfile.close()
    comfile = open(CFN,'r')
    proc = subprocess.Popen(["testcode"], stdin=comfile)
    proc.wait()
    
    os.remove(TEMPFILE)
    os.remove(CFN)
