#!/usr/bin/env python3 -u
# https://stackoverflow.com/a/27534908/4586180
# -u prevents python from buffering stdout. by using -u you can use the 'tee' command

#from EMBL_EBI_converter import MSF2VCFImplementation
#from msa2vcf import MSA2VCFImplementation as MSAVCFI
from msa2vcf import MSA2VCFImplementation as MSA2VCF


'''
Created on Oct 9, 2019

@author: Andrew Davidson (aedavids@ucsc.edu)
'''

import os
import sys


from setupLogging import setupLogging
logConfigFilePath = setupLogging("logging.ini.json")

import argparse
import logging

def main():
    logger = logging.getLogger(__name__)
    logger.info("using logging config file:{}".format(logConfigFilePath))

    description = "Converts a multiple sequence alignment file in the format described at " \
                 + "https://www.ebi.ac.uk/ipd/imgt/hla/nomenclature/index.html to VCF." \
                + " See logging.ini.json to change log level"
    parser = argparse.ArgumentParser(description=description,    
                                     usage="inputFile outputFile [options] aedwip")

    parser.add_argument('--version', action='version', version='%(prog)s 1.0')

    parser.add_argument("inputFile", type=str,
                        help="The file in MSF format.")
    parser.add_argument("outputFile", type=str,
                        help="The VCF version of the file")
    

    # if no arguments print help
    if len(sys.argv)==1:
        parser.print_help(sys.stderr)
        sys.exit(1)
    
    options = parser.parse_args()  
    
    # make sure input file exists and output does not  
    inputFilePath = options.inputFile
    if not os.path.isfile(inputFilePath) :
        logger.error("not found inputFilePath: {}".format(inputFilePath))
        return 1
    
    # make sure output file does not exist
    outputFilePath = options.outputFile
    if os.path.isfile(outputFilePath) :
        logger.error("outputFilePath can not exist: {}".format(outputFilePath))
        return 1  
    
    msa2vcf = MSA2VCF.MSA2VCFImplementation()  
    msa2vcf.parse(inputFilePath, outputFilePath)
    
    return 0

if __name__ == '__main__':
    main()