Code
-- Mutators Collection.
-- MurloW
-- functions sort(part) and allByOne() written mostly by skillful Team AD scripters who wish to remain anonymous.
-- 23-04-2014 : increased speed and compatibility.
-- 18-05-2014 : cleanup and public release.
-- 19-01-2015 : added a mutator; updated wiggle and dialog functions with filter disabling.
IterationMultiplier = 1.1 -- 2.5 -- 1.4
ItMu = IterationMultiplier
normal = true
cival = behavior.GetClashImportance()
nSegs=structure.GetCount()
p=print
function Seed() -- REALLY good seed by Rav3n_pl
seed=os.time()/math.abs(current.GetEnergyScore())
seed=seed%0.001
seed=1/seed
while seed<10000000 do seed=seed*10 end
seed=seed-seed%1
--p("Seed is: "..seed)
math.randomseed(seed)
end
Seed()
function score()
local s=0
if normal==false then
s=current.GetEnergyScore()
else
s=current.GetScore()
end
return s
end
function wig(how, it, iters, minppi)
if DisFil then behavior.SetSlowFiltersDisabled(true) end
if how==nil then how="wa" end
if minppi==nil then minppi=2 end
if iters==nil then iters=2 end
if it==nil then it=10*ItMu end
it=math.ceil(it)
if iters>0 then
iters=iters-1
local sp=score()
if how == "s" then structure.ShakeSidechainsAll(1)
elseif how == "wb" then structure.WiggleAll(it,true,false)
elseif how == "ws" then structure.WiggleAll(it,false,true)
elseif how == "wa" then structure.WiggleAll(it)
end
if score()-sp > minppi and how ~= "s" then return wig(how, it, iters, minppi) end
end
if DisFil then behavior.SetSlowFiltersDisabled(false) end
end
function qStab()
Check()
setCI(cival/2)
wig("wa",5)
setCI(cival)
wig("s")
wig()
Check()
setCI(cival/2)
wig("wa",5)
setCI(cival)
wig()
Check()
end
function Fuze()
Check()
setCI(.05)
wig("s")
setCI(cival)
wig()
Check()
setCI(cival/3)
wig("wa",5)
if Sf == false then
setCI(.07)
wig("s")
setCI(cival)
wig()
Check()
setCI(cival/3)
wig("wa",5)
end
setCI(cival)
wig("s")
wig()
Check()
end
function selectsphere(seg, radius)
if seg>nSegs then seg = nSegs end
if seg<1 then seg = 1 end
for y=1, nSegs do
if structure.GetDistance(seg,y) <= radius then
if y >= 1 and y <= nSegs then
selection.Select(y)
end
end
end
end
function cut(x)
return x-x%1
end
function cut1(x)
return x-x%0.1
end
function cut3(x)
return x-x%0.001
end
function setCI(ci)
return behavior.SetClashImportance(ci)
end
function nomuta()
p("")
p(" No mutable residues. ")
end
function End(errmsg)
p("")
if string.find(errmsg,"Cancelled") then
p(" User cancel.")
p("")
Result()
else
p("")
p(errmsg)
p("")
Result()
end
return errmsg
end
function Result()
save.Quickload(93)
setCI(cival)
recentbest.Restore()
Tgain = bestscore - startscore
if Tgain>0.008 then
p(" gained: "..cut3(Tgain))
else
p(" No change :/ ")
end
end
function Check()
if (bestscore < score()) then
gain = score() - bestscore
bestscore = score()
save.Quicksave(1)
save.Quicksave(93)
else
gain = 0
end
if gain>0.002 then
p(" ",cut3(gain),"pts.")
end
end
function sort(part)
Mseg = {}
for i = 1, nSegs do
Mseg[i] = {}
if part ~= 'total' then
Mseg[i][1] = current.GetSegmentEnergySubscore(i, part)
else
Mseg[i][1] = current.GetSegmentEnergyScore(i)
end
Mseg[i][2] = i
end
sW = 1
while sW ~= 0 do
sW=0
if WF==true then
for i=1, nSegs-1 do
if Mseg[i][1] > Mseg[i+1][1] then
Mseg[i][1],Mseg[i+1][1]=Mseg[i+1][1],Mseg[i][1]
Mseg[i][2],Mseg[i+1][2]=Mseg[i+1][2],Mseg[i][2]
sW = sW +1
end
end
else
for i=1, nSegs-1 do
if Mseg[i][1] < Mseg[i+1][1] then
Mseg[i][1],Mseg[i+1][1]=Mseg[i+1][1],Mseg[i][1]
Mseg[i][2],Mseg[i+1][2]=Mseg[i+1][2],Mseg[i][2]
sW = sW +1
end
end
end -- WF
end -- sW
return Mseg
end
AAs={"a", "c", "d", "e", "f", "g", "h", "i", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "y"}
selection.DeselectAll()
startscore = score()
bestscore = startscore
-----------------------------------
-- try all
function AllSorted()
sort(part)
for y=1, loops do
save.Quickload(93)
lscore=score()
p(" ",cut3(score()))
p("")
local nr=0
for i=1, nSegs do
if structure.IsMutable(Mseg[i][2]) then
nr=nr+1
save.Quickload(93)
Tempscore=score()
selection.Select(Mseg[i][2])
orig_aa = structure.GetAminoAcid(Mseg[i][2])
for j=1, #AAs do
if structure.CanMutate(Mseg[i][2], AAs[j]) then
structure.SetAminoAcid(Mseg[i][2], AAs[j])
if shakeAround == true then
selectsphere(Mseg[i][2],6)
setCI(cival)
structure.ShakeSidechainsSelected(1)
end
Check()
end
selection.DeselectAll()
end
save.Quickload(93)
new_aa = structure.GetAminoAcid(Mseg[i][2])
if new_aa ~= orig_aa then
print("Residue "..Mseg[i][2].." changed from "..orig_aa.." to "..new_aa)
else
print("Residue "..Mseg[i][2].." unchanged from "..orig_aa)
end
end
end
if score()>lscore then
p(" Loop gain:", cut3(score()-lscore))
end
end-- loops
end
function allByOne()
for c=1, loops do
lscore=score()
if inRevers == true then
beginS=nSegs
endS=1
step=(-1)
else
beginS=1
endS=nSegs
step=1
end
for i=beginS, endS, step do
if structure.IsMutable(i) then
orig_aa = structure.GetAminoAcid(i)
for j=1, #AAs do
if structure.CanMutate(i, AAs[j]) then
structure.SetAminoAcid(i, AAs[j])
if shakeAround == true then
selectsphere(i,6)
setCI(cival)
structure.ShakeSidechainsSelected(1)
end
Check()
end
selection.DeselectAll()
end
save.Quickload(93)
new_aa = structure.GetAminoAcid(i)
if new_aa ~= orig_aa then
print("Residue "..i.." changed from "..orig_aa.." to "..new_aa)
else
print("Residue "..i.." unchanged from "..orig_aa)
end
end
end
if score()>lscore then
p(" Loop gain:", cut3(score()-lscore))
end
end -- loops
end
function allWalk()
for c=1, loops do
lscore=score()
if inRevers == true then
beginS=nSegs
endS=1
step=(-1)
else
beginS=1
endS=nSegs
step=1
end
for j=1, #AAs do
for i=beginS, endS, step do
origA = structure.GetAminoAcid(i)
if structure.CanMutate(i, AAs[j]) then
structure.SetAminoAcid(i, AAs[j])
if shakeAround == true then
selectsphere(i,6)
setCI(cival)
structure.ShakeSidechainsSelected(1)
end
Check() --end
selection.DeselectAll()
save.Quickload(93)
newA = structure.GetAminoAcid(i)
if newA ~= origA then
p("Residue "..i.." changed from "..origA.." to "..newA)
end
end end
end -- aa
if score()>lscore then
p(" Loop gain:", cut3(score()-lscore))
end
end -- loops
end
-----------------------------------
-- random mutate
function mutateR()
for y=1, Rloops do
lscore=score()
p(" ",cut3(score()))
p("")
local nr=0
if inRevers == true then
beginS=nSegs
endS=1
step=(-1)
else
beginS=1
endS=nSegs
step=1
end
for i=beginS, endS, step do
if structure.IsMutable(i) then
nr=nr+1
save.Quickload(93)
Tempscore=score()
selection.Select(i)
ranci=(math.random(minCI*100, maxCI*100)/100)
setCI(ranci)
p(" #"..nr,"ci: "..ranci)
structure.MutateSidechainsSelected(1)
Check()
selectsphere(i, 6)
setCI(cival)
if shakeAround == true then
structure.ShakeSidechainsSelected(1)
Check()
end
setCI(cival)
structure.WiggleSelected(8, false, true)
selection.DeselectAll()
Check()
--wig('s')
--wig()
--Check()
if qSt == true then qStab() end
if Fuzz == true then Fuze() else
if fzt>=0 then
if score()>(Tempscore-fzt) then
Fuze()
end
else
if score()>=(Tempscore+FFzt) then
Fuze()
end
end
end
end end
if score()>lscore then
p(" Loop gain:", cut3(score()-lscore))
end end
end
function mutate()
sort(part)
for y=1, loops do
lscore=score()
p(" ",cut3(score()))
p("")
local nr=0
for i=1, nSegs do
if structure.IsMutable(Mseg[i][2]) then
nr=nr+1
save.Quickload(93)
Tempscore=score()
selection.Select(Mseg[i][2])
ranci=(math.random(minCI*100, maxCI*100)/100)
setCI(ranci)
p(" #"..nr.." seg: "..Mseg[i][2],"ci: "..ranci)
structure.MutateSidechainsSelected(1)
Check()
selectsphere(Mseg[i][2], 6)
setCI(cival)
if shakeAround == true then
structure.ShakeSidechainsSelected(1)
Check()
end
setCI(cival)
structure.WiggleSelected(8, false, true)
selection.DeselectAll()
Check()
--wig('s')
--wig()
--Check()
if qSt == true then qStab() end
if Fuzz == true then Fuze() else
if fzt>=0 then
if score()>(Tempscore-fzt) then
Fuze()
end
else
if score()>=(Tempscore+FFzt) then
Fuze()
end
end
end
end end
if score()>lscore then
p(" Loop gain:", cut3(score()-lscore))
end
end end
-----------------------------------
-- dialog
function dia()
-- puz def
for i=1, nSegs-1 do
if structure.IsMutable(i) == true then
MM = true
break
end
end
for d=1, nSegs/2 do
if current.GetSegmentEnergySubscore(d, 'density') ~= -0 then
EDpuzzle = true
break
end
end
-- /puzdef
if MM ~= true then return nomuta() end
p(" Mutator slider:")
p(" 1 = walk mutating with random CI ") -- mutateR
p(" 2 = walk testing all AAs on segment, then next segment ") -- allbyone
p(" 3 = walk testing AA on every segment, then next AA ") -- allwalk
p(" 4 = mutate worst or best first according to (sub)score ") -- mutateS
p(" 5 = bruteforce worst or best first according to (sub)score ") -- AllSorted
opt=dialog.CreateDialog("Choose mutator.")
opt.a=dialog.AddLabel(" See recipe output. ")
opt.Mr=dialog.AddSlider("",1,1,5,0)
opt.dis=dialog.AddCheckbox("Disable slow filters during wiggle.", true)
opt.ok=dialog.AddButton("Go go!", 1)
dialog.Show(opt)
DisFil=opt.dis.value
if opt.Mr.value==1 then RanMut=true
elseif opt.Mr.value==2 then allone=true
elseif opt.Mr.value==3 then allwalk=true
elseif opt.Mr.value==4 then mutateS=true
elseif opt.Mr.value==5 then AllSort=true
end
end
function partDialog()
opt=dialog.CreateDialog("Choose One.")
opt.cl=dialog.AddCheckbox("clashing", false)
opt.p=dialog.AddCheckbox("packing", false)
opt.bo=dialog.AddCheckbox("bonding", false)
if EDpuzzle == true then
opt.d=dialog.AddCheckbox("density", false)
end
opt.ok=dialog.AddButton("Go go!", 1)
dialog.Show(opt)
if opt.bo.value == true then part="bonding" end
if opt.cl.value == true then part="clashing" end
if opt.p.value == true then part="packing" end
if EDpuzzle == true then
if opt.d.value == true then part="density" end
end
if part == nil then return Sdialog() end
end
function allbyoneD()
opt=dialog.CreateDialog("Options")
opt.lo=dialog.AddSlider("Loops: ", 1, 1, 10, 0)
opt.inR=dialog.AddCheckbox(" In reverse, start from end and work to seg #1 ", false)
opt.Sh=dialog.AddCheckbox(" Shake around after mutate ", false)
opt.ok=dialog.AddButton("Go go!", 1)
dialog.Show(opt)
inRevers=opt.inR.value
shakeAround=opt.Sh.value
loops=opt.lo.value
end
function allwalkD()
opt=dialog.CreateDialog("Options")
opt.lo=dialog.AddSlider("Loops: ", 1, 1, 10, 0)
opt.inR=dialog.AddCheckbox(" In reverse, start from end and work to seg #1 ", false)
opt.Sh=dialog.AddCheckbox(" Shake around after mutate ", false)
opt.ok=dialog.AddButton("Go go!", 1)
dialog.Show(opt)
inRevers=opt.inR.value
shakeAround=opt.Sh.value
loops=opt.lo.value
end
function Sdialog()
opt=dialog.CreateDialog("Options")
opt.wf=dialog.AddCheckbox("Worst first; uncheck is best scoring first.",true)
opt.lo=dialog.AddSlider("Loops: ", 2, 1, 10, 0)
opt.sc=dialog.AddLabel(" Choose one scorepart")
opt.s=dialog.AddCheckbox("sidechain", true)
opt.h=dialog.AddCheckbox("hiding", false)
opt.to=dialog.AddCheckbox("total",false)
opt.o=dialog.AddCheckbox("other",false)
opt.aa=dialog.AddLabel(" Minimum and Maximum mutate CI:")
opt.mic=dialog.AddSlider("minCI :", 0.01, 0.00001, 1, 3)
opt.mac=dialog.AddSlider("maxCI :", 0.94, 0.1, 1, 3)
opt.Sh=dialog.AddCheckbox(" Shake around after mutate ",false)
opt.qst=dialog.AddCheckbox("qStab after mutate",false)
opt.fz=dialog.AddCheckbox("always fuze after mutate",false)
opt.fzt=dialog.AddSlider(" or use threshold: ", -100, -500, 50, 1)
opt.sf=dialog.AddCheckbox("Short fuze",false)
opt.ok=dialog.AddButton("Go go!", 1)
dialog.Show(opt)
if opt.to.value == true then part='total' end
if opt.h.value == true then part="hiding" end
if opt.s.value == true then part='sidechain' end
shakeAround=opt.Sh.value
loops=opt.lo.value
WF=opt.wf.value
minCI = opt.mic.value
maxCI = opt.mac.value
fzt = opt.fzt.value
FFzt = (-1*fzt)
Fuzz = opt.fz.value
Sf = opt.sf.value
qSt = opt.qst.value
if opt.o.value == true then partDialog() end
if part == nil then return Sdialog() end
end
function Rdialog()
opt=dialog.CreateDialog("Options.")
opt.rl=dialog.AddSlider("Random mutate loops: ", 2, 1, 50, 0)
opt.inR=dialog.AddCheckbox(" In reverse, start from end and work to seg #1 ", false)
opt.aa=dialog.AddLabel(" Minimum and Maximum mutate CI:")
opt.mic=dialog.AddSlider("minCI :", 0.01, 0.00001, 1, 3)
opt.mac=dialog.AddSlider("maxCI :", 0.94, 0.1, 1, 3)
opt.sha=dialog.AddCheckbox("Shake around after mutate",false)
opt.qst=dialog.AddCheckbox("qStab after mutate",false)
opt.fz=dialog.AddCheckbox("always fuze after mutate",false)
opt.fzt=dialog.AddSlider(" or use threshold: ", -100, -500, 50, 1)
opt.sf=dialog.AddCheckbox("Short fuze",false)
opt.ok=dialog.AddButton("Go go!", 1)
dialog.Show(opt)
minCI = opt.mic.value
maxCI = opt.mac.value
inRevers=opt.inR.value
shakeAround = opt.sha.value
fzt = opt.fzt.value
FFzt = (-1*fzt)
Fuzz = opt.fz.value
Sf = opt.sf.value
qSt = opt.qst.value
Rloops = opt.rl.value
end
function AllSoDia()
opt=dialog.CreateDialog("Options")
opt.wf=dialog.AddCheckbox("Worst first; uncheck is best scoring first.",true)
opt.lo=dialog.AddSlider("Loops: ", 2, 1, 10, 0)
opt.sc=dialog.AddLabel(" Choose one scorepart")
opt.s=dialog.AddCheckbox("sidechain", true)
opt.h=dialog.AddCheckbox("hiding", false)
opt.to=dialog.AddCheckbox("total",false)
opt.o=dialog.AddCheckbox("other",false)
opt.Sh=dialog.AddCheckbox(" Shake around after mutate ",false)
opt.ok=dialog.AddButton("Go go!", 1)
dialog.Show(opt)
if opt.to.value == true then part='total' end
if opt.h.value == true then part="hiding" end
if opt.s.value == true then part='sidechain' end
shakeAround=opt.Sh.value
loops=opt.lo.value
WF=opt.wf.value
if opt.o.value == true then partDialog() end
if part == nil then return Sdialog() end
end
-----------------------------------
p("")
function main()
dia()
p("")
band.DeleteAll()
if DisFil then behavior.SetSlowFiltersDisabled(false) end
startscore = score()
bestscore = startscore
recentbest.Save()
save.Quicksave(1)
save.Quicksave(2)
save.Quicksave(93)
p("")
p(" maxCI = "..cival)
p("")
if RanMut==true then Rdialog()
mutateR()
elseif allwalk==true then allwalkD()
allWalk()
elseif allone==true then allbyoneD()
allByOne()
elseif mutateS==true then Sdialog()
mutate()
elseif AllSort==true then AllSoDia()
AllSorted()
end
end
err = xpcall(main,End)