#!/usr/bin/python
###############################################################################
#                                                                             #
#    readSelector                                                             #
#                                                                             #
#    Entry point. See readselector/readSelector.py for internals              #
#                                                                             #
#    Copyright (C) Michael Imelfort                                           #
#                                                                             #
###############################################################################
#                                                                             #
#    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 2012"
__credits__ = ["Michael Imelfort"]
__license__ = "GPL3"
__version__ = "0.1.0"
__maintainer__ = "Michael Imelfort"
__email__ = "mike@mikeimelfort.com"
__status__ = "Development"

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

import argparse
import sys
from readselector import readSelector

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

def printHelp():
    print '''\
    
             ...::: readSelector :::...
                   
    readSelector unpaired   -> Select unpaired files
    readSelector shuffle  -> Select shuffled files
    readSelector paired   -> Select from paired files
    
    USE: readSelector 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')

    ##################################################
    # Typical workflow
    ##################################################
    
    #-------------------------------------------------
    # select unpaired reads
    unpaired_parser = subparsers.add_parser('unpaired',
                                        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
                                        help='select from unpaired files',
                                        description='Select from unpaired files')
    unpaired_parser.add_argument('number', type=int, help="number of reads to select")
    unpaired_parser.add_argument('files', nargs='+', help="files to parse")
    unpaired_parser.add_argument('-o', '--outfile', nargs=1, default='', help="write to file, none for stdout. Use XXX.fastX.gz to force gzip output. Requires outfile")
    unpaired_parser.add_argument('-v', '--verbose', action="store_true", default=False, help="print more")

    #-------------------------------------------------
    # select shuffled reads
    shuffled_parser = subparsers.add_parser('shuffled',
                                        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
                                        help='select from shuffled files',
                                        description='Select from shuffled files')
    shuffled_parser.add_argument('number', type=int, help="number of read pairs to select")
    shuffled_parser.add_argument('files', nargs='+', help="files to parse")
    shuffled_parser.add_argument('-o', '--outfile', nargs=1, default='', help="write to file, none for stdout. Use XXX.fastX.gz to force gzip output. Requires outfile")
    shuffled_parser.add_argument('-v', '--verbose', action="store_true", default=False, help="print more")


    #-------------------------------------------------
    # select paired reads
    paired_parser = subparsers.add_parser('paired',
                                        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
                                        help='select from paired files',
                                        description='Select from paired files')
    paired_parser.add_argument('number', type=int, help="number of read pairs to select")
    paired_parser.add_argument('files', nargs='+', help="files to parse")
    paired_parser.add_argument('-p', '--prefix', nargs=1, default='', help="out file prefix, none for stdout")
    paired_parser.add_argument('-l', '--large', action='store_true', default=False, help="do NOT gzip output files. Requires outfile")
    paired_parser.add_argument('-v', '--verbose', action="store_true", default=False, help="print more")

    #-------------------------------------------------
    # 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:
        RS_parser = readSelector.ReadSelectorOptionsParser()
        if(False):
            #import pstats
            #p = pstats.Stats('prof')
            #p.sort_stats('cumulative').print_stats(10)
            #p.sort_stats('time').print_stats(10)
            import cProfile
            cProfile.run('RS_parser.parseOptions(args)', 'prof')
        elif False:
            import pdb
            pdb.run(RS_parser.parseOptions(args))
        else:        
            RS_parser.parseOptions(args)
    except:
        print "Unexpected error:", sys.exc_info()[0]
        raise
    
###############################################################################
###############################################################################
###############################################################################
###############################################################################
        
