-
Notifications
You must be signed in to change notification settings - Fork 4
/
ooSubprocess.py
229 lines (192 loc) · 7.26 KB
/
ooSubprocess.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/env python
# Author: Duy Tin Truong ([email protected])
# at CIBIO, University of Trento, Italy
import subprocess
import os
import multiprocessing
from multiprocessing.pool import ThreadPool
import sys
import cStringIO
class ooSubprocess:
def __init__(self, tmp_dir='tmp/'):
self.chain_cmds = []
self.tmp_dir = tmp_dir
mkdir(tmp_dir)
def ex(
self,
prog,
args=[],
get_output=False,
get_out_pipe=False,
out_fn=None,
in_pipe=None,
error_pipe=None,
current_dir=None,
verbose=True):
if isinstance(args, str):
args = args.split()
if not isinstance(args, list):
args = [args]
cmd = [prog] + args
print_cmd = 'ooSubprocess: ' + ' '.join(cmd)
if verbose and out_fn and (not get_output):
print_stderr(print_cmd + ' > ' + out_fn)
elif verbose:
print_stderr(print_cmd)
if get_output:
result = subprocess.check_output(
cmd,
cwd=current_dir,
stdin=in_pipe,
stderr=error_pipe)
elif get_out_pipe:
result = subprocess.Popen(
cmd,
stdin=in_pipe,
stderr=error_pipe,
stdout=subprocess.PIPE,
cwd=current_dir).stdout
elif out_fn:
ofile = open(out_fn, 'w') if out_fn else None
result = subprocess.check_call(
cmd,
stdin=in_pipe,
stderr=error_pipe,
stdout=ofile,
cwd=current_dir)
ofile.close()
else:
result = subprocess.check_call(
cmd,
stdin=in_pipe,
stderr=error_pipe,
cwd=current_dir)
return result
def chain(
self,
prog,
args=[],
stop=False,
in_pipe=None,
error_pipe=None,
get_output=False,
get_out_pipe=False,
out_fn=None,
current_dir=None,
verbose=True):
if in_pipe is None and self.chain_cmds != []:
print_stderr(
'Error: the pipeline was not stopped before creating a new one!')
print_stderr('In cach: %s' % (' | '.join(self.chain_cmds)))
exit(1)
if out_fn and stop == False:
print_stderr(
'Error: out_fn (output_file_name) is only specified when stop = True!')
exit(1)
if isinstance(args, str):
args = args.split()
if not isinstance(args, list):
args = [args]
cmd = [prog] + args
print_cmd = ' '.join(cmd)
if out_fn and (not get_output):
print_cmd += ' > ' + out_fn
self.chain_cmds.append(print_cmd)
if stop:
if in_pipe is None:
print_stderr('Error: No input process to create a pipeline!')
exit(1)
if verbose:
print_stderr('ooSubprocess: ' + ' | '.join(self.chain_cmds))
self.chain_cmds = []
if get_output:
result = subprocess.check_output(
cmd,
stdin=in_pipe,
stderr=error_pipe,
cwd=current_dir)
elif get_out_pipe:
result = subprocess.Popen(
cmd,
stdin=in_pipe,
stderr=error_pipe,
stdout=subprocess.PIPE,
cwd=current_dir).stdout
elif out_fn:
ofile = open(out_fn, 'w')
result = subprocess.check_call(
cmd,
stdin=in_pipe,
stderr=error_pipe,
stdout=ofile,
cwd=current_dir)
ofile.close()
else:
result = subprocess.check_call(
cmd,
stdin=in_pipe,
stderr=error_pipe,
cwd=current_dir)
else:
result = subprocess.Popen(
cmd,
stdin=in_pipe,
stderr=error_pipe,
stdout=subprocess.PIPE,
cwd=current_dir).stdout
return result
def ftmp(self, ifn):
return os.path.join(self.tmp_dir, os.path.basename(ifn))
def fdir(dir, ifn):
return os.path.join(dir, os.path.basename(ifn))
def mkdir(dir):
if not os.path.exists(dir):
os.makedirs(dir)
elif not os.path.isdir(dir):
print_stderr('Error: %s is not a directory!' % dir)
exit(1)
def replace_ext(ifn, old_ext, new_ext):
# if not os.path.isfile(ifn):
# print_stderr('Error: file %s does not exist!'%(ifn))
# exit(1)
if ifn[len(ifn) - len(old_ext):] != old_ext:
# print_stderr('Error: the old file extension %s does not match!'%old_ext)
# exit(1)
new_ifn = ifn + new_ext
else:
new_ifn = ifn[:len(ifn) - len(old_ext)] + new_ext
return new_ifn
def splitext(ifn):
base, ext = os.path.splitext(ifn)
base1, ext1 = os.path.splitext(base)
if ext1 in ['.tar', '.fastq', '.fasta', '.sam']:
base = base1
ext = ext1 + ext
return base, ext
def splitext2(ifn):
basename = os.path.basename(ifn)
base, ext = splitext(basename)
return base, ext
def parallelize(func, args, nprocs=1, use_threads=False):
if nprocs > 1:
if use_threads:
pool = ThreadPool(nprocs)
else:
pool = multiprocessing.Pool(nprocs)
results = pool.map(func, args)
pool.close()
pool.join()
else:
results = serialize(func, args)
return results
def serialize(func, args):
results = []
for arg in args:
results.append(func(arg))
return results
def print_stderr(*args):
sys.stderr.write(' '.join(map(str, args)) + '\n')
sys.stderr.flush()
def print_stdout(*args):
sys.stdout.write(' '.join(map(str, args)) + '\n')
sys.stdout.flush()