/usr/share/pyshared/MMTK/PyMOL.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 | # PyMOL interface
#
# Note: this is still in a rather experimental state.
#
# Written by Konrad Hinsen
#
import sys
in_pymol = sys.modules.has_key('pymol')
del sys
if in_pymol:
try:
from pymol import cmd
from chempy import Atom, Bond, Molecule
from chempy.models import Indexed
except ImportError:
in_pymol = False
import Units, Utility
from Scientific.Geometry import Vector
import traceback
#
# Create a PyMOL representation from an MMTK object
#
class Representation:
def __init__(self, object, name = 'MMTK_model', configuration = None,
b_values = None):
self.object = object
self.universe = object.universe()
self.name = name
self.model = Indexed()
self.index_map = {}
self.atoms = []
chain_id_number = 0
in_chain = True
for o in object.bondedUnits():
if Utility.isSequenceObject(o):
groups = [(g, g.name) for g in o]
if not in_chain:
chain_id_number = (chain_id_number+1) % len(self.chain_ids)
in_chain = True
else:
groups = [(o, o.name)]
in_chain = False
residue_number = 1
for g, g_name in groups:
for a in g.atomList():
atom = Atom()
atom.symbol = a.symbol
atom.name = a.name
atom.resi_number = residue_number
atom.chain = self.chain_ids[chain_id_number]
if b_values is not None:
atom.b = b_values[a]
atom.resn = g_name
self.model.atom.append(atom)
self.index_map[a] = len(self.atoms)
self.atoms.append(a)
residue_number = residue_number + 1
if in_chain:
chain_id_number = (chain_id_number+1) % len(self.chain_ids)
try:
bonds = o.bonds
except AttributeError:
bonds = []
for b in bonds:
bond = Bond()
bond.index = [self.index_map[b.a1], self.index_map[b.a2]]
self.model.bond.append(bond)
self._setCoordinates(configuration)
chain_ids = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
def _setCoordinates(self, configuration):
if configuration is None:
for i in range(len(self.atoms)):
self.model.atom[i].coord = \
list(self.atoms[i].position()/Units.Ang)
else:
for i in range(len(self.atoms)):
self.model.atom[i].coord = \
list(configuration[self.atoms[i]]/Units.Ang)
def show(self):
auto_zoom = cmd.get("auto_zoom")
cmd.set("auto_zoom", 0)
cmd.load_model(self.model, self.name)
cmd.set("auto_zoom", auto_zoom)
def remove(self):
cmd.delete(self.name)
def update(self, configuration=None):
try:
cmd.set("suspend_updates","1")
self._setCoordinates(configuration)
cmd.load_model(self.model, self.name, 1)
except:
cmd.set("suspend_updates","0")
traceback.print_exc()
cmd.set("suspend_updates","0")
cmd.refresh()
def movie(self, configurations):
n = 1
cmd.feedback("disable","executive","actions")
auto_zoom = cmd.get("auto_zoom")
cmd.set("auto_zoom", 0)
for conf in configurations:
self._setCoordinates(conf)
cmd.load_model(self.model, self.name, n)
n = n + 1
cmd.set("auto_zoom", auto_zoom)
cmd.feedback("enable","executive","actions")
cmd.mplay()
#
# Create an MMTK object from a PyMOL model
#
# Note: PyMOL models are closer in structure to a PDB file than
# to an MMTK object. Some guessing is therefore required to make
# the conversion, and that can go wrong. At the moment, this works
# for standard proteins but it requires a lot of polishing or testing
# before it could be used for arbitrary systems. Still, it is useful
# as it is.
#
# This interface might change in future releases. Consider it experimental.
#
def scanModel(pymol_model):
segment_dict = {}
segment_list = []
for atom in pymol_model.atom:
seg_id = atom.segi + '_' + atom.chain
if seg_id not in segment_list:
segment_list.append(seg_id)
res_num = atom.resi_number
residue_dict = segment_dict.get(seg_id, {})
segment_dict[seg_id] = residue_dict
res_name, atom_list = residue_dict.get(res_num, (atom.resn, []))
residue_dict[res_num] = res_name, atom_list
atom_list.append((atom.name, Vector(atom.coord)*Units.Ang))
segments = []
for seg_id in segment_list:
residue_dict = segment_dict[seg_id]
residue_list = residue_dict.items()
residue_list.sort(lambda a, b: cmp(a[0], b[0]))
segments.append((seg_id, [r[1] for r in residue_list]))
return segments
def buildCalphaModel(pymol_model):
import Collections, Proteins
data = scanModel(pymol_model)
chains = []
for seg_id, segment in data:
amino_acids = []
nucleic_acids = []
for res_name, residue in segment:
res_name = res_name.lower()
if res_name in amino_acid_names_ca:
amino_acids.append((res_name, residue))
if amino_acids:
chain = Proteins.PeptideChain([item[0] for item in amino_acids],
model='calpha')
for residue, res_data in zip(chain, amino_acids):
pymol_atom_list = res_data[1]
for atom_name, atom_pos in pymol_atom_list:
if atom_name.strip().lower() == 'ca':
residue.peptide.C_alpha.setPosition(atom_pos)
chains.append(chain)
return Proteins.Protein(chains)
def buildAllAtomModel(pymol_model):
import ChemicalObjects, Collections, Proteins
data = scanModel(pymol_model)
all = Collections.Collection()
peptide_chains = []
for seg_id, segment in data:
amino_acids = []
nucleic_acids = []
for res_name, residue in segment:
res_name = res_name.lower().strip()
if res_name in amino_acid_names:
amino_acids.append((res_name, residue))
elif res_name in nucleic_acid_names:
nucleic_acids.append((res_name, residue))
elif res_name == 'hoh':
if len(residue) == 3:
m = ChemicalObjects.Molecule('water')
for atom_name, atom_pos in residue:
atom = getattr(m, atom_name)
atom.setPosition(atom_pos)
elif len(residue) == 1 and residue[0][0] == 'O':
m = ChemicalObjects.Atom('O')
m.setPosition(residue[0][1])
all.addObject(m)
else:
print "Skipping unknown residue", res_name
if amino_acids:
chain = Proteins.PeptideChain([item[0] for item in amino_acids])
for residue, res_data in zip(chain, amino_acids):
pdbmap = residue.pdbmap[0][1]
try:
altmap = residue.pdb_alternative
except AttributeError:
altmap = {}
pymol_atom_list = res_data[1]
atom_list = residue.atomList()
for atom_name, atom_pos in pymol_atom_list:
try:
atom_name = altmap[atom_name]
except KeyError:
pass
try:
atom = atom_list[pdbmap[atom_name].number]
except KeyError:
print "No atom named " + atom_name
atom.setPosition(atom_pos)
print atom_name, atom.name
chain.findHydrogenPositions()
peptide_chains.append(chain)
if peptide_chains:
all.addObject(Proteins.Protein(peptide_chains))
return all
amino_acid_names = ['ala', 'arg', 'asn', 'asp', 'cys', 'gln', 'glu', 'gly',
'his', 'ile', 'leu', 'lys', 'met', 'phe', 'pro', 'ser',
'thr', 'trp', 'tyr', 'val', 'cyx', 'hsd', 'hse', 'hsp',
'hid', 'hie', 'hip', 'ace', 'nme', 'nhe']
amino_acid_names_ca = ['ala', 'arg', 'asn', 'asp', 'cys', 'gln', 'glu', 'gly',
'his', 'ile', 'leu', 'lys', 'met', 'phe', 'pro', 'ser',
'thr', 'trp', 'tyr', 'val', 'cyx', 'hsd', 'hse', 'hsp',
'hid', 'hie', 'hip']
nucleic_acid_names = ['da', 'da5', 'da3', 'dan', 'dc', 'dc5', 'dc3', 'dcn',
'dg', 'dg5', 'dg3', 'dgn', 'dt', 'dt5', 'dt3', 'dtn',
'ra', 'ra5', 'ra3', 'ran', 'rc', 'rc5', 'rc3', 'rcn',
'rg', 'rg5', 'rg3', 'rgn', 'ru', 'ru5', 'ru3', 'run']
|