Center of mass: Difference between revisions

From PyMOLWiki
Jump to navigation Jump to search
No edit summary
Line 1: Line 1:
{{Infobox script-repo
|type      = Python Module
|filename  = center_of_mass.py
|author    = [[User:Henschel|Henschel]]
|license  =
}}
===Description===
===Description===


Line 31: Line 38:
#Create a single pseudoatom containing the COM for each state found in 1c3y and store it as "COM"
#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!
#The "COM" object will contain MULTIPLE states!
</source>
===PyMOL API===
<source lang="python">
from pymol import cmd
def com(selection,state=None,mass=None,object=None, quiet=1, **kwargs):
  """
DESCRIPTION
  Places a pseudoatom at the center of mass
  Author: Sean Law
  Michigan State University
  slaw (at) msu . edu
SEE ALSO
  pseudoatom, get_com
  """
  quiet = int(quiet)
  if (object == None):
      try:
        object = cmd.get_legal_name(selection)
        object = cmd.get_unused_name(object + "_COM", 0)
      except AttributeError:
        object = 'COM'
  cmd.delete(object)
  if (state != None):
      x, y, z=get_com(selection,mass=mass, quiet=quiet)
      if not quiet:
        print "%f %f %f" % (x, y, z)
      cmd.pseudoatom(object,pos=[x, y, z], **kwargs)
      cmd.show("spheres",object)
  else:
      for i in range(cmd.count_states()):
        x, y, z=get_com(selection,mass=mass,state=i+1, quiet=quiet)
        if not quiet:
            print "State %d:%f %f %f" % (i+1, x, y, z)
        cmd.pseudoatom(object,pos=[x, y, z],state=i+1, **kwargs)
        cmd.show("spheres", 'last ' + object)
cmd.extend("com",com)
def get_com(selection,state=1,mass=None, quiet=1):
  """
DESCRIPTION
  Calculates the center of mass
  Author: Sean Law
  Michigan State University
  slaw (at) msu . edu
  """
  quiet = int(quiet)
  totmass=0.0
  if mass!=None and not quiet:
      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):
        m = a.get_mass()
        x+= a.coord[0]*m
        y+= a.coord[1]*m
        z+= a.coord[2]*m
        totmass+=m
      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)
# vi:expandtab:sw=3
</source>
</source>



Revision as of 06:38, 9 December 2011

Type Python Module
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