142aea09192f8e96b19d2a7be3a313859718183c
[unfold-protein.git] / unfold.py
1 #!/usr/bin/env python
2 #
3 # Copyright (C) 2009-2012 W. Trevor King <wking@tremily.us>
4 #
5 # This file is part of unfold_protein.
6 #
7 # unfold_protein is free software: you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by the Free
9 # Software Foundation, either version 3 of the License, or (at your option) any
10 # later version.
11 #
12 # unfold_protein is distributed in the hope that it will be useful, but WITHOUT
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
15 # details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # unfold_protein.  If not, see <http://www.gnu.org/licenses/>.
19
20 """Run a mechanical protein unfolding experiment."""
21
22 import os
23 import os.path
24
25 import numpy as _numpy
26
27 from pyafm.storage import load_afm as _load_afm
28 from pyafm.config import Kelvin as _Kelvin
29 from unfold_protein import __version__
30 from unfold_protein.unfolder import Unfolder
31 from unfold_protein.scan import UnfoldScanner
32 import unfold_protein.config as _config
33
34
35 if __name__ == '__main__':
36     from argparse import ArgumentParser
37
38     parser = ArgumentParser(
39         description=__doc__, version=__version__)
40     parser.add_argument(
41         '-s', '--song',
42         help='Path to a song to play when the experiment is complete')
43     parser.add_argument(
44         '-S', '--no-stepper-tweaks', dest='stepper_tweaks',
45         action='store_const', const=False, default=True,
46         help=("Don't move the stepper except for the initial approach "
47               "and final retraction"))
48
49     args = parser.parse_args()
50
51     unfold_config = _config.UnfoldCycleConfig()
52     unfold_config['approach'] = _config.ApproachConfig()
53     unfold_config['unfold'] = _config.UnfoldConfig()
54     unfold_config['unfold']['distance'] = 900e-9
55     unfold_config['save'] = _config.SaveConfig()
56     scan_config = _config.ScanConfig()
57     scan_config['velocity'] = _config.VelocityScanConfig()
58     #scan_config['velocity']['unfolding velocities'] = _numpy.array([1e-6])
59     scan_config['velocity']['num loops'] = 500
60     scan_config['position'] = _config.PositionScanConfig()
61
62     devices = []
63     try:
64         afm = _load_afm()
65         afm.load_from_config(devices=devices)
66         afm.piezo.zero()
67         unfolder = Unfolder(config=unfold_config, afm=afm)
68         scanner = UnfoldScanner(config=scan_config, unfolder=unfolder)
69         scanner.run(stepper_tweaks=args.stepper_tweaks)
70     finally:
71         afm.move_away_from_surface()
72         afm.piezo.zero()
73         for device in devices:
74             device.close()
75         if args.song:
76             song = os.path.abspath(os.path.expanduser(args.song))
77             os.system("aplay '%s'" % song)