#!/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"


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description="Split an anvi'o profile into smaller profiles. This is usually great when\
                                                  you want to share a subset of an anvi'o profile. You give this guy an anvi'o\
                                                  profile databsae, a contigs database, and a collection id, and it gives\
                                                  you back directories of profiles for each bin that can be treated as\
                                                  individual anvi'o profiles.")

    groupD = parser.add_argument_group('DATABASES', "Declaring relevant anvi'o databases. First things first.")
    groupD.add_argument(*anvio.A('profile-db'), **anvio.K('profile-db'))
    groupD.add_argument(*anvio.A('contigs-db'), **anvio.K('contigs-db'))

    groupE = 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.')
    groupE.add_argument(*anvio.A('collection-name'), **anvio.K('collection-name'))
    groupE.add_argument(*anvio.A('bin-id'), **anvio.K('bin-id'))

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

    groupQ = 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.")
    groupQ.add_argument(*anvio.A('list-collections'), **anvio.K('list-collections'))
    groupQ.add_argument(*anvio.A('skip-hierarchical-clustering'), **anvio.K('skip-hierarchical-clustering'))
    groupQ.add_argument(*anvio.A('skip-variability-tables'), **anvio.K('skip-variability-tables'))
    groupQ.add_argument(*anvio.A('compress-auxiliary-data'), **anvio.K('compress-auxiliary-data'))
    groupQ.add_argument(*anvio.A('enforce-hierarchical-clustering'), **anvio.K('enforce-hierarchical-clustering'))
    groupQ.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}))
    groupQ.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.ProfileSplitter(args).process()
    except ConfigError as e:
        print(e)
        sys.exit(-1)
    except FilesNPathsError as e:
        print(e)
        sys.exit(-1)
