#!/usr/bin/env python
# -*- coding: utf-8
"""Split an anvi'o profile into smaller pieces."""

import sys
import argparse

import anvio
import anvio.splitter as splitter
import anvio.constants as constants

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__ = ['profile-db', 'contigs-db', 'collection',]
__provides__ = ['split-bins',]
__description__ = ("Split an anvi'o pan or profile database into smaller, self-contained pieces. "
                   "This is usually great when you want to share a subset of an anvi'o project. "
                   "You give this guy your databases, and a collection id, and it gives "
                   "you back directories of individual projects for each bin that can be treated as "
                   "self-contained smaller anvi'o projects. We know you don't read this far into "
                   "these help menus, but please remember: you will either need to provide a profile & "
                   "contigs database pair, or a pan & genomes storage pair. The rest will be taken "
                   "care of. Magic.")


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description=__description__)

    groupA = parser.add_argument_group('DATABASES', "You will either provide a PROFILE/CONTIGS or a PAN/GENOMES STORAGE pair here.")
    groupA.add_argument(*anvio.A('pan-or-profile-db'), **anvio.K('pan-or-profile-db'))
    groupA.add_argument(*anvio.A('contigs-db'), **anvio.K('contigs-db', {'required': False}))
    groupA.add_argument(*anvio.A('genomes-storage'), **anvio.K('genomes-storage', {'required': False}))


    groupB = parser.add_argument_group('PROFILE/CONTIGS OPTIONS', "Some options that are specific to this only.")
    groupB.add_argument(*anvio.A('skip-variability-tables'), **anvio.K('skip-variability-tables'))
    groupB.add_argument(*anvio.A('compress-auxiliary-data'), **anvio.K('compress-auxiliary-data'))

    groupC = parser.add_argument_group('COLLECTION', 'You should provide a valid collection name. If you do not provide\
                                                      bin names, the program will generate an output for each bin in your\
                                                      collection separately.')
    groupC.add_argument(*anvio.A('collection-name'), **anvio.K('collection-name'))
    groupC.add_argument(*anvio.A('bin-id'), **anvio.K('bin-id'))

    groupD = parser.add_argument_group('OUTPUT', 'Where do we want the resulting split profiles to be stored.')
    groupD.add_argument(*anvio.A('output-dir'), **anvio.K('output-dir'))

    groupE = parser.add_argument_group('EXTRAS', "Stuff that you rarely need, but you really really need when the time comes.\
                                                  Following parameters will aply to each of the resulting anvi'o profile that\
                                                  will be split from the mother anvi'o profile.")
    groupE.add_argument(*anvio.A('list-collections'), **anvio.K('list-collections'))
    groupE.add_argument(*anvio.A('skip-hierarchical-clustering'), **anvio.K('skip-hierarchical-clustering'))
    groupE.add_argument(*anvio.A('enforce-hierarchical-clustering'), **anvio.K('enforce-hierarchical-clustering'))
    groupE.add_argument(*anvio.A('distance'), **anvio.K('distance', {'default': None, 'help':
                      'The distance metric for the hierarchical clustering. If you do not use this flag,\
                       the default distance metric will be used for each clustering configuration\
                       which is "%s".' % constants.distance_metric_default}))
    groupE.add_argument(*anvio.A('linkage'), **anvio.K('linkage', {'default': None, 'help':
                      'The same story with the `--distance`, except, the system default for this one\
                       is %s.' % constants.linkage_method_default}))

    args = anvio.get_args(parser)

    try:
        splitter.DBSplitter(args).get()(args).process()
    except ConfigError as e:
        print(e)
        sys.exit(-1)
    except FilesNPathsError as e:
        print(e)
        sys.exit(-1)
