[PyMOL] How to best create a sliding window (a selection of specific residues) and compare specific residue names+numbers in pymol/python?

2019-02-22 Thread Anne Nierobisch
Hi,

I need to calculate the RMSD for the same residue, e.g. 131Thr, from 2 pdb 
files for the same target. As I need a local alignment, I use a sliding window 
of 5 residues (the residue of interest is in the middle.)
I have adapted the script from https://pymolwiki.org/index.php/RmsdByResidue 
(also see below my code below) by adding a sliding window, but I need advice on 
the following:

- I would need to check whether I am actually comparing the intended residues, 
e.g. 131Thr, 131Thr, or whether the same residues (e.g. His, Asp, His), with 
specific residue names and numbers   are in the selection I have chosen from 
both pdb files for my sliding window.
The reason:
I have coded up the script below, but often it skips and doesn't compare two 
residues,  because the atom count is different between the residue from protein 
A and protein B.
(It is exactly double the number, I have already checked and excluded problems 
like occupancy and different conformations as potential causes.)

- I constructed a sliding window by selecting two residues before the residue 
of interest and two residues which follow said residue. (I know that residues 
can be missing, I am working on this.)
Is there a better way for constructing a sliding window? I have not found such 
a method in pymol.

For anyone interested, I have attached a code snipplet below.
I am sorry, if these seem like obvious questions, but I tried various 
approaches and I feel that I need a push in the right direction. I have the 
feeling that I am missing something fundamental.

Many thanks for any suggestion!
Anne (Newbie in Pymol)

##


Code snipplet (Python/Pymol interface):

  referenceProteinChain = cmd.fetch(pbdstructure1)
  targetProteinChain = cmd.fetch(pdbstructure2)
  sel = referenceProteinChain
  list_atoms = [[133, 133]] # example list, I want to calculate the rmsd 
between residue 133 and residue 133 of two pdb structures

for j in list_atoms:
   # I formulate my sliding window of 5 residues, my residue of interest is in 
the middle
ref_begin = int(j[0])-2
ref_end = int(j[0])+2
target_begin = int(j[1])-2
target_end = int(j[1])+2

# I create a selection for the reference protein and the target protein
cmd.create("ref_gzt", referenceProteinChain+" and polymer and not alt B and 
not Alt C and not alt D  and resi %s-%s"
cmd.alter("ref_gzt", "chain='A'")
cmd.alter("ref_gzt", "segi=''")
cmd.create("target_gzt", targetProteinChain+" and polymer and  not alt B 
and not Alt C and not alt D  and resi %s-%s" %(target_begin,target_end) )
cmd.alter("target_gzt", "chain='A'")
cmd.alter("target_gzt", "segi=''")

# I align my selected 5 residues for a local alignment window
cmd.align("target_gzt","ref_gzt",object="align", cycles =5)


   # select alpha carbon of in reference structure to loop over
calpha=cmd.get_model(sel+" and name CA and not alt B and not Alt C and not 
alt D  and resi %s-%s" %(ref_begin,ref_end) )

   ## here I loop over all residues in the sliding window and calculte the rmsd 
for my residues of interest.
for g in calpha.atom : I loop over the slinding window
if g.resi == str(j[0]): ## we select only our residue of interest 
within the sliding window
if cmd.count_atoms("ref_gzt and polymer and resi 
"+g.resi)==cmd.count_atoms("target_gzt and polymer and resi "+g.resi):

## calculte the pairwise RMSD between the residues I specified 
in list_atoms
rmsdRes=cmd.rms_cur("ref_gzt and polymer and resi 
"+g.resi,"target_gzt and polymer and resi "+g.resi)

___
PyMOL-users mailing list
Archives: http://www.mail-archive.com/pymol-users@lists.sourceforge.net
Unsubscribe: 
https://sourceforge.net/projects/pymol/lists/pymol-users/unsubscribe

Re: [PyMOL] How to catch PyMOL command exception within a Python script

2019-02-22 Thread Thomas Evangelidis
Hi Thomas,

With regard to selections, sometimes a residue has missing atoms or is
non-standard amino acid. In the first case it is matched with another
residue in a second structure during the alignment and then when I try to
fit the two selections of atoms from the two structures I get the following
error:

 Calculating RMSD between pocket of model 2hduA and 2r9wA.
> ExecutiveRMS-Error: Atom counts between selections don't match (799 vs 797)
>  Executive: Error -- no atoms left after refinement!
> RMSD = -1.00


Can you think of a clever way to exclude amino acids with missing atoms or
non-standard ones from pair_fit() or rms_cur()?

Best,
Thomas





On Fri, 22 Feb 2019 at 12:32, Thomas Holder 
wrote:

> Hi Thomas,
>
> In general, the PyMOL API should raise pymol.CmdException if things go
> wrong. But in case of cmd.pair_fit() this wasn't happening (see my fix that
> I pushed few minutes ago:
> https://github.com/schrodinger/pymol-open-source/commit/b26d91c40d20344fef511ea9d6bb664a93f1bb4a
> )
>
> cmd.select() correctly raises an exception, so if you want to make a
> script more robust against selection failures, you could always create a
> named selection first to check if it's valid.
>
> tmpsele1 = cmd.get_unused_name('_sele1')
> try:
> cmd.select(tmpsele1, someexpression, 0)
> except pymol.CmdException:
> print('invalid selection')
> finally:
> cmd.delete(tmpsele1)
>
>
> Cheers,
>   Thomas
>
> > On Feb 22, 2019, at 12:11 PM, Thomas Evangelidis 
> wrote:
> >
> > Hi Thomas,
> >
> > This is great! I can even include more atom types in the selection.
> > Just for the record, is it possible to catch PyMOL exceptions like
> "Selector-Error" from within a Python script? Is there any general strategy
> to select which exception type to look for? In the past, I was catching
> exceptions from 'tmalign' with "except AssertionError:" which doesn't work
> for "Selector-Error".
> >
> > Best,
> > Thomas
> >
> >
> > --
> > ==
> > Dr Thomas Evangelidis
> > Research Scientist
> > IOCB - Institute of Organic Chemistry and Biochemistry of the Czech
> Academy of Sciences
> > Prague, Czech Republic
> >   &
> > CEITEC - Central European Institute of Technology
> > Brno, Czech Republic
> >
> > email: teva...@gmail.com
> > website: https://sites.google.com/site/thomasevangelidishomepage/
>
> --
> Thomas Holder
> PyMOL Principal Developer
> Schrödinger, Inc.
>
>

-- 

==

Dr Thomas Evangelidis

Research Scientist

IOCB - Institute of Organic Chemistry and Biochemistry of the Czech Academy
of Sciences 
Prague, Czech Republic
  &
CEITEC - Central European Institute of Technology 
Brno, Czech Republic

email: teva...@gmail.com

website: https://sites.google.com/site/thomasevangelidishomepage/
___
PyMOL-users mailing list
Archives: http://www.mail-archive.com/pymol-users@lists.sourceforge.net
Unsubscribe: 
https://sourceforge.net/projects/pymol/lists/pymol-users/unsubscribe

Re: [PyMOL] How to catch PyMOL command exception within a Python script

2019-02-22 Thread Thomas Holder
Hi Thomas,

In general, the PyMOL API should raise pymol.CmdException if things go wrong. 
But in case of cmd.pair_fit() this wasn't happening (see my fix that I pushed 
few minutes ago: 
https://github.com/schrodinger/pymol-open-source/commit/b26d91c40d20344fef511ea9d6bb664a93f1bb4a
 )

cmd.select() correctly raises an exception, so if you want to make a script 
more robust against selection failures, you could always create a named 
selection first to check if it's valid.

tmpsele1 = cmd.get_unused_name('_sele1')
try:
cmd.select(tmpsele1, someexpression, 0)
except pymol.CmdException:
print('invalid selection')
finally:
cmd.delete(tmpsele1)


Cheers,
  Thomas

> On Feb 22, 2019, at 12:11 PM, Thomas Evangelidis  wrote:
> 
> Hi Thomas,
> 
> This is great! I can even include more atom types in the selection.
> Just for the record, is it possible to catch PyMOL exceptions like 
> "Selector-Error" from within a Python script? Is there any general strategy 
> to select which exception type to look for? In the past, I was catching 
> exceptions from 'tmalign' with "except AssertionError:" which doesn't work 
> for "Selector-Error".
> 
> Best,
> Thomas
> 
> 
> -- 
> ==
> Dr Thomas Evangelidis
> Research Scientist
> IOCB - Institute of Organic Chemistry and Biochemistry of the Czech Academy 
> of Sciences
> Prague, Czech Republic
>   & 
> CEITEC - Central European Institute of Technology
> Brno, Czech Republic 
> 
> email: teva...@gmail.com
> website: https://sites.google.com/site/thomasevangelidishomepage/

--
Thomas Holder
PyMOL Principal Developer
Schrödinger, Inc.



___
PyMOL-users mailing list
Archives: http://www.mail-archive.com/pymol-users@lists.sourceforge.net
Unsubscribe: 
https://sourceforge.net/projects/pymol/lists/pymol-users/unsubscribe

Re: [PyMOL] How to catch PyMOL command exception within a Python script

2019-02-22 Thread Thomas Evangelidis
Hi Thomas,

This is great! I can even include more atom types in the selection.
Just for the record, is it possible to catch PyMOL exceptions like
"Selector-Error" from within a Python script? Is there any general strategy
to select which exception type to look for? In the past, I was catching
exceptions from 'tmalign' with "except AssertionError:" which doesn't work
for "Selector-Error".

Best,
Thomas


On Fri, 22 Feb 2019 at 11:28, Thomas Holder 
wrote:

> Hi Thomas,
>
> Getting rid of the string length limitations would need some major
> refactoring. There is currently no good workaround, other than avoiding
> such long selections.
>
> In this particular case, the script could use cmd.select_list() instead of
> concatenating the index list to a string:
>
> cmd.select_list('sel1', obj1, [int(i) for i in id1], mode='index')
> cmd.select_list('sel2', obj2, [int(i) for i in id2], mode='index')
> cmd.pair_fit("sel1 and aln", "sel2 and aln")
> cmd.delete('sel1')
> cmd.delete('sel2')
>
> Hope that helps.
>
> Cheers,
>   Thomas
>
>
> > On Feb 22, 2019, at 9:32 AM, Thomas Evangelidis 
> wrote:
> >
> > Greetings,
> >
> > I am trying to run focus_alignment command (
> https://pymolwiki.org/index.php/Focus_alignment) from within a Python
> script with my own custom selection (I include CA+CB atoms instead of only
> CA as in the default). Sometimes the selection is very large and PyMOL
> cannot hand it:
> >
> > Selector-Error: Word too long. Truncated:
> > Selector-Error:
> 183+186+191+194+196+199+203+206+208+211+437+440+445+448+456+459+465+468+473+477+480+483+486+490+493+496+499+505+508+512+515+523+526+530+534+537+541+544+827+830+832+835+1138+1141+1150+1153+1155+115Selector-Error:
> Invalid selection name "730".
> > poly and 4kz6A and aln and index
> 183+186+191+194+196+199+203+206+208+211+437+440+445+448+456+459+465+468+473+477+480+483+486+490+493+496+499+505+508+512+515+523+526+530+534+537+541+544+827+830+832+835+1138+1141+1150+1153+1155+1158+1175+1178+1183+1421+1424+1428+1431+1647+1650+1655+1658+
> 730<--
> > Selector-Error: Word too long. Truncated:
> > Selector-Error:
> 216+219+224+227+229+232+236+239+241+244+479+482+487+490+498+501+507+510+515+519+522+525+528+532+535+538+541+547+550+554+557+565+568+572+576+579+583+586+873+876+878+881+1192+1195+1204+1207+1209+121Selector-Error:
> Invalid selection name "868".
> > poly and 2r9wA and aln and index
> 216+219+224+227+229+232+236+239+241+244+479+482+487+490+498+501+507+510+515+519+522+525+528+532+535+538+541+547+550+554+557+565+568+572+576+579+583+586+873+876+878+881+1192+1195+1204+1207+1209+1212+1229+1232+1237+1491+1494+1498+1501+1747+1750+1755+1758+
> 868<--
> > ExecutiveRMS-Error: No atoms selected.
> > Selector-Error: Word too long. Truncated:
> > Selector-Error:
> 183+186+191+194+196+199+203+206+208+211+437+440+445+448+456+459+465+468+473+477+480+483+486+490+493+496+499+505+508+512+515+523+526+530+534+537+541+544+827+830+832+835+1138+1141+1150+1153+1155+115Selector-Error:
> Word too long. Truncated:
> > Selector-Error:
> 216+219+224+227+229+232+236+239+241+244+479+482+487+490+498+501+507+510+515+519+522+525+528+532+535+538+541+547+550+554+557+565+568+572+576+579+583+586+873+876+878+881+1192+1195+1204+1207+1209+121Selector-Error:
> Invalid selection name "730".
> > ( ( poly and 4kz6A and aln and index
> 183+186+191+194+196+199+203+206+208+211+437+440+445+448+456+459+465+468+473+477+480+483+486+490+493+496+499+505+508+512+515+523+526+530+534+537+541+544+827+830+832+835+1138+1141+1150+1153+1155+1158+1175+1178+1183+1421+1424+1428+1431+1647+1650+1655+1658+
> 730 ) in ( poly and 2r9wA and aln and index
> 216+219+224+227+229+232+236+239+241+244+479+482+487+490+498+501+507+510+515+519+522+525+528+532+535+538+541+547+550+554+557+565+568+572+576+579+583+586+873+876+878+881+1192+1195+1204+1207+1209+1212+1229+1232+1237+1491+1494+1498+1501+1747+1750+1755+1758+
> 868 ) )<--
> >
> > Is there a way to ask PyMOL to accept longer selection strings?
> > Alternatively, is it possible to catch this exception from within the
> script using "try: ... except :" statement?
> >
> > Thanks in advance.
> > Thomas
> >
> > --
> > ==
> > Dr Thomas Evangelidis
> > Research Scientist
> > IOCB - Institute of Organic Chemistry and Biochemistry of the Czech
> Academy of Sciences
> > Prague, Czech Republic
> >   &
> > CEITEC - Central European Institute of Technology
> > Brno, Czech Republic
> >
> > email: teva...@gmail.com
> > website: https://sites.google.com/site/thomasevangelidishomepage/
>
> --
> Thomas Holder
> PyMOL Principal Developer
> Schrödinger, Inc.
>
>

-- 

==

Dr Thomas Evangelidis

Research Scientist

IOCB - Institute of Organic Chemistry and Biochemistry of the Czech Academy
of Sciences 
Prague, Czech Republic
  &
CEITEC - Central European Institute of Technology 

Re: [PyMOL] How to catch PyMOL command exception within a Python script

2019-02-22 Thread Thomas Holder
Hi Thomas,

Getting rid of the string length limitations would need some major refactoring. 
There is currently no good workaround, other than avoiding such long selections.

In this particular case, the script could use cmd.select_list() instead of 
concatenating the index list to a string:

cmd.select_list('sel1', obj1, [int(i) for i in id1], mode='index')
cmd.select_list('sel2', obj2, [int(i) for i in id2], mode='index')
cmd.pair_fit("sel1 and aln", "sel2 and aln")
cmd.delete('sel1')
cmd.delete('sel2')

Hope that helps.

Cheers,
  Thomas


> On Feb 22, 2019, at 9:32 AM, Thomas Evangelidis  wrote:
> 
> Greetings,
> 
> I am trying to run focus_alignment command 
> (https://pymolwiki.org/index.php/Focus_alignment) from within a Python script 
> with my own custom selection (I include CA+CB atoms instead of only CA as in 
> the default). Sometimes the selection is very large and PyMOL cannot hand it:
> 
> Selector-Error: Word too long. Truncated:
> Selector-Error: 
> 183+186+191+194+196+199+203+206+208+211+437+440+445+448+456+459+465+468+473+477+480+483+486+490+493+496+499+505+508+512+515+523+526+530+534+537+541+544+827+830+832+835+1138+1141+1150+1153+1155+115Selector-Error:
>  Invalid selection name "730".
> poly and 4kz6A and aln and index 
> 183+186+191+194+196+199+203+206+208+211+437+440+445+448+456+459+465+468+473+477+480+483+486+490+493+496+499+505+508+512+515+523+526+530+534+537+541+544+827+830+832+835+1138+1141+1150+1153+1155+1158+1175+1178+1183+1421+1424+1428+1431+1647+1650+1655+1658+
>  730<--
> Selector-Error: Word too long. Truncated:
> Selector-Error: 
> 216+219+224+227+229+232+236+239+241+244+479+482+487+490+498+501+507+510+515+519+522+525+528+532+535+538+541+547+550+554+557+565+568+572+576+579+583+586+873+876+878+881+1192+1195+1204+1207+1209+121Selector-Error:
>  Invalid selection name "868".
> poly and 2r9wA and aln and index 
> 216+219+224+227+229+232+236+239+241+244+479+482+487+490+498+501+507+510+515+519+522+525+528+532+535+538+541+547+550+554+557+565+568+572+576+579+583+586+873+876+878+881+1192+1195+1204+1207+1209+1212+1229+1232+1237+1491+1494+1498+1501+1747+1750+1755+1758+
>  868<--
> ExecutiveRMS-Error: No atoms selected.
> Selector-Error: Word too long. Truncated:
> Selector-Error: 
> 183+186+191+194+196+199+203+206+208+211+437+440+445+448+456+459+465+468+473+477+480+483+486+490+493+496+499+505+508+512+515+523+526+530+534+537+541+544+827+830+832+835+1138+1141+1150+1153+1155+115Selector-Error:
>  Word too long. Truncated:
> Selector-Error: 
> 216+219+224+227+229+232+236+239+241+244+479+482+487+490+498+501+507+510+515+519+522+525+528+532+535+538+541+547+550+554+557+565+568+572+576+579+583+586+873+876+878+881+1192+1195+1204+1207+1209+121Selector-Error:
>  Invalid selection name "730".
> ( ( poly and 4kz6A and aln and index 
> 183+186+191+194+196+199+203+206+208+211+437+440+445+448+456+459+465+468+473+477+480+483+486+490+493+496+499+505+508+512+515+523+526+530+534+537+541+544+827+830+832+835+1138+1141+1150+1153+1155+1158+1175+1178+1183+1421+1424+1428+1431+1647+1650+1655+1658+
>  730 ) in ( poly and 2r9wA and aln and index 
> 216+219+224+227+229+232+236+239+241+244+479+482+487+490+498+501+507+510+515+519+522+525+528+532+535+538+541+547+550+554+557+565+568+572+576+579+583+586+873+876+878+881+1192+1195+1204+1207+1209+1212+1229+1232+1237+1491+1494+1498+1501+1747+1750+1755+1758+
>  868 ) )<--
> 
> Is there a way to ask PyMOL to accept longer selection strings?
> Alternatively, is it possible to catch this exception from within the script 
> using "try: ... except :" statement?
> 
> Thanks in advance.
> Thomas
> 
> -- 
> ==
> Dr Thomas Evangelidis
> Research Scientist
> IOCB - Institute of Organic Chemistry and Biochemistry of the Czech Academy 
> of Sciences
> Prague, Czech Republic
>   & 
> CEITEC - Central European Institute of Technology
> Brno, Czech Republic 
> 
> email: teva...@gmail.com
> website: https://sites.google.com/site/thomasevangelidishomepage/

--
Thomas Holder
PyMOL Principal Developer
Schrödinger, Inc.



___
PyMOL-users mailing list
Archives: http://www.mail-archive.com/pymol-users@lists.sourceforge.net
Unsubscribe: 
https://sourceforge.net/projects/pymol/lists/pymol-users/unsubscribe

[PyMOL] How to catch PyMOL command exception within a Python script

2019-02-22 Thread Thomas Evangelidis
Greetings,

I am trying to run focus_alignment command (
https://pymolwiki.org/index.php/Focus_alignment) from within a Python
script with my own custom selection (I include CA+CB atoms instead of only
CA as in the default). Sometimes the selection is very large and PyMOL
cannot hand it:

Selector-Error: Word too long. Truncated:
> Selector-Error:
> 183+186+191+194+196+199+203+206+208+211+437+440+445+448+456+459+465+468+473+477+480+483+486+490+493+496+499+505+508+512+515+523+526+530+534+537+541+544+827+830+832+835+1138+1141+1150+1153+1155+115Selector-Error:
> Invalid selection name "730".
> poly and 4kz6A and aln and index
> 183+186+191+194+196+199+203+206+208+211+437+440+445+448+456+459+465+468+473+477+480+483+486+490+493+496+499+505+508+512+515+523+526+530+534+537+541+544+827+830+832+835+1138+1141+1150+1153+1155+1158+1175+1178+1183+1421+1424+1428+1431+1647+1650+1655+1658+
> 730<--
> Selector-Error: Word too long. Truncated:
> Selector-Error:
> 216+219+224+227+229+232+236+239+241+244+479+482+487+490+498+501+507+510+515+519+522+525+528+532+535+538+541+547+550+554+557+565+568+572+576+579+583+586+873+876+878+881+1192+1195+1204+1207+1209+121Selector-Error:
> Invalid selection name "868".
> poly and 2r9wA and aln and index
> 216+219+224+227+229+232+236+239+241+244+479+482+487+490+498+501+507+510+515+519+522+525+528+532+535+538+541+547+550+554+557+565+568+572+576+579+583+586+873+876+878+881+1192+1195+1204+1207+1209+1212+1229+1232+1237+1491+1494+1498+1501+1747+1750+1755+1758+
> 868<--
> ExecutiveRMS-Error: No atoms selected.
> Selector-Error: Word too long. Truncated:
> Selector-Error:
> 183+186+191+194+196+199+203+206+208+211+437+440+445+448+456+459+465+468+473+477+480+483+486+490+493+496+499+505+508+512+515+523+526+530+534+537+541+544+827+830+832+835+1138+1141+1150+1153+1155+115Selector-Error:
> Word too long. Truncated:
> Selector-Error:
> 216+219+224+227+229+232+236+239+241+244+479+482+487+490+498+501+507+510+515+519+522+525+528+532+535+538+541+547+550+554+557+565+568+572+576+579+583+586+873+876+878+881+1192+1195+1204+1207+1209+121Selector-Error:
> Invalid selection name "730".
> ( ( poly and 4kz6A and aln and index
> 183+186+191+194+196+199+203+206+208+211+437+440+445+448+456+459+465+468+473+477+480+483+486+490+493+496+499+505+508+512+515+523+526+530+534+537+541+544+827+830+832+835+1138+1141+1150+1153+1155+1158+1175+1178+1183+1421+1424+1428+1431+1647+1650+1655+1658+
> 730 ) in ( poly and 2r9wA and aln and index
> 216+219+224+227+229+232+236+239+241+244+479+482+487+490+498+501+507+510+515+519+522+525+528+532+535+538+541+547+550+554+557+565+568+572+576+579+583+586+873+876+878+881+1192+1195+1204+1207+1209+1212+1229+1232+1237+1491+1494+1498+1501+1747+1750+1755+1758+
> 868 ) )<--


Is there a way to ask PyMOL to accept longer selection strings?
Alternatively, is it possible to catch this exception from within the
script using "try: ... except :" statement?

Thanks in advance.
Thomas

-- 

==

Dr Thomas Evangelidis

Research Scientist

IOCB - Institute of Organic Chemistry and Biochemistry of the Czech Academy
of Sciences 
Prague, Czech Republic
  &
CEITEC - Central European Institute of Technology 
Brno, Czech Republic

email: teva...@gmail.com

website: https://sites.google.com/site/thomasevangelidishomepage/
___
PyMOL-users mailing list
Archives: http://www.mail-archive.com/pymol-users@lists.sourceforge.net
Unsubscribe: 
https://sourceforge.net/projects/pymol/lists/pymol-users/unsubscribe