project.py 5.89 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#
# 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'
22
__email__ = 'support.bioinfo.genotoul@inra.fr'
23
24
25
__status__ = 'beta'

import logging, tempfile
Penom Nom's avatar
Penom Nom committed
26
from configparser import ConfigParser, RawConfigParser
27
28

from ng6.t3MySQLdb import t3MySQLdb
Jerome Mariette's avatar
Jerome Mariette committed
29
from ng6.run import Run
Jerome Mariette's avatar
Jerome Mariette committed
30
from ng6.analysis import Analysis
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


class Project(object):
    """
    Class Project: Define a nG6 Project object
    """

    def __init__(self, name, description, admin_login=None, id=None):
        """
        Build a Project object
          @param name                  : the project name
          @param description           : the project description
          @param admin_login           : the administrator login
          @param id                    : the project id
        """
        if id == None : # If brand new run
            if admin_login != None:
                t3mysql = t3MySQLdb()
                self.id = t3mysql.add_project(name, description, admin_login)
            else:
                raise ValueError("An admin login is required to create a new project!")
        else :
            self.id = id
        self.name = name
        self.description = description
Penom Nom's avatar
Penom Nom committed
56
57
58
59
60
61
        
    def get_name(self):
        """
        Return the project name
        """
        return self.name
62
63
64
65
66
67
68
69

    def get_runs(self):
        """
        Return a table of Run object that belongs to the project
        """
        t3mysql = t3MySQLdb()
        runs = []
        run_ids = t3mysql.get_project_runs_ids(self.id)
Jerome Mariette's avatar
Jerome Mariette committed
70
        # For each analysis
71
72
73
74
        for run_id in run_ids:
            runs.append(Run.get_from_id(run_id))
        return runs

Jerome Mariette's avatar
Jerome Mariette committed
75
    def add_analysis(self, my_analysis):
76
        """
Jerome Mariette's avatar
Jerome Mariette committed
77
78
        Add the analysis to the project
          @param my_analysis  : the analysis to add
79
        """
Jerome Mariette's avatar
Jerome Mariette committed
80
        analysis_id = my_analysis.save()
81
        t3mysql = t3MySQLdb()
Jerome Mariette's avatar
Jerome Mariette committed
82
        t3mysql.add_analysis_to_project(self.id, analysis_id)
83
84
85
86
87
88
89

    def add_run(self, my_run):
        """
        Add and save the run to the project
          @param my_run  : the run to add
        """
        run_id = my_run.save()
90
        my_run.id = run_id
91
92
93
94
95
        t3mysql = t3MySQLdb()
        t3mysql.add_run_to_project(self.id, run_id)

    def get_analysis(self):
        """
Jerome Mariette's avatar
Jerome Mariette committed
96
        Return a table of Analysis object that belongs to the project
97
98
99
        """
        t3mysql = t3MySQLdb()
        analysis = []
Jerome Mariette's avatar
Jerome Mariette committed
100
101
102
103
        analysis_ids = t3mysql.get_project_analysis_ids(self.id)
        # For each analysis
        for analysis_id in analysis_ids:
            analysis.append(Analysis.get_from_id(analysis_id))
104
105
106
107
        return analysis

    def sync(self):
        """
Jerome Mariette's avatar
Jerome Mariette committed
108
        Synchronyze data related analysis belonging to the Project between temporary folder and long term storage folder.
109
110
111
        """
        logging.getLogger("Project.sync").debug("Synchronizing project id=" + str(self.id))
        try :
Jerome Mariette's avatar
Jerome Mariette committed
112
113
            for my_analysis in self.get_analysis():
                my_analysis.sync()
Penom Nom's avatar
Penom Nom committed
114
        except Exception as err:
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
            logging.getLogger("Project.sync").error("Error when synchronizing analysis that belongs to project id=" + str(self.id))
            raise Exception(str(err))

    def get_config_file(self):
        """
        Return a Project config file
          @return : path the the config file
        """
        config = RawConfigParser()
        config.add_section("Project")
        config.set("Project", "project_id", self.id)
        config.set("Project", "name", self.name)
        config.set("Project", "description", self.description)
        
        config_path = tempfile.NamedTemporaryFile(suffix=".cfg").name
        config.write(open(config_path,'w'))
        return config_path
    
133
134
135
136
137
138
139
140
141
142
143
144
145
146
    def is_admin(self, admin_login):
        """
        Return true/false
          @param admin_user: login of the user 
          @return : true is admin_login is in admin user of the project 
        """
        t3mysql = t3MySQLdb()
        user_ids = t3mysql.get_users(self.id,"administrator")
        my_user_id = t3mysql.get_user_id(admin_login)
        if my_user_id in user_ids :
            return True
        else :
            return False
        
147
148
149
150
151
152
153
154
155
156
157
    @staticmethod
    def get_from_id (id):
        """
        Return a project object specified by its id
          @param id : the project id
        """
        try:
            t3mysql = t3MySQLdb()
            [name, description] = t3mysql.select_project(id)
            return Project(name, description, None, id)
        except :
158
            return None
159
    
Penom Nom's avatar
Penom Nom committed
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
    @staticmethod
    def get_from_name (name):
        """
        Return a project object specified by its name
          @param name : the project name
        """
        try:
            t3mysql = t3MySQLdb()
            [id, description] = t3mysql.select_project_from_name(name)
            return Project(name, description, None, id)
        except :
            return None
    
    
    
    
176
177
178
179
180
181
182
183
184
185
186
187
    @staticmethod
    def get_from_run_id (id):
        """
        Return a project object specified by run id
          @param id : the run id
        """
        try:
            t3mysql = t3MySQLdb()
            id = t3mysql.select_project_id_from_run_id(id)
            return Project.get_from_id(id)
        except :
            return None