-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathparse.py
246 lines (197 loc) · 5.96 KB
/
parse.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
"""
Parse provides a library of pre-built parse functions for different formats
of solution scattering data. Functions are included which can convert data
stored within the filename into python objects for downstream processing.
Author(s):
Benjamin A. Barad
Alexander M. Wolff
"""
from numpy import load
from scipy import stats
from trace import Trace
from pandas import read_table,DataFrame
import numpy as np
import re
# from table import table
def parse(filename):
"""
A wrapper function which uses the suffix of the filename
to determine which method to call upon for parsing the file.
Parameters:
filename (str): path of file to be analyzed
Returns:
Trace:custom object built to hold a single scattering curve and
associated values
"""
if filename.endswith("tpkl"):
return parse_tpkl_2(filename)
elif filename.endswith("dat"):
return parse_dat(filename)
else:
raise TypeError('scattering data can only be read from the following filetypes *.tpkl, *.dat')
def parse_dat(filename):
"""
A function to parse flat text files, with columns separated by spaces.
Parameters:
filename (str): path of file to be analyzed
Returns:
Trace:custom object built to hold a single scattering curve and
associated values
"""
data = read_table(filename, delimiter=" ", engine='python', skiprows=1, names=['q','I','sigI'])
q = data.q
SA = data.I
sigSA = data.sigI
S = np.empty_like(data.q)
sigS = np.empty_like(data.q)
Nj = np.empty_like(data.q)
# return q,SA,sigSA
# sigS = data.sigI
# S = data.I
# Nj = data.q
return Trace(q, sigS, S, sigSA, SA, Nj)
dt = np.dtype({'names': ['q','S','sigS','SA','sigSA','Nj'],
'formats': ['<f8','<f8','<f8','<f8','<f8','<i8']})
def parse_tpkl_2(filename):
"""
A function to parse custom recarray objects.
Parameters:
filename (str): path of file to be analyzed
Returns:
Trace:custom object built to hold a single scattering curve and
associated values
"""
TPKL_HEADER_BYTES = 279 ### this value could vary...original value
# TPKL_HEADER_BYTES = 290 ### march 2018
with open(filename, "rb") as f:
f.seek(TPKL_HEADER_BYTES)
data = np.fromfile(f, dtype=dt)
d2 = DataFrame.from_records(data)
return Trace(d2.q, d2.sigS, d2.S, d2.sigSA, d2.SA, d2.Nj)
def parse_tpkl(filename):
"""
A function to parse custom recarray objects.
Parameters:
filename (str): path of file to be analyzed
Returns:
Trace:custom object built to hold a single scattering curve and
associated values
"""
TPKL_HEADER_BYTES = 290 ### this value could vary (March 2018)
with open(filename, "rb") as f:
f.seek(TPKL_HEADER_BYTES)
data = np.fromfile(f, dtype=dt)
q = data['q']
sigS = data['sigS']
S = data['S']
sigSA = data['sigSA']
SA = data['SA']
Nj = data['Nj']
return Trace(q, sigS, S, sigSA, SA, Nj)
def parse_tpkl_depreciated(filename):
"""
A function to parse custom recarray objects.
This variation is dependent upon table.py from the Anfinrud Lab
Parameters:
filename (str): path of file to be analyzed
Returns:
Trace:custom object built to hold a single scattering curve and
associated values
"""
data = load(filename)
q = data.q
sigS = data.sigS
S = data.S
sigSA = data.sigSA
SA = data.SA
Nj = data.Nj
return Trace(q, sigS, S, sigSA, SA, Nj)
def unit_sort(text):
"""
A function to sort files when timepoints are encoded within the filename
using common abbreviations.
Parameters:
text (str): filename e.g. (proteinX_100ns)
Returns:
Tuple(int, text): for sorting files
"""
if text.startswith("-"):
return 0, text
elif text.endswith("ns"):
return 1, text
elif text.endswith("us"):
return 2, text
elif text.endswith("ms"):
return 3, text
def atof(text):
"""
Function to test whether a str can be rendered as a float.
Useful when retreiving experimental parameters that were
stored within the filename.
Parameters:
text (str)
Returns:
float or text
"""
try:
retval = float(text)
except ValueError:
retval = text
return retval
def natural_keys(text):
'''
A regex function to help with sorting strings containing numeric and non-numeric chunks.
Parameters:
text (str): e.g. if iterated over this list of strings [a1, a11, a2, a13]
Returns:
int: e.g. would return a list with ints assigning sort position [1, 3, 2, 4]
'''
return [ atof(c) for c in re.split(r'[+-]?([0-9]+(?:[.][0-9]*)?|[.][0-9]+)', text) ]
def tryint(s):
"""
Function to test whether a str can be rendered as a int.
Useful when retreiving experimental parameters that were
stored within the filename.
Parameters:
text (str)
Returns:
int or text
"""
try:
return int(s)
except:
return s
def alphanum_key(s):
"""
Turn a string into a list of string and number chunks.
"z23a" -> ["z", 23, "a"]
Parameters:
text (str)
Returns:
list of str and int values
"""
return [ tryint(c) for c in re.split('([0-9]+)', s) ]
def times_numeric(text):
"""
A function to convert timepoints encoded within the filename
into a float corresponding to the value in nanoseconds.
Parameters:
text (str): e.g. (100us)
Returns:
float: e.g. (100,000)
"""
number = float(text[:-2])
if text.endswith("ns"):
return number
elif text.endswith("us"):
return 1e3*number
elif text.endswith("ms"):
return 1e6*number
else:
print("scale could not be calculated")
# Little stub for testing
if __name__ == "__main__":
from sys import argv
filename = argv[1]
trace = parse(filename)
print(trace)