# Path: SDDetector/Db/SqliteDB.py
# class SqliteDB(object):
# 
#     def __init__(self, dbfile='', logLevel='DEBUG'):
#         
#         self.dbfile = dbfile
# 
#         if logLevel:
#             self.setLoggingLevel(logLevel)
# 
#         if dbfile:
#             self.conn = sqlite3.connect(dbfile)
#         else:
#             dbfile='/tmp/tmpSqlite.db' 
#             self.conn = sqlite3.connect(dbfile)
#             logging.info("SQLiteDB in {}".format(dbfile))
# 
#     def setLoggingLevel(self, logLevel):
#         logging.basicConfig(level=logLevel)
# 
# 
#     def getListOfTables(self):
#         pass
# 
# Path: SDDetector/Entities/Alignment.py
# class Alignment(object):
# 
#     __slots__ = ['query','sbjct','qstart','qend','sstart','send',
#                  'length', 'identities', 'qstrand', 'sstrand','id']
# 
#     def __init__(self, query, sbjct, qstart, qend, sstart, send, length, identities, qstrand, sstrand, id=''):
#         """Alignment constructor"""
# 
#         self.query = query
#         self.sbjct = sbjct
#         self.qstart = qstart
#         self.qend = qend
#         self.sstart = sstart
#         self.send = send
#         self.length = length
#         self.identities = identities
#         self.qstrand = qstrand
#         self.sstrand = sstrand
#         self.id = id
# 
#     def __eq__(self, other):
#         """Equality on all args"""
#       
#         return (self.query,self.sbjct,self.qstart,self.qend,self.sstart,self.send,self.length,self.identities,self.qstrand,self.sstrand, self.id) == (other.query,other.sbjct,other.qstart,other.qend,other.sstart,other.send,other.length,other.identities,other.qstrand,other.sstrand, other.id)
# 
#     def __repr__(self):
#         """representation"""
# 
#         return ('{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}'.format(self.query,self.sbjct,self.qstart,self.qend,self.sstart,self.send,self.length,self.identities,self.qstrand,self.sstrand, self.id))
# 
#     def convertToGff3(self):
#         """Write alignment in gff3 format"""
# 
#         if self.sstrand == 1:
#             strand = '+'
#         else:
#             strand = '-'
# 
#         gff3line = '{}\tSQLiteDB\tmatch\t{}\t{}\t.\t{}\t.\tID=match{};Target={} {} {}\n'.format(self.sbjct,self.sstart,self.send,strand,self.id,self.query,self.qstart,self.qend)
#         return gff3line


import os
from SDDetector.Db.SqliteDB import SqliteDB
from SDDetector.Entities.Alignment import Alignment
    def deletelAlignments(self, lIds):
        """Delete a list of alignments"""

        cursor = self.conn.execute('''delete from alignment where id in ({})''' \
                                   .format(','.join(str(id) for id in lIds)))


    def deleteAllAlignments(self):
        """Delete all alignments"""

        cursor = self.conn.execute('''delete from alignment''')
        self.commit()


    def deleteDB(self):
        """Delete the DB"""

        self.conn.close()
        os.remove(self.dbfile)


    def exportDbToGff3(self, fileName):
        """Export entire db in gff3 match features"""

        with open(fileName,'w') as f:
            cursor = self.conn.execute('''select id, query, sbjct, qstart,
                                          qend, sstart, send, length,identities,
                                          qstrand, sstrand from alignment''')

            for row in cursor :

# Path: SDDetector/Db/SqliteDB.py
# class SqliteDB(object):
# 
#     def __init__(self, dbfile='', logLevel='DEBUG'):
#         
#         self.dbfile = dbfile
# 
#         if logLevel:
#             self.setLoggingLevel(logLevel)
# 
#         if dbfile:
#             self.conn = sqlite3.connect(dbfile)
#         else:
#             dbfile='/tmp/tmpSqlite.db' 
#             self.conn = sqlite3.connect(dbfile)
#             logging.info("SQLiteDB in {}".format(dbfile))
# 
#     def setLoggingLevel(self, logLevel):
#         logging.basicConfig(level=logLevel)
# 
# 
#     def getListOfTables(self):
#         pass
# 
# Path: SDDetector/Entities/Alignment.py
# class Alignment(object):
# 
#     __slots__ = ['query','sbjct','qstart','qend','sstart','send',
#                  'length', 'identities', 'qstrand', 'sstrand','id']
# 
#     def __init__(self, query, sbjct, qstart, qend, sstart, send, length, identities, qstrand, sstrand, id=''):
#         """Alignment constructor"""
# 
#         self.query = query
#         self.sbjct = sbjct
#         self.qstart = qstart
#         self.qend = qend
#         self.sstart = sstart
#         self.send = send
#         self.length = length
#         self.identities = identities
#         self.qstrand = qstrand
#         self.sstrand = sstrand
#         self.id = id
# 
#     def __eq__(self, other):
#         """Equality on all args"""
#       
#         return (self.query,self.sbjct,self.qstart,self.qend,self.sstart,self.send,self.length,self.identities,self.qstrand,self.sstrand, self.id) == (other.query,other.sbjct,other.qstart,other.qend,other.sstart,other.send,other.length,other.identities,other.qstrand,other.sstrand, other.id)
# 
#     def __repr__(self):
#         """representation"""
# 
#         return ('{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}'.format(self.query,self.sbjct,self.qstart,self.qend,self.sstart,self.send,self.length,self.identities,self.qstrand,self.sstrand, self.id))
# 
#     def convertToGff3(self):
#         """Write alignment in gff3 format"""
# 
#         if self.sstrand == 1:
#             strand = '+'
#         else:
#             strand = '-'
# 
#         gff3line = '{}\tSQLiteDB\tmatch\t{}\t{}\t.\t{}\t.\tID=match{};Target={} {} {}\n'.format(self.sbjct,self.sstart,self.send,strand,self.id,self.query,self.qstart,self.qend)
#         return gff3line


# Path: SDDetector/Db/AlignDB.py
import os
from SDDetector.Db.SqliteDB import SqliteDB
from SDDetector.Entities.Alignment import Alignment

    def deletelAlignments(self, lIds):
        """Delete a list of alignments"""

        cursor = self.conn.execute('''delete from alignment where id in ({})''' \
                                   .format(','.join(str(id) for id in lIds)))


    def deleteAllAlignments(self):
        """Delete all alignments"""

        cursor = self.conn.execute('''delete from alignment''')
        self.commit()


    def deleteDB(self):
        """Delete the DB"""

        self.conn.close()
        os.remove(self.dbfile)


    def exportDbToGff3(self, fileName):
        """Export entire db in gff3 match features"""

        with open(fileName,'w') as f:
            cursor = self.conn.execute('''select id, query, sbjct, qstart,
                                          qend, sstart, send, length,identities,
                                          qstrand, sstrand from alignment''')

            for row in cursor :

                f.write(Alignment(row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], id=row[0]).convertToGff3())