Re: [chimerax-users] python script for measuring distance

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

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@cgl.ucsf.edu> on behalf of Rangarajan, Amith via ChimeraX-users <chimerax-users@cgl.ucsf.edu> Sent: Saturday, February 12, 2022 9:26 AM To: ChimeraX Users Help <ChimeraX-users@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

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@uga.edu> Sent: Saturday, February 12, 2022 2:18 PM To: ChimeraX Users Help <ChimeraX-users@cgl.ucsf.edu>; Rangarajan, Amith <Amith.Rangarajan@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@cgl.ucsf.edu> on behalf of Rangarajan, Amith via ChimeraX-users <chimerax-users@cgl.ucsf.edu> Sent: Saturday, February 12, 2022 9:26 AM To: ChimeraX Users Help <ChimeraX-users@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
participants (2)
-
Anthony James Schaefer
-
Rangarajan, Amith