User:Remig/plico/plicoCommon

From Jmol
< User:Remig‎ | plico
Revision as of 19:06, 28 March 2014 by Remig (talk | contribs) (Common routines for the Plico suite of Jmol scripts)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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.

#   plicoCommon - Jmol script by Ron Mignery
#   v1.0 beta    3/26/2014 for Jmol 14
#
#   Routines and values common to all Plico suite scripts
#   Must be present in the same directory as other Plico scripts
var kCommon = 1
var kDtolerance = 0.1
var kAtolerance = 5.0
var kCtolerance = 1.85
var kMtolerance = 0.8

# Return L tetrahedron point if i1<i2<i3, else R point
function getTetIdx(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
}

function getTrigonalIdx(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 setDistanceAtoms( static, mobile, desired) {
    try {
        var v = mobile.xyz - static.xyz
        var dist = distance(static, mobile)
        translateSelected @{((v * (desired/dist)) - v)}
    }
    catch {
    }
}
function setDistanceIdx( staticIdx, mobileIdx, desired) {
    setDistanceAtoms({atomIndex=staticIdx}, {atomIndex=mobileIdx}, desired)
}


# Selected must include third parameter but not the first
function setAngleAtoms( 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 setAngleIdx( statorIdx, pivotIdx, rotorIdx, toangle) {
    setAngleAtoms({atomIndex=statorIdx}, {atomIndex=pivotIdx},
        {atomIndex=rotorIdx}, toangle)
}

# Selected must include fourth parameter but not the first
function setDihedralAtoms( stator1, stator2, rotor1, rotor2, toangle) {
    try {
        var curangle =  angle(stator1, stator2, rotor1, rotor2)
        rotateselected {rotor1} {stator2} @{curangle-toangle}
    }
    catch {
    }
}
function setDihedralIdx( stator1idx, stator2idx, rotor1idx, rotor2idx, toangle) {
    setDihedralAtoms({atomIndex = stator1idx}, {atomIndex = stator2idx},
        {atomIndex = rotor1idx}, {atomIndex = rotor2idx}, toangle)
}

function countCollisions(rc) {
    var cAtoms = ({})
    for (var idx = {*}.min.atomIndex; idx <= {*}.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
                if (rc == TRUE) {
                    print format("Collision of idx=%d with %s", idx, lcAtoms)
                }
            }
        }
    }
    return cAtoms.size
}

# A handy debug routine
function cc {
    print countcollisions(TRUE)
}

# A handy debug routine
function hi {
    var selsave = {selected}
    select all
    set hoverlabel "%D %U"
    select {selsave}
}

function plicoPrelim() {

    # Push selected
    gSelSaves = {selected}
    select all
    
    # Bad idea to proceed when collisions present
    while (TRUE) {
        var cc = countCollisions(({}))
        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 all
                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}
                        handleCollisions2( 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
}

function plicoExit() {
    var p = ""
    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 != "") {
                plicoRecord("load tugsave.pdb;")
            }
        }
    }
    if (p != "No") {
        unbind
        halo off
        echo
        select all
        halo off
        star off
        color {selected} @gScheme
        draw gSCcircle DELETE
        gBusy = FALSE
        background ECHO yellow

        # Pop selected
        select gSelSaves
    }
}

Contributors

Remig