Profile


Name
HBuster2
ID
101089
Shared with
Public
Parent
None
Children
None
Created on
June 07, 2015 at 14:50 PM UTC
Updated on
June 07, 2015 at 14:50 PM UTC
Description

HBuster2

Best for


Code


PuzzleDescription = puzzle.GetDescription() PuzzleExpirationTime = puzzle.GetExpirationTime() PuzzleName = puzzle.GetName() PuzzleID = puzzle.GetPuzzleID() GroupRank = scoreboard.GetGroupRank() ScoreType = scoreboard.GetScoreType() GroupRank = scoreboard.GetGroupRank() Platform = ui.GetPlatform() TrackName = ui.GetTrackName() GroupID = user.GetGroupID() PlayerID = user.GetPlayerID() PlayerName = user.GetPlayerName() GroupName = user.GetGroupName() structureCount = structure.GetCount() --AFK ************** DEV={} DEV[104378] = true if(DEV[PlayerID] == true)then print("DEV PuzzleID="..PuzzleID) end AFK = {} AFK._VERSION = "3.0" AFK.ClientName = "AFK"..AFK._VERSION.." - "..PuzzleName structure.ResetApprovedResidues = function() if(not structure["ApprovedResidues"] or type(structure["ApprovedResidues"]) ~= table) then structure.ApprovedResidues = {} structure.ApprovedResidues.s = true structure.ApprovedResidues.t = true structure.ApprovedResidues.n = true structure.ApprovedResidues.q = true structure.ApprovedResidues.r = true structure.ApprovedResidues.h = true structure.ApprovedResidues.k = true structure.ApprovedResidues.d = true structure.ApprovedResidues.e = true structure.ApprovedResidues.v = true structure.ApprovedResidues.w = true structure.ApprovedResidues.y = true structure.ApprovedResidues.l = true structure.ApprovedResidues.m = true structure.ApprovedResidues.p = true structure.ApprovedResidues.i = true structure.ApprovedResidues.a = true structure.ApprovedResidues.c = true structure.ApprovedResidues.f = true structure.ApprovedResidues.g = true end local PrimaryStructureDialog = dialog.CreateDialog(AFK.ClientName) PrimaryStructureDialog.serineInd = dialog.AddCheckbox("serine(s) 1+-", structure.ApprovedResidues.s) PrimaryStructureDialog.threonineInd = dialog.AddCheckbox("threonine(t) 1+-", structure.ApprovedResidues.t) PrimaryStructureDialog.asparagineInd = dialog.AddCheckbox("asparagine(n) 1+, 1-", structure.ApprovedResidues.n) PrimaryStructureDialog.glutamineInd = dialog.AddCheckbox("glutamine(q) 1+, 1-", structure.ApprovedResidues.q) PrimaryStructureDialog.arginineInd = dialog.AddCheckbox("arginine(r) 3+", structure.ApprovedResidues.r) PrimaryStructureDialog.histidineInd = dialog.AddCheckbox("histidine(h) 1+, 1-", structure.ApprovedResidues.h) PrimaryStructureDialog.lysineInd = dialog.AddCheckbox("lysine(k) 1+", structure.ApprovedResidues.k) PrimaryStructureDialog.aspartateInd = dialog.AddCheckbox("aspartic acid(d) 1+, 1-", structure.ApprovedResidues.d) PrimaryStructureDialog.glutamateInd = dialog.AddCheckbox("glutamic acid(e) 1+, 1-", structure.ApprovedResidues.e) PrimaryStructureDialog.valineInd = dialog.AddCheckbox("valine(v)", structure.ApprovedResidues.v) PrimaryStructureDialog.tryptophanInd = dialog.AddCheckbox("tryptophan(w) 1+", structure.ApprovedResidues.w) PrimaryStructureDialog.tyrosineInd = dialog.AddCheckbox("tyrosine(y) 1+-", structure.ApprovedResidues.y) PrimaryStructureDialog.leucineInd = dialog.AddCheckbox("leucine(l)", structure.ApprovedResidues.l) PrimaryStructureDialog.methionineInd = dialog.AddCheckbox("methionine(m)", structure.ApprovedResidues.m) PrimaryStructureDialog.prolineInd = dialog.AddCheckbox("proline(p)", structure.ApprovedResidues.p) PrimaryStructureDialog.isoleucineInd = dialog.AddCheckbox("isoleucine(i)", structure.ApprovedResidues.i) PrimaryStructureDialog.alanineInd = dialog.AddCheckbox("alanine(a)", structure.ApprovedResidues.a) PrimaryStructureDialog.cystineInd = dialog.AddCheckbox("cystine(c) sulfide", structure.ApprovedResidues.c) PrimaryStructureDialog.phenylalanineInd = dialog.AddCheckbox("phenylalanine(f)", structure.ApprovedResidues.f) PrimaryStructureDialog.glycineInd = dialog.AddCheckbox("glycine(g)", structure.ApprovedResidues.g) PrimaryStructureDialog.OK = dialog.AddButton("OK", 1) PrimaryStructureDialog.Cancel = dialog.AddButton("Cancel", 2) local showDialog = dialog.Show(PrimaryStructureDialog) for segmentIndex=1,structureCount do if(not structure[segmentIndex] or type(structure[segmentIndex]) ~= table) then structure[segmentIndex] = {} end if(not structure[segmentIndex]["ApprovedResidues"] or type(structure[segmentIndex]["ApprovedResidues"]) ~= table) then structure[segmentIndex]["ApprovedResidues"] = {} structure[segmentIndex]["ApprovedResidues"]["s"] = structure.CanMutate(segmentIndex, "s") structure[segmentIndex]["ApprovedResidues"]["t"] = structure.CanMutate(segmentIndex, "t") structure[segmentIndex]["ApprovedResidues"]["n"] = structure.CanMutate(segmentIndex, "n") structure[segmentIndex]["ApprovedResidues"]["q"] = structure.CanMutate(segmentIndex, "q") structure[segmentIndex]["ApprovedResidues"]["r"] = structure.CanMutate(segmentIndex, "r") structure[segmentIndex]["ApprovedResidues"]["h"] = structure.CanMutate(segmentIndex, "h") structure[segmentIndex]["ApprovedResidues"]["k"] = structure.CanMutate(segmentIndex, "k") structure[segmentIndex]["ApprovedResidues"]["d"] = structure.CanMutate(segmentIndex, "d") structure[segmentIndex]["ApprovedResidues"]["e"] = structure.CanMutate(segmentIndex, "e") structure[segmentIndex]["ApprovedResidues"]["v"] = structure.CanMutate(segmentIndex, "v") structure[segmentIndex]["ApprovedResidues"]["w"] = structure.CanMutate(segmentIndex, "w") structure[segmentIndex]["ApprovedResidues"]["y"] = structure.CanMutate(segmentIndex, "y") structure[segmentIndex]["ApprovedResidues"]["l"] = structure.CanMutate(segmentIndex, "l") structure[segmentIndex]["ApprovedResidues"]["m"] = structure.CanMutate(segmentIndex, "m") structure[segmentIndex]["ApprovedResidues"]["p"] = structure.CanMutate(segmentIndex, "p") structure[segmentIndex]["ApprovedResidues"]["i"] = structure.CanMutate(segmentIndex, "i") structure[segmentIndex]["ApprovedResidues"]["a"] = structure.CanMutate(segmentIndex, "a") structure[segmentIndex]["ApprovedResidues"]["c"] = structure.CanMutate(segmentIndex, "c") structure[segmentIndex]["ApprovedResidues"]["f"] = structure.CanMutate(segmentIndex, "f") structure[segmentIndex]["ApprovedResidues"]["g"] = structure.CanMutate(segmentIndex, "g") structure[segmentIndex]["ApprovedResidues"][structure.GetAminoAcid(segmentIndex)] = true end if(showDialog == 1) then structure[segmentIndex]["ApprovedResidues"]["s"] = structure.CanMutate(segmentIndex, "s") structure[segmentIndex]["ApprovedResidues"]["t"] = structure.CanMutate(segmentIndex, "t") structure[segmentIndex]["ApprovedResidues"]["n"] = structure.CanMutate(segmentIndex, "n") structure[segmentIndex]["ApprovedResidues"]["q"] = structure.CanMutate(segmentIndex, "q") structure[segmentIndex]["ApprovedResidues"]["r"] = structure.CanMutate(segmentIndex, "r") structure[segmentIndex]["ApprovedResidues"]["h"] = structure.CanMutate(segmentIndex, "h") structure[segmentIndex]["ApprovedResidues"]["k"] = structure.CanMutate(segmentIndex, "k") structure[segmentIndex]["ApprovedResidues"]["d"] = structure.CanMutate(segmentIndex, "d") structure[segmentIndex]["ApprovedResidues"]["e"] = structure.CanMutate(segmentIndex, "e") structure[segmentIndex]["ApprovedResidues"]["v"] = structure.CanMutate(segmentIndex, "v") structure[segmentIndex]["ApprovedResidues"]["w"] = structure.CanMutate(segmentIndex, "w") structure[segmentIndex]["ApprovedResidues"]["y"] = structure.CanMutate(segmentIndex, "y") structure[segmentIndex]["ApprovedResidues"]["l"] = structure.CanMutate(segmentIndex, "l") structure[segmentIndex]["ApprovedResidues"]["m"] = structure.CanMutate(segmentIndex, "m") structure[segmentIndex]["ApprovedResidues"]["p"] = structure.CanMutate(segmentIndex, "p") structure[segmentIndex]["ApprovedResidues"]["i"] = structure.CanMutate(segmentIndex, "i") structure[segmentIndex]["ApprovedResidues"]["a"] = structure.CanMutate(segmentIndex, "a") structure[segmentIndex]["ApprovedResidues"]["c"] = structure.CanMutate(segmentIndex, "c") structure[segmentIndex]["ApprovedResidues"]["f"] = structure.CanMutate(segmentIndex, "f") structure[segmentIndex]["ApprovedResidues"]["g"] = structure.CanMutate(segmentIndex, "g") structure[segmentIndex]["ApprovedResidues"][structure.GetAminoAcid(segmentIndex)] = true if(PrimaryStructureDialog.serineInd.value ~= true) then structure[segmentIndex]["ApprovedResidues"]["s"] = false end if(PrimaryStructureDialog.threonineInd.value ~= true) then structure[segmentIndex]["ApprovedResidues"]["t"] = false end if(PrimaryStructureDialog.asparagineInd.value ~= true) then structure[segmentIndex]["ApprovedResidues"]["n"] = false end if(PrimaryStructureDialog.glutamineInd.value ~= true) then structure[segmentIndex]["ApprovedResidues"]["q"] = false end if(PrimaryStructureDialog.arginineInd.value ~= true) then structure[segmentIndex]["ApprovedResidues"]["r"] = false end if(PrimaryStructureDialog.histidineInd.value ~= true) then structure[segmentIndex]["ApprovedResidues"]["h"] = false end if(PrimaryStructureDialog.lysineInd.value ~= true) then structure[segmentIndex]["ApprovedResidues"]["k"] = false end if(PrimaryStructureDialog.aspartateInd.value ~= true) then structure[segmentIndex]["ApprovedResidues"]["d"] = false end if(PrimaryStructureDialog.glutamateInd.value ~= true) then structure[segmentIndex]["ApprovedResidues"]["e"] = false end if(PrimaryStructureDialog.valineInd.value ~= true) then structure[segmentIndex]["ApprovedResidues"]["v"] = false end if(PrimaryStructureDialog.tryptophanInd.value ~= true) then structure[segmentIndex]["ApprovedResidues"]["w"] = false end if(PrimaryStructureDialog.tyrosineInd.value ~= true) then structure[segmentIndex]["ApprovedResidues"]["y"] = false end if(PrimaryStructureDialog.leucineInd.value ~= true) then structure[segmentIndex]["ApprovedResidues"]["l"] = false end if(PrimaryStructureDialog.methionineInd.value ~= true) then structure[segmentIndex]["ApprovedResidues"]["m"] = false end if(PrimaryStructureDialog.prolineInd.value ~= true) then structure[segmentIndex]["ApprovedResidues"]["p"] = false end if(PrimaryStructureDialog.isoleucineInd.value ~= true) then structure[segmentIndex]["ApprovedResidues"]["i"] = false end if(PrimaryStructureDialog.alanineInd.value ~= true) then structure[segmentIndex]["ApprovedResidues"]["a"] = false end if(PrimaryStructureDialog.cystineInd.value ~= true) then structure[segmentIndex]["ApprovedResidues"]["c"] = false end if(PrimaryStructureDialog.phenylalanineInd.value ~= true) then structure[segmentIndex]["ApprovedResidues"]["f"] = false end if(PrimaryStructureDialog.glycineInd.value ~= true) then structure[segmentIndex]["ApprovedResidues"]["g"] = false end end end return showDialog end GetTimeStamp = function() --The GetTimeStamp function creates the current time-stamp preceding every line in the Fold.It client -- recipe output window local TimeStamp=os.date("*t",os.time()) local Text="" if(TimeStamp.hour<10)then Text=Text.."0" end if(TimeStamp.hour==0) then Text=Text.."0:" else Text=Text..TimeStamp.hour..":" end if(TimeStamp.min<10)then Text=Text.."0" end if(TimeStamp.min==0)then Text=Text.."0" else Text=Text..TimeStamp.min end if(ImportantPrint==true)then Text=Text..":" if(TimeStamp.sec<10)then Text=Text.."0" end if(TimeStamp.sec==0)then Text=Text.."0".." " else Text=Text..TimeStamp.sec.." " end else Text=Text.." " end return Text end structure.PrimaryStructureType = { ["s"] = "serine", ["t"] = "threonine", ["d"] = "aspartic acid", ["e"] = "glutamic acid", ["n"] = "asparagine", ["q"] = "glutamine", ["k"] = "lysine", ["h"] = "histidine", ["a"] = "arginine", ["y"] = "tyrosine", ["w"] = "tryptophan", ["f"] = "phenylalanine", ["c"] = "cystine", ["m"] = "methionine", ["a"] = "alanine", ["v"] = "valine", ["l"] = "leucine", ["i"] = "isoleucine", ["g"] = "glycine", ["p"] = "proline" -- ["M"] = "molecule", -- ["A"] = "adenine", -- ["C"] = "cytosine", -- ["G"] = "guanine", -- ["T"] = "thymine" } structure.SecondaryStructureType = { ["E"] = "sheet", ["H"] = "helix", ["L"] = "loop", ["M"] = "molecule" } structure.GetRange = function(segmentIndex1, segmentIndex2) local segmentIndex1 = segmentIndex1 local segmentIndex2 = segmentIndex2 if(segmentIndex1 and type(segmentIndex1) == "number" and segmentIndex1 > 0)then if(segmentIndex2 and type(segmentIndex2) == "number" and segmentIndex2 >= segmentIndex1 and segmentIndex2 <= structureCount)then else segmentIndex2 = segmentIndex1 end else segmentIndex1 = 1 segmentIndex2=structureCount end return segmentIndex1, segmentIndex2 end selection.DeselectRange = function(segmentIndex1, segmentIndex2) local segmentIndex1, segmentIndex2 = structure.GetRange(segmentIndex1, segmentIndex2) if(segmentIndex1 and type(segmentIndex1) == "number" and segmentIndex1 > 0 and segmentIndex2 and type(segmentIndex2) == "number" and segmentIndex2 >= segmentIndex1 and segmentIndex2 <= structureCount)then for n = segmentIndex1, segmentIndex2 do selection.Deselect(n) end end return end current.GetSegmentEnergyScores = function(segmentIndex1, segmentIndex2) local segmentIndex1, segmentIndex2 = structure.GetRange(segmentIndex1, segmentIndex2) local SegmentScores = {} for segmentIndex = segmentIndex1, segmentIndex2 do SegmentScores[segmentIndex] = current.GetSegmentEnergyScore(segmentIndex) end return SegmentScores end current.GetSegmentEnergySubscores = function(segmentIndex1, segmentIndex2) local segmentIndex1, segmentIndex2 = structure.GetRange(segmentIndex1, segmentIndex2) local SegmentScores = {} SegmentScores.SubscoreTotal = { ["Score"] = 0, ["clashing"] = 0, ["packing"] = 0, ["hiding"] = 0, ["bonding"] = 0, ["backbone"] = 0, ["sidechain"] = 0, ["reference"] = 0, ["disulfides"] = 0, ["other"] = 0 } for n = segmentIndex1, segmentIndex2 do local SegmentEnergyScore = current.GetSegmentEnergyScore(n) SegmentScores[n] = { ["SegmentEnergyScore"] = SegmentEnergyScore, ["clashing"] = current.GetSegmentEnergySubscore(n, "clashing"), ["packing"] = current.GetSegmentEnergySubscore(n, "packing"), ["hiding"] = current.GetSegmentEnergySubscore(n, "hiding"), ["bonding"] = current.GetSegmentEnergySubscore(n, "bonding"), ["backbone"] = current.GetSegmentEnergySubscore(n, "backbone"), ["sidechain"] = current.GetSegmentEnergySubscore(n, "sidechain"), ["reference"] = current.GetSegmentEnergySubscore(n, "reference"), ["disulfides"] = current.GetSegmentEnergySubscore(n, "disulfides"), ["other"] = current.GetSegmentEnergySubscore(n, "other") } SegmentScores.SubscoreTotal.Score = SegmentScores.SubscoreTotal.Score + SegmentEnergyScore SegmentScores.SubscoreTotal.clashing = SegmentScores.SubscoreTotal.clashing + SegmentScores[n]["clashing"] SegmentScores.SubscoreTotal.packing = SegmentScores.SubscoreTotal.packing + SegmentScores[n]["packing"] SegmentScores.SubscoreTotal.hiding = SegmentScores.SubscoreTotal.hiding + SegmentScores[n]["hiding"] SegmentScores.SubscoreTotal.bonding = SegmentScores.SubscoreTotal.bonding + SegmentScores[n]["bonding"] SegmentScores.SubscoreTotal.backbone = SegmentScores.SubscoreTotal.backbone + SegmentScores[n]["backbone"] SegmentScores.SubscoreTotal.sidechain = SegmentScores.SubscoreTotal.sidechain + SegmentScores[n]["sidechain"] SegmentScores.SubscoreTotal.reference = SegmentScores.SubscoreTotal.reference + SegmentScores[n]["reference"] SegmentScores.SubscoreTotal.disulfides = SegmentScores.SubscoreTotal.disulfides + SegmentScores[n]["disulfides"] SegmentScores.SubscoreTotal.other = SegmentScores.SubscoreTotal.other + SegmentScores[n]["other"] end return SegmentScores end structure.GetPrimaryStructure = function(segmentIndex1, segmentIndex2) local segmentIndex1, segmentIndex2 = structure.GetRange(segmentIndex1, segmentIndex2) local primaryStructure = {} for n = segmentIndex1, segmentIndex2 do primaryStructure[n] = structure.GetAminoAcid(n) end return primaryStructure end structure.GetDistanceTable = function(segmentIndex1, segmentIndex2, segmentIndex3, segmentIndex4) local segmentIndex1, segmentIndex2 = structure.GetRange(segmentIndex1, segmentIndex2) local segmentIndex3, segmentIndex4 = structure.GetRange(segmentIndex3, segmentIndex4) local DistanceTable = {} for n = segmentIndex1, segmentIndex2 do DistanceTable[n] = {} for m = segmentIndex3, segmentIndex4 do if n == m then DistanceTable[n][m] = 0 else DistanceTable[n][m] = structure.GetDistance(n, m) end end end return DistanceTable end rotamer.GetCurrentRotamer = function(segmentIndex1, segmentIndex2) local segmentIndex1 = 1 local segmentIndex2 = structure.GetCount() local segmentIndex1, segmentIndex2 = structure.GetRange(segmentIndex1, segmentIndex2) recentbest.Save() for segmentIndex = segmentIndex1, segmentIndex2 do local currentRotamer = "?" local score = current.GetSegmentEnergyScore(segmentIndex) for n=1, rotamer.GetCount(segmentIndex) do rotamer.SetRotamer(segmentIndex, n) if(score == current.GetSegmentEnergyScore(segmentIndex)) then currentRotamer = tostring(n) end end recentbest.Restore() print("structure.setAminoAcid("..segmentIndex..", "..structure.GetAminoAcid(segmentIndex)..")") if(currentRotamer ~= "?") then print("rotamer.SetRotamer("..segmentIndex..", "..currentRotamer..")") else print("selection.Select("..segmentIndex..")") end end end rotamer.GetPossibleRotamers = function(segmentIndex1, segmentIndex2) local segmentIndex1, segmentIndex2 = structure.GetRange(segmentIndex1, segmentIndex2) local rotamers = {} if(segmentIndex1 == segmentIndex2) then for n=1, rotamer.GetCount(segmentIndex1) do rotamer.SetRotamer(segmentIndex1, n) rotamers[n] = current.GetSegmentEnergySubscores(segmentIndex1) end else for n = segmentIndex1, segmentIndex2 do rotamers[n] = {} for m = 1, rotamer.GetCount(n) do rotamer.SetRotamer(n, m) rotamers[n][m] = current.GetSegmentEnergySubscores(n) end end end return rotamers end behavior.ChangeClashImportanceByFactor = function(importance) local importance = importance local clashImportance = behavior.GetClashImportance() if(importance and type(importance) == "number") then importance = math.max(0, math.min(100, math.floor(clashImportance * importance * 100))) / 100 if(importance ~= clashImportance) then behavior.SetClashImportance(importance) clashImportance = importance end end return clashImportance end structure.SuggestMutations = function(segmentIndex1, segmentIndex2) local filtersDisabled = behavior.GetSlowFiltersDisabled() behavior.SetSlowFiltersDisabled(true) recentbest.Restore() local segmentIndex1, segmentIndex2 = structure.GetRange(segmentIndex1, segmentIndex2) local primaryStructures = {} for segmentIndex = segmentIndex1, segmentIndex2 do recentbest.Restore() selection.DeselectAll() primaryStructures[segmentIndex] = {} primaryStructures[segmentIndex][1] = structure.GetAminoAcid(segmentIndex) selection.Select(segmentIndex) behavior.SetClashImportance(0.45) while (behavior.GetClashImportance() > 0.01) do behavior.ChangeClashImportanceByFactor(0.75) structure.MutateSidechainsSelected(1) local primaryStructure = structure.GetAminoAcid(segmentIndex) if(primaryStructures[segmentIndex][#primaryStructures[segmentIndex]] ~= primaryStructure) then primaryStructures[segmentIndex][(#primaryStructures[segmentIndex] + 1)] = primaryStructure print(segmentIndex.." "..primaryStructure.." "..behavior.GetClashImportance()) end end selection.Deselect(segmentIndex) end behavior.SetSlowFiltersDisabled(filtersDisabled) return primaryStructures end structure.Wiggle = function(startScore, doShake) local doShake = doShake if(doShake ~= false) then doShake = true end local clashImportance = behavior.GetClashImportance() local score = current.GetScore() local startScore = startScore if(type(startScore) ~= "number") then startScore = score end if(startScore < score * 0.2) then behavior.SetClashImportance(0.04) structure.WiggleAll(1) end if(startScore < score * 0.2 and doShake) then behavior.SetClashImportance(0.45) structure.ShakeSidechainsAll(3) end if(startScore < score * 0.40) then behavior.SetClashImportance(0.12) structure.WiggleAll(1) end if(startScore > score * 0.9 and startScore ~= score) then behavior.SetClashImportance(0.90) structure.WiggleAll(1) end behavior.SetClashImportance(1) --behavior.SetSlowFiltersDisabled(true) structure.WiggleAll(25) --behavior.SetSlowFiltersDisabled(false) return end Quicksave = function(slot) if(not save.CurrentSlot or save.CurrentSlot=="nil")then save.CurrentSlot = 100 end if(not slot or slot=="nil")then slot=save.CurrentSlot end if(not save[slot]) then save[slot] = {} end local AreConditionsMet = current.AreConditionsMet() local doSave = false local AreConditionsMet = current.AreConditionsMet() if(AreConditionsMet == true or save[slot]["AreConditionsMet"] ~= false) then doSave = true end if(doSave == true) then save.CurrentSlot=slot save[slot]["Score"] = current.GetEnergyScore() save[slot]["AreConditionsMet"] = AreConditionsMet save.Quicksave(slot) end return doSave end Quickload = function(slot) if(not save.CurrentSlot or save.CurrentSlot=="nil")then save.Quicksave() end if(not slot or slot=="nil")then slot=save.CurrentSlot end save.Quickload(slot) save.CurrentSlot=slot end GetScore = function() -- initializes save[currentSlot] print("11") return current.GetScore() --if(type(save[currentSlot]["Score"]) ~= "number" or score > save[currentSlot]["Score"]) then -- save[currentSlot]["Score"] = score -- save.Quicksave() -- end end selection.GetSelectedSegments = function() -- return selectedSegments[1, structureCount] = (true or false) local selectedSegments = { ["Count"] = selection.GetCount()} for segmentIndex = 1, structureCount do selectedSegments[segmentIndex] = selection.IsSelected(segmentIndex) end return selectedSegments end selection.SelectRadius = function(segmentIndex, Radius) if(type(segmentIndex) == "number" and segmentIndex > 0 and segmentIndex<= structureCount and type(Radius) == "number" and Radius > 0)then selection.Select(segmentIndex) for i = 1, structureCount do if(structure.GetDistance(i, segmentIndex) <= Radius) then selection.Select(i) end end end end selection.SelectHydrophiles = function() local AA = { ["s"] = true, ["t"] = true, ["d"] = true, ["e"] = true, ["n"] = true, ["q"] = true, ["k"] = true, ["h"] = true, ["r"] = true, ["a"] = true, ["c"] = true } selection.DeselectAll() for i = 1, structureCount do if(AA[structure.GetAminoAcid(i)] == true) then selection.Select(i) end end end printTable = function(table1) if(type(table1) == "table") then local string output = "" for segmentIndex1 = 1, structureCount do if(type(table1[segmentIndex1]) == "string" or type(table1[segmentIndex1]) == "number" or type(table1[segmentIndex1]) == "boolean") then if(len(output) > 0) then output = output..", "..tostring(table1[segmentIndex1]) else output = "{"..tostring(table1[segmentIndex1]) end end end if(len(output) > 0) then output = output.."}" end end return output end SaveRecentBest = function() if(recentbest.AreConditionsMet() ~= true or current.AreConditionsMet() == true) then recentbest.Save() Quicksave() else recentbest.Restore() Quicksave() end return end RestoreRecentBest = function() if(recentbest.AreConditionsMet() == true or save[slot]["AreConditionsMet"] ~= true) then recentbest.Restore() Quicksave() else Quickload() end return end DetectStructure = function(helixThreshold1, helixThreshold2, sheetThreshold1, sheetThreshold2, mode) local helixThreshold1=helixThreshold1 if(type(helixThreshold1) ~= "number") then helixThreshold1 = 6.5 end local helixThreshold2=helixThreshold2 if(type(helixThreshold2) ~= "number") then helixThreshold2 = 9 end local sheetThreshold1=sheetThreshold1 if(type(sheetThreshold1) ~= "number") then sheetThreshold1 = 6.5 end local sheetThreshold2=sheetThreshold2 if(type(sheetThreshold2) ~= "number") then sheetThreshold2 = 14.1 end local mode = mode local PrimaryStructure = structure.GetPrimaryStructure() local DistanceTable = structure.GetDistanceTable() local SegmentScores = current.GetSegmentEnergySubscores() local SecondaryStructure = {} --local saved_structure=save_secondary_structure(1, structureCount) for n = 1, structureCount do SecondaryStructure[n] = "L" end for n = 3, structureCount - 3 do if((DistanceTable[n - 2][n] < helixThreshold2 or DistanceTable[n][n + 2] < helixThreshold2) and (DistanceTable[n - 1][n + 2] < helixThreshold1 or DistanceTable[n - 2][n + 1] < helixThreshold1)) then SecondaryStructure[n] = "H" end end for n = 3, structureCount - 3 do for m = n + 3, structureCount - 1 do if(SecondaryStructure[n] ~= "H" and SecondaryStructure[m] ~= "H" and DistanceTable[n][m] < sheetThreshold1)then SecondaryStructure[n] = "E" SecondaryStructure[m] = "E" end end end for n = 2, structureCount - 1 do -- E[HL]E and [EL]H[EL] and [HL]E[HL] if(SecondaryStructure[n - 1] == "E" and SecondaryStructure[n] ~= "E" and SecondaryStructure[n + 1] == "E")then SecondaryStructure[n] = "E" end if(SecondaryStructure[n - 1] ~= "H" and SecondaryStructure[n] == "H" and SecondaryStructure[n + 1] ~= "H")then SecondaryStructure[n] = "L" end if(SecondaryStructure[n - 1] ~= "E" and SecondaryStructure[n] == "E" and SecondaryStructure[n + 1] ~= "E")then SecondaryStructure[n] = "L" end end for n = 1, structureCount - 4 do -- EHHHE Hairpin and EHHE Hairpin and [EL]HH[EL] if(SecondaryStructure[n] == "E" and SecondaryStructure[n + 1] == "H" and SecondaryStructure[n + 2] == "H" and SecondaryStructure[n + 3] == "H" and SecondaryStructure[n + 4] == "E")then SecondaryStructure[n + 1] = "A" SecondaryStructure[n + 2] = "A" SecondaryStructure[n + 3] = "A" end if(SecondaryStructure[n] ~= "H" and SecondaryStructure[n + 1] == "H" and SecondaryStructure[n + 2] == "H" and SecondaryStructure[n + 3] ~= "H")then if(SecondaryStructure[n] == "E" and SecondaryStructure[n + 3] == "E")then SecondaryStructure[n + 1] = "A" SecondaryStructure[n + 2] = "A" else SecondaryStructure[n + 1] = "L" SecondaryStructure[n + 2] = "L" end end end for n = 1, structureCount do -- Score if(SecondaryStructure[n] == "E" and (SegmentScores[n]["backbone"] < ( - 8.1) or SegmentScores[n]["backbone"] > 6.1))then SecondaryStructure[n] = "L" end if(PrimaryStructure[n] ~= "g" and SecondaryStructure[n] == "H" and (SegmentScores[n]["backbone"] < ( - 4.1) or SegmentScores[n]["backbone"] > 4.2))then SecondaryStructure[n] = "L" end end for n = 1, structureCount - 3 do -- [EL]HH[EL] if(SecondaryStructure[n] ~= "H" and SecondaryStructure[n + 1] == "H" and SecondaryStructure[n + 2] == "H" and SecondaryStructure[n + 3] ~= "H")then SecondaryStructure[n + 1] = "L" SecondaryStructure[n + 2] = "L" end end for n = 2, structureCount - 1 do -- [EL]H[EL] and [HL]E[HL] if(SecondaryStructure[n - 1] ~= "H" and SecondaryStructure[n] == "H" and SecondaryStructure[n + 1] ~= "H")then SecondaryStructure[n] = "L" end if(SecondaryStructure[n - 1] ~= "E" and SecondaryStructure[n] == "E" and SecondaryStructure[n + 1] ~= "E")then SecondaryStructure[n] = "L" end end return SecondaryStructure end MutateSidechainsFromSuggestions = function(suggestedStructure) local suggestedStructure = suggestedStructure if(suggestedStructure and type(suggestedStructure) == "table") then for segmentIndex = 1, structureCount do recentbest.Restore() local sidechain = structure.GetAminoAcid(segmentIndex) if(suggestedStructure[segmentIndex] and type(suggestedStructure[segmentIndex]) == "table") then for m = 1, #suggestedStructure[segmentIndex] do if(suggestedStructure[segmentIndex][m] and suggestedStructure[segmentIndex][m] ~= sidechain and structure.PrimaryStructureType[suggestedStructure[segmentIndex][m]] ~= "nil") then local score = GetScore() structure.SetAminoAcid(segmentIndex, suggestedStructure[segmentIndex][m]) structure.Wiggle(score) end end end end end return end BruteForce = function(doWiggle, AnalysisLevel, allowedAminoAcids) recentbest.Restore() recentbest.Save() band.DeleteAll() behavior.SetClashImportance(1) local startScore = current.GetEnergyScore() local highScore = current.GetEnergyScore() print("\nstartScore="..highScore) local doWiggle = doWiggle if(doWiggle ~= true) then doWiggle = false else print("doWiggle="..tostring(doWiggle)) end local AnalysisLevel = AnalysisLevel if(type(AnalysisLevel) ~= "number") then AnalysisLevel = 0 else print("AnalysisLevel="..tostring(AnalysisLevel)) end if(selection.GetCount() == 0) then selection.SelectAll() end local selectedSegments = selection.GetSelectedSegments() local originalSelectedSegments = selection.GetSelectedSegments() print("selectedSegments.Count="..selectedSegments.Count) selection.DeselectAll() local segmentScores = {} for segmentIndex = 1, structureCount do segmentScores[segmentIndex] = current.GetSegmentEnergyScore(segmentIndex) end local primaryStructures = {"d","e","h","k","n","q","r","s","t","w","y"} local allowNonBondingAminoAcids = allowNonBondingAminoAcids if(allowNonBondingAminoAcids ~= false) then allowNonBondingAminoAcids = true primaryStructures = {"a","d","e","f","g","h","i","k","l","m","n","p","q","r","s","t","v","w","y"} else print("allowNonBondingAminoAcids="..tostring(allowNonBondingAminoAcids)) end while selectedSegments.Count > 0 do for segmentIndex = 1, structureCount do if(selectedSegments[segmentIndex] == true) then highScore = current.GetEnergyScore() for i = 1, #primaryStructures do if(structure.CanMutate(segmentIndex, primaryStructures[i]) and structure[n]["ApprovedResidues"] == true) then structure.SetAminoAcid(segmentIndex, primaryStructures[i]) local newScore = current.GetEnergyScore() if(newScore > (highScore + 1)) then recentbest.Save() recentbest.Restore() recentbest.Save() print(segmentIndex.." "..structure.GetAminoAcid(segmentIndex).." "..newScore) highScore = current.GetEnergyScore() if(AnalysisLevel > 0) then newSegmentScores = {} for newSegmentIndex = 1, structureCount do newSegmentScores[newSegmentIndex] = current.GetSegmentEnergyScore(newSegmentIndex) if(math.abs(math.abs(newSegmentScores[newSegmentIndex]) - math.abs(segmentScores[newSegmentIndex])) > 1) then print(" "..newSegmentIndex.." "..segmentScores[newSegmentIndex].." "..newSegmentScores[newSegmentIndex]) segmentScores[newSegmentIndex] = newSegmentScores[newSegmentIndex] selectedSegments[newSegmentIndex] = true end end end end local rotamerCount = rotamer.GetCount(segmentIndex) if(rotamerCount > 0) then for position = 1, rotamerCount do if(structure.CanMutate(segmentIndex, primaryStructures[i])) then structure.SetAminoAcid(segmentIndex, primaryStructures[i]) if(position <= rotamer.GetCount(segmentIndex)) then rotamer.SetRotamer(segmentIndex, position) if(doWiggle == true) then selection.SelectRange(math.max(1, segmentIndex - 2), math.min(structureCount, segmentIndex + 2)) newSegmentScores = {} for newSegmentIndex = 1, structureCount do newSegmentScores[newSegmentIndex] = current.GetSegmentEnergyScore(newSegmentIndex) if(math.abs(math.abs(newSegmentScores[newSegmentIndex]) - math.abs(segmentScores[newSegmentIndex])) > 1) then selection.Select(newSegmentIndex) end end structure.WiggleSelected(12, true) selection.DeselectAll() end end newScore = current.GetEnergyScore() if(newScore > (highScore + 1)) then print(segmentIndex.." "..structure.GetAminoAcid(segmentIndex).." "..position.." "..newScore) highScore = current.GetEnergyScore() if(AnalysisLevel > 0) then newSegmentScores = {} for newSegmentIndex = 1, structureCount do newSegmentScores[newSegmentIndex] = current.GetSegmentEnergyScore(newSegmentIndex) if(math.abs(math.abs(newSegmentScores[newSegmentIndex]) - math.abs(segmentScores[newSegmentIndex])) > 1) then print(" "..newSegmentIndex.." "..segmentScores[newSegmentIndex].." "..newSegmentScores[newSegmentIndex]) segmentScores[newSegmentIndex] = newSegmentScores[newSegmentIndex] selectedSegments[newSegmentIndex] = true end end end end end selection.Select(segmentIndex) structure.MutateSidechainsSelected(1) selection.Deselect(segmentIndex) recentbest.Restore() recentbest.Save() end end end recentbest.Restore() recentbest.Save() end end selectedSegments[segmentIndex] = false selectedSegments.Count = selectedSegments.Count - 1 segmentIndex = structureCount + 1 end end for segmentIndex = 1, structureCount do if(originalSelectedSegments[segmentIndex] == true) then selection.Select(segmentIndex) end end end optimize = {} optimize.Rotamer = function(segmentIndex) if(rotamer.GetCount() > 0) then local position = 1 while position <= rotamer.GetCount() do rotamer.SetRotamer(segmentIndex, position) structure.WiggleAll(20) end end end function round(num, idp) local mult = 10^(idp or 0) return math.floor(num * mult + 0.5) / mult end structure.CreateAction = function(Actions) local Actions = Actions if(type(Actions) ~= "table") then Actions = {} end local ActionDialog = dialog.CreateDialog(AFK.ClientName) ActionDialog.Label = dialog.AddLabel("Add additional actions") ActionDialog.Slider1 = dialog.AddSlider("Iterations", 3, 1, 25, 0) ActionDialog.Slider2 = dialog.AddSlider("Clashing Importance", 1, 0, 1, 2) ActionDialog.Label1 = dialog.AddLabel("A radius of 0 performs the action globally") ActionDialog.Slider3 = dialog.AddSlider("Radius", 20, 1, 250, 0) if(#Actions > 0)then ActionDialog.Label2 = dialog.AddLabel("") local count = 0 for action = math.max(1, #Actions - 4), #Actions do count = count + 1 if(count == 1) then ActionDialog.Label3 = dialog.AddLabel(action..") "..Actions[action]["Type"].."("..Actions[action]["Iterations"]..", "..Actions[action]["ClashImportance"]..", "..Actions[action]["Radius"]..")") elseif(count == 2)then ActionDialog.Label4 = dialog.AddLabel(action..") "..Actions[action]["Type"].."("..Actions[action]["Iterations"]..", "..Actions[action]["ClashImportance"]..", "..Actions[action]["Radius"]..")") elseif(count == 3)then ActionDialog.Label5 = dialog.AddLabel(action..") "..Actions[action]["Type"].."("..Actions[action]["Iterations"]..", "..Actions[action]["ClashImportance"]..", "..Actions[action]["Radius"]..")") elseif(count == 4)then ActionDialog.Label6 = dialog.AddLabel(action..") "..Actions[action]["Type"].."("..Actions[action]["Iterations"]..", "..Actions[action]["ClashImportance"]..", "..Actions[action]["Radius"]..")") elseif(count == 5)then ActionDialog.Label7 = dialog.AddLabel(action..") "..Actions[action]["Type"].."("..Actions[action]["Iterations"]..", "..Actions[action]["ClashImportance"]..", "..Actions[action]["Radius"]..")") end end end ActionDialog.Shake = dialog.AddButton("Shake", 1) ActionDialog.Wiggle = dialog.AddButton("Wiggle", 2) ActionDialog.Mutate = dialog.AddButton("Mutate", 3) ActionDialog.Done = dialog.AddButton("Done", 4) ActionDialog.Cancel = dialog.AddButton("Cancel", 0) showDialog1 = dialog.Show(ActionDialog) if(showDialog1 == 1 or showDialog1 == 2 or showDialog1 == 3) then Actions[(#Actions + 1)] = {} Actions[#Actions]["Iterations"] = ActionDialog.Slider1.value Actions[#Actions]["ClashImportance"] = ActionDialog.Slider2.value Actions[#Actions]["Radius"] = ActionDialog.Slider3.value if(showDialog1 == 1) then Actions[#Actions]["Type"] = "Shake" elseif(showDialog1 == 2) then Actions[#Actions]["Type"] = "Wiggle" elseif(showDialog1 == 3) then Actions[#Actions]["Type"] = "Mutate" end return(structure.CreateAction(Actions)) elseif(showDialog1 == 4) then return Actions else return 0 end end structure.PerformActions = function(segmentIndex, Actions) local Actions = Actions if(type(segmentIndex) == "number" and segmentIndex > 0 and segmentIndex <= structureCount and type(Actions) == "table") then if(type(Actions["Type"]) == "string") then if(type(Actions["ClashImportance"]) ~= "number" or Actions["ClashImportance"] < 0 or Actions["ClashImportance"] > 1)then Actions["ClashImportance"] = 1 end if(type(Actions["Radius"]) ~= "number" or Actions["Radius"] < 0)then Actions["Radius"] = 0 end local ClashImportance = behavior.GetClashImportance() if(ClashImportance ~= Actions["ClashImportance"])then behavior.SetClashImportance(Actions["ClashImportance"]) end if(Actions["Radius"] > 0)then selection.SelectRadius(segmentIndex, Actions["Radius"]) if(Actions["Type"] == "Shake")then selection.Deselect(segmentIndex) if(type(Actions["Iterations"]) ~= "number" or Actions["Iterations"] < 1 or Actions["Iterations"] > 100)then Actions["Iterations"] = 3 end if(Actions["Radius"] > 0)then structure.ShakeSidechainsSelected(Actions["Iterations"]) else structure.ShakeSidechainsAll(Actions["Iterations"]) end elseif(Actions["Type"] == "Wiggle") then if(type(Actions["Iterations"]) ~= "number" or Actions["Iterations"] < 1 or Actions["Iterations"] > 100)then Actions["Iterations"] = 25 end if(Actions["Radius"] > 0)then structure.LocalWiggleSelected(Actions["Iterations"]) else structure.WiggleAll(Actions["Iterations"]) end elseif(Actions["Type"] == "Mutate")then selection.Deselect(segmentIndex) if(type(Actions["Iterations"]) ~= "number" or Actions["Iterations"] < 1 or Actions["Iterations"] > 100)then Actions["Iterations"] = 3 end if(Actions["Radius"] > 0)then structure.MutateSidechainsSelected(Actions["Iterations"]) else structure.MutateSidechainsAll(Actions["Iterations"]) end end if(Actions["Radius"] > 0)then selection.DeselectAll() end if(ClashImportance ~= Actions["ClashImportance"])then behavior.SetClashImportance(ClashImportance) end end else for action = 1, #Actions do structure.PerformActions(segmentIndex, Actions[action]) end end end end rotamer.Optimize = function(segmentIndex, doRepeat, Actions) local rotamerCount = rotamer.GetCount(segmentIndex) local AA = structure.GetAminoAcid(segmentIndex) local position = math.min(1, rotamerCount) local HighScore = current.GetEnergyScore() local newScore = 0 while position > 0 and rotamerCount > 0 and position <= rotamerCount do if(structure.CanMutate(segmentIndex, AA) == true) then structure.SetAminoAcid(segmentIndex, AA) end rotamer.SetRotamer(segmentIndex, position) structure.PerformActions(segmentIndex, Actions) rotamerCount = rotamer.GetCount(segmentIndex) newScore = current.GetEnergyScore() if(newScore > HighScore + 0.1 and doRepeat == true)then position = 0 end position = position + 1 recentbest.Restore() HighScore = current.GetEnergyScore() end end structure.BruteForceAmino = function(segmentIndex, doRotamer, doRepeat, Actions) if(type(segmentIndex) == "number" and segmentIndex > 0 and segmentIndex <= structureCount)then Residues = {"s", "t", "d", "e", "n", "q", "k", "h", "r", "v", "w", "y", "l", "m", "p", "i", "a", "c", "f", "g"} for i = 1, #Residues do if(structure[segmentIndex]["ApprovedResidues"][Residues[i]] == true or structure.GetAminoAcid(segmentIndex) == Residues[i]) then if(structure[segmentIndex]["ApprovedResidues"][Residues[i]] == true) then structure.SetAminoAcid(segmentIndex, Residues[i]) end if(doRotamer == true) then rotamer.Optimize(segmentIndex, doRepeat, Actions) else structure.PerformActions(segmentIndex, Actions) end recentbest.Restore() end end end end --current.GetGlycineBonding = function() -- recentbest.Save() -- local GlycineBondingTable = {} -- selection.SelectAll() -- structure.SetAminoAcidSelected("g") -- selection.DeselectAll() -- for i = 1, structureCount do -- GlycineBondingTable[i] = current.GetSegmentEnergySubscore(i, "bonding") -- end -- recentbest.Restore() -- end GetHBondNetwork = function() HBondNetwork = {} end current.GetSlowFilterScore = function() save.Quicksave(99) local SlowFiltersDisabled = behavior.GetSlowFiltersDisabled() if(SlowFiltersDisabled == true) then behavior.SetSlowFiltersDisabled(false) end save.Quickload(99) local filterScore = current.GetEnergyScore() behavior.SetSlowFiltersDisabled(true) save.Quickload(99) filterScore = filterScore - current.GetEnergyScore() if(SlowFiltersDisabled ~= true) then behavior.SetSlowFiltersDisabled(false) save.Quickload(99) end return filterScore end structure.CullPrimaryStructure = function(segmentIndex, scoreThreshold, scoreType) if(type(segmentIndex) == "number") then local scoreThreshold = scoreThreshold recentbest.Save() if(type(scoreThreshold) ~= "number") then scoreThreshold = 0.1 end local amino = structure.GetAminoAcid(segmentIndex) if(amino ~= "g") then local filterScore = 0 if(scoreType == "filter" or scoreType == "Filter") then filterScore = current.GetSlowFilterScore() else filterScore = current.GetEnergyScore() end structure.SetAminoAcid(segmentIndex, "g") if(scoreType == "filter" or scoreType == "Filter") then filterScore = math.abs(filterScore - current.GetSlowFilterScore()) else filterScore = math.abs(filterScore - current.GetEnergyScore()) end if(filterScore < scoreThreshold)then print("Removing "..segmentIndex.." "..amino) recentbest.Save() amino = "g" else recentbest.Restore() end end elseif(segmentIndex == "table") then local SelectedSegments = segmentIndex for segmentIndex = 1, #SelectedSegments do if(SelectedSegments[segmentIndex] == true) then structure.CullPrimaryStructure(i, scoreThreshold, scoreType) end end else for i = 1, structureCount do local AA = structure.GetAminoAcid(i) structure.CullPrimaryStructure(i, scoreThreshold, scoreType) local newAA = structure.GetAminoAcid(i) if(AA ~= newAA and newAA ~= "g") then print(i.." "..AA.."-"..newAA..". current.GetEnergyScore()="..current.GetEnergyScore()) end end end end dialog.ChooseAminoAcid = function(SelectedSegments) local Residues = {"s", "t", "d", "e", "n", "q", "k", "h", "r", "v", "w", "y", "l", "m", "p", "i", "a", "c", "f", "g"} local Choices = { ["s"] = false, ["t"] = false, ["d"] = false, ["e"] = false, ["n"] = false, ["q"] = false, ["k"] = false, ["h"] = false, ["r"] = false, ["v"] = false, ["w"] = false, ["y"] = false, ["l"] = false, ["m"] = false, ["p"] = false, ["i"] = false, ["a"] = false, ["c"] = false, ["f"] = false, ["g"] = false } for i = 1, structureCount do if(selection.IsSelected(i) == true) then for j = 1, #Residues do if(Choices[Residues[j]] ~= true and structure.CanMutate(i, Residues[j])) then Choices[Residues[j]] = true end end end end Dialog1 = dialog.CreateDialog(AFK.ClientName) Dialog1.Label1 = dialog.AddLabel("Choose an Amino Acid") for j = 1, #Residues do if(Choices[Residues[j]] == true) then Dialog1[Residues[j]] = dialog.AddButton(Residues[j], j) end end Dialog1.Cancel = dialog.AddButton("Cancel", 0) local AA = dialog.Show(Dialog1) if(AA ~= 0) then return Residues[AA] else return 0 end end structure.HBuster = function(SelectedSegments, threshold, doRotamer, doRepeat, Actions) if(type(SelectedSegments) == "table") then local filterScore = current.GetSlowFilterScore() local threshold = threshold if(type(threshold) ~= "number")then threshold = 5 end local HighScore = current.GetEnergyScore() local NewScore = (-1 * math.abs(HighScore) - 1) while math.abs(HighScore - NewScore) > threshold and HighScore > NewScore do HighScore = current.GetEnergyScore() print("Script Started "..round(HighScore, 3)) for segmentIndex = 1, structureCount do if(SelectedSegments[segmentIndex] == true) then local AA = structure.GetAminoAcid(segmentIndex) structure.BruteForceAmino(segmentIndex, doRotamer, doRepeat, Actions)--, true) recentbest.Restore() --structure.CullPrimaryStructure(segmentIndex) local newAA = structure.GetAminoAcid(segmentIndex) local newScore = current.GetEnergyScore() if(AA ~= newAA or round(HighScore, 3) ~= round(newScore, 3)) then print(segmentIndex.." "..AA.."-"..newAA.." EnergyScore="..round(newScore, 3)) end end end NewScore = current.GetEnergyScore() if(HighScore ~= NewScore) then structure.Wiggle() NewScore = current.GetEnergyScore() end end else Dialog1 = dialog.CreateDialog(AFK.ClientName) if(selection.GetCount() > 0)then Dialog1.Checkbox1 = dialog.AddCheckbox("Focus on selected segments", true) end Dialog1.Label1 = dialog.AddLabel("*HBuster will attempt to optimize") Dialog1.Label2 = dialog.AddLabel(" Hydrogen Bond Network puzzles") Dialog1.Label3 = dialog.AddLabel("*Cull will remove an Amino Acid which does not") Dialog1.Label4 = dialog.AddLabel(" significantly contribute to scoring") Dialog1.Label3 = dialog.AddLabel("*Seed will attempt to seed Glycine") Dialog1.Label4 = dialog.AddLabel(" with the selected AminoAcids") Dialog1.HBuster = dialog.AddButton("HBuster", 1) Dialog1.Cull = dialog.AddButton("Cull(major)", 2) Dialog1.Seed = dialog.AddButton("Seed", 3) Dialog1.Cancel = dialog.AddButton("Cancel", 0) showDialog1 = dialog.Show(Dialog1) if(type(showDialog1) == "number" and showDialog1 > 0) then if(not Dialog1.Checkbox1 or Dialog1.Checkbox1.value ~= true) then selection.SelectAll() end if(showDialog1 == 1) then if (structure.ResetApprovedResidues() == 1) then local Dialog2 = dialog.CreateDialog(AFK.ClientName) Dialog2.Checkbox1 = dialog.AddCheckbox("Check all Rotamer positions", true) Dialog2.Label1 = dialog.AddLabel("Repeat until point gain falls below") Dialog2.Slider1 = dialog.AddSlider("Point threshold", 5, 0.1, 40, 1) Dialog2.OK = dialog.AddButton("OK", 1) Dialog2.Cancel = dialog.AddButton("Cancel", 0) local showDialog2 = dialog.Show(Dialog2) local Actions = structure.CreateAction() if(showDialog2 == 1 and Actions ~= 0) then local SelectedSegments = selection.GetSelectedSegments() selection.DeselectAll() structure.Wiggle() if(Dialog2.Checkbox1 and Dialog2.Checkbox1.value == true) then structure.HBuster(SelectedSegments, Dialog2.Slider1.value, true, false, Actions) else structure.HBuster(SelectedSegments, Dialog2.Slider1.value, false, false, Actions) end if(SelectedSegments.Count == structureCount) then selection.DeselectAll() else for i = 1, structureCount do if(SelectedSegments[i] == true) then selection.Select(i) end end end else showDialog1 = 0 showDialog2 = 0 end else showDialog1 = 0 end elseif(showDialog1 == 2) then local Dialog2 = dialog.CreateDialog(AFK.ClientName) Dialog2.Label1 = dialog.AddLabel("Segment is removed if it's score is less than") Dialog2.Slider1 = dialog.AddSlider("Point threshold", 0.1, 0.1, 40, 1) Dialog2.Checkbox1 = dialog.AddCheckbox("Use GetSlowFilterScore() instead of GetEnergyScore()", true) Dialog2.OK = dialog.AddButton("OK", 1) Dialog2.Cancel = dialog.AddButton("Cancel", 0) local showDialog2 = dialog.Show(Dialog2) if(showDialog2 == 1) then local SelectedSegments = selection.GetSelectedSegments() selection.DeselectAll() if(Dialog2.Checkbox1 and Dialog2.Checkbox1.value == true) then structure.CullPrimaryStructure(SelectedSegments, Dialog2.Slider1.value, "Filter") else structure.CullPrimaryStructure(SelectedSegments, Dialog2.Slider1.value) end if(SelectedSegments.Count == structureCount) then selection.DeselectAll() else for i = 1, structureCount do if(SelectedSegments[i] == true) then selection.Select(i) end end end else showDialog2 = 0 showDialog1 = 0 end elseif(showDialog1 == 3) then local SelectedSegments = selection.GetSelectedSegments() local AA = dialog.ChooseAminoAcid(SelectedSegments) if (type(AA) == "string") then for i = 1, structureCount do if(SelectedSegments[i] == true) then if(structure.GetAminoAcid(i) ~= "g") then selection.Deselect(i) else print("Seeding "..i.." "..AA) end end end structure.SetAminoAcidSelected(AA) if(SelectedSegments.Count == structureCount) then selection.DeselectAll() else for i = 1, structureCount do if(SelectedSegments[i] == true) then selection.Select(i) end end end else showDialog2 = 0 showDialog1 = 0 end else showDialog1 = 0 end end end end puzzle[2000714] = function() -- 1087: Hydrogen Bond Network Filter --local GlycineBondingTable = current.GetGlycineBonding() structure.HBuster() end puzzle[2000743] = function() -- if (structure.ResetApprovedResidues() == 1) then print("HBuster -- "..GetTimeStamp()) recentbest.Save() for n=1,structureCount do selection.Select(n) local currentScore = current.GetEnergyScore() local startScore = current.GetEnergyScore() local highScore = current.GetEnergyScore() local originalSelectedSegments = selection.GetSelectedSegments() local selectedSegments = selection.GetSelectedSegments() if(selection.GetCount() == 1) then local selectedIndex = 0 for segmentIndex = 1, structureCount do if(selectedIndex == 0 and originalSelectedSegments[segmentIndex] == true) then selectedIndex = segmentIndex end end local primaryStructures = {} if(selectedIndex > 0) then local tempPrimaryStructures = {"a","d","e","f","g","h","i","k","l","m","n","p","q","r","s","t","v","w","y"} for tempPrimaryStructure = 1, #tempPrimaryStructures do if((structure.CanMutate(selectedIndex, tempPrimaryStructures[tempPrimaryStructure]) == true and structure[selectedIndex]["ApprovedResidues"][tempPrimaryStructures[tempPrimaryStructure]] == true) or structure.GetAminoAcid(selectedIndex) == tempPrimaryStructures[tempPrimaryStructure]) then primaryStructures[(#primaryStructures + 1)] = tempPrimaryStructures[tempPrimaryStructure] end end end if(#primaryStructures > 0) then for primaryStructure = 1, #primaryStructures do local segmentScores = current.GetSegmentEnergyScores() structure.SetAminoAcid(selectedIndex, primaryStructures[primaryStructure]) selection.SelectRange(math.max(1, selectedIndex - 2), math.min(structureCount, selectedIndex + 2)) newSegmentScores = {} for newSegmentIndex = 1, structureCount do newSegmentScores[newSegmentIndex] = current.GetSegmentEnergyScore(newSegmentIndex) if(math.abs(math.abs(newSegmentScores[newSegmentIndex]) - math.abs(segmentScores[newSegmentIndex])) > 1) then selection.Select(newSegmentIndex) end end local affectedSegments = selection.GetSelectedSegments() for newSegmentIndex = 1, structureCount do if(selection.IsSelected(newSegmentIndex) == true) then selection.SelectRange(math.max(1, newSegmentIndex - 5), math.min(structureCount, newSegmentIndex + 5)) end end local wiggleSegments = selection.GetSelectedSegments() behavior.SetSlowFiltersDisabled(true) --structure.WiggleSelected(12, true) structure.WiggleAll(12, true) selection.DeselectAll() for newSegmentIndex = 1, structureCount do if(affectedSegments[newSegmentIndex] == true) then selection.Select(newSegmentIndex) end end structure.MutateSidechainsSelected(1) selection.DeselectAll() behavior.SetSlowFiltersDisabled(false) recentbest.Restore() end end elseif(selection.GetCount() > 1) then local SelectedSegments = selection.GetSelectedSegments() selection.DeselectAll() for i = 1, structure.GetCount() do if(SelectedSegments[i] == true) then selection.Select(i) puzzle[2000743]() selection.Deselect(i) end end else end end end -- local scoreRatio = {} -- local scoreRatio2 = {} -- local ratioTotal = 0 -- for segmentIndex = 1, structureCount do -- scoreRatio[segmentIndex] = current.GetSegmentEnergyScore(segmentIndex) -- if(structure.CanMutate(segmentIndex, "g") and structure[n]["ApprovedResidues"] == true) then -- structure.SetAminoAcid(segmentIndex, "g") -- end -- local score = math.max(scoreRatio[segmentIndex], current.GetSegmentEnergyScore(segmentIndex)) - math.min(scoreRatio[segmentIndex], current.GetSegmentEnergyScore(segmentIndex)) -- scoreRatio[segmentIndex] = score -- scoreRatio2[segmentIndex] = score -- ratioTotal = ratioTotal + math.abs(score) -- recentbest.Restore() -- recentbest.Save() -- end -- function comp(w1,w2) -- if w1 > w2 then -- return true -- end -- end -- table.sort(scoreRatio, comp) -- local scoreRatio3 = {} -- for i,n in ipairs(scoreRatio) do -- for j = 1, structureCount do -- if(scoreRatio2[j] == scoreRatio[i]) then -- scoreRatio3[(#scoreRatio3 + 1)] = j -- end -- end -- end -- for segmentIndex = 1, #scoreRatio3 do -- selection.Select(scoreRatio3[segmentIndex]) -- rotamer.BruteForce(true) -- end --if(currentScore > 2) then -- while oldScore + 1 < currentScore do -- oldScore = current.GetEnergyScore() -- rotamer.BruteForce(true, 1) -- currentScore = current.GetEnergyScore() -- end -- end end puzzle[2000790] = function() -- --selection.SelectHydrophiles() selection.DeselectAll() for i = 1, structureCount do local AA = structure.GetAminoAcid(i) selection.Select(i) structure.MutateSidechainsSelected(1) selection.Deselect(i) if(AA ~= structure.GetAminoAcid(i)) then print(i.." "..AA.." "..structure.GetAminoAcid(i)) end end end puzzle[2000793] = function() -- DevPrev Hydrogen Bond Network Asymmetric Test structure.HBuster() end puzzle[2000800] = function() -- Hydrogen Bond Network Asymmetric Test structure.HBuster() end print("Initializing. Please Wait...") --if(puzzle[puzzle.GetPuzzleID()] and puzzle[puzzle.GetPuzzleID()] ~= "nil" and type(puzzle[puzzle.GetPuzzleID()]) == "function") then -- puzzle[puzzle.GetPuzzleID()]() --else -- local showDialog = dialog.Show(dialog.OpeningDialog) -- end structure.HBuster() print("Script Completed") --rotamer.GetCurrentRotamer() --band.Add(segmentOrigin, segmentXAxis, segmentYAxis, rho, theta, phi, [atomIndexOrigin], [atomIndexXAxis], [atomIndexYAxis]) --for n=2, structureCount - 1 do --band.Add(n, n - 1, n + 1, 5, math.rad(30), math.rad(45)) --end --current.Mutate = function() -- local selectedSegments = selection.GetSelectedSegments() -- if(#selectedSegments == 1) -- local primaryStructure = structure.GetAminoAcid(selectedSegments[1]) -- end -- -- structure.IsMutable(integer segmentIndex) -- end --function mutate_one_by_one() -- recentbest.Restore() -- local score = current.GetEnergyScore() -- quick_save() -- local mutate_score=current.GetEnergyScore() -- b_do_mutate=0 -- for n=1,gsc do -- for m=1,20 do -- if(structure[n]["ApprovedResidues"][sidechain_choices(m)] == true) then -- print("Mutating segment "..n) -- selection.DeselectAll() -- recentbest.Restore() -- recent_best_score=current.GetEnergyScore() -- quick_save() -- selection.Select(n) -- structure.SetAminoAcidSelected(sidechain_choices(m)) -- if(current.GetEnergyScore()~=mutate_score)then -- selection.Deselect(n) -- ci_wiggler(mutate_score,ci_shake_variable,ci_wiggle_variable,1,gsc) -- end -- end -- end -- end -- b_do_mutate=1 --end --for segmentIndex = 3, structureCount do -- selection.Select(segmentIndex) -- local primaryStructure = {"a","d","e","f","g","h","i","k","l","m","n","p","q","r","s","t","v","w","y"} -- --local primaryStructure = {"d","e","h","k","n","q","r","s","t","w","y"} -- for i = 1, #primaryStructure do -- if(selection.IsSelected(segmentIndex) and structure.CanMutate(segmentIndex, primaryStructure[i]) and structure[n]["ApprovedResidues"] == true) then -- structure.SetAminoAcid(segmentIndex, primaryStructure[i]) -- local rotamerCount = rotamer.GetCount(segmentIndex) -- -- if(rotamerCount > 0) then -- for position = 1, rotamerCount do -- rotamer.SetRotamer(segmentIndex, position) -- end -- end -- end -- absolutebest.Restore() -- end -- end --function get_aa_helix_propensity(amino_acid) -- if(amino_acid=="a")then return 142 -- elseif(amino_acid=="r")then return 98 -- elseif(amino_acid=="d")then return 101 -- elseif(amino_acid=="n")then return 67 -- elseif(amino_acid=="c")then return 70 -- elseif(amino_acid=="e")then return 151 -- elseif(amino_acid=="q")then return 111 -- elseif(amino_acid=="g")then return 57 -- elseif(amino_acid=="h")then return 100 -- elseif(amino_acid=="i")then return 108 -- elseif(amino_acid=="l")then return 121 -- elseif(amino_acid=="k")then return 114 -- elseif(amino_acid=="m")then return 145 -- elseif(amino_acid=="f")then return 113 -- elseif(amino_acid=="p")then return 57 -- elseif(amino_acid=="s")then return 77 -- elseif(amino_acid=="t")then return 83 -- elseif(amino_acid=="w")then return 108 -- elseif(amino_acid=="y")then return 59 -- elseif(amino_acid=="v")then return 106 -- else return "error retrieving structure propensity for "..amino_acid.."." -- end --end --function get_aa_sheet_propensity(amino_acid) -- if(amino_acid=="a")then return 83 -- elseif(amino_acid=="r")then return 93 -- elseif(amino_acid=="d")then return 51 -- elseif(amino_acid=="n")then return 89 -- elseif(amino_acid=="c")then return 119 -- elseif(amino_acid=="e")then return 37 -- elseif(amino_acid=="q")then return 110 -- elseif(amino_acid=="g")then return 75 -- elseif(amino_acid=="h")then return 87 -- elseif(amino_acid=="i")then return 160 -- elseif(amino_acid=="l")then return 130 -- elseif(amino_acid=="k")then return 74 -- elseif(amino_acid=="m")then return 105 -- elseif(amino_acid=="f")then return 138 -- elseif(amino_acid=="p")then return 55 -- elseif(amino_acid=="s")then return 75 -- elseif(amino_acid=="t")then return 119 -- elseif(amino_acid=="w")then return 137 -- elseif(amino_acid=="y")then return 147 -- elseif(amino_acid=="v")then return 170 -- else return "error retrieving structure propensity for "..amino_acid.."." -- end --end --function get_aa_loop_propensity(amino_acid) -- if(amino_acid=="a")then return 66 -- elseif(amino_acid=="r")then return 95 -- elseif(amino_acid=="d")then return 146 -- elseif(amino_acid=="n")then return 156 -- elseif(amino_acid=="c")then return 119 -- elseif(amino_acid=="e")then return 74 -- elseif(amino_acid=="q")then return 98 -- elseif(amino_acid=="g")then return 156 -- elseif(amino_acid=="h")then return 95 -- elseif(amino_acid=="i")then return 47 -- elseif(amino_acid=="l")then return 59 -- elseif(amino_acid=="k")then return 101 -- elseif(amino_acid=="m")then return 80 -- elseif(amino_acid=="f")then return 80 -- elseif(amino_acid=="p")then return 152 -- elseif(amino_acid=="s")then return 143 -- elseif(amino_acid=="t")then return 95 -- elseif(amino_acid=="w")then return 96 -- elseif(amino_acid=="y")then return 114 -- elseif(amino_acid=="v")then return 50 -- else return "error retrieving structure propensity for "..amino_acid.."." -- end --end --function get_dasgupta_structures() -- local loop = { -- ["a"] = 66, -- ["r"] = 95, -- ["d"] = 146, -- ["n"] = 156, -- ["c"] = 119, -- ["e"] = 74, -- ["q"] = 98, -- ["g"] = 156, -- ["h"] = 95, -- ["i"] = 47, -- ["l"] = 59, -- ["k"] = 101, -- ["m"] = 80, -- ["f"] = 80, -- ["p"] = 152, -- ["s"] = 143, -- ["t"] = 95, -- ["w"] = 96, -- ["y"] = 114, -- ["v"] = 50 -- } -- local sheet = { -- ["a"] = 83, -- ["r"] = 93, -- ["d"] = 51, -- ["n"] = 89, -- ["c"] = 119, -- ["e"] = 37, -- ["q"] = 110, -- ["g"] = 75, -- ["h"] = 87, -- ["i"] = 160, -- ["l"] = 130, -- ["k"] = 74, -- ["m"] = 105, -- ["f"] = 138, -- ["p"] = 55, -- ["s"] = 75, -- ["t"] = 119, -- ["w"] = 137, -- ["y"] = 147, -- ["v"] = 170 -- } -- local helix = { -- ["a"] = 142, -- ["r"] = 98, -- ["d"] = 101, -- ["n"] = 67, -- ["c"] = 70, -- ["e"] = 151, -- ["q"] = 111, -- ["g"] = 57, -- ["h"] = 100, -- ["i"] = 108, -- ["l"] = 121, -- ["k"] = 114, -- ["m"] = 145, -- ["f"] = 113, -- ["p"] = 57, -- ["s"] = 77, -- ["t"] = 83, -- ["w"] = 108, -- ["y"] = 59, -- ["v"] = 106 -- } -- -- --save_primary_structure(1,gsc) -- local primary_structure = structure.GetPrimaryStructure() -- local secondary_structure = structure.SecondaryStructure.GetStructure() -- local propensity={} -- --selection.SelectAll() -- --structure.SetSecondaryStructureSelected('L') -- --selection.DeselectAll() -- for n = 1, structureCount do -- propensity[n] = { -- ["PredictedStructure"] = "L", -- ["Helix"] = 0, -- ["Sheet"] = 0, -- ["Loop"] = 0} -- end -- for n=6,(structureCount-5) do -- for m=(n-5),(n+5) do -- propensity[n]["Helix"]=propensity[n]["Helix"]+helix[primary_structure[m]] -- propensity[n]["Sheet"]=propensity[n]["Sheet"]+sheet[primary_structure[m]] -- propensity[n]["Loop"]=propensity[n]["Loop"]+loop[primary_structure[m]] -- end -- propensity[n]["Helix"]=propensity[n]["Helix"]/11 -- propensity[n]["Sheet"]=propensity[n]["Sheet"]/11 -- propensity[n]["Loop"]=propensity[n]["Loop"]/11 -- if((propensity[n]["Helix"] * 0.95) >= (propensity[n]["Sheet"]) and (propensity[n]["Helix"] * 0.95) >= propensity[n]["Loop"])then -- propensity[n]["PredictedStructure"]="H" -- elseif(propensity[n]["Sheet"] >= (propensity[n]["Helix"] * 0.95) and propensity[n]["Sheet"] >= (propensity[n]["Loop"] * 0.9))then -- propensity[n]["PredictedStructure"]="E" -- end -- end -- for n=6,structureCount do -- --if(propensity[n]["PredictedStructure"] == "H") then -- and propensity[n]["PredictedStructure"] == propensity[n - 1]["PredictedStructure"] and propensity[n]["PredictedStructure"] == propensity[n - 2]["PredictedStructure"] and propensity[n]["PredictedStructure"] == propensity[n - 3]["PredictedStructure"] and propensity[n]["PredictedStructure"] == propensity[n - 4]["PredictedStructure"] and propensity[n]["PredictedStructure"] == propensity[n - 5]["PredictedStructure"])then -- -- selection.SelectRange(n, n-5) -- structure.SetSecondaryStructure(n, propensity[n]["PredictedStructure"]) -- -- selection.Deselect(n) -- -- selection.Deselect(n-1) -- -- selection.Deselect(n-2) -- -- selection.Deselect(n-3) -- -- selection.Deselect(n-4) -- -- selection.Deselect(n-5) -- --end -- end -- --for n=4,structureCount do -- -- if(propensity[n]["PredictedStructure"] == "E" and propensity[n]["PredictedStructure"] == propensity[n - 1]["PredictedStructure"] and propensity[n]["PredictedStructure"] == propensity[n - 2]["PredictedStructure"] and propensity[n]["PredictedStructure"] == propensity[n - 3]["PredictedStructure"])then -- -- --selection.SelectRange(n, n-3) -- -- structure.SetSecondaryStructureSelected(propensity[n]["PredictedStructure"]) -- -- selection.Deselect(n) -- -- selection.Deselect(n-1) -- -- selection.Deselect(n-2) -- -- selection.Deselect(n-3) -- -- end -- --end -- --for n=1,structureCount do -- -- if(propensity[n]["PredictedStructure"] == "L" and (propensity[n]["Helix"] * 0.90) > propensity[n]["Sheet"] and propensity[n]["Helix"] > 90 and (propensity[n]["Helix"] * 0.9) > propensity[n]["Loop"])then -- -- --selection.Select(n) -- -- structure.SetSecondaryStructureSelected('H') -- -- selection.Deselect(n) -- -- elseif(propensity[n]["PredictedStructure"] == "L" and (propensity[n]["Sheet"] * 0.90) > propensity[n]["Helix"] and propensity[n]["Sheet"] > 100 and (propensity[n]["Sheet"] * 0.90) > propensity[n]["Loop"])then -- -- --selection.Select(n) -- -- structure.SetSecondaryStructureSelected('E') -- -- selection.Deselect(n) -- -- end -- --end -- --secondary_structure=save_secondary_structure(1,gsc) -- return secondary_structure --end

Comments


LociOiling Lv 1

I've hit the same error Bruno mentions several times on puzzle 1100. It's been really hard to reproduce, however. It seems likely that the error is occurring in the recipe-defined function rotamer.Optimize(), but I don't see a specific problem there. The function works correctly even when the rotamer count changes during the process of snapping to all the sidechain positions.

The same issue has impacted Acid Tweeker in the past, but I believe Bruno has fixed it there.

This is a case where it would be nice if rotamer.SetRotamer returned an error code instead of crashing the recipe. Fortunately, the Lua pcall() function provides a way to implement this wish list item.

The basic idea is to call rotamer.SetRotamer using pcall, then check for an error:


– SafeSnap uses the Lua protected call function, pcall,
– to call rotamer.SetRotamer

– SafeSnap returns a numeric return code and an error message.

– The return codes are:

– 0 - successful, error message is nil
– -1 - bad rotamer index

– Errors other than a bad rotamer index are rethrown using
– the Lua function error().

function SafeSnap ( segIdx, rotIdx )

– error message when attempting to snap to an invalid rotamer

local BADSNAP = "(snap index out of bounds)"

– don't allow a bad rotamer to stop us

local good, errmsg = pcall ( rotamer.SetRotamer, segIdx, rotIdx )
if good then
return 0, nil
else
local err2 = ParseError ( errmsg )
local errp = err2:find ( BADSNAP )
if errp ~= nil then
return -1, err2
end
error ( errmsg, 0 )
end
end

Note that errors other than "snap index out of bounds" are simply reissued
using the Lua error() function.

The helper function ParseError() is adapted from the cleanup logic developed by Bruno and Jean-Bob:

function ParseError ( errmsg )
    local reason
    local start, stop, line, msg
    start, stop, line, msg = errmsg:find ( ":(%d+):%s()" )
    if msg ~= nil then
        errmsg = errmsg:sub ( msg, #errmsg )
    end
    return errmsg    
end

The recipe then uses SafeSnap instead of rotamer.SetRotamer:

--
--  attempt to snap the sidechain in a safe, non-crashy manner
--
    local rc, errmsg = SafeSnap ( segIdx, rotIdx )
    if rc ~= 0 then
        print ( "optimize: BAD SNAP, segment = "
                    .. segIdx ..
                ", rotamer = "
                    .. rotIdx .. 
                ", current rotamer count = "
                    .. rotamer.GetCount ( segIdx ) )
    end

When the bad rotamer index problem hits, I don't see a reason to stop the recipe. To me, it makes more sense to log the error and continue processing.

So far this logic seems to be working. I'm running a test recipe using this logic now. I'll release the test recipe once it's had a little more time to find a problem.

LociOiling Lv 1

Here's the code from the previous post with added tags:

--
--  SafeSnap uses the Lua protected call function, pcall,
--  to call rotamer.SetRotamer
--
--  SafeSnap returns a numeric return code and an error message.
--
--  The return codes are:
--
--   0 - successful, error message is nil
--  -1 - bad rotamer index
--
--  Errors other than a bad rotamer index are rethrown using 
--  the Lua function error().
--  
    function SafeSnap ( segIdx, rotIdx )
    --
    --  error message when attempting to snap to an invalid rotamer
    --
        local BADSNAP = "(snap index out of bounds)" 
    --
    --  don't allow a bad rotamer to stop us
    --
        local good, errmsg = pcall ( rotamer.SetRotamer, segIdx, rotIdx )
        if good then
            return 0, nil
        else
            local err2 = ParseError ( errmsg )
            local errp = err2:find ( BADSNAP )
            if errp ~= nil then
                return -1, err2
            end
            error ( errmsg, 0 )
        end
    end
    function ParseError ( errmsg )
        local reason
        local start, stop, line, msg
        start, stop, line, msg = errmsg:find ( ":(%d+):%s()" )
        if msg ~= nil then
            errmsg = errmsg:sub ( msg, #errmsg )
        end
        return errmsg    
    end
    --
    --  attempt to snap the sidechain in a safe, non-crashy manner
    --
        local rc, errmsg = SafeSnap ( segIdx, rotIdx )
        if rc ~= 0 then
            print ( "optimize: BAD SNAP, segment = "
                        .. segIdx ..
                    ", rotamer = "
                        .. rotIdx .. 
                    ", current rotamer count = "
                        .. rotamer.GetCount ( segIdx ) )
        end

LociOiling Lv 1

I'm not sure whether error messages are thrown in the local language. If they are, you'll need to change "(snap index out of bounds)" to suit your locale. I'll test this at some point.

brgreening Lv 1

https://fold.it/portal/node/996019

when you call get_rotamer_count initially, you are in initial state

when you call set_rotamer( 1 ), you are in rot_1 state

when you call set_rotamer( 2 ), this would not be the correct thing to do

you have to save the initial state

then set_rotamer( 1 )

then do stuff to check things

then recover initial state

then call set_rotamer( 2 )

then do stuff to check things

then recover initial state

then call set_rotamer( 3 )

this should fix your rotamer out of range problem