Talk:ListSelection2

From PyMOLWiki
Revision as of 14:40, 2 March 2021 by OscarConchillo-Solé (talk | contribs) (Created page with "I modified this script so it runs with python3 and it also works when your selection only contains one residue. I did not test it extensively, so I paste it here insted of mo...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

I modified this script so it runs with python3 and it also works when your selection only contains one residue.

I did not test it extensively, so I paste it here insted of modifying the original one




from pymol import cmd, stored
 
def listselection (selection, output="S", HOH="Y"):
    """    
    usage: listselection selection, [output=N/S/P, [HOH=Y/N ]]
    
    More information at: PymolWiki: http://http://pymolwiki.org/index.php/ListSelection2
    AUTHOR: Pietro Gatti-Lafranconi, 2013
    Please inform me if you use/improve/like/dislike/publish with this script.
    CC BY-NC-SA
    """
    printedselection=""
    extra=""
    counter=0
    sel=selection
    objs=cmd.get_object_list(sel)

    if HOH=="N":
        sel=selection+" and not resn HOH"
        extra=", without HOH"

    for a in range(len(objs)):
        m1=cmd.get_model(sel+" and "+objs[a])
        printedselection+="%s/%s/%s/%s\n" % (objs[a], m1.atom[0].chain, m1.atom[0].resn, m1.atom[0].resi)
        counter+=1
        for x in range(1,len(m1.atom),1):
            if m1.atom[x-1].resi!=m1.atom[x].resi:
                printedselection+="%s/%s/%s/%s\n" % (objs[a], m1.atom[x].chain, m1.atom[x].resn, m1.atom[x].resi)
                counter+=1

    print("Residues in '%s%s': %s" % (selection, extra, counter))
    if output=="S": print(printedselection)
    if output=="P":
        f=open('listselection_'+selection+'.txt','w')
        f.write("Residues in '%s%s': %s\n" % (selection, extra, counter))
        f.write(printedselection)
        f.close()
        print("Results saved in listselection_%s.txt" % selection)
        
cmd.extend('listselection',listselection)