#!/usr/bin/env python3

import os
import os.path
import subprocess
import sys

#Version May 23, 2023
# Run P1HOM, P2HOM as a command
#Synopsis: phom.csh seqfilex seqfiley outfile startx finishx starty finishy\
#                    range minper compfact linesize program

""" ensure that there are enough command line arguments to parse """
if len(sys.argv) < 13:
    print("Usage: phom.py  SEQFILEX  SEQFILEY  OUTFILE  STARTX  FINISHX  STARTY  FINISHY")
    print("         RANGE  MINPER  COMPFACT  LINESIZE  PROGRAM")
    exit();


#Convert arguments to variables
SEQFILEX = sys.argv[1]
SEQFILEY = sys.argv[2]
OUTFILE  = sys.argv[3]
STARTX   = int(sys.argv[4])
FINISHX  = int(sys.argv[5])
STARTY   = int(sys.argv[6])
FINISHY  = int(sys.argv[7])
RANGE    = int(sys.argv[8])
MINPER   = int(sys.argv[9])
COMPFACT = int(sys.argv[10])
LINESIZE = int(sys.argv[11])
PROGRAM  = sys.argv[12]

CFN = 'phom.py' + '.' + str(os.getpid())

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

    # Determine the size of seq. in seqfilex  and seqfiley and make sure that
    # start and finish values aren't greater than seq. length.
    # The following line was broken by Fedora5, so we'll use grep instead.
    #tail +2 $SEQFILEX.$PID > TEMP.$PID
    #grep -v '>' < $SEQFILEX.$PID > TEMP.$PID
    #@ SEQLENGTHX = (`wc -c < TEMP.$PID` - `wc -l < TEMP.$PID`) - 1
    SEQLENGTHX = 0
    h_INFILE = open(SEQFILEX, 'r')
    for line in h_INFILE:
        if not line.startswith('>'):
            SEQLENGTHX += len(line) - 1
    h_INFILE.close()
    SEQLENGTHX -= 1

    print('SEQLENGTHX: ' + str(SEQLENGTHX))

    if (STARTX > SEQLENGTHX):
        STARTX = SEQLENGTHX
    if (FINISHX > SEQLENGTHX):
        FINISHX = SEQLENGTHX


    #tail +2 $SEQFILEY.$PID > TEMP.$PID
    #grep -v '>' < $SEQFILEY.$PID > TEMP.$PID
    #@ SEQLENGTHY = (`wc -c < TEMP.$PID` - `wc -l < TEMP.$PID`) - 1
    SEQLENGTHY = 0
    h_INFILE = open(SEQFILEY, 'r')
    for line in h_INFILE:
        if not line.startswith('>'):
            SEQLENGTHY += len(line) - 1
    h_INFILE.close()
    SEQLENGTHY -= 1

    print('SEQLENGTHY: ' + str(SEQLENGTHY))

    if (STARTY > SEQLENGTHY):
        STARTY = SEQLENGTHY
    if (FINISHY > SEQLENGTHY):
        FINISHY = SEQLENGTHY



    # Open files

    comfile = open(CFN, 'w')
    comfile.write(SEQFILEX + '\n')
    comfile.write('f\n')
    comfile.write(SEQFILEY + '\n')
    comfile.write('f\n')
    comfile.write(OUTFILE + '\n')
    # HACK!! For reasons unclear, in Fedora 31 the C access function seems to
    # return true when checking whether a file exists, even if it doesn't exist.
    # This means tha P1HOM/P2HOM will write a prompt asking if the user wants to overwrite
    # an existing file. As a workaround, we need to include an answer of Y to
    # respond to that prompt.
    comfile.write('Y' + '\n') #outfile 

    # Set parameters
    comfile.write('4\n') #Choose parameter menu

    comfile.write('1\n')     #choose startx
    comfile.write(str(STARTX) + '\n')

    comfile.write('2\n')      #choose finishx
    comfile.write(str(FINISHX) + '\n')

    comfile.write('3\n')      #choose startx
    comfile.write(str(STARTY) + '\n')

    comfile.write('4\n')      #choose finishx
    comfile.write(str(FINISHY) + '\n')

    comfile.write('5\n')      #choose range
    comfile.write(str(RANGE) + '\n')

    comfile.write('7\n')      #choose minper
    comfile.write(str(MINPER) + '\n')

    comfile.write('8\n')      #choose compfact
    comfile.write(str(COMPFACT) + '\n')

    comfile.write('9\n')     #choose linesize
    comfile.write(str(LINESIZE) + '\n')

    comfile.write('0\n')  #exit parameter menu

    comfile.write('6\n') #write output to file

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


    # run p1hom or p2hom and then clean up temporary files
    comfile = open(CFN, 'r')
    p = subprocess.Popen([PROGRAM], stdin=comfile)
    p.wait()
    comfile.close()
    os.remove(CFN)


