-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrun_mirza_scan.py
183 lines (165 loc) · 9.03 KB
/
run_mirza_scan.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
"""This script generates the jobs of the pipeline that will do the analysis on the split 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()
thisDir = options.working_dir
pip_dir = 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']
template = os.path.join(pip_dir, "scripts/template.sh")
with open(template) as tmpl:
template = Template(tmpl.read())
scan_group = jobber.startGroup({'name': 'CalculateCoordinates'})
for f in glob.glob(options.input_dir + "/*.fa"):
input_name = os.path.splitext(f)[0]
#
# Calculate seed matches
#
scan_settings = settings['tasks']['ScanWithMIRZA']
scan_script = 'scripts/rg_extract_data_from_mirza_output.py'
scan_command = """{mirza_bin} {expressions} {mrnas} {mirnas} 50 noupdate | python {script} \\
--seqs {seqs} \\
--output {output} \\
--context {context} \\
--threshold {threshold} \\
-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.fa',
settings['general']['seqs']: 'seqs.fa',
os.path.join(options.input_dir, "mirnas.expression"): 'expressions',
settings['general']['motifs']: 'motifs.fa'}
moveback = {'output': input_name + ".mirzascan"}
seed_command_rendered = template.render(modules=scan_settings.get('modules', None),
command=scan_command,
copy=copy_files,
moveback=moveback,
copydir=copy_dir)
scan_command = str(seed_command_rendered).format(**{
'mirza_bin': settings['general']['mirza_binary'],
'mrnas': 'input.fa',
'mirnas': 'motifs.fa',
'expressions': 'expressions',
'script': os.path.join(pip_dir, scan_script),
'seqs': 'seqs.fa',
'output': 'output',
'threshold': scan_settings.get('threshold', 50),
'context': scan_settings.get('context', 50)})
else:
scan_command = str(seed_command_rendered).format(**{
'mirza_bin': settings['general']['mirza_binary'],
'mrnas': f,
'mirnas': settings['general']['motifs'],
'expressions': os.path.join(options.input_dir, "mirnas.expression"),
'script': os.path.join(pip_dir, scan_script),
'seqs': settings['general']['seqs'],
'output': input_name + ".mirzascan",
'threshold': scan_settings.get('threshold', 50),
'context': scan_settings.get('context', 50)})
scan_id = jobber.job(scan_command,
{'name': 'ScanWithMIRZA',
'uniqueId': True,
'options': [('q', scan_settings.get('queue', 'short.q')),
('l', "h_vmem=%s" % scan_settings.get('mem_req', '2G'))]
})
jobber.endGroup()
# We merge the files into our result file after analysis finishes
merge_command = "cat {output_dir}/*.mirzascan > {output_dir}/scan_results.tab".format(output_dir=options.input_dir)
merge_id = jobber.job(merge_command, {'name': "MergeScan",
'dependencies': [scan_group]})
# Filter results
filter_results_settings = settings['tasks']['FilterScan']
filter_results_script = 'scripts/rg_filter_duplicates_from_scan.py'
filter_results_command = """python {script} \\
--coords {input} \\
--output {output} \\
--split-by \"{split_by}\" \\
--index-after-split {index_after_split} \\
-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 = {os.path.join(options.input_dir, "scan_results.tab"): 'input'}
moveback = {'output': os.path.join(options.input_dir, "scan_result.filtered")}
filter_results_command_rendered = template.render(modules=filter_results_settings.get('modules', None),
command=filter_results_command,
copy=copy_files,
moveback=moveback,
copydir=copy_dir)
filter_results_command = str(filter_results_command_rendered).format(**{'script': os.path.join(pip_dir, filter_results_script),
'input': 'input',
'output': 'output',
'index_after_split': settings['general'].get('index_after_split'),
'split_by': settings['general'].get('split_by', "NONE"),
})
else:
filter_results_command = str(filter_results_command).format(**{'script': os.path.join(pip_dir, filter_results_script),
'input': os.path.join(options.input_dir, "scan_results.tab"),
'output': os.path.join(options.input_dir, "scan_result.filtered"),
'index_after_split': settings['general'].get('index_after_split'),
'split_by': settings['general'].get('split_by', "NONE"),
})
filter_results_id = jobber.job(filter_results_command,
{'name': 'FilterScan',
'uniqueId': True,
'dependencies': [merge_id],
'options': [('q', filter_results_settings.get('queue', 'short.q')),
('l', "h_vmem=%s" % filter_results_settings.get('mem_req', '2G'))]
})
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)