#!/usr/bin/python
# Extract Feature Table (FT) from uniprot_sprot.dat
# Add index on "ac" afterwards

import dbcnf,sys
db=dbcnf.db
dbc=db.cursor()
fn=sys.argv[1]
dbc.execute("""CREATE TABLE deep.swissprot_features (id SERIAL PRIMARY KEY,
ac TEXT, key_name TEXT, start_pos INTEGER, end_pos INTEGER, description TEXT)""")
for l in open(fn):
 if l[:2]=='AC': ac=l.split()[1].strip(';')
 if l[:2]=='FT':
  ft=[l[5:13].strip(),l[14:20].strip(),l[21:27].strip(),l[34:].strip()]
  if not len(filter(str,ft))==4: continue
  try: int(ft[1])+int(ft[2])
  except: continue
  dbc.execute("""INSERT INTO deep.swissprot_features (ac,key_name,start_pos,end_pos,description)
  VALUES (%s,%s,%s,%s,%s)""",tuple([ac]+ft))
db.commit()