forked from GeneROOT/ramtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools_perf.py
306 lines (218 loc) · 11.5 KB
/
tools_perf.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
"""Usage: tools_perf.py generate [-n NUMBER] [--out OUTFILE] GENOMETABLE
tools_perf.py convert bam [-iI] [--out OUTFOLDER] SAMFILE [BAMFILE]
tools_perf.py convert ram [-iINrT] [-a ALG] [--out OUTFOLDER] SAMFILE ROOTFILE
tools_perf.py index bam [-iI] [--out OUTFOLDER] FILE...
tools_perf.py index ram [-iINS] [--out OUTFOLDER] FILE...
tools_perf.py view bam FILE VIEWS [-iIP] [--out OUTFOLDER] [RANGE]
tools_perf.py view ram FILE VIEWS [-ciINPsT] [--out OUTFOLDER] [RANGE] [--macro MACRO]
tools_perf.py parsetreestats TTREEPERFSTATS...
Preprocessing and postprocessing for evaluating the performance of ramtools functions
Arguments:
GENOMETABLE CSV file with name of genome and ranges for each RNAME
VIEWS CSV file with genome, rname and region
RANGE Range in comma/dash separated value for the experiments to execute
LOGFILE Output of calling the tools_perf run on a set of files
FILE ROOTfile for the provided genome
TTREEPERFSTATS File with TTreePerfStats
Options:
-h --help
-a, --alg ALG Compression algorithm of choice
-c --cache Enable TTreeCache
-i --interactive Interactive mode, prints commands before running them
-I --io Profile IO operations
-m, --macro MACRO Custom ramview macro to crossvalidate
-n, --num NUMBER Amount of records to generate
-N --no-compile Avoid compiling code
-o, --out OUTFILE File to save/append values, defaults to stdin
-P, --parallel Run scripts in parallel
-r --no-index Convert as raw without index
-s --stats Print TTreeStats to file
-S --separate Put index in separate file
-T --no-split Reduce Splitlevel for banches
"""
import os
import random
import sys
from docopt import docopt
import pandas as pd
import subprocess
from datetime import datetime
processes = []
def rangestr2list(s):
return sum(((list(range(*[int(j) + k for k, j in enumerate(i.split('-'))]))
if '-' in i else [int(i)]) for i in s.split(',')), [])
def clear_buffer_cache():
code = subprocess.call(['sudo', 'sh', '-c', "echo 3 > /proc/sys/vm/drop_caches"])
if code == 0:
return
else:
print(code)
sys.exit(1)
def sudo_reauth():
subprocess.call('while true; do sudo -n true; sleep 60; kill -0 "$$" || exit; done 2>/dev/null &', shell=True)
def lauch_and_save_output(cmd, outfile, operation=None, interactive=False):
if operation is None:
operation = cmd
print_timestamp()
print("Executing {0}".format(operation))
if interactive:
manual_check(cmd)
with open(outfile, 'w') as f:
processes.append(subprocess.Popen(cmd, stdout=f))
def manual_check(cmd):
print(" ".join(cmd))
print("\nIs this the command you want yo issue [y/N]")
while(True):
choice = input().lower()
if choice == 'y':
break
elif choice == 'n':
sys.exit(1)
else:
print("Invalid Choice [y/N]")
def print_timestamp():
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print("[{0}] ".format(timestamp), end="")
def wait_for_all(processes):
exit_codes = [p.wait() for p in processes]
return exit_codes
def wrap_root_cmd(cmd):
return ["root.exe", "-q", "-l", "-b"] + cmd
def wrap_time_cmd(cmd, time_logfile):
return ["/usr/bin/time", "-v", "--output={0}".format(time_logfile)] + cmd
def wrap_io_cmd(cmd, io_logfile):
return ['strace', '-o', io_logfile, '-TC'] + cmd
if __name__ == '__main__':
arguments = docopt(__doc__)
compilation_flag = '+' if not arguments['--no-compile'] else ''
if arguments['generate']:
N = int(arguments['--num']) if arguments['--num'] else 10
outfile = arguments['--out'] if arguments['--out'] else None
offset = 0
if outfile is not None:
if not os.path.isfile(outfile):
with open(outfile, 'w') as f:
print(",rname,start,end", file=f)
else:
df = pd.read_csv(outfile)
offset = df.index.max() + 1
outfile = open(outfile, 'a') if outfile is not None else sys.stdout
table = pd.read_csv(arguments['GENOMETABLE'])
table = table[~table['RNAME'].str.startswith('GL')]
table = table[~table['RNAME'].str.startswith('*')]
for i in range(N):
row = table.ix[random.choice(table.index)]
rname = row['RNAME']
a, b = random.randint(row['beginPOS'], row['endPOS']), random.randint(row['beginPOS'], row['endPOS'])
a, b = min(a, b), max(a, b)
print("{0},{1},{2},{3}".format(i+offset, rname, a, b), file=outfile)
else:
outfolder = arguments['--out'] if arguments['--out'] else '.'
os.makedirs(outfolder, exist_ok=True)
if arguments['convert']:
if arguments['bam']:
samfile = arguments['SAMFILE']
bamfile = arguments['BAMFILE'] if arguments['BAMFILE'] else arguments['SAMFILE'].replace('.sam', '.bam')
sam_basename = os.path.basename(samfile).split('.sam')[0]
logfile = "samtobam_{0}".format(sam_basename)
logfile = os.path.join(outfolder, logfile)
convert_cmd = ['samtools', 'view', '-bS', '-o', bamfile, samfile]
operation = "samtobam from {0} to {1}".format(samfile, bamfile)
elif arguments['ram']:
samtoram_macro = arguments['--macro'] if arguments['--macro'] else "samtoram.C"
samfile = arguments['SAMFILE']
rootfile = arguments['ROOTFILE']
split = "false" if arguments['--no-split'] else "true"
index = "false" if arguments['--no-index'] else "true"
compression = arguments['--alg'] if arguments['--alg'] else "ROOT::kLZMA"
split_name = 'split' if split == 'true' else 'nosplit'
index_name = 'index' if index == 'true' else 'noindex'
compression_name = compression.split('ROOT::k')[1].lower()
sam_basename = os.path.basename(samfile).split('.sam')[0]
logfile = "samtoram_{0}_{1}_{2}_{3}".format(sam_basename, compression_name, split_name, index_name)
logfile = os.path.join(outfolder, logfile)
convert_cmd = ['samtoram.C{5}("{0}", "{1}", {2}, {3}, {4})'.format(samfile, rootfile, index,
split, compression, compilation_flag)]
convert_cmd = wrap_root_cmd(convert_cmd)
operation = "samtoram from {0} to {1}".format(samfile, rootfile)
convert_cmd = wrap_time_cmd(convert_cmd, logfile + '.perf')
if arguments['--io']:
convert_cmd = wrap_io_cmd(convert_cmd, logfile + '.io')
lauch_and_save_output(convert_cmd, logfile + ".log", operation, interactive=arguments['--interactive'])
wait_for_all(processes)
if arguments['bam']:
with open(logfile + ".log", 'w') as f:
print(subprocess.check_output(['stat', bamfile]).decode()[:-1], file=f)
print(subprocess.check_output(['stat', samfile]).decode(), file=f)
if arguments['index']:
sudo_reauth()
for file in arguments['FILE']:
clear_buffer_cache()
file_basename = os.path.basename(file).split('.sam')[0]
logfile = "index__{0}".format(file_basename)
logfile = os.path.join(outfolder, logfile)
if arguments['bam']:
logfile = logfile.replace('index__', 'bamindex__')
index_cmd = ['samtools', 'index', file, file + '.bai']
operation = "samtools index on {0}".format(file)
elif arguments['ram']:
logfile = logfile.replace('index__', 'ramindex__')
index_cmd = ['ramindex.C{1}("{0}")'.format(file, compilation_flag)]
if arguments['--separate']:
index_cmd[-1] = index_cmd[-1][:-1] + ', false)'
index_cmd = wrap_root_cmd(index_cmd)
operation = "ramtools index {0}".format(file)
index_cmd = wrap_time_cmd(index_cmd, logfile + '.perf')
if arguments['--io']:
index_cmd = wrap_io_cmd(index_cmd, logfile + '.io')
lauch_and_save_output(index_cmd, logfile + ".log", operation, interactive=arguments['--interactive'])
wait_for_all(processes)
if arguments['bam']:
with open(logfile + ".log", 'w') as f:
print(subprocess.check_output(['stat', file + '.bai']).decode(), file=f)
elif arguments['view']:
df = pd.read_csv(arguments['VIEWS'])
if arguments['RANGE']:
df = df.ix[rangestr2list(arguments['RANGE'])]
sudo_reauth()
clear_buffer_cache()
for index, row in df.iterrows():
region = "{0}:{1}-{2}".format(row['rname'], row['start'], row['end'])
file = arguments['FILE'][0]
logfile = "OP__{0}__{1}".format(os.path.basename(file), region)
logfile = os.path.join(outfolder, logfile)
if arguments['bam']:
logfile = logfile.replace("OP__", "bamview__")
cmd = ["samtools", "view", file, region]
operation = "samtools view on {0} {1}".format(file, region)
elif arguments['ram']:
logfile = logfile.replace("OP__", "ramview__")
# Options
ramview_macro = arguments['--macro'] if arguments['--macro'] else "ramview.C"
cache = "false" if arguments['--cache'] else "true"
cmd = ['{2}{4}("{0}", "{1}", {3})'.format(file, region, ramview_macro, cache, compilation_flag)]
cmd = wrap_root_cmd(cmd)
if arguments['--stats']:
ttreeperf_file = logfile + '.root'
cmd[-1] = cmd[-1][:-1] + ', true, "{0}")'.format(ttreeperf_file)
operation = "ramtools view on {0} {1}".format(file, region)
if not arguments['--io']:
cmd = wrap_time_cmd(cmd, logfile + '.perf')
if arguments['--io']:
cmd = wrap_io_cmd(cmd, logfile + '.io')
lauch_and_save_output(cmd, logfile + '.log', operation, interactive=arguments['--interactive'])
if not arguments['--parallel']:
wait_for_all(processes)
clear_buffer_cache()
wait_for_all(processes)
elif arguments['parsetreestats']:
for file in arguments['TTREEPERFSTATS']:
if not file.endswith('.root'):
print("{0} does not have root extension, skiping...")
continue
textfile = os.path.splitext(file)[0] + ".treeperf"
imagefile = os.path.splitext(file)[0] + ".png"
parsetreestats_cmd = ['parsetreestats.C+("{0}", true, "{1}")'.format(file, imagefile)]
parsetreestats_cmd = wrap_root_cmd(parsetreestats_cmd)
operation = "parsetreestats on {0}".format(file)
lauch_and_save_output(parsetreestats_cmd, textfile, operation, interactive=arguments['--interactive'])