Add pysawsim.sawsim module.
authorW. Trevor King <wking@drexel.edu>
Wed, 20 Oct 2010 00:55:27 +0000 (20:55 -0400)
committerW. Trevor King <wking@drexel.edu>
Wed, 20 Oct 2010 00:55:27 +0000 (20:55 -0400)
pysawsim/sawsim.py [new file with mode: 0644]

diff --git a/pysawsim/sawsim.py b/pysawsim/sawsim.py
new file mode 100644 (file)
index 0000000..4b1e377
--- /dev/null
@@ -0,0 +1,64 @@
+# Copyright (C) 2010  W. Trevor King <wking@drexel.edu>
+#
+# 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 <http://www.gnu.org/licenses/>.
+#
+# The author may be contacted at <wking@drexel.edu> 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)