#!/usr/bin/env python
###############################################################################
#                                                                             #
#    bam2fpkc                                                                 #
#                                                                             #
#    Entry point. See bam2fpkc/bam2fpkc.py for internals                      #
#                                                                             #
#    Copyright (C) Michael Imelfort                                           #
#                                                                             #
###############################################################################
#                                                                             #
# 888                              .d8888b.  .d888          888               #
# 888                             d88P  Y88 d88P"           888               #
# 888                                    88 888             888               #
# 88888b.   8888b.  88888b.d88b.       .d88 888888 88888b.  888  888  .d8888b # 
# 888 "88b     "88b 888 "888 "88b  .od888P" 888    888 "88b 888 .88P d88P"    #
# 888  888 .d888888 888  888  888 d88P"     888    888  888 888888K  888      #
# 888 d88P 888  888 888  888  888 888"      888    888 d88P 888 "88b Y88b.    #
# 88888P"  "Y888888 888  888  888 88888888  888    88888P"  888  888  "Y8888P #
#                                                  888                        #
#                                                  888                        #
#                                                  888                        #
#                                                                             #
###############################################################################
#                                                                             #
#    This program is free software: you can redistribute it and/or modify     #
#    it under the terms of the GNU General Public License as published by     #
#    the Free Software Foundation, either version 3 of the License, or        #
#    (at your option) any later version.                                      #
#                                                                             #
#    This program is distributed in the hope that it will be useful,          #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of           #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            #
#    GNU General Public License for more details.                             #
#                                                                             #
#    You should have received a copy of the GNU General Public License        #
#    along with this program. If not, see <http://www.gnu.org/licenses/>.     #
#                                                                             #
###############################################################################

__author__ = "Michael Imelfort"
__copyright__ = "Copyright 2013"
__credits__ = ["Michael Imelfort"]
__license__ = "GPL3"
__version__ = "0.1.0"
__maintainer__ = "Michael Imelfort"
__email__ = "mike@mikeimelfort.com"
__status__ = "Development"

###############################################################################

import argparse
import sys

from bam2fpkc import bam2fpkc

###############################################################################
###############################################################################
###############################################################################
###############################################################################
def printHelp():
    print '''\
    
               ...::: bam2fpkc :::...
                   
    Contig abundance profile maker

    bam2fpkc contig      -> Calculate fpkc for one set of contigs
    bam2fpkc bin         -> Calculate mfpkc for a set of bins
    
    USE: bam2fpkc OPTION -h to see detailed options
    '''
if __name__ == '__main__':

    #-------------------------------------------------
    # intialise the options parser
    parser = argparse.ArgumentParser(add_help=False)
    subparsers = parser.add_subparsers(help="--", dest='subparser_name')

    #-------------------------------------------------
    # Run through a single fasta file and calculate fpkc
    contig_parser = subparsers.add_parser('contig',
                                          formatter_class=argparse.ArgumentDefaultsHelpFormatter,
                                          help='calculate fpkc for one set of contigs',
                                          description='Calculate fpkc for one set of contigs')
    contig_parser.add_argument('contigs', help="fasta file of contigs")
    contig_parser.add_argument('bams', nargs='+', help="bam file to parse")
    contig_parser.add_argument('--outfile', '-o', default='stdout', help="print to file")
    contig_parser.add_argument('--threads', '-t', default=1, help="number of threads to use")
    
    #-------------------------------------------------
    # Run through a single fasta file and calculate fpkc
    bin_parser = subparsers.add_parser('bin',
                                       formatter_class=argparse.ArgumentDefaultsHelpFormatter,
                                       help='calculate mfpkc for a set of bins',
                                       description='Calculate mfpkc for a set of bins')
    bin_parser.add_argument('contigs', help="fasta file of contigs")
    bin_parser.add_argument('bins', help="bin assignments")
    bin_parser.add_argument('bams', nargs='+', help="bam file to parse")
    bin_parser.add_argument('--outfile', '-o', default='stdout', help="print to file")
    bin_parser.add_argument('--threads', '-t', default=1, help="number of threads to use")
    
    #-------------------------------------------------
    # get and check options
    args = None
    if(len(sys.argv) == 1 or sys.argv[1] == '-h' or sys.argv == '--help'):
        printHelp()
        sys.exit(0)
    else:
        args = parser.parse_args()

    #-------------------------------------------------
    # do what we came here to do
    try:
        BT_parser = bam2fpkc.Bam2fpkcOptionsParser()
        if(False):
            import cProfile
            cProfile.run('BT_parser.parseOptions(args)', 'prof')
        else:        
            BT_parser.parseOptions(args)
    except:
        print "Unexpected error:", sys.exc_info()[0]
        raise
    
###############################################################################
###############################################################################
###############################################################################
###############################################################################
        
