Icon representing a recipe

Recipe: TvdL Filter Mutate Combo 1.0.0

created by Timo van der Laan

Profile


Name
TvdL Filter Mutate Combo 1.0.0
ID
45846
Shared with
Public
Parent
None
Children
Created on
March 29, 2013 at 22:02 PM UTC
Updated on
March 29, 2013 at 22:02 PM UTC
Description

Mutate Combo but switches filter off when wiggling

Best for


Code


--[[ Mutater - Combo 2 ways of mutate: random: - randomly choose sphere around mutable segment and mutate in random CI bruteforce: - check all possible aas for single sgmnt At start it search for mutable segments. B4 every loop it shuffles order of sgmnts to mutate. 18-11-2012 Tvdl Made V2 and fixed things so it works with the new filter/ ]]-- -- Position stack module 1.0 -- uses slot 50 and higher Stackmin=50 StackMarks={} StackPos=50 function PushPosition() if StackPos==100 then print("Position stack overflow, exiting") exit() end save.Quicksave(StackPos) StackPos=StackPos+1 end function PopPosition() if StackPos==50 then print("Position stack underflow, exiting") exit() end StackPos=StackPos-1 save.Quickload(StackPos) end function PushMarkPosition() StackMarks[#StackMarks+1]=StackPos PushPosition() end function PopMarkPosition() if #StackMarks == 0 then print("No marked position found, just popping") else StackPos=StackMarks[#StackMarks]+1 StackMarks[#StackMarks]= nil end PopPosition() end function ClrTopPosition() if StackPos > 50 then StackPos=StackPos-1 end end -- Handy shorts module normal= (current.GetExplorationMultiplier() == 0) segCnt=structure.GetCount() segCnt2=segCnt while structure.GetSecondaryStructure(segCnt2)=="M" do segCnt2=segCnt2-1 end -- On request of gmn CIfactor=1 function CI(CInr) behavior.SetClashImportance(CInr*CIfactor) end function CheckCI() local ask=dialog.CreateDialog("Clash importance is not 1") ask.l1=dialog.AddLabel("Last change to change it") ask.l2=dialog.AddLabel("CI settings will be multiplied by set CI") ask.continue=dialog.AddButton("Continue",1) dialog.Show(ask) end if behavior.GetClashImportance() < 0.99 then CheckCI() end CIfactor=behavior.GetClashImportance() -- Score functions function Score(pose) if pose==nil then pose=current end local total= pose.GetEnergyScore() -- FIX for big negatives if normal then return total else return total*pose.GetExplorationMultiplier() end end function SegScore(pose) if pose==nil then pose=current end local total=8000 for i=1,segCnt2 do total=total+pose.GetSegmentEnergyScore(i) end return total end function RBScore() return Score(recentbest) end function round3(x)--cut all afer 3-rd place return x-x%0.001 end bestScore=Score() function SaveBest() local g=Score()-bestScore if g>0 then if g>0.01 then print("Gained another "..round3(g).." pts.") end bestScore=Score() save.Quicksave(3) end end recentbestscore=0 recentbestslot=49 function FilterOff() -- Filters off but remember current recent best PushPosition() recentbest.Restore() recentbestscore=Score() save.Quicksave(49) PopPosition() behavior.SetSlowFiltersDisabled(true) recentbest.Save() end function FilterOn() -- Filter on but compute new recent best and set it save.Quicksave(48) --Current pose recentbest.Restore() behavior.SetSlowFiltersDisabled(false) if recentbestscore > Score() then save.Quickload(49) end recentbest.Save() save.Quickload(48) end -- Wiggle function -- Note the extra parameter to be used if only selected parts must be done function Wiggle(h,i,m,o) if h~="s" then FilterOff() end WiggleFilter(h,i,m,o) if h~="s" then FilterOn() end end function WiggleFilter(how, iters, minppi,onlyselected) --score conditioned recursive wiggle/shake --fixed a bug, absolute difference is the threshold now if how==nil then how="wa" end if iters==nil then iters=6 end if minppi==nil then minppi=0.1 end if onlyselected==nil then onlyselected=false end if iters>0 then iters=iters-1 local sp=Score() if onlyselected then if how == "s" then -- Shake is not considered to do much in second or more rounds structure.ShakeSidechainsSelected(1) return elseif how == "wb" then structure.WiggleSelected(2,true,false) elseif how == "ws" then structure.WiggleSelected(2,false,true) elseif how == "wa" then structure.WiggleSelected(2,true,true) end else if how == "s" then -- Shake is not considered to do much in second or more rounds structure.ShakeSidechainsAll(1) return 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 end if math.abs(Score()-sp) > minppi then return WiggleFilter(how, iters, minppi,onlyselected) end end end -- end of handy shorts module -- Segment set and list module -- Notice that most functions assume that the sets are well formed -- (=ordered and no overlaps) -- 02-05-2012 TvdL Free to use for non commercial purposes function SegmentListToSet(list) local result={} local f=0 local l=-1 table.sort(list) for i=1,#list do if list[i] ~= l+1 and list[i] ~= l then -- note: duplicates are removed if l>0 then result[#result+1]={f,l} end f=list[i] end l=list[i] end if l>0 then result[#result+1]={f,l} end --print("list to set") --SegmentPrintSet(result) return result end function SegmentSetToList(set) local result={} for i=1,#set do --print(set[i][1],set[i][2]) for k=set[i][1],set[i][2] do result[#result+1]=k end end return result end function SegmentCleanSet(set) -- Makes it well formed return SegmentListToSet(SegmentSetToList(set)) end function SegmentInvertSet(set,maxseg) -- Gives back all segments not in the set -- maxseg is added for ligand local result={} if maxseg==nil then maxseg=structure.GetCount() end if #set==0 then return {{1,maxseg}} end if set[1][1] ~= 1 then result[1]={1,set[1][1]-1} end for i=2,#set do result[#result+1]={set[i-1][2]+1,set[i][1]-1} end if set[#set][2] ~= maxseg then result[#result+1]={set[#set][2]+1,maxseg} end return result end function SegmentInList(s,list) table.sort(list) for i=1,#list do if list[i]==s then return true elseif list[i]>s then return false end end return false end function SegmentInSet(set,s) for i=1,#set do if s>=set[i][1] and s<=set[i][2] then return true elseif s<set[i][1] then return false end end return false end function SegmentJoinList(list1,list2) local result=list1 if result == nil then return list2 end for i=1,#list2 do result[#result+1]=list2[i] end table.sort(result) return result end function SegmentJoinSet(set1,set2) return SegmentListToSet(SegmentJoinList(SegmentSetToList(set1),SegmentSetToList(set2))) end function SegmentCommList(list1,list2) local result={} table.sort(list1) table.sort(list2) if #list2==0 then return result end local j=1 for i=1,#list1 do while list2[j]<list1[i] do j=j+1 if j>#list2 then return result end end if list1[i]==list2[j] then result[#result+1]=list1[i] end end return result end function SegmentCommSet(set1,set2) return SegmentListToSet(SegmentCommList(SegmentSetToList(set1),SegmentSetToList(set2))) end function SegmentSetMinus(set1,set2) return SegmentCommSet(set1,SegmentInvertSet(set2)) end function SegmentPrintSet(set) print(SegmentSetToString(set)) end function SegmentSetToString(set) local line = "" for i=1,#set do if i~=1 then line=line..", " end line=line..set[i][1].."-"..set[i][2] end return line end function SegmentSetInSet(set,sub) if sub==nil then return true end -- Checks if sub is a proper subset of set for i=1,#sub do if not SegmentRangeInSet(set,sub[i]) then return false end end return true end function SegmentRangeInSet(set,range) if range==nil or #range==0 then return true end local b=range[1] local e=range[2] for i=1,#set do if b>=set[i][1] and b<=set[i][2] then return (e<=set[i][2]) elseif e<=set[i][1] then return false end end return false end function SegmentSetToBool(set) local result={} for i=1,structure.GetCount() do result[i]=SegmentInSet(set,i) end return result end --- End of Segment Set module -- Module Find Segment Types function FindMutablesList() local result={} for i=1,segCnt2 do if structure.IsMutable(i) then result[#result+1]=i end end return result end function FindMutables() return SegmentListToSet(FindMutablesList()) end function FindFrozenList() local result={} for i=1,segCnt2 do if freeze.IsFrozen(i) then result[#result+1]=i end end return result end function FindFrozen() return SegmentListToSet(FindFrozenList()) end function FindLockedList() local result={} for i=1,segCnt2 do if structure.IsLocked(i) then result[#result+1]=i end end return result end function FindLocked() return SegmentListToSet(FindLockedList()) end function FindSelectedList() local result={} for i=1,segCnt do if selection.IsSelected(i) then result[#result+1]=i end end return result end function FindSelected() return SegmentListToSet(FindSelectedList()) end function FindAAtypeList(aa) local result={} for i=1,segCnt2 do if structure.GetSecondaryStructure(i)== aa then result[#result+1]=i end end return result end function FindAAtype(aa) return SegmentListToSet(FindAAtypeList(aa)) end function FindAminotype(at) --NOTE: only this one gives a list not a set local result={} for i=1,segCnt2 do if structure.GetAminoAcid(i) == at then result[#result+1]=i end end return result end -- end Module Find Segment Types -- 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() -- Thanks too Rav4pl function ShuffleTable(tab) --randomize order of elements local cnt=#tab for i=1,cnt do local r=math.random(cnt) tab[i],tab[r]=tab[r],tab[i] end return tab end function MutateSel(maxitter) if maxitter == nil then maxitter=2 end if LAYERFILTER then -- Needed because this filter restricts on the combination of restrictions local ss local Sellist=FindSelectedList() local itters=1 repeat ss=Score() ShuffleTable(Sellist) for i=1,#Sellist do selection.DeselectAll() selection.Select(Sellist[i]) structure.MutateSidechainsSelected(itters) end itters=itters+1 until math.abs(ss-Score()) > 0.0001 or itters > maxitter else structure.MutateSidechainsSelected(maxitter) end end function MutateAll(maxitter) selection.SelectAll() MutateSel(maxitter) end mutable={} --table to store mutable sgmnts function mut(ci) local done=false CI(ci) MutateSel(2) CI(1) end function Aftermut(sg) CI(0.1) Wiggle("s",1,nil,true) CI(1) Wiggle(nil,3) if sg~=nil then SelectSphere(sg,12) end Wiggle("s",1,nil,true) Wiggle(nil,3) end function SelectSphere(sg, radius) selection.DeselectAll() for i=1, segCnt do if structure.GetDistance(sg,i)<radius then selection.Select(i) end end end function mutate(i,small,large,ci) sc=Score() SelectSphere(i, small) mut(ci) if math.abs(Score()-sc)>1 then SelectSphere(i, large) Aftermut(i) else Wiggle(nil,3,nil,true) end local g=Score()-sc if g>0 then SaveBest() elseif g<0 then save.Quickload(3) end end function MutateRandom(loops) local ss=Score() selection.DeselectAll() recentbest.Save() save.Quicksave(3) for i=1,loops do local ls=Score() mutable=ShuffleTable(mutable) local cnt=#mutable for s=1,cnt do sg=mutable[s]--segment ci=math.random(100)--clash importance ci=ci/100 if ci>1 then ci=1 end--not over 1 sm=5+math.random(7)--small sphere for mutate sb=sm+2+math.random(3)--big sphere for shakeout print("Loop "..i.." of "..loops.." ;sg "..s.." of "..cnt.." ;score: "..round3(Score())) print("In sgmnt "..sg.." spheres "..sm.."-"..sb.." on CI: "..ci) mutate(sg,sm,sb,ci) end print("Loop gain: "..round3(Score()-ls)) end print("Total random mutate gain: "..round3(Score()-ss)) end function Brute(sg) local sco=Score() selection.DeselectAll() print("Mutating segment ",sg) recentbest.Save() local aa1=structure.GetAminoAcid(sg) for i=1,#chains do local c1=Score() structure.SetAminoAcid(sg,chains[i]) if structure.GetAminoAcid(sg) == chains[i] then SelectSphere(sg,9) Aftermut(sg) recentbest.Restore() end end local aa2=structure.GetAminoAcid(sg) local m=false if aa1==aa2 then print("Not changed.") else print("Segment mutated.") m=true end SaveBest() return m end function MutateBrute(loops) CI(1) local ss=Score() for l=1, loops do local ls=Score() print("Mutate loop started at score: "..round3(Score())) ShuffleTable(mutable) local muted=false for i=1,#mutable do local m=Brute(mutable[i]) if m==true then muted=true end print("Loop "..l.." "..#mutable-i.." left. Current score: "..round3(Score())) end print("Loop gain: "..round3(Score()-ls)) if muted==false then print("Not muted any single one! Breaking!") break end end print("Bruteforce mutate completed. Gain: "..round3(Score()-ss)) end chains={"g";"a";"v";"c";"p";"t";"s";"i";"l";"n";"d";"m";"h";"q";"e";"f";"k";"y";"r";"w"}--all of them --chains={"G";"A";"V";"C";"P";"T";"S";"I";"L";"N";"M";"Q";"F";"Y";"W"} --desired for TNT: no ASP, GLU, ARG, LYS, HIS mutable=FindMutablesList() nrrandom=1 nrbrute=1 function Doit() MutateRandom(nrrandom) --1 times all auto MutateBrute(nrbrute) --1 times all bruteforce end function Cleanup(err) FilterOn() CI(1) recentbest.Restore() print(err) end Version= "1.0.0" function GetParams() local ask=dialog.CreateDialog("TvdL Filter Mutate Combo "..Version) ask.nrrandom=dialog.AddSlider("Random nr",nrrandom,1,10,0) ask.nrbrute=dialog.AddSlider("nrbrute nr",nrbrute,1,10,0) ask.OK = dialog.AddButton("OK",1) ask.Cancel = dialog.AddButton("Cancel",0) if dialog.Show(ask) > 0 then nrrandom=ask.nrrandom.value nrbrute=ask.nrbrute.value Doit() end end xpcall(GetParams,Cleanup)

Comments