LoginSignup
8
12

More than 1 year has passed since last update.

pymolをpythonのライブラリとして使う

Last updated at Posted at 2016-03-09

以下の環境で説明する。
Linux: 3.13.0-37-generic #64-Ubuntu

Python 2.7.6

importしたモジュールのパスを表示する

<module name>.path

>>> import pymol
>>> pymol.__path__
['/usr/lib/python2.7/dist-packages/pymol']

biopythonのインストール

[hnishi@hn python_pymol]$ sudo apt-get install python-biopython

モジュールのヘルプの表示

help(<module name>)

>>> import Bio
>>> help(Bio)

pymolの関数を通常のpythonから使う(CUI)

pymol -c: GUI を立ち上げずに、コマンドラインのみで実行する (バッチ処理用)

cui_pymol.py
import pymol
pymol.pymol_argv = ['pymol','-c'] 
pymol.finish_launching()
pymol.cmd.fetch("4m61")

出力結果

[hnishi@hn python_pymol]$ python cui_pymol.py
 PyMOL(TM) Molecular Graphics System, Version 1.7.0.0.
 Copyright (c) Schrodinger, LLC.
 All Rights Reserved.

    Created by Warren L. DeLano, Ph.D.

    PyMOL is user-supported open-source software.  Although some versions
    are freely available, PyMOL is not in the public domain.

    If PyMOL is helpful in your work or study, then please volunteer
    support for our ongoing efforts to create open and affordable scientific
    software by purchasing a PyMOL Maintenance and/or Support subscription.

    More information can be found at "http://www.pymol.org".

    Enter "help" for a list of commands.
    Enter "help <command-name>" for information on a specific command.

 Hit ESC anytime to toggle between text and graphics.

 Command mode. No graphics front end.
 PyMOL: normal program termination.

pymol -qというオプションで、ログの最初に出てくるバージョン情報等を非表示にできる(Quiet)。

pymol起動の際のオプションについては
http://pymolwiki.org/index.php/Command_Line_Options
参照

pymolオブジェクト(分子)の座標情報を取得する

pymol.cmd.iterate_state(<state>,<selection>,<expression>):pymolの関数、オブジェクトの情報

iterate_state.py
#!/usr/bin/python2.7 -i

import sys, os

### pymol launching
import pymol
pymol.pymol_argv = ['pymol','-qc'] + sys.argv[1:]
pymol.finish_launching()
cmd = pymol.cmd

### read pdb
r = cmd.fetch("4m61","obj1")
print r
cmd.iterate_state(1, "obj1 and name CA and resi 3 and chai A", "print x,y,z")  #print coordinates

出力結果

[hnishi@hn python_pymol]$ python iterate_state.py
obj1
9.97799968719 35.7999992371 20.9699993134

PDBファイル上には小数点3桁までの値なのに、4桁以下もあるという謎。

ソース
cui_pymol.py
https://gist.github.com/hnishi/3b9a3247a4bc356e17c7

pythonからpymolを起動する(GUI)

gui_pymol.py
#!/usr/bin/env python

# Tell PyMOL we don't want any GUI features.
# It runs PyMol fullscreen stereo, and disables the internal gui.
import __main__
__main__.pymol_argv = [ 'pymol', '-qei' ]  #$ pymol -qei

# Importing the PyMOL module will create the window.
import pymol

# Call the function below before using any PyMOL modules.
pymol.finish_launching()

[hnishi@hn python_pymol]$ python gui_pymol.py
pymolウィンドウがフルスクリーンで起動する。
内部GUIの機能がオフされる。その代わり、サーバーからxウィンドウ経由で操作しててもヌルヌル動くほど、軽い。

オブジェクトやモジュールのメソッドやメンバを表示する

dir(<something>)

python2.7
>>> import Bio
>>> print dir(Bio)
['BiopythonDeprecationWarning', 'BiopythonExperimentalWarning', 'BiopythonParserWarning', 'BiopythonWarning', 'MissingExternalDependencyError', 'MissingPythonDependencyError', '__builtins__', '__doc__', '__docformat__', '__file__', '__name__', '__package__', '__path__', '__version__']

保留

from multiprocessing import Pool
a = range(100)
a
p = Pool()

リンク

pymolスクリプトの書き方
http://qiita.com/hnishi/items/e77b270ad1a6d574d6cc

参考

pythonからpymolを使う
http://pymolwiki.org/index.php/Launching_From_a_Script
pymol内のオブジェクト情報を取り出す
http://www.pymolwiki.org/index.php/Simple_Scripting

8
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
12