Profile
- Name
- CM - Manual DRW V2.0 - 1.0.2
- ID
- 44055
- Shared with
- Public
- Parent
- None
- Children
- None
- Created on
- October 22, 2012 at 14:09 PM UTC
- Updated on
- October 22, 2012 at 14:09 PM UTC
- Description
Allows to deep rebuild areas between freeze points.
Frozen segments unfroze and added to the list to allow properly to rebuild.
If recipie is cancelled then it loads recent best score.
If puzzle is mutable it will try to mutate selected part as well.
Best for
Code
areas = {}
p=print
CI=behavior.SetClashImportance
segCnt=structure.GetCount()
mutate=false
while structure.GetSecondaryStructure(segCnt)=="M" do
segCnt=segCnt-1
end
for idx = 1,segCnt do
if structure.IsMutable(idx) then
mutate=true
p("Puzzle has mutable segments. Enabling mutations.")
break
end
end
function GetFrozenPairs()
print("Number of segments is "..structure.GetCount());
startPos = 0
endPos = 0
for idx = 1,structure.GetCount() do
if freeze.IsFrozen(idx) then
if startPos == 0 then
startPos = idx;
elseif endPos == 0 then
endPos = idx;
areas[#areas + 1] = {startPos, endPos}
freeze.Unfreeze(startPos,true,true)
freeze.Unfreeze(endPos,true,true)
startPos = 0
endPos = 0
end
end
end
end
function Score()
local s=0
if normal==true then
s=current.GetEnergyScore()
else
s=current.GetScore()
end
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.ShakeSidechainsAll(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, true, true)
end
if Score()-sp > minppi then return Wiggle(how, iters, minppi) end
end
end
SAVEDstructs=false
function AllLoop() --turning entire structure to loops
local anychange=false
for i=1,segCnt do
if structure.GetSecondaryStructure(i)~="L" then
anychange=true
break
end
end
if anychange then
save.SaveSecondaryStructure()
SAVEDstructs=true
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)
if mutate == true then
structure.MutateSidechainsSelected(1)
SaveBest();
end
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
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 IsBridge(i)
return ''..current.GetSegmentEnergySubscore(i,'disulfides') ~= '-0'
end
-- Counts bridges in protein or in rebuild according to globalbridge var
_cyslist = nil
function CountBridges(ss,se)
if not globalbridge then -- no bridges in a null range
if ss == nil then return 0 end
if se == nil then se = ss end
end
if _cyslist == nil then -- build initial list
_cyslist = {}
for i = 1,segCnt do
if structure.GetSecondaryStructure(i) ~= 'M' and structure.GetAminoAcid(i) == 'c' then _cyslist[#_cyslist+1] = i end
end
end
local count = 0
for i = 1,#_cyslist do
local seg = _cyslist[i]
if (globalbridge or (seg >= ss and seg <= se)) and
IsBridge(seg) then count = count + 1 end
end
return count
end
function BridgesBroken(n,ss,se)
return savebridges == true and CountBridges(ss,se) < n
end
function localRebuild(maxiters,ss,se)
if maxiters==nil then maxiters=3 end
local s=Score()
local i=0
local nbridges = CountBridges(ss,se)
repeat
i=i+1
if i>maxiters then break end
structure.RebuildSelected(i)
until Score()~=s and BridgesBroken(nbridges,ss,se) == false
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.")
p(" Wait... Current score: ",round(Score()))
selection.DeselectAll()
CI(rebuildCI)
local bcheckss, bcheckse = ss,se
local nbridges = CountBridges(bcheckss,bcheckse)
selection.SelectRange(ss,se)
if rbSkip> 0 then
p("Skipping ",rbSkip," postitions...")
structure.RebuildSelected(rbSkip)
p("Now scoring rebuilds.")
end
for try=1,tries do -- perform loop for number of tries
local extra_rebuilds = savebridges and 3 or 1 -- extra tries if bridges keep breaking
CI(rebuildCI)
local done
repeat
done=localRebuild(3,bcheckss,bcheckse)
extra_rebuilds = extra_rebuilds - 1
until done or extra_rebuilds > 0
if not BridgesBroken(bcheckss,bcheckse) then SaveBest() end
if done==true and doShake==true then
CI(shakeCI)
if not BridgesBroken(nbridges,bcheckss,bcheckse) then Save(ss,se) end
Wiggle("s",1)
if not BridgesBroken(nbridges,bcheckss,bcheckse) then SaveBest() end
end
if done == true and mutate == true then
structure.MutateSidechainsSelected(1)
end
if done==true and not BridgesBroken(nbridges,bcheckss,bcheckse) then
Save(ss,se)
SaveBest()
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 DeepRebuild()
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]
local nbridges = CountBridges(s,e)
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
if mutate == true then
structure.MutateSidechainsSelected(1)
SaveBest();
end
qStab()
if mutate == true then
structure.MutateSidechainsSelected(1)
SaveBest();
end
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)
if mutate == true then
structure.MutateSidechainsSelected(1)
SaveBest();
end
Fuze(4)
if mutate == true then
structure.MutateSidechainsSelected(1)
end
if not BridgesBroken(nbridges,s,e) then SaveBest() end
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 Cleanup(err)
print("Restoring CI, best result and structures")
behavior.SetClashImportance(1)
save.Quickload(3)
if SAVEDstructs then save.LoadSecondaryStructure() end
print(err)
end
normal = current.GetExplorationMultiplier() == 0
breakOnPoints=true --breaking rebuild after succesfull one to find new worsts
doShake=true --false --shake rebuilded area (only!) every rebuild, slowing down process
shakeCI=0.31 --clash imortance while shaking
struct=true --set in all loop (if true work in structure mode)
savebridges=true -- if set, never accepts a rebuild that breaks a disulfide bond
globalbridge=true -- if true count all bridges in protien, else only check in rebuild segments
fastQstab=true --false --if true faster stabilize, but longer
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
-- main proc
function Main()
p("Manual Deep Rebuild (with disulphide bridges fix) by ComputerMage. Based on DRW Disulphides 3.4.2 code.")
GetFrozenPairs()
if #areas ~= 0 then
while true do --uncomment method/s you want to use
DeepRebuild() --if set "areas" above by hand
end
else
p("Nothing to rebuild. Please freeze segments. It will rebuild between them")
end
end
--main call
xpcall(Main,Cleanup)
--end of scipt