Scripts: mean_distance.py

File mean_distance.py, 1.1 KB (added by goddard, 8 years ago)
Line 
1#
2# Compute mean position of each atom in an ensemble and assign an atom attribute for each atom
3# in each ensemble model giving its distance to the mean. This is to allow coloring atoms by
4# distance from the mean structure.
5#
6# This script takes all open molecules as the ensemble and assigns atom attribute distance_from_mean.
7#
8# Example commands for coloring:
9#
10# open 1mtx
11# show
12# open mean_distance.py
13# rangecolor distance_from_mean 0 blue 3 white 6 red
14#
15# Menu Tools / Depiction / Render by Attribute can be used to do coloring and see a histogram of distances.
16#
17
18from chimera import openModels, numpyArrayFromAtoms, Molecule
19mlist = openModels.list(modelTypes = [Molecule])
20ave_xyz = None
21for m in mlist:
22 mxyz = numpyArrayFromAtoms(m.atoms, xformed = True)
23 if ave_xyz is None:
24 ave_xyz = mxyz.copy()
25 else:
26 ave_xyz += mxyz
27ave_xyz /= len(mlist)
28
29from numpy import sqrt
30for m in mlist:
31 mxyz = numpyArrayFromAtoms(m.atoms, xformed = True)
32 dxyz = mxyz - ave_xyz
33 d = sqrt((dxyz*dxyz).sum(axis=1))
34 for a,d in zip(m.atoms, d):
35 a.distance_from_mean = d