Difference between revisions of "Center of mass"

From PyMOLWiki
Jump to navigation Jump to search
(see also centerofmass)
 
(11 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 +
{{Infobox script-repo
 +
|type      = script
 +
|filename  = center_of_mass.py
 +
|author    = [[User:Henschel|Henschel]]
 +
|license  =
 +
}}
 +
 
===Description===
 
===Description===
  
Line 6: Line 13:
  
 
<source lang="python">
 
<source lang="python">
com selection [,state=None [,mass=None [,name=None]]]
+
com selection [,state=None [,mass=None [,object=None]]]
 
</source>
 
</source>
  
Line 13: Line 20:
  
 
<source lang="python">
 
<source lang="python">
 +
import center_of_mass
 
fetch 1c3y, finish=1, multiplex=0
 
fetch 1c3y, finish=1, multiplex=0
  
 
com 1c3y, state=1
 
com 1c3y, state=1
#create a pseudoatom representing the 1c3y COG and store it as "1c3y_COM"
+
#Create a pseudoatom representing the 1c3y COG and store it as "1c3y_COM"
 
+
#The "1c3y_COM" object will contain 1 state only
com 1c3y, state=1, name=COG
 
#create a pseudoatom representing the 1c3y COG and store it as "COG"
 
 
 
com 1c3y, state=1, name=COM, mass=1
 
#create a pseudoatom representing the 1c3y COM and store it as "COM"
 
 
 
com 1c3y, name=COM, mass=1
 
#create a single pseudoatom containing the COM for each state found in 1c3y and store it as "COM"
 
 
 
</source>
 
 
 
===PyMOL API===
 
 
 
<source lang="python">
 
from pymol import cmd
 
 
 
def com (selection,state=None,mass=None,name=None):
 
 
 
  """
 
  Author: Sean Law
 
  Michigan State University
 
  slaw (at) msu . edu
 
  """
 
  if (name == None):
 
      name=selection+"_COM"
 
  cmd.delete(name)
 
 
 
  if (state != None):
 
      x, y, z=get_com(selection,mass=mass)
 
      print "%f %f %f" % (x, y, z)
 
      cmd.pseudoatom(name,pos=[x, y, z])
 
      cmd.show("spheres",name)
 
  else:
 
      for i in range(cmd.count_states()):
 
        x, y, z=get_com(selection,mass=mass,state=i+1)
 
        print "State %d:%f %f %f" % (i+1, x, y, z)
 
        cmd.pseudoatom(name,pos=[x, y, z],state=i+1)
 
        cmd.show("spheres",name)
 
 
 
  return
 
 
cmd.extend("com",com)
 
  
def get_com (selection,state=1,mass=None):
+
com 1c3y, state=1, object=COG
 +
#Create a pseudoatom representing the 1c3y COG and store it as "COG"
 +
#The "COG" object will contain 1 state only
  
  """
+
com 1c3y, state=1, object=COM, mass=1
  Author: Sean Law
+
#Create a pseudoatom representing the 1c3y COM and store it as "COM"
  Michigan State University
+
#The "COM" object will contain 1 state only
  slaw (at) msu . edu
 
  """
 
 
 
  atmass={'H':1.00800000000000, 'C':12.0110000000000, \
 
              'N':14.0070000000000, 'O':15.9994000000000, \
 
              'P':30.9740000000000, 'S':32.0600000000000, \
 
              'F':18.9980000000000
 
          }
 
 
 
  totmass=0.0
 
  if (mass!=None):
 
      print "Calculating mass-weighted COM"
 
 
 
  state=int(state)
 
  model = cmd.get_model(selection,state)
 
  x,y,z=0,0,0
 
  for a in model.atom:
 
      if (mass != None):
 
        if (a.name[0] in atmass):
 
            x+= a.coord[0]*atmass[a.name[0]]
 
            y+= a.coord[1]*atmass[a.name[0]]
 
            z+= a.coord[2]*atmass[a.name[0]]
 
            totmass+=atmass[a.name[0]]
 
        else:
 
            print "Please add atomic mass for "+a.name+" to atmass"
 
            return
 
      else:
 
        x+= a.coord[0]
 
        y+= a.coord[1]
 
        z+= a.coord[2]
 
 
 
  if (mass!=None):
 
      return x/totmass, y/totmass, z/totmass
 
  else:
 
      return x/len(model.atom), y/len(model.atom), z/len(model.atom)
 
cmd.extend("get_com", get_com)
 
  
 +
com 1c3y, object=COM, mass=1
 +
#Create a single pseudoatom containing the COM for each state found in 1c3y and store it as "COM"
 +
#The "COM" object will contain MULTIPLE states!
 
</source>
 
</source>
  
 +
=== See Also ===
  
 
+
* [[centerofmass]] (new command in PyMOL 1.7.2)
===Previous Implementation===
+
* [[COM]]
 
+
* [[get_extent]]
Here is a script that calculates the center of geometry from a selection.
+
* [[get_position]]
It gets hold of the coordinates with cmd.get_model.
 
Make sure the atoms in the selection are of equal weight.
 
 
 
For a sample application, see: [http://yggdrasil.biotec.tu-dresden.de/abac/b.47.1.2___b.16.1.1___g.4.1.1.html "Convergent Evolution Examples"]
 
 
 
<source lang="python">
 
## Author: Andreas Henschel 2006
 
 
 
from pymol import cmd
 
from pymol.cgo import *
 
 
 
def centerOfMass(selection):
 
  ## assumes equal weights (best called with "and name ca" suffix)
 
  model = cmd.get_model(selection)
 
  x,y,z=0,0,0
 
  for a in model.atom:
 
      x+= a.coord[0]
 
      y+= a.coord[1]
 
      z+= a.coord[2]
 
  return (x/len(model.atom), y/len(model.atom), z/len(model.atom))
 
 
 
cmd.load("/group/bioinf/Data/PDBLinks/1c7c.pdb")
 
cmd.select("domain", "/1c7c//A/143-283/ and name ca") ## selecting a domain
 
 
 
domainCenter=centerOfMass("domain")
 
 
 
print "Center of mass: (%.1f,%.1f,%.1f)"% domainCenter
 
cmd.as("cartoon", "all")
 
cmd.show("spheres", "domain")
 
 
 
## Creating a sphere CGO
 
com = [COLOR, 1.0, 1.0, 1.0, SPHERE]+list(domainCenter) + [3.0] ## white sphere with 3A radius
 
cmd.load_cgo(com, "CoM")
 
 
 
cmd.zoom("1c7c", 1.0)
 
cmd.center("domain")
 
 
 
#ah@bioinfws19:~/Projects/PyMOL$ pymol -qc centerOfMass4.py
 
#Center of mass: (-1.0,24.5,48.2)
 
#ah@bioinfws19:~/Projects/PyMOL$
 
</source>
 
  
 
[[Category:Script_Library|Center of Mass]]
 
[[Category:Script_Library|Center of Mass]]
 
[[Category:Math_Scripts]]
 
[[Category:Math_Scripts]]
 +
[[Category:Pymol-script-repo]]

Latest revision as of 15:53, 12 October 2016

Type Python Script
Download center_of_mass.py
Author(s) Henschel
License
This code has been put under version control in the project Pymol-script-repo

Description

This script calculates the true center-of-mass (COM) or the center-of-geometry (COG) for a given selection and returns the x, y, z values in the form of a Pseudoatom (rather than a CGO sphere). The benefit of using a Pseudoatom is that it can be selected and used in calculations. In addition, this script also iteratively goes through all states of a selection if more than one state exists and appends the corresponding COM/COG values as states into the Pseudoatom. The script itself is quite simple yet robust enough to be applied in different settings. As well, the calculation of the COM/COG is handled independently from the formation of the Pseudoatom and can be called as an independent function where applicable.

Usage

com selection [,state=None [,mass=None [,object=None]]]


Examples

import center_of_mass
fetch 1c3y, finish=1, multiplex=0

com 1c3y, state=1
#Create a pseudoatom representing the 1c3y COG and store it as "1c3y_COM"
#The "1c3y_COM" object will contain 1 state only

com 1c3y, state=1, object=COG
#Create a pseudoatom representing the 1c3y COG and store it as "COG"
#The "COG" object will contain 1 state only

com 1c3y, state=1, object=COM, mass=1
#Create a pseudoatom representing the 1c3y COM and store it as "COM"
#The "COM" object will contain 1 state only

com 1c3y, object=COM, mass=1
#Create a single pseudoatom containing the COM for each state found in 1c3y and store it as "COM"
#The "COM" object will contain MULTIPLE states!

See Also