Icon representing a recipe

Recipe: Gary Rebuild Worst NC v1&v2 Filter Toggle

created by Vredeman

Profile


Name
Gary Rebuild Worst NC v1&v2 Filter Toggle
ID
100467
Shared with
Public
Parent
None
Children
Created on
December 30, 2014 at 23:21 PM UTC
Updated on
December 30, 2014 at 23:21 PM UTC
Description

Added Ravs v1 in v2 and bits's overload filters

Best for


Code


--Gary Rebuild worst delta --A modified version of Walkin Rebuild V4 rav={} CG303={} game={} gary={ --[[ Push v2 randomly pushes then pulls one segment options at end ]]-- -- "lua v1 in v2" library by rav3n_pl --just add it in front of your v1 code and use v2 and v1 code in v2 scripts :) -- print(arg1[,...,argN]) no change :) function are_conditions_met() return current.AreConditionsMet() end function band_add_segment_segment(sgi1, sgi2) band.AddBetweenSegments(sgi1, sgi2) end function band_delete(bndIdx) if bndIdx~= nil then band.Delete(bndIdx) else band.DeleteAll() end end function band_disable(bndIdx) if bndIdx~=nil then band.Disable(bndIdx) else band.DisableAll() end end function band_enable(bndIdx) if bndIdx~=nil then band.Enable(bndIdx) else band.EnableAll() end end function band_set_length(bndIdx, len) band.SetGoalLength(bndIdx, len) end function band_set_strength(bndIdx, str) band.SetStrength(bndIdx, str) end function get_band_count() return band.GetCount() end function deselect_all() selection.DeselectAll() end function deselect_index(sgn) selection.Deselect(sgn) end function select_all() selection.SelectAll() end function select_index(sgn) selection.Select(sgn) end function select_index_range(sg1,sg2) selection.SelectRange(sg1,sg2) end function do_freeze(bbone,schain) freeze.FreezeSelected(bbone,schain) end function do_unfreeze_all() freeze.UnfreezeAll() end function do_global_wiggle_all(iters) structure.WiggleAll(iters,true,true) end function do_global_wiggle_backbone(iters) structure.WiggleAll(iters, true,false) end function do_global_wiggle_sidechains(iters) structure.WiggleAll(iters,false,true) end function do_local_rebuild(iters) structure.RebuildSelected(iters) end function do_local_wiggle(iters) structure.LocalWiggleSelected(iters,true,true) end function do_mutate(iters) structure.MutateSidechainsSelected(iters) end function do_shake(iters) structure.ShakeSidechainsSelected(iters) end function do_sidechain_snap(sgn, snap) rotamer.SetRotamer(sgn, snap) end function get_sidechain_snap_count(sgn) return rotamer.GetCount(sgn) end function load_structure() save.LoadSecondaryStructure() end function save_structure() save.SaveSecondaryStructure() end function quickload(slot) save.Quickload(slot) end function quicksave(slot) save.Quicksave(slot) end function get_exploration_score() return current.GetExplorationMultiplier() end function get_ranked_score() return current.GetScore() end function get_score() return current.GetEnergyScore() end function get_segment_distance(sg1,sg2) return structure.GetDistance(sg1,sg2) end function get_aa(sn) return structure.GetAminoAcid(sn) end function get_segment_count() return structure.GetCount() end function get_ss(sn) return structure.GetSecondaryStructure(sn) end function is_hydrophobic(sn) return structure.IsHydrophobic(sn) end function replace_aa(aa) for i=1,structure.GetCount() do if selection.IsSelected(i) then structure.SetAminoAcid(i, aa) end end end function replace_ss(ss) for i=1,structure.GetCount() do if selection.IsSelected(i) then structure.SetSecondaryStructure(i,ss) end end end function get_segment_score(sg) return current.GetSegmentEnergyScore(sg) end function get_segment_score_part(score_part,sg) return current.GetSegmentEnergySubscore(sg,score_part) end function reset_puzzle() puzzle.StartOver() end function restore_abs_best() absolutebest.Restore() end function restore_credit_best() creditbest.Restore() end function reset_recent_best() recentbest.Save() end function restore_recent_best() recentbest.Restore() end function set_behavior_clash_importance(ci) behavior.SetClashImportance(ci) end -- end of library -- function to copy class/table function CopyTable(orig) local copy = {} for orig_key, orig_value in pairs(orig) do copy[orig_key] = orig_value end return copy end -- functions for filters function FiltersOn() if behavior.GetSlowFiltersDisabled() then behavior.SetSlowFiltersDisabled(false) end end function FiltersOff() if behavior.GetSlowFiltersDisabled()==false then behavior.SetSlowFiltersDisabled(true) end end -- function to overload a funtion function mutFunction(func) local currentfunc = func local function mutate(func, newfunc) local lastfunc = currentfunc currentfunc = function(...) return newfunc(lastfunc, ...) end end local wrapper = function(...) return currentfunc(...) end return wrapper, mutate end -- function to overload a class -- to do: set the name of function classes_copied = 0 myclcp = {} function MutClass(cl, filters) classes_copied = classes_copied+1 myclcp[classes_copied] = CopyTable(cl) local mycl =myclcp[classes_copied] for orig_key, orig_value in pairs(cl) do myfunc, mutate = mutFunction(mycl[orig_key]) if filters==true then mutate(myfunc, function(...) FiltersOn() if table.getn(arg)>1 then -- first arg is self (function pointer), we pack from second argument local arguments = {} for i=2,table.getn(arg) do arguments[i-1]=arg[i] end return mycl[orig_key](unpack(arguments)) else --print("No arguments") return mycl[orig_key]() end end) cl[orig_key] = myfunc else mutate(myfunc, function(...) FiltersOff() if table.getn(arg)>1 then local arguments = {} for i=2, table.getn(arg) do arguments[i-1]=arg[i] end return mycl[orig_key](unpack(arguments)) else return mycl[orig_key]() end end) cl[orig_key] = myfunc end end end -- how to use: MutClass(structure, false) MutClass(band, false) MutClass(current, true) p = print --a short segmentCount = get_segment_count() segCnt=segmentCount function Score()--return score, negative too return get_score(true) 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 "how"=="s" then iters=1 end if iters>0 then iters=iters-1 local sp=Score() if how == "s" then do_shake(1) elseif how == "wb" then do_global_wiggle_backbone(25) elseif how == "ws" then do_global_wiggle_sidechains(25) elseif how == "wa" then do_global_wiggle_all(25) end local ig=Score()-sp if ig > minppi then return Wiggle(how, iters, minppi) end --to learn recursion you have to learn recursion ;] end end function FuseEnd() set_behavior_clash_importance(1) Wiggle() Wiggle("s",1) Wiggle() end function Fuze1(ci1,ci2) set_behavior_clash_importance(ci1) Wiggle("s",1) set_behavior_clash_importance(ci2) Wiggle("wa",1) FuseEnd() end function Fuze2(ci1,ci2) set_behavior_clash_importance(ci1) Wiggle("wa",1) set_behavior_clash_importance(1) Wiggle() set_behavior_clash_importance(ci2) Wiggle("wa",1) FuseEnd() end function PinkFuse() reset_recent_best() quicksave(4) -- store state before fuse Fuze1(0.1,0.7) quickload(4) -- load state before fuse Fuze1(0.3,0.6) quickload(4) -- load state before fuse Fuze2(0.5,0.7) quickload(4) -- load state before fuse Fuze2(0.7,0.5) restore_recent_best() end function qStab() --select_all() set_behavior_clash_importance(0.1) Wiggle("s",1) if fastQstab==false then set_behavior_clash_importance(0.6) Wiggle() set_behavior_clash_importance(1) Wiggle("s",1) end set_behavior_clash_importance(1) Wiggle() end bestScore=Score() function SaveBest() local g=Score()-bestScore if g>0 then p("Gained another ",round(g)," pts.") bestScore=Score() quicksave(3) end end --[[ Tlaloc`s math library ------------------------------------------------------------------------ The original random script this was ported from has the following notices: Copyright (c) 2007 Richard L. Mueller Hilltop Lab web site - http://www.rlmueller.net Version 1.0 - January 2, 2007 You have a royalty-free right to use, modify, reproduce, and distribute this script file in any way you find useful, provided that you agree that the copyright owner above has no warranty, obligations, or liability for such use. ------------------------------------------------------------------------ ]]-- local lngX = 1000 local lngC = 48313 local function _random(m,n) local A_Hi = 63551 local A_Lo = 25354 local M = 4294967296 local H = 65536 function _MWC() local S_Hi = math.floor(lngX / H) local S_Lo = lngX - (S_Hi * H) local C_Hi = math.floor(lngC / H) local F1 = A_Hi * S_Hi local F2 = (A_Hi * S_Lo) + (A_Lo * S_Hi) + C_Hi lngX = ((F2 - (math.floor(F2 / H) * H)) * H) + (A_Lo * S_Lo) + lngC - (C_Hi * H) lngX = lngX - (math.floor(lngX / M) * M) lngC = math.floor((F2 / H) + F1) return lngX end if n == nil and m ~= nil then n = m m = 1 end if (m == nil) and (n == nil) then return _MWC() / M else if n < m then return nil end return math.floor((_MWC() / M) * (n - m + 1)) + m end end local function _abs(value) if value < 0 then return -value else return value end end local function _floor(value) return value - (value % 1) end local function _randomseed(s) if s==nil then s=math.abs(get_score(true)) s=s%0.001 s=1/s while s<10000000 do s=s*10 end s=s-s%1 end lngX = s end math= { abs = _abs, floor = _floor, random = _random, randomseed = _randomseed, } math.randomseed() --[[ End math library ]]-- function down(x)--cut all after comma return x-x%1 end function round(x)--cut all afer 3-rd place return x-x%0.001 end function SaveRB(slot) quicksave(slot) restore_recent_best() SaveBest() quickload(slot) end bigBRK=0 function MakeBands(sgn,bands,center) local SC = get_segment_distance(sgn,center) local search=true local sg2, SR, RC local brk=0 repeat --try determine that band can push in good direction brk=brk+1 sg2=math.random(segmentCount) RC=get_segment_distance(sg2,center) SR=get_segment_distance(sg2,sgn) if SR<15 and RC<SC-3 and math.abs(sg2-sgn)>5 and math.abs(sg2-center)>3 then search=false end if brk>segmentCount then break end until search==false if brk<segmentCount then band_add_segment_segment(sgn, sg2) if push then SR=SR+pushDst --push x units else SR=SR-pushDst --pulls x units end if SR>20 then SR=20 end if SR<3 then SR=3 end band_set_length(get_band_count(), SR) bands=bands-1 ppoints[#ppoints+1]=sg2 else bigBRK=bigBRK+1 end if bands >0 and bigBRK<10 then return MakeBands(sgn,bands,center) end --make next band end ppoints={} function Push(sgn) local ssc=Score() if push then p("Pushing segment ",sgn, " score: ",round(ssc)) else p("Pulling segment ",sgn, " score: ",round(ssc)) end band_delete() quicksave(3) reset_recent_best() ppoints={} bigBRK=0 MakeBands(sgn,numBands,cntSeg) if get_band_count()>0 then if doPlatform then --stabilize pushing point for i=1,#ppoints do local sn=ppoints[i] for x=i,#ppoints do local sn2=ppoints[x] band_add_segment_segment(cntSeg, sn) band_set_length(get_band_count(), get_segment_distance(sn,cntSeg)) band_add_segment_segment(sn2, sn) band_set_length(get_band_count(), get_segment_distance(sn,sn2)) end end end if doFreeze>0 then local ss=sgn-doFreeze local se=sgn+doFreeze if ss<1 then ss=1 end if se>segmentCount then se=segmentCount end deselect_all() select_index_range(1,ss) select_index_range(se,segmentCount) do_freeze(true,false) end select_all() --deselect_all() --SelectAround(326,338,15) set_behavior_clash_importance(wiggleCI) Wiggle("wb",1) SaveRB(4) --maybe it found some already? set_behavior_clash_importance(1) band_delete() do_unfreeze_all() --deselect_all() --SelectAround(326,338,15) qStab() if doPF and Score()>bestScore-PFthreshold then PinkFuse() end SaveBest() quickload(3) local g=round(Score()-ssc) if g>0 then p("Push gain: ",g) end else p("Can`t make proper bands") end end function CanBeUsed(sn) local can=false if #alwaysUse>0 then for i=1,#alwaysUse do local ss=alwaysUse[i][1] local se=alwaysUse[i][2] if sn>=ss and sn<=se then can=true break end end else can=true end return can end function SelectAround(ss,se,radius) for i=1, segCnt do for x=ss,se do if get_segment_distance(x,i)<radius then select_index(i) break end end end end function RandomPush(pushes) if push==true then p("Pushing away from ",cntSeg) else p("Pulling to ",cntSeg) end for i=1,pushes do local search=true local sn local t=0 repeat t=t+1 sn=math.random(segmentCount) local dst=get_segment_distance(sn, cntSeg) if dst>minDist and CanBeUsed(sn) then search=false end if t>10 then break end until search==false if t<10 then Push(sn) end end end alwaysUse={ --always push that area/areas --{326,338}, --391 --{1,100}, --{150,170}, } cntSeg=0 --"center" segment global variable, segment to push from. now randomized numBands=5 --how many bands should push (max, sometimes less) wiggleCI=0.9 --pusching Clash Importance doPF=true --false --use pink fuze PFthreshold=-1 --PinkFuze if we so close to best score after qSatb fastQstab=false --true --only 1s 1w if true minDist=8 --minimum distance from "center" to push segment pushDst=4 --push/pull how far away doPlatform=false --true --makes additional bands to center to stabilize push points doFreeze=0 --10 --if positve freezes entire protein except for area around pushed segment -- set to 0 to disable freeze --Push(78) --run that way to push desired segment while true do cntSeg=math.random(segmentCount) push=true RandomPush(3) --10 pushes from that segment push=false RandomPush(3) --10 pulls to that segment end -- sort a table of talbes on element y -- of the embedded tables BubbleSort = function(x, y) s=gary.CopyScores(x) for i=1,#s - 1 do for j =#s, i + 1, -1 do if s[i][y] < s[j][y] then s[j], s[i] = s[i], s[j] end end end return s end, CopyScores = function(x) copy={} for i=1, #x do copy[i]={x[i][1], x[i][2]} end return copy end, GetSegScores = function() -- set of segment # and score at seg # local scores={} for i=1, game.segCount do --fill score table scores[i]={i, get_segment_score(i)} end return scores end, GetBadRange = function(segW,maxLen) -- get a set of segments up to maxLen with scores worst than segW local segWScore=get_segment_score(segW) local worstFound=true local len=1 local segS=segW local segE=segW while (len<maxLen) and worstFound do worstFound=false if segS>1 then if get_segment_score(segS-1) < segWScore then segS=segS-1 worstFound=true len=len+1 end end if (segE<game.segCount) and (len<maxLen) then if get_segment_score(segE+1) < segWScore then segE=segE+1 worstFound=true len=len+1 end end end return segS, segE end, ZeroScores = function() -- set of segment # and score at seg # local scores={} for i=1, game.segCount do --fill score table with zeroes scores[i]={i, 0} end return scores end, AddScores = function(s1,s2) -- s1 and s2 need to be same size and order -- no test is performed local delta={} for i=1, #s1 do delta[i]={s1[i][1],s1[i][2] + s2[i][2]} end return delta end, SubtractScores = function(s1,s2) -- s1 and s2 need to be same size and order -- no test is performed local delta={} for i=1, #s1 do delta[i]={s1[i][1], s1[i][2] - s2[i][2]} end return delta end } function CG303.detect_ligand(flag) --[[ ligand puzzle detection normally, segments have a secondary structure of "E", "H" or "L" and they always have a spatial distance of about 3.75 to 3.85 to their next index neighbour. a ligand is more far away. this function should respond true if this is a ligand puzzle, and false if it is not. if flag is nil, ligand auto-detection is enabled, distance of last two segments is checked if flag is not nil, ligand auto-detection is disabled, result is flag It also returns the last segment index which is no ligand ]]-- local flag=flag local LastPos=get_segment_count() -- fetch very last segment index number if flag==nil then -- print("Detecting if there is a ligand.") local ss=get_ss(LastPos) flag=not(ss=="L" or ss=="H" or ss=="E" ) -- if last segment's ss is neither "L" nor "H" nor "E" flag=flag or (get_segment_distance(LastPos-1,LastPos)>=3.9) -- or distance to second last segment is bigger or equal than 3.9 end -- if local os="This should " if flag then LastPos=LastPos-1 else os=os.."not " end -- if flag print(os,"be a ligand puzzle.") return flag,LastPos end -- function function Round(x)--cut all afer 3-rd place return x-x%0.001 end function SelectSphere(sg, radius,nodeselect) if nodeselect~=true then deselect_all() end for i=1, game.segCount do if get_segment_distance(sg,i)<radius then select_index(i) end end end function AllLoop() do_unfreeze_all() select_all() replace_ss("L") end mingain = 0.05 --minimum gain per 2 wiggle iterations buddies = 2 -- how many segments asida should be wiggled too (n, n+1 then n, n-1) -- ie worst segment is no 44 and buddies set to 1 then -- willging seg 43+44; later seg 44+45 function wig() local change=false ss=get_score(true) repeat sc=get_score(true) do_local_wiggle(4) se=get_score(true) wg=se-sc until wg<mingain es=get_score(true) if es<ss then restore_recent_best() else change=true end deselect_all() return change end function wiggle(s) local change=false deselect_all() do_unfreeze_all() if s+1<=game.segCount then select_index(s+1)end if s-1>=1 then select_index(s-1)end do_freeze(true, true) deselect_all() select_index(s) if wig() then change=true end if buddies > 0 then --select buddies for b=1, buddies do deselect_all() do_unfreeze_all() if s+b+1<=game.segCount then select_index(s+b+1)end if s-1>=1 then select_index(s-1)end do_freeze(true, true) deselect_all() if s+b>game.segCount then select_index_range(s,game.segCount) else select_index_range(s,s+b)end if wig() then change=true end deselect_all() do_unfreeze_all() if s+1<=game.segCount then select_index(s+1)end if s-b-1>=1 then select_index(s-b-1)end do_freeze(true, true) deselect_all() if s-b<1 then select_index_range(1,s) else select_index_range(s-b,s)end if wig() then change=true end deselect_all() do_unfreeze_all() end end return change end function Gibaj(jak, iters, minppi) --score conditioned recursive wiggle/shake if jak==nil then jak="wa" end if iters==nil then iters=8 end if minppi==nil then minppi=0.04 end if iters>0 then iters=iters-1 local sp=get_score(true) if jak == "s" then do_shake(1) elseif jak == "wb" then do_global_wiggle_backbone(25) elseif jak == "ws" then do_global_wiggle_sidechains(25) elseif jak == "wa" then do_global_wiggle_all(25) end local ep = get_score(true) local ig=ep-sp if ig > minppi then Gibaj(jak, iters, minppi) end end end function BlueFuse(locally) reset_recent_best() if locally ~=true then select_all() end set_behavior_clash_importance(.05) do_shake(1) set_behavior_clash_importance(1) Gibaj() set_behavior_clash_importance(.07) do_shake(1) set_behavior_clash_importance(1) Gibaj() restore_recent_best() set_behavior_clash_importance(.3) do_global_wiggle_all(1) set_behavior_clash_importance(1) Gibaj() restore_recent_best() end function Lws(mingain) --score conditioned local wiggle, local change=false set_behavior_clash_importance(1) if mingain==nil then mingain=1 end repeat --wiggles selected segments local ss=get_score(true) do_local_wiggle(4) local se=get_score(true) local wg=se-ss if wg<0 then restore_recent_best() else change=true end until wg<mingain return change end function AfterRebuild(lws, bf,locally) if lws==nil then lws=true end if bf==nil then bf=true end reset_recent_best() set_behavior_clash_importance(1) Gibaj("s",1) Gibaj("ws",1) Gibaj("s",1) Gibaj("ws",1) if lws then Lws(4) end if bf then BlueFuse(locally) end select_all() Gibaj() end function Rebuild(maxIters)--local rebuild until any change if maxIters==nil then maxIters=5 end local s=get_score(true) local i=0 repeat i=i+1 if i>maxIters then break end--impossible to rebuild! do_local_rebuild(i) until get_score(true)~=s if get_score(true)==s then return false else return true end end function LocalRebuild(ss,se,maxiters,sphere, lws, bf) local change=false deselect_all() select_index_range(ss,se) local sc=get_score(true) local ok=Rebuild(maxIters) if ok then for s=ss, se do SelectSphere(s,sphere,true) end AfterRebuild(lws,bf,true) end local gain =get_score(true)-sc if gain>0 then quicksave(3) print("Gained: ",get_score(true)-sc," NewScore: ",get_score(true)) change=true elseif gain<0 then quickload(3) else print("Unable to rebuild.") end return change end function Build(sSeg, eSeg, backward, len, maxiters, loopmode, sphere, lws, bf) if backward then for i=eSeg, sSeg,-1 do local ss=i-len+1 if ss>sSeg then LocalRebuild(ss,i,maxiters,sphere, lws, bf) end end else for i=sSeg, eSeg do local se=i+len-1 if se<game.segCount then LocalRebuild(i,se,maxiters,sphere, lws, bf) end end end end function gary.RebuildDelta(maxLen, maxiters, sphere, lws, bf, minChange, initRebuilds) local sscore = get_score(true) local rebuilds=initRebuilds print("Gary Rebuild Delta started. Score: ",Round(sscore)) print("delta calculated after ",initRebuilds," good rebuilds") reset_recent_best() quicksave(3) do_unfreeze_all() deselect_all() local oscores=gary.GetSegScores() local delta=gary.BubbleSort(oscores,2) local i=#delta local segW=delta[i][1] local segS, segE = gary.GetBadRange(segW,maxLen) repeat if segS~=segE then print("Working on sgmnts ",segS,"-",segE," with delta ", delta[i][2]) else print("Working on sgmnt ",segS," with delta ", delta[i][2]) end local oldScore=get_score(true) if LocalRebuild(segS,segE,maxiters,sphere, lws, bf) then if get_score(true) - oldScore > minChange then minChange=minChange / minChangeDelta if rebuilds>2 then rebuilds=rebuilds-1 print("Need ", rebuilds," more.") i=i-1 else rebuilds=initRebuilds for x=segS,segE do oscores[x][2]=get_segment_score(x) end nscores=gary.GetSegScores() delta=gary.BubbleSort(gary.SubtractScores(nscores,oscores),2) i=#delta end else i=i-1 end if i > 0 then segW=delta[i][1] segS, segE = gary.GetBadRange(segW,maxLen) end else minChange=minChange * minChangeDelta if segS < segE then segS, segE=gary.GetBadRange(segW,segE-segS) else i=i-1 if i > 0 then segW=delta[i][1] segS, segE = gary.GetBadRange(segW,maxLen) end end end until i==0 print("Total rebuild gain: ",Round(get_score(true)-sscore)) end function ReBuild(sSeg, eSeg, backward, minLen, maxLen, maxiters, loopmode, sphere, lws, bf) local sscore = get_score(true) print("Walking Rebuild started. Score: ",Round(sscore)) do_unfreeze_all() deselect_all() if loopmode then save_structure() AllLoop() end reset_recent_best() quicksave(3) for i=minLen, maxLen do Build(sSeg, eSeg, backward, i, maxiters, loopmode, sphere, lws, bf) end print("Total rebuild gain: ",Round(get_score(true)-sscore)) if loopmode then load_structure() end end --[[ ReBuild(sSeg, eSeg, backward, minLen, maxLen, maxiters, loopmode, sphere, lws, bf) sSeg- start segment to rebuild eSeg- end segment to rebuild backward- start from end minLen- minimum sgmnts len to rebuild (may be even 1) maxLen- maximum len to rebuild (over 7 seems useless) maxiters- maximum tries to rebuild loopmode- work in all-loop mode sphere- sphere size for shake/wiggle after rebuild lws- LWS sphere around (min gain 2pts, not LWSing totally) bf- BlueFuze sphere around minChange- minimum change to accept as a change initRebuilds- Rebuilds needed to calcualte delta. ]]-- game.isLigand, game.segCount=CG303.detect_ligand() sSeg=1 eSeg=game.segCount backward=false minLen=1 maxLen=2 maxiters=5 loopmode=true sphere=9 lws=true bf=true minChange=0.5 minChangeDelta=.99 initRebuilds=5 --ReBuild(sSeg, eSeg, backward, minLen, maxLen, maxiters, loopmode, sphere, lws, bf) gary.RebuildDelta(maxLen, maxiters, sphere, lws, bf, minChange, initRebuilds)

Comments