Code
--[[
ATGrinder
attempt to replicate Acid Tweeker crash
]]--
--Start Generic JET parameters and functions
recipeName = "ATGrinder"
Version = "1.0"
ReVersion = recipeName .. " v." .. Version
WF = 4
PROBABLEFILTER = true
bestScore = 0
startChunque = nil -- initial chuquePoint
CSVOutput = false
flagligand = false
segCnt = structure.GetCount()
segCnt2 = segCnt -- cas de ligands
while structure.GetSecondaryStructure(segCnt2) == "M" do segCnt2 = segCnt2 - 1 flagligand = true end
segStart = 1 --start from (Default option for JET)
segEnd = segCnt2 --end seg: nil=end of protein (Default option for JET) -- duplicat a verifier
ATSegStart = segStart
ATSegEnd = segEnd
function round ( x )--cut all afer 3-rd place
return x - x % 0.001
end
function down ( x )
return x - x % 1
end
function Score ()
return current.GetEnergyScore ()
end
-- Module Random
-- Tvdl, 01-11-2012
Randomseed = os.time () % 1000000
function Seedrandom ()
math.randomseed ( Randomseed )
math.random ( 100 ) -- Because the first is not random
end
Seedrandom ()
--
-- begin chunquePoint Beta package
--
function beginChunque ( startChunque, prefix, scoreFunc, pose )
local chunque = chunquePoint ( prefix, scoreFunc, pose )
if startChunque == nil then
chunquePrint ( chunque )
else
local gain = chunque [ 2 ] - startChunque [ 2 ]
chunquePrint ( chunque, gain )
end
return chunque
end
function endChunque ( startChunque, prefix, scoreFunc, pose )
local chunque = chunquePoint ( prefix, scoreFunc, pose )
chunqueGain ( startChunque, chunque )
end
function chunquePoint ( prefix, scoreFunc, pose )
if prefix == nil then
prefix = ""
end
local score = 0
if pose == nil then
score = scoreFunc ()
else
if pose == nil then
pose = current
end
score = scoreFunc ( pose )
end
local runDate, runClock, runTime
runDate = os.date ()
runClock = os.clock()
runTime = os.time()
return { prefix, score, runDate, runClock, runTime }
end
function chunquePrint ( chunque, gain )
local function round ( ii )
return ii - ii % 0.001
end
if gain == nil then
if CSVOutput then
print ( chunque [ 1 ] .. "," ..
round ( chunque [ 2 ] ) .. "," ..
chunque [ 3 ]
)
else
print ( chunque [ 1 ] .. ", score: " ..
round ( chunque [ 2 ] ) .. ", " ..
chunque [ 3 ]
)
end
else
if CSVOutput then
print ( chunque [ 1 ] .. "," ..
round ( chunque [ 2 ] ) .. "," ..
round ( gain ) .. "," ..
chunque [ 3 ]
)
else
print ( chunque [ 1 ] .. ", score: " ..
round ( chunque [ 2 ] ) .. ", gain: " ..
round ( gain ) .. ", " ..
chunque [ 3 ]
)
end
end
end
function chunqueGain ( startChunk, endChunk )
local function round ( ii )
return ii - ii % 0.001
end
local Clocktime = os.difftime ( endChunk [ 5 ], startChunk [ 5 ] )
local gain = round ( endChunk [ 2 ] - startChunk [ 2 ] )
local hours = Clocktime / ( 60 * 60 )
local pph = gain / hours
if CSVOutput then
print ( endChunk [ 1 ] .. "," ..
round ( endChunk [ 2 ] ) .. "," ..
round ( gain ) .. "," ..
round ( hours ) .. "," ..
round ( pph ) .. "," ..
endChunk [ 3 ]
)
else
print ( endChunk [ 1 ] .. ", score: " ..
round ( endChunk [ 2 ] ) .. ", gain: " ..
round ( gain ) .. ", " ..
endChunk [ 3 ]
)
print ( endChunk [ 1 ] .. ", hours: " ..
round ( hours ) .. ", points per hour: " ..
round ( pph ) )
end
end
--
-- end chunquePoint Beta package
--
function ds ( val )
structure.ShakeSidechainsSelected ( val )
end
function CI ( val )
global_ci = val
behavior.SetClashImportance ( global_ci )
end
function WiggleSimple ( val, how )
if PROBABLEFILTER then
behavior.SetSlowFiltersDisabled ( true )
end
if how == "s" then ds ( 1 )
elseif how == "wb" then structure.WiggleSelected ( val * WF, true, false ) -- backbones
elseif how == "ws" then structure.WiggleSelected ( val * WF, false, true ) -- sidechains
elseif how == "wa" then structure.WiggleSelected ( val * WF, true, true ) -- all
end
if PROBABLEFILTER then
behavior.SetSlowFiltersDisabled ( OriginalFilterSetting )
end
end
function WiggleAT ( how, iters, minppi )
local valiter = 2
local val = 1
if fast == true then
valiter = 1
end
if how == nil then
how = "wa"
end
if iters == nil then
iters = 3
end
if ( minppi == nil ) or ( minppi < 0.01 ) then
minppi = 0.01
end
if global_ci == 1.00 then
val = valiter
end
local sp, ep, ig
repeat
iters = iters - 1
sp = Score ()
WiggleSimple ( val, how )
ep = Score ()
ig = ep - sp
until iters <= 0 or how == "s" or ig < minppi
end
function SelectSphere ( sg, radius, nodeselect )
if nodeselect ~= true then
selection.DeselectAll ()
end
for ii = segStart, segEnd do
if structure.GetDistance ( sg, ii ) < radius then
selection.Select ( ii )
end
end
end
function SaveBest () --adapted to JET
local sp = Score ()
local g = sp - bestScore
if g > 0 then
bestScore = sp
print ( "gained another " .. round ( g ) )
end
end
function wiggle_out ()
CI ( 0.6 )
WiggleSimple ( 1, "wa" ) -- new function BK 8/4/2013
CI ( 1.0 )
WiggleAT ()
WiggleAT ( "s", 1 )
--selection.SelectAll ()
CI ( 0.6 )
WiggleAT ()
CI ( 1.0 )
WiggleAT ()
recentbest.Restore ()
SaveBest ()
end
function getNear ( seg )
if Score () < g_total_score - 1000 then
selection.Deselect ( seg )
CI ( 0.75 )
WiggleSimple ( 1, "s" ) -- new function BK 8/4/2013
WiggleSimple ( 1, "ws" ) -- new function BK 8/4/2013
selection.Select ( seg )
CI ( 1 )
end
if Score () < g_total_score - 1000 then
recentbest.Restore ()
SaveBest ()
return false
end
return true
end
function sidechain_tweak ( sphereSize, ATSegStart, ATSegEnd )
for ATseg = ATSegStart, ATSegEnd do
selection.DeselectAll ()
selection.Select ( ATseg )
structure.SetNote ( 3, "AT phase 1, segment = " .. ATseg )
local ss=Score ()
g_total_score = Score ()
CI ( 0 )
print ( "wiggling segment " .. ATseg )
WiggleSimple ( 2, "s" )
CI ( 1.0 )
print ( "selecting sphere around segment " .. ATseg )
SelectSphere ( ATseg, sphereSize )
print ( "calling getNear ( " .. ATseg .. " )" )
if getNear ( ATseg ) == true then
print ( "wiggle_out for segment " .. ATseg )
wiggle_out ()
end
end
end
function GetParameters ()
local dlog = dialog.CreateDialog ( ReVersion )
local askresult
dlog.WF = dialog.AddSlider ( "wiggle factor", WF, 1, 5, 0 )
dlog.ATSegStart = dialog.AddSlider ( "start segment", ATSegStart, segStart, segEnd, 0 )
dlog.ATSegEnd = dialog.AddSlider ( "end segment", ATSegEnd, segStart, segEnd, 0 )
dlog.PROBABLEFILTER = dialog.AddCheckbox ( "disable slow filters?", PROBABLEFILTER )
dlog.ok = dialog.AddButton ( "OK", 1 )
dlog.cancel = dialog.AddButton ( "Cancel", 0 )
repeat
askresult = dialog.Show ( dlog )
if askresult > 0 then
WF = dlog.WF.value
ATSegStart = dlog.ATSegStart.value
ATSegEnd = dlog.ATSegEnd.value
if ATSegStart > ATSegEnd then
ATSegStart, ATSegEnd = ATSegEnd, ATSegStart
end
PROBABLEFILTER = dlog.PROBABLEFILTER.value
end
until askresult < 2
return askresult > 0
end
function main ()
--
-- model 010 - startup - print recipe name, puzzle name, track
--
print ( ReVersion )
print ( "Puzzle: " .. puzzle.GetName () )
print ( "Track: " .. ui.GetTrackName () )
--
-- model 020 - startup - print recipe parms
--
print ( "options:" )
print ( "Wiggle Factor = " .. WF )
print ( "starting AT segment = " .. ATSegStart )
print ( "ending AT segment = " .. ATSegEnd )
print ( "--" )
--
-- model 030 - startup - print start time, start score
--
startChunque = beginChunque ( nil, "Start", Score )
CI ( 1.00 )
save.Quicksave ( 3 )--in save 3 always best solution. Load in case of crash.
bestScore = Score ()
sidechain_tweak ( 8, ATSegStart, ATSegEnd )
cleanup ()
end
function cleanup ( error )
print ( "---" )
--
-- model 100 - print recipe name, puzzle, track, time, score, and gain
--
local reason
local start, stop, line, msg
if error == nil then
reason = "complete"
else
--
-- model 120 - civilized error reporting,
-- thanks to Bruno K. and Jean-Bob
--
start, stop, line, msg = error:find ( ":(%d+):%s()" )
if msg ~= nil then
error = error:sub ( msg, #error )
end
if error:find ( "Cancelled" ) ~= nil then
reason = "cancelled"
else
reason = "error"
end
end
print ( ReVersion .. " " .. reason )
print ( "Puzzle: " .. puzzle.GetName () )
print ( "Track: " .. ui.GetTrackName () )
if reason == "error" then
print ( "Unexpected error detected" )
print ( "Error line: " .. line )
print ( "Error: \"" .. error .. "\"" )
end
--
-- model 130 - reset clash importance, clear selections, restore structures, etc.
--
recentbest.Restore ()
save.Quickload ( 3 )
selection.DeselectAll ()
freeze.UnfreezeAll ()
band.DeleteAll ()
save.LoadSecondaryStructure ()
CI ( 1 )
behavior.SetSlowFiltersDisabled ( OriginalFilterSetting )
--
-- model 150 - report the final time, score, gain, elapsed time, and points per hour
--
if startChunque ~= nil then
endChunque ( startChunque, "Final", Score )
end
end
if GetParameters () then
xpcall ( main, cleanup )
end