-
Notifications
You must be signed in to change notification settings - Fork 3
/
run_features.py
186 lines (165 loc) · 9.41 KB
/
run_features.py
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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#This script generates the jobs of the pipeline that will do the analysis on the splitted files.
import glob
import os
import sys
from configobj import ConfigObj
from jinja2 import Template
from Jobber import JobClient
from argparse import ArgumentParser, RawTextHelpFormatter
parser = ArgumentParser(description=__doc__, formatter_class=RawTextHelpFormatter)
parser.add_argument("-v",
"--verbose",
dest="verbose",
action="store_true",
default=False,
help="Be loud!")
parser.add_argument("--config",
dest="config",
required=True,
help="Config file")
parser.add_argument("--group-id",
dest="group_id",
required=True,
help="Group Id")
parser.add_argument("--input-dir",
dest="input_dir",
required=True,
help="Input and output directory")
parser.add_argument("--working-dir",
dest="working_dir",
required=True,
help="Working directory of the pipeline. Required because this file is launched from ~")
def main(options):
settings = ConfigObj(options.config).dict()
working_directory = options.working_dir
index_directory = os.path.join(working_directory, 'index')
output_directory = options.input_dir
pipeline_directory = os.path.dirname(os.path.abspath(__file__))
jobber = JobClient.Jobber()
#We can call "extendGroup" if we want to create jobs into an already existing group. Don't forget to call "endGroup" and "launch"
#after you're done
jobber.extendGroup(options.group_id)
#We assume that each file to be analyzed ends with .seqs. Its important to always distinguish input files from any intermediary
#files in case we need to restart the jobs. We should make the jobs unique to prevent duplication of jobs in case this script
#is run multiple times
template = settings['general']['template']
with open(template) as tmpl:
template = Template(tmpl.read())
contrafold_group = jobber.startGroup({'name': 'CONTRAfold'})
for f in glob.glob(options.input_dir + "/*.result"):
input_name = os.path.splitext(f)[0]
#
# Calculate accessibility of the sites
calculate_contrafold_settings = settings['tasks']['CalculateCONTRAFold']
calculate_contrafold_script = 'scripts/Contrafold.py'
calculate_contrafold_command = """python {script} \\
--coords {coords} \\
--out {output} \\
--genome-dir {genome_dir} \\
--contextLen_L 14 \\
--contextLen_U 0 \\
--context 30 \\
--contrabin {contrabin} \\
-v
"""
#
# If there is template use it for command
#
if settings['general'].get('executer', 'drmaa') == 'drmaa':
#
# Copy files by default to the tmp directory
#
copy_dir = "$TMPDIR"
copy_files = {f: 'input'}
moveback = {'output': os.path.join(output_directory, input_name + ".accessibility")}
calculate_contrafold_command_rendered = template.render(modules=calculate_contrafold_settings.get('modules', None),
command=calculate_contrafold_command,
copy=copy_files,
moveback=moveback,
copydir=copy_dir)
calculate_contrafold_command = str(calculate_contrafold_command_rendered).format(**{'script': os.path.join(pipeline_directory, calculate_contrafold_script),
'coords': 'input',
'output': 'output',
'genome_dir': settings['general']['genome'],
'contrabin': settings['general'].get('contrafold_binary', 'contrafold'),
})
else:
calculate_contrafold_command = str(calculate_contrafold_command).format(**{'script': os.path.join(pipeline_directory, calculate_contrafold_script),
'coords': f,
'output': os.path.join(output_directory, input_name + ".accessibility"),
'genome_dir': settings['general']['genome'],
'contrabin': settings['general'].get('contrafold_binary', 'contrafold'),
})
calculate_contrafold_id = jobber.job(calculate_contrafold_command,
{'name': 'CalculateCONTRAFold',
'uniqueId': True,
'options': [('q', calculate_contrafold_settings.get('queue', 'short.q')),
('l', "membycore=%s" % calculate_contrafold_settings.get('mem_req', '2G'))]
})
jobber.endGroup()
#
# Calulate Flanks composition
flanks_group = jobber.startGroup({'name': 'Flanks'})
for f in glob.glob(options.input_dir + "/*.result"):
input_name = os.path.splitext(f)[0]
calculate_flanks_settings = settings['tasks']['CalculateFlanks']
calculate_flanks_script = 'scripts/Flanks_Composition.py'
calculate_flanks_command = """python {script} \\
--coords {coords} \\
--out {output} \\
--genome-dir {genome_dir} \\
--contextLen 30 \\
-v
"""
#
# If there is template use it for command
#
if settings['general'].get('executer', 'drmaa') == 'drmaa':
#
# Copy files by default to the tmp directory
#
copy_dir = "$TMPDIR"
copy_files = {f: 'input'}
moveback = {'output': os.path.join(output_directory, input_name + ".flanks")}
calculate_flanks_command_rendered = template.render(modules=calculate_flanks_settings.get('modules', None),
command=calculate_flanks_command,
copy=copy_files,
moveback=moveback,
copydir=copy_dir)
calculate_flanks_command = str(calculate_flanks_command_rendered).format(**{'script': os.path.join(pipeline_directory, calculate_flanks_script),
'coords': 'input',
'output': 'output',
'genome_dir': settings['general']['genome'],
})
else:
calculate_flanks_command = str(calculate_flanks_command).format(**{'script': os.path.join(pipeline_directory, calculate_flanks_script),
'coords': f,
'output': os.path.join(output_directory, input_name + ".flanks"),
'genome_dir': settings['general']['genome'],
})
calculate_flanks_id = jobber.job(calculate_flanks_command,
{'name': 'CalculateFlanks',
'uniqueId': True,
'options': [('q', calculate_flanks_settings.get('queue', 'short.q')),
('l', "membycore=%s" % calculate_flanks_settings.get('mem_req', '2G'))]
})
jobber.endGroup()
# We merge the files into our result file after analysis finishes
merge_contrafold_command = "cat {output_dir}/*.accessibility > {cwd}/accessibility.tab".format(output_dir=output_directory,
cwd=working_directory)
merge_contrafold_id = jobber.job(merge_contrafold_command, {'name': "MergeCONTRAfold",
'dependencies': [contrafold_group]})
# We merge the files into our result file after analysis finishes
merge_flanks_command = "cat {output_dir}/*.flanks > {cwd}/flanks.tab".format(output_dir=output_directory,
cwd=working_directory)
merge_flanks_id = jobber.job(merge_flanks_command, {'name': "MergeFlanks",
'dependencies': [flanks_group]})
jobber.endGroup()
jobber.launch(options.group_id)
if __name__ == '__main__':
try:
options = parser.parse_args()
except Exception, e:
parser.print_help()
sys.exit()
main(options)