This file is indexed.

/usr/share/pyshared/MMTK/MoleculeFactory.py is in python-mmtk 2.7.9-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# A molecule factory is used to create chemical objects
# in code, without a database definition.
#
# Written by Konrad Hinsen
#

"""
Molecule factory for creating chemical objects
"""

__docformat__ = 'restructuredtext'

from MMTK import Bonds, ChemicalObjects

#
# Molecule factories store AtomTemplate and GroupTemplate objects.
#

class AtomTemplate(object):
    
    def __init__(self, element):
        self.element = element

class GroupTemplate(object):
    
    def __init__(self, name):
        self.name = name
        self.children = []
        self.names = {}
        self.attributes = {}
        self.positions = {}
        self.bonds = []
        self.locked = False

    def addAtom(self, atom_name, element):
        """
        Add an atom.

        :param atom_name: the name of the atom
        :type atom_name: str
        :param element: the chemical element symbol
        :type element: str
        """
        if self.locked:
            raise ValueError("group is locked")
        atom = AtomTemplate(element)
        self.names[atom_name] = len(self.children)
        self.children.append(atom)

    def addSubgroup(self, subgroup_name, subgroup):
        """
        Add a subgroup.

        :param subgroup_name: the name of the subgroup within this group
        :type subgroup_name: str
        :param subgroup: the subgroup type
        :type subgroup: str
        """
        if self.locked:
            raise ValueError("group is locked")
        self.names[subgroup_name] = len(self.children)
        self.children.append(subgroup)

    def addBond(self, atom1, atom2):
        """
        Add a bond.

        :param atom1: the name of the first atom
        :type atom1: str
        :param atom2: the name of the second atom
        :type atom2: str
        """
        if self.locked:
            raise ValueError("group is locked")
        self.bonds.append((self.atomNameToPath(atom1),
                           self.atomNameToPath(atom2)))

    def setAttribute(self, name, value):
        if self.locked:
            raise ValueError("group is locked")
        self.attributes[name] = value

    def setPosition(self, name, vector):
        if self.locked:
            raise ValueError("group is locked")
        self.positions[name] = vector

    def getAtomReference(self, atom_name):
        path = self.atomNameToPath(atom_name)
        object = self
        for path_element in path[:-1]:
            object =  object.children[object.names[path_element]]
        atom_index = object.names[path[-1]]
        reference = 0
        for i in range(atom_index):
            if isinstance(object.children[i], AtomTemplate):
                reference += 1
        from MMTK.Database import AtomReference
        return AtomReference(reference)

    def atomNameToPath(self, atom_name):
        atom_name = atom_name.split('.')
        object = self
        try:
            for path_element in atom_name:
                object = object.children[object.names[path_element]]
            if not isinstance(object, AtomTemplate):
                raise ValueError("no atom " + atom)
        except KeyError:
            raise ValueError("no atom " + '.'.join(atom_name))
        return atom_name

    def writeXML(self, file, memo):
        if memo.get(self.name, False):
            return
        names = len(self.children)*[None]
        for name in self.names:
            names[self.names[name]] = name
        atoms = []
        subgroups = []
        for i in range(len(self.children)):
            object = self.children[i]
            name = names[i]
            if isinstance(object, GroupTemplate):
                object.writeXML(file, memo)
                subgroups.append((name, object))
            else:
                atoms.append((name, object))
        file.write('<molecule id="%s">\n' % self.name)
        for name, subgroup in subgroups:
            file.write('  <molecule ref="%s" title="%s"/>\n'
                       % (subgroup.name, name))
        if atoms:
            file.write('  <atomArray>\n')
            for name, atom in atoms:
                file.write('    <atom title="%s" elementType="%s"/>\n'
                           % (name, atom.element))
            file.write('  </atomArray>\n')
        if self.bonds:
            file.write('  <bondArray>\n')
            for a1, a2 in self.bonds:
                file.write('    <bond atomRefs2="%s %s"/>\n'
                           % (':'.join(a1), ':'.join(a2)))
            file.write('  </bondArray>\n')
        file.write('</molecule>\n')
        memo[self.name] = True

    def getXMLAtomOrder(self):
        atom_names = []
        names = len(self.children)*[None]
        for name in self.names:
            names[self.names[name]] = name
        for i in range(len(self.children)):
            oname = names[i]
            object = self.children[i]
            if isinstance(object, GroupTemplate):
                for name in object.getXMLAtomOrder():
                    atom_names.append(oname + ':' + name)
            else:
                atom_names.append(oname)
        return atom_names

#
# The MoleculeFactory class
#
class MoleculeFactory(object):
    '''
    MoleculeFactory

    A MoleculeFactory serves to create molecules without reference to database
    definitions. Molecules and groups are defined in terms of atoms, groups, and
    bonds. Each MoleculeFactory constitutes an independent set of definitions.
    Definitions within a MoleculeFactory can refer to each other.

    Each MoleculeFactory stores a set of :class:`~MMTK.ChemicalObjects.Group`
    objects which are referred to by names. The typical operation sequence is to
    create a new group and then add atoms, bonds, and subgroups. It is also
    possible to define coordinates and arbitrary attributes (in particular for
    force fields). In the end, a finished object can be retrieved as a 
    :class:`~MMTK.ChemicalObjects.Molecule` object.
    '''

    def __init__(self):
        self.groups = {}
        self.locked_groups = {}

    def createGroup(self, name):
        """
        Create a new (initially empty) group object.
        """
        if self.groups.has_key(name):
            raise ValueError("redefinition of group " + name)
        self.groups[name] = GroupTemplate(name)

    def addSubgroup(self, group, subgroup_name, subgroup):
        """
        Add a subgroup to a group

        :param group: the name of the group
        :type group: str
        :param subgroup_name: the name of the subgroup within the group
        :type subgroup_name: str
        :param subgroup: the subgroup type
        :type subgroup: str
        """
        self.groups[group].addSubgroup(subgroup_name, self.groups[subgroup])

    def addAtom(self, group, atom_name, element):
        """
        Add an atom to a group

        :param group: the name of the group
        :type group: str
        :param atom_name: the name of the atom
        :type atom_name: str
        :param element: the chemical element symbol
        :type element: str
        """
        self.groups[group].addAtom(atom_name, element)

    def addBond(self, group, atom1, atom2):
        """
        Add a bond to a group

        :param group: the name of the group
        :type group: str
        :param atom1: the name of the first atom
        :type atom1: str
        :param atom2: the name of the second atom
        :type atom2: str
        """
        self.groups[group].addBond(atom1, atom2)

    def setAttribute(self, group, name, value):
        self.groups[group].setAttribute(name, value)

    def setPosition(self, group, atom, vector):
        self.groups[group].setPosition(atom, vector)

    def getAtomReference(self, group, atom):
        return self.groups[group].getAtomReference(atom)

    def retrieveMolecule(self, group):
        """
        :param group: the name of the group to be used as a template
        :type group: str
        :returns: a molecule defined by the contents of the group
        :rtype: :class:`~MMTK.ChemicalObjects.Molecule`
        """

        group = self.groups[group]
        return self.makeChemicalObjects(group, True)
    
    def makeChemicalObjects(self, template, top_level):
        self.groups[template.name].locked = True
        if top_level:
            if template.attributes.has_key('sequence'):
                object = ChemicalObjects.ChainMolecule(None)
            else:
                object = ChemicalObjects.Molecule(None)
        else:
            object = ChemicalObjects.Group(None)
        object.atoms = []
        object.bonds = Bonds.BondList([])
        object.groups = []
        object.type = self.groups[template.name]
        object.parent = None
        child_objects = []
        for child in template.children:
            if isinstance(child, GroupTemplate):
                group = self.makeChemicalObjects(child, False)
                object.groups.append(group)
                object.atoms.extend(group.atoms)
                object.bonds.extend(group.bonds)
                group.parent = object
                child_objects.append(group)
            else:
                atom = ChemicalObjects.Atom(child.element)
                object.atoms.append(atom)
                atom.parent = object
                child_objects.append(atom)
        for name, index in template.names.items():
            setattr(object, name, child_objects[index])
            child_objects[index].name = name
        for name, value in template.attributes.items():
            path = name.split('.')
            setattr(self.namePath(object, path[:-1]), path[-1], value)
        for atom1, atom2 in template.bonds:
            atom1 = self.namePath(object, atom1)
            atom2 = self.namePath(object, atom2)
            object.bonds.append(Bonds.Bond((atom1, atom2)))
        for name, vector in template.positions.items():
            path = name.split('.')
            self.namePath(object, path).setPosition(vector)
        return object

    def namePath(self, object, path):
        for item in path:
            object = getattr(object, item)
        return object

    def writeXML(self, file):
        file.write('<?xml version="1.0" encoding="ISO-8859-1" ' +
                   'standalone="yes"?>\n\n')
        file.write('<templates>\n\n')
        memo = {}
        for group in self.groups:
            self.groups[group].writeXML(file, memo)
        file.write('\n</templates>\n')