annotate commons/core/writer/MySqlTranscriptWriter.py @ 49:80a080dc3ee8

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