File: hetatm.py

package info (click to toggle)
pymol 2.4.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 43,312 kB
  • sloc: cpp: 480,106; python: 79,860; ansic: 28,343; javascript: 6,792; sh: 47; makefile: 30; csh: 8
file content (174 lines) | stat: -rw-r--r-- 8,020 bytes parent folder | download | duplicates (3)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#A* -------------------------------------------------------------------
#B* This file contains source code for the PyMOL computer program
#C* copyright 1998-2000 by Warren Lyford Delano of DeLano Scientific.
#D* -------------------------------------------------------------------
#E* It is unlawful to modify or remove this copyright notice.
#F* -------------------------------------------------------------------
#G* Please see the accompanying LICENSE file for further information.
#H* -------------------------------------------------------------------
#I* Additional authors of this source file include:
#-*
#-*
#-*
#Z* -------------------------------------------------------------------

#
#
#

import chempy.models
from chempy.neighbor import Neighbor
from chempy.models import Connected
from chempy import Bond
from chempy import place

MAX_BOND_LEN = 2.2
PEPT_CUTOFF = 1.7

#---------------------------------------------------------------------------------
def generate(model, topology= None, forcefield = None ):

    add_bonds(model,topology=topology,forcefield=forcefield)
    connected = model.convert_to_connected()
    add_hydrogens(connected,topology=topology,forcefield=forcefield)
    place.simple_unknowns(connected)
    return connected.convert_to_indexed()

#---------------------------------------------------------------------------------
def assign_types(model, topology = None, forcefield = None ):
    if not isinstance(model, chempy.models.Indexed):
        raise ValueError('model is not an "Indexed" model object')
    nAtom = model.nAtom
    if nAtom:
        tmpl = topology.normal
        ffld = forcefield.normal
        res_list = model.get_residues()
        if len(res_list):
            for a in res_list:
                base = model.atom[a[0]]
                resn = base.resn
                if resn not in tmpl:
                    raise RuntimeError("unknown residue type '"+resn+"'")
                else:
                    # reassign atom names and build dictionary
                    dict = {}
                    aliases = tmpl[resn]['aliases']
                    for b in range(a[0],a[1]):
                        at = model.atom[b]
                        if at.name in aliases:
                            at.name = aliases[at.name]
                        dict[at.name] = b
                        if forcefield:
                            k = (resn,at.name)
                            if k in ffld:
                                at.text_type = ffld[k]['type']
                                at.partial_charge = ffld[k]['charge']
                            else:
                                raise RuntimeError("no parameters for '"+str(k)+"'")

#---------------------------------------------------------------------------------
def add_bonds(model, topology = None, forcefield = None ):
    if not isinstance(model, chempy.models.Indexed):
        raise ValueError('model is not an "Indexed" model object')
    nAtom = model.nAtom
    if nAtom:
        tmpl = topology.normal
        ffld = forcefield.normal
        res_list = model.get_residues()
        if len(res_list):
            for a in res_list:
                base = model.atom[a[0]]
                resn = base.resn
                if resn not in tmpl:
                    raise RuntimeError("unknown residue type '"+resn+"'")
                else:
                    # reassign atom names and build dictionary
                    dict = {}
                    aliases = tmpl[resn]['aliases']
                    for b in range(a[0],a[1]):
                        at = model.atom[b]
                        if at.name in aliases:
                            at.name = aliases[at.name]
                        dict[at.name] = b
                        if forcefield:
                            k = (resn,at.name)
                            if k in ffld:
                                at.text_type = ffld[k]['type']
                                at.partial_charge = ffld[k]['charge']
                            else:
                                raise RuntimeError("no parameters for '"+str(k)+"'")
                    # now add bonds for atoms which are present
                    bonds = tmpl[resn]['bonds']
                    mbond = model.bond
                    for b in list(bonds.keys()):
                        if b[0] in dict and b[1] in dict:
                            bnd = Bond()
                            bnd.index = [ dict[b[0]], dict[b[1]] ]
                            bnd.order = bonds[b]['order']
                            mbond.append(bnd)

#---------------------------------------------------------------------------------
def add_hydrogens(model,topology=None,forcefield=None):
    if not isinstance(model, chempy.models.Connected):
        raise ValueError('model is not a "Connected" model object')
    nAtom = model.nAtom
    if nAtom:
        if not model.index:
            model.update_index()
        ffld = forcefield.normal
        tmpl = topology.normal
        res_list = model.get_residues()
        if len(res_list):
            for a in res_list:
                base = model.atom[a[0]]
                resn = base.resn
                if resn not in tmpl:
                    raise RuntimeError("unknown residue type '"+resn+"'")
                else:
                    # build dictionary
                    dict = {}
                    for b in range(a[0],a[1]):
                        at = model.atom[b]
                        dict[at.name] = b
                    # find missing bonds with hydrogens
                    bonds = tmpl[resn]['bonds']
                    mbond = model.bond
                    for b in list(bonds.keys()):
                        if b[0] in dict and (b[1] not in dict):
                            at = model.atom[dict[b[0]]]
                            if at.symbol != 'H':
                                name = b[1]
                                symbol = tmpl[resn]['atoms'][name]['symbol']
                                if symbol == 'H':
                                    newat = at.new_in_residue()
                                    newat.name = name
                                    newat.symbol = symbol
                                    k = (resn,newat.name)
                                    newat.text_type = ffld[k]['type']
                                    newat.partial_charge = ffld[k]['charge']
                                    idx1 = model.index[id(at)]
                                    idx2 = model.add_atom(newat)
                                    bnd = Bond()
                                    bnd.index = [ idx1, idx2 ]
                                    bnd.order = bonds[b]['order']
                                    mbond[idx1].append(bnd)
                                    mbond[idx2].append(bnd)
                        if (b[0] not in dict) and b[1] in dict:
                            at = model.atom[dict[b[1]]]
                            if at.symbol != 'H':
                                name = b[0]
                                symbol = tmpl[resn]['atoms'][name]['symbol']
                                if symbol == 'H':
                                    newat = at.new_in_residue()
                                    newat.name = name
                                    newat.symbol = symbol
                                    k = (resn,newat.name)
                                    newat.text_type = ffld[k]['type']
                                    newat.partial_charge = ffld[k]['charge']
                                    idx1 = model.index[id(at)]
                                    idx2 = model.add_atom(newat)
                                    bnd = Bond()
                                    bnd.index = [ idx1, idx2 ]
                                    bnd.order = bonds[b]['order']
                                    mbond[idx1].append(bnd)
                                    mbond[idx2].append(bnd)