Talk:ColorByRMSD

From PyMOLWiki
Revision as of 21:55, 13 July 2009 by Inchoate (talk | contribs) (Created page with 'Shiven, Thanks for starting this! I took your code and then generalized and modified it. This is a step closer to being a general, working function. By that I mean, one shoul…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Shiven,

Thanks for starting this! I took your code and then generalized and modified it. This is a step closer to being a general, working function. By that I mean, one should simply be able to run:

run /path/to/colorByRMSD.py
fetch 1cll 1ggz, async=0
colorByRMSD 1cll, 1ggz, doAlign=True, doPretty=True

but for some reason I have to do:

run /path/to/colorByRMSD.py
fetch 1cll 1ggz, async=0
# coloring is messed up w/o this
alter *, b=0
colorByRMSD 1cll, 1ggz, doAlign=True, doPretty=True

Notes:

  • I prefer to leave the users' objects untouched and work on copies.
  • doAlign = True then align the originals; else leave the originals where they lie
  • doPretty = True then color & highlight the alignments; else leave everything alone
  • It's not ideal
  • We need to remove the prints and other cruft before posting

Here's my most recent attempt:

"""
--- ColorByRMSD: RMSD based coloring --- 
Author  : Shivender Shandilya, modified by Jason Vertrees
Program : ColByRMS
Date    : July 2009
Version : 0.0.2 (very very alpha!)
Mail    : firstname.lastname@umassmed.edu
 
Keywords: color rms rmsd colorbyrms colorbyrmsd
----------------------------------------------------------------------
Reference:
 This email from Warren - http://www.mail-archive.com/pymol-users@lists.sourceforge.net/msg07078.html
Literature:
 DeLano, W.L. The PyMOL Molecular Graphics System (2002) DeLano Scientific, San Carlos, CA, USA. http://www.pymol.org
----------------------------------------------------------------------
"""
 
import pymol
import cmd
from pymol import stored
 
def strTrue(p):
    return p[0].upper() == "T"
 
# The main function that assigns "cur_rms" as the new b-factor
def rmsUpdateB(objA, alnAri, objB, alnBri):
    # don't need the *10 -- PyMOL scales things for us.
    for x in range(len(alnAri)):
        s1 = objA + " and n. CA and i. " + alnAri[x]
        s2 = objB + " and n. CA and i. " + alnBri[x]
        rmsd = cmd.rms_cur(s1, s2, matchmaker=4)
        cmd.alter( s1, "b = " + str(rmsd))
        cmd.alter( s2, "b = " + str(rmsd))
    cmd.sort(objA); cmd.sort(objB)

 
def colorByRMSD(objSel1, objSel2, doAlign="True", doPretty=None):
    # create backup copies; names starting with _ (underscores) are
    # hidden by PyMOL
    tObj1, tObj2, aln = "__tempObj1", "__tempObj2", "__aln"
    
    if strTrue(doAlign):
        # perform the alignment
        print "Aligning"
        cmd.create( tObj1, objSel1 )
        cmd.create( tObj2, objSel2 )
        cmd.super( tObj1, tObj2, object=aln )
        # bug -- every other call undoes this...
        cmd.matrix_copy(tObj1, objSel1)
    else:
        print "Not aligning"
        # perform the alignment
        cmd.create( tObj1, objSel1 )
        cmd.create( tObj2, objSel2 )
        cmd.super( tObj1, tObj2, object=aln )

    print "Altering objects"
    cmd.alter( tObj1 + " or " + tObj2, "b=-10")
    cmd.alter( tObj1 + " or " + tObj2, "chain='A'")
    cmd.alter( tObj1 + " or " + tObj2, "segi='A'")
    # update PyMOL;
    # one of these should do the trick
    cmd.refresh(); cmd.rebuild(); cmd.sort(tObj1); cmd.sort(tObj2)
    
    #  Get the residue identifiers from the aln object
    stored.alnAres, stored.alnBres = [], []
    cmd.iterate(tObj1 + " and n. CA and " + aln, "stored.alnAres.append(resi)")
    cmd.iterate(tObj2 + " and n. CA and " + aln, "stored.alnBres.append(resi)")

    # reset the b-factors for each object
    rmsUpdateB(tObj1,stored.alnAres,tObj2,stored.alnBres)

    # Store the NEW b-factors
    stored.alnAnb, stored.alnBnb = [], []
    cmd.iterate(tObj1 + " and n. CA and " + aln, "stored.alnAnb.append(b)" )
    cmd.iterate(tObj2 + " and n. CA and " + aln, "stored.alnBnb.append(b)" )
  
    # Get rid of all intermediate objects etc.; clean up
    cmd.delete(tObj1)
    cmd.delete(tObj2)
    cmd.delete(aln)
    
    # Assign the just stored NEW b-factors to the original object
    for x in range(len(stored.alnAres)):
        cmd.alter(objSel1 + " and n. CA and i. " + str(stored.alnAres[x]), "b = " + str(stored.alnAnb[x]))
    for x in range(len(stored.alnBres)):
        cmd.alter(objSel2 + " and n. CA and i. " + str(stored.alnBres[x]), "b = " + str(stored.alnBnb[x]))
    cmd.rebuild(); cmd.refresh(); cmd.sort(objSel1); cmd.sort(objSel2)

    if doPretty!=None:
        # Showcase what we did
        cmd.orient()
        cmd.hide("all")
        cmd.show_as("cartoon", objSel1 + " or " + objSel2)
        print "\n"
        print "Colored by 'overall' RMSD...\n"
        cmd.spectrum("b", 'rainbow',  objSel1 + " or " + objSel2 )

cmd.extend("colorByRMSD", colorByRMSD)

Tree 02:55, 14 July 2009 (UTC)