<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.pymol.org/index.php?action=history&amp;feed=atom&amp;title=CollapseSel</id>
	<title>CollapseSel - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.pymol.org/index.php?action=history&amp;feed=atom&amp;title=CollapseSel"/>
	<link rel="alternate" type="text/html" href="https://wiki.pymol.org/index.php?title=CollapseSel&amp;action=history"/>
	<updated>2026-07-04T19:11:38Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.35.1</generator>
	<entry>
		<id>https://wiki.pymol.org/index.php?title=CollapseSel&amp;diff=3410&amp;oldid=prev</id>
		<title>Pyadmin: 4 revisions</title>
		<link rel="alternate" type="text/html" href="https://wiki.pymol.org/index.php?title=CollapseSel&amp;diff=3410&amp;oldid=prev"/>
		<updated>2014-03-28T01:21:03Z</updated>

		<summary type="html">&lt;p&gt;4 revisions&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;= Overview =&lt;br /&gt;
[[CollapseSel]] is a small utility function to compress strings for selections.  So, if  you have a selection with residues 1+2+3+4+20+21+22+100-120 this will return, 1-4+20-22+100-120.&lt;br /&gt;
&lt;br /&gt;
'''CollapseIDs''' is a small utility function to compress strings for an array of IDs.  This does NOT have the logic for detecting duplicates, [[CollapseSel]] does.&lt;br /&gt;
&lt;br /&gt;
=Example=&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
run /dir/to/collapseSel.py&lt;br /&gt;
fetch 1cll&lt;br /&gt;
select EE, resn GLU&lt;br /&gt;
print collapseSel(&amp;quot;EE&amp;quot;)&lt;br /&gt;
#&lt;br /&gt;
# and PyMOL should output:&lt;br /&gt;
#&lt;br /&gt;
# 6-7+11+14+31+45+47+54+67+82-84+87+104+114+119-120+123+127+139-140&lt;br /&gt;
#&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=The Code=&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import pymol&lt;br /&gt;
from pymol import stored&lt;br /&gt;
&lt;br /&gt;
def collapseIDs(ids):&lt;br /&gt;
	&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
	Helper function to make a smart list of IDs: eg turns 1+2+3+4+5 into 1-5.&lt;br /&gt;
	&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
	rVal = []&lt;br /&gt;
	if len(ids)==0:&lt;br /&gt;
		return &amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
	scanning=False&lt;br /&gt;
	anchor = 0&lt;br /&gt;
	start = 0&lt;br /&gt;
	# 1-5 7-10 12 21-23&lt;br /&gt;
	for cur in range(0,len(ids)-1):&lt;br /&gt;
		if ids[cur]+1 == ids[cur+1]:&lt;br /&gt;
			if scanning:&lt;br /&gt;
				scanning=True&lt;br /&gt;
				continue&lt;br /&gt;
			else:&lt;br /&gt;
				scanning=True&lt;br /&gt;
				start = cur&lt;br /&gt;
		else:&lt;br /&gt;
			if scanning:&lt;br /&gt;
				scanning=False&lt;br /&gt;
				rVal.append(str(ids[start]) + &amp;quot;-&amp;quot; + str(ids[cur]))&lt;br /&gt;
				start = cur&lt;br /&gt;
			else:&lt;br /&gt;
				scanning=False&lt;br /&gt;
				rVal.append(str(ids[cur]))&lt;br /&gt;
	if scanning:&lt;br /&gt;
		rVal.append( str(ids[start]) + &amp;quot;-&amp;quot; + str(ids[cur+1]))&lt;br /&gt;
	else:&lt;br /&gt;
		rVal.append(str(ids[-1]))&lt;br /&gt;
	return rVal&lt;br /&gt;
&lt;br /&gt;
def collapseSel(sel=None,lType=&amp;quot;resi&amp;quot;):&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
        collapseSel -- given a valid PyMOL selection and list type, return a collapsed&lt;br /&gt;
                list of numbers corresponding to the lType.  For example, to compactly&lt;br /&gt;
                print the residue identifiers for all the waters, try:&lt;br /&gt;
                        select theWaters, resn HOH&lt;br /&gt;
                        print collapseSel( &amp;quot;theWaters&amp;quot; )&lt;br /&gt;
&lt;br /&gt;
                This will convert: 1+2+3+4+10+11+12 into 1-4+10-12&lt;br /&gt;
&lt;br /&gt;
        PARAMS&lt;br /&gt;
                sel&lt;br /&gt;
                        The selection to collapse&lt;br /&gt;
                lType&lt;br /&gt;
                        The identifier type: 'resi', 'ID', 'id', or any numerical property.&lt;br /&gt;
&lt;br /&gt;
        RETURNS&lt;br /&gt;
                a string of collapsed IDs.&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
        if sel==None:&lt;br /&gt;
                return &amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
        stored.s = set()&lt;br /&gt;
        cmd.iterate( sel, &amp;quot;stored.s.add(int(float(%s)))&amp;quot; % lType)&lt;br /&gt;
        l = list(stored.s)&lt;br /&gt;
        l.sort()&lt;br /&gt;
        return &amp;quot;+&amp;quot;.join(collapseIDs(list(l)))&lt;br /&gt;
&lt;br /&gt;
cmd.extend(&amp;quot;collapseSel&amp;quot;, collapseSel)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:ObjSel_Scripts]]&lt;br /&gt;
[[Category:Script_Library]]&lt;/div&gt;</summary>
		<author><name>Pyadmin</name></author>
	</entry>
</feed>