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

From Jmol
Jump to navigation Jump to search
(Add freeze functionality)
(Avoid "axis," a newly reserved word)
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
−
'''Tug''' allows the user to pull or push by mouse actions to move or rotate one part of a polypeptide against the rest by rotation on its psi and phi bonds with collision detection and restriction.  It also allows the user to move an entire chain to nest against another chain.
+
'''Tug''' allows the user to pull or push by mouse actions to move or rotate one part of a polypeptide against the rest by rotation on its psi and phi bonds with collision detection and restriction.  It also allows the user to move an entire chain to nest against another chain. A full description of its capabilities can be found [[User:Remig/plico/tugManual|here]].
  
 
'''Tug''' is a member of the Plico suite of protein folding tools described in [[User:Remig/plico]] . It may be installed and accessed as a macro with the file:
 
'''Tug''' is a member of the Plico suite of protein folding tools described in [[User:Remig/plico]] . It may be installed and accessed as a macro with the file:
 
<pre>Title=PLICO Tug
 
<pre>Title=PLICO Tug
−
Script=script <path to your scripts folder>/tug.spt;plicotug</pre>
+
Script=script <path to your scripts directory>/tug.spt;plico_tug</pre>
−
saved as plicotug.macro in your .jmol/macros folder as described in [[Macro]].
+
saved as plicotug.macro in your .jmol/macros directory as described in [[Macro]].
  
−
Copy and paste the following into a text editor and save in your scripts folder as tug.spt.  
+
Copy and paste the following into a text editor and save in your scripts directory as tug.spt.  
 
<pre>#  tug - Jmol script by Ron Mignery
 
<pre>#  tug - Jmol script by Ron Mignery
−
#  v1.6 beta    2/27/2014 for Jmol 14 - add freeze
+
#  v1.20 beta    4/12/2016 -axis is now a reserved word
 
#
 
#
 
#  Translate or rotate a stretch of a polypeptide against itself
 
#  Translate or rotate a stretch of a polypeptide against itself
 
#    or against other chains by mouse actions
 
#    or against other chains by mouse actions
 
#
 
#
−
var kTug = 3
+
kTug = 3
−
var kDtolerance = 0.2
+
gCanchorIdx = -1
−
var kAtolerance = 5.0
+
gCanchorNo = -1
−
var kCtolerance = 1.85
+
gPlico = "TUG"
−
var kMtolerance = 0.8
+
gNanchorIdx = -1
−
var gCanchorIdx = -1
+
gNanchorNo = -1
−
var gCanchorNo = -1
+
gCcargoIdx = -1
−
var gPlico = "TUG"
+
gNcargoIdx = -1
−
var gNanchorIdx = -1
+
gCcargoNo = -1
−
var gNanchorNo = -1
+
gNcargoNo = -1
−
var gCcargoIdx = -1
+
gDestAtomIdx = -1
−
var gNcargoIdx = -1
+
g1pivotIdx = -1
−
var gCcargoNo = -1
+
g2pivotIdx = -1
−
var gNcargoNo = -1
+
gSelSaves = ({})
−
var gDestAtomIdx = -1
+
gCrotors = array()
−
var g1pivotIdx = -1
+
gNrotors = array()
−
var g2pivotIdx = -1
+
gOkCollide = ({})
−
var gSelSaves = ({})
+
gChain = ""
−
var gCrotors = array()
+
gMinNo = 1
−
var gNrotors = array()
+
gMaxNo = 9999
−
var gMouseX = 0
+
gCargoSet = ({})
−
var gMouseY = 0
+
gMovingSet = ({})
−
var gOkCollide = ({})
+
gBusy = false
−
var gChain = ""
+
gSCidx = -1
−
var gMinNo = 1
+
gSCcircle = -1
−
var gMaxNo = 9999
+
gSCpt = {0 0 0}
−
var gScheme = "Jmol"
+
gTargetPt = {0 0 0}
−
var gAltScheme = "Rasmol"
+
gNewDrag = false
−
var gCargoSet = ({})
+
gTow = false
−
var gMovingSet = ({})
+
g1dynamicIdx = -1
−
var gBusy = FALSE
+
g2dynamicIdx = -1
−
var gSCidx = -1
+
gSCcheck = true
−
var gSCcircle = -1
+
gBondPicking = false
−
var gSCpt = {0 0 0}
+
gFreeze = array()
−
var gOK = TRUE # global return value to work around jmol *feature*
+
gToab = false
−
var gOk2 = TRUE # "    "
 
−
var gTargetPt = {0 0 0}
 
−
var gNewDrag = FALSE
 
−
var gEcho = ""
 
−
var gZoom = ""
 
−
var gRotate = ""
 
−
var gTow = FALSE
 
−
var g1dynamicIdx = -1
 
−
var g2dynamicIdx = -1
 
−
var gSCcheck = TRUE
 
−
var gBondPicking = FALSE
 
  
  
−
# Return L tetrahedron point if i1<i2<i3, else R point
+
function get_cp_idx (idx) {
−
function getTet(i1, i2, i3, dist) {
+
     var no = {atomIndex=idx}.atomno
−
     var v1 = {atomIndex=i3}.xyz - {atomIndex=i2}.xyz
+
     while ((no < gMaxNo) and ({(atomno=no) and (chain=gChain)
−
     var v2 = {atomIndex=i1}.xyz - {atomIndex=i2}.xyz
+
        and thisModel}.atomName != "C")) {
−
    var axis = cross(v1, v2)
+
         no++
−
    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 getTrigonal(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
+
     return ({(atomno=no) and (chain=gChain) and thisModel}.atomIndex)
 
}
 
}
  
−
function getCAmNo (iNo) {
+
function get_cm_no (iNo) {
−
     while ((iNo > 0) and ({(atomno=iNo) and (chain=gChain)}.atomName != "CA")) {
+
     while ((iNo > 0) and ({(atomno=iNo) and (chain=gChain)
 +
        and thisModel}.atomName != "C")) {
 
         iNo--
 
         iNo--
 
     }
 
     }
Line 105: Line 67:
 
}
 
}
  
−
function getCAmIdx (idx) {
+
function get_nm_idx (idx) {
−
     var no = {atomIndex=idx and (chain=gChain)}.atomno
+
     var no = {atomIndex=idx}.atomno
−
     no = getCAmNo( no)
+
    while ((no > 0) and ({(atomno=no) and (chain=gChain)
−
     return ({(atomno=no) and (chain=gChain)}.atomIndex)
+
        and thisModel}.atomName != "N")) {
 +
        no--
 +
     }
 +
     return ({(atomno=no) and (chain=gChain) and thisModel}.atomIndex)
 
}
 
}
  
−
function getCApNo (iNo) {
+
function get_np_no (iNo) {
−
     while ((iNo < gMaxNo) and ({(atomno=iNo) and (chain=gChain)}.atomName != "CA")) {
+
     while ((iNo < gMaxNo) and ({(atomno=iNo) and (chain=gChain)
 +
        and thisModel}.atomName != "N")) {
 
         iNo++
 
         iNo++
 
     }
 
     }
Line 118: Line 84:
 
}
 
}
  
−
function getCpIdx (idx) {
+
function get_cb_idx (BBidx) {
−
     var no = {atomIndex=idx and (chain=gChain)}.atomno
+
     var no = {atomIndex=BBidx}.atomno
−
     no = getCpNo( no)
+
    var i = 1
−
     return ({(atomno=no) and (chain=gChain)}.atomIndex)
+
    for (; i < 5; i++) {
 +
        if ({(atomno=@{no+i}) and (chain=gChain)
 +
            and thisModel}.atomName == "CB") {
 +
            break
 +
        }
 +
     }
 +
     return {(atomno=@{no+i}) and (chain=gChain) and thisModel}.atomIndex
 
}
 
}
  
−
function getCpNo (iNo) {
+
function get_o_idx (BBidx) {
−
     while ((iNo < gMaxNo) and ({(atomno=iNo) and (chain=gChain)}.atomName != "C")) {
+
     var no = {atomIndex=BBidx}.atomno
−
         iNo++
+
    var i = 1
 +
    for (; i < 4; i++) {
 +
        if ({(atomno=@{no+i}) and (chain=gChain)
 +
            and thisModel}.atomName == "O") {
 +
            break
 +
         }
 
     }
 
     }
−
     return iNo
+
     return {(atomno=@{no+i}) and (chain=gChain) and thisModel}.atomIndex
 
}
 
}
  
−
function getCApIdx (idx) {
+
function get_nward_bb_idx (idx, iChain) {
−
     var no = {atomIndex=idx and (chain=gChain)}.atomno
+
     var no = {atomIndex=idx}.atomno - 1
−
     no = getCApNo( no)
+
     for (; no >= 0; no--) {
−
    return ({(atomno=no) and (chain=gChain)}.atomIndex)
+
        var atomName = {(atomno=no) and (chain=iChain)
−
}
+
            and thisModel}.atomName
−
 
+
        if ((atomName = "N") or (atomName = "C") or (atomName = "CA")) {
−
function getCmNo (iNo) {
+
            break
−
    while ((iNo > 0) and ({(atomno=iNo) and (chain=gChain)}.atomName != "C")) {
+
         }
−
         iNo--
 
 
     }
 
     }
−
     return iNo
+
     return ((no >= 0) ? ({(atomno=no) and (chain=iChain)
 +
        and thisModel}.atomIndex) : -1)
 
}
 
}
  
−
function getCmIdx (idx) {
+
function get_cward_bb_idx (idx, iChain) {
−
     var no = {atomIndex=idx and (chain=gChain)}.atomno
+
     var no = {atomIndex=idx}.atomno + 1
−
     no = getCmNo( no)
+
     for (; no < gMaxNo; no++) {
−
    return ({(atomno=no) and (chain=gChain)}.atomIndex)
+
        var atomName = {(atomno=no) and (chain=iChain)
−
}
+
            and thisModel}.atomName
−
 
+
        if ((atomName = "N") or (atomName = "C") or (atomName = "CA")) {
−
function getNmNo (iNo) {
+
            break
−
    while ((iNo > 0) and ({(atomno=iNo) and (chain=gChain)}.atomName != "N")) {
+
         }
−
         iNo--
 
 
     }
 
     }
−
     return iNo
+
     return ((no >= 0) ? ({(atomno=no) and (chain=iChain)
 +
        and thisModel}.atomIndex) : -1)
 
}
 
}
  
−
function getNmIdx (idx) {
+
function get_sc_set (scIdx, iChain) {
−
     var no = {atomIndex=idx and (chain=gChain)}.atomno
+
     var scSet = ({})
−
     no = getNmNo( no)
+
     var idx = get_sc_bb_idx(scIdx, iChain)
−
     return ({(atomno=no) and (chain=gChain)}.atomIndex)
+
     var iNo = {atomIndex=idx}.atomno + 3
−
}
 
  
−
function getNpNo (iNo) {
+
    for (var i = 1; i < 20; i++) {
−
    while ((iNo < gMaxNo) and ({(atomno=iNo) and (chain=gChain)}.atomName != "N")) {
+
        idx = {(atomno=@{iNo+i}) and (chain=iChain)
−
         iNo++
+
            and thisModel}.atomIndex
 +
        if (is_bb_idx(idx)) {
 +
            break
 +
        }
 +
         scSet = scSet or {atomIndex=idx}
 
     }
 
     }
−
     return iNo
+
     return scSet
 
}
 
}
  
−
function getNpIdx (idx) {
+
function get_sc_bb_idx (idx, iChain) {
 
     var no = {atomIndex=idx}.atomno
 
     var no = {atomIndex=idx}.atomno
−
     no = getNpNo( no)
+
     for (; no > 0; no--) {
−
    return ({(atomno=no) and (chain=gChain)}.atomIndex)
+
        if ({(atomno=no) and (chain=iChain)
−
}
+
            and thisModel}.atomName == "CA") {
−
 
+
            break
−
function getCBidx (BBidx) {
+
        }
−
    var no = {atomIndex=BBidx}.atomno
+
        else if ({(atomno=no) and (chain=iChain)
−
    var i = 1
+
            and thisModel}.atomName == "C") {
−
    for (; i < 5; i++) {
+
            break
−
         if ({(atomno=@{no+i}) and (chain=gChain)}.atomName == "CB") {
+
         }
 +
        else if ({(atomno=no) and (chain=iChain)
 +
            and thisModel}.atomName == "N") {
 
             break
 
             break
 
         }
 
         }
−
    }
+
         else if ({(atomno=no) and (chain=iChain)
−
    return {(atomno=@{no+i}) and (chain=gChain)}.atomIndex
+
            and thisModel}.atomName == "CB") {
−
}
+
            no -= 3
−
 
 
−
function getOidx (BBidx) {
 
−
    var no = {atomIndex=BBidx}.atomno
 
−
    var i = 1
 
−
    for (; i < 4; i++) {
 
−
         if ({(atomno=@{no+i}) and (chain=gChain)}.atomName == "O") {
 
 
             break
 
             break
 
         }
 
         }
 
     }
 
     }
−
     return {(atomno=@{no+i}) and (chain=gChain)}.atomIndex
+
     return {(atomno=no) and (chain=iChain) and thisModel}.atomIndex
 
}
 
}
  
−
function getNwardBBno (iNo, iChain) {
+
function is_bb_idx(aIdx) {
−
     while ((iNo >= 0) and (
+
     var ret = false
−
        ({(atomno=iNo) and (chain=iChain)}.atomName != "N")
+
    switch({atomIndex=aIdx}.atomName) {
−
        and ({(atomno=iNo) and (chain=iChain)}.atomName != "C")
+
    case "N":
−
         and ({(atomno=iNo) and (chain=iChain)}.atomName != "CA"))) {
+
    case "CA":
−
         iNo--
+
    case "C":
 +
         ret = true
 +
         break
 
     }
 
     }
−
     return iNo
+
     return ret
 
}
 
}
  
−
function getNwardBBidx (idx, iChain) {
+
function is_sc_idx(aIdx) {
−
     var no = {atomIndex=idx}.atomno - 1
+
 
−
     no = getNwardBBno( no, iChain)
+
     var ret = false
−
    return ((no >= 0) ? ({(atomno=no) and (chain=iChain)}.atomIndex) : -1)
+
     if (not is_bb_idx(aIDx)) {
−
}
 
  
−
function getCwardBBno (iNo, iChain) {
+
        ret = true
−
    while ((iNo < gMaxNo) and (
+
         switch({atomIndex=aIdx}.atomName) {
−
         ({(atomno=iNo) and (chain=iChain)}.atomName != "N")
+
         case "O":
−
         and ({(atomno=iNo) and (chain=iChain)}.atomName != "C")
+
         case "CB":
−
         and ({(atomno=iNo) and (chain=iChain)}.atomName != "CA"))) {
+
            ret = false
−
         iNo++
+
            break
 +
         }
 
     }
 
     }
−
     return iNo
+
     return ret
 
}
 
}
  
−
function getCwardBBidx (idx, iChain) {
+
function select_add_sc(fromIdx) {
−
     var no = {atomIndex=idx}.atomno + 1
+
     var iNo = {atomIndex=fromIdx}.atomno
−
     no = getCwardBBno( no, iChain)
+
     var iChain = {atomIndex=fromIdx}.chain
−
     return ((no >= 0) ? ({(atomno=no) and (chain=iChain)}.atomIndex) : -1)
+
    select none
 +
     while ({(atomno=iNo) and (chain=iChain)
 +
        and thisModel and sidechain}) {
 +
        var a = {(atomno=iNo) and (chain=iChain)and thisModel}
 +
        a.selected = true
 +
        iNo++
 +
    }
 
}
 
}
  
−
function getScSet (scIdx, iChain) {
+
# Resolve collisions on selection
−
    var scSet = ({})
+
function handle_collisions( targetIdx) {
−
    var idx = getScBBidx(scIdx, iChain)
 
−
    var iNo = {atomIndex=idx}.atomno + 3
 
−
   
 
−
    for (var i = 1; i < 20; i++) {
 
−
        idx = {(atomno=@{iNo+i}) and (chain=iChain)}.atomIndex
 
−
        if (isBBidx(idx)) {
 
−
            break
 
−
        }
 
−
        scSet = scSet or {atomIndex=idx}
 
−
    }
 
−
    return scSet
 
−
}
 
  
−
function getScBBidx (idx, iChain) {
+
    # For all selected atoms
−
     var no = {atomIndex=idx}.atomno
+
     for (var iNo = {selected}.min.atomno; iNo <= {selected}.max.atomno; iNo++) {
−
    for (; no > 0; no--) {
+
         var idx = {(atomno=iNo) and (chain=gchain)
−
        if ({(atomno=no) and (chain=iChain)}.atomName == "CA") {
+
             and thisModel}.atomIndex
−
            break
+
         if ({atomindex=idx}.selected) {
−
         }
 
−
        else if ({(atomno=no) and (chain=iChain)}.atomName == "C") {
 
−
             break
 
−
        }
 
−
        else if ({(atomno=no) and (chain=iChain)}.atomName == "N") {
 
−
            break
 
−
        }
 
−
         else if ({(atomno=no) and (chain=iChain)}.atomName == "CB") {
 
−
            no -= 3
 
−
            break
 
−
        }
 
−
    }
 
−
    return {(atomno=no) and (chain=iChain)}.atomIndex
 
−
}
 
  
−
function isBBidx(aIdx) {
+
            # Collect local colliders
−
    var ret = FALSE
+
            var lcAtoms = (within(kCtolerance, false, {atomIndex=idx})
−
    switch({atomIndex=aIdx}.atomName) {
+
                and not {atomIndex=idx}
−
    case "N":
+
                and not {gOkCollide}
−
    case "CA":
+
                and not connected({atomIndex=idx}))
−
    case "C":
+
            if (lcAtoms) {
−
        ret = TRUE
+
                # Ignore kinked BB
−
        break
+
                try {
−
    }
+
                    if (is_bb_idx(idx) and (angle(
−
    return ret
+
                        {atomIndex=@{get_cward_bb_idx(idx, gChain)}}, {atomIndex=idx},
−
}
+
                        {atomIndex=@{get_nward_bb_idx(idx, gChain)}}) < 100.0)) {
 +
                        continue
 +
                    }
 +
                }
 +
                catch {
 +
                }
  
−
function isSCidx(aIdx) {
+
                # For all local colliders
 +
                for (var c = 1; c <= lcAtoms.size; c++ ) {
 +
                    var cidx = lcAtoms[c].atomIndex
  
−
    var ret = FALSE
+
                    # If it is with water, delete it
−
    if (not isBBidx(aIDx)) {
+
                    if (lcAtoms[c].group = "HOH") {
 +
                        delete {atomIndex=cidx}
 +
                    }
  
−
        ret = TRUE
+
                    # else if it is with side chain not proline, fix it
−
        switch({atomIndex=aIdx}.atomName) {
+
                    else if (is_sc_idx(cidx) and ({atomIndex=cidx}.group != "PRO")) {
−
        case "O":
+
                        fix_sc_collision_2(cidx)
−
        case "CB":
+
                       
−
            ret = FALSE
+
                        # If not fixed, exit fail
−
            break
+
                        if (not gOk2) {
−
        }
+
                            return # early exit (break n jmol bug)
−
    }
+
                        }
−
    return ret
+
                    }
−
}
 
  
−
function addSideChainToSelection(CAno, isAdd, addOXT, iChain) {
+
                    # else if it is itself a side chain not proline, fix it
−
    var iNo = CAno+3
+
                    else if (is_sc_idx(idx) and ({atomIndex=idx}.group != "PRO")) {
−
    while ({(atomno=iNo) and (chain=iChain)}.resno == {(atomno=CAno) and (chain=iChain)}.resno) {
+
                        fix_sc_collision_2(idx)
−
        {(atomno=iNo) and (chain=iChain)}.selected = isAdd
 
−
        if ({(atomno=iNo) and (chain=iChain)}.atomName == "OXT") {
 
−
            {(atomno=iNo) and (chain=iChain)}.selected = addOXT
 
−
        }
 
−
        iNo++
 
−
    }
 
−
}
 
  
−
function selectAddSideChain(fromIdx) {
+
                        # If not fixed, exit fail
−
    var iNo = {atomIndex=fromIdx}.atomno
+
                        if (not gOk2) {
−
    var iChain = {atomIndex=fromIdx}.chain
+
                            return # early exit (break n jmol bug)
−
    select none
+
                        }
−
    while ({(atomno=iNo) and (chain=iChain)}.atomName != "N") {
+
                    }
−
        {(atomno=iNo) and (chain=iChain)}.selected = TRUE
 
−
        iNo++
 
−
        if (iNo > {chain=iChain}.atomno.max) {
 
−
            break
 
−
        }
 
−
    }
 
−
}
 
  
−
# First and last are BB atoms
+
                    # Else if it is with O, counter-rotate
−
# Any side atoms in the range are also selected
+
                    else if (lcAtoms[c].atomName = "O") {
−
function selectNwardIdx (firstIdx, lastIdx) {
+
                        counter_rotate_2(lcAtoms[c].atomIndex,
−
    var firstno = ((firstIdx < 0) ? {atomIndex=lastIdx}.atomno : {atomIndex=firstIdx}.atomno)
+
                            {atomIndex=idx}.xyz, targetIdx, false)
−
    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)
+
                        # If not fixed, exit fail
 +
                        if (not gOk2) {
 +
                            return # early exit (break n jmol bug)
 +
                        }
 +
                    }
  
−
    if ({(atomno=firstno) and (chain=gChain)}.atomName == "C") { # if psi
+
                    # Else if it is itself O, counter-rotate
−
        addSideChainToSelection(firstno-1, TRUE, TRUE, iChain)
+
                    else if ({atomIndex=idx}.atomName = "O") {
−
        {(atomno=@{firstno+1}) and (chain=iChain)}.selected = TRUE # add O
+
                        counter_rotate_2(idx, lcAtoms[c].xyz, targetIdx, false)
−
    }
 
−
    if ({(atomno=firstno) and (chain=iChain)}.atomName == "CA") {
 
−
        addSideChainToSelection(firstno, TRUE, FALSE, iChain)
 
−
    }
 
−
    if ({(atomno=lastno) and (chain=iChain)}.atomName == "C") { # if psi
 
−
        addSideChainToSelection(lastno-1, FALSE, FALSE, iChain)
 
−
    }
 
−
}
 
  
−
# First and last are BB atoms
+
                        # If not fixed, exit fail
−
# Any side atoms in the range are also selected
+
                        if (not gOk2) {
−
function selectCwardIdx (firstIdx, lastIdx) {
+
                            return # early exit (break n jmol bug)
−
    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
+
                    else {    # Else not fixed, exit fail
−
    if ((gNanchorIdx >= 0) and ({atomIndex=gNanchorIdx}.chain == iChain))  {
+
                        gOk2 = false
−
        var aNo = {atomIndex=gNanchorIdx}.atomno
+
                        return # early exit (break n jmol bug)
−
        if (aNo > firstNo) {
+
                    }
−
             firstno = aNo
+
                } # endfor
 +
             }
 
         }
 
         }
−
     }
+
     } # endfor iNo
 +
}
  
−
    # If cWard anchor in range, end selection with it
+
# Rotate rotor set to move target atom to its proper place
−
    if ((gCanchorIdx >= 0) and ({atomIndex=gCanchorIdx}.chain == iChain))  {
+
function tug_track_idx(targetIdx, targetPt, nWard, cDetect) {
−
        var aNo = {atomIndex=gCanchorIdx}.atomno
+
    gOK = false
−
        if (aNo < lastNo) {
+
    var pt = targetPt
−
            lastno = aNo
+
    var dist = distance(pt, {atomIndex=targetIdx}.xyz)
−
        }
 
−
    }
 
  
−
     select (atomno >= firstno) and (atomno <= lastno) and (chain = iChain)
+
     var rotors = (nWard ? gNrotors : gCrotors)
  
−
     if ({(atomno=firstno) and (chain=iChain)}.atomName == "C") { # if psi
+
     # For a number of passes
−
         addSideChainToSelection(firstno-1, FALSE, FALSE, iChain)
+
    for (var pass1 = 0; pass1 < 20; pass1++) {
−
    }
+
         var blocked = ({})
−
    if ({(atomno=lastno) and (chain=iChain)}.atomName == "CA") {
+
         for (var pass2 = 0; pass2 < (rotors.size/4); pass2++) {
−
         addSideChainToSelection(lastno, TRUE, FALSE, iChain)
 
−
    }
 
−
    if ({(atomno=lastno) and (chain=iChain)}.atomName == "C") { # if psi
 
−
        addSideChainToSelection(lastno-1, TRUE, TRUE, iChain)
 
−
        {(atomno=@{lastno+1}) and (chain=iChain)}.selected = TRUE # add O
 
−
    }
 
−
}
 
  
−
# Selected must include second parameter but not the first
+
            var v1 = {atomIndex=targetIdx}.xyz - pt
−
function setDistanceIdx (staticIdx, mobileIdx, desired) {
 
−
    try {
 
−
        var v = {atomIndex=mobileIdx}.xyz - {atomIndex=staticIdx}.xyz
 
−
        var dist = distance({atomIndex=staticIdx}, {atomIndex=mobileIdx})
 
−
        translateSelected @{((v * (desired/dist)) - v)}
 
−
    }
 
−
    catch {
 
−
    }
 
−
}
 
  
 +
            # Find the most orthgonal unused rotor
 +
            var imax = 0
 +
            var smax = 0.5
 +
            for (var i = 1; i < rotors.size; i += 4) {
 +
                var i2 = rotors[i+1]
 +
                var i3 = rotors[i+2]
 +
                var i4 = rotors[i+3]
 +
                if ((i2 != targetIdx) and (i3 != targetIdx) and (i4 != targetIdx)) {
 +
                    if ({blocked and {atomIndex=i2}}.count == 0) {
 +
                        var v2 = {atomIndex=i3}.xyz - {atomIndex=i2}.xyz
  
−
# Selected must include third parameter but not the first
+
                        var s = sin(abs(angle(v1, {0 0 0}, v2)))
−
function setAngleIdx (statorIdx, pivotIdx, rotorIdx, toangle) {
+
                        if (s > smax) {
−
    try {
+
                            smax = s
−
        var v1={atomIndex=statorIdx}.xyz - {atomIndex=pivotIdx}.xyz
+
                            imax = i
−
        var v2={atomIndex=rotorIdx}.xyz - {atomIndex=pivotIdx}.xyz
+
                        }
−
        var axis = cross(v1, v2) + {atomIndex=pivotIdx}.xyz
+
                    }
−
        var curangle =  angle({atomIndex=statorIdx},
+
                }
−
            {atomIndex=pivotIdx}, {atomIndex=rotorIdx})
+
            }
−
        rotateselected @axis {atomIndex = pivotIdx} @{curangle-toangle}
 
−
    }
 
−
    catch {
 
−
    }
 
−
}
 
−
 
 
−
# Selected must include fourth parameter but not the first
 
−
function setDihedralIdx (stator1idx, stator2idx, rotor1idx, rotor2idx, toangle) {
 
−
    try {
 
−
        var a1={atomIndex = stator1idx}.xyz
 
−
        var a2={atomIndex = stator2idx}.xyz
 
−
        var a3={atomIndex = rotor1idx}.xyz
 
−
        var a4={atomIndex = rotor2idx}.xyz
 
−
        var curangle = angle(a1, a2, a3, a4)
 
−
        rotateselected {a3} {a2} @{curangle-toangle}
 
−
    }
 
−
    catch {
 
−
    }
 
−
}
 
  
−
function countCollisions(rc) {
+
             # If no more rotors, break to next full try
−
    var cAtoms = ({})
+
             if (imax == 0) {
−
    for (var idx = {*}.min.atomIndex; idx <= {*}.max.atomIndex; idx++) {
+
              break
−
        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
 
 
             }
 
             }
−
        }
+
            var i1 = rotors[imax+0]
−
    }
+
            var i2 = rotors[imax+1]
−
    return cAtoms.size
+
            var i3 = rotors[imax+2]
−
}
+
            var i4 = rotors[imax+3]
  
−
# Resolve collisions
+
            # Get dihedral of rotor with target point
−
function handleCollisions2( targetIdx) {
+
            var dt = angle({atomIndex=targetIdx}, {atomIndex=i2}, {atomIndex=i3}, pt)
 +
            var dh = angle({atomIndex=i1}, {atomIndex=i2}, {atomIndex=i3}, {atomIndex=i4})
 +
            if (abs(dh).type == "string") {
 +
                dh = -50
 +
            }
 +
            var psi = dh + dt
 +
            var phi = dh + dt
  
−
    # For all selected atoms
+
            # Compute resultant psi and phi
−
    for (var iNo = {selected}.min.atomno; iNo <= {selected}.max.atomno; iNo++) {
+
            #  and select from target atom to first half of rotor
−
        var idx = {(atomno=iNo) and (chain=gchain)}.atomIndex
+
            var movePt = false
−
        if ({atomindex=idx}.selected) {
+
            if (nWard) {
 +
                if ({atomIndex=i2}.atomName="CA") {
 +
                    psi = angle({atomIndex=@{get_cward_bb_idx(i1, gChain)}}, {atomIndex=i1},
 +
                        {atomIndex=i2}, {atomIndex=i3}) + dt
 +
                }
 +
                else {
 +
                    phi = angle({atomIndex=i1}, {atomIndex=i2},
 +
                        {atomIndex=i3}, {atomIndex=@{get_nward_bb_idx(i3, gChain)}}) + dt
 +
                }
  
−
            # Collect local colliders
+
                if ({atomIndex=i2}.atomno > {atomIndex=targetIdx}.atomno) {
−
            var lcAtoms = (within(kCtolerance, FALSE, {atomIndex=idx})
+
                    movePt = true
−
                and not {atomIndex=idx}
+
                    select_nward_idx(i3, get_cward_bb_idx(targetIdx, gChain))
−
                 and not {gOkCollide}
+
                    {atomIndex=targetIdx}.selected = true
−
                 and not connected({atomIndex=idx}))
+
                 }
−
            if (lcAtoms.size > 0) {
+
                 else {
−
 
+
                    select_cward_idx(i2, targetIdx)
−
                # Ignore kinked BB
+
                }
−
                 if (isBBidx(idx) and angle({atomIndex=@{getCwardBBidx(idx, gChain)}},
+
            }
−
                     {atomIndex=idx} , {atomIndex=@{getNwardBBidx(idx, gChain)}}) < 100) {
+
            else {
−
                    continue
+
                 if (({atomIndex=i2}.atomName="CA")) {
 +
                    phi = angle({atomIndex=@{get_nward_bb_idx(i1, gChain)}}, {atomIndex=i1},
 +
                        {atomIndex=i2}, {atomIndex=i3}) + dt
 +
                }
 +
                else {
 +
                     psi = angle({atomIndex=i2}, {atomIndex=i3},
 +
                        {atomIndex=i4}, {atomIndex=@{get_cward_bb_idx(i4, gChain)}}) + dt
 
                 }
 
                 }
  
−
                 # For all local colliders
+
                 if ({atomIndex=i2}.atomno < {atomIndex=targetIdx}.atomno) {
−
                for (var c = 1; c <= lcAtoms.size; c++ ) {
+
                     movePt = true
−
                     var cidx = lcAtoms[c].atomIndex
+
                     select_cward_idx(i3, get_nward_bb_idx(targetIdx, gChain))
−
 
+
                    {atomIndex=targetIdx}.selected = true
−
                     # If it is with water, delete it
+
                }
−
                    if (lcAtoms[c].group = "HOH") {
+
                else {
−
                        delete {atomIndex=cidx}
+
                     select_nward_idx(i2, targetIdx)
−
                    }
+
                }
−
 
+
            }
−
                    # else if it is with side chain not proline, fix it
 
−
                     else if (isSCidx(cidx) and ({atomIndex=cidx}.group != "PRO")) {
 
−
                        fixSCcollision2(cidx)
 
−
                        recollect = TRUE
 
  
−
                        # If not fixed, exit fail
+
            # Relax rules if desperate
−
                        if (not gOk2) {
+
            if (pass1 > 10) {
−
                            return # early exit (break n jmol bug)
+
                phi = -50
−
                        }
+
            }
−
                    }
 
  
−
                    # else if it is itself a side chain not proline, fix it
+
            # If rotation within ramachandran limits
−
                    else if (isSCidx(idx) and ({atomIndex=idx}.group != "PRO")) {
+
            if ((abs(dt) >= 0.1) and
−
                        fixSCcollision2(idx)
+
                (({atomIndex=i2}.group=="GLY") or (phi < 0))) {
−
                        recollect = TRUE
 
  
−
                        # If not fixed, exit fail
+
                # If moving target point, put the target atom there
−
                        if (not gOk2) {
+
                var cp = {atomIndex=targetIdx}.xyz
−
                            return # early exit (break n jmol bug)
+
                if (movePt) {
−
                        }
+
                    dt = -dt
−
                    }
+
                    {atomIndex=targetIdx}.xyz = pt
 +
                }
  
−
                    # Else if it is with O, counter-rotate
+
                # Rotate to minimize vector ====================
−
                    else if (lcAtoms[c].atomName = "O") {
+
                rotateSelected {atomIndex=i2} {atomIndex=i3} @dt
−
                        counterRotate2(lcAtoms[c].atomIndex,
 
−
                            {atomIndex=idx}.xyz, targetIdx, FALSE)
 
  
−
                        # If not fixed, exit fail
+
                # If collision checking
−
                        if (not gOk2) {
+
                if (cDetect) {
−
                            return # early exit (break n jmol bug)
 
−
                        }
 
−
                    }
 
  
−
                     # Else if it is itself O, counter-rotate
+
                     # If collision, back off by eighths
−
                     else if ({atomIndex=idx}.atomName = "O") {
+
                    var wasCollision = false
−
                         counterRotate2(idx, lcAtoms[c].xyz, targetIdx, FALSE)
+
                     for (var ci = 0; ci < 4; ci++) {
−
 
+
                         if (ci < 3) {
−
                         # If not fixed, exit fail
+
                            dt /= 2
 +
                        }
 +
                         handle_collisions( targetIdx)
 
                         if (not gOk2) {
 
                         if (not gOk2) {
−
                             return # early exit (break n jmol bug)
+
                             wasCollision = true
 +
                            rotateSelected {atomIndex=i2} {atomIndex=i3} @{-dt}
 +
                        }
 +
                        else if (wasCollision) {
 +
                            if (ci <3) {
 +
                                rotateSelected {atomIndex=i2} {atomIndex=i3} @{dt}
 +
                            }
 +
                        }
 +
                        else {
 +
                            break
 
                         }
 
                         }
−
                    }
 
  
−
                     else {    # Else not fixed, exit fail
+
                        if (dt < 0.01) {
−
                        gOk2 = FALSE
+
                            break
−
                        return # early exit (break n jmol bug)
+
                        }
−
                     }
+
                     } # endfor
−
                 } # endfor
+
                }
 +
 
 +
                # If moving target point, put the target atom back
 +
                if (movePt) {
 +
                     pt = {atomIndex=targetIdx}.xyz
 +
                    {atomIndex=targetIdx}.xyz = cp
 +
                 }
 +
 
 
             }
 
             }
−
        }
 
−
    } # endfor iNo
 
−
}
 
  
−
# Rotate rotor set to move target atom to its proper place
+
            # If close enough, stop
−
function tugTrackIdx(targetIdx, targetPt, nWard, cDetect) {
+
            if (distance(pt, {atomIndex=targetIdx}) < kDtolerance) {
−
    gOK = FALSE
+
                gOK = true
−
    var pt = targetPt
+
                gTargetPt = pt
−
    var dist = distance(pt, {atomIndex=targetIdx}.xyz)
+
                break
 +
            }
  
−
    var rotors = (nWard ? gNrotors : gCrotors)
+
            # Block rotor
 +
            blocked |= {atomIndex=i2}
  
−
    # For a number of passes
+
         }   # endfor num rotors passes
−
    for (var pass1 = 0; pass1 < 20; pass1++) {
 
−
         var blocked = ({})
 
−
        for (var pass2 = 0; pass2 < (rotors.size/4); pass2++) {
 
  
−
             var v1 = {atomIndex=targetIdx}.xyz - pt
+
        if (gOK) {
 +
             break
 +
        }
 +
    }  # endfor 20 passes
 +
}
  
−
            # Find the most orthgonal unused rotor
+
# Counter rotate bonds on either side of a BB O
−
            var imax = 0
+
function do_counter_rotate(caPhiIdx, nIdx, cIdx, oIdx, caPsiIdx, dir, nWard) {
−
            var smax = 0.5
 
−
            for (var i = 1; i < rotors.size; i += 4) {
 
−
                var i2 = rotors[i+1]
 
−
                var i3 = rotors[i+2]
 
−
                var i4 = rotors[i+3]
 
−
                if ((i2 != targetIdx) and (i3 != targetIdx) and (i4 != targetIdx)) {
 
−
                    if ({blocked and {atomIndex=i2}}.count == 0) {
 
−
                        var v2 = {atomIndex=i3}.xyz - {atomIndex=i2}.xyz
 
  
−
                        var s = sin(abs(angle(v1, {0 0 0}, v2)))
+
    # Rotate psi
−
                        if (s > smax) {
+
    {atomIndex=nIdx}.selected = nWard
−
                            smax = s
+
    {atomIndex=cIdx}.selected = nWard
−
                            imax = i
+
    {atomIndex=oIdx}.selected = nward
−
                        }
+
    rotateSelected {atomIndex=caPsiIdx} {atomIndex=cIdx} @{dir}
−
                    }
+
 
−
                }
+
    # Counter-rotate phi
−
            }
+
    {atomIndex=nIdx}.selected = not nWard
 +
    {atomIndex=cIdx}.selected = not nWard
 +
    {atomIndex=oIdx}.selected = not nward
 +
    rotateSelected {atomIndex=nIdx} {atomIndex=caPhiIdx} @{-dir}
 +
}
  
−
            # If no more rotors, break to next full try
+
function counter_rotate(oIdx, dir, nWard) {
−
            if (imax == 0) {
 
−
              break
 
−
            }
 
−
            var i1 = rotors[imax+0]
 
−
            var i2 = rotors[imax+1]
 
−
            var i3 = rotors[imax+2]
 
−
            var i4 = rotors[imax+3]
 
  
−
            # Get dihedral of rotor with target point
+
    var iChain = {atomIndex=oIdx}.chain
−
            var dt = angle({atomIndex=targetIdx}, {atomIndex=i2}, {atomIndex=i3}, pt)
+
    var selsave = {selected}
−
            var dh = angle({atomIndex=i1}, {atomIndex=i2}, {atomIndex=i3}, {atomIndex=i4})
+
    var cIdx = get_sc_bb_idx(oIdx, iChain)
−
            if (dh == "NaN") {
+
    var nIdx = get_cward_bb_idx(cIdx, iChain)
−
                dh = -50
+
    var caPhiIdx = get_cward_bb_idx(nIdx, iChain)
−
             }
+
    var caPsiIdx = get_nward_bb_idx(cIdx, iChain)
−
            var psi = dh + dt
+
 
−
            var phi = dh + dt
+
    if (nWard) {
 +
        var nNo = {(chain=iChain) and thisModel}.atomno.min
 +
        select_nward_idx(caPsiIdx, {(atomno=nNo) and (chain=iChain)
 +
            and thisModel}.atomIndex)
 +
    }
 +
    else {
 +
        var cNo = {(chain=iChain) and thisModel}.atomno.max
 +
        select_cward_idx(caPhiIdx, {(atomno=cNo) and (chain=iChain)
 +
             and thisModel}.atomIndex)
 +
    }
  
−
            # Compute resultant psi and phi
+
    # Counter-rotate
−
            #  and select from target atom to first half of rotor
+
    do_counter_rotate(caPhiIdx, nIdx, cIdx, oIdx, caPsiIdx, dir, not nWard)
−
            var movePt = FALSE
+
    select selsave
−
            if (nWard) {
+
}
−
                if ({atomIndex=i2}.atomName="CA") {
 
−
                    psi = angle({atomIndex=@{getCwardBBidx(i1, gChain)}}, {atomIndex=i1},
 
−
                        {atomIndex=i2}, {atomIndex=i3}) + dt
 
−
                }
 
−
                else {
 
−
                    phi = angle({atomIndex=i1}, {atomIndex=i2},
 
−
                        {atomIndex=i3}, {atomIndex=@{getNwardBBidx(i3, gChain)}}) + dt
 
−
                }
 
  
−
                if ({atomIndex=i2}.atomno > {atomIndex=targetIdx}.atomno) {
+
function counter_rotate_2(oIdx, toPt, terminalIdx, oDrag) {
−
                    movePt = TRUE
 
−
                    selectNwardIdx(i3, getCwardBBidx(targetIdx, gChain))
 
−
                    {atomIndex=targetIdx}.selected = TRUE
 
−
                }
 
−
                else {
 
−
                    selectCwardIdx(i2, targetIdx)
 
−
                }
 
−
            }
 
−
            else {
 
−
                if (({atomIndex=i2}.atomName="CA")) {
 
−
                    phi = angle({atomIndex=@{getNwardBBidx(i1, gChain)}}, {atomIndex=i1},
 
−
                        {atomIndex=i2}, {atomIndex=i3}) + dt
 
−
                }
 
−
                else {
 
−
                    psi = angle({atomIndex=i2}, {atomIndex=i3},
 
−
                        {atomIndex=i4}, {atomIndex=@{getCwardBBidx(i4, gChain)}}) + dt
 
−
                }
 
  
−
                if ({atomIndex=i2}.atomno < {atomIndex=targetIdx}.atomno) {
+
    var iChain = {atomIndex=oIdx}.chain
−
                    movePt = TRUE
+
    var selsave = {selected}
−
                    selectCwardIdx(i3, getNwardBBidx(targetIdx, gChain))
+
    var gOk2 = true
−
                    {atomIndex=targetIdx}.selected = TRUE
+
    var cIdx = get_sc_bb_idx(oIdx, iChain)
−
                }
+
    var nIdx = get_cward_bb_idx(cIdx, iChain)
−
                else {
+
    var caPhiIdx = get_cward_bb_idx(nIdx, iChain)
−
                    selectNwardIdx(i2, targetIdx)
+
    var caPsiIdx = get_nward_bb_idx(cIdx, iChain)
−
                }
 
−
            }
 
  
−
            # Relax rules if desperate
+
    var nTward = ({atomIndex=oIdx}.atomno < {atomIndex=terminalIdx}.atomno)
−
            if (pass1 > 10) {
+
    if (nTward) {
−
                phi = -50
+
        select_cward_idx(cIdx, terminalIdx)
−
            }
+
    }
 +
    else {
 +
        select_nward_idx(nIdx, terminalIdx)
 +
    }
  
−
            # If rotation within ramachandran limits
+
    # Until all collisions cancelled
−
            if ((abs(dt) >= 0.1) and
+
    var dir = 5
−
                (({atomIndex=i2}.group=="GLY") or (phi < 0))) {
+
    var ang = angle(toPt, {atomIndex=oIdx}, {atomIndex=cIdx})
 +
    var tcount = 0
 +
    while (oDrag or (within(kCtolerance, false, {atomIndex=oIdx})
 +
            and not {atomIndex=oIdx} and not connected({atomIndex=oIdx})
 +
            and not {gOkCollide} > 0)) {
  
−
                # If moving target point, put the target atom there
+
        # Counter-rotate
−
                var cp = {atomIndex=targetIdx}.xyz
+
        do_counter_rotate(caPhiIdx, nIdx, cIdx, oIdx, caPsiIdx, dir, nTward)
−
                if (movePt) {
+
        var newang = angle(toPt, {atomIndex=oIdx}, {atomIndex=cIdx})
−
                    dt = -dt
 
−
                    {atomIndex=targetIdx}.xyz = pt
 
−
                }
 
  
−
                # Rotate to minimize vector ====================
+
        # If wrong direction once, undo and reverse
−
                rotateSelected {atomIndex=i2} {atomIndex=i3} @dt
+
        if (newang > ang) {
 +
            do_counter_rotate(caPhiIDx, nIdx, cIdx, oIdx, caPsiIdx, -dir, nTward)
  
−
                # If collision checking
+
            # If first time, continue in opposite direction
−
                if (cDetect) {
+
            dir *= -1
−
 
+
            if (dir < 0) {
−
                    # If collision, back off by eighths
+
                continue
−
                    var wasCollision = FALSE
+
            }
−
                    for (var ci = 0; ci < 4; ci++) {
+
        }
−
                        if (ci < 3) {
 
−
                            dt /= 2
 
−
                        }
 
−
                        handleCollisions2( targetIdx)
 
−
                        if (not gOk2) {
 
−
                            wasCollision = TRUE
 
−
                            rotateSelected {atomIndex=i2} {atomIndex=i3} @{-dt}
 
−
                        }
 
−
                        else if (wasCollision) {
 
−
                            if (ci <3) {
 
−
                                rotateSelected {atomIndex=i2} {atomIndex=i3} @{dt}
 
−
                            }
 
−
                        }
 
−
                        else {
 
−
                            break
 
−
                        }
 
  
−
                        if (dt < 0.01) {
+
        if (oDrag) {
−
                            break
+
            break
−
                        }
+
        }
−
                    } # endfor
 
−
                }
 
  
−
                # If moving target point, put the target atom back
+
        # If no go, undo and exit
−
                if (movePt) {
+
        tcount++
−
                    pt = {atomIndex=targetIdx}.xyz
+
        if (tcount > (360/abs(dir))) {
−
                    {atomIndex=targetIdx}.xyz = cp
+
            gOk2 = false
−
                }
+
            break
 +
        }
  
−
            }
+
    } # endwhile
 +
    select selsave
 +
}
  
−
            # If close enough, stop
+
# Repair proline
−
            if (distance(pt, {atomIndex=targetIdx}) < kDtolerance) {
+
function repair_proline(BBidx) {
−
                gOK = TRUE
+
    var cbidx = get_cb_idx(BBidx)
−
                gTargetPt = pt
+
    var cbno = {atomIndex=cbidx}.atomno
−
                break
+
    var cgidx = {(atomno=@{cbno+1}) and (chain=gChain)
−
            }
+
        and thisModel}.atomIndex
 +
    var cdidx = {(atomno=@{cbno+2}) and (chain=gChain)
 +
        and thisModel}.atomIndex
 +
    var caidx = {(atomno=@{cbno-3}) and (chain=gChain)
 +
        and thisModel}.atomIndex
 +
    var nidx = {(atomno=@{cbno-4}) and (chain=gChain)
 +
        and thisModel}.atomIndex
  
−
            # Block rotor
+
    select {atomIndex=cbidx}
−
            blocked |= {atomIndex=i2}
+
    set_angle_idx(nidx, caidx, cbidx, 109.5)
  
−
        }   # endfor num rotors passes
+
    select {atomIndex=cdidx}
 +
    set_distance_idx(nidx, cdidx, 1.47)
 +
    set_angle_idx(caidx, nidx, cdidx, 102.7)
 +
    set_dihedral_idx(cbidx, caidx, nidx, cdidx, 16.2)
  
−
        if (gOK) {
+
    select {atomIndex=cgidx}
−
            break
+
    set_distance_idx(cdidx, cgidx, 1.51)
−
        }
+
    set_angle_idx(nidx, cdidx, cgidx, 106.4)
−
     }  # endfor 20 passes
+
     set_dihedral_idx(caidx, nidx, cdidx, cgidx, 16.2)
 
}
 
}
  
−
# Counter rotate bonds on either side of a BB O
+
# Repair side chain
−
function docounterRotate(caPhiIdx, nIdx, cIdx, oIdx, caPsiIdx, dir, nWard) {
+
function repair_sc(targetIdx, nWard) {
  
−
     # Rotate psi
+
     var idx = (nWard ? get_cward_bb_idx(targetIdx, gChain) : get_nward_bb_idx(targetIdx, gChain))
−
    {atomIndex=nIdx}.selected = nWard
 
−
    {atomIndex=cIdx}.selected = nWard
 
−
    {atomIndex=oIdx}.selected = nward
 
−
    rotateSelected {atomIndex=caPsiIdx} {atomIndex=cIdx} @{dir}
 
  
−
     # Counter-rotate phi
+
     if (({atomIndex=targetIdx}.atomName == "CA")
−
    {atomIndex=nIdx}.selected = not nWard
+
        and ({atomIndex=targetIdx}.group != "GLY")) {
−
    {atomIndex=cIdx}.selected = not nWard
+
        var cbidx = get_cb_idx(targetIdx)
−
    {atomIndex=oIdx}.selected = not nward
+
        select none
−
    rotateSelected {atomIndex=nIdx} {atomIndex=caPhiIdx} @{-dir}
+
        select_add_sc(cbidx)
−
}
+
        set_angle_idx(idx, targetIdx, cbidx, 110.0)
−
 
+
        set_distance_idx(targetIdx, cbidx, 1.5)
−
function counterRotate(oIdx, dir, nWard) {
+
        if ({atomIndex=targetIdx}.group != "PRO") {
−
 
+
            var colliders = (within(kCtolerance, false, {selected})
−
    var iChain = {atomIndex=oIdx}.chain
+
                and not {atomIndex=targetIdx} and not {selected})
−
    var selsave = {selected}
+
            if (colliders) {
−
    var cIdx = getScBBidx(oIdx, iChain)
+
                if ({atomIndex=targetIdx}.group != "ALA") {
−
    var nIdx = getCwardBBidx(cIdx, iChain)
+
                    fix_sc_collision_2(cbidx)
−
    var caPhiIdx = getCwardBBidx(nIdx, iChain)
+
                }
−
    var caPsiIdx = getNwardBBidx(cIdx, iChain)
+
            }
−
 
+
        }
−
    if (nWard) {
+
        else {
−
        nNo = {chain=iChain}.atomno.min
+
            if (nWard) {
−
        selectNwardIdx(caPsiIdx, {(atomno=nNo) and (chain=iChain)}.atomIndex)
+
            }
−
    }
+
            else {
−
    else {
+
                set_dihedral_idx(get_nward_bb_idx(idx, gChain), idx, targetIdx, cbidx, 174.2)
−
        cNo = {chain=iChain}.atomno.max
+
            }
−
        selectCwardIdx(caPhiIdx, {(atomno=cNo) and (chain=iChain)}.atomIndex)
+
        }
 
     }
 
     }
  
−
     # Counter-rotate
+
     else if ({atomIndex=targetIdx}.atomName == "C") {
−
    docounterRotate(caPhiIdx, nIdx, cIdx, oIdx, caPsiIdx, dir, not nWard)
+
        var oidx = get_o_idx(targetIdx)
−
    select selsave
+
        select {atomIndex=oidx}
−
}
+
        set_angle_idx(idx, targetIdx, oidx, 120.0)
−
 
+
        set_distance_idx(targetIdx, oidx, 1.21)
−
function counterRotate2(oIdx, toPt, terminalIdx, oDrag) {
+
        if (nWard) {
 +
            set_dihedral_idx(get_cward_bb_idx(idx, gChain), idx, targetIdx, oidx, 0.0)
 +
        }
 +
        if ({atomIndex=idx}.group == "PRO") {
 +
            repair_proline(idx)
 +
            var dNo = {atomIndex=targetIdx}.atomno + 4
 +
            var dIdx = {(atomno=dNO) and (chain=gChain)
 +
                and thisModel}.atomIndex
 +
            var colliders = (within(kCtolerance, false, {atomIndex=dIdx})
 +
                and not connected({atomIndex=dIdx})
 +
                and not {atomIndex=dIdx})
 +
            for (var i = 1; i <= colliders.size; i++) {
 +
                if (colliders[i].atomName == "O") {
 +
                    counter_rotate_2(colliders[i].atomIndex,
 +
                        {atomIndex=dIdx}.xyz, targetIdx, false)
 +
                }
 +
            }
 +
        }
 +
    }
 +
}
  
−
    var iChain = {atomIndex=oIdx}.chain
+
# Rebuild Cward rotors set
−
    var selsave = {selected}
+
function tug_track_c() {
−
    var gOk2 = TRUE
 
−
    var cIdx = getScBBidx(oIdx, iChain)
 
−
    var nIdx = getCwardBBidx(cIdx, iChain)
 
−
    var caPhiIdx = getCwardBBidx(nIdx, iChain)
 
−
    var caPsiIdx = getNwardBBidx(cIdx, iChain)
 
  
−
     var nTward = ({atomIndex=oIdx}.atomno < {atomIndex=terminalIdx}.atomno)
+
    # For all bb atoms cWard of cargo
−
     if (nTward) {
+
     var targetIdx = gCcargoIdx
−
        selectCwardIdx(cIdx, terminalIdx)
+
     var okCount = 0
−
    }
 
−
    else {
 
−
        selectNwardIdx(nIdx, terminalIdx)
 
−
    }
 
  
−
     # Until all collisions cancelled
+
     # Allow collisions with cargo
−
     var dir = 5
+
     gOkCollide = gCargoSet
−
    var ang = angle(toPt, {atomIndex=oIdx}, {atomIndex=cIdx})
 
 
     var tcount = 0
 
     var tcount = 0
−
     while (oDrag or (within(kCtolerance, FALSE, {atomIndex=oIdx})
+
     while (targetIdx != gNanchorIdx) {
−
            and not {atomIndex=oIdx} and not connected({atomIndex=oIdx})
 
−
            and not {gOkCollide} > 0)) {
 
  
−
         # Counter-rotate
+
         # Step to next atom
−
         docounterRotate(caPhiIdx, nIdx, cIdx, oIdx, caPsiIdx, dir, nTward)
+
         targetIdx = get_cward_bb_idx(targetIdx, gChain)
−
        var newang = angle(toPt, {atomIndex=oIdx}, {atomIndex=cIdx})
+
         if (targetIdx < 0) {
−
 
+
             break
−
        # If wrong direction once, undo and reverse
 
−
         if (newang > ang) {
 
−
            docounterRotate(caPhiIDx, nIdx, cIdx, oIdx, caPsiIdx, -dir, nTward)
 
−
 
 
−
            # If first time, continue in opposite direction
 
−
            dir *= -1
 
−
            if (dir < 0) {
 
−
                continue
 
−
             }
 
 
         }
 
         }
  
−
         if (oDrag) {
+
        # No collision with cargo allowed after two atoms placed
−
            break
+
         if (tcount == 2) {
 +
          gOkCollide = ({})
 
         }
 
         }
−
 
−
        # If no go, undo and exit
 
 
         tcount++
 
         tcount++
−
        if (tcount > (360/abs(dir))) {
 
−
            gOk2 = FALSE
 
−
            break
 
−
        }
 
  
−
     } # endwhile
+
        # Compute targets desired coords
−
     select selsave
+
        try {
−
}
+
            var c1idx = get_cward_bb_idx(targetIdx, gChain)
 +
            var n1idx = get_nward_bb_idx(targetIdx, gChain )
 +
            var n2idx = get_nward_bb_idx(n1Idx, gChain)
 +
            var n3idx = get_nward_bb_idx(n2Idx, gChain)
 +
            var pt = {0 0 0}
 +
            if ({atomIndex=targetIdx}.atomName == "N") {
 +
                var oidx = get_o_idx(n1idx)
 +
                select {atomIndex=oidx}
 +
      
 +
                # Desired target location is trigonal O
 +
                set_distance_idx(n1idx, oidx, 1.5)
 +
                pt = get_trigonal_idx(n2idx, n1idx, oidx, 1.37)
 +
                set_distance_idx(n1idx, oidx, 1.21)
 +
            }
 +
            else if (({atomIndex=targetIdx}.atomName == "C")
 +
                and ({atomIndex=targetIdx}.group != "GLY")) {
 +
   
 +
                # Desired target location is tetragonal CB
 +
                var cbidx = get_cb_idx(n1idx)
 +
                pt = get_tet_idx(n2idx, n1idx, cbidx, 1.5)
 +
            }
 +
            else { # CA (or GLY C)
 +
   
 +
                # Save current target coords
 +
                var cp = {atomIndex=targetIdx}.xyz
 +
      
 +
                # Set target atom at desired distance and angle
 +
                select {atomIndex=targetIdx}
 +
                set_distance_idx(n1idx, targetIdx, 1.5)
 +
                set_angle_idx(n2idx, n1idx, targetIdx, 120.0)
 +
                if ({atomIndex=targetIdx}.atomName == "CA") {
 +
                    set_dihedral_idx(n3idx, n2idx, n1idx, targetIdx, 180)
 +
                }
 +
   
 +
                # Record and restore target
 +
                pt = {atomIndex=targetIdx}.xyz
 +
                {atomIndex=targetIdx}.xyz = cp
 +
            }
 +
   
 +
            # If target not at desired location
 +
            if (distance(pt, {atomIndex=targetIdx}) > kDtolerance) {
 +
                okCount = 0
 +
                gTargetPt = pt
 +
                var xcount = 0
 +
                gOK = false
 +
                while ((xcount < 20) and (not gOK)) {
 +
   
 +
                    # Rotate on cWard rotor set to move it there
 +
                    tug_track_idx(targetIdx, pt, false, false)
 +
                    xcount++
 +
                }
 +
            }
 +
            else {
 +
                gOK = true
 +
                okCount++
 +
            }
 +
        }
 +
        catch {
 +
        }
  
−
# Repair proline
+
        # If successful
−
function repairProline(BBidx) {
+
        if (gOK == true) {
−
    var cbidx = getCBidx(BBidx)
 
−
    var cbno = {atomIndex=cbidx}.atomno
 
−
    var cgidx = {(atomno=@{cbno+1}) and (chain=gChain)}.atomIndex
 
−
    var cdidx = {(atomno=@{cbno+2}) and (chain=gChain)}.atomIndex
 
−
    var caidx = {(atomno=@{cbno-3}) and (chain=gChain)}.atomIndex
 
−
    var nidx = {(atomno=@{cbno-4}) and (chain=gChain)}.atomIndex
 
  
−
    select {atomIndex=cbidx}
+
            # Adust any side atoms
−
    setAngleIdx(nidx, caidx, cbidx, 109.5)
+
            repair_sc(targetIdx, false)
 +
        }
  
−
    select {atomIndex=cdidx}
+
        # Else fail
−
    setDistanceIdx(nidx, cdidx, 1.47)
+
        else {
−
    setAngleIdx(caidx, nidx, cdidx, 102.7)
+
            break
−
    setDihedralIdx(cbidx, caidx, nidx, cdidx, 16.2)
+
        }
  
−
    select {atomIndex=cgidx}
+
        # If no movement in 4 tries, we are done
−
    setDistanceIdx(cdidx, cgidx, 1.51)
+
        if (okCount > 3) {
−
    setAngleIdx(nidx, cdidx, cgidx, 106.4)
+
            break
−
     setDihedralIdx(caidx, nidx, cdidx, cgidx, 16.2)
+
        }
 +
     } # endwhile (targetIdx != gCanchorIdx) {
 
}
 
}
  
−
# Repair side chain
+
# Rebuild Nward rotors set
−
function repairSideChain(targetIdx, nWard) {
+
function tug_track_n() {
 +
 
 +
    gOK = true
  
−
     var idx = (nWard ? getCwardBBidx(targetIdx, gChain) : getNwardBBidx(targetIdx, gChain))
+
    # For all bb atoms nWard of cargo
 +
    var targetIdx = gNcargoIdx
 +
     var okCount = 0
  
−
     if (({atomIndex=targetIdx}.atomName == "CA")
+
     # Allow collisions with cargo
−
        and ({atomIndex=targetIdx}.group != "GLY")) {
+
    gOkCollide = gCargoSet
−
        var cbidx = getCBidx(targetIdx)
+
    var tcount = 0
−
        select none
+
    while (targetIdx != gNanchorIdx) {
−
         selectAddSideChain(cbidx)
+
 
−
         setAngleIdx(idx, targetIdx, cbidx, 110.0)
+
         # Step to next atom
−
        setDistanceIdx(targetIdx, cbidx, 1.5)
+
         targetIdx = get_nward_bb_idx(targetIdx, gChain)
−
         if ({atomIndex=targetIdx}.group != "PRO") {
+
         if (targetIdx < 0) {
−
            var colliders = (within(kCtolerance, FALSE, {selected})
+
             break
−
                and not {atomIndex=targetIdx} and not {selected})
 
−
            if (colliders.size > 0) {
 
−
                if ({atomIndex=targetIdx}.group != "ALA") {
 
−
                    fixSCcollision2(cbidx)
 
−
                }
 
−
             }
 
 
         }
 
         }
−
         else {
+
          
−
            if (nWard) {
+
        # No collision with cargo allowed after two atoms placed
−
            }
+
        if (tcount == 2) {
−
            else {
+
          gOkCollide = ({})
−
                setDihedralIdx(getNwardBBidx(idx, gChain), idx, targetIdx, cbidx, 174.2)
 
−
            }
 
 
         }
 
         }
−
    }
+
        tcount++
 +
 
 +
        # Compute targets desired coords
 +
        var n1idx = get_nward_bb_idx(targetIdx, gChain)
 +
        var c1idx = get_cward_bb_idx(targetIdx, gChain)
 +
        var c2idx = get_cward_bb_idx(c1idx, gChain)
 +
        var c3idx = get_cward_bb_idx(c2idx, gChain)
 +
        var pt = {0 0 0}
 +
        if ({atomIndex=targetIdx}.atomName == "CA") {
  
−
    else if ({atomIndex=targetIdx}.atomName == "C") {
+
            # Desired target location is trigonal O
−
        var oidx = getOidx(targetIdx)
+
            var oidx = get_o_idx(c1idx)
−
        select {atomIndex=oidx}
+
            select {atomIndex=oidx}
−
        setAngleIdx(idx, targetIdx, oidx, 120.0)
+
            set_distance_idx(c1idx, oidx, 1.39)
−
        setDistanceIdx(targetIdx, oidx, 1.21)
+
             pt = get_trigonal_idx(c2idx, c1idx, oidx, 1.41)
−
        if (nWard) {
+
             set_distance_idx(c1idx, oidx, 1.21)
−
             setDihedralIdx(getCwardBBidx(idx, gChain), idx, targetIdx, oidx, 0.0)
 
−
        }
 
−
        if ({atomIndex=idx}.group == "PRO") {
 
−
             repairProline(idx)
 
−
            var dNo = {atomIndex=targetIdx}.atomno + 4
 
−
            var dIdx = {(atomno=dNO) and (chain=gChain)}.atomIndex
 
−
            var colliders = (within(kCtolerance, FALSE, {atomIndex=dIdx})
 
−
                and not connected({atomIndex=dIdx})
 
−
                and not {atomIndex=dIdx})
 
−
            for (var i = 1; i <= colliders.size; i++) {
 
−
                if (colliders[i].atomName == "O") {
 
−
                    counterRotate2(colliders[i].atomIndex,
 
−
                        {atomIndex=dIdx}.xyz, targetIdx, FALSE)
 
−
                }
 
−
            }
 
 
         }
 
         }
−
    }
+
        else if (({atomIndex=targetIdx}.atomName == "N")
−
}
+
            and ({atomIndex=targetIdx}.group != "GLY")) {
  
−
# Rebuild Cward rotors set
+
            # Desired target location is r-tetragonal CB
−
function tugTrackC() {
+
            var cbidx = get_cb_idx(c1idx)
 +
            pt = get_tet_idx(cbidx, c1idx, c2idx, 1.5)
 +
        }
 +
        else { # C
  
−
    # For all bb atoms cWard of cargo
+
            # Save current target coords
−
    var targetIdx = gCcargoIdx
+
            var cp = {atomIndex=targetIdx}.xyz
−
    var okCount = 0
 
  
−
    # Allow collisions with cargo
+
            # Set target atom at desired distance and angle
−
    gOkCollide = gCargoSet
+
            select {atomIndex=targetIdx}
−
    var tcount = 0
+
            set_distance_idx(c1idx, targetIdx, 1.37)
−
    while (targetIdx != gCanchorIdx) {
+
            set_angle_idx(c2idx, c1idx, targetIdx, 110.0)
  
−
        # Step to next atom
+
             if ({atomIndex=targetIdx}.group == "PRO") {
−
        targetIdx = getCwardBBidx(targetIdx, gChain)
+
                set_dihedral_idx(c3idx, c2idx, c1idx, targetIdx, -57.0)
−
 
 
−
        # No collision with cargo allowed after two atoms placed
 
−
        if (tcount == 2) {
 
−
          gOkCollide = ({})
 
−
        }
 
−
        tcount++
 
−
 
 
−
        # Compute targets desired coords
 
−
        var c1idx = getCwardBBidx(targetIdx, gChain)
 
−
        var n1idx = getNwardBBidx(targetIdx, gChain )
 
−
        var n2idx = getNwardBBidx(n1Idx, gChain)
 
−
        var n3idx = getNwardBBidx(n2Idx, gChain)
 
−
        var pt = {0 0 0}
 
−
        if ({atomIndex=targetIdx}.atomName == "N") {
 
−
            var oidx = getOidx(n1idx)
 
−
            select {atomIndex=oidx}
 
−
 
 
−
            # Desired target location is trigonal O
 
−
            setDistanceIdx(n1idx, oidx, 1.5)
 
−
             pt = getTrigonal(n2idx, n1idx, oidx, 1.37)
 
−
            setDistanceIdx(n1idx, oidx, 1.21)
 
−
        }
 
−
        else if (({atomIndex=targetIdx}.atomName == "C")
 
−
            and ({atomIndex=targetIdx}.group != "GLY")) {
 
−
 
 
−
            # Desired target location is tetragonal CB
 
−
            var cbidx = getCBidx(n1idx)
 
−
            pt = getTet(n2idx, n1idx, cbidx, 1.5)
 
−
        }
 
−
        else { # CA (or GLY C)
 
−
 
 
−
            # Save current target coords
 
−
            var cp = {atomIndex=targetIdx}.xyz
 
−
 
 
−
            # Set target atom at desired distance and angle
 
−
            select {atomIndex=targetIdx}
 
−
            setDistanceIdx(n1idx, targetIdx, 1.5)
 
−
            setAngleIdx(n2idx, n1idx, targetIdx, 120.0)
 
−
            if ({atomIndex=targetIdx}.atomName == "CA") {
 
−
                setDihedralIdx(n3idx, n2idx, n1idx, targetIdx, 180)
 
 
             }
 
             }
  
Line 921: Line 823:
 
         # If target not at desired location
 
         # If target not at desired location
 
         if (distance(pt, {atomIndex=targetIdx}) > kDtolerance) {
 
         if (distance(pt, {atomIndex=targetIdx}) > kDtolerance) {
−
             okCount = 0
+
             var okCount = 0
 
             gTargetPt = pt
 
             gTargetPt = pt
 
             var xcount = 0
 
             var xcount = 0
−
             gOK = FALSE
+
             gOK = false
 
             while ((xcount < 20) and (not gOK)) {
 
             while ((xcount < 20) and (not gOK)) {
  
−
                 # Rotate on cWard rotor set to move it there
+
                 # Rotate on nWard rotor set to move it there
−
                 tugTrackIdx(targetIdx, pt, FALSE, FALSE)
+
                 tug_track_idx(targetIdx, pt, true, false)
 
                 xcount++
 
                 xcount++
 
             }
 
             }
 
         }
 
         }
 
         else {
 
         else {
−
             gOK = TRUE
+
             gOK = true
 
             okCount++
 
             okCount++
 
         }
 
         }
  
−
         # If successful
+
         # If sucessful
−
         if (gOK == TRUE) {
+
         if (gOK == true) {
  
 
             # Adust any side atoms
 
             # Adust any side atoms
−
             repairSideChain(targetIdx, FALSE)
+
             repair_sc(targetIdx, true)
 
         }
 
         }
  
Line 953: Line 855:
 
             break
 
             break
 
         }
 
         }
−
     } # endwhile (targetIdx != gCanchorIdx) {
+
 
 +
     }   # endwhile (targetIdx != gNanchorIdx) {
 
}
 
}
  
−
# Rebuild Nward rotors set
+
# gPlicoRecord is maintained by the macro pilcoRecord
−
function tugTrackN() {
+
function translate_selected_record(pt) {
 +
    if (gPlicoRecord != "") {
 +
        plico_record(format("select %s;translateSelected %s;", {selected}, pt))
 +
    }
 +
    translateSelected @pt
 +
}
  
−
     gOK = TRUE
+
# gPlicoRecord is maintained by the macro pilcoRecord
 +
function rotate_selected_record(pivotIdx, caxis, a) {
 +
     if (gPlicoRecord != "") {
 +
        plico_record(format("select %s;", {selected}))
 +
        plico_record(format("rotateSelected {atomIndex=%d} @%s @%s;",
 +
            pivotIdx, caxis, a))
 +
    }
 +
    rotateSelected {atomIndex=pivotIdx} @caxis @a
 +
}
  
−
     # For all bb atoms nWard of cargo
+
function collect_sc_rotors(no, iChain) {
−
     var targetIdx = gNcargoIdx
+
     var scBondIdxs = array()
−
    var okCount = 0
+
     for (var iNo = no; iNo >= 0; iNo--) {
−
 
+
        var ile = 0
−
    # Allow collisions with cargo
+
        switch ({(atomno=iNo) and (chain=iChain)
−
    gOkCollide = gCargoSet
+
            and thisModel}.atomName) {
−
    var tcount = 0
+
        case "CA" :
−
    while (targetIdx != gNanchorIdx) {
+
            return scBondIdxs # Early exit since break 1 appears broken
 +
        case "CZ" :
 +
            if ({(atomno=iNo) and (chain=iChain)
 +
                and thisModel}.group == "TYR") {
 +
                break
 +
            }
 +
        case "CE" :
 +
            if ({(atomno=iNo) and (chain=iChain)
 +
                and thisModel}.group == "MET") {
 +
                break
 +
            }
 +
        case "CG1" :
 +
            if ({(atomno=iNo) and (chain=iChain)
 +
                and thisModel}.group == "VAL") {
 +
                break
 +
            }
 +
            if ({(atomno=iNo) and (chain=iChain)
 +
                and thisModel}.group == "ILE") {
 +
                ile = 1
 +
            }
 +
        case "NE" :
 +
        case "CD" :
 +
        case "SD" :
 +
        case "CG" :
 +
        case "CB" :
 +
            scBondIdxs += {(atomno=@{iNo+1+ile}) and (chain=iChain)
 +
                and thisModel}.atomIndex
 +
            scBondIdxs += {(atomno=@{iNo+0}) and (chain=iChain)
 +
                and thisModel}.atomIndex
 +
            if ({(atomno=iNo) and (chain=iChain)
 +
                and thisModel}.atomName%2 == "CG") {
 +
                scBondIdxs += {(atomno=@{iNo-1}) and (chain=iChain)
 +
                    and thisModel}.atomIndex
 +
                scBondIdxs += {(atomno=@{iNo-4}) and (chain=iChain)
 +
                    and thisModel}.atomIndex
 +
            }
 +
            else if ({(atomno=iNo) and (chain=iChain)
 +
                and thisModel}.atomName == "CB") {
 +
                scBondIdxs += {(atomno=@{iNo-3}) and (chain=iChain)
 +
                    and thisModel}.atomIndex
 +
                scBondIdxs += {(atomno=@{iNo-4}) and (chain=iChain)
 +
                    and thisModel}.atomIndex
 +
            }
 +
            else {
 +
                scBondIdxs += {(atomno=@{iNo-1}) and (chain=iChain)
 +
                    and thisModel}.atomIndex
 +
                scBondIdxs += {(atomno=@{iNo-2}) and (chain=iChain)
 +
                    and thisModel}.atomIndex
 +
            }
 +
            break
 +
        }
  
−
        # Step to next atom
+
    }
−
        targetIdx = getNwardBBidx(targetIdx, gChain)
 
  
−
        # No collision with cargo allowed after two atoms placed
+
    return scBondIdxs
−
        if (tcount == 2) {
+
}
−
          gOkCollide = ({})
 
−
        }
 
−
        tcount++
 
  
−
        # Compute targets desired coords
+
# Drag Side Chain
−
        var n1idx = getNwardBBidx(targetIdx, gChain)
+
function drag_sc() {
−
        var c1idx = getCwardBBidx(targetIdx, gChain)
+
    var iNo = {atomIndex=gSCidx}.atomno
−
        var c2idx = getCwardBBidx(c1idx, gChain)
+
    var iChain  = {atomIndex=gSCidx}.chain
−
        var c3idx = getCwardBBidx(c2idx, gChain)
+
    if ({atomIndex=gSCidx}.group != "PRO") {
−
        var pt = {0 0 0}
 
−
        if ({atomIndex=targetIdx}.atomName == "CA") {
 
  
−
            # Desired target location is trigonal O
+
        var scBondIdxs = collect_sc_rotors( iNo, iChain)
−
            var oidx = getOidx(c1idx)
+
        var numChi = scBondIdxs.size / 4
−
            select {atomIndex=oidx}
+
         var dist = distance({atomIndex=gSCidx}.xyz, gSCpt)
−
            setDistanceIdx(c1idx, oidx, 1.39)
+
        var scSet = get_sc_set(gSCidx, iChain)
−
            pt = getTrigonal(c2idx, c1idx, oidx, 1.41)
 
−
            setDistanceIdx(c1idx, oidx, 1.21)
 
−
         }
 
−
        else if (({atomIndex=targetIdx}.atomName == "N")
 
−
            and ({atomIndex=targetIdx}.group != "GLY")) {
 
  
−
            # Desired target location is r-tetragonal CB
+
        # For all rotor combinations
−
            var cbidx = getCBidx(c1idx)
+
        var dh = array()
−
             pt = getTet(cbidx, c1idx, c2idx, 1.5)
+
        for (var i = 0; i < numChi; i++) {
 +
             dh += angle({atomIndex=@{scBondIdxs[4+(4*i)]}},
 +
                {atomIndex=@{scBondIdxs[3+(4*i)]}},
 +
                {atomIndex=@{scBondIdxs[2+(4*i)]}},
 +
                {atomIndex=@{scBondIdxs[1+(4*i)]}})
 
         }
 
         }
−
         else { # C
+
         for (var i = 0; i < numChi; i++) {
 +
            var rot = -120
 +
            for (var j = 0; j < 6; j++) {
 +
                rot += 60*j
 +
                select_add_sc(scBondIdxs[1+(4*i)])
 +
                set_dihedral_idx(scBondIdxs[4+(4*i)], scBondIdxs[3+(4*i)],
 +
                    scBondIdxs[2+(4*i)], scBondIdxs[1+(4*i)], rot)
 +
                var newDist = distance({atomIndex=gSCidx}.xyz, gSCpt)
 +
                if (gSCcheck) {
 +
                    var colliders = (within(kCtolerance, false, scSet)
 +
                        and not connected(scSet) and not {scSet})
 +
                    if (colliders) {
 +
                        continue
 +
                    }
 +
                }
  
−
            # Save current target coords
+
                # Find the best
−
            var cp = {atomIndex=targetIdx}.xyz
+
                if (newDist < dist) {
−
 
+
                    dist = newDist
−
            # Set target atom at desired distance and angle
+
                    for (var k = 0; k < numChi; k++) {
−
            select {atomIndex=targetIdx}
+
                        dh[k+1] = angle({atomIndex=@{scBondIdxs[4+(4*k)]}},
−
            setDistanceIdx(c1idx, targetIdx, 1.37)
+
                        {atomIndex=@{scBondIdxs[3+(4*k)]}},
−
            setAngleIdx(c2idx, c1idx, targetIdx, 110.0)
+
                        {atomIndex=@{scBondIdxs[2+(4*k)]}},
−
 
+
                        {atomIndex=@{scBondIdxs[1+(4*k)]}})
−
            if ({atomIndex=targetIdx}.group == "PRO") {
+
                    }
−
                 setDihedralIdx(c3idx, c2idx, c1idx, targetIdx, -57.0)
+
                 }
 
             }
 
             }
 +
        }
  
−
            # Record and restore target
+
        # Now set the best
−
            pt = {atomIndex=targetIdx}.xyz
+
        for (var i = 0; i < numChi; i++) {
−
             {atomIndex=targetIdx}.xyz = cp
+
             select_add_sc(scBondIdxs[1+(4*i)])
 +
            set_dihedral_idx(scBondIdxs[4+(4*i)], scBondIdxs[3+(4*i)],
 +
                scBondIdxs[2+(4*i)], scBondIdxs[1+(4*i)], dh[i+1])
 +
        }
 +
       
 +
        if (gSCcheck = false) {
 +
            plico_minimize( scSet)
 
         }
 
         }
 +
    }
 +
    else { # PRO - toggle between puckers up and down
 +
        var icd = {(atomno=@{iNo+1}) and (chain=iChain)
 +
            and thisModel}.atomIndex
 +
        var icb = {(atomno=@{iNo-1}) and (chain=iChain)
 +
            and thisModel}.atomIndex
 +
        var ica = {(atomno=@{iNo-4}) and (chain=iChain)
 +
            and thisModel}.atomIndex
 +
        var in = {(atomno=@{iNo-5}) and (chain=iChain)
 +
            and thisModel}.atomIndex
 +
        select {atomIndex=gSCidx}
  
−
        # If target not at desired location
+
         if (angle({atomIndex=ica}, {atomIndex=in},
−
         if (distance(pt, {atomIndex=targetIdx}) > kDtolerance) {
+
             {atomIndex=icd}, {atomIndex=gSCidx}) < -10.0) {
−
            var okCount = 0
+
             set_dihedral_idx(ica, in, icd, gSCidx, 8.7)
−
             gTargetPt = pt
+
            set_angle_idx(in, icd, gSCidx, 110.0)
−
            var xcount = 0
+
             set_distance_idx(icd, gSCidx, 1.5)
−
             gOK = FALSE
 
−
            while ((xcount < 20) and (not gOK)) {
 
−
 
 
−
                # Rotate on nWard rotor set to move it there
 
−
                tugTrackIdx(targetIdx, pt, TRUE, FALSE)
 
−
                xcount++
 
−
             }
 
 
         }
 
         }
 
         else {
 
         else {
−
             gOK = TRUE
+
             set_dihedral_idx(ica, in, icd, gSCidx, -29.5)
−
             okCount++
+
             set_angle_idx(in, icd, gSCidx, 108.8)
 +
            set_distance_idx(icd, gSCidx, 1.5)
 
         }
 
         }
 +
    }
  
−
        # If sucessful
+
    draw gSCcircle CIRCLE {atomIndex=gSCidx} MESH NOFILL
−
        if (gOK == TRUE) {
+
    gSCpt = {atomIndex=gSCidx}.xyz
 +
}
  
−
            # Adust any side atoms
+
# Fix side chain collisions
−
            repairSideChain(targetIdx, TRUE)
+
function fix_sc_collision_2(idx) {
−
         }
+
    gOk2 = false
 +
    var iNo = {atomIndex=idx}.atomno
 +
    var iChain = {atomIndex=idx}.chain
 +
    var resno = {(atomno=iNo) and (chain=iChain)
 +
         and thisModel}.resno
  
−
        # Else fail
+
    # Get SC terminus
−
         else {
+
    while (resno == {(atomno=iNo) and (chain=iChain)
−
            break
+
         and thisModel}.resno) {
−
        }
+
        iNo++
 +
    }
 +
    iNo--
  
−
         # If no movement in 4 tries, we are done
+
    var sc = array()
−
         if (okCount > 3) {
+
    var iBno = iNo
−
             break
+
    while ({(atomno=iBno) and (chain=iChain)
−
         }
+
         and thisModel}.atomName != "CB") {
 +
         sc += {(atomno=iBno) and (chain=iChain)
 +
             and thisModel}
 +
        iBno--
 +
    }
 +
    var cbidx = {(atomno=iBno) and (chain=iChain)
 +
         and thisModel}.atomIndex
  
−
     }  # endwhile (targetIdx != gNanchorIdx) {
+
     var scBondIdxs = collect_sc_rotors( iNo, iChain)
−
}
+
    var numChi = scBondIdxs.size / 4
  
−
# gPlicoRecord is maintained by the macro pilcoRecord
+
    # For all rotor combinations
−
function plicoRecord(s) {
+
    for (var i = 0; i < numChi; i++) {
−
    var g = format("show file \"%s\"", gPlicoRecord)
+
        var rot = -120
−
    var ls = script(g)
+
        for (var j = 0; j < 6; j++) {
−
    if (ls.find("FileNotFoundException")) {
+
            rot += 60
−
        ls = ""
+
            select_add_sc(scBondIdxs[1+(4*i)])
−
    }
+
            set_dihedral_idx(scBondIdxs[1+(4*i)], scBondIdxs[2+(4*i)],
−
    ls += s
+
                scBondIdxs[3+(4*i)], scBondIdxs[4+(4*i)], rot)
−
    write var ls @gPlicoRecord
 
−
}
 
  
−
# gPlicoRecord is maintained by the macro pilcoRecord
+
            # If no collision, exit
−
function translateSelectedRecord(pt) {
+
            var colliders = (within(kCtolerance, false, {sc})
−
    if (gPlicoRecord != "") {
+
                and not {atomIndex=cbidx} and not {sc})
−
        plicoRecord(format("select %s;translateSelected %s;", {selected}, pt))
 
−
    }
 
−
    translateSelected @pt
 
−
}
 
  
−
# gPlicoRecord is maintained by the macro pilcoRecord
+
            # If it is with water, delete the water
−
function rotateSelectedRecord(pivotIdx, axis, a) {
+
            for (var c = 1; c < colliders.size; c++ ) {
−
    if (gPlicoRecord != "") {
+
                if (colliders[c].group = "HOH") {
−
        plicoRecord(format("select %s;", {selected}))
+
                    delete {atomIndex=@{colliders[c].atomIndex}}
−
        plicoRecord(format("rotateSelected {atomIndex=%d} @%s @%s;",
+
                    colliders = {colliders and not @{colliders[c]}}
−
            pivotIdx, axis, a))
+
                }
 +
            }
 +
 
 +
            if (colliders.size == 0) {
 +
                gOk2 = true
 +
                return # Early exit since break 1 appears broken
 +
            }
 +
 
 +
        }
 
     }
 
     }
−
    rotateSelected {atomIndex=pivotIdx} @axis @a
 
 
}
 
}
  
−
function collectSCrotors(no, iChain) {
+
function is_moveable_sc(aIdx) {
−
    var scBondIdxs = array()
+
 
−
     for (var iNo = no; iNo >= 0; iNo--) {
+
     var ret = (({atomIndex=aIdx}.group != "PRO")
−
        var ile = 0
+
         or ({atomIndex=aIdx}.atomName == "CG"))
−
        switch ({(atomno=iNo) and (chain=iChain)}.atomName) {
+
    switch({atomIndex=aIdx}.atomName) {
−
        case "CA" :
+
    case "N":
−
            return scBondIdxs # Early exit since break 1 appears broken
+
    case "CA":
−
        case "CZ" :
+
    case "C":
−
            if ({(atomno=iNo) and (chain=iChain)}.group == "TYR") {
+
    case "CB":
−
                break
+
    case "O":
−
            }
+
    case "O4\'":
−
         case "CE" :
+
        ret = false
−
            if ({(atomno=iNo) and (chain=iChain)}.group == "MET") {
+
        break
−
                break
+
    }
−
            }
+
    return ret
−
        case "CG1" :
+
}
−
            if ({(atomno=iNo) and (chain=iChain)}.group == "VAL") {
+
 
−
                break
+
function is_rotor_avail(i1idx, i2idx) {
−
            }
+
    var ret = true
−
            if ({(atomno=iNo) and (chain=iChain)}.group == "ILE") {
+
    if (i1idx > i2idx) { # frozen bonds are kept as lo-hi
−
                ile = 1
+
        var idx = @i1idx
−
            }
+
        i1idx = @i2idx
−
        case "NE" :
+
        i2idx = @idx
−
        case "CD" :
+
    }
−
        case "SD" :
+
 
−
        case "CG" :
+
    for (var i = 1; i <= gFreeze.size; i += 2) {
−
        case "CB" :
+
        if ((gFreeze[i] == i1idx) and (gFreeze[i+1] == i2idx)) {
−
            scBondIdxs += {(atomno=@{iNo+1+ile}) and (chain=iChain)}.atomIndex
+
             ret = false
−
            scBondIdxs += {(atomno=@{iNo+0}) and (chain=iChain)}.atomIndex
 
−
            if ({(atomno=iNo) and (chain=iChain)}.atomName%2 == "CG") {
 
−
                scBondIdxs += {(atomno=@{iNo-1}) and (chain=iChain)}.atomIndex
 
−
                scBondIdxs += {(atomno=@{iNo-4}) and (chain=iChain)}.atomIndex
 
−
            }
 
−
            else if ({(atomno=iNo) and (chain=iChain)}.atomName == "CB") {
 
−
                scBondIdxs += {(atomno=@{iNo-3}) and (chain=iChain)}.atomIndex
 
−
                scBondIdxs += {(atomno=@{iNo-4}) and (chain=iChain)}.atomIndex
 
−
            }
 
−
            else {
 
−
                scBondIdxs += {(atomno=@{iNo-1}) and (chain=iChain)}.atomIndex
 
−
                scBondIdxs += {(atomno=@{iNo-2}) and (chain=iChain)}.atomIndex
 
−
             }
 
 
             break
 
             break
 
         }
 
         }
−
 
 
     }
 
     }
  
−
     return scBondIdxs
+
     return ret
 
}
 
}
  
−
# Drag Side Chain
+
function xcollect_bb_rotors(nWard) {
−
function dragSC() {
+
    var anchorNo = (nWard
 +
        ? ((gNanchorIdx >= 0) ? {atomIndex=gNanchorIdx}.atomno : gMinNo)
 +
        : ((gCanchorIdx >= 0) ? {atomIndex=gCanchorIdx}.atomno : gMaxNo))
 +
    var cargoNo = (nWard
 +
        ? ((gNcargoIdx >= 0) ? {atomIndex=gNcargoIdx}.atomno
 +
        : {atomIndex=gCcargoIdx}.atomno)
 +
        : {atomIndex=gCcargoIdx}.atomno)
 +
    var rotors = array()
 +
    if (cargoNo < anchorNo) {
  
−
    var iNo = {atomIndex=gSCidx}.atomno
+
        for (var iNo = cargoNo; iNo <= anchorNo; iNo++) {
−
    var iChain  = {atomIndex=gSCidx}.chain
+
            if ({(atomno=iNo) and (chain=gChain)
 +
                and thisModel}.atomName == "CA") {
 +
                if (is_rotor_avail(iNo)) {# xxx
 +
                    if (({(atomno=iNo) and (chain=gChain)
 +
                        and thisModel}.group != "PRO") and (iNo > cargoNo)) { # phi
 +
                        rotors += [{(atomno=@{get_cm_no(iNo-1)}) and (chain=gChain)
 +
                            and thisModel}.atomIndex,
 +
                            {(atomno=@{iNo-1}) and (chain=gChain)
 +
                                and thisModel}.atomIndex]
 +
                        rotors += [{(atomno=@{iNo}) and (chain=gChain)
 +
                            and thisModel}.atomIndex,
 +
                            {(atomno=@{iNo+1}) and (chain=gChain)
 +
                                and thisModel}.atomIndex]
 +
                    }
 +
                    if (iNo != (anchorNo-1)) { # psi
 +
                        rotors += [{(atomno=@{iNo-1}) and (chain=gChain)
 +
                            and thisModel}.atomIndex,
 +
                            {(atomno=@{iNo}) and (chain=gChain)
 +
                                and thisModel}.atomIndex]
 +
                        rotors += [{(atomno=@{iNo+1}) and (chain=gChain)
 +
                            and thisModel}.atomIndex,
 +
                            {(atomno=@{get_np_no(iNo+2)}) and (chain=gChain)
 +
                                and thisModel}.atomIndex]
 +
                    }
 +
                }
 +
            }
 +
        }
 +
    }
 +
    else {
  
−
    if ({atomIndex=gSCidx}.group != "PRO") {
+
        for (var iNo = cargoNo; iNo >= anchorNo; iNo--) {
 +
            if ({(atomno=iNo) and (chain=gChain)
 +
                and thisModel}.atomName == "CA") {
 +
                if (is_rotor_avail(iNo)) {
 +
                    if ((iNo != (anchorNo-1)) and (iNo < cargoNo)) { # psi
 +
                        rotors += [{(atomno=@{get_np_no(iNo+2)}) and (chain=gChain)
 +
                            and thisModel}.atomIndex,
 +
                            {(atomno=@{iNo+1}) and (chain=gChain)
 +
                                and thisModel}.atomIndex]
 +
                        rotors += [{(atomno=@{iNo}) and (chain=gChain)
 +
                            and thisModel}.atomIndex,
 +
                            {(atomno=@{iNo-1}) and (chain=gChain)
 +
                                and thisModel}.atomIndex]
 +
                    }
 +
                    if ({(atomno=iNo) and (chain=gChain)
 +
                        and thisModel}.group != "PRO") { # phi
 +
                        rotors += [{(atomno=@{iNo+1}) and (chain=gChain)
 +
                            and thisModel}.atomIndex,
 +
                            {(atomno=@{iNo}) and (chain=gChain)
 +
                                and thisModel}.atomIndex]
 +
                        rotors += [{(atomno=@{iNo-1}) and (chain=gChain)
 +
                            and thisModel}.atomIndex,
 +
                            {(atomno=@{get_cm_no(iNo-1)}) and (chain=gChain)
 +
                                and thisModel}.atomIndex]
 +
                    }
 +
                }
 +
            }
 +
        }
 +
    }
  
−
        var scBondIdxs = collectSCrotors( iNo, iChain)
+
    if (nWard) {
−
         var numChi = scBondIdxs.size / 4
+
         gNrotors = rotors
−
         var dist = distance({atomIndex=gSCidx}.xyz, gSCpt)
+
    }
−
 
+
    else {
−
        var scSet = ({})
+
         gCrotors = rotors
−
        if (gSCcheck) {
+
    }
−
            scSet = getScSet(gSCidx, iChain)
+
}
−
         }
+
function collect_bb_rotors(nWard) {
−
          
+
    if (nWard) {
−
         # For all rotor combinations
+
        gNrotors = array()
−
         var dh = array()
+
         var nres = {atomIndex=gNcargoIdx}.resno
−
         for (var i = 0; i < numChi; i++) {
+
         var nname = {atomIndex=gNcargoIdx}.atomName
−
             dh += angle({atomIndex=@{scBondIdxs[4+(4*i)]}},
+
         var ares = get_resno_min(gChain)
−
                {atomIndex=@{scBondIdxs[3+(4*i)]}},
+
         var aA = get_atom_rcn( ares, gChain, "N")
−
                {atomIndex=@{scBondIdxs[2+(4*i)]}},
+
         if (gNanchorIdx >= 0) {
−
                {atomIndex=@{scBondIdxs[1+(4*i)]}})
+
             aA = {atomIndex=gNanchorIdx}
 
         }
 
         }
−
         for (var i = 0; i < numChi; i++) {
+
        var aname = aA.atomName
−
             var rot = -120
+
         for (var r = nres; r >= ares; r--) {
−
             for (var j = 0; j < 6; j++) {
+
             var aCp = get_atom_rcn( r-1, gChain, "C")
−
                 rot += 60*j
+
             var aN = get_atom_rcn( r, gChain, "N")
−
                selectAddSideChain(scBondIdxs[1+(4*i)])
+
            var aCa = get_atom_rcn( r, gChain, "CA")
−
                 setDihedralIdx(scBondIdxs[4+(4*i)], scBondIdxs[3+(4*i)],
+
            var aC = get_atom_rcn( r, gChain, "C")
−
                    scBondIdxs[2+(4*i)], scBondIdxs[1+(4*i)], rot)
+
            var aNn = get_atom_rcn( r+1, gChain, "N")
−
                 var newDist = distance({atomIndex=gSCidx}.xyz, gSCpt)
+
            if (aCp.size < 1) {
−
                 if (gSCcheck) {
+
                 aCp = aNn
−
                     var colliders = (within(kCtolerance, FALSE, scSet)
+
            }
−
                        and not connected(scSet) and not {scSet})
+
            # psi
−
                     if (colliders.size > 0) {
+
            if (((r < nres) and (r > ares))
−
                         continue
+
                 or ((r == nres) and (nname == "C"))
 +
                or ((r == ares) and (aname != "C"))) {
 +
                 if (is_rotor_avail(aCa.atomIndex, aC.atomIndex)) {
 +
                    gNrotors += aNn.atomIndex
 +
                    gNrotors += aC.atomIndex
 +
                    gNrotors += aCa.atomIndex
 +
                    gNrotors += aN.atomIndex
 +
                }
 +
            }
 +
                       
 +
            # phi
 +
            if (aCa.group != "PRO") {
 +
                 if (((r <= nres) and (r > ares))
 +
                     or ((r == ares) and (aname == "N"))) {
 +
                     if (is_rotor_avail(aCa.atomIndex, aN.atomIndex)) {
 +
                         gNrotors += aC.atomIndex
 +
                        gNrotors += aCa.atomIndex
 +
                        gNrotors += aN.atomIndex
 +
                        gNrotors += aCp.atomIndex
 
                     }
 
                     }
 
                 }
 
                 }
−
               
+
            }
−
                # Find the best
+
        } # endfor
−
                if (newDist < dist) {
+
    }
−
                    dist = newDist
+
    else {
−
                    for (var k = 0; k < numChi; k++) {
+
        gCrotors = array()
−
                        dh[k+1] = angle({atomIndex=@{scBondIdxs[4+(4*k)]}},
+
        var cres = {atomIndex=gCcargoIdx}.resno
−
                        {atomIndex=@{scBondIdxs[3+(4*k)]}},
+
        var cname = {atomIndex=gCcargoIdx}.atomName
−
                        {atomIndex=@{scBondIdxs[2+(4*k)]}},
+
        var ares = get_resno_max(gChain)
−
                         {atomIndex=@{scBondIdxs[1+(4*k)]}})
+
        var aA = get_atom_rcn( ares, gChain, "C")
 +
        if (gCanchorIdx >= 0) {
 +
            aA = {atomIndex=gCanchorIdx}
 +
        }
 +
        var aname = aA.atomName
 +
        for (var r = cres; r <= ares; r++) {
 +
            var aCp = get_atom_rcn( r-1, gChain, "C")
 +
            var aN = get_atom_rcn( r, gChain, "N")
 +
            var aCa = get_atom_rcn( r, gChain, "CA")
 +
            var aC = get_atom_rcn( r, gChain, "C")
 +
            var aNn = get_atom_rcn( r+1, gChain, "N")
 +
            if (aNn.size < 1) {
 +
                aNn = aCp
 +
            }
 +
           
 +
            # phi
 +
            if (aCa.group != "PRO") {
 +
                if (((r > cres) and (r < ares))
 +
                    or ((r == cres) and (aname == "N"))
 +
                    or ((r == ares) and (aname != "N"))) {
 +
                    if (is_rotor_avail(aN.atomIndex, aCa.atomIndex)) {
 +
                        gCrotors += aCp.atomIndex
 +
                         gCrotors += aN.atomIndex
 +
                        gCrotors += aCa.atomIndex
 +
                        gCrotors += aC.atomIndex
 
                     }
 
                     }
 
                 }
 
                 }
 
             }
 
             }
−
         }
+
           
 +
            # psi
 +
            if (((r >= cres) and (r < ares))
 +
                or ((r == cres) and (aname != "C"))
 +
                or ((r == ares) and (aname == "C"))) {
 +
                if (is_rotor_avail(aCa.atomIndex, aC.atomIndex)) {
 +
                    gCrotors += aN.atomIndex
 +
                    gCrotors += aCa.atomIndex
 +
                    gCrotors += aC.atomIndex
 +
                    gCrotors += aNn.atomIndex
 +
                }
 +
            }
 +
         } # endfor
 +
    }
 +
}
  
−
        # Now set the best
+
function collect_rotors() {
−
        for (var i = 0; i < numChi; i++) {
+
    collect_bb_rotors(false)
−
            selectAddSideChain(scBondIdxs[1+(4*i)])
+
     collect_bb_rotors(true)
−
            setDihedralIdx(scBondIdxs[4+(4*i)], scBondIdxs[3+(4*i)],
+
}
−
                scBondIdxs[2+(4*i)], scBondIdxs[1+(4*i)], dh[i+1])
 
−
        }
 
−
    }
 
−
     else { # PRO - toggle between puckers up and down
 
−
        var icd = {(atomno=@{iNo+1}) and (chain=iChain)}.atomIndex
 
−
        var icb = {(atomno=@{iNo-1}) and (chain=iChain)}.atomIndex
 
−
        var ica = {(atomno=@{iNo-4}) and (chain=iChain)}.atomIndex
 
−
        var in = {(atomno=@{iNo-5}) and (chain=iChain)}.atomIndex
 
−
        select {atomIndex=gSCidx}
 
  
−
        if (angle({atomIndex=ica}, {atomIndex=in},
+
function tug_sc(pt) {
−
            {atomIndex=icd}, {atomIndex=gSCidx}) < -10.0) {
+
 
−
             setDihedralIdx(ica, in, icd, gSCidx, 8.7)
+
    # If destination atom defined
−
            setAngleIdx(in, icd, gSCidx, 110.0)
+
    if (gDestAtomIdx >= 0) {
−
            setDistanceIdx(icd, gSCidx, 1.5)
+
        var v = {atomIndex=gDestAtomIdx}.xyz - {atomIndex=gSCidx}.xyz
 +
        if (abs(angle({atomIndex=gDestAtomIdx}.xyz, {0 0 0}, pt)) < 90) {
 +
             pt = -v/20.0
 
         }
 
         }
 
         else {
 
         else {
−
             setDihedralIdx(ica, in, icd, gSCidx, -29.5)
+
             pt = v/20.0
−
            setAngleIdx(in, icd, gSCidx, 108.8)
 
−
            setDistanceIdx(icd, gSCidx, 1.5)
 
 
         }
 
         }
 
     }
 
     }
 +
    gSCpt += pt
 +
    draw arrow {atomIndex=gSCidx} @gSCpt
 +
}
  
−
     draw gSCcircle CIRCLE {atomIndex=gSCidx} MESH NOFILL
+
function set_colors() {
−
     gSCpt = {atomIndex=gSCidx}.xyz
+
    select (thisModel)
 +
    color {selected} @gScheme
 +
     color {atomIndex=g1pivotIdx} green
 +
    color {atomIndex=g2pivotIdx} green
 +
    color @gCargoSet @gAltScheme
 +
    color {gCargoSet and oxygen} pink
 +
     select {(atomIndex=gCcargoIdx) or (atomIndex=gNcargoIdx)
 +
        or (atomIndex=gCanchorIdx) or (atomIndex=gNanchorIdx)}
 +
    halo on
 +
    select {atomIndex=gDestAtomIdx}
 +
    star on
 +
    select none
 
}
 
}
  
−
# Fix side chain collisions
+
function clear_atom_idxs() {
−
function fixSCcollision2(idx) {
+
     gCcargoIdx = -1
−
     gOk2 = FALSE
+
    gNcargoIdx = -1
−
     var iNo = {atomIndex=idx}.atomno
+
     gCanchorIdx = -1
−
     var iChain = {atomIndex=idx}.chain
+
    gNanchorIdx = -1
−
     var resno = {(atomno=iNo) and (chain=iChain)}.resno
+
    g1pivotIdx = -1
 +
     g2pivotIdx = -1
 +
    g1dynamicIdx = -1
 +
     g2dynamicIdx = -1
 +
    gDestAtomIdx = -1
 +
    gSCidx = -1
 +
}
  
−
     # Get SC terminus
+
function timed_out (s) {
−
     while (resno == {(atomno=iNo) and (chain=iChain)}.resno) {
+
     timeout ID"tug" OFF
−
         iNo++
+
     refresh
 +
    if (prompt(format("%s - Undo?", s), "Yes|No", true) == "Yes") {
 +
        gBusy = false
 +
        restore state gState
 +
        connect
 +
        select gCargoSet
 +
        set bondPicking true
 +
        refresh
 +
        for (var i = 1; i <= gFreeze.size; i+=2) {
 +
            select {atomIndex=@{gFreeze[i]}} or {atomIndex=@{gFreeze[i+1]}}
 +
            color bonds lightblue
 +
        }
 +
        background ECHO yellow
 +
        echo @gEcho
 +
         select all
 +
        quit
 
     }
 
     }
−
    iNo--
+
}
  
−
     var sc = array()
+
function record_drag() {
−
     var iBno = iNo
+
     var ls = format("select %s;", {selected})
−
     while ({(atomno=iBno) and (chain=iChain)}.atomName != "CB") {
+
    ls += format("gCanchorIdx = %d;", gCanchorIdx)
−
        sc += {(atomno=iBno) and (chain=iChain)}
+
    ls += format("gCanchorNo = %d;", gCanchorNo)
−
        iBno--
+
    ls += format("gNanchorIdx = %d;", gNanchorIdx)
−
     }
+
    ls += format("gNanchorNo = %d;", gNanchorNo)
−
     var cbidx = {(atomno=iBno) and (chain=iChain)}.atomIndex
+
    ls += format("gCcargoIdx = %d;", gCcargoIdx)
 +
    ls += format("gNcargoIdx = %d;", gNcargoIdx)
 +
    ls += format("gCcargoNo = %d;", gCcargoNo)
 +
    ls += format("gNcargoNo = %d;", gNcargoNo)
 +
    ls += format("gDestAtomIdx = %d;", gDestAtomIdx)
 +
     ls += format("g1pivotIdx = %d;", g1pivotIdx)
 +
     ls += format("g2pivotIdx = %d;", g2pivotIdx)
 +
    ls += format("gOkCollide = %s;", gOkCollide)
 +
    ls += format("gChain = \"%s\";", gChain)
 +
    ls += format("gMinNo = %d;", gMinNo)
 +
    ls += format("gMaxNo = %d;", gMaxNo)
 +
    ls += format("gCargoSet = %s;", gCargoSet)
 +
    ls += format("gSCidx = %d;", gSCidx)
 +
     ls += format("gSCcircle = %d;", gSCcircle)
 +
     ls += format("gSCpt = %s;", gSCpt)
 +
    ls += "collect_rotors();"
 +
    ls += "tug_drag_done_mb();"
 +
    plico_record(ls)
 +
}
  
−
     var scBondIdxs = collectSCrotors( iNo, iChain)
+
# Pick call-back for freeze
−
    var numChi = scBondIdxs.size / 4
+
function tug_pick_cb() {
 +
     if (_pickInfo[3][6] == "bond") {
 +
        var sel = {selected}
 +
        var i = _pickInfo.find(":")
 +
        var iChain = _pickInfo[i+1]
 +
        i = _pickInfo.find("#")
 +
        var a1no = 0 + _pickInfo[i+1][i+3]
 +
        var j = _pickInfo[i+1][9999].find("#")
 +
        var a2no = 0 + _pickInfo[i+j+1][i+j+3]
 +
        var i1idx = {(atomno=a1no) and (chain=iChain)
 +
            and thisModel}.atomIndex
 +
        var i2idx = {(atomno=a2no) and (chain=iChain)
 +
            and thisModel}.atomIndex
 +
        if (({atomIndex=i1idx}.atomName == "CA")
 +
            or ({atomIndex=i2idx}.atomName == "CA")) {
 +
            if (({atomIndex=i1idx}.atomName != "CB")
 +
                and ({atomIndex=i2idx}.atomName != "CB")) {
 +
                if (i1idx > i2idx) {
 +
                    var idx = 0 + i1idx
 +
                    i1idx = 0 + i2idx
 +
                    i2idx = 0 + idx
 +
                }
 +
                select {atomIndex=i1idx} or {atomIndex=i2idx}
  
−
    # For all rotor combinations
+
                for (i = 1; i <= gFreeze.size; i += 2) {
−
    for (var i = 0; i < numChi; i++) {
+
                    if ((gFreeze[i] == i1idx) and (gFreeze[i+1] == i2idx)) {
−
        var rot = -120
+
                        if (i > 1) {
−
        for (var j = 0; j < 6; j++) {
+
                            if (gFreeze.size = (i+1)) {
−
            rot += 60
+
                                gFreeze = gFreeze[1][i-1]
−
            selectAddSideChain(scBondIdxs[1+(4*i)])
+
                            }
−
            setDihedralIdx(scBondIdxs[1+(4*i)], scBondIdxs[2+(4*i)],
+
                            else {
−
                scBondIdxs[3+(4*i)], scBondIdxs[4+(4*i)], rot)
+
                                gFreeze = gFreeze[1][i-1] + gFreeze[i+2][0]
−
               
+
                            }
−
            # If no collision, exit
+
                        }
−
            colliders = (within(kCtolerance, FALSE, {sc})
+
                        else {
−
                and not {atomIndex=cbidx} and not {sc})
+
                            gFreeze = gFreeze[i+2][0]
−
 
+
                        }
−
            # If it is with water, delete the water
+
                        color bonds NONE
−
            for (var c = 1; c < colliders.size; c++ ) {
+
                        i = 0
−
                 if (colliders[c].group = "HOH") {
+
                        break
−
                     delete {atomIndex=@{colliders[c].atomIndex}}
+
                    }
−
                     colliders = {colliders and not @{colliders[c]}}
+
                }
 +
                 if (i > gFreeze.size) {
 +
                     gFreeze += i1idx
 +
                     gFreeze += i2idx
 +
                    color bonds lightblue
 
                 }
 
                 }
 
             }
 
             }
−
 
−
            if (colliders.size == 0) {
 
−
                gOk2 = TRUE
 
−
                return # Early exit since break 1 appears broken
 
−
            }
 
−
 
 
         }
 
         }
 +
        select {sel}
 
     }
 
     }
 
}
 
}
  
−
function isMovableSideChain(aIdx) {
+
# Bound to LEFT-UP by tug_enable_drag
 +
function tug_drag_done_mb() {
 +
    if (not gBusy) {
 +
        if (gPlicoRecord != "") {
 +
            record_drag()
 +
        }
  
−
    var ret = (({atomIndex=aIdx}.group != "PRO")
+
        # Move by rotation on rotor sets, smallest first
−
         or ({atomIndex=aIdx}.atomName == "CG"))
+
         gBusy = true
−
    switch({atomIndex=aIdx}.atomName) {
+
         background ECHO pink
−
    case "N":
+
         refresh
−
    case "CA":
 
−
    case "C":
 
−
    case "CB":
 
−
    case "O":
 
−
    case "O4\'":
 
−
         ret = FALSE
 
−
         break
 
−
    }
 
−
    return ret
 
−
}
 
  
−
function isRotorAvailable(i1idx, i2idx) {
+
        # If side chain mode
−
    var ret = TRUE
+
        if (gSCidx >= 0) {
−
    if (i1idx > i2idx) {
+
            drag_sc()
−
        var idx = @i1idx
 
−
        i1idx = @i2idx
 
−
        i2idx = @idx
 
−
    }
 
−
   
 
−
    for (var i = 1; i <= gFreeze.size; i += 2) {
 
−
        if ((gFreeze[i] == i1idx) and (gFreeze[i+1] == i2idx)) {
 
−
            ret = FALSE
 
−
            break
 
 
         }
 
         }
−
    }
+
 
−
   
+
         # Else
−
    return ret
+
         else if (not gTow) {
−
}
+
            gOK = true
−
 
+
            timeout ID"tug" 20.0 "timed_out(\"Tug timed out\")"
−
function collectBBrotors(nWard) {
+
            if ((gCrotors.size < gNrotors.size) or (gNanchorIdx < 0)) {
−
    var anchorNo = (nWard
+
                 if (gCrotors.size > 4) {
−
         ? ((gNanchorIdx >= 0) ? {atomIndex=gNanchorIdx}.atomno : gMinNo)
+
                    tug_track_c()
−
         : ((gCanchorIdx >= 0) ? {atomIndex=gCanchorIdx}.atomno : gMaxNo))
+
                }
−
    var cargoNo = (nWard
+
                if (gOK and (gNrotors.size > 4)) {
−
        ? ((gNcargoIdx >= 0) ? {atomIndex=gNcargoIdx}.atomno
+
                    tug_track_n()
−
        : {atomIndex=gCcargoIdx}.atomno)
 
−
        : {atomIndex=gCcargoIdx}.atomno)
 
−
    var rotors = array()
 
−
    if (cargoNo < anchorNo) {
 
−
 
 
−
        for (var iNo = cargoNo; iNo <= anchorNo; iNo++) {
 
−
            if ({(atomno=iNo) and (chain=gChain)}.atomName == "CA") {
 
−
                 if (isRotorAvailable(iNo)) {
 
−
                    if (({(atomno=iNo) and (chain=gChain)}.group != "PRO") and (iNo > cargoNo)) { # phi
 
−
                        rotors += [{(atomno=@{getCmNo(iNo-1)}) and (chain=gChain)}.atomIndex,
 
−
                            {(atomno=@{iNo-1}) and (chain=gChain)}.atomIndex]
 
−
                        rotors += [{(atomno=@{iNo}) and (chain=gChain)}.atomIndex,
 
−
                            {(atomno=@{iNo+1}) and (chain=gChain)}.atomIndex]
 
−
                    }
 
−
                    if (iNo != (anchorNo-1)) { # psi
 
−
                        rotors += [{(atomno=@{iNo-1}) and (chain=gChain)}.atomIndex,
 
−
                            {(atomno=@{iNo}) and (chain=gChain)}.atomIndex]
 
−
                        rotors += [{(atomno=@{iNo+1}) and (chain=gChain)}.atomIndex,
 
−
                            {(atomno=@{getNpNo(iNo+2)}) and (chain=gChain)}.atomIndex]
 
−
                    }
 
 
                 }
 
                 }
 
             }
 
             }
−
        }
+
            else {
−
    }
+
                if (gNrotors.size > 4) {
−
    else {
+
                    tug_track_n()
 +
                }
 +
                if (gOK and (gCrotors.size > 4)) {
 +
                    tug_track_c()
 +
                }
 +
            }
 +
            timeout ID"tug" OFF
  
−
        for (var iNo = cargoNo; iNo >= anchorNo; iNo--) {
+
            # If anchor angles acute, fail
−
             if ({(atomno=iNo) and (chain=gChain)}.atomName == "CA") {
+
             if (gOK == true) {
−
                 if (isRotorAvailable(iNo)) {
+
                 if (gCanchorIdx >= 0) {
−
                     if ((iNo != (anchorNo-1)) and (iNo < cargoNo)) { # psi
+
                     var ic = get_cward_bb_idx(gCanchorIdx, gChain)
−
                        rotors += [{(atomno=@{getNpNo(iNo+2)}) and (chain=gChain)}.atomIndex,
+
                    var in = get_nward_bb_idx(gCanchorIdx, gChain)
−
                            {(atomno=@{iNo+1}) and (chain=gChain)}.atomIndex]
+
                    if ((ic >= 0) and
−
                         rotors += [{(atomno=@{iNo}) and (chain=gChain)}.atomIndex,
+
                         angle({atomIndex=ic}, {atomIndex=gCanchorIdx}, {atomIndex=in})
−
                            {(atomno=@{iNo-1}) and (chain=gChain)}.atomIndex]
+
                        < 100.0) {
 +
                        gOK = false
 
                     }
 
                     }
−
                    if ({(atomno=iNo) and (chain=gChain)}.group != "PRO") { # phi
+
                }
−
                        rotors += [{(atomno=@{iNo+1}) and (chain=gChain)}.atomIndex,
+
                if (gNanchorIdx >= 0) {
−
                            {(atomno=@{iNo}) and (chain=gChain)}.atomIndex]
+
                    var ic = get_cward_bb_idx(gNanchorIdx, gChain)
−
                         rotors += [{(atomno=@{iNo-1}) and (chain=gChain)}.atomIndex,
+
                    var in = get_nward_bb_idx(gNanchorIdx, gChain)
−
                            {(atomno=@{getCmNo(iNo-1)}) and (chain=gChain)}.atomIndex]
+
                    if ((in >= 0) and
 +
                         angle({atomIndex=ic}, {atomIndex=gNanchorIdx}, {atomIndex=in})
 +
                        < 100.0) {
 +
                        gOK = false
 
                     }
 
                     }
 
                 }
 
                 }
 
             }
 
             }
−
        }
 
−
    }
 
  
−
    if (nWard) {
+
            # If too far
−
        gNrotors = rotors
+
            if (not gOK) {
−
    }
+
                timed_out("TUG TOO FAR!")
−
    else {
+
            }
−
         gCrotors = rotors
+
 
 +
            # Else OK
 +
            else {
 +
 
 +
                var idx = {
 +
                    (atomno=@{{(chain=gChain) and thisModel}.atomno.min})
 +
                    and (chain=gChain) and thisModel}.atomIndex
 +
 
 +
 
 +
                var ihc = 0
 +
                for (ihc = 0; ihc < 10; ihc++) {
 +
                    select ((atomno >= gNanchorNo) and (atomno <= gCanchorNo)
 +
                        and (chain = gChain) and thisModel)
 +
                    handle_collisions( idx)
 +
                    if (count_collisions(({})).size == 0) {
 +
                        break
 +
                    }
 +
                }
 +
                if (ihc == 10) {
 +
                    timed_out("Unable to handle all collisions!")
 +
                }
 +
            }
 +
        }
 +
        select {gCargoSet}
 +
         gBusy = false
 +
        background ECHO yellow
 +
        set bondPicking true
 +
        refresh
 
     }
 
     }
 
}
 
}
  
−
function collectRotors() {
+
# Bound to ALT-SHIFT-LEFT-DRAG by tug_enable_drag
−
     collectBBrotors(FALSE)
+
function tug_drag_2_mb() {
−
    collectBBrotors(TRUE)
+
     tug_drag_mb(true)
 
}
 
}
  
−
function tugSideChain(pt) {
+
# Bound to ALT-LEFT-DRAG by tug_enable_drag
−
 
+
function tug_drag_mb(alt) {
−
    # If destination atom defined
+
     if (not gBusy) {
−
     if (gDestAtomIdx >= 0) {
+
        gBusy = true
−
         var v = {atomIndex=gDestAtomIdx}.xyz - {atomIndex=gSCidx}.xyz
+
        var dx = (40.0 * (_mouseX - gMouseX))/_width
−
         if (abs(angle({atomIndex=gDestAtomIdx}.xyz, {0 0 0}, pt)) < 90) {
+
         var dy = (40.0 * (_mouseY - gMouseY))/_height
−
             pt = -v/20.0
+
        var q = quaternion()
−
        }
+
        var ptd = {@dx @dy 0}
−
        else {
+
        var pt = (!q)%ptd
−
            pt = v/20.0
+
        var caxis = {0 0 0}
−
        }
+
         if (distance(pt, {0 0 0}) > 0.004) {
−
    }
+
             # If sidechain mode
−
    gSCpt += pt
+
            if (gSCidx >= 0) {
−
    draw arrow {atomIndex=gSCidx} @gSCpt
+
                if ({atomIndex=gSCidx}.atomName == "O") {
−
}
+
                    if ({atomIndex=gSCidx}.group != "PRO") {
 +
                        var dir = ((abs(dx) > abs(dy))
 +
                            ? ((dx < 0) ? 10 : -10)
 +
                            : ((dy < 0) ? 1 : -1))
 +
                        counter_rotate(gSCidx, dir, not alt)
 +
                    }
 +
                }
 +
                else {
 +
                    gSCcheck = not alt
 +
                    tug_sc(pt)
 +
                }
 +
            }
 +
 
 +
            # Else
 +
            else {
  
−
function setColors() {
+
                # If new drag
−
    select all
+
                if (gNewDrag) {
−
    color {selected} @gScheme
+
                    gNewDrag = false
−
    color {atomIndex=g1pivotIdx} green
+
                    save state gState
−
    color {atomIndex=g2pivotIdx} green
+
                }
−
    color @gCargoSet @gAltScheme
 
−
    select {(atomIndex=gCcargoIdx) or (atomIndex=gNcargoIdx)
 
−
        or (atomIndex=gCanchorIdx) or (atomIndex=gNanchorIdx)}
 
−
    halo on
 
−
    select {atomIndex=gDestAtomIdx}
 
−
    star on
 
−
    select none
 
−
}
 
  
−
function clearAtomIdxs() {
+
                # If destination atom defined
−
    gCcargoIdx = -1
+
                if (gDestAtomIdx >= 0) {
−
    gNcargoIdx = -1
+
                    var v = {atomIndex=gDestAtomIdx}.xyz - {selected}.xyz
−
    gCanchorIdx = -1
+
                    if (abs(angle({atomIndex=gDestAtomIdx}.xyz, {0 0 0}, pt)) < 90) {
−
    gNanchorIdx = -1
+
                        pt = -v/20.0
−
    g1pivotIdx = -1
+
                    }
−
    g2pivotIdx = -1
+
                    else {
−
    g1dynamicIdx = -1
+
                        pt = v/20.0
−
    g2dynamicIdx = -1
+
                    }
−
    gDestAtomIdx = -1
+
                }
−
    gSCidx = -1
 
−
}
 
  
−
function timedOut (s) {
+
                # Move the cargo
−
    timeout ID"tug" OFF
+
                select {gCargoSet}
−
    p = prompt(format("%s - Undo?", s), "Yes|No", TRUE)
 
−
    if (p == "Yes") {
 
−
        gBusy = FALSE
 
−
        background ECHO yellow
 
−
        restore state gState
 
−
        connect
 
−
        select gCargoSet
 
−
        refresh
 
−
        quit
 
−
    }
 
−
}
 
  
−
function recordDrag() {
+
                # If pivots defined, rotate it
−
    var ls = format("select %s;", {selected})
+
                if (g1pivotIdx >= 0) {
−
    ls += format("gCanchorIdx = %d;", gCanchorIdx)
 
−
    ls += format("gCanchorNo = %d;", gCanchorNo)
 
−
    ls += format("gNanchorIdx = %d;", gNanchorIdx)
 
−
    ls += format("gNanchorNo = %d;", gNanchorNo)
 
−
    ls += format("gCcargoIdx = %d;", gCcargoIdx)
 
−
    ls += format("gNcargoIdx = %d;", gNcargoIdx)
 
−
    ls += format("gCcargoNo = %d;", gCcargoNo)
 
−
    ls += format("gNcargoNo = %d;", gNcargoNo)
 
−
    ls += format("gDestAtomIdx = %d;", gDestAtomIdx)
 
−
    ls += format("g1pivotIdx = %d;", g1pivotIdx)
 
−
    ls += format("g2pivotIdx = %d;", g2pivotIdx)
 
−
    ls += format("gOkCollide = %s;", gOkCollide)
 
−
    ls += format("gChain = \"%s\";", gChain)
 
−
    ls += format("gMinNo = %d;", gMinNo)
 
−
    ls += format("gMaxNo = %d;", gMaxNo)
 
−
    ls += format("gCargoSet = %s;", gCargoSet)
 
−
    ls += format("gSCidx = %d;", gSCidx)
 
−
    ls += format("gSCcircle = %d;", gSCcircle)
 
−
    ls += format("gSCpt = %s;", gSCpt)
 
−
    ls += "collectRotors();"
 
−
    ls += "tugDragDoneMB();"
 
−
    plicoRecord(ls)
 
−
}
 
  
−
# Pick call-back for freeze
+
                    # If two pivots
−
function tugPickCB() {
+
                    if (g2pivotIdx >= 0) {
 +
                        caxis = {atomIndex=g2pivotIdx}
 +
                    }
  
−
    if (_pickInfo[3][6] == "bond") {
+
                    # Else
−
        var sel = {selected}
+
                    else {
−
        var i = _pickInfo.find(":")
+
                        caxis = cross(pt, {0 0 0}) + {atomIndex=g1pivotIdx}.xyz
−
        var iChain = _pickInfo[i+1]
 
−
        i = _pickInfo.find("#")
 
−
        var a1no = _pickInfo[i+1][i+3]
 
−
        j = _pickInfo[i+1][9999].find("#")
 
−
        var a2no = _pickInfo[i+j+1][i+j+3]
 
−
        var i1idx = {(atomno=a1no) and (chain=iChain)}.atomIndex
 
−
        var i2idx = {(atomno=a2no) and (chain=iChain)}.atomIndex
 
−
       
 
−
        if (({atomIndex=i1idx}.atomName == "CA")
 
−
            or ({atomIndex=i2idx}.atomName == "CA")) {
 
−
            if (({atomIndex=i1idx}.atomName != "CB")
 
−
                and ({atomIndex=i2idx}.atomName != "CB")) {
 
−
                if (i1idx > i2idx) {
 
−
                    idx = 0 + i1idx
 
−
                    i1idx = 0 + i2idx
 
−
                    i2idx = 0 + idx
 
−
                }
 
−
                select {atomIndex=i1idx} or {atomIndex=i2idx}
 
−
               
 
−
                for (i = 1; i <= gFreeze.size; i += 2) {
 
−
                    if ((gFreeze[i] == i1idx) and (gFreeze[i+1] == i2idx)) {
 
−
                        gFreeze[i] == -1
 
−
                        color bonds NONE
 
−
                        break
 
 
                     }
 
                     }
 +
 +
                    var dir = ((abs(dx) > abs(dy))
 +
                        ? ((dx < 0) ? 2 : -2)
 +
                        : ((dy < 0) ? 2 : -2))
 +
                    rotate_selected_record(g1pivotIdx, caxis, dir)
 +
 
                 }
 
                 }
−
                 if (i > gFreeze.size) {
+
 
−
                     gFreeze += i1idx
+
                # Else translate it
−
                    gFreeze += i2idx
+
                 else {
−
                    color bonds lightblue
+
                     translate_selected_record(pt)
 
                 }
 
                 }
−
            }
 
−
        }
 
−
        select {sel}
 
−
    }
 
−
}
 
  
−
# Bound to LEFT-UP by tugEnableDrag
+
                # If collisions
−
function tugDragDoneMB() {
+
                var cNotSels = (within(kCtolerance, false, {selected})
−
    if (not gBusy) {
+
                    and not {gMovingSet})
−
        if (gPlicoRecord != "") {
+
                if ((cNotSels) and (not alt)) {
−
            recordDrag()
+
                    gOk2 = true
−
        }
+
                    for (var i = 1; i <= cNotSels.size;  i++) {
  
−
        # Move by rotation on rotor sets, smallest first
+
                        # If net collision vector same as move vector
−
        gBusy = TRUE
+
                        var cSels = (within(kCtolerance, false, cNotSels[i]) and {selected})
−
        background ECHO pink
+
                        for (var j = 1; j <= cSels.size;  j++) {
−
        refresh
+
                            var v1 = cNotSels[i].xyz - cSels[j].xyz
 +
                            if (abs(angle(v1, {0 0 0}, pt)) < 90) {
  
−
        # If side chain mode
+
                                # If tow mode
−
        if (gSCidx >= 0) {
+
                                if (gTow) {
−
            dragSC()
 
−
        }
 
  
−
        # Else
+
                                    # Make a dynamic pivot
−
        else if (not gTow) {
+
                                    if (g1pivotIdx < 0) {
−
            gOK = TRUE
+
                                        g1pivotIdx = cSels[j].atomIndex
−
            timeout ID"tug" 20.0 "timedOut(\"Tug timed out\")"
+
                                        g1dynamicIdx = cNotSels[i].atomIndex
−
            if ((gCrotors.size < gNrotors.size) or (gNanchorIdx < 0)) {
+
                                        color {atomIndex=g1pivotIdx} lightgreen
−
                if (gCrotors.size > 4) {
+
                                        set_distance_idx(cNotSels[i].atomIndex,
−
                    tugTrackC()
+
                                            cSels[j].atomIndex,
−
                }
+
                                            kCtolerance + kDtolerance)
−
                if (gOK and (gNrotors.size > 4)) {
+
                                    }
−
                    tugTrackN()
+
                                    else if (g2pivotIdx < 0) {
−
                }
+
                                        g2pivotIdx = cSels[j].atomIndex
−
            }
+
                                        g2dynamicIdx = cNotSels[i].atomIndex
−
            else {
+
                                        color {atomIndex=g2pivotIdx} lightgreen
−
                if (gNrotors.size > 4) {
+
                                        set_distance_idx(cNotSels[i].atomIndex,
−
                    tugTrackN()
+
                                            cSels[j].atomIndex,
−
                }
+
                                            kCtolerance + kDtolerance)
−
                if (gOK and (gCrotors.size > 4)) {
+
                                    }
−
                    tugTrackC()
+
                                    else {
−
                }
+
                                        gOk2 = false
−
            }
+
                                    }
−
            timeout ID"tug" OFF
+
                                }
 +
                                else {
  
−
            # If anchor angles acute, fail
+
                                    # Try to resolve
−
            if (gOK == TRUE) {
+
                                    select @{cSels[j]}
−
                if (gCanchorIdx >= 0) {
+
                                    var idx = {(atomno=@{{(chain=gChain)
−
                    var ic = getCwardBBidx(gCanchorIdx, gChain)
+
                                        and thisModel}.atomno.min})
−
                    var in = getNwardBBidx(gCanchorIdx, gChain)
+
                                        and (chain=gChain) and thisModel}.atomIndex
−
                    if ((ic >= 0) and
+
                                    handle_collisions( idx)
−
                        angle({atomIndex=ic}, {atomIndex=gCanchorIdx}, {atomIndex=in})
+
                                }
−
                         < 100.0) {
+
                            }
−
                         gOK = FALSE
+
                        } # endfor
−
                     }
+
                         if (not gOk2) {
−
                }
+
                            break
−
                if (gNanchorIdx >= 0) {
+
                         }
−
                    var ic = getCwardBBidx(gNanchorIdx, gChain)
+
                     } # endfor
−
                    var in = getNwardBBidx(gNanchorIdx, gChain)
+
 
−
                    if ((in >= 0) and
+
                    # If unable
−
                        angle({atomIndex=ic}, {atomIndex=gNanchorIdx}, {atomIndex=in})
+
                    if (not gOk2) {
−
                         < 100.0) {
+
 
−
                         gOK = FALSE
+
                        # Back off
 +
                        background ECHO pink
 +
                        delay 1
 +
                        if (g1pivotIdx >= 0) {
 +
                            rotate_selected_record(g1pivotIdx, caxis, -a)
 +
                        }
 +
                        else {
 +
                            translate_selected_record(-pt)
 +
                        }
 +
                         background ECHO yellow
 +
                         set bondPicking true
 
                     }
 
                     }
 
                 }
 
                 }
 
             }
 
             }
  
−
             # If too far
+
             # If dynamic pivots
−
             if (not gOK) {
+
             if (g1dynamicIdx >= 0) {
−
                 timedOut("TUG TOO FAR!")
+
                 var v1 = {atomIndex=g1dynamicIdx}.xyz - {atomIndex=g1pivotIdx}.xyz
−
            }
+
                if (abs(angle(v1, {0 0 0}, pt)) > 90) {
 +
                    color {atomIndex=g1pivotIdx} @gAltScheme
 +
                    g1pivotIdx = -1
 +
                    g1dynamicIdx = -1
  
−
            # Else OK
 
−
            else {
 
−
 
−
                select ((atomno >= gNanchorNo) and (atomno <= gCanchorNo)
 
−
                    and (chain = gChain))
 
−
                var idx = {(atomno=@{{chain=gChain}.atomno.min})
 
−
                    and (chain=gChain)}.atomIndex
 
−
 
−
 
−
                var ihc = 0
 
−
                for (ihc = 0; ihc < 10; ihc++) {
 
−
                    handleCollisions2( idx)
 
−
                    if (countCollisions(({})) == 0) {
 
−
                        break
 
−
                    }
 
 
                 }
 
                 }
−
                if (ihc == 10) {
+
            }
−
                     timedOut("Unable to handle all collisions!")
+
            if (g2dynamicIdx >= 0) {
 +
                var v1 = {atomIndex=g2dynamicIdx}.xyz - {atomIndex=g2pivotIdx}.xyz
 +
                if (abs(angle(v1, {0 0 0}, pt)) > 90) {
 +
                    color {atomIndex=g2pivotIdx} @gAltScheme
 +
                    g2pivotIdx = -1
 +
                     g2dynamicIdx = -1
 
                 }
 
                 }
 
             }
 
             }
 +
 +
            gMouseX = _mouseX
 +
            gMouseY = _mouseY
 
         }
 
         }
 
         select {gCargoSet}
 
         select {gCargoSet}
−
         gBusy = FALSE
+
         gBusy = false
−
        background ECHO yellow
 
−
        refresh
 
 
     }
 
     }
 
}
 
}
  
−
# Bound to ALT-SHIFT-LEFT-DRAG by tugEnableDrag
+
# Bound to ALT-LEFT-DOWN by tug_enable_drag
−
function tugDrag2MB() {
+
function tug_mark_mb() {
−
     tugDragMB(TRUE)
+
     gMouseX = _mouseX
 +
    gMouseY = _mouseY
 +
    gNewDrag = true
 
}
 
}
  
−
# Bound to ALT-LEFT-DRAG by tugEnableDrag
+
# Called by tug_cargo_mb
−
function tugDragMB(alt) {
+
function tug_enable_drag() {
−
     if (not gBusy) {
+
     gEcho = "__________TUG__________|ALT-CLICK=mark block|SHIFT-CLICK=anchors" +
−
        gBusy = TRUE
+
         "|ALT-CTRL-CLICK=pivots|ALT-SHIFT-CLICK=dest atom|ALT-DRAG=move" +
−
         var dx = (40.0 * (_mouseX - gMouseX))/_width
+
         "|ALT-SHIFT-DRAG=alt move|CLICK bond=freeze|ALT-DOUBLE-CLICK=undo" +
−
        var dy = (40.0 * (_mouseY - gMouseY))/_height
+
         "|SHIFT-DOUBLE-CLICK=exit"
−
         var q = quaternion()
+
    echo @gEcho
−
        var ptd = {@dx @dy 0}
+
 
−
         var pt = (!q)%ptd
+
    # Allow atoms to be dragged
−
        var axis = {0 0 0}
+
    bind "ALT-LEFT-DOWN" "tug_mark_mb";
−
        if (distance(pt,  {0 0 0}) > 0.004) {
+
    bind "ALT-LEFT-UP" "tug_drag_done_mb";
−
            # If sidechain mode
+
    bind "ALT-SHIFT-LEFT-DOWN" "tug_mark_mb";
−
            if (gSCidx >= 0) {
+
    bind "ALT-LEFT-DRAG" "tug_drag_mb";
−
                if ({atomIndex=gSCidx}.atomName == "O") {
+
    bind "ALT-SHIFT-LEFT-DRAG" "tug_drag_2_mb";
−
                    dir = ((abs(dx) > abs(dy))
 
−
                        ? ((dx < 0) ? 2 : -2)
 
−
                        : ((dy < 0) ? 2 : -2))
 
−
                    counterRotate(gSCidx, dir, not alt)
 
−
                }
 
−
                else {
 
−
                    gSCcheck = not alt
 
−
                    tugSideChain(pt)
 
−
                }
 
−
            }
 
  
−
            # Else
+
    unbind "SHIFT-LEFT-CLICK"
−
            else {
+
    bind "SHIFT-LEFT-CLICK" "_pickAtom";
 +
    bind "SHIFT-LEFT-CLICK" "+:tug_anchor_mb";
  
−
                # If new drag
+
    bind "ALT-CTRL-LEFT-CLICK" "_pickAtom";
−
                if (gNewDrag) {
+
    bind "ALT-CTRL-LEFT-CLICK" "+:tug_pivot_mb";
−
                    gNewDrag = FALSE
 
−
                    save state gState
 
−
                }
 
  
−
                # If destination atom defined
+
    bind "ALT-SHIFT-LEFT-CLICK" "_pickAtom";
−
                if (gDestAtomIdx >= 0) {
+
    bind "ALT-SHIFT-LEFT-CLICK" "+:tug_dest_atom_mb";
−
                    var v = {atomIndex=gDestAtomIdx}.xyz - {selected}.xyz
+
}
−
                    if (abs(angle({atomIndex=gDestAtomIdx}.xyz, {0 0 0}, pt)) < 90) {
 
−
                        pt = -v/20.0
 
−
                    }
 
−
                    else {
 
−
                        pt = v/20.0
 
−
                    }
 
−
                }
 
  
−
                # Move the cargo
+
# Bound to SHIFT-LEFT-CLICK by tug_cargo_mb
−
                select {gCargoSet}
+
function tug_anchor_mb() {
 +
    if ({atomIndex=_atomPicked}.chain == gChain) {
 +
        var aPidx = get_sc_bb_idx( _atomPicked, gChain)
  
−
                # If pivots defined, rotate it
+
        var pno = {atomIndex=aPidx}.atomno
−
                if (g1pivotIdx >= 0) {
+
        if (pno > {atomIndex=gCcargoIdx}.atomno) {
−
 
+
            select {atomIndex=gCanchorIdx}
−
                    # If two pivots
+
            halo off
−
                    if (g2pivotIdx >= 0) {
+
            if (gCanchorIdx == aPidx) {
−
                        axis = {atomIndex=g2pivotIdx}
+
                gCanchorIdx = -1
−
                    }
+
                gCanchorNo = gMaxNo + 1
−
 
+
            }
−
                    # Else
+
            else {
−
                    else {
+
                gCanchorIdx = aPidx
−
                        axis = cross(pt, {0 0 0}) + {atomIndex=g1pivotIdx}.xyz
+
                gCanchorNo = {atomIndex=gCanchorIdx}.atomno
−
                    }
+
                select {atomIndex=gCanchorIdx}
−
 
+
                halo on
−
                    dir = ((abs(dx) > abs(dy))
+
            }
−
                        ? ((dx < 0) ? 2 : -2)
+
            collect_bb_rotors(false)
−
                        : ((dy < 0) ? 2 : -2))
+
        }
−
                    rotateSelectedRecord(g1pivotIdx, axis, dir)
+
        else if (pno < {atomIndex=gNcargoIdx}.atomno) {
−
 
+
            select {atomIndex=gNanchorIdx}
−
                 }
+
            halo off
−
 
+
            if (gNanchorIdx == aPidx) {
−
                 # Else translate it
+
                gNanchorIdx = -1
−
                else {
+
                gNanchorNo = gMinNo - 1
−
                    translateSelectedRecord(pt)
+
            }
−
                }
+
            else {
 +
                gNanchorIdx = aPidx
 +
                 gNanchorNo = {atomIndex=gNanchorIdx}.atomno
 +
                select {atomIndex=gNanchorIdx}
 +
                 halo on
 +
            }
 +
            collect_bb_rotors(true)
 +
        }
 +
        else {
 +
            tow_cargo_mb()
 +
        }
  
−
                # If collisions
+
        # Get moving atoms set
−
                var cNotSels = (within(kCtolerance, FALSE, {selected})
+
        gMovingSet = {((atomno < gCanchorNo) and (atomno > gNanchorNo)
−
                    and not {gMovingSet})
+
            and (chain=gChain) and thisModel)}
−
                if ((cNotSels.size > 0) and (not alt)) {
+
    }
−
                    gOk2 = TRUE
+
    select {gCargoSet}
−
                    for (var i = 1; i <= cNotSels.size;  i++) {
+
}
  
−
                        # If net collision vector same as move vector
+
# Bound to ALT-SHIFT-LEFT-CLICK by tug_cargo_mb
−
                        cSels = (within(kCtolerance, FALSE, cNotSels[i]) and {selected})
+
function tug_dest_atom_mb() {
−
                        for (var j = 1; j <= cSels.size;  j++) {
+
    var aOk = true
−
                            var v1 = cNotSels[i].xyz - cSels[j].xyz
+
    if ({atomIndex=_atomPicked}.chain == gChain) {
−
                            if (abs(angle(v1, {0 0 0}, pt)) < 90) {
 
  
−
                                # If tow mode
+
        var pno = {atomIndex=_atomPicked}.atomno
−
                                if (gTow) {
+
        if ((pno <= {atomIndex=gCcargoIdx}.atomno)
 +
            and (pno >= {atomIndex=gNcargoIdx}.atomno)) {
 +
            aOk = false
 +
        }
 +
    }
 +
    if (aOk) {
 +
        select {atomIndex=gDestAtomIdx}
 +
        star off
 +
        if (gDestAtomIdx == _atomPicked) {
 +
            gDestAtomIdx = -1
 +
        }
 +
        else {
 +
            gDestAtomIdx = _atomPicked
 +
            select {atomIndex=gDestAtomIdx}
 +
            star on
 +
        }
 +
        select {gCargoSet}
 +
    }
 +
}
  
−
                                    # Make a dynamic pivot
+
# Bound to CTRL-LEFT-CLICK by tug_cargo_mb
−
                                    if (g1pivotIdx < 0) {
+
function tug_pivot_mb() {
−
                                        g1pivotIdx = cSels[j].atomIndex
+
    if (g1pivotIdx == _atomPicked) {
−
                                        g1dynamicIdx = cNotSels[i].atomIndex
+
        color {atomIndex=g1pivotIdx} @gScheme
−
                                        color {atomIndex=g1pivotIdx} lightgreen
+
        if (g2pivotIdx >= 0) {
−
                                        setDistanceIdx(cNotSels[i].atomIndex,
+
            g1pivotIdx = g2pivotIdx
−
                                            cSels[j].atomIndex,
+
            g2pivotIdx = -1
−
                                            kCtolerance + kDtolerance)
+
            g2dynamicIdx = -1
−
                                    }
+
        }
−
                                    else if (g2pivotIdx < 0) {
+
        else {
−
                                        g2pivotIdx = cSels[j].atomIndex
+
            g1pivotIdx = -1
−
                                        g2dynamicIdx = cNotSels[i].atomIndex
+
            g1dynamicIdx = -1
−
                                        color {atomIndex=g2pivotIdx} lightgreen
+
        }
−
                                        setDistanceIdx(cNotSels[i].atomIndex,
+
    }
−
                                            cSels[j].atomIndex,
+
    else if (g2pivotIdx == _atomPicked) {
−
                                            kCtolerance + kDtolerance)
+
        color {atomIndex=g2pivotIdx} @gScheme
−
                                    }
+
        g2pivotIdx = -1
−
                                    else {
+
        g2dynamicIdx = -1
−
                                        gOk2 = FALSE
+
    }
−
                                    }
+
    else if (g1pivotIdx >= 0) {
−
                                }
+
        if (g2pivotIdx >= 0) {
−
                                else {
+
            color {atomIndex=g2pivotIdx} @gScheme
 +
        }
  
−
                                    # Try to resolve
+
        g2pivotIdx = _atomPicked
−
                                    select cSels[j]
+
        g2dynamicIdx = -1
−
                                    var idx = {(atomno=@{{chain=gChain}.atomno.min})
+
        color {atomIndex=g2pivotIdx} green
−
                                        and (chain=gChain)}.atomIndex
+
    }
−
                                    handleCollisions2( idx)
+
    else {
−
                                }
+
        g1pivotIdx = _atomPicked
−
                            }
+
        g1dynamicIdx = -1
−
                        } # endfor
+
        color {atomIndex=g1pivotIdx} green
−
                        if (not gOk2) {
+
    }
−
                            break
+
    select {gCargoSet}
−
                        }
+
}
−
                    } # endfor
 
  
−
                    # If unable
+
# Bound to SHIFT-LEFT-CLICK by plico_tug
−
                    if (not gOk2) {
+
function tow_cargo_mb() {
 +
    gTow = true
 +
    gChain = {atomIndex=_atomPicked}.chain
 +
    gMinNo = {(chain=gChain) and thisModel}.atomno.min
 +
    gMaxNo = {(chain=gChain) and thisModel}.atomno.max
 +
    gCcargoIdx = -1
 +
    gNcargoIdx = -1
 +
    gCanchorIdx = -1
 +
    gCanchorNo = gMaxNo + 1
 +
    gNanchorIdx = -1
 +
    gNanchorNo = gMinNo - 1
  
−
                        # Back off
+
    # Highlight cargo cluster
−
                        background ECHO pink
+
    select {(chain=gChain) and thisModel}
−
                        delay 1
+
    gCargoSet = {selected}
−
                        if (g1pivotIdx >= 0) {
+
    gMovingSet = {selected}
−
                            rotateSelectedRecord(g1pivotIdx, axis, -a)
+
    set_colors()
−
                        }
 
−
                        else {
 
−
                            translateSelectedRecord(-pt)
 
−
                        }
 
−
                        background ECHO yellow
 
−
                    }
 
−
                }
 
−
            }
 
  
−
            # If dynamic pivots
+
    # Enable dragging
−
            if (g1dynamicIdx >= 0) {
+
    tug_enable_drag()
−
                var v1 = {atomIndex=g1dynamicIdx}.xyz - {atomIndex=g1pivotIdx}.xyz
+
    select {gCargoSet}
−
                if (abs(angle(v1, {0 0 0}, pt)) > 90) {
+
    halo off
−
                    color {atomIndex=g1pivotIdx} @gAltScheme
+
    var es = gEcho.replace("anchors","mark chain")
−
                    g1pivotIdx = -1
+
    echo @es
−
                    g1dynamicIdx = -1
+
    unbind "SHIFT-LEFT-CLICK"
 +
    bind "SHIFT-LEFT-CLICK" "_pickAtom";
 +
    bind "SHIFT-LEFT-CLICK" "+:tow_cargo_mb";
 +
}
  
−
                }
+
# Bound to ALT-LEFT-CLICK by plico_tug or called by plicotoab.toabCargoMB
−
            }
+
function tug_cargo_mb() {
−
            if (g2dynamicIdx >= 0) {
 
−
                var v1 = {atomIndex=g2dynamicIdx}.xyz - {atomIndex=g2pivotIdx}.xyz
 
−
                if (abs(angle(v1, {0 0 0}, pt)) > 90) {
 
−
                    color {atomIndex=g2pivotIdx} @gAltScheme
 
−
                    g2pivotIdx = -1
 
−
                    g2dynamicIdx = -1
 
−
                }
 
−
            }
 
  
−
             gMouseX = _mouseX
+
    # If O or movable side chain atom picked
−
             gMouseY = _mouseY
+
    if ((({atomIndex=_atomPicked}.atomName = "O")
 +
        and ({atomIndex=_atomPicked}.group != "PRO"))
 +
        or (is_moveable_sc( _atomPicked))) {
 +
        if (gSCidx >= 0) {
 +
             draw gSCcircle DELETE
 +
        }
 +
        if (gSCidx == _atomPicked) {
 +
            gSCidx = -1
 +
        }
 +
        else {
 +
            gSCidx = _atomPicked
 +
            gSCpt = {atomIndex=gSCidx}.xyz
 +
             draw gSCcircle CIRCLE {atomIndex=gSCidx} MESH NOFILL
 
         }
 
         }
−
        select {gCargoSet}
 
−
        gBusy = FALSE
 
 
     }
 
     }
−
}
+
    else {
−
 
+
        if ({atomIndex=_atomPicked}.chain != gChain) {
−
# Bound to ALT-LEFT-DOWN by tugEnableDrag
+
            if (gTow) {
−
function tugMarkMB() {
+
                select {(chain=gChain) and thisModel}
−
    gMouseX = _mouseX
+
                color {selected} @gScheme
−
    gMouseY = _mouseY
+
            }
−
    gNewDrag = TRUE
+
            gChain = {atomIndex=_atomPicked}.chain
−
}
+
            select ({atomIndex=gCcargoIdx} or {atomIndex=gNcargoIdx}
−
 
+
                or {atomIndex=gCanchorIdx} or {atomIndex=gNanchorIdx})
−
# Called by tugCargoMB
+
            halo off
−
function tugEnableDrag() {
+
            gCcargoIdx = -1
−
    gEcho = "__________TUG__________|ALT-CLICK=mark block|SHIFT-CLICK=anchors" +
+
            gNcargoIdx = -1
−
         "|ALT-CTRL-CLICK=pivots|ALT-SHIFT-CLICK=dest atom|ALT-DRAG=move" +
+
            gCanchorIdx = -1
−
         "|SHIFT-ALT-DRAG=alt move|CLICK bond=freeze|DOUBLE-CLICK=exit"
+
            gNanchorIdx = -1
−
    echo @gEcho
+
        }
 +
        gTow = false
 +
         gMinNo = {(chain=gChain) and thisModel}.atomno.min
 +
        gMaxNo = {(chain=gChain) and thisModel}.atomno.max
 +
         if (gNanchorIdx < 0) {
 +
            gNanchorNo = gMinNo - 1
 +
        }
 +
        if (gCanchorIdx < 0) {
 +
            gCanchorNo = gMaxNo + 1
 +
        }
 +
        var aPidx = get_sc_bb_idx( _atomPicked, gChain)
  
−
    # Allow atoms to be dragged
+
        gSCidx = -1
−
    bind "ALT-LEFT-DOWN" "tugMarkMB";
+
        draw gSCcircle DELETE
−
    bind "ALT-LEFT-UP" "tugDragDoneMB";
 
−
    bind "ALT-SHIFT-LEFT-DOWN" "tugMarkMB";
 
−
    bind "ALT-SHIFT-LEFT-UP" "tugDragDoneMB";
 
−
    bind "ALT-LEFT-DRAG" "tugDragMB";
 
−
    bind "ALT-SHIFT-LEFT-DRAG" "tugDrag2MB";
 
  
−
    unbind "SHIFT-LEFT-CLICK"
+
        # If existing cWard cargo picked
−
    bind "SHIFT-LEFT-CLICK" "_pickAtom";
+
        if (gCcargoIdx == aPidx) {
−
    bind "SHIFT-LEFT-CLICK" "+:tugAnchorMB";
 
  
−
    bind "ALT-CTRL-LEFT-CLICK" "_pickAtom";
+
            # Clear the highlight
−
    bind "ALT-CTRL-LEFT-CLICK" "+:tugPivotMB";
+
            select {atomIndex=gCcargoIdx}
 +
            halo off
  
−
    bind "ALT-SHIFT-LEFT-CLICK" "_pickAtom";
+
            # If nWard cargo exists, mark it as the cWard cargo
−
    bind "ALT-SHIFT-LEFT-CLICK" "+:tugDestAtomMB";
+
            if (gNcargoIdx != gCcargoIdx) {
−
}
+
                gCcargoIdx = get_cp_idx(gNcargoIdx)
−
 
+
                gCcargoNo = {atomIndex=gCcargoIdx}.atomno
−
# Bound to SHIFT-LEFT-CLICK by tugCargoMB
 
−
function tugAnchorMB() {
 
−
    if ({atomIndex=_atomPicked}.chain == gChain) {
 
−
        var aPidx = getScBBidx( _atomPicked, gChain)
 
−
 
 
−
        var pno = {atomIndex=aPidx}.atomno
 
−
        if (pno > {atomIndex=gCcargoIdx}.atomno) {
 
−
            select {atomIndex=gCanchorIdx}
 
−
            halo off
 
−
            if (gCanchorIdx == aPidx) {
 
−
                gCanchorIdx = -1
 
−
                gCanchorNo = gMaxNo + 1
 
 
             }
 
             }
 
             else {
 
             else {
−
                 gCanchorIdx = aPidx
+
                 gCcargoIdx = -1
−
                 gCanchorNo = {atomIndex=gCanchorIdx}.atomno
+
                 gNcargoIdx = -1
−
                select {atomIndex=gCanchorIdx}
 
−
                halo on
 
 
             }
 
             }
−
            collectBBrotors(FALSE)
 
 
         }
 
         }
−
         else if (pno < {atomIndex=gNcargoIdx}.atomno) {
+
         else if (gNcargoIdx == aPidx) {
−
             select {atomIndex=gNanchorIdx}
+
             select {atomIndex=gNcargoIdx}
 
             halo off
 
             halo off
−
             if (gNanchorIdx == aPidx) {
+
             gNcargoIdx = get_nm_idx(gCcargoIdx)
−
                gNanchorIdx = -1
+
             gNcargoNo = {atomIndex=gNcargoIdx}.atomno
−
                gNanchorNo = gMinNo - 1
 
−
            }
 
−
             else {
 
−
                gNanchorIdx = aPidx
 
−
                gNanchorNo = {atomIndex=gNanchorIdx}.atomno
 
−
                select {atomIndex=gNanchorIdx}
 
−
                halo on
 
−
            }
 
−
            collectBBrotors(TRUE)
 
−
        }
 
−
        else {
 
−
            towCargoMB()
 
 
         }
 
         }
 +
        else if (gCcargoIdx >= 0) {
  
−
        # Get moving atoms set
+
            var no = {atomIndex=aPidx}.atomno
−
        gMovingSet = {((atomno < gCanchorNo) and (atomno > gNanchorNo)
 
−
            and (chain=gChain))}
 
−
    }
 
−
    select {gCargoSet}
 
−
}
 
  
−
# Bound to ALT-SHIFT-LEFT-CLICK by tugCargoMB
+
            # If pick is nWard of it
−
function tugDestAtomMB() {
+
            if (no < {atomIndex=gCcargoIdx}.atomno) {
−
    var aOk = TRUE
 
−
    if ({atomIndex=_atomPicked}.chain == gChain) {
 
  
−
        var pno = {atomIndex=_atomPicked}.atomno
+
                # If exists, clear its highlight
−
        if ((pno <= {atomIndex=gCcargoIdx}.atomno) and (pno >= {atomIndex=gNcargoIdx}.atomno)) {
+
                if (gNcargoIdx != gCcargoIdx) {
−
            aOk = FALSE
+
                    select {atomIndex=gNcargoIdx}
−
        }
+
                    halo off
−
    }
+
                }
−
    if (aOk) {
+
 
−
        select {atomIndex=gDestAtomIdx}
+
                # Set new nWard cargo and highlight it
−
        star off
+
                gNcargoIdx = get_nm_idx(aPidx)
−
        if (gDestAtomIdx == _atomPicked) {
+
                gNcargoNo = {atomIndex=gNcargoIdx}.atomno
−
             gDestAtomIdx = -1
+
             }
−
        }
+
 
−
        else {
+
             # Else cWard
−
            gDestAtomIdx = _atomPicked
+
             else {
−
             select {atomIndex=gDestAtomIdx}
 
−
             star on
 
−
        }
 
−
        select {gCargoSet}
 
−
    }
 
−
}
 
  
−
# Bound to CTRL-LEFT-CLICK by tugCargoMB
+
                # Clear its old highlight
−
function tugPivotMB() {
+
                select {atomIndex=gCcargoIdx}
−
    if (g1pivotIdx == _atomPicked) {
+
                if (gNcargoIdx != gCcargoIdx) {
−
        color {atomIndex=g1pivotIdx} @gScheme
+
                    halo off
−
        if (g2pivotIdx >= 0) {
+
                }
−
            g1pivotIdx = g2pivotIdx
+
 
−
            g2pivotIdx = -1
+
                # Set new cWard cargo and highlight
−
             g2dynamicIdx = -1
+
                gCcargoIdx = get_cp_idx(aPidx)
 +
                gCcargoNo = {atomIndex=gCcargoIdx}.atomno
 +
             }
 
         }
 
         }
 +
 +
        # Else no cWard cargo
 
         else {
 
         else {
−
             g1pivotIdx = -1
+
 
−
             g1dynamicIdx = -1
+
            # Set new cWard cargo and highlight
 +
             gCcargoIdx = get_cp_idx(aPidx)
 +
             gCcargoNo = {atomIndex=gCcargoIdx}.atomno
 +
            gNcargoIdx = get_nm_idx(gCcargoIdx)
 +
            gNcargoNo = {atomIndex=gNcargoIdx}.atomno
 
         }
 
         }
−
    }
+
 
−
    else if (g2pivotIdx == _atomPicked) {
+
        # If any anchor now inside cargo cluster, kill it
−
        color {atomIndex=g2pivotIdx} @gScheme
+
        if ({atomIndex=gCanchorIdx}.atomno <= {atomIndex=gCcargoIdx}.atomno) {
−
        g2pivotIdx = -1
+
            gCanchorIdx = -1
−
        g2dynamicIdx = -1
+
            gCanchorNo = gMaxNo + 1
−
    }
+
        }
−
    else if (g1pivotIdx >= 0) {
+
        if ({atomIndex=gNanchorIdx}.atomno >= {atomIndex=gNcargoIdx}.atomno) {
−
        if (g2pivotIdx >= 0) {
+
            gNanchorIdx = -1
−
             color {atomIndex=g2pivotIdx} @gScheme
+
             gNanchorNo = gMinNo - 1
 
         }
 
         }
  
−
         g2pivotIdx = _atomPicked
+
         # Highlight cargo cluster
−
         g2dynamicIdx = -1
+
        select_nward_idx(gCcargoIdx, gNcargoIdx)
−
         color {atomIndex=g2pivotIdx} green
+
        gCargoSet = {selected}
 +
         set_colors()
 +
 
 +
        # Collect the rotor sets
 +
        collect_rotors()
 +
 
 +
        # Get moving atoms set
 +
         gMovingSet = {((atomno < gCanchorNo) and (atomno > gNanchorNo)
 +
            and (chain=gChain) and thisModel)}
 +
    }
 +
 
 +
    # Enable dragging
 +
    if (gToab) {
 +
        to_ab_enable_drag()
 
     }
 
     }
 
     else {
 
     else {
−
         g1pivotIdx = _atomPicked
+
         tug_enable_drag()
−
        g1dynamicIdx = -1
 
−
        color {atomIndex=g1pivotIdx} green
 
 
     }
 
     }
 +
 
     select {gCargoSet}
 
     select {gCargoSet}
 
}
 
}
  
−
# Bound to SHIFT-LEFT-CLICK by plicoTug
+
function tug_undo_mb() {
−
function towCargoMB() {
+
     if (prompt("Undo", "Yes|No", true) == "Yes") {
−
     gTow = TRUE
+
        restore state gState
−
    gChain = {atomIndex=_atomPicked}.chain
+
     }
−
     gMinNo = {chain=gChain}.atomno.min
+
}
−
    gMaxNo = {chain=gChain}.atomno.max
+
 
−
    gCcargoIdx = -1
+
# Top level of Tug
−
    gNcargoIdx = -1
+
function plico_tug() {
−
    gCanchorIdx = -1
 
−
    gCanchorNo = gMaxNo + 1
 
−
    gNanchorIdx = -1
 
−
    gNanchorNo = gMinNo - 1
 
  
−
     # Highlight cargo cluster
+
     # Load common functions if not already
−
     select {chain=gChain}
+
     if (kCommon < 7) {
−
    gCargoSet = {selected}
+
        script $SCRIPT_PATH$plicoCommon.spt
−
     gMovingSet = {selected}
+
        if (kCommon < 7) {
−
    setColors()
+
            prompt ("A newer version of plicoCommon.SPT is required")
 +
            quit
 +
        }
 +
     }
  
−
     # Enable dragging
+
     gPlico = "TUG"
−
     tugEnableDrag()
+
     plico_prelim(true, true)
−
     select {gCargoSet}
+
      
−
     halo off
+
     gBondPicking = bondPicking
−
     var es = gEcho.replace("anchors","mark chain")
+
    set bondPicking true
−
     echo @es
+
    set PickCallback "jmolscript:tug_pick_cb"
−
     unbind "SHIFT-LEFT-CLICK"
+
 
 +
     gEcho = ("_________TUG_________|ALT-CLICK=mark block|SHIFT-CLICK=mark chain" +
 +
        "|CLICK bond=freeze|ALT-DOUBLE-CLICK=undo|SHIFT-DOUBLE-CLICK=exit")
 +
     echo @gEcho
 +
    gCrotors = array()
 +
     gNrotors = array()
 +
    clear_atom_idxs()
 +
    gToab = false
 +
 
 +
    bind "ALT-LEFT-CLICK" "_pickAtom";
 +
    bind "ALT-LEFT-CLICK" "+:tug_cargo_mb";
 
     bind "SHIFT-LEFT-CLICK" "_pickAtom";
 
     bind "SHIFT-LEFT-CLICK" "_pickAtom";
−
     bind "SHIFT-LEFT-CLICK" "+:towCargoMB";
+
     bind "SHIFT-LEFT-CLICK" "+:tow_cargo_mb";
 +
    bind "ALT-LEFT-DOUBLE" "tug_undo_mb";
 +
    bind "SHIFT-DOUBLE" "tug_exit(true)";
 +
    bind "LEFT-CLICK" "+:plico_menu_toggle";
 
}
 
}
  
−
# Bound to ALT-LEFT-CLICK by plicoTug or called by plicotoab.toabCargoMB
+
# Bound to DOUBLE by plico_tug
−
function tugCargoMB() {
+
function tug_exit() {
−
 
+
     if (plico_exit()) {
−
    # If O or movable side chain atom picked
+
         set bondPicking gBondPicking
−
     if (({atomIndex=_atomPicked}.atomName = "O")
+
        set PickCallback NONE
−
        or (isMovableSideChain( _atomPicked))) {
+
         select all
−
         if (gSCidx >= 0) {
+
         color bonds none
−
            draw gSCcircle DELETE
+
       
−
         }
+
         reset kTug
−
         if (gSCidx == _atomPicked) {
+
         reset gCanchorIdx 
−
            gSCidx = -1
+
        reset gCanchorNo 
−
         }
+
        reset gPlico     
−
         else {
+
        reset gNanchorIdx 
−
            gSCidx = _atomPicked
+
         reset gNanchorNo 
−
            gSCpt = {atomIndex=gSCidx}.xyz
+
        reset gCcargoIdx 
−
            draw gSCcircle CIRCLE {atomIndex=gSCidx} MESH NOFILL
+
         reset gNcargoIdx  
−
         }
+
        reset gCcargoNo   
−
    }
+
        reset gNcargoNo   
−
    else {
+
        reset gDestAtomIdx
−
         if ({atomIndex=_atomPicked}.chain != gChain) {
+
        reset g1pivotIdx 
−
            if (gTow) {
+
        reset g2pivotIdx 
−
                select {chain=gChain}
+
        reset gSelSaves   
−
                color {selected} @gScheme
+
         reset gCrotors   
−
            }
+
         reset gNrotors   
−
            gChain = {atomIndex=_atomPicked}.chain
+
         reset gOkCollide 
−
            select ({atomIndex=gCcargoIdx} or {atomIndex=gNcargoIdx}
+
         reset gChain      
−
                or {atomIndex=gCanchorIdx} or {atomIndex=gNanchorIdx})
+
         reset gMinNo      
−
            halo off
+
         reset gMaxNo      
−
            gCcargoIdx = -1
+
         reset gCargoSet   
−
            gNcargoIdx = -1
+
         reset gMovingSet 
−
            gCanchorIdx = -1
+
         reset gSCidx      
−
            gNanchorIdx = -1
+
         reset gSCcircle  
−
         }
+
        reset gSCpt       
−
         gTow = FALSE
+
         reset gTargetPt   
−
         gMinNo = {chain=gChain}.atomno.min
+
         reset gNewDrag   
−
         gMaxNo = {chain=gChain}.atomno.max
+
        reset gTow       
−
         if (gNanchorIdx < 0) {
+
        reset g1dynamicIdx
−
            gNanchorNo = gMinNo - 1
+
        reset g2dynamicIdx
−
        }
+
        reset gSCcheck   
−
         if (gCanchorIdx < 0) {
+
        reset gBondPicking
−
            gCanchorNo = gMaxNo + 1
+
        reset gFreeze     
−
         }
+
        reset gToab       
−
         var aPidx = getScBBidx( _atomPicked, gChain)
+
        reset function "get_cp_idx"
−
 
+
        reset function "get_cm_no"
−
         gSCidx = -1
+
        reset function "get_nm_idx"
−
         draw gSCcircle DELETE
+
        reset function "get_np_no"
−
 
+
        reset function "get_cb_idx"
−
         # If existing cWard cargo picked
+
        reset function "get_o_idx"
−
         if (gCcargoIdx == aPidx) {
+
        reset function "get_nward_bb_idx"
−
 
+
         reset function "get_cward_bb_idx"
−
            # Clear the highlight
+
         reset function "get_sc_set"
−
            select {atomIndex=gCcargoIdx}
+
        reset function "get_sc_bb_idx"
−
            halo off
+
        reset function "is_bb_idx"
−
 
+
        reset function "is_sc_idx"
−
            # If nWard cargo exists, mark it as the cWard cargo
+
        reset function "select_add_sc"
−
            if (gNcargoIdx != gCcargoIdx) {
+
         reset function "handle_collisions"
−
                gCcargoIdx = getCpIdx(gNcargoIdx)
+
         reset function "tug_track_idx"
−
                gCcargoNo = {atomIndex=gCcargoIdx}.atomno
+
        reset function "do_counter_rotate"
−
            }
+
        reset function "counter_rotate"
−
            else {
+
        reset function "counter_rotate_2"
−
                gCcargoIdx = -1
+
        reset function "repair_proline"
−
                gNcargoIdx = -1
+
        reset function "repair_sc"
−
            }
+
        reset function "tug_track_c"
−
         }
+
        reset function "tug_track_n"
−
         else if (gNcargoIdx == aPidx) {
+
        reset function "translate_selected_record"
−
            select {atomIndex=gNcargoIdx}
+
        reset function "rotate_selected_record"
−
            halo off
+
        reset function "collect_sc_rotors"
−
            gNcargoIdx = getNmIdx(gCcargoIdx)
+
        reset function "drag_sc"
−
            gNcargoNo = {atomIndex=gNcargoIdx}.atomno
+
        reset function "fix_sc_collision_2"
−
         }
+
        reset function "is_moveable_sc"
−
         else if (gCcargoIdx >= 0) {
+
        reset function "is_rotor_avail"
−
 
+
        reset function "xcollect_bb_rotors"
−
            var no = {atomIndex=aPidx}.atomno
+
        reset function "collect_bb_rotors"
−
 
+
        reset function "collect_rotors"
−
            # If pick is nWard of it
+
        reset function "tug_sc"
−
            if (no < {atomIndex=gCcargoIdx}.atomno) {
+
        reset function "set_colors"
−
 
+
        reset function "clear_atom_idxs"
−
                # If exists, clear its highlight
+
        reset function "timed_out"
−
                if (gNcargoIdx != gCcargoIdx) {
+
        reset function "record_drag"
−
                    select {atomIndex=gNcargoIdx}
+
        reset function "tug_pick_cb"
−
                    halo off
+
        reset function "tug_drag_done_mb"
−
                }
+
        reset function "tug_drag_2_mb"
−
 
+
        reset function "tug_drag_mb"
−
                # Set new nWard cargo and highlight it
+
         reset function "tug_mark_mb"
−
                gNcargoIdx = getNmIdx(aPidx)
+
         reset function "tug_anchor_mb"
−
                gNcargoNo = {atomIndex=gNcargoIdx}.atomno
+
         reset function "tug_enable_drag"
−
            }
+
         reset function "tug_dest_atom_mb"
−
 
+
         reset function "tug_pivot_mb"
−
            # Else cWard
+
         reset function "tow_cargo_mb"
−
            else {
+
         reset function "tug_cargo_mb"
−
 
+
         reset function "tug_undo_mb"
−
                # Clear its old highlight
+
         reset function "plico_tug"
−
                select {atomIndex=gCcargoIdx}
 
−
                if (gNcargoIdx != gCcargoIdx) {
 
−
                    halo off
 
−
                }
 
−
 
 
−
                # Set new cWard cargo and highlight
 
−
                gCcargoIdx = getCpIdx(aPidx)
 
−
                gCcargoNo = {atomIndex=gCcargoIdx}.atomno
 
−
            }
 
−
         }
 
−
 
 
−
         # Else no cWard cargo
 
−
         else {
 
−
 
 
−
            # Set new cWard cargo and highlight
 
−
            gCcargoIdx = getCpIdx(aPidx)
 
−
            gCcargoNo = {atomIndex=gCcargoIdx}.atomno
 
−
            gNcargoIdx = getNmIdx(gCcargoIdx)
 
−
            gNcargoNo = {atomIndex=gNcargoIdx}.atomno
 
−
 
 
−
            # Set default cWard anchor at cWard N
 
−
            var iNo = gMaxNo
 
−
            for (; iNo > 0; iNo--) {
 
−
                if ({(atomno=iNo) and (chain=gChain)}.atomName == "N") {
 
−
                    break;
 
−
                }
 
−
            }
 
−
            gCanchorIdx = {(atomno=iNo) and (chain=gChain)}.atomIndex
 
−
            gCanchorNo = {atomIndex=gCanchorIdx}.atomno
 
−
         }
 
−
 
 
−
         # If any anchor now inside cargo cluster, kill it
 
−
         if ({atomIndex=gCanchorIdx}.atomno <= {atomIndex=gCcargoIdx}.atomno) {
 
−
            gCanchorIdx = -1
 
−
            gCanchorNo = gMaxNo + 1
 
−
         }
 
−
         if ({atomIndex=gNanchorIdx}.atomno >= {atomIndex=gNcargoIdx}.atomno) {
 
−
            gNanchorIdx = -1
 
−
            gNanchorNo = gMinNo - 1
 
−
         }
 
  
−
        # Highlight cargo cluster
 
−
        selectNwardIdx(gCcargoIdx, gNcargoIdx)
 
−
        gCargoSet = {selected}
 
−
        setColors()
 
−
 
−
        # Collect the rotor sets
 
−
        collectRotors()
 
−
 
−
        # Get moving atoms set
 
−
        gMovingSet = {((atomno < gCanchorNo) and (atomno > gNanchorNo)
 
−
            and (chain=gChain))}
 
−
    }
 
  
−
    # Enable dragging
 
−
    if (gToab) {
 
−
        toabEnableDrag()
 
 
     }
 
     }
−
    else {
 
−
        tugEnableDrag()
 
−
    }
 
−
   
 
−
    select {gCargoSet}
 
−
}
 
−
 
−
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
 
−
}
 
−
 
−
# Top level of Tug
 
−
function plicoTug() {
 
−
    gPlico = "TUG"
 
−
    plicoPrelim()
 
−
    gBondPicking = bondPicking
 
−
    set bondPicking TRUE
 
−
    set PickCallback "jmolscript:tugPickCB"
 
−
           
 
−
    gEcho = ("_________TUG_________|ALT-CLICK=mark block|SHIFT-CLICK=mark chain" +
 
−
        "|CLICK bond=freeze|DOUBLE-CLICK=exit")
 
−
    echo @gEcho
 
−
    gCrotors = array()
 
−
    gNrotors = array()
 
−
    clearAtomIdxs()
 
−
 
−
    bind "ALT-LEFT-CLICK" "_pickAtom";
 
−
    bind "ALT-LEFT-CLICK" "+:tugCargoMB";
 
−
    bind "SHIFT-LEFT-CLICK" "_pickAtom";
 
−
    bind "SHIFT-LEFT-CLICK" "+:towCargoMB";
 
−
    bind "DOUBLE" "tugExit";
 
−
}
 
−
 
−
# Bound to DOUBLE by plicoTug
 
−
function tugExit() {
 
−
    set bondPicking gBondPicking
 
−
    set PickCallback NONE
 
−
    plicoExit()
 
 
}
 
}
  
−
function plicoExit() {
+
# End of TUG.SPT
−
    var p = ""
+
</pre>
−
    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
 
−
    }
 
−
}
 
−
# End of TUG.SPT</pre>
 

Latest revision as of 17:15, 12 April 2016

Tug allows the user to pull or push by mouse actions to move or rotate one part of a polypeptide against the rest by rotation on its psi and phi bonds with collision detection and restriction. It also allows the user to move an entire chain to nest against another chain. A full description of its capabilities can be found here.

Tug is a member of the Plico suite of protein folding tools described in User:Remig/plico . It may be installed and accessed as a macro with the file:

Title=PLICO Tug
Script=script <path to your scripts directory>/tug.spt;plico_tug

saved as plicotug.macro in your .jmol/macros directory as described in Macro.

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

#   tug - Jmol script by Ron Mignery
#   v1.20 beta    4/12/2016 -axis is now a reserved word
#
#   Translate or rotate a stretch of a polypeptide against itself
#    or against other chains by mouse actions
#
kTug = 3
gCanchorIdx = -1
gCanchorNo = -1
gPlico = "TUG"
gNanchorIdx = -1
gNanchorNo = -1
gCcargoIdx = -1
gNcargoIdx = -1
gCcargoNo = -1
gNcargoNo = -1
gDestAtomIdx = -1
g1pivotIdx = -1
g2pivotIdx = -1
gSelSaves = ({})
gCrotors = array()
gNrotors = array()
gOkCollide = ({})
gChain = ""
gMinNo = 1
gMaxNo = 9999
gCargoSet = ({})
gMovingSet = ({})
gBusy = false
gSCidx = -1
gSCcircle = -1
gSCpt = {0 0 0}
gTargetPt = {0 0 0}
gNewDrag = false
gTow = false
g1dynamicIdx = -1
g2dynamicIdx = -1
gSCcheck = true
gBondPicking = false
gFreeze = array()
gToab = false


function get_cp_idx (idx) {
    var no = {atomIndex=idx}.atomno
    while ((no < gMaxNo) and ({(atomno=no) and (chain=gChain)
        and thisModel}.atomName != "C")) {
        no++
    }
    return ({(atomno=no) and (chain=gChain) and thisModel}.atomIndex)
}

function get_cm_no (iNo) {
    while ((iNo > 0) and ({(atomno=iNo) and (chain=gChain)
        and thisModel}.atomName != "C")) {
        iNo--
    }
    return iNo
}

function get_nm_idx (idx) {
    var no = {atomIndex=idx}.atomno
    while ((no > 0) and ({(atomno=no) and (chain=gChain)
        and thisModel}.atomName != "N")) {
        no--
    }
    return ({(atomno=no) and (chain=gChain) and thisModel}.atomIndex)
}

function get_np_no (iNo) {
    while ((iNo < gMaxNo) and ({(atomno=iNo) and (chain=gChain)
        and thisModel}.atomName != "N")) {
        iNo++
    }
    return iNo
}

function get_cb_idx (BBidx) {
    var no = {atomIndex=BBidx}.atomno
    var i = 1
    for (; i < 5; i++) {
        if ({(atomno=@{no+i}) and (chain=gChain)
            and thisModel}.atomName == "CB") {
            break
        }
    }
    return {(atomno=@{no+i}) and (chain=gChain) and thisModel}.atomIndex
}

function get_o_idx (BBidx) {
    var no = {atomIndex=BBidx}.atomno
    var i = 1
    for (; i < 4; i++) {
        if ({(atomno=@{no+i}) and (chain=gChain)
            and thisModel}.atomName == "O") {
            break
        }
    }
    return {(atomno=@{no+i}) and (chain=gChain) and thisModel}.atomIndex
}

function get_nward_bb_idx (idx, iChain) {
    var no = {atomIndex=idx}.atomno - 1
    for (; no >= 0; no--) {
        var atomName = {(atomno=no) and (chain=iChain)
            and thisModel}.atomName
        if ((atomName = "N") or (atomName = "C") or (atomName = "CA")) {
            break
        }
    }
    return ((no >= 0) ? ({(atomno=no) and (chain=iChain)
        and thisModel}.atomIndex) : -1)
}

function get_cward_bb_idx (idx, iChain) {
    var no = {atomIndex=idx}.atomno + 1
    for (; no < gMaxNo; no++) {
        var atomName = {(atomno=no) and (chain=iChain)
            and thisModel}.atomName
        if ((atomName = "N") or (atomName = "C") or (atomName = "CA")) {
            break
        }
    }
    return ((no >= 0) ? ({(atomno=no) and (chain=iChain)
        and thisModel}.atomIndex) : -1)
}

function get_sc_set (scIdx, iChain) {
    var scSet = ({})
    var idx = get_sc_bb_idx(scIdx, iChain)
    var iNo = {atomIndex=idx}.atomno + 3

    for (var i = 1; i < 20; i++) {
        idx = {(atomno=@{iNo+i}) and (chain=iChain)
            and thisModel}.atomIndex
        if (is_bb_idx(idx)) {
            break
        }
        scSet = scSet or {atomIndex=idx}
    }
    return scSet
}

function get_sc_bb_idx (idx, iChain) {
    var no = {atomIndex=idx}.atomno
    for (; no > 0; no--) {
        if ({(atomno=no) and (chain=iChain)
            and thisModel}.atomName == "CA") {
            break
        }
        else if ({(atomno=no) and (chain=iChain)
            and thisModel}.atomName == "C") {
            break
        }
        else if ({(atomno=no) and (chain=iChain)
            and thisModel}.atomName == "N") {
            break
        }
        else if ({(atomno=no) and (chain=iChain)
            and thisModel}.atomName == "CB") {
            no -= 3
            break
        }
    }
    return {(atomno=no) and (chain=iChain) and thisModel}.atomIndex
}

function is_bb_idx(aIdx) {
    var ret = false
    switch({atomIndex=aIdx}.atomName) {
    case "N":
    case "CA":
    case "C":
        ret = true
        break
    }
    return ret
}

function is_sc_idx(aIdx) {

    var ret = false
    if (not is_bb_idx(aIDx)) {

        ret = true
        switch({atomIndex=aIdx}.atomName) {
        case "O":
        case "CB":
            ret = false
            break
        }
    }
    return ret
}

function select_add_sc(fromIdx) {
    var iNo = {atomIndex=fromIdx}.atomno
    var iChain = {atomIndex=fromIdx}.chain
    select none
    while ({(atomno=iNo) and (chain=iChain)
        and thisModel and sidechain}) {
        var a = {(atomno=iNo) and (chain=iChain)and thisModel}
        a.selected = true
        iNo++
    }
}

# Resolve collisions on selection
function handle_collisions( targetIdx) {

    # For all selected atoms
    for (var iNo = {selected}.min.atomno; iNo <= {selected}.max.atomno; iNo++) {
        var idx = {(atomno=iNo) and (chain=gchain)
            and thisModel}.atomIndex
        if ({atomindex=idx}.selected) {

            # Collect local colliders
            var lcAtoms = (within(kCtolerance, false, {atomIndex=idx})
                and not {atomIndex=idx}
                and not {gOkCollide}
                and not connected({atomIndex=idx}))
            if (lcAtoms) {
                # Ignore kinked BB
                try {
                    if (is_bb_idx(idx) and (angle(
                        {atomIndex=@{get_cward_bb_idx(idx, gChain)}}, {atomIndex=idx},
                        {atomIndex=@{get_nward_bb_idx(idx, gChain)}}) < 100.0)) {
                        continue
                    }
                }
                catch {
                }

                # For all local colliders
                for (var c = 1; c <= lcAtoms.size; c++ ) {
                    var cidx = lcAtoms[c].atomIndex

                    # If it is with water, delete it
                    if (lcAtoms[c].group = "HOH") {
                        delete {atomIndex=cidx}
                    }

                    # else if it is with side chain not proline, fix it
                    else if (is_sc_idx(cidx) and ({atomIndex=cidx}.group != "PRO")) {
                        fix_sc_collision_2(cidx)
                        
                        # If not fixed, exit fail
                        if (not gOk2) {
                            return # early exit (break n jmol bug)
                        }
                    }

                    # else if it is itself a side chain not proline, fix it
                    else if (is_sc_idx(idx) and ({atomIndex=idx}.group != "PRO")) {
                        fix_sc_collision_2(idx)

                        # If not fixed, exit fail
                        if (not gOk2) {
                            return # early exit (break n jmol bug)
                        }
                    }

                    # Else if it is with O, counter-rotate
                    else if (lcAtoms[c].atomName = "O") {
                        counter_rotate_2(lcAtoms[c].atomIndex,
                            {atomIndex=idx}.xyz, targetIdx, false)

                        # If not fixed, exit fail
                        if (not gOk2) {
                            return # early exit (break n jmol bug)
                        }
                    }

                    # Else if it is itself O, counter-rotate
                    else if ({atomIndex=idx}.atomName = "O") {
                        counter_rotate_2(idx, lcAtoms[c].xyz, targetIdx, false)

                        # If not fixed, exit fail
                        if (not gOk2) {
                            return # early exit (break n jmol bug)
                        }
                    }

                    else {    # Else not fixed, exit fail
                        gOk2 = false
                        return # early exit (break n jmol bug)
                    }
                } # endfor
            }
        }
    } # endfor iNo
}

# Rotate rotor set to move target atom to its proper place
function tug_track_idx(targetIdx, targetPt, nWard, cDetect) {
    gOK = false
    var pt = targetPt
    var dist = distance(pt, {atomIndex=targetIdx}.xyz)

    var rotors = (nWard ? gNrotors : gCrotors)

    # For a number of passes
    for (var pass1 = 0; pass1 < 20; pass1++) {
        var blocked = ({})
        for (var pass2 = 0; pass2 < (rotors.size/4); pass2++) {

            var v1 = {atomIndex=targetIdx}.xyz - pt

            # Find the most orthgonal unused rotor
            var imax = 0
            var smax = 0.5
            for (var i = 1; i < rotors.size; i += 4) {
                var i2 = rotors[i+1]
                var i3 = rotors[i+2]
                var i4 = rotors[i+3]
                if ((i2 != targetIdx) and (i3 != targetIdx) and (i4 != targetIdx)) {
                    if ({blocked and {atomIndex=i2}}.count == 0) {
                        var v2 = {atomIndex=i3}.xyz - {atomIndex=i2}.xyz

                        var s = sin(abs(angle(v1, {0 0 0}, v2)))
                        if (s > smax) {
                            smax = s
                            imax = i
                        }
                    }
                }
            }

            # If no more rotors, break to next full try
            if (imax == 0) {
               break
            }
            var i1 = rotors[imax+0]
            var i2 = rotors[imax+1]
            var i3 = rotors[imax+2]
            var i4 = rotors[imax+3]

            # Get dihedral of rotor with target point
            var dt = angle({atomIndex=targetIdx}, {atomIndex=i2}, {atomIndex=i3}, pt)
            var dh = angle({atomIndex=i1}, {atomIndex=i2}, {atomIndex=i3}, {atomIndex=i4})
            if (abs(dh).type == "string") {
                dh = -50
            }
            var psi = dh + dt
            var phi = dh + dt

            # Compute resultant psi and phi
            #  and select from target atom to first half of rotor
            var movePt = false
            if (nWard) {
                if ({atomIndex=i2}.atomName="CA") {
                    psi = angle({atomIndex=@{get_cward_bb_idx(i1, gChain)}}, {atomIndex=i1},
                        {atomIndex=i2}, {atomIndex=i3}) + dt
                }
                else {
                    phi = angle({atomIndex=i1}, {atomIndex=i2},
                        {atomIndex=i3}, {atomIndex=@{get_nward_bb_idx(i3, gChain)}}) + dt
                }

                if ({atomIndex=i2}.atomno > {atomIndex=targetIdx}.atomno) {
                    movePt = true
                    select_nward_idx(i3, get_cward_bb_idx(targetIdx, gChain))
                    {atomIndex=targetIdx}.selected = true
                }
                else {
                    select_cward_idx(i2, targetIdx)
                }
            }
            else {
                if (({atomIndex=i2}.atomName="CA")) {
                    phi = angle({atomIndex=@{get_nward_bb_idx(i1, gChain)}}, {atomIndex=i1},
                        {atomIndex=i2}, {atomIndex=i3}) + dt
                }
                else {
                    psi = angle({atomIndex=i2}, {atomIndex=i3},
                        {atomIndex=i4}, {atomIndex=@{get_cward_bb_idx(i4, gChain)}}) + dt
                }

                if ({atomIndex=i2}.atomno < {atomIndex=targetIdx}.atomno) {
                    movePt = true
                    select_cward_idx(i3, get_nward_bb_idx(targetIdx, gChain))
                    {atomIndex=targetIdx}.selected = true
                }
                else {
                    select_nward_idx(i2, targetIdx)
                }
            }

            # Relax rules if desperate
            if (pass1 > 10) {
                phi = -50
            }

            # If rotation within ramachandran limits
            if ((abs(dt) >= 0.1) and
                (({atomIndex=i2}.group=="GLY") or (phi < 0))) {

                # If moving target point, put the target atom there
                var cp = {atomIndex=targetIdx}.xyz
                if (movePt) {
                    dt = -dt
                    {atomIndex=targetIdx}.xyz = pt
                }

                # Rotate to minimize vector ====================
                rotateSelected {atomIndex=i2} {atomIndex=i3} @dt

                # If collision checking
                if (cDetect) {

                    # If collision, back off by eighths
                    var wasCollision = false
                    for (var ci = 0; ci < 4; ci++) {
                        if (ci < 3) {
                            dt /= 2
                        }
                        handle_collisions( targetIdx)
                        if (not gOk2) {
                            wasCollision = true
                            rotateSelected {atomIndex=i2} {atomIndex=i3} @{-dt}
                        }
                        else if (wasCollision) {
                            if (ci <3) {
                                rotateSelected {atomIndex=i2} {atomIndex=i3} @{dt}
                            }
                        }
                        else {
                            break
                        }

                        if (dt < 0.01) {
                            break
                        }
                    } # endfor
                }

                # If moving target point, put the target atom back
                if (movePt) {
                    pt = {atomIndex=targetIdx}.xyz
                    {atomIndex=targetIdx}.xyz = cp
                }

            }

            # If close enough, stop
            if (distance(pt, {atomIndex=targetIdx}) < kDtolerance) {
                gOK = true
                gTargetPt = pt
                break
            }

            # Block rotor
            blocked |= {atomIndex=i2}

        }   # endfor num rotors passes

        if (gOK) {
            break
        }
    }   # endfor 20 passes
}

# Counter rotate bonds on either side of a BB O
function do_counter_rotate(caPhiIdx, nIdx, cIdx, oIdx, caPsiIdx, dir, nWard) {

    # Rotate psi
    {atomIndex=nIdx}.selected = nWard
    {atomIndex=cIdx}.selected = nWard
    {atomIndex=oIdx}.selected = nward
    rotateSelected {atomIndex=caPsiIdx} {atomIndex=cIdx} @{dir}

    # Counter-rotate phi
    {atomIndex=nIdx}.selected = not nWard
    {atomIndex=cIdx}.selected = not nWard
    {atomIndex=oIdx}.selected = not nward
    rotateSelected {atomIndex=nIdx} {atomIndex=caPhiIdx} @{-dir}
}

function counter_rotate(oIdx, dir, nWard) {

    var iChain = {atomIndex=oIdx}.chain
    var selsave = {selected}
    var cIdx = get_sc_bb_idx(oIdx, iChain)
    var nIdx = get_cward_bb_idx(cIdx, iChain)
    var caPhiIdx = get_cward_bb_idx(nIdx, iChain)
    var caPsiIdx = get_nward_bb_idx(cIdx, iChain)

    if (nWard) {
        var nNo = {(chain=iChain) and thisModel}.atomno.min
        select_nward_idx(caPsiIdx, {(atomno=nNo) and (chain=iChain)
            and thisModel}.atomIndex)
    }
    else {
        var cNo = {(chain=iChain) and thisModel}.atomno.max
        select_cward_idx(caPhiIdx, {(atomno=cNo) and (chain=iChain)
            and thisModel}.atomIndex)
    }

    # Counter-rotate
    do_counter_rotate(caPhiIdx, nIdx, cIdx, oIdx, caPsiIdx, dir, not nWard)
    select selsave
}

function counter_rotate_2(oIdx, toPt, terminalIdx, oDrag) {

    var iChain = {atomIndex=oIdx}.chain
    var selsave = {selected}
    var gOk2 = true
    var cIdx = get_sc_bb_idx(oIdx, iChain)
    var nIdx = get_cward_bb_idx(cIdx, iChain)
    var caPhiIdx = get_cward_bb_idx(nIdx, iChain)
    var caPsiIdx = get_nward_bb_idx(cIdx, iChain)

    var nTward = ({atomIndex=oIdx}.atomno < {atomIndex=terminalIdx}.atomno)
    if (nTward) {
        select_cward_idx(cIdx, terminalIdx)
    }
    else {
        select_nward_idx(nIdx, terminalIdx)
    }

    # Until all collisions cancelled
    var dir = 5
    var ang = angle(toPt, {atomIndex=oIdx}, {atomIndex=cIdx})
    var tcount = 0
    while (oDrag or (within(kCtolerance, false, {atomIndex=oIdx})
            and not {atomIndex=oIdx} and not connected({atomIndex=oIdx})
            and not {gOkCollide} > 0)) {

        # Counter-rotate
        do_counter_rotate(caPhiIdx, nIdx, cIdx, oIdx, caPsiIdx, dir, nTward)
        var newang = angle(toPt, {atomIndex=oIdx}, {atomIndex=cIdx})

        # If wrong direction once, undo and reverse
        if (newang > ang) {
            do_counter_rotate(caPhiIDx, nIdx, cIdx, oIdx, caPsiIdx, -dir, nTward)

            # If first time, continue in opposite direction
            dir *= -1
            if (dir < 0) {
                continue
            }
        }

        if (oDrag) {
            break
        }

        # If no go, undo and exit
        tcount++
        if (tcount > (360/abs(dir))) {
            gOk2 = false
            break
        }

    } # endwhile
    select selsave
}

# Repair proline
function repair_proline(BBidx) {
    var cbidx = get_cb_idx(BBidx)
    var cbno = {atomIndex=cbidx}.atomno
    var cgidx = {(atomno=@{cbno+1}) and (chain=gChain)
        and thisModel}.atomIndex
    var cdidx = {(atomno=@{cbno+2}) and (chain=gChain)
        and thisModel}.atomIndex
    var caidx = {(atomno=@{cbno-3}) and (chain=gChain)
        and thisModel}.atomIndex
    var nidx = {(atomno=@{cbno-4}) and (chain=gChain)
        and thisModel}.atomIndex

    select {atomIndex=cbidx}
    set_angle_idx(nidx, caidx, cbidx, 109.5)

    select {atomIndex=cdidx}
    set_distance_idx(nidx, cdidx, 1.47)
    set_angle_idx(caidx, nidx, cdidx, 102.7)
    set_dihedral_idx(cbidx, caidx, nidx, cdidx, 16.2)

    select {atomIndex=cgidx}
    set_distance_idx(cdidx, cgidx, 1.51)
    set_angle_idx(nidx, cdidx, cgidx, 106.4)
    set_dihedral_idx(caidx, nidx, cdidx, cgidx, 16.2)
}

# Repair side chain
function repair_sc(targetIdx, nWard) {

    var idx = (nWard ? get_cward_bb_idx(targetIdx, gChain) : get_nward_bb_idx(targetIdx, gChain))

    if (({atomIndex=targetIdx}.atomName == "CA")
        and ({atomIndex=targetIdx}.group != "GLY")) {
        var cbidx = get_cb_idx(targetIdx)
        select none
        select_add_sc(cbidx)
        set_angle_idx(idx, targetIdx, cbidx, 110.0)
        set_distance_idx(targetIdx, cbidx, 1.5)
        if ({atomIndex=targetIdx}.group != "PRO") {
            var colliders = (within(kCtolerance, false, {selected})
                and not {atomIndex=targetIdx} and not {selected})
            if (colliders) {
                if ({atomIndex=targetIdx}.group != "ALA") {
                    fix_sc_collision_2(cbidx)
                }
            }
        }
        else {
            if (nWard) {
            }
            else {
                set_dihedral_idx(get_nward_bb_idx(idx, gChain), idx, targetIdx, cbidx, 174.2)
            }
        }
    }

    else if ({atomIndex=targetIdx}.atomName == "C") {
        var oidx = get_o_idx(targetIdx)
        select {atomIndex=oidx}
        set_angle_idx(idx, targetIdx, oidx, 120.0)
        set_distance_idx(targetIdx, oidx, 1.21)
        if (nWard) {
            set_dihedral_idx(get_cward_bb_idx(idx, gChain), idx, targetIdx, oidx, 0.0)
        }
        if ({atomIndex=idx}.group == "PRO") {
            repair_proline(idx)
            var dNo = {atomIndex=targetIdx}.atomno + 4
            var dIdx = {(atomno=dNO) and (chain=gChain)
                and thisModel}.atomIndex
            var colliders = (within(kCtolerance, false, {atomIndex=dIdx})
                and not connected({atomIndex=dIdx})
                and not {atomIndex=dIdx})
            for (var i = 1; i <= colliders.size; i++) {
                if (colliders[i].atomName == "O") {
                    counter_rotate_2(colliders[i].atomIndex,
                        {atomIndex=dIdx}.xyz, targetIdx, false)
                }
            }
        }
    }
}

# Rebuild Cward rotors set
function tug_track_c() {

    # For all bb atoms cWard of cargo
    var targetIdx = gCcargoIdx
    var okCount = 0

    # Allow collisions with cargo
    gOkCollide = gCargoSet
    var tcount = 0
    while (targetIdx != gNanchorIdx) {

        # Step to next atom
        targetIdx = get_cward_bb_idx(targetIdx, gChain)
        if (targetIdx < 0) {
            break
        }

        # No collision with cargo allowed after two atoms placed
        if (tcount == 2) {
           gOkCollide = ({})
        }
        tcount++

        # Compute targets desired coords
        try {
            var c1idx = get_cward_bb_idx(targetIdx, gChain)
            var n1idx = get_nward_bb_idx(targetIdx, gChain )
            var n2idx = get_nward_bb_idx(n1Idx, gChain)
            var n3idx = get_nward_bb_idx(n2Idx, gChain)
            var pt = {0 0 0}
            if ({atomIndex=targetIdx}.atomName == "N") {
                var oidx = get_o_idx(n1idx)
                select {atomIndex=oidx}
    
                # Desired target location is trigonal O
                set_distance_idx(n1idx, oidx, 1.5)
                pt = get_trigonal_idx(n2idx, n1idx, oidx, 1.37)
                set_distance_idx(n1idx, oidx, 1.21)
            }
            else if (({atomIndex=targetIdx}.atomName == "C")
                and ({atomIndex=targetIdx}.group != "GLY")) {
    
                # Desired target location is tetragonal CB
                var cbidx = get_cb_idx(n1idx)
                pt = get_tet_idx(n2idx, n1idx, cbidx, 1.5)
            }
            else { # CA (or GLY C)
    
                # Save current target coords
                var cp = {atomIndex=targetIdx}.xyz
    
                # Set target atom at desired distance and angle
                select {atomIndex=targetIdx}
                set_distance_idx(n1idx, targetIdx, 1.5)
                set_angle_idx(n2idx, n1idx, targetIdx, 120.0)
                if ({atomIndex=targetIdx}.atomName == "CA") {
                    set_dihedral_idx(n3idx, n2idx, n1idx, targetIdx, 180)
                }
    
                # Record and restore target
                pt = {atomIndex=targetIdx}.xyz
                {atomIndex=targetIdx}.xyz = cp
            }
    
            # If target not at desired location
            if (distance(pt, {atomIndex=targetIdx}) > kDtolerance) {
                okCount = 0
                gTargetPt = pt
                var xcount = 0
                gOK = false
                while ((xcount < 20) and (not gOK)) {
    
                    # Rotate on cWard rotor set to move it there
                    tug_track_idx(targetIdx, pt, false, false)
                    xcount++
                }
            }
            else {
                gOK = true
                okCount++
            }
        }
        catch {
        }

        # If successful
        if (gOK == true) {

            # Adust any side atoms
            repair_sc(targetIdx, false)
        }

        # Else fail
        else {
            break
        }

        # If no movement in 4 tries, we are done
        if (okCount > 3) {
            break
        }
    } # endwhile (targetIdx != gCanchorIdx) {
}

# Rebuild Nward rotors set
function tug_track_n() {

    gOK = true

    # For all bb atoms nWard of cargo
    var targetIdx = gNcargoIdx
    var okCount = 0

    # Allow collisions with cargo
    gOkCollide = gCargoSet
    var tcount = 0
    while (targetIdx != gNanchorIdx) {

        # Step to next atom
        targetIdx = get_nward_bb_idx(targetIdx, gChain)
        if (targetIdx < 0) {
            break
        }
        
        # No collision with cargo allowed after two atoms placed
        if (tcount == 2) {
           gOkCollide = ({})
        }
        tcount++

        # Compute targets desired coords
        var n1idx = get_nward_bb_idx(targetIdx, gChain)
        var c1idx = get_cward_bb_idx(targetIdx, gChain)
        var c2idx = get_cward_bb_idx(c1idx, gChain)
        var c3idx = get_cward_bb_idx(c2idx, gChain)
        var pt = {0 0 0}
        if ({atomIndex=targetIdx}.atomName == "CA") {

            # Desired target location is trigonal O
            var oidx = get_o_idx(c1idx)
            select {atomIndex=oidx}
            set_distance_idx(c1idx, oidx, 1.39)
            pt = get_trigonal_idx(c2idx, c1idx, oidx, 1.41)
            set_distance_idx(c1idx, oidx, 1.21)
        }
        else if (({atomIndex=targetIdx}.atomName == "N")
            and ({atomIndex=targetIdx}.group != "GLY")) {

            # Desired target location is r-tetragonal CB
            var cbidx = get_cb_idx(c1idx)
            pt = get_tet_idx(cbidx, c1idx, c2idx, 1.5)
        }
        else { # C

            # Save current target coords
            var cp = {atomIndex=targetIdx}.xyz

            # Set target atom at desired distance and angle
            select {atomIndex=targetIdx}
            set_distance_idx(c1idx, targetIdx, 1.37)
            set_angle_idx(c2idx, c1idx, targetIdx, 110.0)

            if ({atomIndex=targetIdx}.group == "PRO") {
                set_dihedral_idx(c3idx, c2idx, c1idx, targetIdx, -57.0)
            }

            # Record and restore target
            pt = {atomIndex=targetIdx}.xyz
            {atomIndex=targetIdx}.xyz = cp
        }

        # If target not at desired location
        if (distance(pt, {atomIndex=targetIdx}) > kDtolerance) {
            var okCount = 0
            gTargetPt = pt
            var xcount = 0
            gOK = false
            while ((xcount < 20) and (not gOK)) {

                # Rotate on nWard rotor set to move it there
                tug_track_idx(targetIdx, pt, true, false)
                xcount++
            }
        }
        else {
            gOK = true
            okCount++
        }

        # If sucessful
        if (gOK == true) {

            # Adust any side atoms
            repair_sc(targetIdx, true)
        }

        # Else fail
        else {
            break
        }

        # If no movement in 4 tries, we are done
        if (okCount > 3) {
            break
        }

    }   # endwhile (targetIdx != gNanchorIdx) {
}

# gPlicoRecord is maintained by the macro pilcoRecord
function translate_selected_record(pt) {
    if (gPlicoRecord != "") {
        plico_record(format("select %s;translateSelected %s;", {selected}, pt))
    }
    translateSelected @pt
}

# gPlicoRecord is maintained by the macro pilcoRecord
function rotate_selected_record(pivotIdx, caxis, a) {
    if (gPlicoRecord != "") {
        plico_record(format("select %s;", {selected}))
        plico_record(format("rotateSelected {atomIndex=%d} @%s @%s;",
            pivotIdx, caxis, a))
    }
    rotateSelected {atomIndex=pivotIdx} @caxis @a
}

function collect_sc_rotors(no, iChain) {
    var scBondIdxs = array()
    for (var iNo = no; iNo >= 0; iNo--) {
        var ile = 0
        switch ({(atomno=iNo) and (chain=iChain)
            and thisModel}.atomName) {
        case "CA" :
            return scBondIdxs # Early exit since break 1 appears broken
        case "CZ" :
            if ({(atomno=iNo) and (chain=iChain)
                and thisModel}.group == "TYR") {
                break
            }
        case "CE" :
            if ({(atomno=iNo) and (chain=iChain)
                and thisModel}.group == "MET") {
                break
            }
        case "CG1" :
            if ({(atomno=iNo) and (chain=iChain)
                and thisModel}.group == "VAL") {
                break
            }
            if ({(atomno=iNo) and (chain=iChain)
                and thisModel}.group == "ILE") {
                ile = 1
            }
        case "NE" :
        case "CD" :
        case "SD" :
        case "CG" :
        case "CB" :
            scBondIdxs += {(atomno=@{iNo+1+ile}) and (chain=iChain)
                and thisModel}.atomIndex
            scBondIdxs += {(atomno=@{iNo+0}) and (chain=iChain)
                and thisModel}.atomIndex
            if ({(atomno=iNo) and (chain=iChain)
                and thisModel}.atomName%2 == "CG") {
                scBondIdxs += {(atomno=@{iNo-1}) and (chain=iChain)
                    and thisModel}.atomIndex
                scBondIdxs += {(atomno=@{iNo-4}) and (chain=iChain)
                    and thisModel}.atomIndex
            }
            else if ({(atomno=iNo) and (chain=iChain)
                and thisModel}.atomName == "CB") {
                scBondIdxs += {(atomno=@{iNo-3}) and (chain=iChain)
                    and thisModel}.atomIndex
                scBondIdxs += {(atomno=@{iNo-4}) and (chain=iChain)
                    and thisModel}.atomIndex
            }
            else {
                scBondIdxs += {(atomno=@{iNo-1}) and (chain=iChain)
                    and thisModel}.atomIndex
                scBondIdxs += {(atomno=@{iNo-2}) and (chain=iChain)
                    and thisModel}.atomIndex
            }
            break
        }

    }

    return scBondIdxs
}

# Drag Side Chain
function drag_sc() {
    var iNo = {atomIndex=gSCidx}.atomno
    var iChain  = {atomIndex=gSCidx}.chain
    if ({atomIndex=gSCidx}.group != "PRO") {

        var scBondIdxs = collect_sc_rotors( iNo, iChain)
        var numChi = scBondIdxs.size / 4
        var dist = distance({atomIndex=gSCidx}.xyz, gSCpt)
        var scSet = get_sc_set(gSCidx, iChain)

        # For all rotor combinations
        var dh = array()
        for (var i = 0; i < numChi; i++) {
            dh += angle({atomIndex=@{scBondIdxs[4+(4*i)]}},
                {atomIndex=@{scBondIdxs[3+(4*i)]}},
                {atomIndex=@{scBondIdxs[2+(4*i)]}},
                {atomIndex=@{scBondIdxs[1+(4*i)]}})
        }
        for (var i = 0; i < numChi; i++) {
            var rot = -120
            for (var j = 0; j < 6; j++) {
                rot += 60*j
                select_add_sc(scBondIdxs[1+(4*i)])
                set_dihedral_idx(scBondIdxs[4+(4*i)], scBondIdxs[3+(4*i)],
                    scBondIdxs[2+(4*i)], scBondIdxs[1+(4*i)], rot)
                var newDist = distance({atomIndex=gSCidx}.xyz, gSCpt)
                if (gSCcheck) {
                    var colliders = (within(kCtolerance, false, scSet)
                        and not connected(scSet) and not {scSet})
                    if (colliders) {
                        continue
                    }
                }

                # Find the best
                if (newDist < dist) {
                    dist = newDist
                    for (var k = 0; k < numChi; k++) {
                        dh[k+1] = angle({atomIndex=@{scBondIdxs[4+(4*k)]}},
                        {atomIndex=@{scBondIdxs[3+(4*k)]}},
                        {atomIndex=@{scBondIdxs[2+(4*k)]}},
                        {atomIndex=@{scBondIdxs[1+(4*k)]}})
                    }
                }
            }
        }

        # Now set the best
        for (var i = 0; i < numChi; i++) {
            select_add_sc(scBondIdxs[1+(4*i)])
            set_dihedral_idx(scBondIdxs[4+(4*i)], scBondIdxs[3+(4*i)],
                scBondIdxs[2+(4*i)], scBondIdxs[1+(4*i)], dh[i+1])
        }
        
        if (gSCcheck = false) {
            plico_minimize( scSet)
        }
    }
    else { # PRO - toggle between puckers up and down
        var icd = {(atomno=@{iNo+1}) and (chain=iChain)
            and thisModel}.atomIndex
        var icb = {(atomno=@{iNo-1}) and (chain=iChain)
            and thisModel}.atomIndex
        var ica = {(atomno=@{iNo-4}) and (chain=iChain)
            and thisModel}.atomIndex
        var in = {(atomno=@{iNo-5}) and (chain=iChain)
            and thisModel}.atomIndex
        select {atomIndex=gSCidx}

        if (angle({atomIndex=ica}, {atomIndex=in},
            {atomIndex=icd}, {atomIndex=gSCidx}) < -10.0) {
            set_dihedral_idx(ica, in, icd, gSCidx, 8.7)
            set_angle_idx(in, icd, gSCidx, 110.0)
            set_distance_idx(icd, gSCidx, 1.5)
        }
        else {
            set_dihedral_idx(ica, in, icd, gSCidx, -29.5)
            set_angle_idx(in, icd, gSCidx, 108.8)
            set_distance_idx(icd, gSCidx, 1.5)
        }
    }

    draw gSCcircle CIRCLE {atomIndex=gSCidx} MESH NOFILL
    gSCpt = {atomIndex=gSCidx}.xyz
}

# Fix side chain collisions
function fix_sc_collision_2(idx) {
    gOk2 = false
    var iNo = {atomIndex=idx}.atomno
    var iChain = {atomIndex=idx}.chain
    var resno = {(atomno=iNo) and (chain=iChain)
        and thisModel}.resno

    # Get SC terminus
    while (resno == {(atomno=iNo) and (chain=iChain)
        and thisModel}.resno) {
        iNo++
    }
    iNo--

    var sc = array()
    var iBno = iNo
    while ({(atomno=iBno) and (chain=iChain)
        and thisModel}.atomName != "CB") {
        sc += {(atomno=iBno) and (chain=iChain)
            and thisModel}
        iBno--
    }
    var cbidx = {(atomno=iBno) and (chain=iChain)
        and thisModel}.atomIndex

    var scBondIdxs = collect_sc_rotors( iNo, iChain)
    var numChi = scBondIdxs.size / 4

    # For all rotor combinations
    for (var i = 0; i < numChi; i++) {
        var rot = -120
        for (var j = 0; j < 6; j++) {
            rot += 60
            select_add_sc(scBondIdxs[1+(4*i)])
            set_dihedral_idx(scBondIdxs[1+(4*i)], scBondIdxs[2+(4*i)],
                scBondIdxs[3+(4*i)], scBondIdxs[4+(4*i)], rot)

            # If no collision, exit
            var colliders = (within(kCtolerance, false, {sc})
                and not {atomIndex=cbidx} and not {sc})

            # If it is with water, delete the water
            for (var c = 1; c < colliders.size; c++ ) {
                if (colliders[c].group = "HOH") {
                    delete {atomIndex=@{colliders[c].atomIndex}}
                    colliders = {colliders and not @{colliders[c]}}
                }
            }

            if (colliders.size == 0) {
                gOk2 = true
                return # Early exit since break 1 appears broken
            }

        }
    }
}

function is_moveable_sc(aIdx) {

    var ret = (({atomIndex=aIdx}.group != "PRO")
        or ({atomIndex=aIdx}.atomName == "CG"))
    switch({atomIndex=aIdx}.atomName) {
    case "N":
    case "CA":
    case "C":
    case "CB":
    case "O":
    case "O4\'":
        ret = false
        break
    }
    return ret
}

function is_rotor_avail(i1idx, i2idx) {
    var ret = true
    if (i1idx > i2idx) { # frozen bonds are kept as lo-hi
        var idx = @i1idx
        i1idx = @i2idx
        i2idx = @idx
    }

    for (var i = 1; i <= gFreeze.size; i += 2) {
        if ((gFreeze[i] == i1idx) and (gFreeze[i+1] == i2idx)) {
            ret = false
            break
        }
    }

    return ret
}

function xcollect_bb_rotors(nWard) {
    var anchorNo = (nWard
        ? ((gNanchorIdx >= 0) ? {atomIndex=gNanchorIdx}.atomno : gMinNo)
        : ((gCanchorIdx >= 0) ? {atomIndex=gCanchorIdx}.atomno : gMaxNo))
    var cargoNo = (nWard
        ? ((gNcargoIdx >= 0) ? {atomIndex=gNcargoIdx}.atomno
        : {atomIndex=gCcargoIdx}.atomno)
        : {atomIndex=gCcargoIdx}.atomno)
    var rotors = array()
    if (cargoNo < anchorNo) {

        for (var iNo = cargoNo; iNo <= anchorNo; iNo++) {
            if ({(atomno=iNo) and (chain=gChain)
                and thisModel}.atomName == "CA") {
                if (is_rotor_avail(iNo)) {# xxx
                    if (({(atomno=iNo) and (chain=gChain)
                        and thisModel}.group != "PRO") and (iNo > cargoNo)) { # phi
                        rotors += [{(atomno=@{get_cm_no(iNo-1)}) and (chain=gChain)
                            and thisModel}.atomIndex,
                            {(atomno=@{iNo-1}) and (chain=gChain)
                                and thisModel}.atomIndex]
                        rotors += [{(atomno=@{iNo}) and (chain=gChain)
                            and thisModel}.atomIndex,
                            {(atomno=@{iNo+1}) and (chain=gChain)
                                and thisModel}.atomIndex]
                    }
                    if (iNo != (anchorNo-1)) { # psi
                        rotors += [{(atomno=@{iNo-1}) and (chain=gChain)
                            and thisModel}.atomIndex,
                            {(atomno=@{iNo}) and (chain=gChain)
                                and thisModel}.atomIndex]
                        rotors += [{(atomno=@{iNo+1}) and (chain=gChain)
                            and thisModel}.atomIndex,
                            {(atomno=@{get_np_no(iNo+2)}) and (chain=gChain)
                                and thisModel}.atomIndex]
                    }
                }
            }
        }
    }
    else {

        for (var iNo = cargoNo; iNo >= anchorNo; iNo--) {
            if ({(atomno=iNo) and (chain=gChain)
                and thisModel}.atomName == "CA") {
                if (is_rotor_avail(iNo)) {
                    if ((iNo != (anchorNo-1)) and (iNo < cargoNo)) { # psi
                        rotors += [{(atomno=@{get_np_no(iNo+2)}) and (chain=gChain)
                            and thisModel}.atomIndex,
                            {(atomno=@{iNo+1}) and (chain=gChain)
                                and thisModel}.atomIndex]
                        rotors += [{(atomno=@{iNo}) and (chain=gChain)
                            and thisModel}.atomIndex,
                            {(atomno=@{iNo-1}) and (chain=gChain)
                                and thisModel}.atomIndex]
                    }
                    if ({(atomno=iNo) and (chain=gChain)
                        and thisModel}.group != "PRO") { # phi
                        rotors += [{(atomno=@{iNo+1}) and (chain=gChain)
                            and thisModel}.atomIndex,
                            {(atomno=@{iNo}) and (chain=gChain)
                                and thisModel}.atomIndex]
                        rotors += [{(atomno=@{iNo-1}) and (chain=gChain)
                            and thisModel}.atomIndex,
                            {(atomno=@{get_cm_no(iNo-1)}) and (chain=gChain)
                                and thisModel}.atomIndex]
                    }
                }
            }
        }
    }

    if (nWard) {
        gNrotors = rotors
    }
    else {
        gCrotors = rotors
    }
}
function collect_bb_rotors(nWard) {
    if (nWard) {
        gNrotors = array()
        var nres = {atomIndex=gNcargoIdx}.resno
        var nname = {atomIndex=gNcargoIdx}.atomName
        var ares = get_resno_min(gChain)
        var aA = get_atom_rcn( ares, gChain, "N")
        if (gNanchorIdx >= 0) {
            aA = {atomIndex=gNanchorIdx}
        }
        var aname = aA.atomName
        for (var r = nres; r >= ares; r--) {
            var aCp = get_atom_rcn( r-1, gChain, "C")
            var aN = get_atom_rcn( r, gChain, "N")
            var aCa = get_atom_rcn( r, gChain, "CA")
            var aC = get_atom_rcn( r, gChain, "C")
            var aNn = get_atom_rcn( r+1, gChain, "N")
            if (aCp.size < 1) {
                aCp = aNn
            }
            # psi
            if (((r < nres) and (r > ares))
                or ((r == nres) and (nname == "C"))
                or ((r == ares) and (aname != "C"))) {
                if (is_rotor_avail(aCa.atomIndex, aC.atomIndex)) {
                    gNrotors += aNn.atomIndex
                    gNrotors += aC.atomIndex
                    gNrotors += aCa.atomIndex
                    gNrotors += aN.atomIndex
                }
            }
                        
            # phi
            if (aCa.group != "PRO") {
                if (((r <= nres) and (r > ares))
                    or ((r == ares) and (aname == "N"))) {
                    if (is_rotor_avail(aCa.atomIndex, aN.atomIndex)) {
                        gNrotors += aC.atomIndex
                        gNrotors += aCa.atomIndex
                        gNrotors += aN.atomIndex
                        gNrotors += aCp.atomIndex
                    }
                }
            }
        } # endfor
    }
    else {
        gCrotors = array()
        var cres = {atomIndex=gCcargoIdx}.resno
        var cname = {atomIndex=gCcargoIdx}.atomName
        var ares = get_resno_max(gChain)
        var aA = get_atom_rcn( ares, gChain, "C")
        if (gCanchorIdx >= 0) {
            aA = {atomIndex=gCanchorIdx}
        }
        var aname = aA.atomName
        for (var r = cres; r <= ares; r++) {
            var aCp = get_atom_rcn( r-1, gChain, "C")
            var aN = get_atom_rcn( r, gChain, "N")
            var aCa = get_atom_rcn( r, gChain, "CA")
            var aC = get_atom_rcn( r, gChain, "C")
            var aNn = get_atom_rcn( r+1, gChain, "N")
            if (aNn.size < 1) {
                aNn = aCp
            }
            
            # phi
            if (aCa.group != "PRO") {
                if (((r > cres) and (r < ares))
                    or ((r == cres) and (aname == "N"))
                    or ((r == ares) and (aname != "N"))) {
                    if (is_rotor_avail(aN.atomIndex, aCa.atomIndex)) {
                        gCrotors += aCp.atomIndex
                        gCrotors += aN.atomIndex
                        gCrotors += aCa.atomIndex
                        gCrotors += aC.atomIndex
                    }
                }
            }
            
            # psi
            if (((r >= cres) and (r < ares))
                or ((r == cres) and (aname != "C"))
                or ((r == ares) and (aname == "C"))) {
                if (is_rotor_avail(aCa.atomIndex, aC.atomIndex)) {
                    gCrotors += aN.atomIndex
                    gCrotors += aCa.atomIndex
                    gCrotors += aC.atomIndex
                    gCrotors += aNn.atomIndex
                }
            }
        } # endfor
    }
}

function collect_rotors() {
    collect_bb_rotors(false)
    collect_bb_rotors(true)
}

function tug_sc(pt) {

    # If destination atom defined
    if (gDestAtomIdx >= 0) {
        var v = {atomIndex=gDestAtomIdx}.xyz - {atomIndex=gSCidx}.xyz
        if (abs(angle({atomIndex=gDestAtomIdx}.xyz, {0 0 0}, pt)) < 90) {
            pt = -v/20.0
        }
        else {
            pt = v/20.0
        }
    }
    gSCpt += pt
    draw arrow {atomIndex=gSCidx} @gSCpt
}

function set_colors() {
    select (thisModel)
    color {selected} @gScheme
    color {atomIndex=g1pivotIdx} green
    color {atomIndex=g2pivotIdx} green
    color @gCargoSet @gAltScheme
    color {gCargoSet and oxygen} pink
    select {(atomIndex=gCcargoIdx) or (atomIndex=gNcargoIdx)
        or (atomIndex=gCanchorIdx) or (atomIndex=gNanchorIdx)}
    halo on
    select {atomIndex=gDestAtomIdx}
    star on
    select none
}

function clear_atom_idxs() {
    gCcargoIdx = -1
    gNcargoIdx = -1
    gCanchorIdx = -1
    gNanchorIdx = -1
    g1pivotIdx = -1
    g2pivotIdx = -1
    g1dynamicIdx = -1
    g2dynamicIdx = -1
    gDestAtomIdx = -1
    gSCidx = -1
}

function timed_out (s) {
    timeout ID"tug" OFF
    refresh
    if (prompt(format("%s - Undo?", s), "Yes|No", true) == "Yes") {
        gBusy = false
        restore state gState
        connect
        select gCargoSet
        set bondPicking true
        refresh
        for (var i = 1; i <= gFreeze.size; i+=2) {
            select {atomIndex=@{gFreeze[i]}} or {atomIndex=@{gFreeze[i+1]}}
            color bonds lightblue
        }
        background ECHO yellow
        echo @gEcho
        select all
        quit
    }
}

function record_drag() {
    var ls = format("select %s;", {selected})
    ls += format("gCanchorIdx = %d;", gCanchorIdx)
    ls += format("gCanchorNo = %d;", gCanchorNo)
    ls += format("gNanchorIdx = %d;", gNanchorIdx)
    ls += format("gNanchorNo = %d;", gNanchorNo)
    ls += format("gCcargoIdx = %d;", gCcargoIdx)
    ls += format("gNcargoIdx = %d;", gNcargoIdx)
    ls += format("gCcargoNo = %d;", gCcargoNo)
    ls += format("gNcargoNo = %d;", gNcargoNo)
    ls += format("gDestAtomIdx = %d;", gDestAtomIdx)
    ls += format("g1pivotIdx = %d;", g1pivotIdx)
    ls += format("g2pivotIdx = %d;", g2pivotIdx)
    ls += format("gOkCollide = %s;", gOkCollide)
    ls += format("gChain = \"%s\";", gChain)
    ls += format("gMinNo = %d;", gMinNo)
    ls += format("gMaxNo = %d;", gMaxNo)
    ls += format("gCargoSet = %s;", gCargoSet)
    ls += format("gSCidx = %d;", gSCidx)
    ls += format("gSCcircle = %d;", gSCcircle)
    ls += format("gSCpt = %s;", gSCpt)
    ls += "collect_rotors();"
    ls += "tug_drag_done_mb();"
    plico_record(ls)
}

# Pick call-back for freeze
function tug_pick_cb() {
    if (_pickInfo[3][6] == "bond") {
        var sel = {selected}
        var i = _pickInfo.find(":")
        var iChain = _pickInfo[i+1]
        i = _pickInfo.find("#")
        var a1no = 0 + _pickInfo[i+1][i+3]
        var j = _pickInfo[i+1][9999].find("#")
        var a2no = 0 + _pickInfo[i+j+1][i+j+3]
        var i1idx = {(atomno=a1no) and (chain=iChain)
            and thisModel}.atomIndex
        var i2idx = {(atomno=a2no) and (chain=iChain)
            and thisModel}.atomIndex
        if (({atomIndex=i1idx}.atomName == "CA")
            or ({atomIndex=i2idx}.atomName == "CA")) {
            if (({atomIndex=i1idx}.atomName != "CB")
                and ({atomIndex=i2idx}.atomName != "CB")) {
                if (i1idx > i2idx) {
                    var idx = 0 + i1idx
                    i1idx = 0 + i2idx
                    i2idx = 0 + idx
                }
                select {atomIndex=i1idx} or {atomIndex=i2idx}

                for (i = 1; i <= gFreeze.size; i += 2) {
                    if ((gFreeze[i] == i1idx) and (gFreeze[i+1] == i2idx)) {
                        if (i > 1) {
                            if (gFreeze.size = (i+1)) {
                                gFreeze = gFreeze[1][i-1]
                            }
                            else {
                                gFreeze = gFreeze[1][i-1] + gFreeze[i+2][0]
                            }
                        }
                        else {
                            gFreeze = gFreeze[i+2][0]
                        }
                        color bonds NONE
                        i = 0
                        break
                    }
                }
                if (i > gFreeze.size) {
                    gFreeze += i1idx
                    gFreeze += i2idx
                    color bonds lightblue
                }
            }
        }
        select {sel}
    }
}

# Bound to LEFT-UP by tug_enable_drag
function tug_drag_done_mb() {
    if (not gBusy) {
        if (gPlicoRecord != "") {
            record_drag()
        }

        # Move by rotation on rotor sets, smallest first
        gBusy = true
        background ECHO pink
        refresh

        # If side chain mode
        if (gSCidx >= 0) {
            drag_sc()
        }

        # Else
        else if (not gTow) {
            gOK = true
            timeout ID"tug" 20.0 "timed_out(\"Tug timed out\")"
            if ((gCrotors.size < gNrotors.size) or (gNanchorIdx < 0)) {
                if (gCrotors.size > 4) {
                    tug_track_c()
                }
                if (gOK and (gNrotors.size > 4)) {
                    tug_track_n()
                }
            }
            else {
                if (gNrotors.size > 4) {
                    tug_track_n()
                }
                if (gOK and (gCrotors.size > 4)) {
                    tug_track_c()
                }
            }
            timeout ID"tug" OFF

            # If anchor angles acute, fail
            if (gOK == true) {
                if (gCanchorIdx >= 0) {
                    var ic = get_cward_bb_idx(gCanchorIdx, gChain)
                    var in = get_nward_bb_idx(gCanchorIdx, gChain)
                    if ((ic >= 0) and
                        angle({atomIndex=ic}, {atomIndex=gCanchorIdx}, {atomIndex=in})
                        < 100.0) {
                        gOK = false
                    }
                }
                if (gNanchorIdx >= 0) {
                    var ic = get_cward_bb_idx(gNanchorIdx, gChain)
                    var in = get_nward_bb_idx(gNanchorIdx, gChain)
                    if ((in >= 0) and
                        angle({atomIndex=ic}, {atomIndex=gNanchorIdx}, {atomIndex=in})
                        < 100.0) {
                        gOK = false
                    }
                }
            }

            # If too far
            if (not gOK) {
                timed_out("TUG TOO FAR!")
            }

            # Else OK
            else {

                var idx = {
                    (atomno=@{{(chain=gChain) and thisModel}.atomno.min})
                    and (chain=gChain) and thisModel}.atomIndex


                var ihc = 0
                for (ihc = 0; ihc < 10; ihc++) {
                    select ((atomno >= gNanchorNo) and (atomno <= gCanchorNo)
                        and (chain = gChain) and thisModel)
                    handle_collisions( idx)
                    if (count_collisions(({})).size == 0) {
                        break
                    }
                }
                if (ihc == 10) {
                    timed_out("Unable to handle all collisions!")
                }
            }
        }
        select {gCargoSet}
        gBusy = false
        background ECHO yellow
        set bondPicking true
        refresh
    }
}

# Bound to ALT-SHIFT-LEFT-DRAG by tug_enable_drag
function tug_drag_2_mb() {
    tug_drag_mb(true)
}

# Bound to ALT-LEFT-DRAG by tug_enable_drag
function tug_drag_mb(alt) {
    if (not gBusy) {
        gBusy = true
        var dx = (40.0 * (_mouseX - gMouseX))/_width
        var dy = (40.0 * (_mouseY - gMouseY))/_height
        var q = quaternion()
        var ptd = {@dx @dy 0}
        var pt = (!q)%ptd
        var caxis = {0 0 0}
        if (distance(pt,  {0 0 0}) > 0.004) {
            # If sidechain mode
            if (gSCidx >= 0) {
                if ({atomIndex=gSCidx}.atomName == "O") {
                    if ({atomIndex=gSCidx}.group != "PRO") {
                        var dir = ((abs(dx) > abs(dy))
                            ? ((dx < 0) ? 10 : -10)
                            : ((dy < 0) ? 1 : -1))
                        counter_rotate(gSCidx, dir, not alt)
                    }
                }
                else {
                    gSCcheck = not alt
                    tug_sc(pt)
                }
            }

            # Else
            else {

                # If new drag
                if (gNewDrag) {
                    gNewDrag = false
                    save state gState
                }

                # If destination atom defined
                if (gDestAtomIdx >= 0) {
                    var v = {atomIndex=gDestAtomIdx}.xyz - {selected}.xyz
                    if (abs(angle({atomIndex=gDestAtomIdx}.xyz, {0 0 0}, pt)) < 90) {
                        pt = -v/20.0
                    }
                    else {
                        pt = v/20.0
                    }
                }

                # Move the cargo
                select {gCargoSet}

                # If pivots defined, rotate it
                if (g1pivotIdx >= 0) {

                    # If two pivots
                    if (g2pivotIdx >= 0) {
                        caxis = {atomIndex=g2pivotIdx}
                    }

                    # Else
                    else {
                        caxis = cross(pt, {0 0 0}) + {atomIndex=g1pivotIdx}.xyz
                    }

                    var dir = ((abs(dx) > abs(dy))
                        ? ((dx < 0) ? 2 : -2)
                        : ((dy < 0) ? 2 : -2))
                    rotate_selected_record(g1pivotIdx, caxis, dir)

                }

                # Else translate it
                else {
                    translate_selected_record(pt)
                }

                # If collisions
                var cNotSels = (within(kCtolerance, false, {selected})
                    and not {gMovingSet})
                if ((cNotSels) and (not alt)) {
                    gOk2 = true
                    for (var i = 1; i <= cNotSels.size;  i++) {

                        # If net collision vector same as move vector
                        var cSels = (within(kCtolerance, false, cNotSels[i]) and {selected})
                        for (var j = 1; j <= cSels.size;  j++) {
                            var v1 = cNotSels[i].xyz - cSels[j].xyz
                            if (abs(angle(v1, {0 0 0}, pt)) < 90) {

                                # If tow mode
                                if (gTow) {

                                    # Make a dynamic pivot
                                    if (g1pivotIdx < 0) {
                                        g1pivotIdx = cSels[j].atomIndex
                                        g1dynamicIdx = cNotSels[i].atomIndex
                                        color {atomIndex=g1pivotIdx} lightgreen
                                        set_distance_idx(cNotSels[i].atomIndex,
                                            cSels[j].atomIndex,
                                            kCtolerance + kDtolerance)
                                    }
                                    else if (g2pivotIdx < 0) {
                                        g2pivotIdx = cSels[j].atomIndex
                                        g2dynamicIdx = cNotSels[i].atomIndex
                                        color {atomIndex=g2pivotIdx} lightgreen
                                        set_distance_idx(cNotSels[i].atomIndex,
                                            cSels[j].atomIndex,
                                            kCtolerance + kDtolerance)
                                    }
                                    else {
                                        gOk2 = false
                                    }
                                }
                                else {

                                    # Try to resolve
                                    select @{cSels[j]}
                                    var idx = {(atomno=@{{(chain=gChain)
                                        and thisModel}.atomno.min})
                                        and (chain=gChain) and thisModel}.atomIndex
                                    handle_collisions( idx)
                                }
                            }
                        } # endfor
                        if (not gOk2) {
                            break
                        }
                    } # endfor

                    # If unable
                    if (not gOk2) {

                        # Back off
                        background ECHO pink
                        delay 1
                        if (g1pivotIdx >= 0) {
                            rotate_selected_record(g1pivotIdx, caxis, -a)
                        }
                        else {
                            translate_selected_record(-pt)
                        }
                        background ECHO yellow
                        set bondPicking true
                    }
                }
            }

            # If dynamic pivots
            if (g1dynamicIdx >= 0) {
                var v1 = {atomIndex=g1dynamicIdx}.xyz - {atomIndex=g1pivotIdx}.xyz
                if (abs(angle(v1, {0 0 0}, pt)) > 90) {
                    color {atomIndex=g1pivotIdx} @gAltScheme
                    g1pivotIdx = -1
                    g1dynamicIdx = -1

                }
            }
            if (g2dynamicIdx >= 0) {
                var v1 = {atomIndex=g2dynamicIdx}.xyz - {atomIndex=g2pivotIdx}.xyz
                if (abs(angle(v1, {0 0 0}, pt)) > 90) {
                    color {atomIndex=g2pivotIdx} @gAltScheme
                    g2pivotIdx = -1
                    g2dynamicIdx = -1
                }
            }

            gMouseX = _mouseX
            gMouseY = _mouseY
        }
        select {gCargoSet}
        gBusy = false
    }
}

# Bound to ALT-LEFT-DOWN by tug_enable_drag
function tug_mark_mb() {
    gMouseX = _mouseX
    gMouseY = _mouseY
    gNewDrag = true
}

# Called by tug_cargo_mb
function tug_enable_drag() {
    gEcho = "__________TUG__________|ALT-CLICK=mark block|SHIFT-CLICK=anchors" +
        "|ALT-CTRL-CLICK=pivots|ALT-SHIFT-CLICK=dest atom|ALT-DRAG=move" +
        "|ALT-SHIFT-DRAG=alt move|CLICK bond=freeze|ALT-DOUBLE-CLICK=undo" +
        "|SHIFT-DOUBLE-CLICK=exit"
    echo @gEcho

    # Allow atoms to be dragged
    bind "ALT-LEFT-DOWN" "tug_mark_mb";
    bind "ALT-LEFT-UP" "tug_drag_done_mb";
    bind "ALT-SHIFT-LEFT-DOWN" "tug_mark_mb";
    bind "ALT-LEFT-DRAG" "tug_drag_mb";
    bind "ALT-SHIFT-LEFT-DRAG" "tug_drag_2_mb";

    unbind "SHIFT-LEFT-CLICK"
    bind "SHIFT-LEFT-CLICK" "_pickAtom";
    bind "SHIFT-LEFT-CLICK" "+:tug_anchor_mb";

    bind "ALT-CTRL-LEFT-CLICK" "_pickAtom";
    bind "ALT-CTRL-LEFT-CLICK" "+:tug_pivot_mb";

    bind "ALT-SHIFT-LEFT-CLICK" "_pickAtom";
    bind "ALT-SHIFT-LEFT-CLICK" "+:tug_dest_atom_mb";
}

# Bound to SHIFT-LEFT-CLICK by tug_cargo_mb
function tug_anchor_mb() {
    if ({atomIndex=_atomPicked}.chain == gChain) {
        var aPidx = get_sc_bb_idx( _atomPicked, gChain)

        var pno = {atomIndex=aPidx}.atomno
        if (pno > {atomIndex=gCcargoIdx}.atomno) {
            select {atomIndex=gCanchorIdx}
            halo off
            if (gCanchorIdx == aPidx) {
                gCanchorIdx = -1
                gCanchorNo = gMaxNo + 1
            }
            else {
                gCanchorIdx = aPidx
                gCanchorNo = {atomIndex=gCanchorIdx}.atomno
                select {atomIndex=gCanchorIdx}
                halo on
            }
            collect_bb_rotors(false)
        }
        else if (pno < {atomIndex=gNcargoIdx}.atomno) {
            select {atomIndex=gNanchorIdx}
            halo off
            if (gNanchorIdx == aPidx) {
                gNanchorIdx = -1
                gNanchorNo = gMinNo - 1
            }
            else {
                gNanchorIdx = aPidx
                gNanchorNo = {atomIndex=gNanchorIdx}.atomno
                select {atomIndex=gNanchorIdx}
                halo on
            }
            collect_bb_rotors(true)
        }
        else {
            tow_cargo_mb()
        }

        # Get moving atoms set
        gMovingSet = {((atomno < gCanchorNo) and (atomno > gNanchorNo)
            and (chain=gChain) and thisModel)}
    }
    select {gCargoSet}
}

# Bound to ALT-SHIFT-LEFT-CLICK by tug_cargo_mb
function tug_dest_atom_mb() {
    var aOk = true
    if ({atomIndex=_atomPicked}.chain == gChain) {

        var pno = {atomIndex=_atomPicked}.atomno
        if ((pno <= {atomIndex=gCcargoIdx}.atomno)
            and (pno >= {atomIndex=gNcargoIdx}.atomno)) {
            aOk = false
        }
    }
    if (aOk) {
        select {atomIndex=gDestAtomIdx}
        star off
        if (gDestAtomIdx == _atomPicked) {
            gDestAtomIdx = -1
        }
        else {
            gDestAtomIdx = _atomPicked
            select {atomIndex=gDestAtomIdx}
            star on
        }
        select {gCargoSet}
    }
}

# Bound to CTRL-LEFT-CLICK by tug_cargo_mb
function tug_pivot_mb() {
    if (g1pivotIdx == _atomPicked) {
        color {atomIndex=g1pivotIdx} @gScheme
        if (g2pivotIdx >= 0) {
            g1pivotIdx = g2pivotIdx
            g2pivotIdx = -1
            g2dynamicIdx = -1
        }
        else {
            g1pivotIdx = -1
            g1dynamicIdx = -1
        }
    }
    else if (g2pivotIdx == _atomPicked) {
        color {atomIndex=g2pivotIdx} @gScheme
        g2pivotIdx = -1
        g2dynamicIdx = -1
    }
    else if (g1pivotIdx >= 0) {
        if (g2pivotIdx >= 0) {
            color {atomIndex=g2pivotIdx} @gScheme
        }

        g2pivotIdx = _atomPicked
        g2dynamicIdx = -1
        color {atomIndex=g2pivotIdx} green
    }
    else {
        g1pivotIdx = _atomPicked
        g1dynamicIdx = -1
        color {atomIndex=g1pivotIdx} green
    }
    select {gCargoSet}
}

# Bound to SHIFT-LEFT-CLICK by plico_tug
function tow_cargo_mb() {
    gTow = true
    gChain = {atomIndex=_atomPicked}.chain
    gMinNo = {(chain=gChain) and thisModel}.atomno.min
    gMaxNo = {(chain=gChain) and thisModel}.atomno.max
    gCcargoIdx = -1
    gNcargoIdx = -1
    gCanchorIdx = -1
    gCanchorNo = gMaxNo + 1
    gNanchorIdx = -1
    gNanchorNo = gMinNo - 1

    # Highlight cargo cluster
    select {(chain=gChain) and thisModel}
    gCargoSet = {selected}
    gMovingSet = {selected}
    set_colors()

    # Enable dragging
    tug_enable_drag()
    select {gCargoSet}
    halo off
    var es = gEcho.replace("anchors","mark chain")
    echo @es
    unbind "SHIFT-LEFT-CLICK"
    bind "SHIFT-LEFT-CLICK" "_pickAtom";
    bind "SHIFT-LEFT-CLICK" "+:tow_cargo_mb";
}

# Bound to ALT-LEFT-CLICK by plico_tug or called by plicotoab.toabCargoMB
function tug_cargo_mb() {

    # If O or movable side chain atom picked
    if ((({atomIndex=_atomPicked}.atomName = "O")
        and ({atomIndex=_atomPicked}.group != "PRO"))
        or (is_moveable_sc( _atomPicked))) {
        if (gSCidx >= 0) {
            draw gSCcircle DELETE
        }
        if (gSCidx == _atomPicked) {
            gSCidx = -1
        }
        else {
            gSCidx = _atomPicked
            gSCpt = {atomIndex=gSCidx}.xyz
            draw gSCcircle CIRCLE {atomIndex=gSCidx} MESH NOFILL
        }
    }
    else {
        if ({atomIndex=_atomPicked}.chain != gChain) {
            if (gTow) {
                select {(chain=gChain) and thisModel}
                color {selected} @gScheme
            }
            gChain = {atomIndex=_atomPicked}.chain
            select ({atomIndex=gCcargoIdx} or {atomIndex=gNcargoIdx}
                or {atomIndex=gCanchorIdx} or {atomIndex=gNanchorIdx})
            halo off
            gCcargoIdx = -1
            gNcargoIdx = -1
            gCanchorIdx = -1
            gNanchorIdx = -1
        }
        gTow = false
        gMinNo = {(chain=gChain) and thisModel}.atomno.min
        gMaxNo = {(chain=gChain) and thisModel}.atomno.max
        if (gNanchorIdx < 0) {
            gNanchorNo = gMinNo - 1
        }
        if (gCanchorIdx < 0) {
            gCanchorNo = gMaxNo + 1
        }
        var aPidx = get_sc_bb_idx( _atomPicked, gChain)

        gSCidx = -1
        draw gSCcircle DELETE

        # If existing cWard cargo picked
        if (gCcargoIdx == aPidx) {

            # Clear the highlight
            select {atomIndex=gCcargoIdx}
            halo off

            # If nWard cargo exists, mark it as the cWard cargo
            if (gNcargoIdx != gCcargoIdx) {
                gCcargoIdx = get_cp_idx(gNcargoIdx)
                gCcargoNo = {atomIndex=gCcargoIdx}.atomno
            }
            else {
                gCcargoIdx = -1
                gNcargoIdx = -1
            }
        }
        else if (gNcargoIdx == aPidx) {
            select {atomIndex=gNcargoIdx}
            halo off
            gNcargoIdx = get_nm_idx(gCcargoIdx)
            gNcargoNo = {atomIndex=gNcargoIdx}.atomno
        }
        else if (gCcargoIdx >= 0) {

            var no = {atomIndex=aPidx}.atomno

            # If pick is nWard of it
            if (no < {atomIndex=gCcargoIdx}.atomno) {

                # If exists, clear its highlight
                if (gNcargoIdx != gCcargoIdx) {
                    select {atomIndex=gNcargoIdx}
                    halo off
                }

                # Set new nWard cargo and highlight it
                gNcargoIdx = get_nm_idx(aPidx)
                gNcargoNo = {atomIndex=gNcargoIdx}.atomno
            }

            # Else cWard
            else {

                # Clear its old highlight
                select {atomIndex=gCcargoIdx}
                if (gNcargoIdx != gCcargoIdx) {
                    halo off
                }

                # Set new cWard cargo and highlight
                gCcargoIdx = get_cp_idx(aPidx)
                gCcargoNo = {atomIndex=gCcargoIdx}.atomno
            }
        }

        # Else no cWard cargo
        else {

            # Set new cWard cargo and highlight
            gCcargoIdx = get_cp_idx(aPidx)
            gCcargoNo = {atomIndex=gCcargoIdx}.atomno
            gNcargoIdx = get_nm_idx(gCcargoIdx)
            gNcargoNo = {atomIndex=gNcargoIdx}.atomno
        }

        # If any anchor now inside cargo cluster, kill it
        if ({atomIndex=gCanchorIdx}.atomno <= {atomIndex=gCcargoIdx}.atomno) {
            gCanchorIdx = -1
            gCanchorNo = gMaxNo + 1
        }
        if ({atomIndex=gNanchorIdx}.atomno >= {atomIndex=gNcargoIdx}.atomno) {
            gNanchorIdx = -1
            gNanchorNo = gMinNo - 1
        }

        # Highlight cargo cluster
        select_nward_idx(gCcargoIdx, gNcargoIdx)
        gCargoSet = {selected}
        set_colors()

        # Collect the rotor sets
        collect_rotors()

        # Get moving atoms set
        gMovingSet = {((atomno < gCanchorNo) and (atomno > gNanchorNo)
            and (chain=gChain) and thisModel)}
    }

    # Enable dragging
    if (gToab) {
        to_ab_enable_drag()
    }
    else {
        tug_enable_drag()
    }

    select {gCargoSet}
}

function tug_undo_mb() {
    if (prompt("Undo", "Yes|No", true) == "Yes") {
        restore state gState
    }
}

# Top level of Tug
function plico_tug() {

    # Load common functions if not already
    if (kCommon < 7) {
        script $SCRIPT_PATH$plicoCommon.spt
        if (kCommon < 7) {
            prompt ("A newer version of plicoCommon.SPT is required")
            quit
        }
    }

    gPlico = "TUG"
    plico_prelim(true, true)
    
    gBondPicking = bondPicking
    set bondPicking true
    set PickCallback "jmolscript:tug_pick_cb"

    gEcho = ("_________TUG_________|ALT-CLICK=mark block|SHIFT-CLICK=mark chain" +
        "|CLICK bond=freeze|ALT-DOUBLE-CLICK=undo|SHIFT-DOUBLE-CLICK=exit")
    echo @gEcho
    gCrotors = array()
    gNrotors = array()
    clear_atom_idxs()
    gToab = false

    bind "ALT-LEFT-CLICK" "_pickAtom";
    bind "ALT-LEFT-CLICK" "+:tug_cargo_mb";
    bind "SHIFT-LEFT-CLICK" "_pickAtom";
    bind "SHIFT-LEFT-CLICK" "+:tow_cargo_mb";
    bind "ALT-LEFT-DOUBLE" "tug_undo_mb";
    bind "SHIFT-DOUBLE" "tug_exit(true)";
    bind "LEFT-CLICK" "+:plico_menu_toggle";
}

# Bound to DOUBLE by plico_tug
function tug_exit() {
    if (plico_exit()) {
        set bondPicking gBondPicking
        set PickCallback NONE
        select all
        color bonds none
        
        reset kTug
        reset gCanchorIdx  
        reset gCanchorNo   
        reset gPlico       
        reset gNanchorIdx  
        reset gNanchorNo   
        reset gCcargoIdx   
        reset gNcargoIdx   
        reset gCcargoNo    
        reset gNcargoNo    
        reset gDestAtomIdx 
        reset g1pivotIdx   
        reset g2pivotIdx   
        reset gSelSaves    
        reset gCrotors     
        reset gNrotors     
        reset gOkCollide   
        reset gChain       
        reset gMinNo       
        reset gMaxNo       
        reset gCargoSet    
        reset gMovingSet   
        reset gSCidx       
        reset gSCcircle    
        reset gSCpt        
        reset gTargetPt    
        reset gNewDrag     
        reset gTow         
        reset g1dynamicIdx 
        reset g2dynamicIdx 
        reset gSCcheck     
        reset gBondPicking 
        reset gFreeze      
        reset gToab        
        reset function "get_cp_idx"
        reset function "get_cm_no"
        reset function "get_nm_idx"
        reset function "get_np_no"
        reset function "get_cb_idx"
        reset function "get_o_idx"
        reset function "get_nward_bb_idx"
        reset function "get_cward_bb_idx"
        reset function "get_sc_set"
        reset function "get_sc_bb_idx"
        reset function "is_bb_idx"
        reset function "is_sc_idx"
        reset function "select_add_sc"
        reset function "handle_collisions"
        reset function "tug_track_idx"
        reset function "do_counter_rotate"
        reset function "counter_rotate"
        reset function "counter_rotate_2"
        reset function "repair_proline"
        reset function "repair_sc"
        reset function "tug_track_c"
        reset function "tug_track_n"
        reset function "translate_selected_record"
        reset function "rotate_selected_record"
        reset function "collect_sc_rotors"
        reset function "drag_sc"
        reset function "fix_sc_collision_2"
        reset function "is_moveable_sc"
        reset function "is_rotor_avail"
        reset function "xcollect_bb_rotors"
        reset function "collect_bb_rotors"
        reset function "collect_rotors"
        reset function "tug_sc"
        reset function "set_colors"
        reset function "clear_atom_idxs"
        reset function "timed_out"
        reset function "record_drag"
        reset function "tug_pick_cb"
        reset function "tug_drag_done_mb"
        reset function "tug_drag_2_mb"
        reset function "tug_drag_mb"
        reset function "tug_mark_mb"
        reset function "tug_anchor_mb"
        reset function "tug_enable_drag"
        reset function "tug_dest_atom_mb"
        reset function "tug_pivot_mb"
        reset function "tow_cargo_mb"
        reset function "tug_cargo_mb"
        reset function "tug_undo_mb"
        reset function "plico_tug"


    }
}

# End of TUG.SPT

Contributors

Remig