t3MySQLdb.py 24.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#
# Copyright (C) 2009 INRA
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

__author__ = 'Plateforme bioinformatique Midi Pyrenees'
__copyright__ = 'Copyright (C) 2009 INRA'
__license__ = 'GNU General Public License'
__version__ = '1.0'
__email__ = 'support.genopole@toulouse.inra.fr'
__status__ = 'beta'

import datetime, time, logging
try:
    from MySQLdb import *
except:
    print "Import Error: MySQLdb is required to use t3MySQLdb object !"
    
from config_reader import NG6ConfigReader


class t3MySQLdb(object):
    """
    Class t3MySQLdb: in charge to execute common requets on mysql db
    """

    def __init__(self):
        """
        Build a t3MySQLdb
          @param host   : the host where the database is installed
          @param user   : the user name
          @param passwd : the user password
          @param db     : the database name to connect
        """
        self.cfg_reader = NG6ConfigReader()
        [host, user, passwd, db] = self.cfg_reader.get_typo3_db_params()
        self.host = host
        self.user = user
        self.passwd = passwd
        self.db = db

    #--------------------------------------------------------------------
    # Project functions

    def add_project(self, name, pdescription, admin_login, hidden=1):
        """
        Add to the database a new Project
          @return                     : the Project ID added
          @param name                 : the project name
          @param pdescription         : the project description
          @param admin_login          : the administrator login
          @param hidden               : is the project hidden
        """
Jerome Mariette's avatar
Jerome Mariette committed
66
        exists = False
67
68
69
70
71
72
        conn = connect(self.host, self.user, self.passwd, self.db)
        curs = conn.cursor()
        req = "SELECT uid FROM tx_nG6_project WHERE name = '" + str(name) + "'"
        curs.execute(req)
        try:
            project_id = curs.fetchall()[0][0]
Jerome Mariette's avatar
Jerome Mariette committed
73
            exists = True
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
        except:
            crdate = time.mktime(datetime.date.today().timetuple())

            if admin_login: # If the administrator login is specified
                
                req = "SELECT uid FROM fe_users WHERE username = '" + str(admin_login) + "'"
                curs.execute(req)
                try:
                    admin_id = curs.fetchall()[0][0]
                except:
                    raise Exception('t3MySQLdb', 'The administration login ' + admin_login + ' does not exist.')

                req = "SELECT right_level_id FROM fe_rights_levels WHERE right_level_label = 'administrator'"
                curs.execute(req)
                try:
                    right_level_id = curs.fetchall()[0][0]
                except:
                    raise Exception('t3MySQLdb', 'The administration value does not exist in the fe_rights_levels table.')

                req = "INSERT INTO tx_nG6_project (pid, tstamp, crdate, description, name, hidden) VALUES ('" + str(self.cfg_reader.get_pid()) + "','" + str(crdate) + "','" + str(crdate) + "','" + pdescription.replace("'", "\'") + "','" + name.replace("'", "\'") + "','" + str(hidden) + "')"
                curs.execute(req)
                project_id = conn.insert_id()
                req = "INSERT INTO fe_rights (fe_user_id, project_id, right_id) VALUES ('" + str(admin_id) + "','" + str(project_id) + "','" + str(right_level_id) + "')"
                curs.execute(req)
                
            else :
                raise Exception('t3MySQLdb', 'The administration login is required.')
Jerome Mariette's avatar
Jerome Mariette committed
101
        conn.commit()
102
103
        curs.close()
        conn.close()
Jerome Mariette's avatar
Jerome Mariette committed
104
        if exists: raise ValueError("Impossible to create project {0}, this name is already used!".format(name))
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
        return project_id

    def select_project(self, project_id):
        """
        Return the run infos
          @param project_id     : the project id to select
          @return: [name, description]
        """
        logging.getLogger("t3MySQLdb.select_project").debug("Selecting project id=" + str(project_id))
        conn = connect(self.host, self.user, self.passwd, self.db)
        curs = conn.cursor()
        req = "SELECT name, description FROM tx_nG6_project WHERE tx_nG6_project.uid ='" + str(project_id) + "'"
        logging.getLogger("t3MySQLdb.select_project").debug(req)
        curs.execute(req)
        res = curs.fetchall()[0]
        curs.close()
        conn.close()
        logging.getLogger("t3MySQLdb.select_project").debug("Returning [" + str([res[0], res[1]]) + "]")
        return [res[0], res[1]]

    def get_project_analysis_ids(self, project_id):
        """
Jerome Mariette's avatar
Jerome Mariette committed
127
        Return a list of analysis ids belonging to the project
128
          @param project_id  : the project id
Jerome Mariette's avatar
Jerome Mariette committed
129
          @return            : table of analysis ids
130
131
132
133
134
        """
        conn = connect(self.host, self.user, self.passwd, self.db)
        curs = conn.cursor()
        req = "SELECT analyze_id FROM tx_nG6_project_analyze WHERE project_id='" + str(project_id) + "'"
        curs.execute(req)
Jerome Mariette's avatar
Jerome Mariette committed
135
        analysis_ids = []
136
        result_set = curs.fetchall()
Jerome Mariette's avatar
Jerome Mariette committed
137
138
        for analysis_id in result_set:
            analysis_ids.append(analysis_id[0])
139
140
        curs.close()
        conn.close()
Jerome Mariette's avatar
Jerome Mariette committed
141
        return analysis_ids
142

143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
    def select_analysis_project_id(self, analysis_id):
        """
        Return the analysis project id or None
          @param analysis_id : the analysis ID
          @return            : the project id the analysis belongs to
        """
        try:
            conn = connect(self.host, self.user, self.passwd, self.db)
            curs = conn.cursor()
            req = "SELECT project_id FROM tx_nG6_project_analyze WHERE analyze_id='" + str(analysis_id) + "'"
            curs.execute(req)
            res = curs.fetchall()[0]
            curs.close()
            conn.close()
            return res[0]
        except:
            return None
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195

    def get_project_runs_ids(self, project_id):
        """
        Return a list of run ids belonging to the project
          @param project_id  : the project id
          @return            : table of run ids
        """
        conn = connect(self.host, self.user, self.passwd, self.db)
        curs = conn.cursor()
        req = "SELECT run_id FROM tx_nG6_project_run WHERE project_id='" + str(project_id) + "'"
        curs.execute(req)
        run_ids = []
        result_set = curs.fetchall()
        for run_id in result_set:
            run_ids.append(run_id[0])
        curs.close()
        conn.close()
        return run_ids

    #--------------------------------------------------------------------
    # Run functions

    def add_run(self, name, date, directory, species, data_nature,
                type, nb_sequences, full_seq_size, description, sequencer, hidden=1):
        """
        Add to the database a new run
          @param name          : the run name
          @param date          : the run date in datetime format
          @param directory     : the directory where run results are stored
          @param species       : the species used in the run
          @param data_nature   : the data nature used in the run
          @param type          : the run type
          @param nb_sequences  : the number of sequences returned by the sequencing
          @param full_seq_size : the full sequences size in the run
          @param description   : the run description
          @param sequencer     : the sequencer used
Jerome Mariette's avatar
Jerome Mariette committed
196
          @param hidden        : is the analysis hidden
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
        """
        conn = connect(self.host, self.user, self.passwd, self.db)
        curs = conn.cursor()
        crdate = str(time.mktime(datetime.date.today().timetuple())).split(".")[0]
        req = "INSERT INTO tx_nG6_run (pid,tstamp,crdate,name,date,directory,species,data_nature,"
        req += "type,nb_sequences,full_seq_size,description,hidden,sequencer) VALUES ('" + str(self.cfg_reader.get_pid())+ "','"
        req += str(crdate) + "'," + "'" + str(crdate) + "','" + name.replace("'", "\'") + "','" + str(time.mktime(date.timetuple())) + "','"
        req += directory + "','" + species.replace("'", "\'") + "','" + data_nature.replace("'", "\'") + "','" + type.replace("'", "\'") + "','"
        req += str(nb_sequences) + "','" + str(full_seq_size) + "','" + str(description.replace("'", "\'")) + "','" + str(hidden) + "','"
        req += sequencer.replace("'", "\'") + "')"
        curs.execute(req)
        run_id = conn.insert_id()
        
        "Check the run_id"
        req = "SELECT directory FROM tx_nG6_run WHERE uid=" + str(run_id)
        curs.execute(req)
        run_directory = curs.fetchone()
Jerome Mariette's avatar
Jerome Mariette committed
214
        conn.commit()
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
        curs.close()
        conn.close()
        if( str(run_directory[0]) != str(directory) ):
            raise Exception('t3MySQLdb', 'The id of the run ' + name + ' cannot be to retrieve.')
        
        return run_id

    def add_run_to_project(self, project_id, run_id):
        """
        Add to the database a new run
          @param project_id    : the project_id the run belong to
          @param run_id        : the run to add
        """
        conn = connect(self.host, self.user, self.passwd, self.db)
        curs = conn.cursor()
        crdate = str(time.mktime(datetime.date.today().timetuple())).split(".")[0]
        req = "INSERT INTO tx_nG6_project_run (pid, tstamp, crdate, project_id, run_id) VALUES('" + str(self.cfg_reader.get_pid()) + "','" + str(crdate)
        req += "','" + str(crdate) + "','" + str(project_id) + "','" + str(run_id) + "')"
        curs.execute(req)
Jerome Mariette's avatar
Jerome Mariette committed
234
        conn.commit()
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
        curs.close()
        conn.close()

    def select_run(self, run_id):
        """
        Return the run infos
          @param run_id         : the run id to select
          @return: [name, date, species, data_nature, type, description, sequencer]
        """
        logging.getLogger("t3MySQLdb.select_run").debug("Selecting run id=" + str(run_id))
        conn = connect(self.host, self.user, self.passwd, self.db)
        curs = conn.cursor()
        req = "SELECT name, date, species, data_nature, type, description, sequencer FROM tx_nG6_run WHERE tx_nG6_run.uid ='" + str(run_id) + "'"
        logging.getLogger("t3MySQLdb.select_run").debug(req)
        curs.execute(req)
        res = curs.fetchall()[0]
        curs.close()
        conn.close()
        logging.getLogger("t3MySQLdb.select_run").debug("Returning [" + str([res[0], res[1], res[2], res[3], res[4], res[5], res[6]]) + "]")
        return [res[0], res[1], res[2], res[3], res[4], res[5], res[6]]

    def select_run_directory(self, run_id):
        """
        Return the run directory
          @param run_id  : the run ID
          @return        : the run directory
        """
        conn = connect(self.host, self.user, self.passwd, self.db)
        curs = conn.cursor()
        req = "SELECT directory FROM tx_nG6_run WHERE uid='" + str(run_id) + "'"
        curs.execute(req)
        res = curs.fetchall()[0]
        curs.close()
        conn.close()
        return res[0]

    def update_run_info(self, run_id, name=None, date=None, directory=None, species=None, data_nature=None, type=None, nb_sequences=None, full_seq_size=None, description=None, sequencer=None, hidden=None):
        """
        Add to the database a new Analyze done on a run
          @param project_id    : the project_id the run belong to
          @param run_id        : the run to add
          @param name          : the run name
          @param date          : the run date in datetime format
          @param directory     : the directory where run results are stored
          @param species       : the species used in the run
          @param data_nature   : the data nature used in the run
          @param type          : the run type
          @param nb_sequences  : the number of sequences returned by the sequencing
          @param full_seq_size : the full sequences size in the run
          @param description   : the run description
          @param sequencer     : the sequencer used
Jerome Mariette's avatar
Jerome Mariette committed
286
          @param hidden        : is the analysis hidden
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
        """
        conn = connect(self.host, self.user, self.passwd, self.db)
        curs = conn.cursor()
        req =  "UPDATE tx_nG6_run "
        req += "SET "
        if name:
            req += "name='" + name.replace("'", "\'") + "',"
        if date:
            req += "date='" + str(time.mktime(date.timetuple())) + "',"
        if directory:
            req += "directory='" + directory + "',"
        if species:
            req += "species='" + species.replace("'", "\'") + "',"
        if data_nature:
            req += "data_nature='" + data_nature.replace("'", "\'") + "',"
        if type:
            req += "type='" + type.replace("'", "\'") + "',"
        if nb_sequences:
            req += "nb_sequences='" + str(nb_sequences) + "',"
        if full_seq_size:
            req += "full_seq_size='" + str(full_seq_size) + "',"
        if description:
            req += "description='" + str(description.replace("'", "\'")) + "',"
        if hidden:
            req += "hidden=" + str(hidden) + ","
        if sequencer:
            req += "sequencer='" + sequencer.replace("'", "\'") + "', "
        req = req[:-1] #remove last comma for WHERE
        req += "WHERE uid = '"+ str(run_id) + "'"
        curs.execute(req)
Jerome Mariette's avatar
Jerome Mariette committed
317
        conn.commit()
318
319
320
321
        conn.close()

    def get_run_analysis_ids(self, run_id):
        """
Jerome Mariette's avatar
Jerome Mariette committed
322
        Return a list of analysis ids belonging to the run
323
          @param run_id  : the run id
Jerome Mariette's avatar
Jerome Mariette committed
324
          @return        : table of analysis ids
325
326
327
328
329
        """
        conn = connect(self.host, self.user, self.passwd, self.db)
        curs = conn.cursor()
        req = "SELECT analyze_id FROM tx_nG6_run_analyze WHERE run_id='" + str(run_id) + "'"
        curs.execute(req)
Jerome Mariette's avatar
Jerome Mariette committed
330
        analysis_ids = []
331
        result_set = curs.fetchall()
Jerome Mariette's avatar
Jerome Mariette committed
332
333
        for analysis_id in result_set:
            analysis_ids.append(analysis_id[0])
334
335
        curs.close()
        conn.close()
Jerome Mariette's avatar
Jerome Mariette committed
336
        return analysis_ids
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359

    def add_sample_to_run(self, run_id, mid, description):
        """
        add a sample to the run
          @param run_id      : the run id
          @param mid         : the mid name
          @param description : the mid description
          @return sample id
        """
        conn = connect(self.host, self.user, self.passwd, self.db)
        curs = conn.cursor()
        crdate = str(time.mktime(datetime.date.today().timetuple())).split(".")[0]
        req = "INSERT INTO tx_nG6_sample (pid,tstamp,run_id,mid,description) "
        req += "VALUES ('" + str(self.cfg_reader.get_pid())+ "','" + str(crdate) + "','"
        req += str(run_id) + "','" + mid.replace("'", "\'") + "','"
        req += description.replace("'", "\'") + "')"
        curs.execute(req)
        sample_id = conn.insert_id()
        
        "Check the sample_id"
        req = "SELECT pid FROM tx_nG6_sample WHERE uid=" + str(sample_id)
        curs.execute(req)
        sample_pid = curs.fetchone()
Jerome Mariette's avatar
Jerome Mariette committed
360
        conn.commit()
361
362
363
364
365
        curs.close()
        conn.close()
        if( str(sample_pid[0]) != str(self.cfg_reader.get_pid()) ):
            raise Exception('t3MySQLdb', 'The id of the sample ' + mid + ' cannot be to retrieve.')
        
366
        return sample_id    
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386

    def select_run_samples(self, run_id):
        """
        select all samples linked to the run
          @param run_id : the run id
          @return a mid description table {"MID1":"desc1", ...}
        """
        conn = connect(self.host, self.user, self.passwd, self.db)
        curs = conn.cursor()
        req = "SELECT mid, description FROM tx_nG6_sample WHERE run_id='" + str(run_id) + "'"
        curs.execute(req)
        mids = {}
        result_set = curs.fetchall()
        for result in result_set:
            mids[result[0]] = result[1]
        return mids

    #--------------------------------------------------------------------
    # Analyze functions

Jerome Mariette's avatar
Jerome Mariette committed
387
    def add_analysis(self, aclass, name, description, date, directory, software, version, params, hidden=1, parent_uid=0):
388
389
        """
        Update an Analyze
Jerome Mariette's avatar
Jerome Mariette committed
390
391
392
393
394
395
396
          @param aclass      : the class of the analysis
          @param name        : the analysis name
          @param description : the analysis description
          @param date        : the analysis date in datetime format
          @param directory   : the directory where analysis results are stored
          @param software    : the software name used for the analysis
          @param version     : the software version used for the analysis
397
          @param params      : the software params used
Jerome Mariette's avatar
Jerome Mariette committed
398
          @param hidden      : is the analysis hidden
399
400
401
402
403
404
405
406
407
408
409
          @param parent_uid  : the analysis parent uid
        """
        conn = connect(self.host, self.user, self.passwd, self.db)
        curs = conn.cursor()
        crdate = str(time.mktime(datetime.date.today().timetuple())).split(".")[0]
        req = "INSERT INTO tx_nG6_analyze (pid,tstamp,crdate,class,name,description,date,directory,software,version,"
        req += "hidden,params,parent_uid) VALUES ('" + str(self.cfg_reader.get_pid())+ "','" + str(crdate) + "','"
        req += str(crdate) + "','" + aclass + "','" + name.replace("'", "\'") + "','" + description.replace("'", "\'") + "','"
        req += str(crdate) + "','" + directory + "','" + software.replace("'", "\'") + "','"
        req += version.replace("'", "\'") + "','" + str(hidden) + "','" + params.replace("'", "\'") + "','" + str(parent_uid) + "')"
        curs.execute(req)
Jerome Mariette's avatar
Jerome Mariette committed
410
        analysis_id = conn.insert_id()
411
        
Jerome Mariette's avatar
Jerome Mariette committed
412
        req = "SELECT directory FROM tx_nG6_analyze WHERE uid=" + str(analysis_id)
413
        curs.execute(req)
Jerome Mariette's avatar
Jerome Mariette committed
414
        analysis_directory = curs.fetchone()
Jerome Mariette's avatar
Jerome Mariette committed
415
        conn.commit()
416
417
        curs.close()
        conn.close()
Jerome Mariette's avatar
Jerome Mariette committed
418
419
        if( str(analysis_directory[0]) != str(directory) ):
            raise Exception('t3MySQLdb', 'The id of the analysis ' + name + ' cannot be to retrieve.')
420
        
Jerome Mariette's avatar
Jerome Mariette committed
421
        return analysis_id
422

Jerome Mariette's avatar
Jerome Mariette committed
423
    def add_analysis_to_project(self, project_id, analysis_id):
424
425
        """
        Add to the database a new Analyze done on a project
Jerome Mariette's avatar
Jerome Mariette committed
426
427
          @param project_id  : the project_id the analysis has been done on
          @param analysis_id  : the analysis id
428
429
430
431
432
        """
        conn = connect(self.host, self.user, self.passwd, self.db)
        curs = conn.cursor()
        crdate = str(time.mktime(datetime.date.today().timetuple())).split(".")[0]
        req = "INSERT INTO tx_nG6_project_analyze (pid, tstamp, crdate, project_id, analyze_id) VALUES('" + str(self.cfg_reader.get_pid()) + "','" + str(crdate)
Jerome Mariette's avatar
Jerome Mariette committed
433
        req += "','" + str(crdate) + "','" + str(project_id) + "','" + str(analysis_id) + "')"
434
        curs.execute(req)
Jerome Mariette's avatar
Jerome Mariette committed
435
        conn.commit()
436
437
438
        curs.close()
        conn.close()

Jerome Mariette's avatar
Jerome Mariette committed
439
    def add_analysis_to_run(self, run_id, analysis_id):
440
441
        """
        Add to the database a new Analyze done on a run
Jerome Mariette's avatar
Jerome Mariette committed
442
443
          @param run_id      : the run_id the analysis has been done on
          @param analysis_id  : the analysis id
444
445
446
447
448
        """
        conn = connect(self.host, self.user, self.passwd, self.db)
        curs = conn.cursor()
        crdate = str(time.mktime(datetime.date.today().timetuple())).split(".")[0]
        req = "INSERT INTO tx_nG6_run_analyze (pid, tstamp, crdate, run_id, analyze_id) VALUES('" + str(self.cfg_reader.get_pid()) + "','" + str(crdate)
Jerome Mariette's avatar
Jerome Mariette committed
449
        req += "','" + str(crdate) + "','" + str(run_id) + "','" + str(analysis_id) + "')"
450
        curs.execute(req)
Jerome Mariette's avatar
Jerome Mariette committed
451
        conn.commit()
452
453
454
        curs.close()
        conn.close()

Jerome Mariette's avatar
Jerome Mariette committed
455
    def add_result(self, analysis_id, file, key, value, group):
456
        """
Jerome Mariette's avatar
Jerome Mariette committed
457
458
459
        Add to the analysis a new result
          @param analysis_id : the analysis id
          @param file       : the file on which the analysis has been performed
460
461
462
463
464
465
466
467
468
469
          @param key        : the result key
          @param value      : the result value
          @param group      : the group value
        """
        # First select the sample
        conn = connect(self.host, self.user, self.passwd, self.db)
        curs = conn.cursor()
        crdate = str(time.mktime(datetime.date.today().timetuple())).split(".")[0]
        req = "INSERT INTO tx_nG6_result (pid, tstamp, crdate, analyze_id, file, rkey, rvalue, rgroup) " 
        req += "VALUES('" + str(self.cfg_reader.get_pid()) + "','" + str(crdate)
Jerome Mariette's avatar
Jerome Mariette committed
470
        req += "','" + str(crdate) + "','" + str(analysis_id) + "','" + file
471
472
        req += "','" + str(key) + "','" + str(value) + "','" + str(group) + "')"
        curs.execute(req)
Jerome Mariette's avatar
Jerome Mariette committed
473
        conn.commit()
474
475
476
        curs.close()
        conn.close()
        
Jerome Mariette's avatar
Jerome Mariette committed
477
    def update_analysis_field(self, analysis_id, field, value):
478
479
        """
        Update an Analyze
Jerome Mariette's avatar
Jerome Mariette committed
480
          @param analysis_id  : the analysis id
481
482
483
484
485
486
487
          @param field       : the field to update
          @param value       : the value
        """
        conn = connect(self.host, self.user, self.passwd, self.db)
        curs = conn.cursor()
        req =  "UPDATE tx_nG6_analyze "
        req += "SET " + field + "='" + str(value).replace("'", "\'") + "' "
Jerome Mariette's avatar
Jerome Mariette committed
488
        req += "WHERE uid = '"+ str(analysis_id) + "'"
489
        curs.execute(req)
Jerome Mariette's avatar
Jerome Mariette committed
490
        conn.commit()
491
492
493
        curs.close()
        conn.close()

Jerome Mariette's avatar
Jerome Mariette committed
494
    def select_analysis_directory(self, analysis_id):
495
        """
Jerome Mariette's avatar
Jerome Mariette committed
496
497
498
        Return the analysis directory
          @param analysis_id  : the analysis ID
          @return            : the analysis directory
499
500
501
        """
        conn = connect(self.host, self.user, self.passwd, self.db)
        curs = conn.cursor()
Jerome Mariette's avatar
Jerome Mariette committed
502
        req = "SELECT directory FROM tx_nG6_analyze WHERE uid='" + str(analysis_id) + "'"
503
504
505
506
507
508
        curs.execute(req)
        res = curs.fetchall()[0]
        curs.close()
        conn.close()
        return res[0]

Jerome Mariette's avatar
Jerome Mariette committed
509
    def select_analysis_run_id(self, analysis_id):
510
        """
Jerome Mariette's avatar
Jerome Mariette committed
511
512
513
        Return the analysis run id or None
          @param analysis_id  : the analysis ID
          @return            : the run id the analysis belongs to
514
        """
515
516
517
518
519
520
521
522
523
524
525
        try:
            conn = connect(self.host, self.user, self.passwd, self.db)
            curs = conn.cursor()
            req = "SELECT run_id FROM tx_nG6_run_analyze WHERE analyze_id='" + str(analysis_id) + "'"
            curs.execute(req)
            res = curs.fetchall()[0]
            curs.close()
            conn.close()
            return res[0]
        except:
            return None
526

Jerome Mariette's avatar
Jerome Mariette committed
527
    def select_analysis(self, analysis_id):
528
        """
Jerome Mariette's avatar
Jerome Mariette committed
529
530
        Return the analysis infos
          @param analysis_id    : the analysis id to select
531
532
          @return: [name, date, description, software, options, version]
        """
Jerome Mariette's avatar
Jerome Mariette committed
533
        logging.getLogger("t3MySQLdb.select_analysis").debug("Selecting analysis id=" + str(analysis_id))
534
535
        conn = connect(self.host, self.user, self.passwd, self.db)
        curs = conn.cursor()
Jerome Mariette's avatar
Jerome Mariette committed
536
537
        req = "SELECT name, date, description, software, params, version FROM tx_nG6_analyze WHERE tx_nG6_analyze.uid ='" + str(analysis_id) + "'"
        logging.getLogger("t3MySQLdb.select_analysis").debug(req)
538
539
540
541
        curs.execute(req)
        if curs.rowcount == 0:
            curs.close()
            conn.close()
Jerome Mariette's avatar
Jerome Mariette committed
542
            raise Exception("The analysis id=" + str(analysis_id) + " does not exists in the database.\n")
543
544
545
546
        else:
            res = curs.fetchall()[0]
            curs.close()
            conn.close()
Jerome Mariette's avatar
Jerome Mariette committed
547
            logging.getLogger("t3MySQLdb.select_analysis").debug("Returning [" + str([res[0], res[1], res[2], res[3], res[4], res[5]]) + "]")
548
549
            return [res[0], res[1], res[2], res[3], res[4], res[5]]

Jerome Mariette's avatar
Jerome Mariette committed
550
    def select_analysis_run_id(self, analysis_id):
551
        """
Jerome Mariette's avatar
Jerome Mariette committed
552
553
        Return the run_id the analysis belongs to
          @param analysis_id    : the analysis id to select
554
555
556
557
          @return: run_id
        """
        conn = connect(self.host, self.user, self.passwd, self.db)
        curs = conn.cursor()
Jerome Mariette's avatar
Jerome Mariette committed
558
559
        try : # If it's a run analysis
            req = "SELECT run_id FROM tx_nG6_run_analyze WHERE tx_nG6_run_analyze.analyze_id='" + str(analysis_id) + "'"
560
561
562
563
564
565
566
567
568
569
            curs.execute(req)
            res = curs.fetchall()[0]
            curs.close()
            conn.close()
            return res[0]
        except : # If not, return None
            curs.close()
            conn.close()
            return None