#!/usr/bin/env python
# -*- coding: utf-8

import sys

import anvio
import anvio.terminal as terminal

from anvio.contigops import GenbankToAnvio
from anvio.errors import ConfigError, FilesNPathsError


__author__ = "Developers of anvi'o (see AUTHORS.txt)"
__copyright__ = "Copyleft 2015-2018, the Meren Lab (http://merenlab.org/)"
__credits__ = []
__license__ = "GPL 3.0"
__version__ = anvio.__version__
__maintainer__ = "Mike Lee"
__email__ = "michael.lee0517@gmail.com"
__description__ = ("This script takes a GenBank file, and outputs a FASTA file, as well as two "
                   "additional TAB-delimited output files for external gene calls and gene "
                   "functions that can be used with the programs `anvi-gen-contigs-database` "
                   "and `anvi-import-functions`.")


run = terminal.Run()
progress = terminal.Progress()


if __name__ == '__main__':
    import argparse

    parser = argparse.ArgumentParser(description=__description__)


    groupA = parser.add_argument_group('INPUT', 'Give us the preciousss...')
    groupA.add_argument('-i', '--input-genbank', action='store', required=True, help='Input GenBank file', metavar='GENBANK')

    groupB = parser.add_argument_group('OUTPUT', "You either provide a 'prefix', or provide specific output\
                                                  file names/paths. You can't mix the two (well, you can try).")
    groupB.add_argument(*anvio.A('output-file-prefix'), **anvio.K('output-file-prefix'))
    groupB.add_argument("--output-fasta", help='Output FASTA file path.', metavar='FASTA')
    groupB.add_argument("--output-gene-calls", help='Output file path for external gene calls', metavar='TAB DELIMITED FILE')
    groupB.add_argument("--output-functions", help="Output file path for anvi'o-importable gene functions file", metavar='TAB DELIMITED FILE')

    groupC = parser.add_argument_group('DETAILS', "Setting the annotation source and version data to appear in the output\
                                                   file for functional annotations file.")
    groupC.add_argument('--annotation-source', action='store', default='NCBI_PGAP', help='Annotation \
                             source (default: "NCBI_PGAP")')
    groupC.add_argument("--annotation-version", action="store", default="v4.6", help='Annotation \
                             source version to be stored in the database (default: "v4.6")')

    args = anvio.get_args(parser)

    try:
        genbank_to_anvio = GenbankToAnvio(args)
        genbank_to_anvio.process()
    except ConfigError as e:
        print(e)
        sys.exit(-1)
    except FilesNPathsError as e:
        print(e)
        sys.exit(-2)
