Answers.Body: <p>Firstly, I would preface that the PyMOL command line in the GUI is great, but <strong>you can run PyMOL as a regular Python library</strong> (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 <code>cmd.</code>) and PyMOLWiki documentation is only slightly weaker for them (<code>help()</code> fills the gaps).</p>
  
  <p>But if you really want to use the GUI route...</p>
  
  <p>Let's get the names of the models in the selection</p>
  
  <pre><code>onames = cmd.get_object_list('chains')
  print onames
  </code></pre>
  
  <p>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)</p>
  
  <p>And google that number as <code>&lt;n&gt; choose 5</code>. 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.</p>
  
  <pre><code> from itertools import combinations
   for s in combinations(onames, 5): cmd.save('-'.join(s)+'.pdb', ' or '.join(s))
  </code></pre>
  
  <p>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.</p>
  
  <pre><code> for i, s in enumerate(combinations(onames, 5)): cmd.save(str(i)+'.pdb', ' or '.join(s))
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-03T23:17:11.767Z
Answers.LastActivityDate: 2019-11-03T23:17:11.767Z
Answers.UserDisplayName: Matteo Ferla
Body: <p>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 :</p>
  
  <pre><code>fetch 3q2v
  symexp sym,3q2v,(3q2v),30
  select chains, bychain all within 30 of 3q2v 
  </code></pre>
  
  <p>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?</p>
  
Tags: <protein-structure><visualization><pymol>
Title: pymol script to only select 5 chains within a distance from a reference and save the selection
ThreadText: # pymol script to only select 5 chains within a distance from a reference and save the selection
  
  (Asked by: username_0 on 2019-11-03)
  
  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?
  
  **Tags:** protein-structure, visualization, pymol
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-11-03. Score: 1 | Accepted answer
  
  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))
  
  ```
  
  
  ---
  
  