#!python

# -*- coding: utf-8 -*-

# Created on Sun Dec 23 10:53:21 2018

# Author: XiaoTao Wang

from __future__ import division, print_function
import os.path as op
import glob, sys, argparse, runHiC

currentVersion = runHiC.__version__

def getargs():
    ## Construct an ArgumentParser object for command-line arguments
    parser = argparse.ArgumentParser(description = '''Phase a pairsam file based on phased SNPs.
                                     Note that this program uses a different algorithm compared
                                     to "pairtools phase" and typically results in ten times
                                     more haplotype-resolved contacts.''',
                                     formatter_class = argparse.ArgumentDefaultsHelpFormatter)
    
    # Version
    parser.add_argument('-v', '--version', action = 'version',
                        version = ' '.join(['%(prog)s', currentVersion]),
                        help = 'Print version number and exit')
    
    parser.add_argument('--pairs-path',
                        help='''Path to a pairsam file.''')
    parser.add_argument('-O', '--output',
                        help='Output file path.')
    parser.add_argument('--phased-SNPs',
                        help='''Path to a TXT file containing phased SNPs.''')
    parser.add_argument('--max-molecule-size', type = int, default = 750,
                        help='''The maximal size of a Hi-C molecule.''')
    parser.add_argument('--include-readid', action = 'store_true',
                        help='''If specified, add read IDs to the outputed .pairsam files.''')
    parser.add_argument('--include-sam', action = 'store_true',
                        help='''If specified, add sam columns to the outputed .pairsam files.''')
    parser.add_argument('--nproc-in', type = int, default = 3,
                        help='''Number of processes used by the auto-guessed input decompressing command.''')
    parser.add_argument('--nproc-out', type = int, default = 8,
                        help='''Number of processes used by the auto-guessed output compressing command.''')
    
    ## Parse the command-line arguments
    commands = sys.argv[1:]
    if not commands:
        commands.append('-h')
    args = parser.parse_args(commands)
    
    return args, commands

def phase():

    # Parse Arguments
    args, commands = getargs()
    # Improve the performance if you don't want to run it
    if commands[0] not in ['-h', '-v', '--help', '--version']:

        from runHiC.phase import phase_pipeline

        phased_snp = op.abspath(op.expanduser(args.phased_SNPs))
        infil = args.pairs_path
        out = args.output

        phase_pipeline(
            infil,
            out,
            phased_snp,
            nproc_in=args.nproc_in,
            nproc_out=args.nproc_out,
            include_readid=args.include_readid,
            include_sam=args.include_sam,
            maximum_dist=args.max_molecule_size
        )

if __name__ == '__main__':
    phase()