From: W. Trevor King Date: Wed, 20 Oct 2010 00:55:27 +0000 (-0400) Subject: Add pysawsim.sawsim module. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=4680251ce7cd179a7c1e83837f95f19b5c445561;p=sawsim.git Add pysawsim.sawsim module. --- diff --git a/pysawsim/sawsim.py b/pysawsim/sawsim.py new file mode 100644 index 0000000..4b1e377 --- /dev/null +++ b/pysawsim/sawsim.py @@ -0,0 +1,64 @@ +# Copyright (C) 2010 W. Trevor King +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# The author may be contacted at on the Internet, or +# write to Trevor King, Drudge's University, Physics Dept., 3141 Chestnut St., +# Philadelphia PA 19104, USA. + + +"""`sawsim` output parsing utilities. + +* `Event` instances represent domain state transitions. +* `parse()` parses the output of a typical `sawsim` run. +""" + + +from collections import namedtuple + + + +Event = namedtuple( + typename='Event', + field_names=['force', 'intial_state', 'final_state']) + + +def parse(text): + """Parse the output of a `sawsim` run. + + >>> text = '''#Force (N)\\tinitial state\\tFinal state + ... 2.90301e-10\\tfolded\\tunfolded + ... 2.83948e-10\\tfolded\\tunfolded + ... 2.83674e-10\\tfolded\\tunfolded + ... 2.48384e-10\\tfolded\\tunfolded + ... 2.43033e-10\\tfolded\\tunfolded + ... 2.77589e-10\\tfolded\\tunfolded + ... 2.85343e-10\\tfolded\\tunfolded + ... 2.67796e-10\\tfolded\\tunfolded + ... ''' + >>> events = list(parse(text)) + >>> len(events) + 8 + >>> events[0] # doctest: +ELLIPSIS + Event(force=2.9030...e-10, intial_state='folded', final_state='unfolded') + """ + for line in text.splitlines(): + line = line.strip() + if len(line) == 0 or line.startswith('#'): + continue + fields = line.split('\t') + if len(fields) != 3: + raise ValueError(fields) + force,initial_state,final_state = fields + yield Event(float(force), initial_state, final_state)