# 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/Gene.py
# class Gene(object):
# 
# 
#     def __init__(self, id, seqid, start, end, strand, lTranscripts=[]):
#         """Gene constructor"""
# 
#         self.id = id
#         self.seqid = seqid
#         self.start = start
#         self.end = end
#         self.strand = strand
#         self.lTranscripts = lTranscripts
# 
#     def __eq__(self, other):
#         """Equality on all args"""
#       
#         return ((self.id,self.seqid,self.start,self.end,self.strand, self.lTranscripts) == (other.id, other.seqid, other.start, other.end, other.strand, other.lTranscripts))
# 
#     def __repr__(self):
#         """Gene representation"""
# 
#         return 'Gene: {}-{}-{}-{}-{}-{}'.format(self.id,self.seqid,self.start,self.end,self.strand, self.lTranscripts)
# 
# Path: SDDetector/Entities/Transcript.py
# class Transcript(object):
# 
# 
#     def __init__(self, id, seqid, start, end, strand, gene_id, lCDS=[]):
#         """Transcript constructor"""
# 
#         self.id = id
#         self.seqid = seqid
#         self.start = start
#         self.end = end
#         self.strand = strand
#         self.gene_id = gene_id
#         self.lCDS = lCDS
# 
#     def __eq__(self, other):
#         """Equality on all args"""
#       
#         return ((self.id,self.seqid,self.start,self.end,self.strand,self.lCDS, self.gene_id) == (other.id, other.seqid, other.start, other.end, other.strand, other.lCDS, other.gene_id))
# 
#     def __repr__(self):
#         """Transcript representation"""
# 
#         return 'Transcript: {}-{}-{}-{}-{}-{}-{}'.format(self.id,self.seqid,self.start,self.end,self.strand,self.lCDS, self.gene_id)
# 
# Path: SDDetector/Entities/CDS.py
# class CDS(object):
# 
# 
#     def __init__(self,cds_id, seqid, start, end, strand, transcript_id):
#         """Transcript constructor"""
# 
#         self.cds_id = cds_id
#         self.seqid = seqid
#         self.start = start
#         self.end = end
#         self.strand = strand
#         self.transcript_id = transcript_id
# 
#     def __eq__(self, other):
#         """Equality on all args"""
#       
#         return ((self.cds_id,self.seqid,self.start,self.end,self.strand,self.transcript_id) == (other.cds_id,other.seqid, other.start, other.end, other.strand, other.transcript_id))
# 
#     def __repr__(self):
#         """CDS representation"""
# 
#         return 'CDS: {}'.format(self.cds_id)


import os
import logging
from SDDetector.Db.SqliteDB import SqliteDB
from SDDetector.Entities.Gene import Gene
from SDDetector.Entities.Transcript import Transcript
from SDDetector.Entities.CDS import CDS

        cursor = self.conn.execute('''select cds_id, seqid, start,end,strand,transcript_id from cds order by start''')
        for row in cursor:
            cds = CDS(row[0],row[1],row[2],row[3],row[4],row[5])

            if len(dTranscripts[cds.transcript_id].lCDS) > 0:
                dTranscripts[cds.transcript_id].lCDS.append(cds)
            else:
                dTranscripts[cds.transcript_id].lCDS = [cds]

        return dGenes.values()


    def getlGenesFromCoordinates(self, seqid, start, end):
        """Get genes included in a defined region"""

        lGenes = []
        dGenes = {}
        lTranscripts = []
        dTranscripts = {}
        lCDS = []
        dCDS = {}

        cursor = self.conn.execute('''select id, seqid, start, end, strand from gene where seqid = \'{}\' and start > {} and end < {} order by start'''.format(seqid,start,end)) 
        for row in cursor:        
            dGenes[row[0]] = Gene(row[0],row[1],row[2],row[3],row[4])

        if dGenes:
            cursor = self.conn.execute('''select id, seqid, start,end,strand,gene_id from transcript where seqid = \'{}\' and start > {} and end < {} order by start'''.format(seqid,start,end)) 
            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/Gene.py
# class Gene(object):
# 
# 
#     def __init__(self, id, seqid, start, end, strand, lTranscripts=[]):
#         """Gene constructor"""
# 
#         self.id = id
#         self.seqid = seqid
#         self.start = start
#         self.end = end
#         self.strand = strand
#         self.lTranscripts = lTranscripts
# 
#     def __eq__(self, other):
#         """Equality on all args"""
#       
#         return ((self.id,self.seqid,self.start,self.end,self.strand, self.lTranscripts) == (other.id, other.seqid, other.start, other.end, other.strand, other.lTranscripts))
# 
#     def __repr__(self):
#         """Gene representation"""
# 
#         return 'Gene: {}-{}-{}-{}-{}-{}'.format(self.id,self.seqid,self.start,self.end,self.strand, self.lTranscripts)
# 
# Path: SDDetector/Entities/Transcript.py
# class Transcript(object):
# 
# 
#     def __init__(self, id, seqid, start, end, strand, gene_id, lCDS=[]):
#         """Transcript constructor"""
# 
#         self.id = id
#         self.seqid = seqid
#         self.start = start
#         self.end = end
#         self.strand = strand
#         self.gene_id = gene_id
#         self.lCDS = lCDS
# 
#     def __eq__(self, other):
#         """Equality on all args"""
#       
#         return ((self.id,self.seqid,self.start,self.end,self.strand,self.lCDS, self.gene_id) == (other.id, other.seqid, other.start, other.end, other.strand, other.lCDS, other.gene_id))
# 
#     def __repr__(self):
#         """Transcript representation"""
# 
#         return 'Transcript: {}-{}-{}-{}-{}-{}-{}'.format(self.id,self.seqid,self.start,self.end,self.strand,self.lCDS, self.gene_id)
# 
# Path: SDDetector/Entities/CDS.py
# class CDS(object):
# 
# 
#     def __init__(self,cds_id, seqid, start, end, strand, transcript_id):
#         """Transcript constructor"""
# 
#         self.cds_id = cds_id
#         self.seqid = seqid
#         self.start = start
#         self.end = end
#         self.strand = strand
#         self.transcript_id = transcript_id
# 
#     def __eq__(self, other):
#         """Equality on all args"""
#       
#         return ((self.cds_id,self.seqid,self.start,self.end,self.strand,self.transcript_id) == (other.cds_id,other.seqid, other.start, other.end, other.strand, other.transcript_id))
# 
#     def __repr__(self):
#         """CDS representation"""
# 
#         return 'CDS: {}'.format(self.cds_id)


# Path: SDDetector/Db/GeneDB.py
import os
import logging
from SDDetector.Db.SqliteDB import SqliteDB
from SDDetector.Entities.Gene import Gene
from SDDetector.Entities.Transcript import Transcript
from SDDetector.Entities.CDS import CDS


        cursor = self.conn.execute('''select cds_id, seqid, start,end,strand,transcript_id from cds order by start''')
        for row in cursor:
            cds = CDS(row[0],row[1],row[2],row[3],row[4],row[5])

            if len(dTranscripts[cds.transcript_id].lCDS) > 0:
                dTranscripts[cds.transcript_id].lCDS.append(cds)
            else:
                dTranscripts[cds.transcript_id].lCDS = [cds]

        return dGenes.values()


    def getlGenesFromCoordinates(self, seqid, start, end):
        """Get genes included in a defined region"""

        lGenes = []
        dGenes = {}
        lTranscripts = []
        dTranscripts = {}
        lCDS = []
        dCDS = {}

        cursor = self.conn.execute('''select id, seqid, start, end, strand from gene where seqid = \'{}\' and start > {} and end < {} order by start'''.format(seqid,start,end)) 
        for row in cursor:        
            dGenes[row[0]] = Gene(row[0],row[1],row[2],row[3],row[4])

        if dGenes:
            cursor = self.conn.execute('''select id, seqid, start,end,strand,gene_id from transcript where seqid = \'{}\' and start > {} and end < {} order by start'''.format(seqid,start,end)) 
            for row in cursor:

                transcript = Transcript(row[0],row[1],row[2],row[3],row[4],row[5])