#!/usr/bin/env python

# Jacob Joseph
# 2 Feb 2010

# Write a set of blast or NC scores to a file

import sys, getopt
from DurandDB import blastq


def write_scores( stype, run_id, fname, symmetric=False):
    bq = blastq.blastq()

    scores = bq.fetch_hits_direct( stype=stype, br_id = run_id,
                                   nc_id = run_id,
                                   symmetric = symmetric)

    fd = open(fname, 'w')
    for tup in scores:
        fd.write("%d\t%d\t%g\n" % tup)
    fd.close()
    return

def usage():
    s = """
    --stype <string>
       'nc_score', 'bit_score', or 'e_value'

    --run_id <integer>
       NC or Blast run id

    -o, --output <filename>
       Write scores to this file.
    
    --symmetric
        Report symmetric blast hits.  If not specified, raw scores are
        reported.

    -h, --help
        Print this message."""


def main():
    try:
        opts, args = getopt.getopt( sys.argv[1:], "ho:",
                                    ["stype=", "run_id=",
                                     "output=", "symmetric",
                                     "help"])
    except getopt.GetoptError:
        print usage()
        raise

    printhelp = False
    stype = None
    run_id = None
    fname = None
    symmetric = False

    for o,a in opts:
        print o, a
        
        if o in ('-h', '--help'):
            printhelp = True
        elif o == '--stype': stype = a
        elif o == '--run_id': run_id = int(a)
        elif o in ('-o', '--output'): fname = a
        elif o == '--symmetric': symmetric = True

    if printhelp or stype is None or run_id is None or fname is None:
        print usage()
        sys.exit()

    write_scores( stype, run_id, fname, symmetric)


if __name__ == "__main__":
    main()




