| 6 | 1 # | 
|  | 2 # Copyright INRA-URGI 2009-2010 | 
|  | 3 # | 
|  | 4 # This software is governed by the CeCILL license under French law and | 
|  | 5 # abiding by the rules of distribution of free software. You can use, | 
|  | 6 # modify and/ or redistribute the software under the terms of the CeCILL | 
|  | 7 # license as circulated by CEA, CNRS and INRIA at the following URL | 
|  | 8 # "http://www.cecill.info". | 
|  | 9 # | 
|  | 10 # As a counterpart to the access to the source code and rights to copy, | 
|  | 11 # modify and redistribute granted by the license, users are provided only | 
|  | 12 # with a limited warranty and the software's author, the holder of the | 
|  | 13 # economic rights, and the successive licensors have only limited | 
|  | 14 # liability. | 
|  | 15 # | 
|  | 16 # In this respect, the user's attention is drawn to the risks associated | 
|  | 17 # with loading, using, modifying and/or developing or reproducing the | 
|  | 18 # software by the user in light of its specific status of free software, | 
|  | 19 # that may mean that it is complicated to manipulate, and that also | 
|  | 20 # therefore means that it is reserved for developers and experienced | 
|  | 21 # professionals having in-depth computer knowledge. Users are therefore | 
|  | 22 # encouraged to load and test the software's suitability as regards their | 
|  | 23 # requirements in conditions enabling the security of their systems and/or | 
|  | 24 # data to be ensured and, more generally, to use and operate it in the | 
|  | 25 # same conditions as regards security. | 
|  | 26 # | 
|  | 27 # The fact that you are presently reading this means that you have had | 
|  | 28 # knowledge of the CeCILL license and that you accept its terms. | 
|  | 29 # | 
|  | 30 import random | 
|  | 31 from SMART.Java.Python.structure.Interval import Interval | 
|  | 32 from SMART.Java.Python.mySql.MySqlTable import MySqlTable | 
|  | 33 | 
|  | 34 | 
|  | 35 class MySqlExonTable(MySqlTable): | 
|  | 36     """A table of exon in a mySQL database""" | 
|  | 37 | 
|  | 38     def __init__(self, connection, name = None, chromosome = None, verbosity = 0): | 
|  | 39         if chromosome == None: | 
|  | 40             chromosome = "" | 
|  | 41         else: | 
|  | 42             chromosome = "_%s" % chromosome | 
|  | 43         if name == None: | 
|  | 44             name = "TmpTable_%d" % (random.randint(0, 100000)) | 
|  | 45         name = "%s%s_exons" % (name, chromosome) | 
|  | 46         super(MySqlExonTable, self).__init__(connection, name, verbosity) | 
|  | 47 | 
|  | 48 | 
|  | 49     def createExonTable(self): | 
|  | 50         variables = Interval.getSqlVariables() | 
|  | 51         variables.append("transcriptId") | 
|  | 52         types = Interval.getSqlTypes() | 
|  | 53         types["transcriptId"] = "int" | 
|  | 54         sizes = Interval.getSqlSizes() | 
|  | 55         sizes["transcriptId"] = 11 | 
|  | 56         self.create(variables, types, sizes) | 
|  | 57 | 
|  | 58 | 
|  | 59     def rename(self, name): | 
|  | 60         super(MySqlExonTable, self).rename("%s_exons" % name) | 
|  | 61 | 
|  | 62 | 
|  | 63     def addExon(self, exon, transcriptId): | 
|  | 64         values = exon.getSqlValues() | 
|  | 65         values["transcriptId"] = transcriptId | 
|  | 66         id = self.addLine(values) | 
|  | 67         exon.id = id | 
|  | 68 | 
|  | 69 | 
|  | 70     def retrieveExonsFromTranscriptId(self, transcriptId): | 
|  | 71         if not self.created: | 
|  | 72             return [] | 
|  | 73         query = self.mySqlConnection.executeQuery("SELECT * FROM %s WHERE transcriptId = %d" % (self.name, transcriptId)) | 
|  | 74         exons = [] | 
|  | 75         for exonLine in query.getIterator(): | 
|  | 76             exon = Interval() | 
|  | 77             exon.setSqlValues(exonLine) | 
|  | 78             exons.append(exon) | 
|  | 79         return exons | 
|  | 80 | 
|  | 81 | 
|  | 82     def retrieveExonsFromBulkTranscriptIds(self, transcriptIds): | 
|  | 83         if not transcriptIds: | 
|  | 84             return {} | 
|  | 85         if not self.created: | 
|  | 86             return {} | 
|  | 87         exons = dict([(transcriptId, []) for transcriptId in transcriptIds]) | 
|  | 88         query = self.mySqlConnection.executeQuery("SELECT * FROM %s WHERE transcriptId IN (%s)" % (self.name, ", ".join(["%s" % (transcriptId) for transcriptId in transcriptIds]))) | 
|  | 89         for exonLine in query.getIterator(): | 
|  | 90             exon = Interval() | 
|  | 91             exon.setSqlValues(exonLine) | 
|  | 92             exons[exonLine[-1]].append(exon) | 
|  | 93         return exons | 
|  | 94 | 
|  | 95 | 
|  | 96     def removeFromTranscriptId(self, transcriptId): | 
|  | 97         self.mySqlConnection.executeQuery("DELETE FROM %s WHERE transcriptId = %d" % (self.name, transcriptId)) |