getting the center of mass of each part of a molecule

Cameron Simpson cs at zip.com.au
Mon May 15 19:11:19 EDT 2017


On 15May2017 13:29, qasimpars at gmail.com <qasimpars at gmail.com> wrote:
>I need to get the center of mass (COM) of each half of the ligand shown in the 
>figure (https://i.stack.imgur.com/dtdul.png). I get the main COM all the 
>ligand, lets say it is close to C1 atom. In addition to the main COM of all 
>the ligand, I need to find the COM of each half of the ligand. The atoms of 
>the first part/half according to the main COM of the ligand are C2, C7, C8 and 
>C9. As for the second part they are C3, C4, C5 and C6 atoms. The question is 
>how can I divide the ligand in two parts according to the main COM and 
>calculate the COM of each divided part (except for the atom closest to the 
>main COM, C1)? By the way I have multiple ligands. The code should be user 
>firendly.

Leaving aside that you are using numpy arrays, if sources like you need to 
partition an existing array into 2 parts around the dividing element (C1).

Figure out how to find the position of C1 in the array. If your atoms are 
unique in the sense that C1 != C2 even if they are both carbon atoms) you could 
use the .index method of a list (hoping numpy array support that). But 
otherwise, loop over the array until you find the element C1 and return the 
index. Write a short function for this purpose, eg (incomplete):

  def find_atom(atom, atoms):
    for i, a  in enumerate(atoms):
      if a is the target atom "atom":
        return i
    raise IndexError("no such atom")

Then all you need to do is:

  - figure out _which_ atom is your "C1"
  - locate it in the main array of atoms
  - extract the left and half subarrays

For the last part, remember that to get a sublist you can write L[a:b] to get 
all the elements of L from a up to but excluding b. And that omitting "a" 
implies index 0 and omitting "b" implies len(L).

The homework suggestion that the "code should be user firendly" I would take to 
imply that the last step should itself be a function, eg:

  def partition_atoms(atom, atoms):

which takes your target atom (C1) and the original array, calls find_atom to 
locate C1, and returns the left and right sublists.

Cheers,
Cameron Simpson <cs at zip.com.au>



More information about the Python-list mailing list