[Chimera-users] Loop through hundreds of files and find HBonds

Samuel Kyobe samuelkyobe at gmail.com
Tue Apr 19 12:01:16 PDT 2022


Hi Eric

I now have a new challenge. My files are in several folders as shown below:
		.
		├── 8_C_pol
		│   ├── 247_CnA1_pol_frag_1_conf_36_out_01.pdb
		│   ├── 247_CnA1_pol_frag_1_conf_62_out_07.pdb
		│   └── HLA_5w6a_model_4.pdb
		└── 94_C_vif
		    ├── 247_CnA1_pol_frag_1_conf_36_out_01.pdb
		    ├── 247_CnA1_pol_frag_1_conf_62_out_07.pdb
		    └── HLA_5w6a_model_4.pdb


I would like to iterate through the folders and execute the commands with the original script below (only works inside one folder):

	1	import chimera
	2	import os
	3	import glob
	4
	5	from chimera import runCommand as rc # use 'rc' as shorthand for runCommand
	6	from chimera import replyobj # for emitting status messages
	7
	8	# change to folder with data files
	9	os.chdir("/Volumes/Sammy/Grants/H3Africa/CAfGEN/CAfGEN_PhD_Work/Allele_mechanisms/Mini_ligands_All/Auto_chimera”)
	10
	11	# absolute path to search all .pdb files of ligands
	12	path = r'*out*.pdb'
	13	files = glob.glob(path)
	14	print(files)
	15
	16	file_names = files
	17
	18	for files in file_names:
	19		my_mod=chimera.openModels.open('./HLA_5w6a_model_4.pdb',type="PDB")
	20		replyobj.status("Processing " + files)
	21		rc("open " + files)
	22		rc("addh")
	23		hbonds_output = files[:-4] + "hbonds.txt" # change ".pdb" to "hbonds.txt"
	24		rc("findhbond intermodel  true intramodel  false relax  false reveal  true saveFile " + hbonds_output)
	25		rc("close all")
	26	# uncommenting the line below will cause Chimera to exit when the script is done
	27	rc("stop now")

But I have tried the following modification but it returns errors. 

	1	import chimera
	2	import os
	3	import glob
	4
	5	from chimera import runCommand as rc # use 'rc' as shorthand for runCommand
	6	from chimera import replyobj # for emitting status messages
	7
	8	rootdir = '/Volumes/Sammy/Grants/H3Africa/CAfGEN/CAfGEN_PhD_Work/Allele_mechanisms/Mini_ligands_All/Auto_chimera/try/'
	9	for subdir, dirs, files in os.walk(rootdir):
	10
	12		for file in files:
	13			print os.path.join(file)
	14	
	15			path = r'*out*.pdb'
	16			glob.glob('*out*.pdb')
	17			print(file)
	18
	19			my_mod=chimera.openModels.open('./HLA_5w6a_model_4.pdb',type="PDB")
	20			replyobj.status("Processing " + file)
	21			rc("open " + file)
	22			rc("addh")
	23			hbonds_output = files[:-4] + "hbonds.txt" # change ".pdb" to "hbonds.txt"
	24			rc("findhbond intermodel  true intramodel  false relax  false reveal  true saveFile " + hbonds_output)
	25			rc("close all”)
	26
	27	# uncommenting the line below will cause Chimera to exit when the script is done
	28	rc("stop now")

After hours of trying various options on stack overflow, I have to turn to your expert help.

Thank you

Samuel




> On 15 Apr 2022, at 17:03, Eric Pettersen <pett at cgl.ucsf.edu> wrote:
> 
> You need a space between saveFile and the quote that follows it.
> 
>> On Apr 15, 2022, at 6:39 AM, Samuel Kyobe <samuelkyobe at gmail.com> wrote:
>> 
>> Hi Eric
>> 
>> I have modified the code:
>> 
>> 	hbonds_output = files[:-4] + "hbonds.txt" # change ".pdb" to "hbonds.txt"
>> 	rc("findhbond intermodel  true intramodel  false relax  false reveal  true saveFile" + hbonds_output)
>> 
>> But, the code returns the error below:
>> 
>> File "/private/var/folders/y2/p069gv9n6lsf1x8w08bym32w0000gn/T/AppTranslocation/AF4918BD-D759-418D-AA97-756E6B1D941C/d/Chimera.app/Contents/Resources/share/Midas/midas_text.py", line 3396, in _parseTyped
>>   % typed
>> MidasError: No value provided for keyword 'saveFile247_CnA1_pol_frag_1_conf_36_out_01hbonds.txt'
>> 
>> Not sure what could be causing the error.
>> 
>> Thank you
>> 
>> Samuel
>> 
>> 
>> 
>> 
>>> On 15 Apr 2022, at 04:02, Eric Pettersen <pett at cgl.ucsf.edu> wrote:
>>> 
>>> Hi Samuel,
>>> 	You know the ligand file name, so you could just modify that to make a corresponding unique output file name.  So between lines 22 and 23 you could do:
>>> 
>>> 	hbonds_output = files[:-4] + “.hbonds.txt” # change “.pdb” to “.hbonds.txt”
>>> 
>>> and your findhbond command would be rc("findhbond intermodel  true intramodel  false relax  false reveal  true saveFile “ + hbonds_output)
>>> 
>>> —Eric
>>> 
>>>> On Apr 14, 2022, at 6:10 AM, Samuel Kyobe via Chimera-users <chimera-users at cgl.ucsf.edu> wrote:
>>>> 
>>>> Hi Elaine
>>>> 
>>>> Thank you so much for your responses.
>>>> I have modified the recommended example as seen below:
>>>> 
>>>> 		1	import chimera
>>>> 		2	import os
>>>> 		3	import glob
>>>> 		4	
>>>> 		5	from chimera import runCommand as rc 
>>>> 		6	from chimera import replyobj 
>>>> 		7	
>>>> 		8	# change to folder with data files
>>>> 		9	os.chdir("/Volumes/Auto_chimera")
>>>> 		10	
>>>> 		11	# absolute path to search all .pdb files of ligands
>>>> 		12	path = r'./*_out_*.pdb’ # several files with output name specified as *_out_*.pdb
>>>> 		13	files = glob.glob(path)
>>>> 		14	print(files)
>>>> 		15	
>>>> 		16	file_names = files
>>>> 		17	
>>>> 		18	for files in file_names:
>>>> 		19		my_mod=chimera.openModels.open('./HLA_5w6a_model_4.pdb',type="PDB")
>>>> 		20		replyobj.status("Processing " + files)
>>>> 		21		rc("open " + files)
>>>> 		22		rc("addh")
>>>> 		23		rc("findhbond intermodel  true intramodel  false relax  false reveal  true saveFile")
>>>> 		24		rc("close all")
>>>> 		25	rc("stop now”)
>>>> 
>>>> My current challenge is how to save the different resultant files with unique file_names (line 25).
>>>> 
>>>> Is there an alternative way I can call the files names after saveFile (line 25)? Your example doesn’t not work with my modified code.
>>>> 
>>>> Thank you
>>>> 
>>>> Samuel
>>>> 
>>>> 
>>>> 
>>>>> On 13 Apr 2022, at 19:04, Elaine Meng <meng at cgl.ucsf.edu> wrote:
>>>>> 
>>>>> Hi Samuel,
>>>>> Although the first link I sent you shows Chimera commands, the second link shows how to put those commands together with Python for looping.
>>>>> Elaine
>>>>> 
>>>>>> On Apr 13, 2022, at 9:00 AM, Samuel Kyobe via Chimera-users <chimera-users at cgl.ucsf.edu> wrote:
>>>>>> 
>>>>>> Dear Elaine
>>>>>> 
>>>>>> The challenge I have is that the Command Index is not written for Python which I would prefer to use for looping in several files.
>>>>>> 
>>>>>> I am using only Chimera I have no access to ChimeraX.
>>>>>> 
>>>>>> Thank you
>>>>>> 
>>>>>> Samuel
>>>>> 
>>>> 
>>>> 
>>>> _______________________________________________
>>>> Chimera-users mailing list: Chimera-users at cgl.ucsf.edu
>>>> Manage subscription: https://www.rbvi.ucsf.edu/mailman/listinfo/chimera-users
>>>> 
>>> 
>> 
>> 
> 




More information about the Chimera-users mailing list