__init__.py 4.59 KB
Newer Older
Jerome Mariette's avatar
Jerome Mariette committed
1
2
#
# Copyright (C) 2012 INRA
3
#
Jerome Mariette's avatar
Jerome Mariette committed
4
5
6
7
# 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.
8
#
Jerome Mariette's avatar
Jerome Mariette committed
9
10
11
12
# 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.
13
#
Jerome Mariette's avatar
Jerome Mariette committed
14
15
16
17
18
19
20
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import os
import sys

21
22
from jflow.utils import  display_error_message

23
24
25
from ng6.ng6workflow import BasicNG6Workflow
from ng6.project import Project
from ng6.run import Run
26
from ng6.analysis import Analysis
Jerome Mariette's avatar
Jerome Mariette committed
27

28
class AddAnalysis (BasicNG6Workflow):
29

Penom Nom's avatar
Penom Nom committed
30
31
    def get_description(self):
        return "Add a brand new analysis to a run or a project"
32

Penom Nom's avatar
Penom Nom committed
33
34
35
36
37
    def define_parameters(self, function="process"):
        self.add_input_file_list("data_file", " Which data files should be added to the analysis", required = True)
        self.add_parameter("compression", "How should data be compressed once archived", choices = ['none', 'gz', 'bz2'], default = 'none')
        self.add_parameter("delete", "Should the input data be deleted once the process is over", type = bool , default = False)
        self.add_parameter("archive_name", "Give a name to the final archive", default = 'ng6_archive')
Celine Noirot's avatar
Celine Noirot committed
38
        self.add_parameter("project_id", "The project id the analysis belongs to", type = 'int', rules="Exclude=run_id;RequiredIf?ALL[run_id=None]")
39
        self.add_parameter("run_id", "The run id the run belongs to", type = 'int')
Penom Nom's avatar
Penom Nom committed
40
41
42
43
44
        self.add_parameter("analysis_name", "Give a name to your analysis", required = True)
        self.add_parameter("analysis_description", "Give a description to your analysis", required = True)
        self.add_parameter("analysis_software", "Which software was used for this analysis", required = True)
        self.add_parameter("analysis_software_options", "Which software options were used for this analysis", required = True)
        self.add_parameter("analysis_software_version", "analysis_software_version", required = True)
45
        self.add_parameter("parent_analysis", "The id of an analysis to be used as a parent analysis", type= 'int')
46
47


Jerome Mariette's avatar
Jerome Mariette committed
48
    def process(self):
Penom Nom's avatar
Penom Nom committed
49
50
        self.runobj = None
        self.project = None
Jerome Mariette's avatar
Jerome Mariette committed
51
        # First check if files provided exists
52
        files_to_save = []
Penom Nom's avatar
Penom Nom committed
53
        for file in self.data_file:
Jerome Mariette's avatar
Jerome Mariette committed
54
55
56
            if os.path.isfile(file):
                files_to_save.append(file)
            else:
57
                display_error_message("error: %s file does not exists\n" % (file))
58
59

        # Check if user provided a project or a run
Penom Nom's avatar
Penom Nom committed
60
61
62
        if self.project_id :
            self.project = Project.get_from_id(self.project_id)
            self.metadata.append("project_id="+str(self.project_id))
63
            # if user is not allowed to add data on project (is not admin)
Penom Nom's avatar
Penom Nom committed
64
            if self.project is not None and not self.project.is_admin(self.admin_login):
65
                display_error_message( "The user login '" + self.admin_login + "' is not allowed to add data on project '" + self.project.name + "'.\n" )
Penom Nom's avatar
Penom Nom committed
66
67
68
        elif self.run_id :
            self.runobj = Run.get_from_id(self.run_id)
            self.metadata.append("run_id="+str(self.run_id))
69
        else :
70
            display_error_message("One of run_id or project_id parameter is required!\n")
71

72
        addto = "project" if self.project else "run"
73

74
75
76
77
78
79
80
81
82
        parent = None
        if self.parent_analysis :
            parent = Analysis.get_from_id(self.parent_analysis)
            if self.project :
                analyses_ids = [ o.id for o in self.project.get_analysis()]
            else :
                analyses_ids = [ o.id for o in self.runobj.get_analysis()]
            if parent.id not in analyses_ids :
                    display_error_message("The parent analysis associated with id '%s' does not belong to the %s id '%s' " % (parent.id, addto, self.project_id or self.run_id) )
83
84
85
86
87

        basicanalysis = self.add_component("BasicAnalysis", [files_to_save, self.analysis_name,
                        self.analysis_description, self.analysis_software, self.analysis_software_options,
                        self.analysis_software_version, self.compression, self.delete,
                        self.archive_name], addto=addto, parent = parent)
Penom Nom's avatar
Penom Nom committed
88
89
90
91
92

    def post_process(self):
        if self.runobj:
            self.runobj.sync()
        elif self.project:
Celine Noirot's avatar
Celine Noirot committed
93
            self.project.sync()