#!/usr/bin/env python3

'''
    Uses python3.
    email: romain.guerillot@unimelb.edu.au
    Authors: Romain Guerillot, Torsten Seemann, Mark B. Schultz
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero 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 Affero General Public License for more details.
    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
'''

from multiprocessing import cpu_count
import argparse
import os
import sys
from RMseq.__init__ import (__version__,
                            __version_date__,
                            __author__,
                            __author_email__,
                            __github_username__,
                            __download_url__)


PARSER = argparse.ArgumentParser(description='Run RM-seq pipeline.')

SUBPARSERS = PARSER.add_subparsers(title='Commands',
                                   help='', metavar='', dest='subparser_name')
#---------------------------- check ------------------------------------
SUBPARSER_VERSION = SUBPARSERS.add_parser('version', help='Print version.',
                                        usage='pandoo version',
                                        description='Print the pipeline ' +\
                                                    'version')
SUBPARSER_RUN = SUBPARSERS.add_parser('run', help='Run the pipeline.',
                                        usage='pandoo run [options]',
                                        description='Run the pipeline')

SUBPARSER_RUN.add_argument('-d', '--debug_on', help='Switch on debug mode.',
                           action='store_true', default=False, required=False)
SUBPARSER_RUN.add_argument('-1', '--R1', help='Path to read pair 1',
                           required=True)
SUBPARSER_RUN.add_argument('-2', '--R2', help='Path to read pair 2',
                           required=True)
SUBPARSER_RUN.add_argument('-n', '--refnuc',
                           help='Reference gene that will be used for ' +\
                                'premapping filtering (fasta).',
                           required=True)
SUBPARSER_RUN.add_argument('-p', '--refprot',
                           help='Reference protein that will be use for ' +\
                                'annotating variants (fasta).',
                           required=True)
SUBPARSER_RUN.add_argument('-o', '--outdir', help='Output directory.',
                           required=True)
SUBPARSER_RUN.add_argument('-f', '--force', help='Force overwite of existing.',
                           action='store_true', default=False, required=False)
SUBPARSER_RUN.add_argument('-b', '--barlen',
                           help='Length of barcode (default 16)',
                           default=16, required=False)
SUBPARSER_RUN.add_argument('-m', '--minfreq',
                           help='Minimum barcode frequency to keep ' +\
                                '(default 5)',
                           default=5, required=False)
SUBPARSER_RUN.add_argument('-c', '--cpus',
                           help='Number of CPUs to use (default ' +\
                                str(cpu_count())+')', default=cpu_count(),
                           required=False)
SUBPARSER_RUN.add_argument('-r', '--minsize',
                           help='Minimum ORF size in bp used when ' +\
                                'annotating variants (default 200)',
                           default=200, required=False)
SUBPARSER_RUN.add_argument('-w', '--wsize',
                           help='Word-size option to pass to diffseq for ' +\
                                'comparison with reference sequence ' +\
                                '(default 5)',
                           default=5, required=False)
SUBPARSER_RUN.add_argument('-s', '--subsample',
                           help='Only examine this many reads.',
                           default=None, type=int, required=False)
SUBPARSER_RUN.add_argument('-k', '--keepfiles',
                           help='Do not delete intermediate files.',
                           action='store_true', default=False, required=False)
SUBPARSER_VERSION.add_argument('-v', '--version', action='store_true',
                               help='Print version and exit.', default=False)

ARGS = PARSER.parse_args()


def version():
    '''
    Print version information.
    '''
    VERSION = '\nRM-seq version: ' + __version__ +\
              '\nVersion date: ' + __version_date__ +\
              '\nAuthors: '+ __author__ +\
              '\nCorresponding author email: '+ __author_email__ +\
              '\nGithub: ' + __github_username__ +\
              '\nDownload url: ' + __download_url__ +'\n'
    return VERSION


def main():
    '''
    This script is a python wrapper to launch the RM-seq pipeline.
    '''
    if ARGS.debug_on:
        debug = ' --debug'
    else:
        debug = ''
    if ARGS.keepfiles:
        keepfiles = ' --keepfiles'
    else:
        keepfiles = ''
    if ARGS.force:
        force = ' --force'
    else:
        force = ''
    if ARGS.subsample and ARGS.subsample > 0:
        subsample = ' --subsample '+str(ARGS.subsample)
    else:
        subsample = ''

    cmd = './RM-seq.pl'+debug+keepfiles+force +\
          ' --R1 ' +\
          os.path.abspath(ARGS.R1) +\
          ' --R2 ' +\
          os.path.abspath(ARGS.R2) +\
          ' --refnuc ' +\
          os.path.abspath(ARGS.refnuc) +\
          ' --refprot ' +\
          os.path.abspath(ARGS.refprot) +\
          ' --outdir ' +\
          os.path.abspath(ARGS.outdir) +\
          ' --barlen ' +\
          str(ARGS.barlen) +\
          ' --minfreq ' +\
          str(ARGS.minfreq) +\
          ' --cpus ' +\
          str(ARGS.cpus) +\
          ' --minsize ' +\
          str(ARGS.minsize) +\
          ' --wsize ' +\
          str(ARGS.wsize) +\
          subsample

    os.system(cmd)

if __name__ == '__main__':
    if not ARGS.subparser_name:
        os.system('rmseq -h')
    if ARGS.subparser_name == 'run':
        main()
    if ARGS.subparser_name == 'version':
        print(version())