# pymol script to only select 5 chains within a distance from a reference and save the selection

I am generating 30 asymmetric units of a protein structure. Now, I want to select different combinations of 5 units which are 30 Angstroms away from the reference and save it as a different file. Here is the command I could work out till now :

fetch 3q2v
symexp sym,3q2v,(3q2v),30
select chains, bychain all within 30 of 3q2v


How to loop in and select only 5 chains at a time obtained from the setup obtained by the above code, and save it in separate files?

Firstly, I would preface that the PyMOL command line in the GUI is great, but you can run PyMOL as a regular Python library (calling the appropriate python). This makes life so much simpler for more complex manipulations. You can do really complex manipulations with it. I cannot suggest it enough. The API version of the commands works only marginally differently, but you can do API calls in the GUI cmd.) and PyMOLWiki documentation is only slightly weaker for them (help() fills the gaps).

But if you really want to use the GUI route...

Let's get the names of the models in the selection

onames = cmd.get_object_list('chains')
print onames


Now, you want to save subsets of 5 chains (technically, objects or models). That is, you do not care about the order they are in and you don't want the same one twice (actually selections with 4 or less), so you want all combinations. Make sure to do the following print len(onames)

And google that number as <n> choose 5. That is how many files you will get. I am pretty sure most of these sets will be meaningful and will be useless. In a script a loop is easy, while in the gui you have to keep it simple or use a list coprehension hackishly.

 from itertools import combinations
for s in combinations(onames, 5): cmd.save('-'.join(s)+'.pdb', ' or '.join(s))


Obviously, you could alternatively use an enumerate method around the iterator and get an index for the file. But do note f-strings will not work.

 for i, s in enumerate(combinations(onames, 5)): cmd.save(str(i)+'.pdb', ' or '.join(s))