#!/usr/bin/env python
# -*- coding: utf-8
"""Return counts of AAs in bins, contigs, or gene caller ids"""

import sys

import anvio
import anvio.terminal as terminal

from anvio.errors import ConfigError, FilesNPathsError
from anvio.dbops import AA_counts


__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):
    aa_counts = AA_counts(args)
    aa_counts.report()


if __name__ == '__main__':
    import argparse

    parser = argparse.ArgumentParser(description='Collects AA counts information from a contigs database\
                                                  for a given bin, set of contigs, or set of genes.')

    group0 = parser.add_argument_group('MANDATORY STUFF', 'You have to set the following two parameters, then\
                                                           you will select one set of parameters from the\
                                                           following optional sections. If you select nothing\
                                                           from those sets, AA counts for everything in the\
                                                           contigs database will be reported.')
    group0.add_argument(*anvio.A('contigs-db'), **anvio.K('contigs-db', {'required': True}))
    group0.add_argument(*anvio.A('output-file'), **anvio.K('output-file'))

    groupA = parser.add_argument_group('OPTIONAL PARAMS FOR BINS')
    groupA.add_argument(*anvio.A('profile-db'), **anvio.K('profile-db', {'required': False}))
    groupA.add_argument(*anvio.A('collection-name'), **anvio.K('collection-name'))
    groupA.add_argument(*anvio.A('bin-ids-file'), **anvio.K('bin-ids-file'))

    groupB = parser.add_argument_group('OPTIONAL PARAMS FOR CONTIGS')
    groupB.add_argument(*anvio.A('contigs-of-interest'), **anvio.K('contigs-of-interest', {'help': "A file with\
                                                         contig names. There should be only one column in the file,\
                                                         and each line should correspond to a unique split name."}))

    groupC = parser.add_argument_group('OPTIONAL PARAMS FOR GENE CALLS')
    groupC.add_argument(*anvio.A('gene-caller-ids'), **anvio.K('gene-caller-ids'))

    args = parser.parse_args()

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