annotate commons/core/writer/MySqlTranscriptWriter.py @ 11:2da30502c2f1

Updated CompareOverlappingSmallQuery.xml
author m-zytnicki
date Thu, 14 Mar 2013 05:37:08 -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 os
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
31 import random
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
32 from SMART.Java.Python.mySql.MySqlTable import MySqlTable
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33 from SMART.Java.Python.mySql.MySqlTranscriptTable import MySqlTranscriptTable
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34 from SMART.Java.Python.misc.Progress import Progress
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36 class MySqlTranscriptWriter(object):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38 A class that writes a transcript list into a mySQL table
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39 @ivar name: name of the tables
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40 @type name: string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41 @ivar tables: the tables
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42 @type tables: dict of L{MySqlTranscriptTable<MySqlTranscriptTable>}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 @ivar mySqlConnection: connection to a MySQL database
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44 @type mySqlConnection: class L{MySqlConnection<MySqlConnection>}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 @ivar tmpTranscriptFileHandles: files where transcripts are temporary stored, before copy into database
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46 @type tmpTranscriptFileHandles: dict of file handles
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 @ivar nbTranscriptsByChromosome: number of transcripts written
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48 @type nbTranscriptsByChromosome: dict of int (one for each chromosome)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 @ivar randomNumber: a random number, used for having a unique name for the tables
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50 @type randomNumber: int
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51 @ivar toBeWritten: there exists transcripts to be copied into database
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 @type toBeWritten: bool
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53 @ivar verbosity: verbosity
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54 @type verbosity: int
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58 def __init__(self, connection, name = None, verbosity = 0):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 Constructor
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 @param name: name of the file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 @type name: string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 @param verbosity: verbosity
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 @type verbosity: int
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 self.name = name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 self.verbosity = verbosity
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68 self.tables = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69 self.indices = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 self.tmpTranscriptFileHandles = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71 self.nbTranscriptsByChromosome = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 self.toBeWritten = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 self.randomNumber = random.randint(0, 100000)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74 self.mySqlConnection = connection
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 self.nbTmpFiles = 100
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 self.transcriptValues = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 self.nbTranscriptValues = 1000
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 if self.name != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79 pos = self.name.rfind(os.sep)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 if pos != -1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81 self.name = self.name[pos+1:]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84 def __del__(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 Destructor
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 Possibly write into into database the last transcripts
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 if self.toBeWritten:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 self.write()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93 def addIndex(self, name, values):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 Add an index to the tables
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96 @param name: name of the index
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 @type name: string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 @param values: values to index
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 @type values: list of strings
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 self.indices[name] = values
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 def createTable(self, chromosome):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 Create a table for a chromosome
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 @param chromosome: a chromosome name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 @type chromosome: string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 self.tables[chromosome] = MySqlTranscriptTable(self.mySqlConnection, self.name, chromosome, self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 self.tables[chromosome].createTranscriptTable()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112 for name, values in self.indices.iteritems():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 self.tables[chromosome].createIndex("%s_%s_%d" % (name, chromosome, self.randomNumber), values)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117 def addTranscript(self, transcript):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119 Add a transcript to the list of transcripts to be written
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120 @param transcript: transcript to be written
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 @type transcript: class L{Transcript<Transcript>}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123 chromosome = transcript.getChromosome()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 if chromosome not in self.tables:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125 self.createTable(chromosome)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 self.nbTranscriptsByChromosome[chromosome] = 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127 if chromosome not in self.transcriptValues:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128 self.transcriptValues[chromosome] = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
129
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130 self.transcriptValues[chromosome].append(transcript.getSqlValues())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132 self.nbTranscriptsByChromosome[chromosome] += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133 self.toBeWritten = True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134 if sum([len(transcripts) for transcripts in self.transcriptValues.values()]) > self.nbTranscriptValues:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135 self.write()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
138 def addElement(self, element):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
140 Same as "addTranscript"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
141 @param element: transcript to be written
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
142 @type element: class L{Transcript<Transcript>}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
143 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
144 self.addTranscript(element)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
145
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
146
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
147 # def addTranscriptList(self, transcriptListParser):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
148 # """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
149 # Add a list of transcripts to the transcripts to be written
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
150 # @param transcriptListParser: transcripts to be written
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
151 # @type transcriptListParser: class L{TranscriptListParser<TranscriptListParser>}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
152 # """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
153 # progress = Progress(transcriptListParser.getNbTranscripts(), "Storing %s into database" % (transcriptListParser.fileName), self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
154 # for transcript in transcriptListParser.getIterator():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
155 # self.addTranscript(transcript)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
156 # progress.inc()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
157 # progress.done()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
158
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
159
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
160 def addTranscriptList(self, transcriptListParser):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
161 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
162 Add a list of transcripts to the transcripts to be written
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
163 @param transcriptListParser: transcripts to be written
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
164 @type transcriptListParser: class L{TranscriptListParser<TranscriptListParser>}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
165 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
166 self.transcriptListParser = transcriptListParser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
167 self.mySqlConnection.executeManyQueriesIterator(self)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
168
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
169
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
170 def getIterator(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
171 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
172 Iterator to the SQL commands to insert the list
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
173 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
174 progress = Progress(self.transcriptListParser.getNbTranscripts(), "Storing %s into database" % (self.transcriptListParser.fileName), self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
175 for transcript in self.transcriptListParser.getIterator():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
176 chromosome = transcript.getChromosome()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
177 if chromosome not in self.tables:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
178 self.createTable(chromosome)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
179 self.nbTranscriptsByChromosome[chromosome] = self.nbTranscriptsByChromosome.get(chromosome, 0) + 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
180 values = transcript.getSqlValues()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
181 yield "INSERT INTO '%s' (%s) VALUES (%s)" % (self.tables[chromosome].name, ", ".join(self.tables[chromosome].variables), ", ".join([MySqlTable.formatSql(values[variable], self.tables[chromosome].types[variable], self.tables[chromosome].sizes[variable]) for variable in self.tables[chromosome].variables]))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
182 progress.inc()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
183 progress.done()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
184
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
185
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
186 def write(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
187 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
188 Copy the content of the files into the database
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
189 (May add transcripts to already created databases)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
190 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
191 for chromosome in self.transcriptValues:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
192 if chromosome in self.transcriptValues:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
193 self.tables[chromosome].insertMany(self.transcriptValues[chromosome])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
194 self.transcriptValues = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
195 self.toBeWritten = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
196
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
197
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
198 def getTables(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
199 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
200 Get the tables
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
201 @return: the mySQL tables
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
202 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
203 if self.toBeWritten:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
204 self.write()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
205 return self.tables
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
206
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
207
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
208
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
209 def removeTables(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
210 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
211 Drop the tables
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
212 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
213 for chromosome in self.tables:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
214 self.tables[chromosome].remove()