annotate SMART/Java/Python/mySql/MySqlTranscriptTable.py @ 9:1eb55963fe39

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