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

import sys

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

import anvio
from anvio.parsers import parser_modules
from anvio.errors import ConfigError, FilesNPathsError


__author__ = "A. Murat Eren"
__copyright__ = "Copyright 2015, The anvio Project"
__credits__ = []
__license__ = "GPL 3.0"
__version__ = anvio.__version__
__maintainer__ = "A. Murat Eren"
__email__ = "a.murat.eren@gmail.com"
__status__ = "Development"


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

def main(args):
    # make sure we have the parser
    if not args.parser:
        raise ConfigError, "You need to choose a parser :/ See the '--help' menu."

    if args.parser not in parser_modules['collections']:
        raise ConfigError, "I don't know what to do with '%s'. You must use one of the available parsers\
                            for clustering of splits (please see the documentation if you are not sure what is\
                            going on here): %s" % (args.parser, ', '.join(parser_modules['collections']))

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

    # get the clusters dict.
    parser = parser_modules['collections'][args.parser](args.input_files)
    clusters_dict = parser.get_clusters_dict()

    if not len(clusters_dict):
        raise ConfigError, "Your parser (%s) returned an empty dictionary 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)

    collections = dbops.TablesForCollections(args.annotation_db_path, t.annotation_db_version)
    collections.append(args.parser, clusters_dict)


if __name__ == '__main__':
    available_parsers = parser_modules['collections'].keys()
    num_available_parsers = len(available_parsers)

    import argparse
    parser = argparse.ArgumentParser(description='This program deals with populating collections_* tables in the\
                                                  annotation database. Collections are essentially clusters of splits.\
                                                  This information can be generated by any binning software (including\
                                                  anvio itself through the interactive binning process). You can use any\
                                                  genome binning software to identify clusters (or draft genomes) in your\
                                                  metagenome, and incorporate that information with the rest of your\
                                                  analysis on anvio as long as there is a parser to make sense of the\
                                                  output file(s) generated by teh genome binnign software you used.\
                                                  If yours is not available, please get in touch with the developers, and\
                                                  it will be included in no time. Or you can use the default matrix you\
                                                  manually curate until a native parsers specificly written for your\
                                                  favorite genome binning software is available in anvio. Please see the\
                                                  documentation for details.')
    parser.add_argument('annotation_db_path', metavar = 'ANNOTATION_DB',
                        help = 'Annotation database to update.')
    parser.add_argument('-p', '--parser', default = None,
                        help = 'A parser to make sense of the input files. Please consult the documentation if you have\
                                questions regarding parsers, or contact anvio developers. Currently there %s: %s.' %\
                                    ('are %d parsers' % num_available_parsers if num_available_parsers > 1 else 'is only one parser',
                                    ', '.join(available_parsers)))
    parser.add_argument('-i', '--input-files', metavar = 'FILE(S)', nargs='+', default = None,
                        help = 'Input file(s) for selected parser. Each parser requires one or more input files\
                                generated by you or your genome binnig software. Please see the documentation for details.')


    args = parser.parse_args()

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