from Bio.Blast import NCBIWWW
from Bio.Blast import NCBIXML
from Bio import Fasta
from sys import *
import sqlite3

def searchAndStoreBlastSearch(query, matrix_id, filename):
    # Creates handle to store the results sent by NCBI website
    results_handle = NCBIWWW.qblast("blastp", "swissprot", query, expect=10,
                   descriptions=2000, alignments=2000, hitlist_size=2000,
                   matrix_name=matrix_id)
    # Reads results into memory
    blast_results = results_handle.read()

    # Creates and opens a file in filesystem for writing
    save_file = open(filename, 'w')

    # Store results from memory into the filesystem
    save_file.write(blast_results)

    #Close the file handler
    save_file.close()

def xmlToDatabase(filename, matrix_id, dbCursor, dbConn):
    blast_fileptr=open(filename)
    record=NCBIXML.parse(blast_fileptr).next()

    for alignment in record.alignments:
         hsp=alignment.hsps[0]
         storeIntoDatabase(hsp, alignment, matrix_id,dbCursor,dbConn)
    blast_fileptr.close()

def storeIntoDatabase(hsp, alignment, matrix_id, dbCursor, dbConn):
    # Insert a row of data into the table (securely)
    t = (hsp.expect, hsp.score, matrix_id, alignment.accession, alignment.title,)
    dbCursor.execute("insert into sequences values (?,?,?,?,?)",t)
    # Save (commit) the changes
    dbConn.commit()

MatrixName=['PAM70','BLOSUM80']
FileName=['TestPAM70.xml','TestBLOSUM80.xml']
dbFileName='data.db3'

def doIt():
    print "Import fasta file"
    query_file = open('P00624.fasta')
    fasta = Fasta.Iterator(query_file)
    query = fasta.next()
    query_file.close()
    print "Creating database interface"
    dbConnection = sqlite3.connect(dbFileName)
    dbCursor = dbConnection.cursor()

    # Create tables
    dbCursor.execute("DROP TABLE IF EXISTS sequences")

    dbCursor.execute("CREATE TABLE IF NOT EXISTS sequences (expect real, score int, matrix text, accession text, description text)")
    print "Iterate over matrixes"
    for i in range(0,len(MatrixName)):
        print "Sending blast search"
        searchAndStoreBlastSearch(query, MatrixName[i], FileName[i])
        print "Processing blase search"
        xmlToDatabase(FileName[i], MatrixName[i], dbCursor, dbConnection)
    print "Program done"

