#!/usr/bin/env python3

import os
import subprocess
import sys

# xylem_shuffle.py                  
# This is a front end for xylem_shuffle. 

INFILE  = str(sys.argv[1])
WINDOW  = str(sys.argv[2])
OVERLAP = str(sys.argv[3])
OUTFILE = str(sys.argv[4])
BIRCH_PLATFORM = os.environ.get('BIRCH_PLATFORM')


if BIRCH_PLATFORM == 'osx-x86_64':

    INFILE_H = open(INFILE, 'r')
    lines_in = INFILE_H.readlines()
    INFILE_H.close()

    #Create a temporary input file to be read by shuffle
    PID = str(os.getpid())
    TIFN=  'xylem_shuffle.py.in' + '.' + PID
    tempin = open(TIFN,'w')
    tempin.writelines(lines_in)
    tempin.close
    TOFN = 'xylem_shuffle.py.out' + '.' + PID  

    # run shuffle, and delete the first 2 lines, which are message lines
    # that might confuse programs that read fasta files.
    SEED = randint(1,32767)
    tempin = open(TIFN,'r')
    tempout = open(TOFN,'w')
    p = subprocess.Popen(['shuffle', '-s' + str(SEED), '-w' + WINDOW, '-o' + OVERLAP], stdin=tempin, stdout=tempout)
    p.wait()
    tempin.close()
    tempout.close()
    tempout = open(TOFN,'r')
    lines_out = tempout.readlines()
    tempout.close()
    lines_out = lines_out[2:]


    # Truncate the name lines after the first blank and
    # add "-rand" to each name o indicate that the sequences
    # have been randomized.
    # Convert the output into GDE flat file format, using the
    # flag character that was found earlier to indicate either DNA or protein
    OUTFILE_H = open(OUTFILE, 'w')
    for line in lines_out:
        if line[0] == '>' :
           line = line.replace('>',SFLAG)
           line = line.split()[0] + '-rand\n'
        OUTFILE_H.write(line)
    OUTFILE_H.close()

    os.remove(TIFN)
    os.remove(TOFN)
    
else:
    tempin = open(INFILE,'r')
    tempout = open(OUTFILE,'w')
    p = subprocess.Popen(['xylem_shuffle','-w' + WINDOW, '-o' + OVERLAP], stdin=tempin, stdout=tempout)
    p.wait()
    tempin.close()
    tempout.close()
