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

import sys

import anvio
import anvio.tables as t
import anvio.terminal as terminal

from anvio.parsers import parser_modules
from anvio.errors import ConfigError, FilesNPathsError
from anvio.tables.taxonomy import TablesForTaxonomy


__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"


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


def main(args):
    # make sure we have the parser
    if not args.parser:
        raise ConfigError("You really need to specify a parser.")

    if args.parser not in parser_modules['taxonomy']:
        raise ConfigError("Anvi'o does not know what to do with '%s'. You must use one of the available taxonomy\
                            parsers so anvi'o can make sense of your input data. Here is what anvi'o has to parse\
                            incoming taxonomy (you should take a look at the documentation if you are not sure\
                            how to deal with this situation, because it is not that hard, in fact (or, you know\
                            you can send an e-mail to us)): %s"\
                                                 % (args.parser, ', '.join(parser_modules['taxonomy'])))
    if not args.input_files:
        raise ConfigError("You need to use '--input-files' parameter to list file(s) that is/are required the\
                            parser you chose. Please see the documentation for details.")

    parser = parser_modules['taxonomy'][args.parser](args.input_files, t.splits_taxonomy_table_structure, run=run, progress=progress)
    genes_taxonomy_dict, taxon_names_dict = parser.process()

    if not len(genes_taxonomy_dict):
        raise ConfigError("Your parser (%s) returned an empty result for your input file. Something must have gone wrong.\
                            Maybe you selected a wrong parser, or the input file format has changed between when this\
                            parser was implemented and now. Either ways, if you have exhausted your ideas for troubleshooting\
                            you should send an e-mail to anvio developers! Sorry for this!" % (args.parser))


    tables_for_taxonomy = TablesForTaxonomy(args.contigs_db, run, progress)
    tables_for_taxonomy.create(genes_taxonomy_dict, taxon_names_dict, args.parser)


if __name__ == '__main__':
    import argparse

    parser = argparse.ArgumentParser(description="Import taxonomy information into an anvi'o contigs database.")

    parser.add_argument(*anvio.A('contigs-db'), **anvio.K('contigs-db'))

    parser.add_argument('-p', '--parser', default = None,
                        help = 'Parser to make sense of the input files. There are %d parsers readily available: %s.\
                                It is OK if you do not select a parser, but in that case there will be no additional\
                                contigs available except the identification of single-copy genes in your contigs\
                                for later use. Using a parser will not prevent the analysis of single-copy genes,\
                                but make anvio more powerful to help you make sense of your results. Please see the\
                                documentation, or get in touch with the developers if you have any questions\
                                regarding parsers.' % (len(parser_modules['taxonomy']), list(parser_modules['taxonomy'].keys())))
    parser.add_argument('-i', '--input-files', metavar = 'FILE(S)', nargs='+', default = None, required = True,
                        help = 'Input file(s) for selected parser. Each parser (except "blank") requires input files to\
                                process that you generate before running anvio. Please see the documentation for details.')

    args = anvio.get_args(parser)

    try:
        main(args)
    except ConfigError as e:
        print(e)
        sys.exit(-1)
    except FilesNPathsError as e:
        print(e)
        sys.exit(-2)
