text: user14684: I trying to write a pythonic PyMol script that loads a PDB, cmd.iterates through the PDB and stores the residues (three letter code) into an array.  I then want to loop through this array and calculate the center of masses (COM) of each aa's sidechains (except gly) and add it into a second array.  I am able to calculate the COM of a residue as a whole, i.e. including backbone atoms.  But what I want to calculate is the COM of the sidechain.  Please point me in the right direction.
  path = 'Path to PDB dir'
  for filename in glob.glob(os.path.join(path, '*.PDB')):
      cmd.load(filename)  
      stored_residues = []
      cmd.iterate('name ca', 'stored_residues.append(resn)')
  
      stored_residues_COM = []
  
      for i in stored_residues:
           stored_residues_sidechain_COM.append(centerOfMass('resn %s' % i))
  
  
  user6322: I am guessing you used the command pymol.cmd.centerofmass? If so the selector is the ticket to doing it by selecting everything of the residue bar the backbone atoms, e.g. f'resi {r} not name N+CA+C+O+H'
  Parenthetically, the cmd.iterate command is really ugly and counter-intuitive —it's heavily used because it is older. The command get_model(selection) in pymol2 module is so much nicer.
  for atom in pymol.cmd.get_model('name CA').atom:
       atom.resi #note it is type str due to insertion codes!
       atom.resn
       atom.coords # List[float]
  
  Second parenthesis. A and not element H is not needed or beneficial to pymol.cmd.centerofmass as this takes account of mass and occupancy. There is no need to select only heavy atoms as a proton weighs nothing. The barycentre and centroid are the same in geometry where there is no mass to the points, but here mass is present.
  
original_text: 14684: <p>I trying to write a pythonic PyMol script that loads a PDB, cmd.iterates through the PDB and stores the residues (three letter code) into an array.  I then want to loop through this array and calculate the center of masses (COM) of each aa's sidechains (except gly) and add it into a second array.  I am able to calculate the COM of a residue as a whole, i.e. including backbone atoms.  But what I want to calculate is the COM of the sidechain.  Please point me in the right direction.</p>
  <pre><code>path = 'Path to PDB dir'
  for filename in glob.glob(os.path.join(path, '*.PDB')):
      cmd.load(filename)  
      stored_residues = []
      cmd.iterate('name ca', 'stored_residues.append(resn)')
  
      stored_residues_COM = []
  
      for i in stored_residues:
           stored_residues_sidechain_COM.append(centerOfMass('resn %s' % i))
  </code></pre>
   6322: <p>I am guessing you used the command <code>pymol.cmd.centerofmass</code>? If so the selector is the ticket to doing it by selecting everything of the residue bar the backbone atoms, e.g. f'resi {r} not name N+CA+C+O+H'</p>
  <p>Parenthetically, the <code>cmd.iterate</code> command is really ugly and counter-intuitive —it's heavily used because it is older. The command <code>get_model(selection)</code> in pymol2 module is so much nicer.</p>
  <pre><code>for atom in pymol.cmd.get_model('name CA').atom:
       atom.resi #note it is type str due to insertion codes!
       atom.resn
       atom.coords # List[float]
  </code></pre>
  <p>Second parenthesis. A <code>and not element H</code> is not needed or beneficial to <code>pymol.cmd.centerofmass</code> as this takes account of mass and occupancy. There is no need to select only heavy atoms as a proton weighs nothing. The barycentre and centroid are the same in geometry where there is no mass to the points, but here mass is present.</p>
  