# 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
        row = cursor.fetchone()
        if row:
            return Alignment(row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], id=row[0])
        else:
            return None

    def selectAllSbjcts(self):
        """Select all sequence name used as subject"""

        cursor = self.conn.execute('''select distinct(sbjct) from alignment order by sbjct ASC''' )

        return [ row[0] for row in cursor ]

    def selectAllQueries(self):
        """Select all sequence name used as query"""

        cursor = self.conn.execute('''select distinct(query) from alignment order by query ASC''' )

        return [ row[0] for row in cursor ]

    def selectAlignmentsWithDefinedSbjctAndQueryOrderBySbjctCoord(self, sbjct, query):
        """Select ordered sub-list of alignments with defined sbjct and query"""

        cursor = self.conn.execute('''select id, query, sbjct, qstart,
                                   qend, sstart, send, length, identities,
                                   qstrand, sstrand from alignment where \
                                   sbjct = \'{}\' and query = \'{}\' \
                                   order by sstart ASC''' \
                                   .format(sbjct,query))


# 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

        row = cursor.fetchone()
        if row:
            return Alignment(row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], id=row[0])
        else:
            return None

    def selectAllSbjcts(self):
        """Select all sequence name used as subject"""

        cursor = self.conn.execute('''select distinct(sbjct) from alignment order by sbjct ASC''' )

        return [ row[0] for row in cursor ]

    def selectAllQueries(self):
        """Select all sequence name used as query"""

        cursor = self.conn.execute('''select distinct(query) from alignment order by query ASC''' )

        return [ row[0] for row in cursor ]

    def selectAlignmentsWithDefinedSbjctAndQueryOrderBySbjctCoord(self, sbjct, query):
        """Select ordered sub-list of alignments with defined sbjct and query"""

        cursor = self.conn.execute('''select id, query, sbjct, qstart,
                                   qend, sstart, send, length, identities,
                                   qstrand, sstrand from alignment where \
                                   sbjct = \'{}\' and query = \'{}\' \
                                   order by sstart ASC''' \
                                   .format(sbjct,query))


        return [ Alignment(row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], id=row[0]) for row in cursor ]