-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparse_utils.py
268 lines (184 loc) · 7.71 KB
/
parse_utils.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
import os
import matplotlib.pyplot as plt
import numpy as np
import re
from glob import glob
import pandas as pd
columns = ['file', 'genome', 'method', 'index', 'cache', 'alg', 'split', 'memlim', 'region', 'usertime', 'systemtime', 'cpu_usage', 'memory', 'filesize']
def load_perf_file(file, method=""):
if method == "":
method = os.path.basename(file).split('_')[0]
with open(file, 'r') as f:
s = f.read()
genome = os.path.basename(file).split('__')[1]
region = os.path.basename(file).split('__')[2].split('.perf')[0]
lines = s.split('\n')
usertime = float(lines[1].split(': ')[1])
systemtime = float(lines[2].split(': ')[1])
cpu_usage = float(lines[3].split(': ')[1].split('%')[0])
memory = int(lines[9].split(': ')[1].split('%')[0])
filesize = os.stat(file.replace('.perf', '.log')).st_size
if '_ram' in file:
method = 'ramtools'
if '_lzma' in file:
alg = 'lzma'
elif '_zlib' in file:
alg = 'zlib'
else:
alg = ''
split = '_nosplit' not in file
index = '_index' in file
cache = '_cache' in file
elif '_sam' in file:
method = 'samtools'
alg = split = index = cache = None
else:
raise ValueError(method)
memlim = 'memlim' in file
basegenome = os.path.splitext(os.path.basename(genome))[0]
if '_' in basegenome:
basegenome = basegenome.split('_')[0]
return [genome, basegenome, method, index, cache, alg, split, memlim, region, usertime, systemtime, cpu_usage, memory, filesize]
def load_perf_folder(folder):
perfs = [load_perf_file(f) for f in glob('{0}/*.perf'.format(folder))]
df = pd.DataFrame(data=perfs, columns=columns)
df['totaltime'] = df['usertime'] + df['systemtime']
df['Speed_MBps'] = (df['filesize']/1024**2) / df['totaltime']
df['mem_MB'] = df['memory']/(1024)
df['size_MB'] = (df['filesize']/(1024**2)).round(2)
df = df.sort_values(['genome', 'method', 'index', 'cache', 'alg', 'split', 'memlim', 'region'])
return df
def load_perf_superfolders(folders):
df = pd.DataFrame(columns=columns)
for folder in folders:
df = pd.concat([df]+[load_perf_folder(subfolder) for subfolder in glob('{0}/*'.format(folder))], ignore_index=True)
df = df.sort_values(['genome', 'method', 'index', 'cache', 'alg', 'split', 'memlim'])
return df
def get_metric(df, column, regions):
return np.array([df[df['region'] == r][column].values for r in regions])
def compare_metrics(df, methods, regions, column, save=False, relative=None, log=False):
plt.figure(figsize=(25, 6))
dfs = [df[df['method'] == m] for m in methods]
metrics = [get_metric(d, column, regions) for d in dfs]
x = np.arange(len(regions))
N = len(methods) + 1
for i, (m, method) in enumerate(zip(metrics, methods)):
if relative is None:
plt.bar(x+1/N*i, m, width=1/N, label=method)
else:
plt.bar(x+1/N*i, m/metrics[relative], width=1/N, label=method)
# print(method, m)
plt.xticks(x, regions, rotation=15)
plt.title(column, fontsize=25)
plt.legend(fontsize=16)
if log:
plt.yscale('log')
if save:
plt.savefig("images/{0}.png".format(column), format='png')
def load_samtoram_perf(file, method=''):
with open(file, 'r') as f:
s = f.read()
lines = s.split('\n')
if "Command exited" in lines[0]:
lines = lines[1:]
if method == '':
method = os.path.basename(lines[0].split(', ')[1]).strip(' "')
usertime = float(lines[1].split(': ')[1])
systemtime = float(lines[2].split(': ')[1])
cpu_usage = float(lines[3].split(': ')[1].split('%')[0])
memory = int(lines[9].split(': ')[1].split('%')[0])
logfile = file.replace('.perf', '.log')
with open(logfile, 'r') as f:
s = f.read()
lines = s.split('\n')
filesize = int(lines[4].split(' = ')[-1].strip(' *'))
compression = float(lines[5].split(' = ')[-1].strip(' *'))
file = method.split('_')[0]
method = "ramtools_" + method[len(file):].split('.root')[0]
return [file, method, usertime, systemtime, cpu_usage, memory, filesize, compression]
def load_samtobam_perf(file, method=''):
with open(file, 'r') as f:
s = f.read()
lines = s.split('\n')
if "Command exited" in lines[0]:
lines = lines[1:]
if method == '':
method = lines[0].split(' ')[-1].strip(' "').replace('.sam', '.bam')
usertime = float(lines[1].split(': ')[1])
systemtime = float(lines[2].split(': ')[1])
cpu_usage = float(lines[3].split(': ')[1].split('%')[0])
memory = int(lines[9].split(': ')[1].split('%')[0])
logfile = file.replace('.perf', '.log')
with open(logfile, 'r') as f:
s = f.read()
lines = s.split('\n')
filesize = int(lines[1].split('\t')[0].split(': ')[1])
filesize_org = int(lines[10].split('\t')[0].split(': ')[1])
compression = filesize_org / filesize
file = os.path.basename(method).split('.bam')[0]
return [file, 'samtools', usertime, systemtime, cpu_usage, memory, filesize, compression]
def load_bamindex_perf(file, method=''):
with open(file, 'r') as f:
s = f.read()
lines = s.split('\n')
if "Command exited" in lines[0]:
lines = lines[1:]
if method == '':
method = os.path.basename(lines[0].split(' ')[-2])
usertime = float(lines[1].split(': ')[1])
systemtime = float(lines[2].split(': ')[1])
cpu_usage = float(lines[3].split(': ')[1].split('%')[0])
memory = int(lines[9].split(': ')[1].split('%')[0])
logfile = file.replace('.perf', '.log')
with open(logfile, 'r') as f:
s = f.read()
lines = s.split('\n')
filesize = int(lines[1].split('\t')[0].split(': ')[1])
file = method.split('.bam')[0]
return [file, 'samtools', usertime, systemtime, cpu_usage, memory, filesize]
def load_ramindex_perf(file, method=''):
with open(file, 'r') as f:
s = f.read()
lines = s.split('\n')
if "Command exited" in lines[0]:
lines = lines[1:]
if method == '':
method = os.path.basename(lines[0].split('"')[2])
usertime = float(lines[1].split(': ')[1])
systemtime = float(lines[2].split(': ')[1])
cpu_usage = float(lines[3].split(': ')[1].split('%')[0])
memory = int(lines[9].split(': ')[1].split('%')[0])
logfile = file.replace('.perf', '.log')
with open(logfile, 'r') as f:
s = f.read()
lines = s.split('\n')
for line in lines:
if line.startswith('Size'):
filesize = int(line.split('\t')[0].split(': ')[1])
file = method.split('_')[0]
method = "ramtools_" + method[len(file):].split('.root')[0]
return [file, method, usertime, systemtime, cpu_usage, memory, filesize]
def load_samtoram_folder(folder):
perfs = []
for file in glob('{0}/samtoram*.perf'.format(folder)):
print(file)
perfs += [load_samtoram_perf(file)]
for file in glob('{0}/samtobam*.perf'.format(folder)):
print(file)
perfs += [load_samtobam_perf(file)]
columns = ['file', 'method', 'usertime', 'systemtime', 'cpu_usage', 'memory', 'filesize', 'compression']
df = pd.DataFrame(data=perfs, columns=columns)
df['size_GB'] = df['filesize']/(1024**3)
return df
def load_index_folder(folder):
perfs = []
for file in glob('{0}/ramindex*.perf'.format(folder)):
print(file)
perfs += [load_ramindex_perf(file)]
for file in glob('{0}/bamindex*.perf'.format(folder)):
print(file)
perfs += [load_bamindex_perf(file)]
columns = ['file', 'method', 'usertime', 'systemtime', 'cpu_usage', 'memory', 'filesize']
df = pd.DataFrame(data=perfs, columns=columns)
df['size_GB'] = df['filesize']/(1024**3)
return df