-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseAimlessLog.py
More file actions
284 lines (225 loc) · 7.31 KB
/
Copy pathparseAimlessLog.py
File metadata and controls
284 lines (225 loc) · 7.31 KB
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
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import os
class batchParseAimlessLog():
# a class to parse a series of AIMLESS log files
# (generated in ccp4i gui). Here the In/I1 intensity
# statistics are retrieved for each dataset within
# the full series
def __init__(self,
numDatasets = 11,
presets = 'CCCT',
autoPlot = False):
self.numDatasets = numDatasets
self.getPresets(type = presets)
lists = {'AvI' : [],
'NMeas' : [],
'full' : []}
for i in range(numDatasets):
dName = '{}{}'.format(self.datasetNamePrefix,i+1)
p = parseAimlessLog(location = self.locations[i],
datasetName = dName,
printText = False)
output =p.getOutput()
lists['AvI'].append(output[0])
lists['NMeas'].append(output[1])
lists['full'].append(output[2])
self.data = lists
if autoPlot is True:
self.plotGraphs()
def plotGraphs(self):
# plot and save all key graphs in one go
self.plotDataByResBin(index = 'NMeas',
saveFig = True)
self.plotDataByResBin(index = 'AvI',
saveFig = True)
self.plotDataByDataset(index = 'NMeas',
saveFig = True,
bin = 'overall')
self.plotDataByDataset(index = 'AvI',
saveFig = True,
bin = 'overall')
def rearrangeDataByBin(self,
bin = 0,
index = 'AvI'):
# the dataList contains a set of dictionaries of data
# from each dataset - output a list of the values per
# dataset for a specific bin
if index not in ('AvI','NMeas'):
print 'Invalid index name'
return
if isinstance(bin,int) is False:
print '"bin" must take integer value'
return
binData = []
for data in self.data['full']:
chosenData = data[index][bin]
binData.append(chosenData)
return binData
def plotDataByResBin(self,
dataset = 'all',
index = 'AvI',
saveFig = False,
axisFontSize = 18,
titleFontSize = 24,
plotType = '.svg'):
# plot graph of metric against resolution bin, for each dataset.
# 'dataset' takes 'all' to plot for all datasets or an integer n
# to plot for dataset n
sns.set_palette(palette = 'hls',
n_colors = self.numDatasets,
desat = .6)
sns.set_context(rc = {"figure.figsize":(10, 10)})
fig = plt.figure()
if dataset != 'all':
title = '{} values per resolution bin: dataset {}'.format(index,dataset+1)
saveName = '{}_dataset-{}{}'.format(index,dataset,plotType)
xData = self.data['full'][dataset]['Dmid']
yData = self.data['full'][dataset][index]
plt.plot(xData,
yData,
label = 'Dataset {}'.format(dataset+1))
else:
for d in range(self.numDatasets):
title = '{} values per resolution bin: all datasets'.format(index)
saveName = '{}_dataset-all{}'.format(index,plotType)
xData = self.data['full'][d]['Dmid']
yData = self.data['full'][d][index]
plt.plot(xData,
yData,
label = 'Dataset {}'.format(d+1))
plt.legend(loc = 'best')
plt.xlabel('Resolution bin centre (Angstroms)',
fontsize = axisFontSize)
plt.ylabel(index,
fontsize = axisFontSize)
fig.suptitle(title,
fontsize = titleFontSize)
if saveFig is False:
plt.show()
else:
fig.savefig(saveName)
def plotDataByDataset(self,
bin = 0,
index = 'AvI',
normalise = False,
normCycle = 1,
saveFig = False,
axisFontSize = 18,
titleFontSize = 24,
plotType = '.svg'):
# plot a specific bin's values for each successive dataset
if bin !='overall':
data = self.rearrangeDataByBin(bin = bin,
index = index)
title = '{} values per dataset: bin {}'.format(index,bin)
saveName = '{}_bin-{}{}'.format(index,bin,plotType)
elif bin == 'overall':
data = self.data[index]
title = '{} overall values per dataset'.format(index)
saveName = '{}_overall{}'.format(index,plotType)
# normalise data if required and allow normalisation
# by non-start values (i.e can normalise every odd
# term by I1 and even term by I2 if normCyle = 2)
if normalise is True:
plotData = []
for d in data:
j = len(plotData) % normCycle
plotData.append(float(d)/data[j])
saveName = saveName.replace(plotType,'_normalise-{}_normCyle-{}{}'.format(normalise,normCycle,plotType))
else:
plotData = data
sns.set_palette(palette = 'hls',
n_colors = self.numDatasets,
desat = .6)
sns.set_context(rc={"figure.figsize":(10, 10)})
fig = plt.figure()
plt.plot(range(1,len(plotData)+1),plotData)
plt.xlabel('Dataset',
fontsize = axisFontSize)
plt.ylabel(index,
fontsize = axisFontSize)
plt.xlim(0,self.numDatasets+1)
fig.suptitle(title,
fontsize = titleFontSize)
if saveFig is False:
plt.show()
else:
fig.savefig(saveName)
def getPresets(self,type='DNA'):
# presets for DIALS processing
if type == 'DNA':
self.locations = ['dataset{}-b/'.format(i+1) for i in range(self.numDatasets)]
self.datasetNamePrefix = 'FROMDIALS'
elif type == 'GH7':
self.locations = ['dataset{}/'.format(i+1) for i in range(self.numDatasets)]
self.datasetNamePrefix = 'FROMDIALS'
elif type == 'CCCT':
self.locations = ['dataset{}/'.format(i+1) for i in range(self.numDatasets)]
self.datasetNamePrefix = 'FROMDIALS'
class parseAimlessLog():
# a class to parse a single specified AIMLESS log file
# (generated within the ccp4i gui). The In/I1 intensity
# information is retrieved for this dataset
def __init__(self,
location = './',
logName = 'aimless-logfile.log',
datasetName = 'FROMDIALS1',
run = True,
printText = True):
self.location = location
self.printText = printText
if logName in os.listdir(self.location):
self.logName = logName
else:
self.logName = self.findLogInDir()
if self.logName is False:
return
self.datasetName = datasetName
if run is True:
self.output = self.parseInFromLog()
def getOutput(self):
# return the output
return self.output
def findLogInDir(self):
# rather than explicitly provide a log file name,
# find a .log file in directory
for file in os.listdir(self.location):
if file.endswith('.log') is True:
return file
print 'No .log file found in location specified'
return False
def parseInFromLog(self):
# parse the output log file to find relevant
# In/I1 statistics for current batch
aimlessLog = open('{}{}'.format(self.location,self.logName),'r')
tableFound = False
readNextLine = False
data = {'NMeas' : [],
'AvI' : [],
'Dmid' : []}
for l in aimlessLog.readlines():
if '$TABLE: Analysis against resolution, {}'.format(self.datasetName) in l:
tableFound = True
continue
if tableFound is True:
if l.split()[0] == 'N':
# start reading next line
readNextLine = True
continue
if readNextLine is True:
if l.split()[0] == '$$':
break
else:
data['NMeas'].append(int(l.split()[8]))
data['AvI'].append(int(l.split()[9]))
data['Dmid'].append(float(l.split()[2]))
aimlessLog.close()
# calculate In metric here
In = np.dot(data['NMeas'],data['AvI'])
totMeas = np.sum(data['NMeas'])
if self.printText is True:
print data
print 'In={} #Meas={}'.format(In,totMeas)
return (In,totMeas,data)