#!/usr/bin/env python
# Purpose: Annotate transposon insertion locations using
# one or more .ptt files for the genome
#
# Copyright (c) 2014 University of Washington

#-----------------------------------------------------
# modules
#-----------------------------------------------------
import sys
import optparse
import os
import re
import glob
import common

#------------------------------------------------------------------------------
# constants
#------------------------------------------------------------------------------
PROGRESS_FREQ = 100
OUT_SUFFIX = "_annot.xls"

#------------------------------------------------------------------------------
# functions
#------------------------------------------------------------------------------
def init_options():
    usage = "usage: %prog [options]"
    parser = optparse.OptionParser(prog=sys.argv[0],
                          usage=usage,
                          add_help_option=True)
    parser.add_option("-i", "--infile", action="store", type="string", dest="infile", help="path to read counts summary file")
    parser.add_option("-a", "--annofiles", action="store", type="string", dest="annofiles", help="path to .ptt annotation file(s) (comma-separated list if using more than one; order must match sequences in reference fasta)")
    parser.add_option("-f", "--fasta", action="store", type="string", dest="fasta", help="path to reference fasta containing replicon names")
    parser.add_option("-o", "--outfile", action="store", type="string", dest="outfile", help="path to output file with merged read counts (default <infile>" + OUT_SUFFIX + ")")

    opts, args = parser.parse_args()
    if (opts.infile is None or opts.annofiles is None or opts.fasta is None):
        parser.print_help()
        exit(1)

    # Create the default output filename if not provided
    if (opts.outfile is None):
        parts = os.path.splitext(os.path.basename(opts.infile))
        opts.outfile = parts[0] + OUT_SUFFIX + parts[1]

    return opts, args

def annotate(replicon, position, direction, annotations):
    # Positions immediately right and left of the Tn
    if direction == 'F':
        lpos = position
        rpos = position - 8
    else:
        lpos = position + 8
        rpos = position

    effectivepos = "";