t3MySQLdb.py 26.5 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
#
# 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

Celine Noirot's avatar
Celine Noirot committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
    #--------------------------------------------------------------------
    # User functions
    
    def get_user_id(self, login):
        """
            Select the user id from login
            @return               : tthe user id
            @param login          : the login name
        """
        conn = connect(self.host, self.user, self.passwd, self.db)
        curs = conn.cursor()
        req = "SELECT uid FROM fe_users WHERE username = '" + str(login) + "'"
        curs.execute(req)
        try:
            id = curs.fetchall()[0][0]
        except:
            raise Exception('t3MySQLdb', 'The login ' + login + ' does not exist.')
        curs.close()
        conn.close()
        return id
    
    def get_users (self, project_id, right):   
        """
            Select the users ids for a project and a level of rigth
            @return               : [user ids]
            @param project_id     : project id
            @param rigth          : right (string)
        """
        conn = connect(self.host, self.user, self.passwd, self.db)
        curs = conn.cursor()

        req = "SELECT r.fe_user_id FROM fe_rights_levels l , fe_rights r  WHERE \
        l.right_level_label = '" + right + "' AND \
        r.project_id=" + str(project_id) + " AND \
        l.right_level_id = r.right_id;"
        curs.execute(req)
        users_ids=[]
        try:
            result_set = curs.fetchall()
            for user_id in result_set:
                users_ids.append(user_id[0])
            
            logging.getLogger("t3MySQLdb.get_users").debug(str(users_ids))
        except:
            raise Exception('t3MySQLdb', 'Unable to retrieve right for level ' + right + ' and project '+ str(project_id))
        return users_ids
        
101
102
103
    #--------------------------------------------------------------------
    # Project functions

Celine Noirot's avatar
Celine Noirot committed
104
    def add_project(self, name, pdescription, cruser_login, hidden=1):
105
106
107
108
109
        """
        Add to the database a new Project
          @return                     : the Project ID added
          @param name                 : the project name
          @param pdescription         : the project description
Celine Noirot's avatar
Celine Noirot committed
110
          @param cruser_login         : the administrator login
111
112
          @param hidden               : is the project hidden
        """
Jerome Mariette's avatar
Jerome Mariette committed
113
        exists = False
114
115
116
117
118
119
        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
120
            exists = True
121
122
123
        except:
            crdate = time.mktime(datetime.date.today().timetuple())

Celine Noirot's avatar
Celine Noirot committed
124
125
            if cruser_login: # If the administrator login is specified
                admin_id=self.get_user_id(cruser_login)
126
127
128
129
130
131
132
                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.')

Celine Noirot's avatar
Celine Noirot committed
133
                req = "INSERT INTO tx_nG6_project (pid, tstamp, crdate, cruser_id, description, name, hidden) VALUES ('" + str(self.cfg_reader.get_pid()) + "','" + str(crdate) + "','" + str(crdate) + "','" + str(admin_id) +  "','" + pdescription.replace("'", "\'") + "','" + name.replace("'", "\'") + "','" + str(hidden) + "')"
134
135
136
137
138
139
140
                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
141
        conn.commit()
142
143
        curs.close()
        conn.close()
Jerome Mariette's avatar
Jerome Mariette committed
144
        if exists: raise ValueError("Impossible to create project {0}, this name is already used!".format(name))
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
        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
167
        Return a list of analysis ids belonging to the project
168
          @param project_id  : the project id
Jerome Mariette's avatar
Jerome Mariette committed
169
          @return            : table of analysis ids
170
171
172
173
174
        """
        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
175
        analysis_ids = []
176
        result_set = curs.fetchall()
Jerome Mariette's avatar
Jerome Mariette committed
177
178
        for analysis_id in result_set:
            analysis_ids.append(analysis_id[0])
179
180
        curs.close()
        conn.close()
Jerome Mariette's avatar
Jerome Mariette committed
181
        return analysis_ids
182

183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
    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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222

    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,
Celine Noirot's avatar
Celine Noirot committed
223
                type, nb_sequences, full_seq_size, description, sequencer, cruser_login, hidden=1):
224
225
226
227
228
229
230
231
232
233
234
235
        """
        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
Celine Noirot's avatar
Celine Noirot committed
236
          @param cruser_login  : the create user login
Jerome Mariette's avatar
Jerome Mariette committed
237
          @param hidden        : is the analysis hidden
238
        """
Celine Noirot's avatar
Celine Noirot committed
239
240
        cruser_id=self.get_user_id(cruser_login)

241
242
243
        conn = connect(self.host, self.user, self.passwd, self.db)
        curs = conn.cursor()
        crdate = str(time.mktime(datetime.date.today().timetuple())).split(".")[0]
Celine Noirot's avatar
Celine Noirot committed
244
        req = "INSERT INTO tx_nG6_run (pid,tstamp,crdate,cruser_id,name,date,directory,species,data_nature,"
245
        req += "type,nb_sequences,full_seq_size,description,hidden,sequencer) VALUES ('" + str(self.cfg_reader.get_pid())+ "','"
Celine Noirot's avatar
Celine Noirot committed
246
        req += str(crdate) + "'," + "'" + str(crdate) + "'," + str(cruser_id)+ ",'" + name.replace("'", "\'") + "','" + str(time.mktime(date.timetuple())) + "','"
247
248
249
250
251
252
253
254
255
256
        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
257
        conn.commit()
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
        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
277
        conn.commit()
278
279
280
        curs.close()
        conn.close()

Celine Noirot's avatar
Celine Noirot committed
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
    def select_project_id_from_run_id(self, run_id):
        """
        Return the project id
          @param run_id         : the run id to select
          @return: project_id    : the project_id
        """
        logging.getLogger("t3MySQLdb.select_project_id_from_run_id").debug("Selecting run id=" + str(run_id))
        conn = connect(self.host, self.user, self.passwd, self.db)
        curs = conn.cursor()
        req = "SELECT distinct(project_id) FROM tx_nG6_project_run WHERE tx_nG6_project_run.run_id ='" + 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]))
        return res[0]
    
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
    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
347
          @param hidden        : is the analysis hidden
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
        """
        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("'", "\'") + "',"
Penom Nom's avatar
Penom Nom committed
365
        if nb_sequences is not None:
366
            req += "nb_sequences='" + str(nb_sequences) + "',"
Penom Nom's avatar
Penom Nom committed
367
        if full_seq_size is not None:
368
369
370
371
372
373
374
375
            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
376
        req += " WHERE uid = '"+ str(run_id) + "'"
377
        curs.execute(req)
Jerome Mariette's avatar
Jerome Mariette committed
378
        conn.commit()
379
380
381
382
        conn.close()

    def get_run_analysis_ids(self, run_id):
        """
Jerome Mariette's avatar
Jerome Mariette committed
383
        Return a list of analysis ids belonging to the run
384
          @param run_id  : the run id
Jerome Mariette's avatar
Jerome Mariette committed
385
          @return        : table of analysis ids
386
387
388
389
390
        """
        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
391
        analysis_ids = []
392
        result_set = curs.fetchall()
Jerome Mariette's avatar
Jerome Mariette committed
393
394
        for analysis_id in result_set:
            analysis_ids.append(analysis_id[0])
395
396
        curs.close()
        conn.close()
Jerome Mariette's avatar
Jerome Mariette committed
397
        return analysis_ids
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420

    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
421
        conn.commit()
422
423
424
425
426
        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.')
        
427
        return sample_id    
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447

    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

Celine Noirot's avatar
Celine Noirot committed
448
449
    def add_analysis(self, aclass, name, description, cruser_login, date, directory, software, version, params, is_editable, hidden=1, parent_uid=0):

450
451
        """
        Update an Analyze
Celine Noirot's avatar
Celine Noirot committed
452
453
454
455
456
457
458
459
460
461
462
          @param aclass       : the class of the analysis
          @param name         : the analysis name
          @param description  : the analysis description
          @param cruser_login : the user login
          @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
          @param params       : the software params used
          @param hidden       : is the analysis hidden
          @param parent_uid   : the analysis parent uid
463
          @param is_editable : True if analysis has been added by an user
464
        """
Celine Noirot's avatar
Celine Noirot committed
465
        cruser_id=self.get_user_id(cruser_login)
466
467
468
        conn = connect(self.host, self.user, self.passwd, self.db)
        curs = conn.cursor()
        crdate = str(time.mktime(datetime.date.today().timetuple())).split(".")[0]
Celine Noirot's avatar
Celine Noirot committed
469
        
470
471
        if is_editable : analysis_is_editable = 1 
        else : analysis_is_editable = 0      
Celine Noirot's avatar
Celine Noirot committed
472
        req = "INSERT INTO tx_nG6_analyze (pid,tstamp,crdate,cruser_id,class,name,description,date,directory,software,version,"
473
        req += "hidden, params, is_editable, parent_uid) VALUES ('" + str(self.cfg_reader.get_pid())+ "','" + str(crdate) + "','"
Celine Noirot's avatar
Celine Noirot committed
474
        req += str(crdate) + "'," + str(cruser_id) + ",'" + aclass + "','" + name.replace("'", "\'") + "','" + description.replace("'", "\'") + "','"
475
        req += str(crdate) + "','" + directory + "','" + software.replace("'", "\'") + "','"
476
477
        req += version.replace("'", "\'") + "','" + str(hidden) + "','" + params.replace("'", "\'") + "','"
        req += str(analysis_is_editable) + "','"  + str(parent_uid) + "')"
478
        curs.execute(req)
Jerome Mariette's avatar
Jerome Mariette committed
479
        analysis_id = conn.insert_id()
Celine Noirot's avatar
Celine Noirot committed
480

Jerome Mariette's avatar
Jerome Mariette committed
481
        req = "SELECT directory FROM tx_nG6_analyze WHERE uid=" + str(analysis_id)
482
        curs.execute(req)
Jerome Mariette's avatar
Jerome Mariette committed
483
        analysis_directory = curs.fetchone()
Jerome Mariette's avatar
Jerome Mariette committed
484
        conn.commit()
485
486
        curs.close()
        conn.close()
Jerome Mariette's avatar
Jerome Mariette committed
487
488
        if( str(analysis_directory[0]) != str(directory) ):
            raise Exception('t3MySQLdb', 'The id of the analysis ' + name + ' cannot be to retrieve.')
489
        
Jerome Mariette's avatar
Jerome Mariette committed
490
        return analysis_id
491

Jerome Mariette's avatar
Jerome Mariette committed
492
    def add_analysis_to_project(self, project_id, analysis_id):
493
494
        """
        Add to the database a new Analyze done on a project
Jerome Mariette's avatar
Jerome Mariette committed
495
496
          @param project_id  : the project_id the analysis has been done on
          @param analysis_id  : the analysis id
497
498
499
500
501
        """
        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
502
        req += "','" + str(crdate) + "','" + str(project_id) + "','" + str(analysis_id) + "')"
503
        curs.execute(req)
Jerome Mariette's avatar
Jerome Mariette committed
504
        conn.commit()
505
506
507
        curs.close()
        conn.close()

Jerome Mariette's avatar
Jerome Mariette committed
508
    def add_analysis_to_run(self, run_id, analysis_id):
509
510
        """
        Add to the database a new Analyze done on a run
Jerome Mariette's avatar
Jerome Mariette committed
511
512
          @param run_id      : the run_id the analysis has been done on
          @param analysis_id  : the analysis id
513
514
515
516
517
        """
        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
518
        req += "','" + str(crdate) + "','" + str(run_id) + "','" + str(analysis_id) + "')"
519
        curs.execute(req)
Jerome Mariette's avatar
Jerome Mariette committed
520
        conn.commit()
521
522
523
        curs.close()
        conn.close()

Jerome Mariette's avatar
Jerome Mariette committed
524
    def add_result(self, analysis_id, file, key, value, group):
525
        """
Jerome Mariette's avatar
Jerome Mariette committed
526
527
528
        Add to the analysis a new result
          @param analysis_id : the analysis id
          @param file       : the file on which the analysis has been performed
529
530
531
532
533
534
535
536
537
538
          @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
539
        req += "','" + str(crdate) + "','" + str(analysis_id) + "','" + file
540
541
        req += "','" + str(key) + "','" + str(value) + "','" + str(group) + "')"
        curs.execute(req)
Jerome Mariette's avatar
Jerome Mariette committed
542
        conn.commit()
543
544
545
        curs.close()
        conn.close()
        
Jerome Mariette's avatar
Jerome Mariette committed
546
    def update_analysis_field(self, analysis_id, field, value):
547
548
        """
        Update an Analyze
Jerome Mariette's avatar
Jerome Mariette committed
549
          @param analysis_id  : the analysis id
550
551
552
553
554
555
556
          @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
557
        req += "WHERE uid = '"+ str(analysis_id) + "'"
558
        curs.execute(req)
Jerome Mariette's avatar
Jerome Mariette committed
559
        conn.commit()
560
561
562
        curs.close()
        conn.close()

Jerome Mariette's avatar
Jerome Mariette committed
563
    def select_analysis_directory(self, analysis_id):
564
        """
Jerome Mariette's avatar
Jerome Mariette committed
565
566
567
        Return the analysis directory
          @param analysis_id  : the analysis ID
          @return            : the analysis directory
568
569
570
        """
        conn = connect(self.host, self.user, self.passwd, self.db)
        curs = conn.cursor()
Jerome Mariette's avatar
Jerome Mariette committed
571
        req = "SELECT directory FROM tx_nG6_analyze WHERE uid='" + str(analysis_id) + "'"
572
573
574
575
576
577
        curs.execute(req)
        res = curs.fetchall()[0]
        curs.close()
        conn.close()
        return res[0]

Jerome Mariette's avatar
Jerome Mariette committed
578
    def select_analysis_run_id(self, analysis_id):
579
        """
Jerome Mariette's avatar
Jerome Mariette committed
580
581
582
        Return the analysis run id or None
          @param analysis_id  : the analysis ID
          @return            : the run id the analysis belongs to
583
        """
584
585
586
587
588
589
590
591
592
593
594
        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
595

Jerome Mariette's avatar
Jerome Mariette committed
596
    def select_analysis(self, analysis_id):
597
        """
Jerome Mariette's avatar
Jerome Mariette committed
598
599
        Return the analysis infos
          @param analysis_id    : the analysis id to select
600
601
          @return: [name, date, description, software, options, version]
        """
Jerome Mariette's avatar
Jerome Mariette committed
602
        logging.getLogger("t3MySQLdb.select_analysis").debug("Selecting analysis id=" + str(analysis_id))
603
604
        conn = connect(self.host, self.user, self.passwd, self.db)
        curs = conn.cursor()
Jerome Mariette's avatar
Jerome Mariette committed
605
606
        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)
607
608
609
610
        curs.execute(req)
        if curs.rowcount == 0:
            curs.close()
            conn.close()
Jerome Mariette's avatar
Jerome Mariette committed
611
            raise Exception("The analysis id=" + str(analysis_id) + " does not exists in the database.\n")
612
613
614
615
        else:
            res = curs.fetchall()[0]
            curs.close()
            conn.close()
Jerome Mariette's avatar
Jerome Mariette committed
616
            logging.getLogger("t3MySQLdb.select_analysis").debug("Returning [" + str([res[0], res[1], res[2], res[3], res[4], res[5]]) + "]")
617
618
            return [res[0], res[1], res[2], res[3], res[4], res[5]]