generate_tool_snakefile.py 5.41 KB
Newer Older
1
2
3
4
5
#!/usr/bin/python3
# This script will take an tool and generate snakefile
# Accepts 1 parameter:
# Usage: ./generate_tool_snakefile.py tool_name

mmassaviol's avatar
mmassaviol committed
6
from tools import read_yaml
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
import sys

PATH_OUTPUT = "./"
PATH_INPUT = "./tools/"

def generate(name):

    result = "# File generate with generate_tool_snakefile.py\n\n"

    yaml = read_yaml(PATH_INPUT+name+"/"+name+".yaml")

    result_dir = "config[\"results_dir\"]"

    sample = ""

    if "sample" in yaml: 

        if yaml['sample'] :

            sample = "{sample}_"

    if "commands" in yaml: 

        for cmd in yaml['commands']:

32
            result += "rule <step_name>__" + cmd['name'] + ":\n"
33

34
            rule_dir = "config[\"<step_name>__" + cmd['name'] + "_output_dir\"]"
35
36
37
38
39
40
41
42
43
44
45

            # input 

            if "inputs" in cmd: 

                result += "\n\tinput:\n"

                cmd_inputs = cmd['inputs']

                size_inputs = len(cmd_inputs)

mmassaviol's avatar
mmassaviol committed
46
                #if(size_inputs == 0):
47
                result += "\t\t**<step_name>__" + name + "_inputs(),\n"
48
49
50
51
52

                for input in cmd_inputs:

                    if "name" in input: 

mmassaviol's avatar
mmassaviol committed
53
54
                        # if input['name'] == "reads":
                        #     result += "\t\t**" + name + "_inputs(),\n"
55

mmassaviol's avatar
mmassaviol committed
56
                        if "directory" in input:
57
58
59

                            result += "\t\t" +  input['name'] + " = " + result_dir + " + \"/\"" + " + \"" + input['directory'] + "\",\n" 

mmassaviol's avatar
mmassaviol committed
60
                        if "file" in input:
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

                            result += "\t\t" +  input['name'] + " = " + result_dir + " + \"/\"" + " + \"" + input['file'] + "\",\n" 


                if "options" in cmd:
                    
                    cmd_options = cmd['options']

                    count_input = 1

                    for option in cmd_options:

                        if "type" in option:

                            if option['type'] == "input_file":

                                if "name" in option:
78
                                    result += "\t\t" + option['name'].replace(cmd["name"]+"_","").replace(name+"_","") + " = " + "config[\"<step_name>__" + option['name'] + "\"],\n"
mmassaviol's avatar
mmassaviol committed
79
                                    #result += "\t\tinput_file_p" + str(count_input) + " = " + "config[\"" + option['name'] + "\"],\n"
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
                                    count_input += 1

            # output

            if "outputs" in cmd: 

                result += "\n\toutput: \n"

                for output in cmd["outputs"]:

                    output_name = ""

                    if "name" in output:
                        output_name = output['name'] + " = "

                    if "file" in output:

                        if "name" in cmd:
                            
mmassaviol's avatar
mmassaviol committed
99
                            result += "\t\t" + output_name + result_dir + " + \"/\" + " + rule_dir + " + \"/" + output['file'] + "\",\n" 
100
101
102
103
104

                    if "directory" in output:

                        if "name" in cmd:

mmassaviol's avatar
mmassaviol committed
105
                            result += "\t\tdirectory(" + result_dir + " + \"/\" + " + rule_dir + " + \"/" + output['directory'] + "\"),\n" 
106
107
108

            # params

mmassaviol's avatar
mmassaviol committed
109
            result += "\n\tparams: \n"
110
111
112
113
114
115
116

            sample_output = ""

            if "sample" in yaml:
                if yaml['sample']:
                    sample_output = "{sample}"

mmassaviol's avatar
mmassaviol committed
117
            result += "\t\toutput_dir = " + result_dir + " + \"/\" + " + rule_dir + "+ \"/" + sample_output + "\",\n"
118

119
120
121
            if "command" in cmd:
                result += "\t\tcommand = config[\"<step_name>__" + cmd['name'] + "_command\"],\n"

122
123
124
125
126
127
            if "options" in cmd:
                
                cmd_options = cmd['options']

                for option in cmd_options:

mmassaviol's avatar
mmassaviol committed
128
129
130
                    if "type" in option and option["type"] != "input_file":

                        if "name" in option:
131

mmassaviol's avatar
mmassaviol committed
132
                            if "_threads" not in option['name']:
133

134
                                result += "\t\t" + option['name'].replace(cmd["name"]+"_","").replace(name+"_","") + " = " + "config[\"<step_name>__"+option['name']+"\"],\n"
135
136
137
138
139
140
141

            # log 

            if "name" in cmd:

                result += "\n\tlog: \n"

142
                result += "\t\t" + result_dir + " + \"/logs/\" + " + rule_dir + " + \"/" + sample + cmd['name'] + "_log.txt\"\n"
143
144
145
146
147
148
149
150
151
152
153

            # threads

            result += "\n\tthreads: "

            count_thread = 0

            for option in cmd_options:

                if "name" in option: 

mmassaviol's avatar
mmassaviol committed
154
                    if "_threads" in option['name']:
155
                        result += "\n\t\tconfig[\"<step_name>__" + option['name'] + "\"]\n"
156
157
158
159
160
161
162
163
                        count_thread += 1

            if count_thread == 0:

                result += "1\n"

            # shell

jlopez's avatar
jlopez committed
164
            if "command" in cmd:
165

jlopez's avatar
jlopez committed
166
                cmd_shell = cmd['command']
167
168
169

                result += "\n\tshell: \n"

mmassaviol's avatar
mmassaviol committed
170
171
                #for shell in cmd_shell:
                #    result += "\t\t\"" + shell + "\"\n"
172
                result += "\t\t\"{params.command}\"\n"
173
174
175
176
177
178
179
180
            

            # End cmd

            result += "\n\n"

        # End loop cmd

181
    #print(result)
jlopez's avatar
jlopez committed
182
183
184
185
186
187

    path_snakefile = PATH_INPUT + "/" + name + "/" + name + ".rule.snakefile"
    f = open(path_snakefile, "w")
    f.write(result)
    f.close()

188
189
190
191
192
193
194
195
196
197
198
199

def main():
    if len(sys.argv) == 2:
        generate(sys.argv[1])
    else:
        exit("""Needs 1 argument: tool_name
Usage: ./generate_tool_snakefile.py tool_name """)


if __name__ == "__main__":
    # execute only if run as a script
    main()