#!/usr/bin/env python

# Jacob Joseph
# 25 Feb 2010

# Insert sequence annotation file from Sven (22 Feb 2010 email)

import sys
from DurandDB import seqq

def parse_file( fname):
    """Return an iterator of (identifier, synonyms, description)..."""

    fd = open( fname)

    for l in fd:
        larr = l.strip('\n').split('\t')

        if larr == ['pthr', 'synonyms', 'description']:
            continue

        if len(larr) == 1 and larr[0].startswith('(') and larr[0].endswith(' rows)'):
            break

        assert len(larr) == 3, "Invalid line: '%s' (%s)" % (l, larr)

        yield tuple(larr)

    fd.close()
    return

    
def insert_fa_annotate( set_id, annotations):

    q_id = """SELECT seq_id
    FROM fa_descr
    JOIN prot_seq_set_member USING (seq_id)
    WHERE set_id = %(set_id)s
    AND descr = %(fa_descr)s"""

    i = """INSERT into fa_annotate
    ( seq_id, synonyms, descr)
    VALUES
    ( %(seq_id)s, 
      %(synonyms)s,
      %(description)s)"""

    for (fa_descr, synonyms, description) in annotations:
        if len(synonyms) == 0 and len(description) == 0:
            continue

        seq_id = q.dbw.fetchsingle( q_id, locals())

        if seq_id is None:
            print "No database mapping for '%s'" % fa_descr
            continue

        q.dbw.execute( i, locals())

    q.dbw.commit()
    return

if __name__ == "__main__":

    q = seqq.seqq()
    
    fname = sys.argv[1]
    set_id = int(sys.argv[2])

    ann_iter = parse_file( fname)

    insert_fa_annotate( set_id, ann_iter)
    
    
