#!/opt/anaconda1anaconda2anaconda3/bin/python
# -*- coding: utf-8
"""Populate misc data tables"""

import sys
import argparse

import anvio

from anvio.errors import ConfigError, FilesNPathsError
from anvio.tables.miscdata import MiscDataTableFactory


__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__
__email__ = "a.murat.eren@gmail.com"
__requires__ = ['genes-db', 'pan-db', 'profile-db', 'misc-data-items-txt', 'dendrogram', 'phylogeny', 'misc-data-layers-txt', 'misc-data-layer-orders-txt']
__provides__ = ['misc-data-items', 'misc-data-layers', 'misc-data-layer-orders']


def main(args):
    # - you're ugly.
    # - no u :(
    MiscDataTableFactory(args).populate_from_file(args.data_file)


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description="Populate additional data or order tables in pan or profile databases for\
                                                  items or layers (the Swiss army knife-level serious stuff).")

    parser.add_argument('data_file', metavar = "TAB DELIMITED FILE", help = 'The input file that describes an additional data\
                        for layers or items. The expected format of this file depends on the data table you will target. This\
                        can feel complicated, but we promise it is not (you probably have a PhD or working on one, so trust us\
                        when we say "it is not complicated"). You need to read the online documentation if this is your first\
                        time with this.')

    parser.add_argument(*anvio.A('pan-or-profile-db'), **anvio.K('pan-or-profile-db'))
    parser.add_argument(*anvio.A('target-data-table'), **anvio.K('target-data-table', {'required': True}))
    parser.add_argument(*anvio.A('target-data-group'), **anvio.K('target-data-group'))
    parser.add_argument(*anvio.A('transpose'), **anvio.K('transpose'))
    parser.add_argument(*anvio.A('just-do-it'), **anvio.K('just-do-it'))

    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)
