t3MySQLdb.py 26.6 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
        except:
Jerome Mariette's avatar
Jerome Mariette committed
122
            crdate = str(time.mktime(datetime.date.today().timetuple())).split(".")[0]
Celine Noirot's avatar
Celine Noirot committed
123
124
            if cruser_login: # If the administrator login is specified
                admin_id=self.get_user_id(cruser_login)
125
126
127
128
129
130
131
                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
132
                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) + "')"
133
134
135
136
137
138
139
                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
140
        conn.commit()
141
142
        curs.close()
        conn.close()
Jerome Mariette's avatar
Jerome Mariette committed
143
        if exists: raise ValueError("Impossible to create project {0}, this name is already used!".format(name))
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
        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
166
        Return a list of analysis ids belonging to the project
167
          @param project_id  : the project id
Jerome Mariette's avatar
Jerome Mariette committed
168
          @return            : table of analysis ids
169
170
171
172
173
        """
        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
174
        analysis_ids = []
175
        result_set = curs.fetchall()
Jerome Mariette's avatar
Jerome Mariette committed
176
177
        for analysis_id in result_set:
            analysis_ids.append(analysis_id[0])
178
179
        curs.close()
        conn.close()
Jerome Mariette's avatar
Jerome Mariette committed
180
        return analysis_ids
181

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

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

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

Celine Noirot's avatar
Celine Noirot committed
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
    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]
    
298
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
    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]

331
    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, storage_size=None):
332
333
334
335
336
337
338
339
340
341
342
343
344
345
        """
        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
346
          @param hidden        : is the analysis hidden
347
          @param storage_size  : the size of the run
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
            req += "full_seq_size='" + str(full_seq_size) + "',"
        if description:
            req += "description='" + str(description.replace("'", "\'")) + "',"
        if hidden:
            req += "hidden=" + str(hidden) + ","
        if sequencer:
374
375
376
            req += "sequencer='" + sequencer.replace("'", "\'") + "',"
        if storage_size:
            req += "storage_size='" + str(storage_size) + "',"
377
        req = req[:-1] #remove last comma for WHERE
378
        req += " WHERE uid = '"+ str(run_id) + "'"
379
        curs.execute(req)
Jerome Mariette's avatar
Jerome Mariette committed
380
        conn.commit()
381
382
383
384
        conn.close()

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

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

    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
450
451
    def add_analysis(self, aclass, name, description, cruser_login, date, directory, software, version, params, is_editable, hidden=1, parent_uid=0):

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

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

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

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

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

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

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

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