
# Copyright (c) 2013, Ian Reid, Concordia University Centre for Structural and Functional Genomics
# All rights reserved.

__author__ = 'ian'

import sys, os

this_dir = os.path.dirname(__file__)
src = os.path.dirname(this_dir)
sys.path.append(src)
import argparse

DESCRIPTION = 'Convert output from samtools depth command into bedGraph format'
VERSION = '0.1'


def get_args():
    argparser = argparse.ArgumentParser(description=DESCRIPTION)
    # standard options
    argparser.add_argument('--version', action='version', version='%(prog)s' + VERSION)
    argparser.add_argument('--verbose', '-v', action='count', default=0,
                           help='Omit to see only fatal error messages; -v to see warnings; -vv to see warnings and progress messages')
    # options to customize
    argparser.add_argument('--in', '-i', dest='input', type=argparse.FileType('r'), nargs='?', default=sys.stdin,
                           help='Path to the input file; if omitted or -, input is read from stdin')
    argparser.add_argument('--out', '-o', type=argparse.FileType('w'), nargs='?', default=sys.stdout,
                           help='Path to the output file; if omitted or -, output is written to stdout')
    return argparser.parse_args()


if __name__ == '__main__':
    args = get_args()

    curr_val = 0
    curr_start = curr_end = 0
    curr_chrom = None

    for line in args.input:
        try:
            chrom, start_s, val_s = line.strip().split('\t')
            if not curr_chrom:
                curr_chrom = chrom
            if chrom != curr_chrom:
                # flush data
                print >> args.out, '\t'.join([curr_chrom, str(curr_start), str(curr_end), str(curr_val)])
                curr_chrom = chrom
                curr_start = curr_end = 0
                curr_val = 0
            start = int(start_s)
            if start <= curr_end:
                raise ValueError('Input is out of order at %s' % line.strip())
            if start > curr_end + 2:
                # gap in coverage
                if curr_val > 0:
                    print >> args.out, '\t'.join([curr_chrom, str(curr_start), str(curr_end + 1), str(curr_val)])
                    curr_start = curr_end + 1
                curr_val = 0
                curr_end = start - 1
            val = int(val_s)
            if val != curr_val:
                print >> args.out, '\t'.join([curr_chrom, str(curr_start), str(start - 1), str(curr_val)])
                curr_val = val
                curr_start = start -1
#                curr_end = curr_start
#            else:
            curr_end = start - 1
        except Exception, e:
            print >> sys.stderr, e

    if all([curr_chrom, curr_start, curr_end, curr_val]):
        print >> args.out, '\t'.join([curr_chrom, str(curr_start), str(curr_end ), str(curr_val)])

    args.out.close()
    print >> sys.stderr, sys.argv[0], 'done.'
