annotate SMART/Java/Python/mySql/MySqlTranscriptTable.py @ 46:169d364ddd91

Uploaded
author m-zytnicki
date Mon, 30 Sep 2013 03:19:26 -0400
parents 44d5973c188c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
36
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
1 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
2 # Copyright INRA-URGI 2009-2010
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
3 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
4 # This software is governed by the CeCILL license under French law and
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
5 # abiding by the rules of distribution of free software. You can use,
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
6 # modify and/ or redistribute the software under the terms of the CeCILL
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
7 # license as circulated by CEA, CNRS and INRIA at the following URL
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
8 # "http://www.cecill.info".
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
9 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
10 # As a counterpart to the access to the source code and rights to copy,
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
11 # modify and redistribute granted by the license, users are provided only
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
12 # with a limited warranty and the software's author, the holder of the
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
13 # economic rights, and the successive licensors have only limited
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
14 # liability.
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
15 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
16 # In this respect, the user's attention is drawn to the risks associated
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
17 # with loading, using, modifying and/or developing or reproducing the
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
18 # software by the user in light of its specific status of free software,
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
19 # that may mean that it is complicated to manipulate, and that also
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
20 # therefore means that it is reserved for developers and experienced
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
21 # professionals having in-depth computer knowledge. Users are therefore
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
22 # encouraged to load and test the software's suitability as regards their
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
23 # requirements in conditions enabling the security of their systems and/or
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
24 # data to be ensured and, more generally, to use and operate it in the
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
25 # same conditions as regards security.
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
26 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
27 # The fact that you are presently reading this means that you have had
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
28 # knowledge of the CeCILL license and that you accept its terms.
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
29 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
30 import random
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
31 import sys
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
32 from SMART.Java.Python.structure.TranscriptList import TranscriptList
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
33 from SMART.Java.Python.mySql.MySqlExonTable import MySqlExonTable
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
34 from SMART.Java.Python.mySql.MySqlTable import MySqlTable
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
35 from SMART.Java.Python.structure.Transcript import Transcript
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
36 from SMART.Java.Python.misc.Progress import Progress
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
37
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
38 class MySqlTranscriptTable(MySqlTable):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
39 """A table of transcripts in a mySQL database"""
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
40
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
41 def __init__(self, connection, name = None, chromosome = None, verbosity = 0):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
42 if chromosome == None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
43 chromosome = ""
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
44 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
45 chromosome = "_%s" % chromosome
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
46 if name == None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
47 name = "TmpTable_%d" % (random.randint(0, 100000))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
48 name = "%s%s" % (name, chromosome)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
49 super(MySqlTranscriptTable, self).__init__(connection, "%s_transcripts" % name, verbosity)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
50
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
51
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
52 def createTranscriptTable(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
53 self.create(Transcript.getSqlVariables(), Transcript.getSqlTypes(), Transcript.getSqlSizes())
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
54
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
55
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
56 def rename(self, name):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
57 super(MySqlTranscriptTable, self).rename("%s_transcripts" % name)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
58
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
59
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
60 def remove(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
61 super(MySqlTranscriptTable, self).remove()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
62
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
63
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
64 def clear(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
65 super(MySqlTranscriptTable, self).clear()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
66
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
67
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
68 def copy(self, transcriptTable):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
69 self.remove()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
70 super(MySqlTranscriptTable, self).copy(transcriptTable)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
71
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
72
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
73 def add(self, transcriptTable):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
74 super(MySqlTranscriptTable, self).add(transcriptTable)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
75
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
76
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
77 def addTranscript(self, transcript):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
78 id = self.addLine(transcript.getSqlValues())
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
79 transcript.id = id
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
80
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
81
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
82 def addTranscriptList(self, transcriptList):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
83 progress = Progress(transcriptList.getNbTranscript(), "Storing list to %s" % (self.name), self.verbosity)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
84 for transcript in transcriptList.getIterator():
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
85 self.addTranscript(transcript)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
86 progress.inc()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
87 progress.done()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
88
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
89
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
90 def removeTranscript(self, transcript):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
91 self.removeFromId(transcript.id)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
92
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
93
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
94 def retrieveTranscriptFromId(self, id):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
95 transcript = Transcript()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
96 transcript.setSqlValues(self.retrieveFromId(id))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
97 return transcript
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
98
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
99
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
100 def retrieveBulkTranscriptFromId(self, ids):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
101 if not ids:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
102 return []
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
103 transcripts = self.retrieveBulkFromId(ids)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
104 idsToTranscripts = {}
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
105 for values in transcripts:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
106 transcript = Transcript()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
107 transcript.setSqlValues(values)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
108 idsToTranscripts[values[0]] = transcript
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
109 return idsToTranscripts.values()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
110
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
111
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
112 def selectTranscripts(self, command, simple = False):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
113 MAXSIZE = 100000
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
114 found = True
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
115 cpt = 0
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
116 while found:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
117 found = False
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
118 if simple:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
119 thisCommand = command
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
120 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
121 thisCommand = "%s LIMIT %d OFFSET %d" % (command, MAXSIZE, MAXSIZE * cpt)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
122 query = self.mySqlConnection.executeQuery(thisCommand)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
123 for line in query.getIterator():
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
124 found = True
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
125 id = int(line[0])
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
126 transcript = Transcript()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
127 transcript.setSqlValues(line)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
128 yield (id, transcript)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
129 cpt += 1
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
130 if simple:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
131 return
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
132
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
133
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
134 def getIterator(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
135 for id, transcript in self.selectTranscripts("SELECT * FROM '%s'" % (self.name)):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
136 yield transcript
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
137
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
138
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
139 def retrieveTranscriptList(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
140 transcriptList = TranscriptList()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
141 for transcriptLine in self.getLines():
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
142 transcript = Transcript()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
143 transcript.setSqlValues(transcriptLine)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
144 transcriptList.addTranscript(transcript)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
145 return transcriptList
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
146
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
147
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
148 def setDefaultTagValue(self, name, value):
46
169d364ddd91 Uploaded
m-zytnicki
parents: 36
diff changeset
149 super(MySqlTranscriptTable, self).setDefaultTagValue(Transcript.getSqlVariables().index("tags")+1, name, value)