2014年9月29日月曜日

開発環境

Practical Programming: An Introduction to Computer Science Using Python 3 (Pragmatic Programmers) (Paul Gries (著)、Jennifer Campbell (著)、Jason Montojo (著)、Lynn Beighley (編集)、Pragmatic Bookshelf)のChapter 10(Reading and Writing Files)、10.10(Exercises) 7.を解いてみる。

10.10(Exercises) 7.

コード(BBEdit)

sample7.py

#!/usr/bin/env python3
#-*- coding: utf-8 -*-

def readMolecule(reader):
    line = reader.readline()
    if not line:
        return None
    if not line.isspace() and not line.startswith('CMNT'):
        key, name = line.split()
        molecule = [name]
    else:
        return None

    while True:
        line = reader.readline()
        if line.startswith('END'):
            break
        if not line.isspace() and not line.startswith('CMNT'):
            key, num, atom_type, x, y, z = line.split()
            molecule.append([atom_type, x, y, z])

    return molecule

def readAllMolecules(reader):
    result = []
    reading = True
    while reading:
        molecule = readMolecule(reader)
        if molecule:
            result.append(molecule)
        else:
            reading = False
    return result

if __name__ == '__main__':
    import sys
    with open(sys.argv[1]) as f:
        molecules = readAllMolecules(f)
        for molecule in molecules:
            print(molecule)

入出力結果(Terminal, IPython)

$ cat multimol.pdb
COMPND      AMMONIA
ATOM      1  N  0.257  -0.363   0.000
ATOM      2  H  0.257   0.727   0.000
ATOM      3  H  0.771  -0.727   0.890
ATOM      4  H  0.771  -0.727  -0.890
END
COMPND      METHANOL
ATOM      1  C  -0.748  -0.015   0.024
ATOM      2  O  0.558   0.420  -0.278
ATOM      3  H  -1.293  -0.202  -0.901
ATOM      4  H  -1.263   0.754   0.600
ATOM      5  H  -0.699  -0.934   0.609
ATOM      6  H  0.716   1.404   0.137
END
$ cat multimol1.pdb
COMPND      AMMONIA

ATOM      1  N  0.257  -0.363   0.000
ATOM      2  H  0.257   0.727   0.000

ATOM      3  H  0.771  -0.727   0.890
ATOM      4  H  0.771  -0.727  -0.890
END
COMPND      METHANOL
CMNT コメント
ATOM      1  C  -0.748  -0.015   0.024
ATOM      2  O  0.558   0.420  -0.278

ATOM      3  H  -1.293  -0.202  -0.901
ATOM      4  H  -1.263   0.754   0.600
ATOM      5  H  -0.699  -0.934   0.609
ATOM      6  H  0.716   1.404   0.137
END
$ ./sample7.py multimol.pdb
['AMMONIA', ['N', '0.257', '-0.363', '0.000'], ['H', '0.257', '0.727', '0.000'], ['H', '0.771', '-0.727', '0.890'], ['H', '0.771', '-0.727', '-0.890']]
['METHANOL', ['C', '-0.748', '-0.015', '0.024'], ['O', '0.558', '0.420', '-0.278'], ['H', '-1.293', '-0.202', '-0.901'], ['H', '-1.263', '0.754', '0.600'], ['H', '-0.699', '-0.934', '0.609'], ['H', '0.716', '1.404', '0.137']]
$ ./sample7.py multimol1.pdb
['AMMONIA', ['N', '0.257', '-0.363', '0.000'], ['H', '0.257', '0.727', '0.000'], ['H', '0.771', '-0.727', '0.890'], ['H', '0.771', '-0.727', '-0.890']]
['METHANOL', ['C', '-0.748', '-0.015', '0.024'], ['O', '0.558', '0.420', '-0.278'], ['H', '-1.293', '-0.202', '-0.901'], ['H', '-1.263', '0.754', '0.600'], ['H', '-0.699', '-0.934', '0.609'], ['H', '0.716', '1.404', '0.137']]
$

0 コメント:

コメントを投稿