#!/usr/bin/env python
# -*- coding: utf-8

import sys
import anvio.profiler

import anvio
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"


if __name__ == '__main__':
    import argparse

    parser = argparse.ArgumentParser(description='Main entry point for Post-Assembly Metagenomics Pipeline')

    groupI = parser.add_argument_group('INPUTS', 'There are two possible inputs for anvio profiler. You must\
                                                  to declare either of these two.')
    groupI.add_argument('-i', '--input-file', metavar = 'INPUT_BAM', default = None,
                        help = 'Sorted and indexed BAM file to analyze. Takes a long time depending on the\
                                length of the file and parameters used for profiling.')

    groupM = parser.add_argument_group('EXTRAS', 'Things that are not mandatory, but can be useful if/when declared.')
    groupQ = parser.add_argument_group('NUMBERS', 'Defaults of these\
                                        parameters will impact your analysis. You can always come back to them\
                                        and update your profiles, but it is important to make sure defaults are\
                                        reasonable for your sample.')
    groupC = parser.add_argument_group('CONTIGS', 'Sweet parameters of convenience')

    groupI.add_argument(*anvio.A('contigs-db'), **anvio.K('contigs-db', {'required': False}))
    groupI.add_argument(*anvio.A('serialized-profile'), **anvio.K('serialized-profile'))
    groupI.add_argument(*anvio.A('blank-profile'), **anvio.K('blank-profile'))
    groupM.add_argument(*anvio.A('cluster-contigs'), **anvio.K('cluster-contigs'))
    groupM.add_argument(*anvio.A('output-dir'), **anvio.K('output-dir'))
    groupM.add_argument(*anvio.A('overwrite-output-destinations'), **anvio.K('overwrite-output-destinations'))
    groupM.add_argument(*anvio.A('sample-name'), **anvio.K('sample-name'))
    groupM.add_argument(*anvio.A('report-variability-full'), **anvio.K('report-variability-full'))
    groupM.add_argument(*anvio.A('skip-SNV-profiling'), **anvio.K('skip-SNV-profiling'))
    groupM.add_argument(*anvio.A('profile-AA-frequencies'), **anvio.K('profile-AA-frequencies'))
    groupM.add_argument(*anvio.A('gen-serialized-profile'), **anvio.K('gen-serialized-profile'))
    groupQ.add_argument(*anvio.A('min-contig-length'), **anvio.K('min-contig-length'))
    groupQ.add_argument(*anvio.A('min-mean-coverage'), **anvio.K('min-mean-coverage'))
    groupQ.add_argument(*anvio.A('min-coverage-for-variability'), **anvio.K('min-coverage-for-variability'))
    groupC.add_argument(*anvio.A('list-contigs'), **anvio.K('list-contigs'))
    groupC.add_argument(*anvio.A('contigs-of-interest'), **anvio.K('contigs-of-interest'))

    args = parser.parse_args()

    try:
        profiler = anvio.profiler.BAMProfiler(args)
        profiler._run()
    except ConfigError, e:
        print e
        sys.exit(-1)
    except FilesNPathsError, e:
        print e
        sys.exit(-1)
