#!/opt/anaconda1anaconda2anaconda3/bin/python
# -*- coding: utf-8

import sys

import anvio
import anvio.dbops as dbops
import anvio.terminal as terminal

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__ = "A. Murat Eren"
__email__ = "a.murat.eren@gmail.com"
__requires__ = ['contigs-fasta']
__provides__ = ['contigs-db']


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


if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser(description='Generate a new anvio contigs database.')

    groupA = parser.add_argument_group('MANDATORY INPUTS', 'Things you really need to provide to be in business.')
    groupA.add_argument(*anvio.A('contigs-fasta'), **anvio.K('contigs-fasta'))
    groupA.add_argument(*anvio.A('project-name'), **anvio.K('project-name'))

    groupB = parser.add_argument_group('OPTIONAL INPUTS', 'Things you may want to tweak.')
    groupB.add_argument(*anvio.A('output-db-path'), **anvio.K('output-db-path', {'default': 'CONTIGS.db'}))
    groupB.add_argument(*anvio.A('description'), **anvio.K('description'))
    groupB.add_argument(*anvio.A('split-length'), **anvio.K('split-length'))
    groupB.add_argument(*anvio.A('kmer-size'), **anvio.K('kmer-size'))

    groupC = parser.add_argument_group('GENES IN CONTIGS', 'Expert thingies.')
    groupC.add_argument(*anvio.A('skip-gene-calling'), **anvio.K('skip-gene-calling'))
    groupC.add_argument(*anvio.A('prodigal-translation-table'), **anvio.K('prodigal-translation-table'))
    groupC.add_argument(*anvio.A('external-gene-calls'), **anvio.K('external-gene-calls'))
    groupC.add_argument(*anvio.A('ignore-internal-stop-codons'), **anvio.K('ignore-internal-stop-codons'))
    groupB.add_argument(*anvio.A('skip-mindful-splitting'), **anvio.K('skip-mindful-splitting'))

    args = anvio.get_args(parser)

    try:
        a = dbops.ContigsDatabase(args.output_db_path, run, progress, quiet=False, skip_init = True)
        a.create(args)
    except ConfigError as e:
        print(e)
        sys.exit(-1)
    except FilesNPathsError as e:
        print(e)
        sys.exit(-2)
