copying from Calculate Psi/Phi and/or dihaedral Pymol - Pymol API
the /model/chain/segi/resn/resi/atom_name
atom selection was something I wasnt able to find
until I stumbles into Selection Macros :
Selection Macros allow to represent a long atom selection phrase such as:
PyMOL> select pept and segi lig and chain B and resi 142 and name CA
in a more compact form:
PyMOL> select /pept/lig/B/142/CA
I tried to get phy , psi and omega ; double and triple check my code to see If I got it right :
#!/usr/bin/env python3
import pymol
from pymol import cmd, stored
pymol.finish_launching()
# pymol.finish_launching(['pymol', '-q', '-W' , '1200' , '-H' , '900' ])
cmd.fab('AAAGGAAGGVVPCAGCA', 'peptide')
# cmd.fab('AG', 'peptide')
# cmd.save('peptide.pdb' , 'peptide')
# cmd.hide('sticks' , 'peptide')
cmd.hide('everything' , 'peptide')
cmd.show('lines' , 'peptide')
a = cmd.select('selected' , 'peptide and resi 1-5 and name CA')
print('\n a ---> ' , a, '\n\n')
stored.dict = {}
cmd.iterate('selected' , "stored.dict[int(resi)] = [model, segi, chain, resn, int(resi)]")
print('\n stored.dict -------> ', stored.dict,'\n\n')
for i in stored.dict.keys() :
print( i , type(i))
print('stored.dict.keys ', stored.dict.keys() , type(stored.dict.keys()))
def dihedral_res( res_id , selected):
if int(res_id) - 1 in [i for i in selected.keys()] :
print(selected[res_id][3], ' can calculate phy', ' ____________' , res_id , int(res_id))
i = selected[res_id - 1]
# print(i)
s1 = "/{}/{}/{}/{}`{}/C".format(i[0] , i[1] , i [2] , i[3] , i[4])
# print(s1)
i = selected[res_id]
s2 = "/{}/{}/{}/{}`{}/N".format(i[0] , i[1] , i [2] , i[3] , i[4])
s3 = "/{}/{}/{}/{}`{}/CA".format(i[0] , i[1] , i [2] , i[3] , i[4])
s4 = "/{}/{}/{}/{}`{}/C".format(i[0] , i[1] , i [2] , i[3] , i[4])
cmd.select('dihedral_phy_'+str(res_id) , s1 +' '+ s2 +' '+ s3 +' '+ s4)
try:
dihedral_val_phy = cmd.get_dihedral(s1, s2, s3, s4, state=0)
except:
dihedral_val_phy = None
else :
print(selected[res_id][3], ' cannot calculate phy' , '____________' , res_id , int(res_id))
dihedral_val_phy = None
if int(res_id) + 1 in [i for i in selected.keys()] :
print(selected[res_id][3], ' can calculate psi', ' ____________' , res_id , int(res_id))
i = selected[res_id ]
# print(i)
s1 = "/{}/{}/{}/{}`{}/N".format(i[0] , i[1] , i [2] , i[3] , i[4])
# print(s1)
s2 = "/{}/{}/{}/{}`{}/CA".format(i[0] , i[1] , i [2] , i[3] , i[4])
s3 = "/{}/{}/{}/{}`{}/C".format(i[0] , i[1] , i [2] , i[3] , i[4])
i = selected[res_id + 1]
s4 = "/{}/{}/{}/{}`{}/N".format(i[0] , i[1] , i [2] , i[3] , i[4])
cmd.select('dihedral_psi_'+str(res_id) , s1 +' '+ s2 +' '+ s3 +' '+ s4)
try:
dihedral_val_psi = cmd.get_dihedral(s1, s2, s3, s4, state=0)
except:
dihedral_val_psi = None
else :
print(selected[res_id][3], ' cannot calculate psi' , '____________' , res_id , int(res_id))
dihedral_val_psi = None
if int(res_id) - 1 in [i for i in selected.keys()] :
print(selected[res_id][3], ' can calculate omega', ' ____________' , res_id , int(res_id))
i = selected[res_id - 1]
# print(i)
s1 = "/{}/{}/{}/{}`{}/CA".format(i[0] , i[1] , i [2] , i[3] , i[4])
s2 = "/{}/{}/{}/{}`{}/C".format(i[0] , i[1] , i [2] , i[3] , i[4])
# print(s1)
i = selected[res_id]
s3 = "/{}/{}/{}/{}`{}/N".format(i[0] , i[1] , i [2] , i[3] , i[4])
s4 = "/{}/{}/{}/{}`{}/CA".format(i[0] , i[1] , i [2] , i[3] , i[4])
cmd.select('dihedral_omega_'+str(res_id) , s1 +' '+ s2 +' '+ s3 +' '+ s4)
try:
dihedral_val_omega = cmd.get_dihedral(s1, s2, s3, s4, state=0)
except:
dihedral_val_omega = None
else :
print(selected[res_id][3], ' cannot calculate omega' , '____________' , res_id , int(res_id))
dihedral_val_omega = None
return dihedral_val_phy , dihedral_val_psi , dihedral_val_omega
for i in stored.dict.keys() :
print( dihedral_res( i, stored.dict) )
snippet of printed output for my code calculating phy, psi, omega for first 5 residues of a fake peptide AAAGGAAGGVVPCAGCA . Hope I got the directions right.
ALA can calculate phy ____________ 3 3
ALA can calculate psi ____________ 3 3
ALA can calculate omega ____________ 3 3
(180.0, -179.39898681640625, -180.0)
here the result hihlighting the residues involved in one of such dihedrals:

for the other dihedrals , I assume could works the same way. I bet there is a better way to create a function that returns the dihedrals but not an expert in Python or protein molecular graphics. Just for the sake of completeness, enclosed a table with the naming convention of the different aminoacid side chain dihedrals, hope the table is good [taken from http://www.mlb.co.jp/linux/science/garlic/doc/commands/dihedrals.html :
