#!/opt/anaconda1anaconda2anaconda3/bin/python
# -*- coding: utf-8
"""Summarizer for anvi'o pan or profile db's.

   Essentially, this program takes a collection id along with either,
   
   - a profile database and a contigs database
   - or a pan database and a genomes storage

   and generates a static HTML output for what is described in a given
   collection."""

import sys

import anvio
import anvio.dbops as dbops
import anvio.terminal as terminal
import anvio.summarizer as summarizer

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"


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


def main(args):
    db_type = dbops.get_db_type(args.pan_or_profile_db)

    if db_type == 'pan':
        args.pan_db = args.pan_or_profile_db
        summary = summarizer.PanSummarizer(args)
    elif db_type == 'profile':
        args.profile_db = args.pan_or_profile_db
        profile_db = dbops.ProfileDatabase(args.profile_db)

        if not profile_db.meta['contigs_db_hash']:
            raise ConfigError("This profile database is not affiliated with any contigs database. Anvi'o currently does not\
                               summarize such stuff :/")
        profile_db.disconnect()

        if not args.contigs_db:
            raise ConfigError("You must provide a contigs database when you summarize anvi'o profiles. True story.")

        summary = summarizer.ProfileSummarizer(args)
    else:
        raise ConfigError("Well. '%s' is neither an anvi'o pan database, nor an anvi'o profile database. There is nothing this\
                            program can't do for you if you feed it with the right stuff. Just sayin'" % args.pan_or_profile_db)

    summary.process()


if __name__ == '__main__':
    import argparse

    parser = argparse.ArgumentParser(description="Summarize an anvi'o collection. Fun stuff.")

    groupA = parser.add_argument_group('PROFILE', "The profile. It could be a standard or pan profile database.")
    groupB = parser.add_argument_group('PROFILE TYPE SPECIFIC PARAMETERS', "If you are summarizing a collection stored in\
                                        a standard anvi'o profile, you will need a contigs database to go with it. If you\
                                        are working with a pan profile, then you will need to provide a genomes storage.\
                                        Don't worry too much, because anvi'o will warn you gently if you make a mistake.")
    groupC = parser.add_argument_group('STANDARD PROFILE SPEFIFIC PARAMS', "Parameters that are only relevant to standard\
                                        profile summaries (declaring or not declaring them will not change anything if you\
                                        are summarizing a pan profile).")
    groupD = parser.add_argument_group('COMMONS', "Common parameters for both pan and standard profile summaries.")


    groupA.add_argument(*anvio.A('pan-or-profile-db'), **anvio.K('pan-or-profile-db'))
    groupB.add_argument(*anvio.A('contigs-db'), **anvio.K('contigs-db', {'required': False}))
    groupB.add_argument(*anvio.A('genomes-storage'), **anvio.K('genomes-storage', {'required': False}))
    groupC.add_argument(*anvio.A('init-gene-coverages'), **anvio.K('init-gene-coverages'))
    groupD.add_argument(*anvio.A('collection-name'), **anvio.K('collection-name'))
    groupD.add_argument(*anvio.A('output-dir'), **anvio.K('output-dir'))
    groupD.add_argument(*anvio.A('list-collections'), **anvio.K('list-collections'))
    groupD.add_argument(*anvio.A('taxonomic-level'), **anvio.K('taxonomic-level'))
    groupD.add_argument(*anvio.A('cog-data-dir'), **anvio.K('cog-data-dir'))
    groupD.add_argument(*anvio.A('quick-summary'), **anvio.K('quick-summary'))
    groupD.add_argument(*anvio.A('debug'), **anvio.K('debug'))

    args = parser.parse_args()

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