#!/opt/anaconda1anaconda2anaconda3/bin/python
# -*- coding: utf-8
"""Takes an anvi'o profile or pan database and returns a quick text summary
of bins and collections found in it."""

import sys

import anvio
import anvio.utils as utils
import anvio.terminal as terminal
import anvio.ccollections as ccollections

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"


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


def main(args):
    utils.is_pan_or_profile_db(args.pan_or_profile_db)

    progress.new('Accessing to the collections table')
    progress.update('...')
    collections = ccollections.Collections()
    collections.populate_collections_dict(args.pan_or_profile_db)
    progress.end()

    for source in collections.collections_dict:
        run.warning('', header = 'Collection: "%s"' % source, lc = 'green')
        run.info('Collection ID', source)
        run.info('Number of bins', collections.collections_dict[source]['num_bins'])
        run.info('Number of splits described', collections.collections_dict[source]['num_splits'])
        run.info('Bin names', ', '.join(sorted(collections.collections_dict[source]['bin_names'].split(','))), nl_after = 2)


if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser(description="A script to display collections stored in an anvi'o profile or pan database.")

    parser.add_argument(*anvio.A('pan-or-profile-db'), **anvio.K('pan-or-profile-db'))

    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)
