Skip to content

Instantly share code, notes, and snippets.

@Rav3nPL
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rav3nPL/f15246a0603cc484b728 to your computer and use it in GitHub Desktop.
Save Rav3nPL/f15246a0603cc484b728 to your computer and use it in GitHub Desktop.
Foldit DRW v4
--[[
Rav3n_pl Deep Rebuild v4.x LUA V2
WARNING!
1) Script uses save slots 3-10!!!
2) Best score are always in slot 3
Description:
This is a long run re-builder. Idea is to rebuild given/found area so many times to found better position.
Each rebuild is scored on 7 different ways and saved if better.
After rebuild finishes script trying to stabilize and fuze each saved position (up to 7).
Because some positions are best in more than 1 way sometimes it is only 2 potions to stabilize.
]]--
-- All options at end.
p=print
function CI(c)
if CapCI==true then
if c>maxCI then
c=maxCI
end
else
c=c*maxCI
end
behavior.SetClashImportance(c)
end
segCnt=structure.GetCount()
while structure.GetSecondaryStructure(segCnt)=="M" do segCnt=segCnt-1 end
function filtersOn()
if filters then
behavior.SetSlowFiltersDisabled(false)
end
end
function filtersOff()
if filters then
behavior.SetSlowFiltersDisabled(true)
end
end
function Score()--return score, exploration too
filtersOn()
local s=0
if energy then
s=current.GetEnergyScore()
else
s=current.GetScore()
end
filtersOff()
return s
end
function round(x)--cut all afer 3-rd place
return x-x%0.001
end
function abs(x)
if x<0 then x=-x end
return x
end
function Wiggle(how, iters, minppi) --score conditioned recursive wiggle/shake
if how==nil then how="wa" end
if iters==nil then iters=6 end
if minppi==nil then minppi=0.1 end
if iters>0 then
iters=iters-1
local sp=Score()
if how == "s" then structure.ShakeSidechainsSelected(1)
elseif how == "wb" then structure.WiggleAll(2, true,false)
elseif how == "ws" then structure.WiggleAll(2,false,true)
elseif how == "wa" then structure.WiggleAll(2)
end
if Score()-sp > minppi then return Wiggle(how, iters, minppi) end
end
end
function AllLoop() --turning entire structure to loops
local ok=false
for i=1, segCnt do
local s=structure.GetSecondaryStructure(i)
if s~="L" then
save.SaveSecondaryStructure()
ok=true
break
end
end
if ok then
selection.SelectAll()
structure.SetSecondaryStructureSelected("L")
end
end
function SelectAround(ss,se,radius,nodeselect)
if nodeselect~=true then selection.DeselectAll() end
for i=1, segCnt do
for x=ss,se do
if structure.GetDistance(x,i)<radius then selection.Select(i) break end
end
end
end
function qStab()
CI(0.1)
Wiggle("s",1)
selection.SelectAll()
if fastQstab==false then
CI(0.4)
Wiggle("wa",1)
CI(1)
Wiggle("s",1)
end
CI(1)
Wiggle()
end
function FuzeEnd()
CI(1)
Wiggle("wa",1)
Wiggle("s",1)
Wiggle()
SaveBest()
end
function Fuze1(ci1,ci2)
CI(ci1)
Wiggle("s",1)
CI(ci2)
Wiggle("wa",1)
end
function Fuze2(ci1,ci2)
CI(ci1)
Wiggle("wa",1)
CI(1)
Wiggle("wa",1)
CI(ci2)
Wiggle("wa",1)
end
function reFuze(scr,slot)
local s=Score()
if s<scr then
save.Quickload(slot)
else
scr=s
save.Quicksave(slot)
end
return scr
end
function Fuze(slot)
local scr=Score()
selection.SelectAll()
recentbest.Save()
Fuze1(0.3,0.6) FuzeEnd()
scr=reFuze(scr,slot)
Fuze2(0.3,1) SaveBest()
scr=reFuze(scr,slot)
Fuze1(0.05,1) SaveBest()
scr=reFuze(scr,slot)
Fuze2(0.7,0.5) FuzeEnd()
scr=reFuze(scr,slot)
Fuze1(0.07,1) SaveBest()
reFuze(scr,slot)
recentbest.Restore()
SaveBest()
end
function Sort(tab,items) --BACWARD bubble sorting - lowest on top, only needed items
for x=1,items do --items do
for y=x+1,#tab do
if tab[x][1]>tab[y][1] then
tab[x],tab[y]=tab[y],tab[x]
end
end
end
return tab
end
function FindWorst()
if sEnd==nil then sEnd=segCnt end
p("Searching worst scoring parts of len "..len)
wrst={}
for i=sStart,sEnd-len+1 do
local s=getAreaScore(i,i+len-1,scrPart)
wrst[#wrst+1]={s,i}
end
wrst=Sort(wrst,reBuild)
areas={}
local rb=reBuild
if rb>#wrst then rb=#wrst end
for i=1,rb do
local w=wrst[i]
local ss=w[2]
areas[#areas+1]={ss,ss+len-1}
end
end
bestScore=Score()
function SaveBest()
local g=Score()-bestScore
if g>0 then
if g>0.01 then p("Gained another "..round(g).." pts.") end
bestScore=Score()
save.Quicksave(3)
end
end
function localRebuild(maxiters)
if maxiters==nil then maxiters=3 end
local s=Score()
local i=0
repeat
i=i+1
if i>maxiters then break end
structure.RebuildSelected(i)
until Score()~=s
if Score()~=s then return true else return false end
end
function ReBuild(ss,se,tries)
Clear() --reser score tables
if ss>se then ss,se=se,ss end --switch if needed
p("DR area "..ss.."-"..se.." "..tries.." times. Wait... Current score: "..round(Score()))
selection.DeselectAll()
CI(rebuildCI)
selection.SelectRange(ss,se)
if rbSkip> 0 then
p("Skipping "..rbSkip.." positions...")
structure.RebuildSelected(rbSkip)
p("Now scoring rebuilds.")
end
for try=1,tries do -- perform loop for number of tries
CI(rebuildCI)
local done=localRebuild()
SaveBest()
if done==true and doShake==true then
CI(shakeCI)
Save(ss,se)
if doMutate==true then
structure.MutateSidechainsSelected(1)
else
Wiggle("s",1)
end
SaveBest()
end
if done==true then Save(ss,se) end
end
CI(1)
end
function getPartScore(segments,attr)--using partial scoring
local score = 0
for i=1,#attr do
score = score + current.GetSegmentEnergySubscore(segments,attr[i])
end
return score
end
function getAreaScore(ss,se,attr)
local s=0
if attr~=nil then --local scoring
for i=ss,se do
s=s+getPartScore(i,attr)
end
else --total segment scores
for i=ss,se do
s=s+current.GetSegmentEnergyScore(i)-current.GetSegmentEnergySubscore(i,'reference') --ignore reference part
end
end
return s
end
Scores={} --{save_no,points}
function Clear()
Scores={
{10,-99999999},
{9,-99999999},
{8,-99999999},
{7,-99999999},
{6,-99999999},
{5,-99999999},
{4,-99999999},
}
slotScr={}
end
function Save(ss,se) --there you can change scoring methods for each save
local scr=
{
{10,getAreaScore(ss,se,{'backbone'})},
{9,getAreaScore(ss,se,{'sidechain'})},
{8,getAreaScore(ss,se,{'clashing'})},
{7,getAreaScore(ss,se,{'hiding'})},
{6,getAreaScore(ss,se,{'bonding'})},
{5,getAreaScore(ss,se)},
{4,Score()}
}
for i=1,#Scores do
local s=scr[i][2]
if s>Scores[i][2] then
local slot=scr[i][1]
save.Quicksave(slot) --p("Saved slot ",slot," pts" ,s) --debug
Scores[i][2]=s
end
end
SaveBest()
end
slotScr={}
function ScoresCheck(slot)
save.Quickload(slot)
local s=Score()
local ok=true
if #slotScr > 0 then
for i=1,#slotScr do
if slotScr[i]==s then ok=false break end
end
end
slotScr[#slotScr+1]=s
return ok
end
function PrintAreas()
if #areas<19 then
local a=""
local x=0
for i=1,#areas do
x=x+1
a=a..areas[i][1].."-"..areas[i][2].." "
if x>6 then
p(a)
a=""
x=0
end
end
if x>0 then p(a) end
else
p("It is "..#areas.." places, not listing.")
end
end
function AddLoop(sS)
local ss=sS
local ssStart=structure.GetSecondaryStructure(ss)
local se=ss
for i=ss+1,segCnt do
if structure.GetSecondaryStructure(i)==ssStart then se=i
else break end
end
if se-ss+2>minLen and loops==true then
areas[#areas+1]={ss,se}
end
return se
end
function AddOther(sS)
local ss=sS
local ssStart=structure.GetSecondaryStructure(ss)
local se=ss
if ss>1 then
for i=ss-1,1,-1 do --search bacward for start
local sec=structure.GetSecondaryStructure(i)
if sec=="L" then ss=i
else break end
end
end
if se<segCnt-1 then --now forward to find end
local change=false
repeat
se=se+1
if se==segCnt then break end
local sec=structure.GetSecondaryStructure(se)
if change==false then
if sec~=ssStart then change=true end
end
until change==true and sec~="L"
if se<segCnt then se=se-1 end
end
if sheets==false and ssStart=="E" then return se end
if helices==false and ssStart=="H" then return se end
if se-ss+2>minLen then
areas[#areas+1]={ss,se}
end
return se
end
function FindAreas()
if loops then
local done=false
local ss=0
repeat--loops
ss=ss+1
local ses=structure.GetSecondaryStructure(ss)
if ses=="L" then
ss=AddLoop(ss)
end
if ss==segCnt then done=true end
until done~=false
end
if sheets or helices then
local done=false
local ss=0
repeat--other
ss=ss+1
local ses=structure.GetSecondaryStructure(ss)
if ses~="L" then
ss=AddOther(ss)
end
if ss==segCnt then done=true end
until done~=false
end
end
function DeepRebuild()
if sEnd==nil then sEnd=segCnt end
local ss=Score()
if struct==false then AllLoop() end
save.Quicksave(3)
p("DeepRebuild started at score: "..round(ss))
for i=1,#areas do
local ss=Score()
local s=areas[i][1]
local e=areas[i][2]
ReBuild(s,e,rebuilds)
for r=1,#Scores do
slot=Scores[r][1]
if ScoresCheck(slot) then
p("Stabilizing slot "..slot)
SelectAround(s,e,12) --local shake after rebuild
qStab()
save.Quicksave(slot)
SaveBest()
end
end
save.Quickload(4)
recentbest.Save()
for i=5,10 do
save.Quickload(i)
end
recentbest.Restore()
p("Fuzing best position.")
save.Quicksave(4)
Fuze(4)
SaveBest()
save.Quickload(3)
if breakOnPoints and Score()>ss then break end --break if points found
end
p("DeepRebuild gain: "..round(Score()-ss))
if struct==false then save.LoadSecondaryStructure() end
end
function DRcall(how)
if filters then filtersOff() end
if sEnd==nil then sEnd=segCnt end
if how=="drw" then
for i=minLen,maxLen do --search from minl to maxl worst segments
len=i
FindWorst() --fill areas table. Comment it if you have set them by hand
PrintAreas()
DeepRebuild()
end
elseif how=="fj" then --DRW len cut on pieces
FindWorst() --add to areas table worst part
areas2={}
for a=1,#areas do
local s=areas[a] --{ss,se}
local ss=s[1] --start segment of worst area
local se=s[2] --end segment of worst area
for i=ss,se do
for x=1,len do
if i+x<=se then
areas2[#areas2+1]={i,i+x}
end
end
end
end
areas=areas2
PrintAreas()
DeepRebuild()
elseif how=="all" then
areas={}
for i=minLen,maxLen do
for x=sStart,sEnd do
if i+x-1<=sEnd then
areas[#areas+1]={x,x+i-1}
end
end
end
PrintAreas()
DeepRebuild()
elseif how=="simple" then
FindWorst()
PrintAreas()
DeepRebuild()
elseif how=="areas" then
areas={}
FindAreas()
PrintAreas()
DeepRebuild()
end
if filters then filtersOn() end
end
--[[
USAGE
1. 'drw' - need 'minLen' and 'maxLen'; finding worst scores by len between that 2
2. 'fj' - need 'len'; searching len then cutting in pieces 2->len and rebuilds pieces
3. 'all' - need 'minLen' and 'maxLen'; rebuilding ENTIRE protein (from min to max) like in WalkinRebuild script
4. 'simple' - need 'len'; find and rebuild worst scoring parts of that length
5. 'areas' - need secondary structure set and 'true' on at least one of structure
]]--
----------------- options below VVVV
normal=true --set false for exploration if want to explore
filters=false --true --if true filters are enabled only for scoring
areas={ --start segment, end segment. use for last line call
--{1,10},
--{20,30},
--{32,35},
}
scrPart=nil --{'backbone','sidechain','clashing'} --change there to find worst scoring parts by partial scoring
--options for (5)"areas" setting
loops=true --rebuild loops alone
sheets=false --rebuild sheets + surrounding loops
helices=true --false --rebuild helices + surrounding loops
breakOnPoints=true --breaking rebuild after successful one to find new worst
doShake=true --false --shake (or mutate) rebuild area (only!) every rebuild, slowing down process
doMutate=false --true --mutate instead of shake when rebuild
shakeCI=0.11 --clash importance while shaking or mutating
maxCI=1 --0.5 --maximum Clash Importance
CapCI=false --false=use maxci as multiplier, true=just cap it to maxci
struct=false --set in all loop (if true work in structure mode)
fastQstab=true --false --if true faster stabilize, but longer
sStart=1 --start segment to search worst
sEnd=segCnt --end segment to search worst
reBuild=7 --up to worst parts to look at
rebuilds=15 --how many rebuilds to try, set at least 10!
rbSkip=0 --skip that many rebuilds b4 scoring (for aunt_deen) ;]
rebuildCI=0 --clash importance while rebuild
len=6 --find worst segments part
minLen=3 --or specify minimum len
maxLen=4 --and maximim len
while true do --uncomment method/s you want to use
-- DRcall("areas")
DRcall("drw")
-- DRcall("fj")
-- DRcall("all")
-- DRcall("simple")
-- DeepRebuild() --if set "areas" above by hand
reBuild=reBuild+3 --three worst places more every loop
end
-- end of script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment