Difference between revisions of "User:Remig/plico/plicoCommon"

From Jmol
Jump to navigation Jump to search
m
m
Line 3: Line 3:
 
Copy and paste the following into a text editor and save in your scripts folder as plicoCommon.spt.
 
Copy and paste the following into a text editor and save in your scripts folder as plicoCommon.spt.
 
<pre>#  plicoCommon - Jmol script by Ron Mignery
 
<pre>#  plicoCommon - Jmol script by Ron Mignery
#  v1.5 beta    7/16/2014 -handle multiple frames+
+
#  v1.6 beta    7/16/2014 -handle multiple frames+
 
#
 
#
 
#  Routines and values common to all Plico suite scripts
 
#  Routines and values common to all Plico suite scripts
Line 16: Line 16:
 
gMinNo = 1
 
gMinNo = 1
 
gMaxNo = 9999
 
gMaxNo = 9999
gOK = TRUE # global return value to work around jmol *feature*
+
gOK = true # global return value to work around jmol *feature*
gOk2 = TRUE # "    "
+
gOk2 = true # "    "
 
gScheme = "Jmol"
 
gScheme = "Jmol"
 
gAltScheme = "Rasmol"
 
gAltScheme = "Rasmol"
gBusy = FALSE
+
gBusy = false
  
 
# Return L tetrahedron point if i1<i2<i3, else R point
 
# Return L tetrahedron point if i1<i2<i3, else R point
Line 123: Line 123:
 
     if ({(atomno=firstno) and (chain=gChain)
 
     if ({(atomno=firstno) and (chain=gChain)
 
         and (file=f) and (model=m)}.atomName == "C") { # if psi
 
         and (file=f) and (model=m)}.atomName == "C") { # if psi
         add_sc_to_select(firstno-1, TRUE, TRUE, iChain)
+
         add_sc_to_select(firstno-1, true, true, iChain)
 
         var a = {(atomno=@{firstno+1}) and (chain=iChain) and (file=f) and (model=m)}
 
         var a = {(atomno=@{firstno+1}) and (chain=iChain) and (file=f) and (model=m)}
         a.selected = TRUE # add O
+
         a.selected = true # add O
 
     }
 
     }
 
     if ({(atomno=firstno) and (chain=iChain)
 
     if ({(atomno=firstno) and (chain=iChain)
 
         and (file=f) and (model=m)}.atomName == "CA") {
 
         and (file=f) and (model=m)}.atomName == "CA") {
         add_sc_to_select(firstno, TRUE, FALSE, iChain)
+
         add_sc_to_select(firstno, true, false, iChain)
 
     }
 
     }
 
     if ({(atomno=lastno) and (chain=iChain)
 
     if ({(atomno=lastno) and (chain=iChain)
 
         and (file=f) and (model=m)}.atomName == "C") { # if psi
 
         and (file=f) and (model=m)}.atomName == "C") { # if psi
         add_sc_to_select(lastno-1, FALSE, FALSE, iChain)
+
         add_sc_to_select(lastno-1, false, false, iChain)
 
     }
 
     }
 
}
 
}
Line 168: Line 168:
 
     if ({(atomno=firstno) and (chain=iChain)
 
     if ({(atomno=firstno) and (chain=iChain)
 
         and (file=f) and (model=m)}.atomName == "C") { # if psi
 
         and (file=f) and (model=m)}.atomName == "C") { # if psi
         add_sc_to_select(firstno-1, FALSE, FALSE, iChain)
+
         add_sc_to_select(firstno-1, false, false, iChain)
 
     }
 
     }
 
     if ({(atomno=lastno) and (chain=iChain)
 
     if ({(atomno=lastno) and (chain=iChain)
 
         and (file=f) and (model=m)}.atomName == "CA") {
 
         and (file=f) and (model=m)}.atomName == "CA") {
         add_sc_to_select(lastno, TRUE, FALSE, iChain)
+
         add_sc_to_select(lastno, true, false, iChain)
 
     }
 
     }
 
     if ({(atomno=lastno) and (chain=iChain)
 
     if ({(atomno=lastno) and (chain=iChain)
 
         and (file=f) and (model=m)}.atomName == "C") { # if psi
 
         and (file=f) and (model=m)}.atomName == "C") { # if psi
         add_sc_to_select(lastno-1, TRUE, TRUE, iChain)
+
         add_sc_to_select(lastno-1, true, true, iChain)
 
         var a = {(atomno=@{lastno+1}) and (chain=iChain) and (file=f) and (model=m)}
 
         var a = {(atomno=@{lastno+1}) and (chain=iChain) and (file=f) and (model=m)}
         a.selected = TRUE # add O
+
         a.selected = true # add O
 
     }
 
     }
 
}
 
}
  
 
function count_collisions(rc) {
 
function count_collisions(rc) {
     f = _frameID/1000000
+
     var f = (_frameID/1000000)
     m = _frameID%1000000
+
     var m = (_frameID%1000000)
 
     var cAtoms = ({})
 
     var cAtoms = ({})
 
     for (var idx = {(file=f) and (model=m)}.min.atomIndex;
 
     for (var idx = {(file=f) and (model=m)}.min.atomIndex;
 
         idx <= {(file=f) and (model=m)}.max.atomIndex; idx++) {
 
         idx <= {(file=f) and (model=m)}.max.atomIndex; idx++) {
 
         if ({atomIndex=idx}.size > 0) {
 
         if ({atomIndex=idx}.size > 0) {
             var lcAtoms = (within(kCtolerance, FALSE, {atomIndex=idx})
+
             var lcAtoms = (within(kCtolerance, false, {atomIndex=idx})
 
                 and {atomIndex > idx}
 
                 and {atomIndex > idx}
 
                 and not {rc}
 
                 and not {rc}
Line 195: Line 195:
 
             if (lcAtoms.size > 0) {
 
             if (lcAtoms.size > 0) {
 
                 cAtoms = cAtoms or lcAtoms or {atomIndex=idx}
 
                 cAtoms = cAtoms or lcAtoms or {atomIndex=idx}
                 if (rc == TRUE) {
+
                 if (rc == true) {
 
                     print format("Collision of idx=%d with %s", idx, lcAtoms)
 
                     print format("Collision of idx=%d with %s", idx, lcAtoms)
 
                 }
 
                 }
Line 207: Line 207:
 
     var f = (_frameID/1000000)
 
     var f = (_frameID/1000000)
 
     var m = (_frameID%1000000)
 
     var m = (_frameID%1000000)
     is = FALSE
+
     is = false
 
     var cAtoms = ({})
 
     var cAtoms = ({})
 
     var iChain = {selected}.chain
 
     var iChain = {selected}.chain
Line 213: Line 213:
 
         a = {(atomno=no) and (chain=iChain) and (file=f) and (model=m)}
 
         a = {(atomno=no) and (chain=iChain) and (file=f) and (model=m)}
 
         if (a.size > 0) {
 
         if (a.size > 0) {
             is = TRUE
+
             is = true
 
             break
 
             break
 
         }
 
         }
Line 222: Line 222:
 
# A handy debug routine
 
# A handy debug routine
 
function cc {
 
function cc {
     print count_collisions(TRUE)
+
     print count_collisions(true)
 
}
 
}
  
 
# Utility
 
# Utility
 
function atom_rcn( iResno, iChain, iName) {
 
function atom_rcn( iResno, iChain, iName) {
     f = (_frameID/1000000)
+
     var f = (_frameID/1000000)
     m = (_frameID%1000000)
+
     var m = (_frameID%1000000)
 
     return {(resno=iResno) and (chain=iChain) and (atomName=iName)
 
     return {(resno=iResno) and (chain=iChain) and (atomName=iName)
 
         and (file=f) and (model=m)}
 
         and (file=f) and (model=m)}
Line 235: Line 235:
 
# Utility
 
# Utility
 
function atom_noc( iNo, iChain) {
 
function atom_noc( iNo, iChain) {
     f = (_frameID/1000000)
+
     var f = (_frameID/1000000)
     m = (_frameID%1000000)
+
     var m = (_frameID%1000000)
 
     return {(atomno=iNo) and (chain=iChain) and (atomName=iName)
 
     return {(atomno=iNo) and (chain=iChain) and (atomName=iName)
 
         and (file=f) and (model=m)}
 
         and (file=f) and (model=m)}
Line 243: Line 243:
 
# A handy debug routine
 
# A handy debug routine
 
function hi {
 
function hi {
 +
    print "hoverLabel=%D %U (atomIndex...)"
 
     var selsave = {selected}
 
     var selsave = {selected}
    var f = (_frameID/1000000)
+
     select all
    var m = (_frameID%1000000)
 
     select ((file=f) and (model=m))
 
 
     set hoverlabel "%D %U"
 
     set hoverlabel "%D %U"
 
     select {selsave}
 
     select {selsave}
Line 264: Line 263:
 
         if (cc > 0) {
 
         if (cc > 0) {
 
             var p = prompt(format("%d collision%s present!\nProceed anyway?",
 
             var p = prompt(format("%d collision%s present!\nProceed anyway?",
                 cc, ((cc > 1) ? "s" : "")), "OK|Cancel|Repair", TRUE)
+
                 cc, ((cc > 1) ? "s" : "")), "OK|Cancel|Repair", true)
 
             if (p == "Cancel") {
 
             if (p == "Cancel") {
 
                 quit
 
                 quit
Line 316: Line 315:
 
function plico_exit() {
 
function plico_exit() {
 
     var p = ""
 
     var p = ""
     var done = FALSE
+
     var done = false
 
     if (gPlico.size > 0) {
 
     if (gPlico.size > 0) {
         p = prompt(format("Exit %s?", gPlico), "Yes|No|Undo", TRUE)
+
         p = prompt(format("Exit %s?", gPlico), "Yes|No|Undo", true)
 
         if (p == "Undo") {
 
         if (p == "Undo") {
 
             load tugsave.pdb
 
             load tugsave.pdb
Line 340: Line 339:
 
         color {selected} @gScheme
 
         color {selected} @gScheme
 
         draw gSCcircle DELETE
 
         draw gSCcircle DELETE
         gBusy = FALSE
+
         gBusy = false
 
         background ECHO yellow
 
         background ECHO yellow
         done = TRUE
+
         done = true
  
 
         # Pop selected
 
         # Pop selected
Line 349: Line 348:
 
     return done
 
     return done
 
}
 
}
 
 
# end of plicocommon.spt</pre>
 
# end of plicocommon.spt</pre>

Revision as of 17:44, 18 August 2014

This script contains routines used by other scripts of the Plico suite. It must be located in the same directory as any script that uses these routines.

Copy and paste the following into a text editor and save in your scripts folder as plicoCommon.spt.

#   plicoCommon - Jmol script by Ron Mignery
#   v1.6 beta    7/16/2014 -handle multiple frames+
#
#   Routines and values common to all Plico suite scripts
#   Must be present in the same directory as other Plico scripts that use it
kCommon = 3
kDtolerance = 0.1
kAtolerance = 5.0
kCtolerance = 1.85
kMtolerance = 0.8
gMouseX = 0
gMouseY = 0
gMinNo = 1
gMaxNo = 9999
gOK = true # global return value to work around jmol *feature*
gOk2 = true # "    "
gScheme = "Jmol"
gAltScheme = "Rasmol"
gBusy = false

# Return L tetrahedron point if i1<i2<i3, else R point
function get_tet_idx(i1, i2, i3, dist) {
    var v1 = {atomIndex=i3}.xyz - {atomIndex=i2}.xyz
    var v2 = {atomIndex=i1}.xyz - {atomIndex=i2}.xyz
    var axis = cross(v1, v2)
    var pma = ({atomIndex=i1}.xyz + {atomIndex=i3}.xyz)/2
    var pmo = {atomIndex=i2}.xyz + {atomIndex=i2}.xyz - pma
    var pt = pmo + (axis/axis)

    var v = pt - {atomIndex=i2}.xyz
    var cdist = distance(pt, {atomIndex=i2})
    var factor = (dist/cdist)
    var lpt = v * factor

    return lpt + {atomIndex=i2}.xyz
}

# Work around - Functions returning values must be in lower case for 14.0.13
function get_trigonal_idx(i1, i2, i3, dist) {
    var v1 = {atomIndex=i1}.xyz - {atomIndex=i2}.xyz
    var v2 = {atomIndex=i3}.xyz - {atomIndex=i2}.xyz
    var pt = {atomIndex=i2}.xyz - (v1 + v2)

    var v = pt - {atomIndex=i2}.xyz
    var cdist = distance(pt, {atomIndex=i2})
    var factor = (dist/cdist)
    var lpt = (v * factor)

    return lpt + {atomIndex=i2}.xyz
}

function abs( x) {
    if (x < 0) {
        x = -x
    }
    return x
}
# Selected must include second parameter but not the first
function set_distance_atoms( static, mobile, desired) {
    try {
        var v = mobile.xyz - static.xyz
        var dist = distance(static, mobile)
        translateSelected @{((v * (desired/dist)) - v)}
    }
    catch {
    }
}
function set_distance_idx( staticIdx, mobileIdx, desired) {
    set_distance_atoms({atomIndex=staticIdx}, {atomIndex=mobileIdx}, desired)
}

# Selected must include third parameter but not the first
function set_angle_atoms( stator, pivot, rotor, toangle) {
    try {
        var v1=stator.xyz - pivot.xyz
        var v2=rotor.xyz - pivot.xyz
        var axis = cross(v1, v2) + pivot.xyz
        var curangle =  angle(stator, pivot, rotor)
        rotateselected @axis @pivot @{curangle-toangle}
    }
    catch {
    }
}
function set_angle_idx( statorIdx, pivotIdx, rotorIdx, toangle) {
    set_angle_atoms({atomIndex=statorIdx}, {atomIndex=pivotIdx},
        {atomIndex=rotorIdx}, toangle)
}

# Selected must include fourth parameter but not the first
function set_dihedral_atoms( stator, pivot1, pivot2, rotor, toangle) {
    try {
        var curangle =  angle(stator, pivot1, pivot2, rotor)
        rotateselected {pivot2} {pivot1} @{curangle-toangle}
    }
    catch {
    }
}
function set_dihedral_idx( statorIdx, pivot1idx, pivot2idx, rotorIdx, toangle) {
    set_dihedral_atoms({atomIndex = statorIdx}, {atomIndex = pivot1idx},
        {atomIndex = pivot2idx}, {atomIndex = rotorIdx}, toangle)
}

function angle_idx_4( a1idx, a2idx, a3idx, a4idx) {
    return angle({atomIndex=a1idx}, {atomIndex=a2idx},
        {atomIndex=a3idx}, {atomIndex=a4idx})
}

# First and last are BB atoms
# Any side atoms in the range are also selected
function select_nward_idx (firstIdx, lastIdx) {
    var f = (_frameID/1000000)
    var m = (_frameID%1000000)
    var firstno = ((firstIdx < 0) ? {atomIndex=lastIdx}.atomno : {atomIndex=firstIdx}.atomno)
    var lastno = ((lastIdx < 0) ? firstno : {atomIndex=lastIdx}.atomno)
    var iChain = ((firstIdx < 0)
        ? {atomIndex=lastIdx}.chain : {atomIndex=firstIdx}.chain)

    select ((atomno <= firstno) and (atomno >= lastno) and (chain = iChain)
        and (file=f) and (model=m))
    if ({(atomno=firstno) and (chain=gChain)
        and (file=f) and (model=m)}.atomName == "C") { # if psi
        add_sc_to_select(firstno-1, true, true, iChain)
        var a = {(atomno=@{firstno+1}) and (chain=iChain) and (file=f) and (model=m)}
        a.selected = true # add O
    }
    if ({(atomno=firstno) and (chain=iChain)
        and (file=f) and (model=m)}.atomName == "CA") {
        add_sc_to_select(firstno, true, false, iChain)
    }
    if ({(atomno=lastno) and (chain=iChain)
        and (file=f) and (model=m)}.atomName == "C") { # if psi
        add_sc_to_select(lastno-1, false, false, iChain)
    }
}

# First and last are BB atoms
# Any side atoms in the range are also selected
function select_cward_idx (firstIdx, lastIdx) {
    var f = (_frameID/1000000)
    var m = (_frameID%1000000)
    var firstno = ((firstIdx < 0) ? gMaxNo : {atomIndex=firstIdx}.atomno)
    var lastno = ((lastIdx < 0) ? 1 : {atomIndex=lastIdx}.atomno)
    var iChain = ((firstIdx < 0)
        ? {atomIndex=lastIdx}.chain : {atomIndex=firstIdx}.chain)

    # If nWard anchor in range, begin selection with it
    if ((gNanchorIdx >= 0) and ({atomIndex=gNanchorIdx}.chain == iChain))  {
        var aNo = {atomIndex=gNanchorIdx}.atomno
        if (aNo > firstNo) {
            firstno = aNo
        }
    }

    # If cWard anchor in range, end selection with it
    if ((gCanchorIdx >= 0) and ({atomIndex=gCanchorIdx}.chain == iChain))  {
        var aNo = {atomIndex=gCanchorIdx}.atomno
        if (aNo < lastNo) {
            lastno = aNo
        }
    }

    select ((atomno >= firstno) and (atomno <= lastno) and (chain = iChain)
        and (file=f) and (model=m))

    if ({(atomno=firstno) and (chain=iChain)
        and (file=f) and (model=m)}.atomName == "C") { # if psi
        add_sc_to_select(firstno-1, false, false, iChain)
    }
    if ({(atomno=lastno) and (chain=iChain)
        and (file=f) and (model=m)}.atomName == "CA") {
        add_sc_to_select(lastno, true, false, iChain)
    }
    if ({(atomno=lastno) and (chain=iChain)
        and (file=f) and (model=m)}.atomName == "C") { # if psi
        add_sc_to_select(lastno-1, true, true, iChain)
        var a = {(atomno=@{lastno+1}) and (chain=iChain) and (file=f) and (model=m)}
        a.selected = true # add O
    }
}

function count_collisions(rc) {
    var f = (_frameID/1000000)
    var m = (_frameID%1000000)
    var cAtoms = ({})
    for (var idx = {(file=f) and (model=m)}.min.atomIndex;
        idx <= {(file=f) and (model=m)}.max.atomIndex; idx++) {
        if ({atomIndex=idx}.size > 0) {
            var lcAtoms = (within(kCtolerance, false, {atomIndex=idx})
                and {atomIndex > idx}
                and not {rc}
                and not connected({atomIndex=idx}))
            if (lcAtoms.size > 0) {
                cAtoms = cAtoms or lcAtoms or {atomIndex=idx}
                if (rc == true) {
                    print format("Collision of idx=%d with %s", idx, lcAtoms)
                }
            }
        }
    }
    return cAtoms
}

function is_collision_in_select() {
    var f = (_frameID/1000000)
    var m = (_frameID%1000000)
    is = false
    var cAtoms = ({})
    var iChain = {selected}.chain
    for (var no = {selected}.min.atomno; no <= {selected}.max.atomno; no++) {
        a = {(atomno=no) and (chain=iChain) and (file=f) and (model=m)}
        if (a.size > 0) {
            is = true
            break
        }
    }
    return is
}

# A handy debug routine
function cc {
    print count_collisions(true)
}

# Utility
function atom_rcn( iResno, iChain, iName) {
    var f = (_frameID/1000000)
    var m = (_frameID%1000000)
    return {(resno=iResno) and (chain=iChain) and (atomName=iName)
        and (file=f) and (model=m)}
}

# Utility
function atom_noc( iNo, iChain) {
    var f = (_frameID/1000000)
    var m = (_frameID%1000000)
    return {(atomno=iNo) and (chain=iChain) and (atomName=iName)
        and (file=f) and (model=m)}
}

# A handy debug routine
function hi {
    print "hoverLabel=%D %U (atomIndex...)"
    var selsave = {selected}
    select all
    set hoverlabel "%D %U"
    select {selsave}
}

function plico_prelim(repair) {

    # Push selected
    gSelSaves = {selected}
    var f = (_frameID/1000000)
    var m = (_frameID%1000000)
    select ((file=f) and (model=m))

    # Bad idea to proceed when collisions present
    while (repair) {
        var cc = count_collisions(({})).size
        if (cc > 0) {
            var p = prompt(format("%d collision%s present!\nProceed anyway?",
                cc, ((cc > 1) ? "s" : "")), "OK|Cancel|Repair", true)
            if (p == "Cancel") {
                quit
            }
            else if (p == "Repair") {
                select ((file=f) and (model=m))
                allSet = {selected}
                gChain = "XX"
                for (var idx = {allSet}.atomIndex.min;
                    idx <= {allSet}.atomIndex.max; idx++) {
                    if ({atomIndex=idx}.chain != gChain) {
                        gChain = {atomIndex=idx}.chain
                        select {(chain=gChain) and (file=f) and (model=m)}
                        handle_collisions( idx)
                    }
                }
            }
            else {
                break
            }
        }
        else {
            break
        }
    } # endwhile

    gZoom = script("show zoom")
    gRotate = script("show rotation")
    write tugsave.pdb
    select none

    gScheme = defaultColorScheme
    gAltScheme = ((gScheme == "jmol") ? "rasmol" : "jmol")
    set echo TOP LEFT
    background ECHO yellow
    gChain = ""
    unbind
}

# gPlicoRecord is maintained by the macro pilcoRecord
function plico_record(s) {
    var g = format("show file \"%s\"", gPlicoRecord)
    var ls = script(g)
    if (ls.find("FileNotFoundException")) {
        ls = ""
    }
    ls += s
    write var ls @gPlicoRecord
}

function plico_exit() {
    var p = ""
    var done = false
    if (gPlico.size > 0) {
        p = prompt(format("Exit %s?", gPlico), "Yes|No|Undo", true)
        if (p == "Undo") {
            load tugsave.pdb
            script inline gZoom
            rotate @gRotate
            echo Tug session undone
            if (gPlicoRecord != "") {
                plico_record("load tugsave.pdb;")
            }
        }
    }
    if (p != "No") {
        unbind
        halo off
        echo
        var f = (_frameID/1000000)
        var m = (_frameID%1000000)
        select ((file=f) and (model=m))
        halo off
        star off
        color {selected} @gScheme
        draw gSCcircle DELETE
        gBusy = false
        background ECHO yellow
        done = true

        # Pop selected
        select gSelSaves
    }
    return done
}
# end of plicocommon.spt

Contributors

Remig