comparison SMART/Java/Python/mySql/MySqlTranscriptTable.py @ 38:2c0c0a89fad7

Uploaded
author m-zytnicki
date Thu, 02 May 2013 09:56:47 -0400
parents 44d5973c188c
children 169d364ddd91
comparison
equal deleted inserted replaced
37:d22fadc825e3 38:2c0c0a89fad7
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 import sys
32 from SMART.Java.Python.structure.TranscriptList import TranscriptList
33 from SMART.Java.Python.mySql.MySqlExonTable import MySqlExonTable
34 from SMART.Java.Python.mySql.MySqlTable import MySqlTable
35 from SMART.Java.Python.structure.Transcript import Transcript
36 from SMART.Java.Python.misc.Progress import Progress
37
38 class MySqlTranscriptTable(MySqlTable):
39 """A table of transcripts in a mySQL database"""
40
41 def __init__(self, connection, name = None, chromosome = None, verbosity = 0):
42 if chromosome == None:
43 chromosome = ""
44 else:
45 chromosome = "_%s" % chromosome
46 if name == None:
47 name = "TmpTable_%d" % (random.randint(0, 100000))
48 name = "%s%s" % (name, chromosome)
49 super(MySqlTranscriptTable, self).__init__(connection, "%s_transcripts" % name, verbosity)
50
51
52 def createTranscriptTable(self):
53 self.create(Transcript.getSqlVariables(), Transcript.getSqlTypes(), Transcript.getSqlSizes())
54
55
56 def rename(self, name):
57 super(MySqlTranscriptTable, self).rename("%s_transcripts" % name)
58
59
60 def remove(self):
61 super(MySqlTranscriptTable, self).remove()
62
63
64 def clear(self):
65 super(MySqlTranscriptTable, self).clear()
66
67
68 def copy(self, transcriptTable):
69 self.remove()
70 super(MySqlTranscriptTable, self).copy(transcriptTable)
71
72
73 def add(self, transcriptTable):
74 super(MySqlTranscriptTable, self).add(transcriptTable)
75
76
77 def addTranscript(self, transcript):
78 id = self.addLine(transcript.getSqlValues())
79 transcript.id = id
80
81
82 def addTranscriptList(self, transcriptList):
83 progress = Progress(transcriptList.getNbTranscript(), "Storing list to %s" % (self.name), self.verbosity)
84 for transcript in transcriptList.getIterator():
85 self.addTranscript(transcript)
86 progress.inc()
87 progress.done()
88
89
90 def removeTranscript(self, transcript):
91 self.removeFromId(transcript.id)
92
93
94 def retrieveTranscriptFromId(self, id):
95 transcript = Transcript()
96 transcript.setSqlValues(self.retrieveFromId(id))
97 return transcript
98
99
100 def retrieveBulkTranscriptFromId(self, ids):
101 if not ids:
102 return []
103 transcripts = self.retrieveBulkFromId(ids)
104 idsToTranscripts = {}
105 for values in transcripts:
106 transcript = Transcript()
107 transcript.setSqlValues(values)
108 idsToTranscripts[values[0]] = transcript
109 return idsToTranscripts.values()
110
111
112 def selectTranscripts(self, command, simple = False):
113 MAXSIZE = 100000
114 found = True
115 cpt = 0
116 while found:
117 found = False
118 if simple:
119 thisCommand = command
120 else:
121 thisCommand = "%s LIMIT %d OFFSET %d" % (command, MAXSIZE, MAXSIZE * cpt)
122 query = self.mySqlConnection.executeQuery(thisCommand)
123 for line in query.getIterator():
124 found = True
125 id = int(line[0])
126 transcript = Transcript()
127 transcript.setSqlValues(line)
128 yield (id, transcript)
129 cpt += 1
130 if simple:
131 return
132
133
134 def getIterator(self):
135 for id, transcript in self.selectTranscripts("SELECT * FROM '%s'" % (self.name)):
136 yield transcript
137
138
139 def retrieveTranscriptList(self):
140 transcriptList = TranscriptList()
141 for transcriptLine in self.getLines():
142 transcript = Transcript()
143 transcript.setSqlValues(transcriptLine)
144 transcriptList.addTranscript(transcript)
145 return transcriptList
146
147
148 def setDefaultTagValue(self, name, value):
149 super(MySqlTranscriptTable, self).setDefaultTagValue(Transcript.getSqlVariables().index("tags")+1, name, value)