#!/opt/anaconda1anaconda2anaconda3/bin/python
# -*- coding: utf-8
"""Fetches information from the variable positions table"""

import sys
import argparse

import anvio

from anvio.errors import ConfigError, FilesNPathsError
from anvio.variabilityops import variability_engines


__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"


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Extract information for variable positions')

    groupD = parser.add_argument_group('DATABASES', 'Declaring relevant anvi\'o databases. First things first.')
    groupS = parser.add_argument_group('SPLITS', 'Declaring relevant splits for the analysis. There are two\
                                                  ways to do it. One, you can give a file path with split names,\
                                                  or, as an alternative, you can provide a collection id with a\
                                                  bin name.')
    groupO = parser.add_argument_group('OUTPUT', 'Output file and output style')
    groupE = parser.add_argument_group('EXTRAS', 'Parameters that will help you to do a very precise analysis.\
                                                  If you declare nothing from this bunch, you will get "everything"\
                                                  to play with, which is not necessarily a good thing...')

    groupS.add_argument(*anvio.A('splits-of-interest'), **anvio.K('splits-of-interest'))
    groupS.add_argument(*anvio.A('collection-name'), **anvio.K('collection-name'))
    groupS.add_argument(*anvio.A('bin-id'), **anvio.K('bin-id'))
    groupD.add_argument(*anvio.A('profile-db'), **anvio.K('profile-db'))
    groupD.add_argument(*anvio.A('contigs-db'), **anvio.K('contigs-db'))
    groupO.add_argument(*anvio.A('output-file'), **anvio.K('output-file', {'default': 'variability.txt'}))
    groupO.add_argument(*anvio.A('samples-of-interest'), **anvio.K('samples-of-interest'))
    groupO.add_argument(*anvio.A('quince-mode'), **anvio.K('quince-mode'))
    groupO.add_argument(*anvio.A('include-contig-names'), **anvio.K('include-contig-names'))
    groupO.add_argument(*anvio.A('include-split-names'), **anvio.K('include-split-names'))
    groupE.add_argument(*anvio.A('engine'), **anvio.K('engine'))
    groupE.add_argument(*anvio.A('num-positions-from-each-split'), **anvio.K('num-positions-from-each-split'))
    groupE.add_argument(*anvio.A('min-scatter'), **anvio.K('min-scatter'))
    groupE.add_argument(*anvio.A('min-coverage-in-each-sample'), **anvio.K('min-coverage-in-each-sample'))
    groupE.add_argument(*anvio.A('min-departure-from-reference'), **anvio.K('min-departure-from-reference'))
    groupE.add_argument(*anvio.A('max-departure-from-reference'), **anvio.K('max-departure-from-reference'))
    groupE.add_argument(*anvio.A('min-departure-from-consensus'), **anvio.K('min-departure-from-consensus'))
    groupE.add_argument(*anvio.A('max-departure-from-consensus'), **anvio.K('max-departure-from-consensus'))
    groupE.add_argument(*anvio.A('min-occurrence-of-variable-positions'), **anvio.K('min-occurrence-of-variable-positions'))
    groupE.add_argument(*anvio.A('genes-of-interest'), **anvio.K('genes-of-interest'))

    args = parser.parse_args()
    
    try:
        if args.engine not in variability_engines:
            raise ConfigError("You are doing something wrong :/ Focus '%s' does not correspond to an available engine." % args.engine)

        variability_engine = variability_engines[args.engine](args)
        variability_engine.process()
        variability_engine.report()
    except ConfigError as e:
        print(e)
        sys.exit(1)
    except FilesNPathsError as e:
        print(e)
        sys.exit(2)
