[chimerax-users] python script for measuring distance

Rangarajan, Amith Amith.Rangarajan at ucsf.edu
Sun Feb 13 18:59:50 PST 2022


Dear tony,

Thanks for this very helpful tip ! it writes out the values into a separate csv file with the filenames and distance values.

including the code below :
_________________

import os
from chimerax.core.commands import run as rc


target_folder = '/wynton/home/jacobson/avranga1008/Documents/0-combo'
os.chdir(target_folder)
home = os.getcwd()
xp_distance_file = os.path.join(home,'xp-distance-corrected.log')
xp_distance_obj = open(xp_distance_file, "w")

for f in os.listdir():
    if f.endswith('.mae'):
        print(f'working on {f}')
        rc(session, "open " + f)
        rc(session, "measure center #1.1 & ligand mark true")
        rc(session, "measure center #1.1 :310,335,728,759 mark true")
        # grab distance
        dist = rc(session, "distance #2 #3")
        rc(session, "close")
        xp_distance_obj.write(f"{f},{dist} \n")

xp_distance_obj.close()

exit()

------------------

thanks once again for your review,

best,
amith
________________________________
From: Anthony James Schaefer <tony.schaefer at uga.edu>
Sent: Saturday, February 12, 2022 2:18 PM
To: ChimeraX Users Help <ChimeraX-users at cgl.ucsf.edu>; Rangarajan, Amith <Amith.Rangarajan at ucsf.edu>
Subject: Re: python script for measuring distance

Amith, The "run" function can return a value depending on the command that is executed. For the distance command, the distance is returned as a floating-point number. If I'm understanding what you're trying to do, this should work ZjQcmQRYFpfptBannerStart
This Message Is From an External Sender
This message came from outside your organization.
ZjQcmQRYFpfptBannerEnd
Amith,

The "run" function can return a value depending on the command that is executed. For the distance command, the distance is returned as a floating-point number. If I'm understanding what you're trying to do, this should work (I have not tested this):

import os
from chimerax.core.commands import run as rc


target_folder = '/wynton/home/jacobson/avranga1008/Documents/0-combo'
os.chdir(target_folder)
home = os.getcwd()
xp_distance_file = os.path.join(home,'xp-distance.log')
xp_distance_obj = open(xp_distance_file, "w")

for f in os.listdir():
    if f.endswith('.mae'):
        print(f'working on {f}')
        rc(session, "open " + f)
        rc(session, "measure center #1.1 & ligand mark true")
        rc(session, "measure center #1.1 :310,335,728,759 mark true")
        # grab distance
        dist = rc(session, "distance #2 #3")
        rc(session, "close")
        xp_distance_obj.write("%s    %.2f" % (f, dist))

xp_distance_obj.close()

exit()

Best,

Tony
________________________________
From: ChimeraX-users <chimerax-users-bounces at cgl.ucsf.edu> on behalf of Rangarajan, Amith via ChimeraX-users <chimerax-users at cgl.ucsf.edu>
Sent: Saturday, February 12, 2022 9:26 AM
To: ChimeraX Users Help <ChimeraX-users at cgl.ucsf.edu>
Subject: Re: [chimerax-users] python script for measuring distance

[EXTERNAL SENDER - PROCEED CAUTIOUSLY]

Hello everyone, Please see my script below: import os import sys from chimerax.core.commands import run as rc target_folder = '/wynton/home/jacobson/avranga1008/Documents/0-combo' os.chdir(target_folder) home =os.getcwd() ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ZjQcmQRYFpfptBannerStart
This Message Is From an External Sender
This message came from outside your organization.
ZjQcmQRYFpfptBannerEnd
Hello everyone,

Please see my script below:

import os
import sys
from chimerax.core.commands import run as rc
target_folder = '/wynton/home/jacobson/avranga1008/Documents/0-combo'
os.chdir(target_folder)
home =os.getcwd()
all = os.listdir()
xp_distance_file = os.path.join(home,'xp-distance.log')
print_to_screen = sys.stdout
mae_files =[]
for f in all:
    if f.endswith('.mae'):
        mae_files.append(f)
        print(f'working on {f}')
        sys.stdout = open(xp_distance_file,'a')
        rc(session,"open " + f)
        rc(session,"measure center #1.1 & ligand mark true")
        rc(session,"measure center #1.1 :310,335,728,759 mark true")
        rc(session,"distance #2 #3")
        rc(session,"close")
        sys.stdout = print_to_screen
print(f'completed writing xp-distance values to {xp_distance_file} ; now moving to next loop')
for f in mae_files:
    line = f + '\n'
    open('file_list.txt','a').write(line)
print("loop is complete")
exit()

I am curently using stdout to write the distance values to a file .I am currently running it on command line
[cid:68afa919-1a35-470f-808c-242e97c85957]

 it seems like a round about way to do it. The output file has lot of special characters and it takes a bit of effort using regex to extract the values.

 I think there might be some logger in chimerax which can do this or some other better way to write results to a file  , but i am not able to find it.
#############



please let me know your suggestions on improving this code.

thanks,
amith


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.rbvi.ucsf.edu/pipermail/chimerax-users/attachments/20220214/2a8c789d/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 3867 bytes
Desc: image.png
URL: <http://www.rbvi.ucsf.edu/pipermail/chimerax-users/attachments/20220214/2a8c789d/attachment.png>


More information about the ChimeraX-users mailing list