summaryrefslogtreecommitdiff
path: root/cad/src/experimental/graphene/pdb2mmp.py
blob: ab55cfa70c068187d9757bf84a52b17eea319867 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Copyright 2006-2007 Nanorex, Inc.  See LICENSE file for details.
"""Convert CoNTub's PDB output to an MMP file

Typical usage:
    python pdb2mmp.py < hetero.pdb > hetero.mmp
"""

import sys, string

class Atom:
    def __init__(self, index, elem, xyz):
        self.index = index
        self.elem = elem
        self.xyz = xyz
        self.bonds = [ ]
    def __repr__(self):
        def s(x):
            # convert distances to int picometers
            return int(1000.0 * x)
        r = ('atom %d (%d) (%d, %d, %d) def' %
             (self.index, self.elem,
              s(self.xyz[0]), s(self.xyz[1]), s(self.xyz[2])))
        if self.bonds:
            r += '\nbondg'
            for y in self.bonds:
                r += ' %d' % y
        return r

atoms = { }
bonds = [ ]

for L in sys.stdin.readlines():
    L = L.rstrip().upper()
    #print L
    fields = L.split()
    if fields[0] == 'HETATM':
        index = string.atoi(fields[1])
        elem = {'C': 6, 'H': 1, 'N': 7}[fields[2]]
        xyz = map(string.atof, fields[-3:])
        atoms[index] = Atom(index, elem, xyz)
    elif fields[0] == 'CONECT':
        fields = map(string.atoi, fields[1:])
        x = fields[0]
        for y in fields[1:]:
            if y < x:
                bonds.append((x, y))

for x, y in bonds:
    atoms[x].bonds.append(y)

r = '''mmpformat 050920 required; 051103 preferred
kelvin 300
group (View Data)
info opengroup open = True
csys (HomeView) (1.000000, 0.000000, 0.000000, 0.000000) (10.000000) (0.000000, 0.000000, 0.000000) (1.000000)
csys (LastView) (1.000000, 0.000000, 0.000000, 0.000000) (10.943023) (0.000000, 0.000000, 0.000000) (1.000000)
egroup (View Data)
group (Heterojunction)
info opengroup open = True
mol (Heterojunction-1) def
'''

for index in atoms:
    r += repr(atoms[index]) + '\n'

r += '''egroup (Heterojunction)
end1
group (Clipboard)
info opengroup open = False
egroup (Clipboard)
end molecular machine part 1881'''

print r