[chimerax-users] python loop with ChimeraX

Elaine Meng meng at cgl.ucsf.edu
Wed Jul 21 12:24:34 PDT 2021


Hi Amith,
The logical error in your workflow is that if you always save the defattr files with the same name, then you will only have results for one structure, probably the last one that was opened.

If you want to save all the results for all of the structures, you have to use "$file" to substitute in the name of the structure file into the names of the output defattr files, so that they will all have unique names.  This is described in the help for the "foreach" option:
<https://rbvi.ucsf.edu/chimerax/docs/user/commands/open.html#forEachFile>

e.g. in your sasa.cxc instead of this

save CYS.defattr attrName r:area selectedOnly true

...could be something like this

save $file-CYS.defattr attrName r:area selectedOnly true

Then you will get separate files of results for each input structure.
I hope this helps,
Elaine


> On Jul 21, 2021, at 11:55 AM, Rangarajan, Amith via ChimeraX-users <chimerax-users at cgl.ucsf.edu> wrote:
> 
> Hi,
> 
> Thanks for this suggestion. 
> 
> I updated my chimeraX to the latest(nightly) build and i wrote a cxc script to measure SASA of CYS,CSO,CSD,OCS residues . I added the commands into .cxc file and used tom's example ( open sasa.cxc foreach *.pdb)  to run it inside a folder with a set of 1000 pdb structures .  The SASA and defattr file is not getting updated for each pdb file.
> 
> A sample of the log is as shown below :
> open 7BY4_mm1.pdb
> select :CYS12 atoms, 10 bonds, 2 residues, 1 model selected
> save CYS.defattr attrName r:area selectedOnly trueSaved attribute 'area' of 0 residuesusing match mode: 1-to-1 to CYS.defattr
> select :CSO7 atoms, 6 bonds, 1 residue, 1 model selected
> save CSO.defattr attrName r:area selectedOnly trueSaved attribute 'area' of 0 residues using match mode: 1-to-1 to CSO.defattr
> 
> I have attached the output to this email along with the cxc script.
> 
> I look  forward to hearing  your thoughts regarding fixing this.
> 
> Sincerely appreciate your thoughts and comments,
> 
> best,
> Amith
> 
> 
> From: Tom Goddard <goddard at sonic.net>
> Sent: Tuesday, July 20, 2021 6:24 PM
> To: Rangarajan, Amith <Amith.Rangarajan at ucsf.edu>
> Cc: ChimeraX Users Help <ChimeraX-users at cgl.ucsf.edu>
> Subject: Re: [chimerax-users] python loop with ChimeraX
>  
> This Message Is From an External Sender 
> This message came from outside your organization. 
> Hi Amith,
> 
> It is also possible to run a ChimeraX command script (.cxc) file once on each PDB file in a directory.
> 
> open myscript.cxc foreach ~/data/*.cif
> 
> as described in the docs
> 
> https://www.rbvi.ucsf.edu/chimerax/docs/user/commands/open.html#forEachFile
> 
> That was added after the ChimeraX 1.2 release so would need the ChimeraX daily build to use it.
> 
> Tom
> 
> 
>> On Jul 20, 2021, at 5:42 PM, Rangarajan, Amith via ChimeraX-users <chimerax-users at cgl.ucsf.edu> wrote:
>> 
>> Dear Elaine  & Eric
>> 
>> Thanks a lot !. i now have a better sense of how to approach this . I will update this thread soon with the results.
>> 
>> 
>> best
>> Amith
>> From: Eric Pettersen <pett at cgl.ucsf.edu>
>> Sent: Tuesday, July 20, 2021 3:50 PM
>> To: ChimeraX Users Help <ChimeraX-users at cgl.ucsf.edu>
>> Cc: Rangarajan, Amith <Amith.Rangarajan at ucsf.edu>
>> Subject: Re: [chimerax-users] python loop with ChimeraX
>>  
>> This Message Is From an External Sender 
>> This message came from outside your organization. 
>> To supplement Elaine's answer, looping through a set of data files and applying commands to them is done in a fashion very similar to the way it is done in Chimera, and therefore the documentation for how to do it in Chimera is still pretty useful:  Very Basic Chimera Programming Primer
>> 
>> The differences are:
>> 
>> 1) ChimeraX is Python 3 whereas Chimera is Python 2.  That doesn't actually make any difference to the code shown in the example except for the import statements, which would have to change anyway.
>> 
>> 2) To run a command, instead of:
>> 
>> from chimera import runCommand
>> runCommand("color red")
>> 
>> it's:
>> 
>> from chimerax.core.commands import run
>> run(session, "color red")
>> 
>> 3) To issue status messages, instead of:
>> 
>> from chimera import replyobj
>> replyobj.status("Processing file")
>> 
>> it's just:
>> 
>> session.logger.status("Processing file")
>> 
>> --Eric
>> 
>> Eric Pettersen
>> UCSF Computer Graphics Lab
>> 
>> 
>>> On Jul 20, 2021, at 11:28 AM, Elaine Meng via ChimeraX-users <chimerax-users at cgl.ucsf.edu> wrote:
>>> 
>>> One correction, the saved attribute file should have "defattr" filename extension instead of "txt", so my examples should instead be:
>>> 
>>> save /location/myfile.defattr  attrName r:area
>>> save /location/myfile.defattr attrName r:area selectedOnly true
>>> 
>>> Sorry about that.
>>> Elaine
>>> 
>>>> On Jul 20, 2021, at 11:23 AM, Elaine Meng via ChimeraX-users <chimerax-users at cgl.ucsf.edu> wrote:
>>>> 
>>>> Hi Amith,
>>>> Seems like two parts to this question:
>>>> (A) what are the ChimeraX commands (which could be put into a .cxc file)?
>>>> (B) how to loop over multiple inputs?  ...alluded to by "python loop" in your subject line... .cxc files only contain ChimeraX commands, not python.
>>>> 
>>>> I don't know python, so somebody else would need to address part B, but I can give some hints on part A.  However, depending on the solution to part B, you might instead choose to just make a python script instead of using ChimeraX commands directly.  The usual procedure is to figure out the commands first, often with some trial and error of simply typing them in to the command line.
>>>> 
>>>> Relevant ChimeraX commands are "measure sasa" to assign residue SASA values as an attribute and then "save" to write the file of attribute values. 
>>>> 
>>>> <https://rbvi.ucsf.edu/chimerax/docs/user/commands/measure.html#sasa>
>>>> <https://rbvi.ucsf.edu/chimerax/docs/user/commands/save.html#attributes>
>>>> 
>>>> I'm guessing you want the SASA of the residues but in the context of the complex, i.e. if they are completely buried in the interface between two proteins even though they would be on the surface of one protein by itself, you would still expect SASA zero.  In that case, you'd want the surface to enclose all the proteins in one blob, and then get the SASA of individual residues, and the command would be something like
>>>> 
>>>> measure sasa protein setAttribute true
>>>> save /location/myfile.txt  attrName r:area
>>>> 
>>>> However, if you only want to specify certain residues instead of writing a file for all residues, you would need to use "select" command to select them and then save with the selectedOnly option, e.g. something like
>>>> 
>>>> select /A:24-53 /B:258
>>>> save /location/myfile.txt  attrName r:area selectedOnly true
>>>> 
>>>> For part B you may need to embed these in some python script where it keeps closing the previous structure, opening a new structure, and substituting a different name for each save file.
>>>> 
>>>> I hope this helps,
>>>> Elaine
>>>> -----
>>>> Elaine C. Meng, Ph.D.                       
>>>> UCSF Chimera(X) team
>>>> Department of Pharmaceutical Chemistry
>>>> University of California, San Francisco
>>>> 
>>>>> On Jul 20, 2021, at 10:12 AM, Rangarajan, Amith via ChimeraX-users <chimerax-users at cgl.ucsf.edu> wrote:
>>>>> 
>>>>> Hi,
>>>>> I am new to chimeraX , i want to loop through a set of biological assemblies  measuring SASA of  selecting residues and write them to a file. I need some help with starting on this .How to include this into the .cxc script? is there a example somewhere?
>>>>> please share your tips and suggestions,
>>>>> thanks everyone !
>>>>> warm regards,
>>>>> Amith
>>>> 




More information about the ChimeraX-users mailing list