#!/usr/bin/env python
# -*- coding: utf-8
"""Split a merged 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__ = "A. Murat Eren"
__copyright__ = "Copyright 2017, The anvio Project"
__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 a merged profile into smaller profiles. This is usually great when\
                                                  you want to share a subset of a merged profile. You give this guy a merged\
                                                  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 merged 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 merged profile that\
                                                  will be split from the mother merged 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('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 = parser.parse_args()

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