#!/usr/bin/env python3

import os
import subprocess
import sys

""" ensure that there are enough command line arguments to parse """
if len(sys.argv) < 3:
    print("Usage: free2fasta.py  INFILE  OUTFILE")
    exit();

#Version Jan. 11, 2020
# Convert free format file to pseudo GenBank format
# to be read by GDE.
#Synopsis: free2gb.py infile outfile
#Convert arguments to variables
INFILE  = sys.argv[1]
OUTFILE = sys.argv[2]

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

    # Create a command file to use as input for funnel
    PID = str(os.getpid()) #process id
    CFN = 'free2fasta.py' + '.' + PID
    comfile = open(CFN,'w')    
    comfile.write(INFILE + '\n')        #input filename
    comfile.write(PID + '.raw\n')       #outfile
    # 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 FUNNEL 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  
    comfile.write('50\n')               # print 50 nt per line
    comfile.close()

    #run funnel to delete non-sequence characters
    comfile = open(CFN,'r')
    p = subprocess.Popen(['funnel'], stdin=comfile)
    p.wait()
    comfile.close()
    os.remove(CFN)

    # $INFILE could be a fully qualified path, so we don't want to use
    # that as the sequence name. Get rid of the path and just
    # keep the file name.
    # get rid of the file extension, if any
    SEQNAME = os.path.splitext(os.path.basename(INFILE))[0]
    h_fastaout = open(OUTFILE, 'w')

    # Create a Fasta format file for input to readseq.
    h_fastaout.write('>' + SEQNAME + '\n')

    # copy any non-comment lines to end of fasta file
    h_raw = open(PID + '.raw', 'r')
    for line in h_raw:
        sc_index = line.find(';')
        if sc_index == 0:
            line = ""
        elif sc_index > 0:
            line = line[0:sc_index]
        h_fastaout.write(line)
    h_raw.close()

    # delete temporary files
    os.remove(PID + '.raw')

