#!/usr/bin/env python3
"""
maxalign.py - Python wrapper script to run maxalign.pl from BioLegato.

 Synopsis:
   
        maxalign.py infile [options]
   

@modified: July 5 2023
@author: Brian Fristensky
@contact: brian.fristensky@umanitoba.ca
"""

import argparse
import os
import shutil
import subprocess
import sys


PROGRAM = "maxalign.py: "
USAGE = "\n\t USAGE: maxalign.py [--remgaps] [--heuristic <1|2|3>] [--charset '<characters>'] [--seqtype <pro|nuc>] infile "

DEBUG = True

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Wrapper class for command line parameters"
class Parameters:

    def __init__(self):
        """
                Initializes arguments:
                Then calls read_args() to fill in their values from command line
        """

        self.IFN = "" 
        self.SEQTYPE = "pro"
        self.REMGAPS = False
        self.HEURISTIC = 0
        self.CHARSET = ""
        self.read_args()

    def read_args(self):
        """
            Read command line arguments into a Paramters object
        """
        parser = argparse.ArgumentParser()
        parser.add_argument("infile", action="store", default="", help="input file")
        parser.add_argument("--remgaps", action="store_true", default=False, help="-a option in maxalign.pl")
        parser.add_argument("--heuristic", action="store", default="0", help="-h option in maxalign.pl")
        # Note: -h would conflict with the Python --help option
        parser.add_argument("--charset", action="store", default="", help="-c option in maxalign.pl")
        parser.add_argument("--seqtype", action="store", default="pro", help="pro or nuc")

        try:
            args = parser.parse_args()           
            self.IFN = args.infile
            self.REMGAPS = args.remgaps
            self.HEURISTIC = int(args.heuristic)
            self.CHARSET = args.charset
            self.SEQTYPE = args.seqtype
        except ValueError:
            print(USAGE)

        if DEBUG :
            print("IFN: " + self.IFN)
            print("REMGAPS: " + str(self.REMGAPS))
            print("HEURISTIC: " + str(self.HEURISTIC))
            print("CHARSET: " + self.CHARSET)
            print("SEQTYPE: " + self.SEQTYPE)

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Run maxalign"
def Runmaxalign(P):

        COMMAND=["maxalign.pl"]
        if P.REMGAPS :
            COMMAND.append("-a") # remove columns in which all seqs. have gaps
        if P.HEURISTIC > 0 :
            COMMAND.append("-h=" + str(P.HEURISTIC))
        if P.CHARSET != "" :
            COMMAND.append("-c=" + P.CHARSET) # Character set for amino acides or nucleotides
        COMMAND.append("-d") #detailed report
        COMMAND.append(P.IFN)
        print(COMMAND)
        p = subprocess.Popen(COMMAND)
        p.wait()

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def RunBioLegato(DESTINATION, FN):
    """
    Run the blnfetch or blpfetch in the background and 
    remove the temporary file when done
    """
    COMMAND = '(nohup ' + DESTINATION + ' ' +  FN + '; rm -f ' + FN + ' > /dev/null)&'
    os.system(COMMAND)
           	
    return

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def RunTextedit(OFN,DELETEFILE):
    """
    Run the texteditor in the background and
    remove the temporary file when done
    """
    if DELETEFILE :
        p = subprocess.Popen(['chooseviewer.py', OFN, '--delete'])
    else :
        p = subprocess.Popen(['chooseviewer.py', OFN])
    #if DELETEFILE :
    #    p = subprocess.Popen(['$BL_TEXTEDIT', OFN, '; rm -f ', OFN ])
    #else :
    #    p = subprocess.Popen(['$BL_TEXTEDIT', OFN])
    #p.wait()
   	
    return


#======================== MAIN PROCEDURE ==========================
def main():
    """
        Called when not in documentation mode.
        """

    P = Parameters ()

    # Make a temporary directory in which to run the program
    STARTDIR = os.getcwd()
    TEMPDIR  = 'maxalign.' + str(os.getpid())
    os.mkdir(TEMPDIR)
    shutil.copyfile(P.IFN, os.path.join(TEMPDIR, P.IFN))
    # From here on, everything is done in TEMPDIR
    os.chdir(TEMPDIR)

    Runmaxalign(P) 
    ALIGNMENTFILE = os.path.join(STARTDIR, P.IFN + ".fsa") 
    shutil.copyfile("heuristic.fsa", ALIGNMENTFILE) 
    REPORTFILE = os.path.join(STARTDIR, P.IFN + ".report.txt")
    shutil.copyfile("report.txt", REPORTFILE) 
    EXCLUDEDFILE = os.path.join(STARTDIR, P.IFN + ".excluded.txt")
    shutil.copyfile("heuristic_exclude_headers.txt", EXCLUDEDFILE) 
    RETAINEDFILE = os.path.join(STARTDIR, P.IFN + ".retained.txt")
    shutil.copyfile("heuristic_include_headers.txt", RETAINEDFILE) 

    os.chdir(STARTDIR)
    #shutil.rmtree(TEMPDIR)

    if P.SEQTYPE == "pro" :
        RunBioLegato("blpalign",ALIGNMENTFILE) 
    else :
        RunBioLegato("blnalign",ALIGNMENTFILE)
    RunTextedit(REPORTFILE,True)
    RunTextedit(EXCLUDEDFILE,True)
    RunTextedit(RETAINEDFILE,True)

if ("-test" in sys.argv):
    pass
else:
    main()
